function bypassDetect() {
    var sep = (document.location.href.indexOf("?") > -1) ? "&" : "?";
    document.location = document.location.href + sep + "detectflash=false";
   }
 
function encode(txt) {
	/* just for now himmy
    txt = txt.toLowerCase();
    txt = escape(txt);
    txt = txt.replace(/\//g, "%2F");
    txt = txt.replace(/\?/g, "%3F");
    txt = txt.replace(/=/g, "%3D");
    txt = txt.replace(/&/g, "%26");
    txt = txt.replace(/@/g, "%40");
	/**/
    txt = txt.replace(/=/g, "_");
    txt = txt.replace(/&/g, ",");
    //var formata="format=format.htm?";
    //formata = formata + txt;
    //return formata;
	return txt;
   }


   function check_if_max_selected()
{
	var show_alert=1;//flag for showing the alert only once
	var num_selected=0;//counts the number of current selected options
	var tempSelected=new Array();
	var newSelected=new Array();
	for (var i=0;i<document.forms["articleform"]["countrycode"].length;++i)
	{
		if (document.forms["articleform"]["countrycode"][i].selected)
		{
			var found=0;//flag for if this option was already selected last time
			var option_status="new";
			for (var j=0;j<savedValues.length;++j)//look for it in saved values
			{
				if (savedValues[j]==i)//this was already selected last time
				{
					option_status="was selected";
					++num_selected;
					newSelected[newSelected.length]=i;
				}
			}
		}
		else
		{
			option_status="not selected";
		}
		tempSelected[i]=option_status;
	}

	for (var i=0;i<tempSelected.length;++i)//see which one of the new selected can be selected now
	{
		if ( (tempSelected[i]=="new") && (num_selected<MAX) )
		{
			++num_selected;
			newSelected[newSelected.length]=i;
		}
		else if (tempSelected[i]=="new")
		{
			if (show_alert)//to only show the alert once
			{
				alert("You can only select "+MAX+" values.");
				show_alert=0;
			}
			document.forms["articleform"]["countrycode"][i].selected=false;
		}
	}

	savedValues=newSelected;//assign to global array for next time
	return;
}


/*  
expects dates in the format yyyy-mm-dd checks whetere a to date is greater
than a from date.
check_bounds = flag if set to yes checks the from and to to be within a timeframe boundary set by:
st_limit and nd_limit. If the from and to are out of the boundaries it checks with the user whether to use these boundary values instead or stop submision.
returns 1 if successful;
*/
function check_range(form,from,to,check_bounds,st_limit,nd_limit){
	var start = document.forms[form].from.value.replace(/-/gim,"");
	var end = document.forms[form].to.value.replace(/-/gim,"");
	var data_begin = st_limit.replace(/-/gim,"");
	var data_end = nd_limit.replace(/-/gim,"");
	var st_ans = 0;
	var en_ans = 0;
	//TODO have a smart regex that checks if start or end are strings or numbers
	if (start =="from" || end =="to"){
		alert("please select a valid date");
		return 0;
	}else if (start > end){
		alert("The start date is greater than the end date, please choose a suitable date");
		return 0;
	}else{
		if (check_bounds){
			var st_ans = 1;
			var en_ans = 1;		
			if(start < data_begin){
				var st_ans = confirm("We don't have data prior to "+st_limit+". Do you want to make this your start date?");
				if (st_ans){
					document.forms[form].from.value = st_limit;
				}else{
					st_ans=0;
					return 0;
				}
			}
			if(end > data_end){
				var en_ans = confirm("We don't have data after "+nd_limit+". Do you want to make this your end date?");
				if (en_ans){
					document.forms[form].to.value = nd_limit;
				}else{
					en_ans=0;
					return 0;
				}
			}
		}
		if (st_ans && en_ans){
			return 1;
		}
	}
	return 0;
}

/* 
returns the selected value of a radio list.
expects a form and field name 
*/
function getRadioValue(nform,field){
	for (var i=0; i < eval('document.forms[nform].'+field+'.length'); i++){
		if (eval('document.forms[nform].'+field+'[i].checked')){
			var rad_val = eval('document.forms[nform].'+field+'[i].value');
			return  rad_val
		}
	}
}

//changed ~ for - cause of the codes syntax 
function encodew(txt) {
    txt = txt.replace(/=/g, "~");
    txt = txt.replace(/&/g, ",");
    return txt;	
}


