var elementArr = new Array();

function removeNewElements(f)
{
  var e;
  for ( ; elementArr.length > 0;)
  {
    e = elementArr.pop();
    f.removeChild(e);
  }
}

function addElementToForm(f, name, value)
{
    var e = document.createElement("input");
    e.setAttribute("type", "hidden");
    e.setAttribute("name", name);
    e.setAttribute("value", value);
    f.appendChild(e);
    elementArr.push(e);
    return e;
}

function addListToForm(f, list, name)
{
  var selected_key = false;
  for(i = 0; i < list.length; i++)
  {
    if (list[i].checked)
    {
	selected_key = true;
	addElementToForm(f, name, list[i].value);
    }
  }
  return selected_key;
}

function submitForm(f)
{
  f.submit();
  removeNewElements(f);
  return false;
}

function getSummaryViewTablesPost(eventName)
{
  var f = document.forms["eiusiteGetSearchOptionsForm"];

  var datasource = f.datasource.value;
  var country = document.getElementById("selectcountry").contentWindow.document.getElementsByName("country");
  var selected_key = addListToForm(f, country, "country");

  if (datasource == "citydata")
  { 
    var city = document.getElementById("selectcity").contentWindow.document.getElementsByName("city");
    if (addListToForm(f, city, "city"))
	selected_key = true;
    if (!selected_key && eventName == "") {
	alert("Please select a city or a country"); 
	return false;
    }
  } else
  {
    var region = document.getElementById("selectregion").contentWindow.document.getElementsByName("region");
    if (addListToForm(f, region, "region"))
	selected_key = true;
    if (!selected_key && eventName == "") {
	alert("Please select a country or a region"); 
	return false;
    }
  }

  var series3 = document.getElementById("selectseries").contentWindow.document.getElementsByName("series");
  var selected_series = addListToForm(f, series3, "series");
  if (!selected_series && eventName == "") {
	alert("Please select a series");
	return false;
  }
 
  var year = document.getElementById("selectyears").contentWindow.document.getElementsByName("year");
  if (year.length > 0) {
	yearoptions = year[0].options;
	for(i = 0; i < yearoptions.length; i++)
		if (yearoptions[i].selected == true)
			addElementToForm(f, "year", yearoptions[i].value);
   }

  if (eventName != "")
	addElementToForm(f, eventName, "1");

 
  return submitForm(f);
}

function getContinueViewTablesPost(eventName)
{
  var f = document.forms["eiusiteGetSearchOptionsForm"];
  if (eventName == "__save.x" && f.__searchname.value == "")
  {
     alert("Please enter a name for your search"); 
     return false;      
  }

  var PresetCheckVariable = "";
  var selected_series = false;

  var preselect = document.getElementById("selectpreselect").contentWindow.document.getElementsByName("preselect");
  for(i = 0; i < preselect.length; i++)
	if (preselect[i].checked)
		PresetCheckVariable = preselect[i].value;

  var series1 = document.getElementById("selectseries").contentWindow.document.getElementsByName("catid");
  if (addListToForm(f, series1, "catid"))
	selected_series = true;

  var series2 = document.getElementById("selectseries").contentWindow.document.getElementsByName("subcatid");
  if (addListToForm(f, series2, "subcatid"))
	selected_series = true;
  
  var series3 = document.getElementById("selectseries").contentWindow.document.getElementsByName("series");
  if (addListToForm(f, series3, "series"))
	selected_series = true;
  
  if ((eventName == "" || eventName == "__save.x") && PresetCheckVariable == "0" && !selected_series)
  {
	if (eventName == "__save.x")
	    alert("No criteria was selected, please go back and select a combination country/Series"); 
	else
	    alert("Please select a series"); 
	return false;      
  }
  else if (PresetCheckVariable != "")
  {
	addElementToForm(f, "preselect", PresetCheckVariable);
  }
  
  var year = document.getElementById("selectyears").contentWindow.document.getElementsByName("year");
  if (year.length > 0) {
	yearoptions = year[0].options;
	for(i = 0; i < yearoptions.length; i++)
		if (yearoptions[i].selected == true)
			addElementToForm(f, "year", yearoptions[i].value);
  }

  if (eventName != "")
	addElementToForm(f, eventName, "1");

  if (eventName == "__save.x")
  {
	f.target = "remote";
    addElementToForm(f, '__msgReturn', 'GetSearchOptions');
	submitForm(f);
	f.target = "_self";
	return false;
  }

  return submitForm(f);
}

