function chkError(docXML)
{// check if the first node is an errorMsg element, or if there's any error reading the XML for that matter
	if (!docXML.documentElement)
	{
		alert("XML reply doesn't seem to be valid");
		return false;
	}
	if (docXML.documentElement.tagName == "error")
	{ //first node indicates error.  Output error message(s) and stop further processing
		//traverse all errorData nodes and output the text
		var errorQty, errorMsg, i;
		errorMsg = "";
		errorQty = docXML.documentElement.childNodes.length;
		for (i=0; i<errorQty; i++)
		{
			errorMsg = errorMsg + "\n" + docXML.documentElement.childNodes[0].childNodes[0].nodeValue;
		}
		alert(errorMsg);
		return false;
	}
	return true;
}
//------------------------------------------------------------------------
function msgNode()
{
	var msgText = null;
	var msgType = null;
}
//-----------------------------------------------------------------
function bannerMsgChk(docXML) {
   //read our xml file.
	if (docXML.documentElement.tagName == "bannerMsg")
	{	
	//if don't already exist, create a div to hold the small messages
		var notesDiv;
		if	(document.getElementById('notificationsAreaDiv'))
			notesDiv = document.getElementById('notificationsAreaDiv');
		else
		{
			notesDiv = document.createElement('div')
			notesDiv.setAttribute('id', 'notificationsAreaDiv');
			notesDiv.setAttribute('style','position:fixed;z-index:99');
			notesDiv.style.bottom="2%";
			notesDiv.style.right="5%";
			document.body.appendChild(notesDiv);
		}
		//get number of messages
	   var baseElement = docXML.documentElement.childNodes;
	   var msgNo = baseElement.length;
	   var counter1, counter2;
	   counter1= 0;
	   
	   //make array to contain messages
	   var msgAr = new Array();
	   var text, type;
	   while (counter1 < msgNo)
	   {
			counter2 = 0;
			var nodeTypes = baseElement[counter1].childNodes.length;
		   //clear variables	
			text = null;
			type = null;
			
		   while (counter2 < nodeTypes)
		   {					
				if (baseElement[counter1].childNodes[counter2].tagName == "text")
					text = docXML.documentElement.childNodes[counter1].childNodes[counter2].childNodes[0].nodeValue;
				
				if (baseElement[counter1].childNodes[counter2].tagName == "type")
					type = docXML.documentElement.childNodes[counter1].childNodes[counter2].childNodes[0].nodeValue;
			   counter2++
		   }
			var node = new msgNode()
			node.msgText = URLDecode(text);
			node.msgType = type;
			msgAr[counter1] = node;
			counter1++;
	   }
	   var divID = "banner" + Math.floor(Math.random()*100)
	   var div = document.createElement("div");		   
   
	   div.setAttribute('id', divID);
	   div.style.padding = '3px';
	   //for every item in msgAr, create a new Span.
	   //and assign a style according to the type of message
	   
	   for (counter1 = 0; counter1 < msgAr.length; counter1++)
	   {
			var span = document.createElement("span");
			span.appendChild(document.createTextNode(msgAr[counter1].msgText));					
		
			if (msgAr[counter1].msgType == 'error')
			{
			// give one style for error
				span.setAttribute('class','bannerMsgChkRed');
			}
			if (msgAr[counter1].msgType == 'info')
			{
			//give one style for info
				span.setAttribute('class','bannerMsgChkGreen');
			}
			div.appendChild(span);
	   }
	   //this span contains the message		   
	   notesDiv.appendChild(div);
	   //remove div
	   setTimeout('removeDiv("'+divID+'")', 4000);
	   //refresh page for session setting
	   return false;
	 }		 
	 return true;
}
 //-----------------------------------------------------------------
