function addCountryList(region){
	var countryList;
	if(region=="Asia and the Pacific"){countryList=EastAsiaandthePacific}
	else if(region=="Europe and Central Asia"){countryList=EuropeandCentralAsia}
	else if(region=="Latin America and the Caribbean"){countryList=LatinAmericaandtheCaribbean}
	else if(region=="Middle East and North Africa"){countryList=MiddleEastandNorthAfrica}
	else if(region=="South Asia"){countryList=SouthAsia}
	else if(region=="Sub-Saharan Africa"){countryList=SubSaharanAfrica}
	else{countryList=allCountries}
	if(countryList==null){
		return false;
	}
	for(i=0; i<countryList.length; i++){
		addNewEntry(countryFlt, countryList[i]);
		//country_flt.options.add(new Option(entry,entry))
	}
	return true;
}

function addRegionEntry(region_flt){
/*	region_flt.options.add(new Option("All",""));
	region_flt.options.add(new Option("Middle East and North America", "Middle"));
	region_flt.options.add(new Option("Asia and the Pacific", "Asia"));
	region_flt.options.add(new Option("Sub-Saharan Africa", "Sub"));
	region_flt.options.add(new Option("Latin America and the Caribbean", "Latin"));
	region_flt.options.add(new Option("Europe and Central Asia", "Europe"));
	region_flt.options.add(new Option("Pan-regional", "Pan"));
*/
	addNewEntry(region_flt,"All Regions")
	addNewEntry(region_flt,"Middle East and North Africa")
	addNewEntry(region_flt,"Asia and the Pacific")
	addNewEntry(region_flt,"Sub-Saharan Africa")
	addNewEntry(region_flt,"Latin America and the Caribbean")
	addNewEntry(region_flt,"Europe and Central Asia")
	addNewEntry(region_flt,"South Asia")
}

function addSectorEntry(sector_flt){
	addNewEntry(sector_flt,"All Sectors")
	addNewEntry(sector_flt,"Oil and Gas")
	addNewEntry(sector_flt,"Mining")
	addNewEntry(sector_flt,"Chemicals")
}


function addCountryEntry(country_flt){
	addCountryList("all");
}

//set the query string value to dropdown menus
function updateSeletionByURL(seletionObj, target){
	for(i=0; i<seletionObj.length;i++){
		if(seletionObj.options[i].value.toLowerCase()==target.toLowerCase()){
			seletionObj.selectedIndex=i;
			return true;
		}
	}
	return false;
}

function populateSelections(selObj, col){
	
	selObj.options.add(new Option("All",""))
	var existArr = new dhtmlxArray();
	for(var i=0;i<mygrid.getRowsNum();i++){
		var entry = mygrid.cells2(i,col).getValue();
		if(existArr._dhx_find(entry)==-1){
			selObj.options.add(new Option(entry,entry))
			existArr[existArr.length] = entry;
		}
	}
}


function populateCountry(selObj, col){
	selObj.options.add(new Option("All Countries","All Countries"))
	var existArr = new dhtmlxArray();
	for(var i=0;i<mygrid.getRowsNum();i++){
		var entry = mygrid.cells2(i,col).getValue()
		entry=entry.slice(entry.lastIndexOf('Country: ')+9, entry.lastIndexOf(')'))
		entry=entry.split(';')
		for(k=0;k<entry.length;k++){
			if(entry[k].charAt(0)==" "){
				entry[k]=entry[k].substr(1);
			}
			if(existArr._dhx_find(entry[k])==-1 && entry[k].charAt(0)!="<" && entry[k]!=""){
				selObj.options.add(new Option(entry[k],entry[k]))
				existArr[existArr.length] = entry[k];
			}
		}
	}
}

function populateRegion(country){
	var countryList
	for(i=0;i<regionList.length;i++){
		countryList=regionList[i][1];
		for(j=0;j<countryList.length;j++){
			
			if(countryList[j]==country){
				//contains the country we want
				
				return regionList[i][0]
			}
		}
	}	
	return false;
}

