/* Ajax Class To Initialize and Create Http Object */
function AjaxClass()
{
	/* Initialising class variables with blank value */
	this.url = '';
	this.method = '';
	this.params = '';
	
	/* Function to create a request object */
	this.createRequestObject = function() 
	{
	/* Initialising the javascript memory variable xmlhttp */
		var xmlhttp = false;
		
	/* Try and catch block for creating xmlhttp object according to the browser */
		try
		{
		/* The xmlhttp object is built into the Microsoft XML Parser. */
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			/* Try and catch block for creating xmlhttp object according to the browser */
			try 
			{
			/* The xmlhttp object is built into the Microsoft IE. */
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				xmlhttp = false;
			}
		}
		/* The xmlhttp object is built into the browsers other than Microsoft IE. */
		if (!xmlhttp && typeof XMLHttpRequest!='undefined')
		{
			xmlhttp = new XMLHttpRequest();
		}
		/* To return http request object */
		return xmlhttp;
	}
	
	/* Function to send the http request */
	this.ajaxRequest = function() 
	{
		/* Creating the object by invoking the createRequestObject() function. */
		var ajaxRequestObject = this.createRequestObject();
	
		/* Preparing the HTTP request */
		/* If the http request method is POST */
		if(this.method == 'POST')	
		{
			/* Open the http request connection to send post values */
			ajaxRequestObject.open("POST", this.url, true);
			/* To send Http request headers */
			ajaxRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			/* To send the http request params */
			ajaxRequestObject.send(this.params); 
		}
		/* If the http request method is GET */
		else if(this.method == 'GET') 
		{
			/* Open the http request connection to send get values */
			ajaxRequestObject.open("GET", this.url+'?'+this.params, true);
			/* To send the http request params as NULL */
			ajaxRequestObject.send(null);
		}		
	/* To return http request object */
	return ajaxRequestObject;
	}
}

/* Function to get all the key value pair of the form elements */
function ajaxPost(objFormName)	
{
	/* Initialize the post string with blank value */
	var strPost = '';
	/* For loop to get form elements values in key value pair format */
	for(cntFields=0; cntFields<objFormName.elements.length; cntFields++)	
	{
		/* To check is the form element is a radio or check box */
		if( objFormName.elements[cntFields].type == "radio" || objFormName.elements[cntFields].type == "checkbox" )
		{
			/* To get the values of selected radio button or check box */
			if( objFormName.elements[cntFields].checked == true )
			{ 
				/* To append all  the variable in the name=value format */
				strPost += objFormName.elements[cntFields].name+"="+objFormName.elements[cntFields].value+"&";
			}
		}
		
		/* To check is the form element is not a radio or check box */
		if( objFormName.elements[cntFields].type != "radio" && objFormName.elements[cntFields].type != "checkbox"  )
		{
			/*
			To append all the variable in the name=value format
			To get values from other form elements used in the question
			*/
			strPost += objFormName.elements[cntFields].name+"="+ replaceChars( objFormName.elements[cntFields].value ) +"&";
			//strPost += objFormName.elements[cntFields].name+"="+ objFormName.elements[cntFields].value  +"&";
		}
	}
	/* To return str of form elements with key value pair format  */
	return strPost;
}

/* Function to replace the special character passed in the ajax processing file via post */
	function replaceChars(entry) 
	{
		/* To assign the regular expression rule to the memory variable */
		re = /^\$|&|{|}|(<\/)|<|>|;/g;
		/* To return the formated string */
		return entry.replace( re, "");
	}

function populateCity(objForm, selectedID)
{
	/* Init ajax global class */
	objAjaxReq = new AjaxClass();
	/* Ajax processing file */
	objAjaxReq.url = HTTP_SERVER + 'ajaxGetCity.php';
	/* Ajax http request method */
	objAjaxReq.method = 'POST';
	/* Ajax http request method parameters as the request method is POST */
	objAjaxReq.params = ajaxPost(objForm);	
	/* Ajax http request function which creates http request object */
	var objHttpRequest = objAjaxReq.ajaxRequest();
	
	objHttpRequest.onreadystatechange = function ()	
	{/* If http reponse data is received correctly */
		if ((objHttpRequest.readyState == 4) && (objHttpRequest.status == 200))
		{
			/* To get http text response*/
			var strReponseResult = objHttpRequest.responseText;
			//alert(strReponseResult);

			/* To get object Http XML response */
			var xmlResponse = objHttpRequest.responseXML.documentElement;
			
			var objCmbCity = objForm.cmbCity;
			
			objCmbCity.options.length = 0;
			objCmbCity.options[0] = new Option("- - - - Select - - - -", "");
			
			if( xmlResponse.getElementsByTagName('CityID').length > 0 )
			{
				for(var cnt = 0; cnt < xmlResponse.getElementsByTagName('CityID').length; cnt++)
				{
					/* To get the Vertical ID from the XML response and assign to the memory variable */
					var CityID= xmlResponse.getElementsByTagName('CityID').item(cnt).firstChild.nodeValue;
					/* To get the Vertical Name from the xml response and assign to the memory variable */
					var CityName = xmlResponse.getElementsByTagName('CityName').item(cnt).firstChild.nodeValue;
	
					objCmbCity.options[cnt+1] = new Option(CityName,CityID);
				}
			}
			
			if (selectedID != '')
			{
				objCmbCity.value = selectedID;
			}
		}
	}
}