// <!------------------------------------------------------------------------------------------------------------------------------
// CONFIDENTIALITY NOTICE:
// This source is a confidential proprietary of Thoughts Ware Pte Ltd.
//
// Copyright (c) 2007 Thoughts Ware Pte Ltd.
// 
// Use of this code without a valid license from Thoughts Ware Pte Ltd will be considered a voilation of the copyright.
// You are not allowed to modify or copy this code, in-part or full, without written approval from the management of Thoughts Ware Pte Ltd.
// -------------------------------------------------------------------------------------------------------------------------------->

function goToLet(nurl)
{
	location.href = nurl;
}
function trimResponse(somestr)
{
	// trims all leading and trailing whitespaces
	var qreg = /(^\s*)|(\s*$)/g;
	var qnewreg = somestr.replace(qreg, "");

	return qnewreg;
}

function getScriptBase(sp)
{
	var thisscr = location.href;
	var pat = /(.*)\/(.*)$/;
	var actions = thisscr.match(pat);
	switch (sp)
	{
		case 1:
			return actions[1] + "/";
			break;
		case 2: 
			return actions[2] + "/";
			break;
		case 3: 
			return actions[3] + "/";
			break;
		case 4: 
			return actions[1];
			break;
		default:
			return actions[1] + "/";
			break;
	}											
}

function getPathBase(path)
{
	var pat = /(.*)\/(.*)$/;
	var actions = path.match(pat);
	return actions[2];									
}

function makeSyncPOSTRequest(src, params)
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	xmlHTTPObj.open("POST", src, false);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
	return trimResponse(xmlHTTPObj.responseText);
}

function makeSyncPOSTRequestToDIV(src, params, divContainer)
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	xmlHTTPObj.open("POST", src, false);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
	var resp = trimResponse(xmlHTTPObj.responseText);
	if(resp)
	{
		var divcode = MM_findObj(divContainer);
		if(divcode)
		{
			divcode.innerHTML = resp;
		}
	}
}

function makeSyncPOSTRequestToSelectByID(src, params, selectboxid, presetValue)
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) {
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	xmlHTTPObj.open("POST", src, false);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
	var resp = trimResponse(xmlHTTPObj.responseText);
	var comboBox = null;
	try {
		comboBox = MM_findObj(selectboxid);
	}
	catch (e) 
	{
		alert("Unable to locate destination object");
		return false;
	}
	//alert("response =" + resp );
	//alert("comboBox =" + comboBox );	
	var optional = resp.split(":_:");
	while(comboBox.options.length) comboBox.options[0] = null;	
	if(resp)
	{
		var terminator = optional.length % 2;
		if(terminator == 1)
		{
			for(mdx=0; mdx<optional.length - 1; mdx+=2)
			{
				var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
				comboBox.options[mdx/2] = newoption;
			}				
			comboBox.value=optional[mdx];
		}
		else
		{
			for(mdx=0; mdx<optional.length; mdx+=2)
			{
				var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
				comboBox.options[mdx/2] = newoption;
			}	
			if (comboBox.length)
			{
				comboBox.selectedIndex=0;
			}
			else
			{
				comboBox.selectedIndex=-1;
			}								
		}
		if (presetValue)
		{
			comboBox.value=presetValue;
		}
	}	
}

function makeAsynchPOSTRequest(url, params, divContainer) 
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	xmlHTTPObj.onreadystatechange = function() { asynchResponseReceived(xmlHTTPObj, divContainer); };
	xmlHTTPObj.open('POST', url, true);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
}