function removeDiv(divID){
	var div = document.getElementById(divID);
   if (div) {
	   div.parentNode.removeChild(div);
   }  
}
//------------------------------------------------------------------   
function chkKeypress(e, chkCharacter, f)
{	
	var characterCode;
	var fr = f;
	if(e && e.which) // NN4 specific code
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event
		characterCode = e.keyCode; // IE specific code
	}
	if (characterCode == chkCharacter) //// Enter key is 13
	{
		e.returnValue=false;
		e.cancelBubble = true;
		fr();
		return false;
	}     
}
//------------------------------------------------------------------
//this function initialises AJAX object for various browsers
function ajaxInit() {
	var xmlHttp;
	try {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp = new XMLHttpRequest();				
	}
	catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");			
		}
		catch (e) {
			try {
				// code for IE6, IE5
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");				
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}
	return xmlHttp;
}
//------------------------------------------------------------------------
//URL Decode function, used in lots of JS files
function URLDecodeOLD(tempStr)
{
	var lsRegExp = /\+/g;	
	//decode superscript character
	var temp = decodeURI(tempStr);			
	// Return the decoded string
	return unescape(String(temp).replace(lsRegExp, " "));
}


function URLDecode(encoded) {
	return  decodeURIComponent(encoded.replace(/\+/g,  " "));
}
//------------------------------------------------------------------------
//Urlencode functions
function URLencodeOLD(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function URLencode(unencoded) {
	return  encodeURIComponent(unencoded);
}
//------------------------------------------------------------------------
function getWindowWidth() {
	if (document.body && document.body.offsetWidth) 
	{
		winW = document.body.offsetWidth;
	}
	if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) 
	{
		winW = document.documentElement.offsetWidth; 
	}
	if (window.innerWidth) 
	{
		winW = window.innerWidth;
	}
	return winW;
}
//------------------------------------------------------------------------
function getWindowHeight() 
{
	if (document.body && document.body.offsetWidth) {
		winH = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' && document.documentElement) {
		winH = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
		winH = window.innerHeight;
	}
	return winH;
}
//------------------------------------------------------------------------
function getIEVersionNumber() {
    var ua = navigator.userAgent;
	var tempV;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        tempV = 0;
    } else {
        tempV = parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }		
	if (navigator.appName == "Microsoft Internet Explorer")
	{	
		if(tempV <= 7)
		{
			alert("Best Viewed in IE 8 and above, Google Chrome, Mozilla Firefox or Opera.");
		}
	}
}
//============================= pop up start =============================
function dragStart(event, id) 
{

	var el;
	var x, y;
	// If an element id was given, find it. Otherwise use the element being
	// clicked on.
	if (id)
		dragObj.elNode = document.getElementById(id);
	else {
		if (browser.isIE)
			dragObj.elNode = window.event.srcElement;
		if (browser.isNS)
			dragObj.elNode = event.target;
		// If this is a text node, use its parent element.
		if (dragObj.elNode.nodeType == 3)
			dragObj.elNode = dragObj.elNode.parentNode;
	}

	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
		  + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		  + document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Save starting positions of cursor and element.

	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 0);
	dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 0);

	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = +540  //  +540;  //+700
	if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;

	if (browser.isIE) {
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup", dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		document.addEventListener("mousemove", dragGo, true);
		document.addEventListener("mouseup", dragStop, true);
		event.preventDefault();
	}
}
function dragGo(event) {

	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
		  + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		  + document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	// Move drag element by the same amount the cursor has moved.

	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";

	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS)
		event.preventDefault();
}
	
function dragStop(event) {

	// Stop capturing mousemove and mouseup events.	
	if (browser.isIE) {
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup", dragStop);
	}
	if (browser.isNS) {
		document.removeEventListener("mousemove", dragGo, true);
		document.removeEventListener("mouseup", dragStop, true);
	}
}
	
