var ajaxLoadImage = new Image();
ajaxLoadImage.src = "images/aloader.gif";

var AJAX_IMAGE = "<img src=\""+ajaxLoadImage.src+"\" />";

function getAjaxObject() {
	var ao;
	try {
		ao = new XMLHttpRequest();
		ao.overrideMimeType('text/html');
	}catch(e) {
		try {
			ao = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e) {
			try {
				ao = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e) {
				alert("Your browser cannot support this page!");
				return false;
			}
		}
	}
	
	return ao;
}

function ajax(url, parsefunc, addParams) {
	xmlhttp = getAjaxObject();
	xmlhttp.open("POST", url, true); 
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			parsefunc(xmlhttp.responseText, addParams);
		}
		
	}
	xmlhttp.send("");
}

function formToURI(formData) {
	var fd = new String();
	
	//combine form elements
	fde = formData.elements;
	for (i=0; i< formData.elements.length; i++) {
		if (typeof i != "string") {
			var fe = formData.elements[i];
			
			if (typeof fe.value != "undefined") {
				//alert(fe.name);
				fd += ((i!=0) ? "&" : ""); 
				if (fe.type == "checkbox") {
					fd += fe.name + "=" + encodeURI(fe.checked);
				}else{
					fd += fe.name + "=" + encodeURI(fe.value);
				}
			}
		}
	}
	
	return fd;
}

//must be an associative array!
function stringArrayToURI(strings) {
	var fd = new String();
	var cnt = 0;
	for(i in strings) {
		fd += ((cnt != 0) ? "&" : "") + i + "=" + encodeURI(strings[i]);
		cnt++;
	}
	return fd;
}

//use fd MUST be URI encoded!
function ajaxSend(url, fd, parsefunc, addParams) {
	xmlhttp = getAjaxObject();
	
	xmlhttp.open("POST", url, true); 
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			parsefunc(xmlhttp.responseText, addParams);
		}
		
	}
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", fd.length);
    xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(fd);
} 

function ajaxResponseArray(res) {
	var rval = Array();
	var ns;
	var q = res.split(/&@/g);
	
	for (i in q) {
		if(q[i]!=undefined){
		if (q[i].indexOf("=" != -1)) {
			ns = q[i].indexOf("=");
			rval[q[i].substr(0, ns)] = q[i].substr(ns+1, q[i].length);
		}
	}}
	
	
	return rval;
}

//in the format...
//DIVID|PAGE(xmlHTTPpage,null)|PAGEVAR(null,self,xml.var)|[elemType]||nextelem
//basically...
//1 = the destination variable.  must be an HTML container element with innerHTML props
//2 = the source of the data.  can be a file, "self", or "null"
//	self = data from this page
//	null = no data; 
//3 = the source variable.  if the source type is "null", this is the new VALUE of the destination form
//4 = the destination form type
//5 = the destination form name (important)
//--------------------------
//Please note that for styling purposes, the style of the created form will be the style of the
//div it gets created in plus a "_form"
function ajaxMultiParse(parseStr) {
	
	if(parseStr!=undefined)
	var actions = parseStr.split(/\|\|/g);
	else
	var actions = "";
	//var filesToCall = array();
	var params = new Array();
	
	//the idea is to split it up by page so that multiple calls
	//are not made to the same page.
	for (i in actions) {
		t = actions[i].split(/\|/g);
		if (typeof params[t[1]] == "undefined") {
			params[t[1]] = new Array(new Array(t[0], t[2], t[3], t[4]));
		}else{
			params[t[1]].push(new Array(t[0], t[2], t[3], t[4]));		
		}
	}	
	
	for (i in params) {
		if (i == "null" || i == "self") {//no data source; blank fields
			ajaxMultiFormFill(i, params[i]);
		}else{//data source from AJAX form
			ajax(i, ajaxMultiFormFill, params[i]);
		}
	}
}

function ajaxMultiFormFill(page, params) {
	//check the type of data source given to the function
	if (page == "self") {
		//loop through datasources and populate the page array
		for (i in params) {
			page[params[i][1]] = document.getElementById(params[i][1]);
		}
	}else if (page != "null"){
		//populate the page array with ajax vars
		page = ajaxResponseArray(page);
	}
	//alert(params);
	
	for (i in params) {
		//set the value...  nulls get the pointer itself
		val = ((page == "null") ? params[i][1] : page[params[i][1]]);
		
		myStyle = document.getElementById(params[i][0]).className;
		if (typeof myStyle == "undefined") {
			myStyle = "aform_def";
		}else{
			myStyle += "_form";
		}
		
		if (params[i][2] == "textarea") {
			val = '<textarea name="'+params[i][3]+'" style="width: 100%; font-family: sans-serif; font-size: 11px;" rows="12">' + val + '</textarea>';
		}else if(params[i][2] == "select") {
			throw("Unimplemented Feature... select comes in v2");
		}else{
			val = '<input name="'+params[i][3]+'" value="'+val+'" type="'+params[i][2]+'" class="'+myStyle+'" />';
		}
		document.getElementById(params[i][0]).innerHTML = val;
	}
}



