// declare a global  XMLHTTP Request object
var XmlHttpObj;
var fl_study1_country;
var fl_study1_region;
var fl_study1_city;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function study_CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}


//START STUDY 1
// called from onChange or onClick event of the continent dropdown list
function study1_CountryListOnChange(pfl_country,pfl_region,pfl_city) 
{
    fl_study1_country = pfl_country;
    fl_study1_region  = pfl_region;
    fl_study1_city    = pfl_city;
    window.status = "Loading Studies .....";	
    //var Geo_Country = document.getElementById("Geo_Country");
    var Geo_Country = document.getElementById(fl_study1_country);
    // get selected continent from dropdown list
    var selectedCountry = Geo_Country.options[Geo_Country.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
    s = current_date_txt();
    requestUrl = "modmysite/worldcities/xml_data_provider_study1.php" + "?action=r&country=" + encodeURIComponent(selectedCountry) + "&date=" + s;
    
	study_CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = study1_RegionStateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function study1_RegionStateChangeHandler()
{
	//alert(fl_country + fl_region + fl_city);
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			study1_PopulateRegionList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Region dropdown list
function study1_PopulateRegionList(RegionNode)
{
	
    	//var RegionList = document.getElementById("Geo_Region");
    	var RegionList = document.getElementById(fl_study1_region);
	// clear the Region list 	
	for (var count = RegionList.options.length-1; count >-1; count--)
	{
		RegionList.options[count] = null;
	}
		
	
	var RegionNodes = RegionNode.getElementsByTagName('region_study1');
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	
	for (var count = 0; count < RegionNodes.length; count++)
	{
   		textValue = GetInnerText(RegionNodes[count]);
		idValue = RegionNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		RegionList.options[RegionList.length] = optionItem;
	}
	window.status = "";
}
//END STUDY 1


//START STUDY 2


var fl_study2_country;
var fl_study2_region;
var fl_study2_city;

// called from onChange or onClick event of the continent dropdown list
function study2_CountryListOnChange(pfl_country,pfl_region,pfl_city) 
{
    fl_study2_country = pfl_country;
    fl_study2_region  = pfl_region;
    fl_study2_city    = pfl_city;
    window.status = "Loading Studies .....";	
    //var Geo_Country = document.getElementById("Geo_Country");
    var Geo_Country = document.getElementById(fl_study2_country);
    // get selected continent from dropdown list
    var selectedCountry = Geo_Country.options[Geo_Country.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
    s = current_date_txt();
    requestUrl = "modmysite/worldcities/xml_data_provider_study2.php" + "?action=r&country=" + encodeURIComponent(selectedCountry) + "&date=" + s;
    //alert(requestUrl);
	study_CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = study2_RegionStateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function study2_RegionStateChangeHandler()
{
	//alert(fl_country + fl_region + fl_city);
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			study2_PopulateRegionList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Region dropdown list
function study2_PopulateRegionList(RegionNode)
{
	
    	//var RegionList = document.getElementById("Geo_Region");
    	var RegionList = document.getElementById(fl_study2_region);
	// clear the Region list 	
	for (var count = RegionList.options.length-1; count >-1; count--)
	{
		RegionList.options[count] = null;
	}
		
	
	var RegionNodes = RegionNode.getElementsByTagName('region_study2');
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	
	for (var count = 0; count < RegionNodes.length; count++)
	{
   		textValue = GetInnerText(RegionNodes[count]);
		idValue = RegionNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		RegionList.options[RegionList.length] = optionItem;
	}
	window.status = "";
}

//END STUDY 2




//START STUDY looking 1


var fl_studyL1_country;
var fl_studyL1_region;
var fl_studyL1_city;

// called from onChange or onClick event of the continent dropdown list
function studyL1_CountryListOnChange(pfl_country,pfl_region,pfl_city) 
{
    fl_studyL1_country = pfl_country;
    fl_studyL1_region  = pfl_region;
    fl_studyL1_city    = pfl_city;
    window.status = "Loading Studies .....";	
    //var Geo_Country = document.getElementById("Geo_Country");
    var Geo_Country = document.getElementById(fl_studyL1_country);
    // get selected continent from dropdown list
    var selectedCountry = Geo_Country.options[Geo_Country.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
    s = current_date_txt();
    requestUrl = "modmysite/worldcities/xml_data_provider_studyL1.php" + "?action=r&country=" + encodeURIComponent(selectedCountry) + "&date=" + s;
    
	study_CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = studyL1_RegionStateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function studyL1_RegionStateChangeHandler()
{
	//alert(fl_country + fl_region + fl_city);
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			studyL1_PopulateRegionList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Region dropdown list
function studyL1_PopulateRegionList(RegionNode)
{
	
    	//var RegionList = document.getElementById("Geo_Region");
    	var RegionList = document.getElementById(fl_studyL1_region);
	// clear the Region list 	
	for (var count = RegionList.options.length-1; count >-1; count--)
	{
		RegionList.options[count] = null;
	}
		
	
	var RegionNodes = RegionNode.getElementsByTagName('region_studyL1');
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	
	for (var count = 0; count < RegionNodes.length; count++)
	{
   		textValue = GetInnerText(RegionNodes[count]);
		idValue = RegionNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		RegionList.options[RegionList.length] = optionItem;
	}
	window.status = "";
}

//END STUDY looking 1



// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function yesno(msg)
{
var truthBeTold = confirm(msg);
if (!truthBeTold)
   {
   alert("CANCELLED!");
   return false;
   }
return true;
}

function current_date_txt()
{
	 var d,s;
	 d = new Date();
	 s = (d.getMonth() + 1) + '_' ;
  	 s += d.getDate()  + '_' ;
  	 s += d.getFullYear() + '_' ;
	 s += d.getHours() + '_' ;
  	 s += d.getMinutes() + '_' ;
  	 s += d.getSeconds();
  	 return s;
}	