function addNewEntry(selObj,title_,value_){
	if(!(value_)){
		value_=title_;
	}
	var elOptNew = document.createElement('option');
	elOptNew.text = title_;
	elOptNew.value = value_;
	try {
		selObj.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		selObj.add(elOptNew); // IE only
	}
}


function searchBtn(criteria){
	if(changingCriteriaLocal(criteria)){
		searchBtn.removeAttribute("disabled")
	}
	else{
		searchBtn.setAttribute("disabled","disabled")
	}
}

function titleSearch(){
	tVal = tValObj.value.toString();
	var newQueryString = "?OpenDocument"
	if(tVal && tVal!="Search"){		newQueryString +=("&Title="+tVal)	}
	location.href = location.href.split("?")[0]+=(newQueryString);	
}


//enter key detection
$(function() {   
    $("input").keypress(function (e) {   
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {   
            titleSearch()
            return false;   
        } else {   
            return true;   
        }   
    });   
});  

function backToAll(c){
	clickCount++;
	getValue()
	
	if(clickCount==2 ){
		//triger region backAll when select 'All'
		if(c=='Region' && rVal=="All"){
			location.href = location.href.split("?")[0];
		}
		//triger country backAll when BOTH select 'All'
		if(c=='Country' && cVal=="All" && rVal=="All"){
			location.href = location.href.split("?")[0];
		}
	}
}





EastAsiaandthePacific=[
'All Countries',
'Cambodia',
'China',
'Fiji',
'Indonesia',
'Kiribati',
'Korea, Republic of',
'Lao PDR','Malaysia',
'Marshall Islands',
'Micronesia, Fed. States of',
'Mongolia',
'Myanmar',
'Palau',
'Papua New Guinea',
'Philippines',
'Samoa',
'Solomon Islands',
'Thailand','Timor-Leste',
'Tonga',
'Vanuatu',
'Vietnam']
					   
EuropeandCentralAsia=[
'All Countries',
'Albania',
'Armenia',
'Azerbaijan',
'Belarus',
'Bosnia and Herzegovina',
'Bulgaria',
'Croatia',
'Czech Republic',
'Estonia',
'Georgia',
'Hungary',
'Kazakhstan',
'Kyrgyz Republic',
'Latvia',
'Lithuania',
'Macedonia, FYR of',
'Malta',
'Moldova',
'Montenegro',
'Poland',
'Romania',
'Russian Federation',
'Serbia',
'Slovak Republic',
'Slovenia',
'Tajikistan',
'Turkey',
'Turkmenistan',
'Ukraine',
'Uzbekistan']


LatinAmericaandtheCaribbean=[
'All Countries',
'Antigua and Barbuda',
'Argentina',
'Bahamas, The',
'Barbados',
'Belize',
'Bolivia',
'Brazil',
'Chile',
'Colombia',
'Costa Rica',
'Dominica',
'Dominican Republic',
'Ecuador',
'El Salvador',
'Grenada',
'Guatemala',
'Guyana',
'Haiti',
'Honduras',
'Jamaica',
'Mexico',
'Nicaragua',
'Panama',
'Paraguay',
'Peru',
'St. Kitts and Nevis',
'St. Lucia',
'Trinidad and Tobago',
'Uruguay',
'Venezuela']


MiddleEastandNorthAfrica=[
'All Countries',
'Afghanistan',
'Algeria',
'Bahrain',
'Egypt, Arab Rep. of',
'Iran, Islamic Rep. of',
'Iraq',
'Jordan',
'Kuwait',
'Lebanon',
'Libya',
'Morocco',
'Oman',
'Qatar',
'Pakistan',
'Saudi Arabia',
'Syrian Arab Republic',
'Tunisia',
'United Arab Emirates',
'Yemen, Republic of']


SouthAsia=[
'All Countries',
'Bangladesh',
'Bhutan',
'India',
'Maldives',
'Nepal',
'Sri Lanka']