var browser = new Browser();
// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;
function Browser() 
{
	var ua, s, i;
	this.isIE = false;
	this.isNS = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	// Treat any other "Gecko" browser as NS 6.1.		
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}
//============================== pop up end ==============================
//---------------------------------------------------------------------------------------------------------
function displayCurrency() {
	var xmlHttp = ajaxInit();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.responseXML.documentElement != null) {
				displayCurrencyXML(xmlHttp.responseXML)
			}
		}
	};
	xmlHttp.open("GET", "/currencyRate.ashx?function=displayCurrency", true);
	xmlHttp.send(null);
	return false;
}
//---------------------------------------------------------------------------------------------------------
function displayCurrencyXML(xmlDoc) {
	var currencyList = new Array();
	var xmlObj = xmlDoc.getElementsByTagName("entryList");
	var xmlLength = xmlDoc.getElementsByTagName("entryList")[0].getElementsByTagName("entry").length;
	var currencyData = xmlDoc.getElementsByTagName("entryList")[0].getElementsByTagName("entry");

	if (xmlDoc.getElementsByTagName("currencyInfo")[0].getElementsByTagName("selectedCurrencyCode")[0].childNodes[0].nodeValue != null) {
		var selectedCurrency = xmlDoc.getElementsByTagName("currencyInfo")[0].getElementsByTagName("selectedCurrencyCode")[0].childNodes[0].nodeValue;
	}
	else
		var selectedCurrency = "SGD";

	for (counter = 0; counter < xmlLength; counter++) {
		currencyId = currencyData[counter].getElementsByTagName("currencyId")[0].childNodes[0].nodeValue; //currencyId
		currencyCode = currencyData[counter].getElementsByTagName("currencyCode")[0].childNodes[0].nodeValue; // currencyCode

		var span = document.createElement("span");
		span.setAttribute('id', 'currencySpan');	            
		var li = document.createElement("li");
		var a = document.createElement("a");	            
		li.setAttribute('onclick', 'changeCurrency("' + currencyCode + '",' + currencyId + ')');
		li.setAttribute('style', 'cursor:pointer;');
		li.appendChild(document.createTextNode(currencyCode));
		a.appendChild(li);
		span.appendChild(a);
		document.getElementById("showCurrencyTypeDiv").appendChild(span);
	}
	var span = document.createElement("span");
	span.setAttribute("id", "currentType");
	var a = document.createElement("a");
	a.setAttribute('onclick', 'showType()');
	a.appendChild(document.createTextNode("Selected Currency: " + selectedCurrency));
	span.appendChild(a);
	document.getElementById("currencyDiv").appendChild(span);
}
//---------------------------------------------------------------------------------------------------
function changeCurrency(currencyCode, currencyId) {
	//alert("changeCurrency: " + currencyId);
	document.getElementById("showCurrencyTypeDiv").style.display = "none";
	//clear existing value and replace with selected currency
	if (document.getElementById("currentType")) {
		document.getElementById("currencyDiv").removeChild(document.getElementById("currentType"));
	}
	var span = document.createElement("span");
	span.setAttribute("id", "currentType");
	var a = document.createElement("a");
	a.setAttribute('onclick', 'showType()');
	a.appendChild(document.createTextNode("Selected Currency: " + currencyCode));
	span.appendChild(a);
	document.getElementById("currencyDiv").appendChild(span);
	//change with chosen currency	      
	saveType(currencyCode, currencyId);	       
   // setTimeout('location.reload(true)', 8000); 
}
//---------------------------------------------------------------------------------------------------------
function saveType(currencyCode, currencyId) {	        
	var xmlHttp = ajaxInit();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {	                
			bannerMsgChk(xmlHttp.responseXML);	                
		}
	};
	xmlHttp.open("GET", "/currencyRate.ashx?function=saveCurrencyType&currId=" + currencyId, true);
	xmlHttp.send(null);
	return false;
}
//---------------------------------------------------------------------------------------------------------
function showType() {
	if (!(checkClose))
	{
		checkClose = 1;
		document.getElementById("showCurrencyTypeDiv").style.display = "block";
	}
	else
	{
		checkClose = 0;
		document.getElementById("showCurrencyTypeDiv").style.display = "none";
	}
} 