function getContinueFormElementsPost()
{
  var selected_key = false;
  var freqSelected = "";

  var nextURL = "";
  var f = document.forms["eiusiteGetSearchOptionsForm"];

  var datasource = f.datasource.value;
  var country = document.getElementById("selectcountry").contentWindow.document.getElementsByName("country");
  if (addListToForm(f, country, "country"))
	selected_key = true;

  if (datasource == "citydata")
  { 
    var city = document.getElementById("selectcity").contentWindow.document.getElementsByName("city");
    if (addListToForm(f, city, "city"))
	selected_key = true;
    if (!selected_key) {
	alert("Please select a city or a country"); 
	return false;
    }
  } else
  {
    var aggregatecountry = document.getElementById("aggregatecountry").contentWindow.document.getElementsByName("country");
    if (addListToForm(f, aggregatecountry, "country"))
	selected_key = true;
    var region  = document.getElementById("selectregion").contentWindow.document.getElementsByName("region");
    if (addListToForm(f, region, "region"))
	selected_key = true;
    if (!selected_key) {
	alert("Please select a country or a region"); 
	return false;
    }
  }

  return submitForm(f);
}

function RecallProfile(act)
{
  var f = document.forms["eiusiteGetSearchOptionsForm"];
  var searchid = document.getElementById("searchid");
  if (searchid.value == "")
  {  
     alert("Please select a saved search"); 
     return false;      
  }
  var recallType = document.getElementById("recalltype");
  recallType.value = act;
  return true;
}

function change_data_viewx()
{
  document.forms["eiusiteWorkSpaceForm"].submit();
  return false;
}

function ChangeFrequency(freq)
{
  var f = document.forms["eiusiteWorkSpaceForm"];
  f.frequency.value = freq;
  if (typeof(f.date) != "undefined")
	ChangeDate("")
  else
  	f.submit();
  return false;
}

function ChangeCountry(country)
{
  var f = document.forms["eiusiteWorkSpaceForm"];
  for (i=0; i != f.country.length; i++) {
    f.country[i].selected = "false";
  }
  f.country[0].value = country;
  f.country[0].selected = "true";
  f.submit();
  return false;
}

function ChangeCity(city)
{
  var f = document.forms["eiusiteWorkSpaceForm"];
  for (i=0; i != f.city.length; i++) {
    f.city[i].selected = "false";
  }
  f.city[0].value = city;
  f.city[0].selected = "true";
  f.submit();
  return false;
}

function ChangeSeries(series)
{
  var f = document.forms["eiusiteWorkSpaceForm"];
  for (i=0; i != f.series.length; i++) {
    f.series[i].selected = "false";
  }
  f.series[0].value = series;
  f.series[0].selected = "true";
  f.submit();
  return false;
}

function ChangeDate(date)
{
  var f = document.forms["eiusiteWorkSpaceForm"];
  for (i=0; i != f.date.length; i++) {
    f.date[i].selected = "false";
  }
  f.date[0].value = date;
  f.date[0].selected = "true";
  f.submit();
  return false;
}

function getNewWindow(param)
{
   var f = document.forms["eiusiteWorkSpaceForm"];
   if (param == "excel") {
	f.target = "_blank";
	f.topic.value ="SelectExcel";
   }
   else {
	f.target ="chart";
	f.topic.value = "Chart";
   }
   f.ChartType.value = param;
   f.submit();

   f.target = "_self";
   f.topic.value = "ViewTables";
   
   return false;
}

function PrintWorkSpace()
{
 var windowx = document.getElementById("ViewTables").contentWindow;
 if (windowx.print)
 {
    windowx.focus();
    windowx.print();
 }
 return false;
}

function setswitchprotocol(param, val) {
	var field = document.getElementById(param);
	if(field) {
		field.value = val;
	}
}