function get_dropdown_request(form,DROP_DOWN){

	var category = document.getElementById('category').value;
	var rt = getRadioValue(form,'rt');
	var date_to_from = new Array();
	var date_range = new Array();
	if (rt > 0){
	   var period = document.getElementById('period').value;
	   var today = new Date();  
	   var year = today.getFullYear();
	   //alert(year);
	   if (period != 1){
		   if (period == 0){
			   period = year - 2000;
		   }
		   //alert("period" + period);
		   date_to_from.push(year - period);
		   date_to_from.push(year);
		   date_range = interpolate_range(date_to_from);
	   }
	   else{
		   //alert("year 1 = " + (year - 1));
		   date_range.push(year - 1);
	   }
	}
	else{
		if (
			document.getElementById('startdate').value 
			== document.getElementById('enddate').value 
			){
				date_range.push(document.getElementById('enddate').value);
		}
		else{
			date_to_from.push(document.getElementById('startdate').value);
			date_to_from.push(((document.getElementById('enddate').value*1)+1));
			date_range = interpolate_range(date_to_from);
		}
	}
	// return the category name part we are interested
	// basicfacts.0.population to population.
	var cat = category.match(/\.\d\..*$/);
	var str = ""+cat;
	category = str.substring(3);
	var categories = new Array();
	for (var i = 0; i < date_range.length; i++ ){
		//alert("DATE RANGE" + date_range[i]);
		categories.push(category+""+date_range[i]);
	}
	//alert(category + " " + rt + " " + date_range + "");
	//alert("CATEGORIES" + categories);
	var countries = merge_unique(DROP_DOWN,categories);
	countries =	remove_duplicate_est(countries,"*");
	delete_select('countrycode_unsel');
	countries = check_selected('countrycode',countries);
	append_to_select('countrycode_unsel',countries[0]);
	delete_select('countrycode');
	append_to_select('countrycode',countries[1]);
	//return categories;
}

function interpolate_range(years){
//alert("years"+years);
var count = years[1] - years[0];
//alert("count "+count);
var date_range = new Array();
	for (var i = 0; i < count; i++){
		var year = years[0] * 1; // make sure is a number
		date_range.push(year+i);	
	}
//alert(" DATE RANGE" + date_range);
return date_range;
}

/*
requires /bin/js/array.js for the unique prototype
compares a set of multidimensional arrays nodes.
the nodes are a list of items delimited by commas
it returns an array with unique list of items
expects an XML Dom Object and an array with the nodenames to compare
*/
function merge_unique(oDomDoc,tagname){
	
	var countries = new Array();
	var countries_sets = new Array();
	var ex_c_set = new Array(); // exploded country set
	if (tagname.length < 1){
		return;
	}
	for (var i = 0; i < tagname.length ; i++ ){
		countries_sets.push( push_XML_node(oDomDoc,tagname[i]) );
	}
	for (var a =0; a < countries_sets.length; a++){	// c_arr = country array
	//alert("lopping array" + countries_sets[a]);	
		for ( var i = 0 ; i < countries_sets[a].length ; i++ ){
			//alert("lopping array content" + countries_sets[a][i]);
			ex_c_set = countries_sets[a][i];
			//alert("Stuuf "+ ex_c_set);
			ex_c_set = ex_c_set.replace(/~/g, "&"); // XML encoded
			ex_c_set = ex_c_set.split(",");
			countries = countries.concat(ex_c_set);
		}
	}
	//alert("countries all" + countries);
	countries = countries.unique(true);
	//alert("unique " + countries);
	return countries.sort();
}

/*
returns an array with the text nodes of the specified tagname
*/
function push_XML_node(oDomDoc,tagnames){
	var res = new Array();
	var msg ="";
	var x=oDomDoc.getElementsByTagName(tagnames);
	for (var i=0;i<x.length;i++){
		res.push(x[i].childNodes[0].nodeValue);
		msg +=  x[i].childNodes[0].nodeValue;
	}
	//alert("TAG" + tagnames +" "+msg);
	return res;
}
/*
items is an array of name-value items
*/
function append_to_select(id,items,selected){
	var item = new Array();
	if (items == undefined){
		return;
	}
	var select = document.getElementById(id);
	for (var i = 0; i < items.length ; i++ ){
		var string = items[i];
		item = get_select_arr(string);
		select.options[select.options.length]
			=new Option(item[0],item[1],(i == selected?true:false),(i == selected?true:false));
		;
	}
}
/*
removes items from a select
id = select  id
items = array of items[0] = name-value
*/
function check_selected(id,items){
	var select = document.getElementById(id);
	var item = new Array();
	var to_keep = new Array();
	for (var b= 0 ; b < select.options.length ; b++ ){
		//alert("option"+select.options[b].value);
		for (var i = 0; i < items.length ; i++ ){
			//alert("item before" + items[i]);
			item = get_select_arr(items[i]);
			//alert("item" + item[0]);
			if (select.options[b].value == item[1]){
				//alert("ITEM EXISTS ON RHS " + item[0] );
				//alert("to keep adding" + items[i] );		
				// remove select	
				to_keep.push(items[i]);
				items.splice(i,1);
				//prompt("items now" + items);
			}
		}
	}
	return [items, to_keep];
}
/*
splits a string delimied by - 
*/
function get_select_arr(string){
	if (string == undefined){
		return; 
	}
	var arr = new Array();
	arr = string.split("-");
	return arr;
}
/*
deletes all items of a select
*/
function delete_select(id){
	var select = document.getElementById(id);
	select.options.length = 1;
	for (var b= 0 ; b < select.options.length ; b++ ){
		select.options[b] = null;
	}
}

