var rfCountryLocationHandler = {
	
	COUNTRY_LOCATION_SERVICE : "/libraries/handlers/country_location_handler.php",
	COUNTRY_REGION_SERVICE : "/libraries/handlers/country_region_handler.php",
	
	RequestCountryLocations : function(countryElementId, countryLocationElementId)
	{
	    	if(document.getElementById(countryElementId))
		{
			var countryEl = document.getElementById(countryElementId);
			var countryCode = countryEl.options[countryEl.selectedIndex].value;
			
			var httpRequest = rfCountryLocationHandler.CreateHttpRequest();
			
			//Prepare the URL
			var domain = "http://" + document.domain;
			var url = domain + rfCountryLocationHandler.COUNTRY_LOCATION_SERVICE + "?CountryCode=" + countryCode;
			
			//Add TimeStamp to Avoid IE Cache Issue
			var testDate=new Date();
			var millis = Date.UTC(testDate.getFullYear(),testDate.getMonth(),testDate.getDate(),testDate.getHours(),testDate.getMinutes(),testDate.getSeconds(),0);
			url+='&timeStamp='+millis;
			
			//Open the Connection
			httpRequest.open("GET",url,true);
			rfCountryLocationHandler.SetRequestHeaders(httpRequest, "GET", 0);
		    
		    	//Set up the callback function
			//To Avoid IE Re-Use Problem must appear between OPEN and SEND
			httpRequest.onreadystatechange = function()
			{
				rfCountryLocationHandler.HandleResponse(countryElementId, countryLocationElementId, httpRequest);
			}
			
			//Send the Initial Request
			httpRequest.send(null);	
		}
	},
	
	RequestCountryRegions : function(countryElementId, countryRegionElementId)
	{
	    	if(document.getElementById(countryElementId))
		{
			var countryEl = document.getElementById(countryElementId);
			var countryCode = countryEl.options[countryEl.selectedIndex].value;
			
			var httpRequest = rfCountryLocationHandler.CreateHttpRequest();
			
			//Prepare the URL
			var domain = "http://" + document.domain;
			var url = domain + rfCountryLocationHandler.COUNTRY_REGION_SERVICE + "?CountryCode=" + countryCode;
			
			//Add TimeStamp to Avoid IE Cache Issue
			var testDate=new Date();
			var millis = Date.UTC(testDate.getFullYear(),testDate.getMonth(),testDate.getDate(),testDate.getHours(),testDate.getMinutes(),testDate.getSeconds(),0);
			url+='&timeStamp='+millis;
			
			//Open the Connection
			httpRequest.open("GET",url,true);
			rfCountryLocationHandler.SetRequestHeaders(httpRequest, "GET", 0);
		    
		    	//Set up the callback function
			//To Avoid IE Re-Use Problem must appear between OPEN and SEND
			httpRequest.onreadystatechange = function()
			{
				rfCountryLocationHandler.HandleResponseRegions(countryElementId, countryRegionElementId, httpRequest);
			}
			
			//Send the Initial Request
			httpRequest.send(null);	
		}
	},
	
	HandleResponse : function(countryElementId, countryLocationElementId, httpRequest)
	{
		var status,statusText;
		if(httpRequest.readyState==4)
		{
		    	try
			{
			    status = httpRequest.status;
			    statusText = httpRequest.statusText;
			}
			catch(ex)
			{
			    status = 404;
			    statusText = "Closed";
			}
		
			if(status==301 || status==302)
			{
			    var lines = httpRequest.getAllResponseHeaders().split("\n");
			    eval(httpRequest.responseText);
			    return;				
			}
			
			if(status==403)
			{
			    eval(httpRequest.responseText);
			    return;
			}
			
			if(status!=200)
			{
			    return;
			}			
		
			if(httpRequest.responseXML)
			{
				//alert(httpRequest.responseText);
				if(document.getElementById(countryLocationElementId))
				{
					var countryLocationEl = document.getElementById(countryLocationElementId);
					
					//Cycle through and remove all options except the first one
					rfCountryLocationHandler.RemoveAllOptions(countryLocationEl);
					
					var curNodes = httpRequest.responseXML.getElementsByTagName("countrylocation");
					
					if(curNodes.length > 0)
					{
						var opEl = document.createElement('option');
						opEl.value = "NULL";
						opEl.innerHTML = "Please Select";						
						countryLocationEl.appendChild(opEl);
								
						for (var i = 0; i < curNodes.length; i++)
						{
							var opValue = rfCountryLocationHandler.CheckEmptyTag(curNodes[i].getElementsByTagName("countrylocationid"));
							var opText = rfCountryLocationHandler.CheckEmptyTag(curNodes[i].getElementsByTagName("title"));
							var addressText = rfCountryLocationHandler.CheckEmptyTag(curNodes[i].getElementsByTagName("address"));
							
							if(opText == '' && addressText != '')
								opText = addressText;
							
							if(opText != "")
							{
								opEl = document.createElement('option');
								opEl.value = opValue;
								opEl.innerHTML = opText;
								
								countryLocationEl.appendChild(opEl);
							}				
						}
						
						countryLocationEl.disabled = false;
					}
					else
					{
						var opEl = document.createElement('option');
						opEl.value = "NULL";
						opEl.innerHTML = "Select a country below";						
						countryLocationEl.appendChild(opEl);
						countryLocationEl.disabled = true;
						
						alert("No locations found for this country. Try another country or add locations to this country");	
					}
				}
			}
		}
	},
	
	HandleResponseRegions : function(countryElementId, countryRegionElementId, httpRequest)
	{
		var status,statusText;
		if(httpRequest.readyState==4)
		{
		    	try
			{
			    status = httpRequest.status;
			    statusText = httpRequest.statusText;
			}
			catch(ex)
			{
			    status = 404;
			    statusText = "Closed";
			}
		
			if(status==301 || status==302)
			{
			    var lines = httpRequest.getAllResponseHeaders().split("\n");
			    eval(httpRequest.responseText);
			    return;				
			}
			
			if(status==403)
			{
			    eval(httpRequest.responseText);
			    return;
			}
			
			if(status!=200)
			{
			    return;
			}			
		
			if(httpRequest.responseXML)
			{
				//alert(httpRequest.responseText);
				if(document.getElementById(countryRegionElementId))
				{
					var countryRegionEl = document.getElementById(countryRegionElementId);
					
					//Cycle through and remove all options except the first one
					rfCountryLocationHandler.RemoveAllOptions(countryRegionEl);
					
					var curNodes = httpRequest.responseXML.getElementsByTagName("countryregion");
					
					if(curNodes.length > 0)
					{
						var opEl = document.createElement('option');
						opEl.value = "NULL";
						opEl.innerHTML = "Please Select";						
						countryRegionEl.appendChild(opEl);
								
						for (var i = 0; i < curNodes.length; i++)
						{
							var opValue = rfCountryLocationHandler.CheckEmptyTag(curNodes[i].getElementsByTagName("countryregionid"));
							var opText = rfCountryLocationHandler.CheckEmptyTag(curNodes[i].getElementsByTagName("title"));
														
							if(opText != "")
							{
								opEl = document.createElement('option');
								opEl.value = opValue;
								opEl.innerHTML = opText;
								
								countryRegionEl.appendChild(opEl);
							}				
						}
						
						countryRegionEl.disabled = false;
					}
					else
					{
						var opEl = document.createElement('option');
						opEl.value = "";
						opEl.innerHTML = "Please Select";
						countryRegionEl.appendChild(opEl);	
						var opEl = document.createElement('option');
						opEl.value = "Other";
						opEl.innerHTML = "Other";					
						countryRegionEl.appendChild(opEl);
						countryRegionEl.disabled = false;
					}
				}
			}
		}
	},
	
	RemoveAllOptions : function(selectbox)
	{
		var i;
		for(i=selectbox.options.length-1;i>=0;i--)
		{
			//selectbox.options.remove(i);
			selectbox.remove(i);
		}
	},
	
	CheckEmptyTag: function(obj)
	{
		if(obj != null && obj.length > 0 && obj[0].hasChildNodes())
			return obj[0].firstChild.nodeValue;
		else
			return "";
	},	
	
	SetRequestHeaders : function(xmlHttpRequest, submissionMethod, contentLength)
	{
		xmlHttpRequest.setRequestHeader("X-Requested-With","XMLHttpRequest");
		xmlHttpRequest.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*");
		
		if(submissionMethod == "POST")
		{
		    xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    if(contentLength)
		        xmlHttpRequest.setRequestHeader("Content-length", contentLength);
		    xmlHttpRequest.setRequestHeader("Connection", "close");
		}
		else
		{
		    xmlHttpRequest.setRequestHeader("Connection","Keep-Alive");
		} 
	},
	
	AddEvent : function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener)
		{ 
			elm.addEventListener(evType, fn, useCapture); 
			return true; 
		}
		else if (elm.attachEvent)
		{ 
			var r = elm.attachEvent('on' + evType, fn); 
			return r; 
		}
		else
		{
		elm['on' + evType] = fn;
		}
	},
	
	CreateHttpRequest : function()
   	{
	    var xmlHttpRequest=null;
	    try
	    {
		    xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch(e)
	    {
		    try
		    {
			    xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    catch(ex)
		    {
			    xmlHttpRequest=null;
		    }
	    }
    	
	    if(!xmlHttpRequest&&typeof XMLHttpRequest!='undefined')
	    {
		    xmlHttpRequest=new XMLHttpRequest();
	    }
	    return xmlHttpRequest;
    	}
}