SubSaharanAfrica=[
'All Countries',
'Angola',
'Benin',
'Botswana',
'Burkina Faso',
'Burundi',
'Cameroon',
'Cape Verde',
'Central African Rep.',
'Chad',
'Comoros',
'Congo, Democratic Republic',
'Congo, Republic of',
"Cote d'Ivoire",
'Djibouti',
'Equatorial Guinea',
'Eritrea',
'Ethiopia',
'Gabon',
'Gambia, The',
'Ghana',
'Guinea',
'Guinea-Bissau',
'Kenya',
'Lesotho',
'Liberia',
'Madagascar',
'Malawi',
'Mali',
'Mauritania',
'Mauritius',
'Mozambique',
'Namibia',
'Niger',
'Nigeria',
'Rwanda',
'Sao Tome and Principe',
'Senegal',
'Seychelles',
'Sierra Leone',
'Somalia',
'South Africa',
'Sudan',
'Swaziland',
'Tanzania',
'Togo',
'Uganda',
'Zambia',
'Zimbabwe']

allCountries=[
"All Countries",
"World Region",
"Afghanistan","Africa Region","Albania","Algeria","Angola","Antigua and Barbuda","Argentina","Armenia","Asia Region",
"Azerbaijan","Bangladesh","Barbados","Belarus","Belize","Benin","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana",
"Brazil","Bulgaria","Burkina Faso","Burundi","Cambodia","CAMENA Region","Cameroon","Cape Verde","Caribbean Region",
"Central Africa Region","Central America Region","Central Asia","Central Asia Region","Central Europe",
"Central Europe Region","Chad","Chile","China","China, Hong Kong SAR","Colombia","Congo Republic",
"Congo, Democratic Republic of","Costa Rica","Cote D'Ivoire","Country Name Pending","Croatia","Czech Republic",
"Dominican Republic","East Asia and Pacific","East Asia and Pacific Region","Eastern Africa","Eastern Africa Region",
"Eastern Europe","Eastern Europe Region","Ecuador","Egypt","El Salvador","Eritrea","Estonia","Ethiopia",
"Europe Region","Fiji","Gabon","Gambia, The","Georgia","Ghana","Greece","Grenada","Guatemala","Guinea",
"Guinea-Bissau","Guyana","Haiti","Honduras","Hungary","India","Indonesia","Indonesia","Iran, Islamic Republic of",
"Iraq","Jamaica","Jordan","Kazakhstan","Kenya","Korea, Republic of","Kosovo","Kyrgyz Republic",
"Lao People's Democratic Republic","Latin America Region","Latvia","Lebanon","Lesotho","Liberia","Lithuania","Luxembourg",
"Macedonia, Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali",
"Marshall Islands","Mauritania","Mauritius","MENA Region","Mexico","Moldova","Mongolia","Montenegro","Morocco","Morrocco","Mozambique","Namibia","Nepal","Nicaragua","Niger","Nigeria","Oman","Pakistan","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Romania","Russian Federation","Rwanda","Saint Kitts and Nevis","Saint Lucia","Samoa","Saudi Arabia","Senegal","Serbia","Serbia and Montenegro","Seychelles","Sierra Leone","Slovakia","Slovenia","Solomon Islands","South Africa","South and Central America","Southeast Asia Region","Southern Africa Region","Southern Asia Region","Southern Europe","Southern Europe Region","Sri Lanka","Sudan","Swaziland","Syrian Arab Republic","Tajikistan","Tanzania, United Republic of","Thailand","Togo","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Uganda","Ukraine","United Arab Emirates","United States","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","West Bank and Gaza","Western Africa Region","World Region","Yemen, Republic of","Yugoslavia","Yugoslavia, Fed. Rep. of","Zambia","Zimbabwe"
/*
'Afghanistan',
'Albania',
'Algeria',
'Angola',
'Antigua and Barbuda',
'Argentina',
'Armenia',
'Azerbaijan',
'Bahamas, The',
'Bahrain',
'Bangladesh',
'Barbados',
'Belarus',
'Belize',
'Benin',
'Bhutan',
'Bolivia',
'Bosnia and Herzegovina',
'Botswana',
'Brazil',
'Bulgaria',
'Burkina Faso',
'Burundi',
'Cambodia',
'Cameroon',
'Cape Verde',
'Central African Rep.',
'Chad',
'Chile',
'China',
'Colombia',
'Comoros',
'Congo, Democratic Republic',
'Congo, Republic of',
'Costa Rica',
"Cote d'Ivoire",
'Croatia',
'Czech Republic',
'Djibouti',
'Dominica',
'Dominican Republic',
'Ecuador',
'Egypt, Arab Rep. of',
'El Salvador',
'Equatorial Guinea',
'Eritrea',
'Estonia',
'Ethiopia',
'Fiji',
'Gabon',
'Gambia, The',
'Georgia',
'Ghana',
'Grenada',
'Guatemala',
'Guinea',
'Guinea-Bissau',
'Guyana',
'Haiti',
'Honduras',
'Hungary',
'India',
'Indonesia',
'Iran, Islamic Rep. of',
'Iraq',
'Jamaica',
'Jordan',
'Kazakhstan',
'Kenya',
'Kiribati',
'Korea, Republic of',
'Kuwait',
'Kyrgyz Republic',
'Lao PDR','Malaysia',
'Latvia',
'Lebanon',
'Lesotho',
'Liberia',
'Libya',
'Lithuania',
'Macedonia, FYR of',
'Madagascar',
'Malawi',
'Maldives',
'Mali',
'Malta',
'Marshall Islands',
'Mauritania',
'Mauritius',
'Mexico',
'Micronesia, Fed. States of',
'Moldova',
'Mongolia',
'Montenegro',
'Morocco',
'Mozambique',
'Myanmar',
'Namibia',
'Nepal',
'Nicaragua',
'Niger',
'Nigeria',
'Oman',
'Pakistan',
'Palau',
'Panama',
'Papua New Guinea',
'Paraguay',
'Peru',
'Philippines',
'Poland',
'Qatar',
'Romania',
'Russian Federation',
'Rwanda',
'Samoa',
'Sao Tome and Principe',
'Saudi Arabia',
'Senegal',
'Serbia',
'Seychelles',
'Sierra Leone',
'Slovak Republic',
'Slovenia',
'Solomon Islands',
'Somalia',
'South Africa',
'Sri Lanka',
'St. Kitts and Nevis',
'St. Lucia',
'Sudan',
'Swaziland',
'Syrian Arab Republic',
'Tajikistan',
'Tanzania',
'Thailand','Timor-Leste',
'Togo',
'Tonga',
'Trinidad and Tobago',
'Tunisia',
'Turkey',
'Turkmenistan',
'Uganda',
'Ukraine',
'United Arab Emirates',
'Uruguay',
'Uzbekistan',
'Vanuatu',
'Venezuela',
'Vietnam',
'Yemen, Republic of',
'Zambia',
'Zimbabwe'
*/
]