// code repetition on populate_year_selects and get_selects 
function populate_year_selects(id_srt,id_end,begin_date){
	var today = new Date();  
	var this_year = today.getFullYear();
	var date_range = new Array();
	
	// interpolate dates
	date_range = interpolate_range([begin_date,this_year]);
	
	// create select name-value
	for (var i = 0; i < date_range.length ; i++ ){
		date_range[i] = date_range[i]+"-"+date_range[i]; 
	}

	// delete and append
	delete_select(id_srt);
	append_to_select(id_srt,date_range);
	delete_select(id_end);
	var enddate = new Array();
	enddate = date_range.reverse();
	//enddate.pop();
	append_to_select(id_end,enddate);
}

function prepare_date_dropdown(arr){
	for (var i = 0; i < arr.length ; i++ ){
		arr[i] = arr[i]+"-"+arr[i]; 
	}
	return arr;
}

function get_selects(id_srt,id_end,begin_date){
	var today = new Date();  
	var this_year = today.getFullYear();
	var date_range = new Array();
	
	// interpolate dates
	date_range = interpolate_range([begin_date,this_year]);
	
	var res = new Array();
	var srt = date_range.join("|");
	var enddate = new Array();
	enddate = date_range.reverse();
	//enddate.pop();
	var end = enddate.join("|");

	res[id_srt] = srt;
	res[id_end] = end;
	return res;
}

function make_date_range_valid(id_srt,id_end,this_id){
	
	var sel_drp = document.getElementById(this_id);
	var res = new Array();
	var boxes = new Array();
	var b_options = new Array; // the new options for box to susb
	var bs =(this_id == id_srt)?id_end:id_srt; // box to susbtract from
	var as =(this_id == id_srt)?id_end:id_srt; // box to susbtract from
	var as_obj = document.getElementById(as);
	var as_sel_id;

	res = get_selects(id_srt,id_end,2000);
	boxes[id_srt] = res[id_srt].split("|"); 
	boxes[id_end] = res[id_end].split("|"); 
	//alert("start" + boxes[id_srt] + "end" + boxes[id_end] + "box to remove" + bs + "this value " + sel_drp.value );
		for (var b = 0 ; b < boxes[bs].length; b++){
			//alert(" val" + sel_drp.value + "year " + boxes[bs][b] );
			if (boxes[bs][b] >= sel_drp.value){
				b_options.push(boxes[bs][b]);
				if (boxes[bs][b] == as_obj.value){
					as_sel_id = b;
				}
			}
		}
	//alert(as_sel_id);
	//alert(as_obj.value);
	if (b_options.length < 1 ){
		b_options.push(sel_drp.value);
	}
	b_options = prepare_date_dropdown(b_options);
	delete_select(bs);
	append_to_select(bs,b_options,as_sel_id);
}
/*
remove a section of a string from the index - before index 
till the after index.
uses prototype's to.Array.
i	= index,
bi	= before_index,
ai	= after_index
*/
String.prototype.removeSection = function(i,bi,ai){
	var str2Arr = new Array();
	str2Arr = this.toArray();
	str2Arr.splice((i-bi),ai);
	//alert(str2Arr.join(""));
	this.value = str2Arr.join("");
	return this.value;
	/*var msg ="";
	for (i in this ){
		msg += "value" + i + " " + this[i] + "\n";
	}
	prompt("msg" + msg);
	*/
}

/*
looks for a pattern i.e * if it finds it it looks if the item
is repeated on the array without the pattern
france *
france
it removes france from the array
*/

function remove_duplicate_est(arr,pattern){
	var indexes = arr.indexOfPattern(pattern,0);
	for (var i = 0; i < indexes.length ; i ++ ){
		var index = arr[i];
		var pattern_index = arr[i].indexOf(pattern);
		index = index.removeSection(pattern_index,1,2);
		arr.splice(arr.indexOf(index),1);
		document.getElementById("est").innerHTML = "* Denotes that the data of at least one year is an estimate.";
	}
	if (indexes.length < 1){
		document.getElementById("est").innerHTML = "";
	}
	return arr;
}


function getRadioValue(nform,field){
	for (var i=0; i < eval('document.forms[nform].'+field+'.length'); i++){
		if (eval('document.forms[nform].'+field+'[i].checked')){
			var rad_val = eval('document.forms[nform].'+field+'[i].value');
			return  rad_val
		}
	}
}