var xmlreqs = new Array();


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");
	}
}

function xmlreqGET(url, fn_ready, fn_waiting, box_id) {
	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("GET",url,true);
		//xmlreqs[pos].xmlhttp.setRequestHeader('Content-Type', 'text/html; charset=Big5');
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos,fn_ready, fn_waiting, box_id); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
}

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, action1); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlreqs[pos].xmlhttp.send(data);
	}
}

function xmlhttpChange(pos, fn_ready, fn_waiting, box_id) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0) {
		if (xmlreqs[pos].xmlhttp.readyState == 4) {
			if (typeof(fn_ready) == 'function') 
				fn_ready(xmlreqs[pos].xmlhttp.responseText, box_id);
			/*
			if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
				//if (typeof(fn_ready) != 'undefined') fn_ready(xmlreqs[pos].xmlhttp.responseText);
			} else {
				// handle_error(); 
			}
			*/
			xmlreqs[pos].freed = 1;
		} else {						
			if (box_id != undefined) {
				if (typeof(fn_waiting) == 'function') fn_waiting(xmlreqs[pos].xmlhttp.readyState, box_id);
				else show_txt_loading(box_id);
			}
		} //		if (xmlreqs[pos].xmlhttp.readyState == 4) {
	} // 	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0)
} // fn xmlhttpChange


// usage :
// xmlreqGET('wfoto_getThemeColorOptlist.php', action1, action1_waiting);
/*
function action1(responseText){
	var optlistitem = responseText.split('|');
	var arrSelData = new Array();
	for (var i in optlistitem) {
		eachOpt = optlistitem[i];
		optvalue = eachOpt.split(':');
		arrSelData[i] = [optvalue[0], optvalue[1], optvalue[2]];
	}

	//redraw list
	create_themeColoroptList('color_a1_id', arrSelData);	
}

function action1_waiting(){ loadingmsg('client_id'); }

function loadingmsg(f_ID){
	//alert('waiting');
	var tmp = document.createElement("span")
	tmp.id=f_ID;
	tmp.innerHTML="Loading....";
	
	var oldsel = document.getElementById(f_ID);
	var nodeparent = oldsel.parentNode;
	nodeparent.replaceChild(tmp, oldsel);
}
*/


function show_txt_loading(f_ID){
	var tmp = document.createElement("span")
	tmp.id=f_ID;
	tmp.innerHTML="Loading....";
	
	var oldsel = document.getElementById(f_ID);
	var nodeparent = oldsel.parentNode;
	nodeparent.replaceChild(tmp, oldsel);
}

function txt2ary(responseText, num_items){
	if (num_items == 'undefined') num_items = 2;

	var arrAllRows = responseText.split('|');
	var arrAllItems = new Array();
	for (var i in arrAllRows) {
		eachRow = arrAllRows[i];
		arrItems = eachRow.split(':');
		if (num_items == 2) 	arrAllItems[i] = [arrItems[0], arrItems[1]];
		else 	if (num_items == 3) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2]];
		else 	if (num_items == 4) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2], arrItems[3]];
		else 	if (num_items == 5) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2], arrItems[3], arrItems[4]];
		else 	if (num_items == 6) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2], arrItems[3], arrItems[4], arrItems[5]];
		else 	if (num_items == 7) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2], arrItems[3], arrItems[4], arrItems[5], arrItems[6]];
		else 	if (num_items == 8) 	arrAllItems[i] = [arrItems[0], arrItems[1], arrItems[2], arrItems[3], arrItems[4], arrItems[5], arrItems[6], arrItems[7]];
	}
	return arrAllItems;
}

