// Copyright 2009 RFQs Inc //

//** Set Variables **//
//var inProgress = 'N';
//var timer = 0;
//var delay = 250; // 250
var xmlreqs = new Array();

//var ts = 0; //ql+$('query').value.substring(ql-4,ql);// unique identifyer that is passed to know if a current search is right (was time stamp)

function doAjax(url) {
	if (url) xmlreqGET(url, '1');
	return;
}

function handle_response(r) {
	var result = r.substr(0,1);
	var index = r.indexOf('::');
	var string = r.substring(index+2,r.length);
	var id = r.substring(1,index);
	// [case][ID]::[string]
	//alert(">"+r+"<");
	switch (result) {
		case 'e': // error pop up
			showDiv($('fade_error'));
			showDiv($('error_holder'));
			
			$('error').innerHTML = string;
			break;
		case 'g': // go to URL form good
			window.location = string;
			break;
		case 'm': // loads locations from mail codes
			array = string.split('|');
			//$(pre+'c_location_ID').value = array[0];
			if(array[0]) $(id+'_country').value = array[0];
			if(array[1]) $(id+'_city').value = array[1];
			
			$(id+'_region').options.length = 0;
			
			var re = array[3].split('+');
			for(i=0;i<re.length-1;i++){
				var parts = re[i].split('-');
				$(id+'_region').options[i] = new Option(parts[0],parts[1]);
			}
						
			$(id+'_region').value = array[2];
			break;
		/*case 'n': // loads region drop down
			$(id+'_region').options.length = 0;
						
			var array = string.split('+');
			for(i=1;i<array.length-1;i++){
				var parts = array[i].split('-');
				$(id+'_region').options[i-1] = new Option(parts[0],parts[1]);
			}*/
		case 'p': // any pop up
			showDiv($('fade_top'));
			showDiv($('top_holder'));
			
			$('top').innerHTML = string;
			break;
		case 'q': // search after update	
			doSearch('0');
			break;
		case 'r': // update div then search	
			$(id).innerHTML = string;
			doSearch('0');
			break;
		case 's': //save notice
			if ($(id)) {
				$(id).innerHTML = string; // for divs
				//alert($(id).type);
				if($(id).type != '') $(id).value = string; // for forms
				opacity(id, 0, 100, 500);
			}
			
			if ($(id+'Save')) {
				showDiv($(id+'Save'));
				fade(id+'Save');
			}
			
			break;
		case 't': // tack onto div
			tackOn($(id), string);
			break;
		
		default:
			//if(r == "Error") alert("Your Ajax Call was Lost.");
			//else if(r) alert(">"+r+"<"); // comment out when you can
			break;
	}
	//alert(">"+r+"<");
	return;
}

//** Ajax Functions **//
function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return;
}

function xmlreqGET(url, i) {
	//var pos = i;
	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 CXMLReq(1); }
	//xmlreqs[pos] = new CXMLReq(1);

	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
	return;
}

function xmlreqPOST(url,data) {
	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 CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("POST",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "multipart/form-data; \
		//boundary=\"--rfqs\"");
		xmlreqs[pos].xmlhttp.send(data);
	}
}

function xmlhttpChange(pos) {
	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) {
			handle_response(xmlreqs[pos].xmlhttp.responseText);
		} else {
			handle_response("Error");
		}
		xmlreqs[pos].freed = 1;
	}
	return;
}

// Copyright 2009 RFQs Inc //
