/**
 * Ajax.js
 *
 * Collection of Scripts to allow in page communication from browser to (struts) server
 * ie can reload part instead of full page
 *
 * How to use
 * ==========
 * 1) Call retrieveURL from the relevant event on the HTML page (e.g. onclick)
 * 2) Pass the url to contact (e.g. Struts Action) and the name of the HTML form to post
 * 3) When the server responds ...
 *		 - the script loops through the response , looking for <span id="name">newContent</span>
 * 		 - each <span> tag in the *existing* document will be replaced with newContent
 *
 * NOTE: <span id="name"> is case sensitive. Name *must* follow the first quote mark and end in a quote
 *		 Everything after the first '>' mark until </span> is considered content.
 *		 Empty Sections should be in the format <span id="name"></span>
 */

//global variables
 
  var which;


function GetXmlHttpObject(){
	
	try{
	var xmlHttp=null;
	try{
    xmlHttp=new XMLHttpRequest();
	}
    catch (e)
	{
   try
	{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp;
	}catch(e){
	alert("error in getXmlHttpObject"); }
	}

  /**
   * Get the contents of the URL via an Ajax call
   * url - to get content from (e.g. /struts-ajax/sampleajax.do?ask=COMMAND_NAME_1) 
   * nodeToOverWrite - when callback is made
   * nameOfFormToPost - which form values will be posted up to the server as part 
   *					of the request (can be null)
   */
   
    function callAjax(url) {
       //get the (form based) params to push up as part of the get request
	    params="";
	   xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		/*xmlHttp.open("GET",url,false);
		xmlHttp.onreadystatechange = processStateChange;
		xmlHttp.send(null);*/
		xmlHttp.open("POST",url,false);  // false for synchroounous
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
	
		
        //xmlHttp.onreadystatechange = callAjaxprocessStateChange;
      
		xmlHttp.send(params);
		
		return xmlHttp.responseText;
  }
   







    //var xmlHttp;
	//var tagId;
   function retrieveURL(url,nameOfFormToPost,ajaxTagId) {
  //alert("retrieveURL"+url)
	    //get the (form based) params to push up as part of the get request
	 //   params=getFormAsString(nameOfFormToPost);
	 var params="";
		var tagId=ajaxTagId;
		var xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		/*xmlHttp.open("GET",url,false);
		xmlHttp.onreadystatechange = processStateChange;
		xmlHttp.send(null);*/
		xmlHttp.open("POST",url,false);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");	
     //   xmlHttp.onreadystatechange = processStateChange;         
		xmlHttp.send(params);
	//	alert(xmlHttp.responseText)
		
		var completeResponse=xmlHttp.responseText;		
		spanPos = completeResponse.indexOf('<div id="'+tagId+'"');
		endDivPos =completeResponse.indexOf("</div><!--"+tagId+"-->",spanPos);
		startDivPos=completeResponse.indexOf('">',spanPos)+2;
		content=completeResponse.substring(startDivPos,endDivPos);
	//	alert(content)
		document.getElementById(tagId).innerHTML=content;
     
     
		
		
  }
  
   
    
    /*
  function processStateChange() {
  try{
//alert("processStateChange")
	  if (xmlHttp.readyState == 4) { // Complete
	 // alert(xmlHttp.responseText)
	 
		var completeResponse=xmlHttp.responseText;
		
		spanPos = completeResponse.indexOf('<span id="'+tagId+'"');
		endDivPos =completeResponse.indexOf("</span><!--"+tagId+"-->",spanPos);
		startDivPos=completeResponse.indexOf('">',spanPos)+2;
		content=completeResponse.substring(startDivPos,endDivPos);
		//alert(content)
		document.getElementById(tagId).innerHTML=content;
		
	}
  
	}
	catch(e){
	alert("Error in processStateChange"+e.description); }
  }


*/
/**
  * gets the contents of the form as a URL encoded String
  * suitable for appending to a url
  * @param formName to encode
  * @return string with encoded form values , beings with &
  */ 
 function getFormAsString(formName){

	//alert(formName);
 	
 	//Setup the return String
 	returnString ="";
 	//alert(document.forms[formName]);
  	//Get the form values
 	formElements=document.forms[formName].elements;
 	
 	//loop through the array , building up the url
 	//in the form /strutsaction.do&name=value
 	
 	for ( var i=formElements.length-1; i>=0; --i ){
 		//we escape (encode) each value
		if(i == formElements.length-1){
			returnString=returnString+escape(formElements[i].name)+"="+escape(formElements[i].value);
		}
 		returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
 	}
 	
 	//return the values
 	return returnString; 
 }
