/*
Copyright (c) willFarrell 2011
*/

//** Set Variables **//
/*global window:true*/
/*global Option:true, XMLHttpRequest:true, ActiveXObject:true*/
/*global x:true*/ // var.global.js
/*global $:true, setDisplay:true, setInnerHTML:true, getLength:true, setValue:true, addInnerHTML:true*/ // obj.js
/*global doSearch:true*/ // search.js
/*global makePop:true, opacity:true*/ // design.js



function handle_response(r) {
	// [case][ID]::[string]
	//console.log(r);
	var result = r.substr(0,1),
			index = r.indexOf('::'),
			string = r.substring(index+2,getLength(r)),
			id = r.substring(1,index),
			o = $(id), i,
			_save = 'Save',
			_region = '_region',
			array, re; // case m
	
	switch (result) {
		case 'e': // error pop up
			setDisplay($('fade_error'), 1);
			setDisplay($('error_holder'), 1);
			setInnerHTML($('error'), 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]) { setValue($(id+'_country'), array[0]); }
			if(array[1]) { setValue($(id+'_city'), array[1]); }
			
			$(id+_region).options.length = 0;
			
			re = array[3].split('+');
			for(i=re.length-1;i--;) {
				var parts = re[i].split('-');
				$(id+_region).options[i] = new Option(parts[0],parts[1]);
			}
						
			setValue($(id+_region), array[2]);
			break;
		case 'p': // any pop up
			makePop(string);
			break;
		case 'q': // search after update	
			doSearch('0');
			break;
		case 'r': // reload/update div then search	
			setInnerHTML($(id), string);
			doSearch('0');
			break;
		case 's': //save notice
			
			if (o.id) {
				setInnerHTML(o, string); // for divs
				
				if(o.type !== '') { setValue(o, string); } // for forms
				opacity(id, 0, 100, 500);
			}
			
			if ($(id+_save).id) {
				setDisplay($(id+_save), 1);
				opacity(id+_save, 100, 0, 2000);
			}
			
			break;
		case 't': // tack onto div
			addInnerHTML($(id), string, 0);
			break;
		
		default:
			//if(r == "Error") alert("Your Ajax Call was Lost.");
			//else if(r) alert(">"+r+"<"); // comment out when you can
			break;
	}
	return;
}

function href(u){
	//var p = ls.getItem(u); // requires localstorage
	//if(p && !online) {
	//	setInnerHTML($('body'), p);
	//} else {
		window.location = u;
	//}
}

//** Ajax Functions **//
function CXMLReq(f) {
	var o = this;
	o.freed = f;
	o.xmlhttp = false; // o.xmlhttp = !1;
	o.xmlhttp = window.XMLHttpRequest?
		new XMLHttpRequest():
		window.ActiveXObject && new ActiveXObject('Microsoft.XMLHTTP');
	return o;
}

function xmlhttpChange(pos) {
	var xp = x[pos],
			xpx = xp.xmlhttp;
	if (typeof(xp) !== 'undefined' && xp.freed === 0 && xpx.readyState === 4) {
		if (xpx.status === 200 || xpx.status === 304) {
			handle_response(xpx.responseText);
		} else {
			handle_response("Error");
		}
		x[pos].freed = 1;
	}
	return;
}

function xmlreqGET(url) {
	var j, pos = -1;
	//var pos = i;
	for (j=x.length; j--;) {
		if (x[j].freed === 1) { pos = j; break; }
	}
	if (pos === -1) { pos = x.length; x[pos] = new CXMLReq(1); }
	if (x[pos].xmlhttp) {
		x[pos].freed = 0;
		x[pos].xmlhttp.open('GET',url,true);
		x[pos].xmlhttp.onreadystatechange = function() {
			if(typeof(xmlhttpChange) !== 'undefined') {
				xmlhttpChange(pos);
			}
		};
		if (window.XMLHttpRequest) {
			x[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			x[pos].xmlhttp.send();
		}
	}
	return;
}

function xmlreqPOST(url,data) {
	var i, pos = -1;
	for (i=x.length; i--;) {
		if (x[i].freed === 1) { pos = i; break; }
	}
	if (pos === -1) { pos = x.length; x[pos] = new CXMLReq(1); }
	if (x[pos].xmlhttp) {
		x[pos].freed = 0;
		x[pos].xmlhttp.open('POST',url,true);
		x[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) !== 'undefined') { xmlhttpChange(pos); }
		};
		x[pos].xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		x[pos].xmlhttp.send(data);
	}
}



function ajax(url) {
	//alert(url);
	url && xmlreqGET(url); // if(url) { xmlreqGET(url); }
}
