iQAjaxSend = function(sToPost){
	var CallbackHandler = this.CallbackHandler;
	var CallbackParams = this.CallbackParams;
	var ResponseFormat = this.ResponseFormat.toUpperCase();
	var ret = null;

	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		var xmlHttpReq = new XMLHttpRequest();
	// IE
	}else if(window.ActiveXObject) {
		var xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open(this.HTTPMethod, this.URL, this.bSync);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {

			if(ResponseFormat == 'JSON'){
				ret = eval('(' + xmlHttpReq.responseText + ')');
			}else{
				ret = xmlHttpReq.responseText;
			}

			if(CallbackHandler != null){
				if(CallbackParams != null){
					var arrVals = CallbackParams.split(',');

					if(arrVals.length > 0){
						var tmp = ''
						for (i=0;i<arrVals.length;i++){
							if(tmp == ''){
								tmp = "'" + arrVals[i] + "'";
							}else{
								tmp = tmp + ",'" + arrVals[i] + "'";
							}
						}
						eval(CallbackHandler + '(ret,' + tmp + ')');
					}else{
						eval(CallbackHandler + '(ret,CallbackParams)');
					}
				}else{
					eval(CallbackHandler + '(ret)');
				}
			}else{
				return ret;	
			}
		}
	}

	if(this.CFCFunction != ''){
		sToPost += '&Method=' + this.CFCMethod;

		if(ResponseFormat != ''){
			sToPost += '&ReturnFormat=' + ResponseFormat;
		}
	}


	xmlHttpReq.send(sToPost);	
}

function iQAjax(){
	this.CFCMethod = null;
	this.ResponseFormat = 'JSON';
	this.URL = '';
	this.CallbackHandler = null
	this.CallbackParams = null
	this.HTTPMethod = 'POST';
	this.bSync = true;
	this.send = iQAjaxSend;
}

function jsonFieldUpdate(jsonData,sDescriptionField,sValueField,sFormName,sFormField){
	var getCol = new Object();
	
	//This creates a mapping to the Column name that points at its position
	//This makes it much easier to reference the column name
	for(var i = 0; i < jsonData.COLUMNS.length; i++) {
		getCol[jsonData.COLUMNS[i]] = i;      
	}

	//This clears the current options
	eval('document.' + sFormName + '.' + sFormField + '.options').length = 0;
	
	//Now Loop through the JSON data and create an option for each
	for(i=0;i<jsonData.DATA.length;i++){
		var option = new Option();
		option.text = jsonData.DATA[i][getCol[sDescriptionField.toUpperCase()]];
		option.value = jsonData.DATA[i][getCol[sValueField.toUpperCase()]];
		eval('document.' + sFormName + '.' + sFormField + '.options')[i] = option;
	}

	var tmpVals = eval('_' + sFormField);

	if(tmpVals){
		if(tmpVals.indexOf(',') > 0){
			var arrVals = tmpVals.split(",");
			
			for(i=0;i<arrVals.length;i++){
				var tmpVal = arrVals[i];
				for(j=0;j<eval('document.' + sFormName + '.' + sFormField + '.options').length;j++){
					if(eval('document.' + sFormName + '.' + sFormField + '.options')[j].value == tmpVal){
						eval('document.' + sFormName + '.' + sFormField + '.options')[j].selected = true;
						break;
					}
				}
			}
			//this just makes sure that if there is a , in the value that if we have a option that has that
			//exact value it gets selected
			for(j=0;j<eval('document.' + sFormName + '.' + sFormField + '.options').length;j++){
				if(eval('document.' + sFormName + '.' + sFormField + '.options')[j].value == tmpVals){
					eval('document.' + sFormName + '.' + sFormField + '.options')[j].selected = true;
					break;
				}
			}			
		}else{
			for(j=0;j<eval('document.' + sFormName + '.' + sFormField + '.options').length;j++){
				if(eval('document.' + sFormName + '.' + sFormField + '.options')[j].value == tmpVals){
					eval('document.' + sFormName + '.' + sFormField + '.options')[j].selected = true;
					break;
				}
			}
			
		}
	}
}


