/*
	This function is used to Create and Send the Request to Server Asynchronously
	inputs: 1: ServerURL to know to where the request is to be sent.
			2: Name of the Container
			3: Flag to know that the request is for Get or POST--> Ture for POST and False for GET
			4: OPTIONAL Parameters in the case of POST request
	OUTPut: Relays the Data accordingly			
*/

function CreateXMLHttpObjectAndProcess(SrvURL,ContainerId, flag, parameters)
  {
	var xmlHttp; 	// Used to store xmlHttp Object
	var Container	=	document.getElementById(ContainerId);	// Used to store the container of data.	
	 
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp	=	new XMLHttpRequest();

		if (xmlHttp.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            xmlHttp.overrideMimeType('text/html');
         }
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}//Closing of try
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}//Closing of Catch
	//-------------------------------------------------------------------------------------------
	xmlHttp.onreadystatechange=function()
	{
		try {
				if(xmlHttp.readyState==4)
				{ 
					if (xmlHttp.status == 200)
					{
						//If Container is a text box
						if(Container.type == "text")
							Container.value		=	xmlHttp.responseText;
						else
						{
							
							//if there is nothing, don't perform any action)
							if( xmlHttp.responseText != "" )
							{
								if(ContainerId	==	"divPageContents" || ContainerId	==	"divHCRContents")
								{
									//Split the Coming data into array and then insert that to the required locations.
									var aryData	=	xmlHttp.responseText.split("###__###");
	
									//Set the Meta Data Contents
									//Split the Coming Meta String into Array
									var aryMetaData	=	aryData[2].split("#~#__#~#");
									
									document.getElementsByTagName('meta')[1].content	=	aryData[0];
									document.getElementsByTagName('meta')[2].name		=	aryMetaData[1];
									document.getElementsByTagName('meta')[2].content	=	aryMetaData[2];
									document.getElementsByTagName('meta')[3].content	=	aryMetaData[3];
									document.getElementsByTagName('meta')[4].content	=	aryMetaData[4];
									document.getElementsByTagName('meta')[5].content	=	aryMetaData[5];
									document.getElementsByTagName('meta')[6].content	=	aryMetaData[6];
									document.getElementsByTagName('meta')[7].content	=	aryMetaData[7];
									document.getElementsByTagName('meta')[8].content	=	aryMetaData[8];
									
									//Set the Title of the page
									document.getElementsByTagName('title')[0].text		=	aryData[0];
									
									//Split the URL to get the Coming page Symbol Request and Further split it with & then =.
									$aryURL	=	SrvURL.split("?")[1].split("&")[0].split("=");
	
									//Set the Contents Heading but not in the case of Home
									if($aryURL[1]	!=	"Hm")
									{
										//Now set the Contents Heading of the page.
										document.getElementById("divHeadings").className	=	aryData[1];
									}// Closing of if($aryURL[1]	!=	"Hm")

									//Show the Contents
									Container.innerHTML	=	aryData[3];
	
								}// Closing of if(ContainerId	=	"divPageContents" || ContainerId	=	"divHCRContents")
								else
									Container.innerHTML	=	xmlHttp.responseText;
							}// Closing of if( xmlHttp.responseText == "" )
							else
									Container.innerHTML	=	"";
						}// Closing of else
					}//Closing of if (xmlHttp.status == 200)
/*					else
						alert('Sorry! We could not fulfill your request. The server could not be located or reached.');
*/				}//Closing of if(xmlHttp.readyState==4)
				else
				{
					//If Container is a text box
					if(Container.type == "text")
						Container.value		=	"<div align=\"center\">Loading...</div>";			
					//If container is Div
					else
						Container.innerHTML	=	"<div align=\"center\" class=\"Contents_txt\"><br /><img src='" + root + "images/loader.gif'>  Loading the contents, Please be patient....</div>";
				}
			}//End of try
		catch(e){}		
	}//Closing of Annonymous function
	//-------------------------------------------------------------------------------------------
	//Time stamp for cache problem
	var Stamp = new Date();

	//If the request is for post
	if(flag)
	{
	  
	  xmlHttp.open('POST', SrvURL, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);		
	}// Closing of if(url)
	else
	{
		//Make URL
		//Make sure that the URL has also query string part
		 if(SrvURL.indexOf("?") != -1)
			SrvURL = SrvURL+ "&TS="+ Stamp;
		 else  if(SrvURL.indexOf("?") == -1)
			SrvURL = SrvURL + "?TS="+ Stamp;
		
		//Open the Connection with the Server
		xmlHttp.open("GET",SrvURL,true);
		//Now finally send the Request
		xmlHttp.send(null);	
    }// Else
  }//Closing of CreateXMLHttpObjectAndProcess()