var w3 = document.getElementById ? true : false;
var ns = document.layers ? true : false;
var ie = document.all ? true : false;

function showmenuie5(e){
	//Find out how close the mouse is to the corner of the window
	var rightedge=ie5? document.documentElement.clientWidth-event.clientX : window.innerWidth-e.clientX
	var bottomedge=ie5? document.documentElement.clientHeight-event.clientY : window.innerHeight-e.clientY

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<menuobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		menuobj.style.left=ie5? document.documentElement.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth+ "px";
	else
		//position the horizontal position of the menu where the mouse was clicked
		menuobj.style.left=ie5? document.documentElement.scrollLeft+event.clientX : window.pageXOffset+e.clientX+ "px";
		
	//same concept with the vertical position
	if (bottomedge<menuobj.offsetHeight)
		menuobj.style.top=ie5? document.documentElement.scrollTop+event.clientY-menuobj.offsetHeight : window.pageYOffset+e.clientY-menuobj.offsetHeight+ "px";
	else
		menuobj.style.top=ie5? document.documentElement.scrollTop+event.clientY : window.pageYOffset+e.clientY+ "px";

	menuobj.style.visibility="visible"
	return false
}

function hidemenuie5(e){
	menuobj.style.visibility="hidden"
}

function highlightie5(e){
	var firingobj=ie5? event.srcElement : e.target
	if (firingobj.className=="cntxtmenuitems"||ns6&&firingobj.parentNode.className=="cntxtmenuitems"){
		if (ns6&&firingobj.parentNode.className=="cntxtmenuitems") firingobj=firingobj.parentNode //up one node
			firingobj.style.backgroundColor="highlight"
			firingobj.style.color="white"
		if (display_url==1)
			window.status=event.srcElement.title
	}
}

function lowlightie5(e){
	var firingobj=ie5? event.srcElement : e.target
	if (firingobj.className=="cntxtmenuitems"||ns6&&firingobj.parentNode.className=="cntxtmenuitems"){
		if (ns6&&firingobj.parentNode.className=="cntxtmenuitems") firingobj=firingobj.parentNode //up one node
			firingobj.style.backgroundColor=""
			firingobj.style.color="black"
		window.status=''
	}
}


function popUp(strURL,strHeight,strWidth) {
	strOptions="height="+strHeight+",width="+strWidth+",location=yes,status=yes,scrollbars=yes,resizable=yes";
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();
}

//dynamic status message
function SetStatus (StatusText) {
	var objDynaMsg=document.getElementById('dynamsgtext');
	if (StatusText.lastIndexOf('...')>0) {
		StatusText+=' <img src=images/wait.gif width=94 height=17>'
	}
	if (objDynaMsg!=null) {
		objDynaMsg.innerHTML=StatusText;
	}
}

//will set the message and hide the element
function ProcessingFinished (StatusText) {
	SetStatus(StatusText);
	var objDynaMsg=document.getElementById('dynamsgtext');
	if (objDynaMsg!=null){
		document.getElementById('dynamsg').style.display='none'
	}
}

function isValidInteger (n) {
	return ((n%1==0) && (n > 0));
}

function isValidDecimal(n) {
//    var testRegEx = /[0-9]{1,8}\.?[0-9]{0,2}/
    var testRegEx = /^\d+(\.\d{1,2})?$/
    return testRegEx.test(n)
}

function isValidNumber(n) {
    return ((n % 1 >= 0) && (n >= 0));
}


function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}


function checkForValue (d) {
	//checks for null, empty, and whitespace only
	var reWhitespace = /^\s+$/
	return !(d == null || d.length==0 || reWhitespace.test(d));
}


function AddEventHandler(obj, eventName, functionNotify) {
    if (obj.attachEvent) {
        obj.attachEvent('on' + eventName, functionNotify);
    }
    else if (obj.addEventListener) {
        obj.addEventListener(eventName, functionNotify, true);
    }
    else {
        obj['on' + eventName] = functionNotify;
    }
}

function RemoveEventHandler(obj, eventName, functionNotify) {
    if (obj.detachEvent) {
        obj.detachEvent('on' + eventName, functionNotify);
    }
    else if (obj.removeEventListener) {
        obj.removeEventListener(eventName, functionNotify, true);
    }
    else {
        obj['on' + eventName] = null;
    }
}

function showLocalCurrencyValue(BaseValue,DisplayAt){
	if ((document.forms['main'])) {//check if form is present...
		var BaseValueElement = document.getElementById(BaseValue);
		if(FXBuyRate == null){return;}
		if(BaseValue == null){return;}
		if(BaseValueElement.value == null){return;}
		BaseValue = trim(BaseValueElement.value);
		BaseValueElement.value = BaseValue;
		if (BaseValue < 0) {
			convertedvalue =0
		} else {
			convertedvalue = BaseValue / FXBuyRate;
		}
		
		if (isValidDecimal(BaseValue)==false) {
			convertedvalue = 0;
		}

		convertedvalue = (Math.round(convertedvalue * 100) / 100);
		if (convertedvalue.toFixed) {
			convertedvalue = convertedvalue.toFixed(2);
		} else {
			convertedvalue = (Math.round(convertedvalue*100)/100);
		}
		if( (isNaN(convertedvalue)) || convertedvalue == 0){convertedvalue = "0.00";}
		document.main.amount.value=convertedvalue;
		if (document.getElementById(DisplayAt)) {
			document.getElementById(DisplayAt).innerHTML=convertedvalue;
		}
		if(ie){
		if((document.all(DisplayAt)!=null)){document.all(DisplayAt).innerHTML=convertedvalue;}
		}else if(w3){if((document.getElementById(DisplayAt)!=null)){document.getElementById(DisplayAt).innerHTML=convertedvalue;}
		}else if(ns){if((document.layers[DisplayAt]!=null)){document.layers[DisplayAt].document.open();document.layers[DisplayAt].document.write(convertedvalue);document.layers[DisplayAt].document.close();}
		}
	}
}
