

/************************************ Global Variables *************************************************/
var xmlreqs = new Array();


/*********************************** AJAX Calling Methods ***************************************************/
function SkipXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function retrieveCalculateReq(url,fldLabel) {
	//alert("retrieve url "+url+" field "+fldLabel);
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new SkipXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		
		if (window.XMLHttpRequest) {	
			try {
				xmlreqs[pos].xmlhttp.open("GET",url,false);
			} catch ( e ) {
				alert(e);
			}						
			xmlreqs[pos].xmlhttp.onreadystatechange = function() {
				if (typeof(skipXmlhttpChangeAC) != 'undefined') { skipXmlhttpChangeAC(pos,fldLabel); }
			}
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.open("GET", url, false);
			xmlreqs[pos].xmlhttp.onreadystatechange = function() {
				if (typeof(skipXmlhttpChangeAC) != 'undefined') { skipXmlhttpChangeAC(pos,fldLabel); }
			}      		

        	xmlreqs[pos].xmlhttp.send();        	
      	}				
	}
}



function skipXmlhttpChangeAC(pos,fldLabel) 
{
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
			populateCalculate(xmlreqs[pos].xmlhttp,fldLabel);
		} else {
			//alert("Problem: " + xmlreqs[pos].xmlhttp.statusText);
		}
		xmlreqs[pos].freed = 1;
	}
}

// this is only used to call the servlet again to save the calculated field once its value is populated on the form
function retrieveSaveReq(url,fldLabel) {
	//alert("retrieve url "+url+" field "+fldLabel);
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new SkipXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		
		if (window.XMLHttpRequest) {	
			try {
				xmlreqs[pos].xmlhttp.open("GET",url,false);
			} catch ( e ) {
				alert(e);
			}						
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.open("GET", url, false);
        		xmlreqs[pos].xmlhttp.send();        	
      		}				
	}
}
  

 
  
  
/*********************************** Retrieve History Methods *************************************************/

  
  
  function populateCalculate(req, fieldLabel)
  {
  	//alert('refreshing field with label ' + fieldLabel + ' with ' + req.responseText);
  	var el = document.getElementsByName(fieldLabel);
  	if (el != null && el[0] != null) { //affected field is on the page
  		el[0].value = req.responseText;
  	}
  }
  
  
  function calculateField(triggerField, fieldsToCalc, fieldsToSaveFirst)
  {	
	//This is actually a synchronous call; making it asynchronous was leading to some funky behaviour if the user clicked around quickly
    grayOut(true, 10);
  	//alert('FieldtoCalc: ' + fieldsToCalc + ' triggerField: ' + triggerField + ' fieldsToSaveFirst: '+fieldsToSaveFirst);
	var fldsToSave = fieldsToSaveFirst.split("|");  	
	var fldsToCalc = fieldsToCalc.split("|");
	
  	var qs2 = new Querystring(getFormData(document.forms[0]));	

	var fParams = "";
  	for ( var i = 0; i< fldsToSave.length; i++ ) 
  	{
  		var newVal = qs2.get(fldsToSave[i]);
  		fParams = fParams + "&" + fldsToSave[i] + "=" + newVal;
  	}
	var triggerFieldVal = qs2.get(triggerField);
	fParams = fParams + "&" + triggerField + "=" + triggerFieldVal;

  	for ( var i = 0; i< fldsToCalc.length; i++ ) 
  	{
 		var url = 'jsp/ajax.executeRule?autocalcfield=true&fldLabel=' + fldsToCalc[i] + fParams;
		//alert("calling url " + url);
		var waitTime = 200;
		//setTimeout('retrieveCalculateReq("' + url + '", "' +  fldsToCalc[i] + '")', waitTime);
		retrieveCalculateReq(url, fldsToCalc[i]);

		// JH 5/27/09 - save the calculated field in case it is referenced in other rules in the page
 		//var url = 'jsp/ajax.saveAnswer?autocalcfield=true&fldLabel=' + fldsToCalc[i];
		//var waitTime = 200;
		//retrieveSaveReq(url, fldsToCalc[i]);
	}


	grayOut(false, 0);								
  }
   

function saveField(fieldLabel)
  {	
	//This is actually a synchronous call; making it asynchronous was leading to some funky behaviour if the user clicked around quickly
    	grayOut(true, 10);

	var qs2 = new Querystring(getFormData(document.forms[0]));	

  	var newVal = qs2.get(fieldLabel);
  	var fParams = "&" + fieldLabel + "=" + newVal;

	// JH 5/27/09 - save the calculated field in case it is referenced in other rules in the page
 	var url = 'jsp/ajax.saveAnswer?autocalcfield=true&fldLabel=' + fieldLabel + fParams;
	var waitTime = 200;
	retrieveSaveReq(url, fieldLabel);

	grayOut(false, 0);								
  } 
  
  
  