function asynchResponseReceived(xmlHTTPObj, divContainer) {
	if (xmlHTTPObj.readyState == 4) {
		if (xmlHTTPObj.status == 200) {
			var resp = trimResponse(xmlHTTPObj.responseText);
			if(resp)
			{
				var divcode = MM_findObj(divContainer);
				if(divcode)
				{
					//alert(resp);
					//document.write(resp); ret.replace("/<\/script>/i","'</script' + '>'");
					divcode.innerHTML = resp.replace("/<\/script>/i","'</script' + '>'");
					//document.write(divcode.innerHTML);
				}
			}
			else
			{
				alert("No data returned. Please try again")
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}


function makeAsynchSelectFeed(url, params, form, selectboxname, presetValue) 
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	xmlHTTPObj.onreadystatechange = function() { asynchSelectFeedReceived(xmlHTTPObj, form, selectboxname, presetValue); };
	xmlHTTPObj.open('POST', url, true);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
}

// responseformat
// name:_:value:_:name:_:value
function asynchSelectFeedReceived(xmlHTTPObj, form, selectboxname, presetValue) {
	if (xmlHTTPObj.readyState == 4) {
		if (xmlHTTPObj.status == 200) {
			var resp = trimResponse(xmlHTTPObj.responseText);
			if(resp)
			{
				var comboBox = null;
				try {
					comboBox = form[selectboxname];
				}
				catch (e) 
				{
					alert("Unable to locate destination object");
					return false;
				}
				var optional = resp.split(":_:");
				while(comboBox.options.length) comboBox.options[0] = null;
				var terminator = optional.length % 2;
				if(terminator == 1)
				{
					for(mdx=0; mdx<optional.length - 1; mdx+=2)
					{
						var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
						comboBox.options[mdx/2] = newoption;
					}				
					comboBox.value=optional[mdx];
				}
				else
				{
					for(mdx=0; mdx<optional.length; mdx+=2)
					{
						var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
						comboBox.options[mdx/2] = newoption;
					}	
					if (comboBox.length)
					{
						comboBox.selectedIndex=0;
					}
					else
					{
						comboBox.selectedIndex=-1;
					}								
				}
				if (presetValue)
				{
					comboBox.value=presetValue;
				}			
			}
			else
			{
				alert("No data returned. Please try again")
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}


function makeAsynchSelectFeedByID(url, params, selectboxid, presetValue) 
{
	var xmlHTTPObj = false;

	if (window.XMLHttpRequest) {
		xmlHTTPObj = new XMLHttpRequest();
		if (xmlHTTPObj.overrideMimeType) {
			xmlHTTPObj.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xmlHTTPObj) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	xmlHTTPObj.onreadystatechange = function() { asynchSelectFeedByIDReceived(xmlHTTPObj, selectboxid, presetValue); };
	xmlHTTPObj.open('POST', url, true);
	xmlHTTPObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTPObj.send(params);
}

function asynchSelectFeedByIDReceived(xmlHTTPObj, selectboxname, presetValue) {
	if (xmlHTTPObj.readyState == 4) {
		if (xmlHTTPObj.status == 200) {
			var resp = trimResponse(xmlHTTPObj.responseText);
			//alert(resp);
			if(resp)
			{
				var comboBox = null;
				try {
					comboBox = MM_findObj(selectboxname);
				}
				catch (e) 
				{
					alert("Unable to locate destination object");
					return false;
				}
				var optional = resp.split(":_:");
				while(comboBox.options.length) comboBox.options[0] = null;
				var terminator = optional.length % 2;
				if(terminator == 1)
				{
					for(mdx=0; mdx<optional.length - 1; mdx+=2)
					{
						var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
						comboBox.options[mdx/2] = newoption;
					}				
					comboBox.value=optional[mdx];
				}
				else
				{
					for(mdx=0; mdx<optional.length; mdx+=2)
					{
						var newoption = new Option(optional[mdx], optional[mdx+1], true, true);
						comboBox.options[mdx/2] = newoption;
					}	
					if (comboBox.length)
					{
						comboBox.selectedIndex=0;
					}
					else
					{
						comboBox.selectedIndex=-1;
					}								
				}
				if (presetValue)
				{
					comboBox.value=presetValue;
				}			
			}
			else
			{
				//alert("No data returned. Please try again")
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}
