
//global variables
  var req = null;
  var which;
  var spanId;
  var div1;
  var div2;

  /**
   * Get the contents of the URL via an Ajax call
   * 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 retrieveURL(url,nameOfFormToPost,id) {
	  spanId=id;
    //get the (form based) params to push up as part of the get request
    url=url+getFormAsString(nameOfFormToPost);
    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
	
      req = new XMLHttpRequest();
      req.onreadystatechange = processStateChange;
      try {
      	req.open("POST", url, true); 
      } catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }
      req.send(null);
    } else if (window.ActiveXObject) { // IE
	
      req = new ActiveXObject("Msxml2.XMLHTTP.5.0");
	  if (req == null || !req || typeof ActiveXObject != "undefined"){
		  try{ req=new ActiveXObject("Msxml2.XMLHTTP.5.0"); 
		  }catch(e)
		  {req=false;}
          
	  }
	  if (req){  
        req.open("POST", url, true);
		req.onreadystatechange = processStateChange;
        req.send(null);
	}
    }
  }

/*
   * Set as the callback method for when XmlHttpRequest State Changes 
   * used by retrieveUrl
  */
  function processStateChange() {
  
  	  if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response     
	  document.getElementById('wait').style.visibility = "hidden";
        //Split the text response into Span elements
        spanElements = splitTextIntoSpan(req.responseText);
        //Use these span elements to update the page
        replaceExistingWithNewHtml(spanElements);
      } else {
        alert("Problem with server response:\n " + req.statusText);
      }
    } else {
		document.getElementById('wait').style.visibility = "visible";
	}
  }

   function processStateChangeIfu() {
  
  	  if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response     
	  
        //Split the text response into Span elements
        spanElements = splitTextIntoSpan(req.responseText);
        //Use these span elements to update the page
        replaceExistingWithNewHtml(spanElements);
        openTextDiv();
      } else {
        alert("Problem with server response:\n " + req.statusText);
      }
    }
  }
 
 /**
  * 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 ="";
 	var val;
  	//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((formElements[i].type == 'checkbox' || formElements[i].type == 'radio') && formElements[i].checked) {
		   returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
	    } else if ((formElements[i].type == 'checkbox' || formElements[i].type == 'radio') && !formElements[i].checked) {
			returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(-1);
		} else {
			if(formElements[i].name != 'cid'){
				returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
			} 
		}
 	}
 	//return the values
	document.getElementById('wait').style.visibility = "visible";
	returnString = returnString +'&ms='+escape(new Date().getTime());
 	return returnString;
 }
 
 /**
 * Splits the text into <span> elements
 * @param the text to be parsed
 * @return array of <span> elements - this array can contain nulls
 */
 function splitTextIntoSpan(textToSplit){
  	//Split the document
 	returnElements=textToSplit.split("</span>")
 	//Process each of the elements 	
 	for ( var i=returnElements.length-1; i>=0; --i ){
 		
 		//Remove everything before the 1st span
 		spanPos = returnElements[i].indexOf('<span id="'+spanId+'"');
 		//if we find a match , take out everything before the span
 		if(spanPos>0){
 			subString=returnElements[i].substring(spanPos);
 			returnElements[i]=subString;
 		
 		} 
 	}
 	
 	return returnElements;
 }
 
 /*
  * Replace html elements in the existing (ie viewable document)
  * with new elements (from the ajax requested document)
  * WHERE they have the same name AND are <span> elements
  * @param newTextElements (output of splitTextIntoSpan)
  *					in the format <span id=name>texttoupdate
  */
 function replaceExistingWithNewHtml(newTextElements){
 	//loop through newTextElements
 	for ( var i=newTextElements.length-1; i>=0; --i ){
  
 		//check that this begins with <span
 		if(newTextElements[i].indexOf('<span id="'+spanId+'"')>-1){
 			
 			//get the name - between the 1st and 2nd quote mark
 			startNamePos=newTextElements[i].indexOf('"')+1;
 			endNamePos=newTextElements[i].indexOf('"',startNamePos);
 			name=newTextElements[i].substring(startNamePos,endNamePos);
 			
 			//get the content - everything after the first > mark
 			startContentPos=newTextElements[i].indexOf('>')+1;
 			content=newTextElements[i].substring(startContentPos);
 			
 			//Now update the existing Document with this element
 			
	 			//check that this element exists in the document
	 			if(document.getElementById(name)){
	 				//alert("Replacing Element:"+name);
	 				document.getElementById(spanId).innerHTML = content;
	 			} else {
	 				//alert("Element:"+name+"not found in existing document");
	 			}
 		}
 	}
 }
 
 function processAjaxRequest(url,id) {

            spanId=id;
    //get the (form based) params to push up as part of the get request

    url=url;
	
    //Do the Ajax call

    if (window.XMLHttpRequest) { // Non-IE browsers

      req = new XMLHttpRequest();

      req.onreadystatechange = processStateChangeIfu;

      try {
          req.open("POST", url, true); 

      } catch (e) {
        alert("Problem Communicating with Server\n"+e);

      }

      req.send(null);

    } else if (window.ActiveXObject) { // IE
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
        

        req.open("POST", url, true);
        req.onreadystatechange = processStateChangeIfu;
        req.send();

      }

    }

  }
  function processChromeRequest(url,id,div_1,div_2) {
            div1 = div_1;
			div2 = div_2;
            spanId=id;
    //get the (form based) params to push up as part of the get request

    url=url;
	if (document.URL.substring(document.URL.indexOf('pageid/')+7,document.URL.indexOf('/catid')) == "pgbkhmc001")
 {
  var subtraction = document.body.scrollHeight-document.body.offsetHeight;
  if (subtraction > 0) {
  document.getElementById("div2").style.marginTop = (subtraction + 30) +"px";
  document.getElementById("iframe").style.marginTop = (subtraction + 30) +"px";
  } else {
        document.getElementById("div2").style.marginTop = 30+"px";
		document.getElementById("iframe").style.marginTop = 30+"px";
 }
}
	if (document.URL.substring(document.URL.indexOf('pageid/')+7,document.URL.indexOf('/rqpg')) == "pgbkpress001")
 {
  if (document.documentElement.scrollTop > 0) {
  document.getElementById("div2").style.marginTop = (document.documentElement.scrollTop + 30) + "px";
  document.getElementById("iframe").style.marginTop = (document.documentElement.scrollTop + 30) + "px";
  } else {
        document.getElementById("div2").style.marginTop = 30+"px";
		document.getElementById("iframe").style.marginTop = 30+"px";
 }
}
    //Do the Ajax call

    if (window.XMLHttpRequest) { // Non-IE browsers

      req = new XMLHttpRequest();

      req.onreadystatechange = processStateChangeChrome;

      try {
          req.open("POST", url, true); 

      } catch (e) {
        alert("Problem Communicating with Server\n"+e);

      }

      req.send(null);

    } else if (window.ActiveXObject) { // IE
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
		  req.open("POST", url, true);
        req.onreadystatechange = processStateChangeChrome;
        req.send();

      }

    }

  }
function processStateChangeChrome() {
  	  if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response     
      document.getElementById(spanId).innerHTML = req.responseText;
		opendiv(div1,div2);
      } else {
        alert("Problem with server response:\n " + req.statusText);
      }
    }
  }

  
