// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//


var req;			//xmlhttprequest object

//initialize cross browser xmlhttprequest object
function Initialize()
{
/*	if (req)
	{
		req.abort();
		return;
	}
*/	try
	{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req && typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}
	return req;
}

function ShowDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="show";
	else
	{ 
		document.getElementById(divid).style.visibility="visible";
		document.getElementById(divid).style.borderStyle="solid";
		document.getElementById(divid).style.borderWidth="1px";
	}
}

function HideDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="hide";
	else 
	{
		document.getElementById(divid).style.visibility="hidden";
		document.getElementById(divid).style.borderStyle="none";
	}
}
/*
function ShowDiv(divid)
{
	if (document.layers) 
		document.layers[divid].display="block";
	else
	{ 
		document.getElementById(divid).display="block";
	}
}

function HideDiv(divid)
{
	if (document.layers) 
		document.layers[divid].display="none";
	else 
	{
		document.getElementById(divid).display="none";
	}
}
*/
function movein(which,overcolor)
{
	which.style.background=overcolor;
}

function moveout(which,outcolor)
{
	which.style.background=outcolor;
}

//tbid - Textbox id
function press(idid,id,tbid,text,dd_press)
{
	el(idid).value=id;
	el(tbid).value=text;
	HideDiv(dd_press);
}

//SendQuery function sends request to server asyncrhonously.
function SendQuery(arg,key,dd_,styshet_, _hdlr)
{
	if (key.length > 1)
	{
		Initialize();
		var baseurl=window.location.href; //get the url of the current web page
		
		var queryPos = baseurl.indexOf("?");
		if (queryPos > -1)
			baseurl=baseurl.substr(0, queryPos);
		//check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
		if (baseurl.indexOf("?")==-1)
			var url=baseurl+"?"+arg+"="+key; //the get argument must be unique for each control.arg is a unique variable of a web page
		else
			var url=baseurl+"&"+arg+"="+key;	
		url += "&timestamp=" + new Date().getTime(); // circumvent IE caching of requests
		if(req!=null)
		{
			if (_hdlr == null)
				req.onreadystatechange = function() {Process(dd_,styshet_);}; //event handler for each state change
			else
				req.onreadystatechange = _hdlr;
			req.open("GET", url, true);
			req.send(null);
		}
	}
}
function UpdateItem(keyname,keyval,fldname,fldval, hdlr)
{
	if (keyname.length > 1)
	{
		Initialize();
		var baseurl=window.location.href; //get the url of the current web page
		var queryPos = baseurl.indexOf("?");
		if (queryPos > -1)
			baseurl=baseurl.subString(0, queryPos);
		//check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
		if (baseurl.indexOf("?")==-1)
			var url=baseurl+"?"; //the get argument must be unique for each control.arg is a unique variable of a web page
		else
			var url=baseurl+"&";	
		url += keyname+"="+escape(keyval) + "&" + fldname + "=" +escape(fldval) 
		url += "&timestamp=" + new Date().getTime(); // circumvent IE caching of requests
		if(req!=null)
		{
//			req.onreadystatechange = function() {Process(dd_,styshet_);}; //event handler for each state change
			if (hdlr)
				req.onreadystatechange = hdlr;
			req.open("GET", url, true);	// Use POST instead of GET to circumvent IE caching of requests
			req.send(null);
		}
	}
}

function Process(dd,styshet)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{
			if(req.responseText=="")
				HideDiv(dd);//hide drop down area
			else
			{
				ShowDiv(dd);//show drop down area
				var xml = xmlParse(req.responseText);
				var xslt = xmlParse(styshet);
				var html = xsltProcess(xml, xslt);
				if (html=="")
					HideDiv(dd);
				el(dd).innerHTML=html;
				return true;
					
			}
		}
		else
		{
			el(dd).innerHTML="There was a problem retrieving data:<br>"+ req.statusText;
		}
	}
	return false;
}

// Sets both the value and value attributes of each option to the text of the xml node
function FillSelect1(element)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{

			ClearSelect(element);
			var el = document.getElementById(element);
		    var node = req.responseXml.childNodes[0];
		    if (node != null) 
				for(i = 0; i < node.childNodes.length; i++)
				{
					opt = node.childNodes[i];
					el.options[i] = new Option(opt.text, opt.text);
					sel = opt.attributes["selected"];
					if (opt.attributes.length > 1 && opt.attributes[1].nodeName == "selected"  && opt.attributes[1].text == "1")
						el.selectedIndex = i;
				}
			else
			{
				var re1 = /<\/option>/g;		
				var re2 = /\<option/g;		
				var re3 = /\>/g;
				//document.body.innerHTML = req.ResponseText.replace(re1, "&lt;/option&gt;<br>").replace(re2, "&lt;option&gt;");
				var p = document.createElement("p");
				var d1 = req.ResponseText.replace(/&/g, "&amp;").replace(re1, "&lt;/option&gt;<br>");
				p.innerHTML = d1.replace(re2, "&lt;option"); //.replace(re3, "&gt;");
				document.body.appendChild(p);
			}
			
		}
		else
		{
			document.body.innerHTML = "There was a problem retrieving data:<br>"+ req.statusText + "<hr>" + req.responseText;;
		}
	}
}

function FillSelect2(element)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{

			ClearSelect(element);
			var el = document.getElementById(element);
		    var node = req.responseXml.childNodes[0];
		    if (node != null) 
				for(i = 0; i < node.childNodes.length; i++)
				{
					opt = node.childNodes[i];
					el.options[i] = new Option(opt.text, opt.attributes[0].value);
					sel = opt.attributes["selected"];
					if (opt.attributes.length > 1 && opt.attributes[1].nodeName == "selected"  && opt.attributes[1].text == "1")
						el.selectedIndex = i;
				}
			else
			{
				document.write("Invlid XML encountered<hr>" + req.responseText + "<hr>");
			}
			
		}
		else
		{
			document.body.innerHTML = "There was a problem retrieving data:<br>"+ req.statusText + "<hr>" + req.responseText;;
		}
	}
}

function FillElement(element)
{
	if (req.readyState == 4)
	{
		var el = document.getElementById(element);
		if (req.status == 200)// only if "OK"
			el.innerHTML = req.responseText;
		else
			el.innerHTML="There was a problem retrieving data:<br>"+ req.statusText + "<hr>" + req.responseText;
	}
}

function ClearSelect(selName)
{
	var el = document.getElementById(selName);
	for (i = el.options.length; i >= 0; i--) 
		el.options[i] = null;
}

function ShowSelectLoading(selName)
{
	ClearSelect(selName);
	var el = document.getElementById(selName);
	el.options[0] = new Option("Loading...", "Loading...");
}


function dumper(obj)
{
	attr = 'left=0,top=0,height=' + 600 + ',width=' + 600 + ',menubar=no,scrollbars=yes,status=yes,toolbar=no,location=no,resizeable=yes';
	win = window.open("#",'popup', attr);

	var doc = win.document;
	doc.writeln("<table style='font-family:verdana; font-size:8pt;'>");
	for(var i in obj)
	{
		doc.writeln("<tr><td>" + i + "</td><td>=</td><td>" + obj[i] + "</td></tr>");
	}
	doc.writeln("</table>");
	doc.writeln("<hr>");
}