regionList=[['Asia and the Pacific',EastAsiaandthePacific],
			['Europe and Central Asia',EuropeandCentralAsia],
			['Latin America and the Caribbean',LatinAmericaandtheCaribbean],
			['Middle East and North Africa',MiddleEastandNorthAfrica],
			['South Asia',SouthAsia],
			['Sub-Saharan Africa',SubSaharanAfrica]];

/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
	
	[Constructor]
	new Querystring([querystring]) 
	Creates a new Querystring object, optionally passing a string qs to parse. If qs is omitted, the querystring from the current page is used. 
	If querystring is passed, it should not begin with a "?". 
	// Parse the current page's querystring
	var qs = new Querystring()
	
	// Parse a given querystring
	var qs2 = new Querystring("name1=value1&name2=value2")
	
	[Methods]
	qs.get(name[, default_value]) 
	Returns the value of querystring parameter name if it exists, or default_value if it doesn't. 
	If default_value is omitted and parameter name doesn't exist, returns null. 
	var v1 = qs2.get("name1")
	var v3 = qs2.get("name3", "default value")
	
	Note: If a name appears more than once in a querystring only the last value is kept. 
	qs.contains(name) 
	Returns true if the querystring has a parameter name, else false. 
	if (qs2.contains("name1")){ alert(qs2.get("name1"));}

*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
// value=name if no value contained in query string		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}