/**
 * 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>
 */
  /**
   * 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) 
   * nameOfFormToPost - which form values will be posted up to the server as part 
   *					of the request (can be null)
   * asynchronismFlg - true(asynchronism),false(synchronization)
   * flgId - its content will be replaced
   * startFlg - replaced from there
   * endFlg - replaced to there
   */
  function retrieveURLWithStartEnd(url,nameOfFormToPost,asynchronismFlg,flgId,startFlg,endFlg) {
  	  document.getElementById(flgId.toString()).innerHTML="Loading....";
	  var req;
	  if(nameOfFormToPost != null){
		url=url+getFormAsString(nameOfFormToPost);
	  }
      //Do the Ajax call
      if (window.XMLHttpRequest) { // Non-IE browsers
      	req = new XMLHttpRequest();
      } else if (window.ActiveXObject) { // IE
      	req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
      if (req) {
      	req.onreadystatechange =  function ()
	  {
  	  	if (req.readyState == 4) { // Complete
        	if (req.status == 200) { // OK response
				var textToSplit = req.responseText;
				var startIndex = 0;
				var endIndex = textToSplit.length;
				if(startFlg != null){
					startIndex = textToSplit.lastIndexOf(startFlg.toString());
				}
				if(endFlg != null){
					endIndex = textToSplit.indexOf(endFlg.toString(),startIndex);
				}
				var returnElements=textToSplit.substring(startIndex,endIndex);
				if(startFlg != null){
					if(returnElements.indexOf(startFlg.toString())>-1){
						startContentPos=returnElements.indexOf('>')+1;
						returnElements=returnElements.substring(startContentPos);
					}
				}
				if(document.getElementById(flgId.toString())){
					document.getElementById(flgId.toString()).innerHTML = returnElements;
				}	
      		} else {
        		alert("Problem with server response:\n " + req.statusText);
      		}
    	}
  	  }
      try {
	  	req.open("POST", url, asynchronismFlg); //was get
	  }catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }	
	  req.send(null);
      }
  }
  /**
   * 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) 
   * nameOfFormToPost - which form values will be posted up to the server as part 
   *					of the request (can be null)
   * asynchronismFlg - true(asynchronism),false(synchronization)
   * flgId - it's content will be replaced
   */
  function retrieveURL(url,nameOfFormToPost,asynchronismFlg,flgId) {
  	  document.getElementById(flgId.toString()).innerHTML="Loading....";
	  var req;
	  if(nameOfFormToPost != null){
		url=url+getFormAsString(nameOfFormToPost);
	  }
      //Do the Ajax call
      if (window.XMLHttpRequest) { // Non-IE browsers
      	req = new XMLHttpRequest();
      } else if (window.ActiveXObject) { // IE
      	req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
      if (req) {
	  	if(asynchronismFlg){
      		req.onreadystatechange =  function ()
	  		{
		  	  	if (req.readyState == 4) { // Complete
		        	if (req.status == 200) { // OK response
						var returnElements = req.responseText;
						if(document.getElementById(flgId.toString())){
						document.getElementById(flgId.toString()).innerHTML = returnElements;
						}	
		      		} else {
		        		alert("Problem with server response:\n " + req.statusText);
		      		}
		    	}
	  	    }
		}
      try {
	  	req.open("POST", url, asynchronismFlg); //was get
	  }catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }	
	  req.send(null);
	  if(!asynchronismFlg){
			var returnElements = req.responseText;
				if(document.getElementById(flgId.toString())){
				document.getElementById(flgId.toString()).innerHTML = returnElements;
				}
		}
      }
  }
  
  /**
   * 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) 
   * asynchronismFlg - true(asynchronism),false(synchronization)
   * flgId - it's content will be replaced
   */
  function retrieveURLWithoutForm(url,asynchronismFlg,flgId) {
  	  document.getElementById(flgId.toString()).innerHTML="Loading....";
	  var req;
      //Do the Ajax call
      if (window.XMLHttpRequest) { // Non-IE browsers
      	req = new XMLHttpRequest();
      } else if (window.ActiveXObject) { // IE
      	req = new ActiveXObject("Microsoft.XMLHTTP");
	  }

      if (req) {
      	req.onreadystatechange =  function ()
	  {
  	  	if (req.readyState == 4) { // Complete
        	if (req.status == 200) { // OK response
				var returnElements = req.responseText;
				if(document.getElementById(flgId.toString())){
				document.getElementById(flgId.toString()).innerHTML = returnElements;
				}	
      		} else {
        		alert("Problem with server response:\n " + req.statusText);
      		}
    	}
  	  }
      try {
	  	req.open("GET", url, asynchronismFlg); //was get
	  }catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }	
	  req.send(null);
      }
  }
 /**
  * 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){
 	
 	//Setup the return String
 	returnString ="";
  	//Get the form values
 	formElements=document.forms[formName].elements;
 	for ( var i=formElements.length-1; i>=0; --i ){
 		//we escape (encode) each value
 		returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
 	}
 	//return the values
 	return returnString; 
 }

