//---------------------------------------------------------------------------
// Initialize xml http object
var g_xmlhttp = null;
var g_parser = null;

if (window.XMLHttpRequest) // Others
	g_xmlhttp = new XMLHttpRequest()
else if (window.ActiveXObject)			// IE
	g_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")

//-------------------------------------
// Post message to server
//-------------------------------------
function postMessage(xmlMsg, url, synchronous) 
{
	if (g_xmlhttp == null)
	{
		// error('g_xmlhttp Not supported by this browser.');
		return null;
	}	
	
	// synchronized post to client_commands.jsp
	g_xmlhttp.open('POST', url, !synchronous);

	var xmlMsg = xmlMsg;
	g_xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	g_xmlhttp.setRequestHeader("content-length", xmlMsg.length);
  
    try
    {
		g_xmlhttp.send(xmlMsg);
	}
	catch(err)
	{
		if (err.description)
			alert(err.description);
			
		return null;
	}
	
	if (g_xmlhttp.readyState == 4)
  	{    	
		// if "OK"
	  	if (g_xmlhttp.status == 200)
	  	{
			return g_xmlhttp.responseText;  	  
	  	}	  
	}
	
	return null;
}

//-------------------------------------
//Post AJAX message to server
//-------------------------------------
function postAjaxMessage(xmlMsg, url) 
{
	if (g_xmlhttp == null)
	{
		// error('g_xmlhttp Not supported by this browser.');
		return null;
	}	
	
	// synchronized post 
	g_xmlhttp.open('POST', url, false);

	var xmlMsg = xmlMsg;
	g_xmlhttp.setRequestHeader("content-type", "text/xml");
	g_xmlhttp.setRequestHeader("content-length", xmlMsg.length);

	try
	{
		g_xmlhttp.send(xmlMsg);
	}
	catch(err)
	{
		if (err.description)
			alert(err.description);
			
		return null;
	}
	
	if (g_xmlhttp.readyState == 4)
	{    	
		// if "OK"
	  	if (g_xmlhttp.status == 200)
	  	{
			var resp = g_xmlhttp.responseText;

			var doc = xmlGetParser().loadXML(resp);
			var root = xmlGetDocumentElement(doc);
			
			return root;
	  	}	  
	}
	
	return null;
}

//-------------------------------------
// Post AJAX form
// 	posts a 'sendform' command
//	if form has a name, it is added as a name parameter to the ajax command
//	if actionUrl is not specified, action from form is used
//
//  form content is sent using the format <name>value</name>
//	where 'name' is the input name and 'value' is the control's value
//-------------------------------------
function postAjaxForm(myForm, actionUrl, cmdName)
{
	if (!actionUrl)
		actionUrl = myForm.action;
	
	if (!cmdName)
		cmdName = 'sendform';
		
	if (myForm.name)
		extra = ' name="' + xmlFixParam(myForm.name) + '"';
	else
		extra = '';
		
	var ajax = '<ajax cmd="' + cmdName + '"' + extra + '>';
	
	for (i = 0; i < myForm.elements.length; i++)
	{
		if (myForm.elements[i].name == '')
			continue;
		
		if (myForm.elements[i].type == 'checkbox')
		{
			ajax += 
				'<' + myForm.elements[i].name + '>' + 
				//(myForm.elements[i].checked ? xmlFixParam(myForm.elements[i].value) : '') + 
				(myForm.elements[i].checked ? 'true' : 'false') +
				'<'+'/' + myForm.elements[i].name + '>';
		}
		else
		{		
			ajax += 
				'<' + myForm.elements[i].name + '>' + 
				xmlFixParam(myForm.elements[i].value) + 
				'<'+'/' + myForm.elements[i].name + '>';
		}
	}
	
	ajax += '</ajax>';
	
	return postAjaxMessage(ajax, actionUrl);
}


//-------------------------------------
//Get XML parser
//-------------------------------------
function xmlGetParser()
{
	if (!g_parser)
	{
		if (window.ActiveXObject)
		{
			g_parser = new Object();
			g_parser.loadXML = function(xml)
			{
	 			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = "false";
				xmlDoc.loadXML(xml);
				return xmlDoc;
			}
		}
		else
		{			
			g_parser = new DOMParser();
			g_parser.loadXML = function(xml)
			{
				//var xmlDoc = document.implementation.createDocument("","",null);
				//xmlDoc.async = "false";
				//xmlDoc.loadXML(xml);
				
				var parser = new DOMParser();
				var xmlDoc = parser.parseFromString(xml, "text/xml");
				
				return xmlDoc;
			}
		}
	}
	
	return g_parser;
}

//-------------------------------------
//Get document root
//-------------------------------------
function xmlGetDocumentElement(domDoc)
{
	var docRoot = null;
	
	if (domDoc.getDocumentElement)
		docRoot = domDoc.getDocumentElement();
	else
	{
		if (domDoc.childNodes.length)
			docRoot = domDoc.childNodes[domDoc.childNodes.length-1];
	}
	
	return docRoot;
}

//-------------------------------------
function xmlGetFirstElementByTagName(el, tagName)
{
	if (!el)
		return null;
		
	var node = el.firstChild;
	
	while (node)
	{
		var next = node.nextSibling;
		
		if (node.nodeName == tagName)
			return node;
			
		node = next;
	}
	
	return null;
}

//-------------------------------------
// Fix parameter for XML
//-------------------------------------
function xmlFixParam(xmlMsg)
{
	xmlMsg = new String(xmlMsg);
	xmlMsg = xmlMsg.replace(/&/g, "&amp;");
	xmlMsg = xmlMsg.replace(/</g, "&lt;");
	xmlMsg = xmlMsg.replace(/>/g, "&gt;");
	xmlMsg = xmlMsg.replace(/\"/g, "&quot;");	
	
	return xmlMsg;
}

//-------------------------------------
// Simple name-value tag
//-------------------------------------
function xmlSimpleTag(tagName, tagValue)
{
	return '<' + tagName + '>' + xmlFixParam(tagValue) + '</' + tagName + '>';
}