// GLOBAL VARIABLES
var child;
var child2;

function submitForm() 
{
    document.form.submit();
}

function submitOpener() 
{
    window.opener.document.form.H05.value = 'OrderFinalize';
    window.opener.document.form.submit();
    window.close();	
}

function searchOpener(kwords) 
{
    window.opener.document.form.QkSearchkeywords.value = kwords;
    window.opener.document.form.action.value = 'QuickSearch';
    window.opener.document.form.submit();
    window.close();	
}

function closeCAWSEMsg(id) {
	document.getElementById(id).style.display = "none";
 
}

function openCAWSEMsg(id) {
	document.getElementById(id).style.display = "";
 
}

function ClosePopup() 
{
	if (child)
	{
		child.close();
	}
	if (child2)
	{
		child2.close();
	}
}

//function ClosePopup()
//{
//	if (document.form.html06.value)
//	{
//		if (window.opener)
//		{
//			window.opener.document.form.html06.value = document.form.html06.value;
//		}
//	}
//}

function LoadPopup()
{
	if (document.form.ClosePopup.value == 'ClosePopup')
	{
		window.opener.document.form.jcapg.value = document.form.ClosePopupPage.value;
		window.opener.document.form.html06.value = document.form.html06.value;
		window.opener.document.form.submit();
		window.close();
	}
}

function GetObject(id)
{
	// This function checks for DOM strategy, then 
	// returns an object reference.
	if (document.all)
	{
		return document.all[id];
	}
	else if(document.layers)
	{
		return document.layers[id];
	}
	else
	{
		return document.getElementById(id);
	}
}

// OPEN WINDOW WITH NO MENU ETC.
function OpenWindow(url, name, width, height)
{
	child = window.open( url, name,"titlebar=no, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, dependent=yes, width=" + width + ", height=" + height );
	child.moveTo(50,50);
	child.focus();
}

function OpenWindow2(url, name, width, height)
{
	child2 = window.open( url, name,"titlebar=no, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, dependent=yes, width=" + width + ", height=" + height );
	child2.moveTo(50,50);
	child2.focus();
}

function OpenProductPopup( url, name)
{
	OpenWindow( url, name, '575', '600');
}

function OpenProductPopup2( url, name)
{
	OpenWindow2( url, name, '575', (screen.availHeight - 100));
}

function couponcalc(val,am) 
{
    amount = 0.0;
    payment = 0.0;
    coupons = 0.0;
    total = 0.0;
    couptot = 0.0;

    amount = am;
    payment = paytot.innerHTML;
    coupons = coupontot.innerHTML;
    total = purchase.innerHTML;

    if (val.checked) {
        couptot = parseFloat(coupons) + parseFloat(amount);

        coupontot.innerHTML = round_decimals(couptot,2);
	coupontbltot.innerHTML = round_decimals(couptot,2);
        if (document.getElementById('couponstot') == null) {
            paytot.innerHTML = round_decimals(total - couptot,2);
	    ctot.innerHTML = round_decimals(couptot,2);
	} else {
	    paytot.innerHTML = round_decimals(total - (couptot + parseFloat(couponstot.innerHTML)),2);
       	    ctot.innerHTML = round_decimals(couptot + parseFloat(couponstot.innerHTML),2);
	}

	if (paytot.innerHTML < 0) {
	    paytot.innerHTML = '0.00';
	}

    }
    else {
        couptot = parseFloat(coupons) - parseFloat(amount);


	if (document.getElementById('couponstot') == null) {
	    paytot.innerHTML = round_decimals(total - couptot,2);
	    ctot.innerHTML = round_decimals(couptot,2);
	} else {
	    paytot.innerHTML = round_decimals(total - (couptot + parseFloat(couponstot.innerHTML)),2);
       	    ctot.innerHTML = round_decimals(couptot + parseFloat(couponstot.innerHTML),2);
	}
        coupontot.innerHTML = round_decimals(couptot,2);
	coupontbltot.innerHTML = round_decimals(couptot,2);

	if (paytot.innerHTML < 0) {
	    paytot.innerHTML = '0.00';
	}
 	
    }
    
}

function couponcalc_school(val,am) 
{
    amount = 0.0;
    payment = 0.0;
    coupons = 0.0;
    total = 0.0;
    couptot = 0.0;

    amount = am;
    payment = paytot.innerHTML;
    coupons = couponstot.innerHTML;
    total = purchase.innerHTML;

    if (val.checked) {
        couptot = parseFloat(coupons) + parseFloat(amount);

        couponstot.innerHTML = round_decimals(couptot,2);
	coupontblstot.innerHTML = round_decimals(couptot,2);
	if (document.getElementById('coupontot') == null) {
	    paytot.innerHTML = round_decimals(total - couptot,2);
	    ctot.innerHTML = round_decimals(couptot,2);
	} else {
	    paytot.innerHTML = round_decimals(total - (couptot + parseFloat(coupontot.innerHTML)),2);
            ctot.innerHTML = round_decimals(couptot + parseFloat(coupontot.innerHTML),2);
	}

	if (paytot.innerHTML < 0) {
	    paytot.innerHTML = '0.00';
	}

    }
    else {
        couptot = parseFloat(coupons) - parseFloat(amount);

        if (document.getElementById('coupontot') == null) {
	    paytot.innerHTML = round_decimals(total - couptot,2);
	    ctot.innerHTML = round_decimals(couptot,2);
	} else {
	    paytot.innerHTML = round_decimals(total - (couptot + parseFloat(coupontot.innerHTML)),2);
            ctot.innerHTML = round_decimals(couptot + parseFloat(coupontot.innerHTML),2);
	}
        couponstot.innerHTML = round_decimals(couptot,2);
	coupontblstot.innerHTML = round_decimals(couptot,2);

	if (paytot.innerHTML < 0) {
	    paytot.innerHTML = '0.00';
	}
    }
    
}


function round_decimals(original_number, decimals) 
{
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) 
{
   var value_string = rounded_value.toString()
   var decimal_location = value_string.indexOf(".")
   if (decimal_location == -1) {
   		decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
     decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function reconciliationCalc() {
	var val = 0.0;
	var chq = 0.0;
	var credit = 0.0;
	var money = 0.0;
	var tok = document.getElementById("token");
	var vch = document.getElementById("voucher");
	var tot = document.getElementById("enclosed");

	chq = parseFloat(document.getElementById("cheque").value);
	credit = parseFloat(document.getElementById("ccslip").value);
	money = parseFloat(document.getElementById("morder").value);

	if (isNaN(chq)) {
		chq = 0;
	}else{
		document.getElementById("cheque").value = round_decimals(chq,2);
	}
	if (isNaN(credit)) {
		credit = 0;
	}else{
		document.getElementById("ccslip").value = round_decimals(credit,2);
	}
	if (isNaN(money)) {
		money = 0;
	}else{
		document.getElementById("morder").value = round_decimals(money,2);
	}
	
	if ((tok != null) || (vch != null)){
		val = chq + credit + money;
		if ((tok != null) && (!isNaN(tok.innerHTML))){
			val = val + parseFloat(tok.innerHTML);
		}
		if ((vch != null) && (!isNaN(vch.innerHTML))){
			val = val + parseFloat(vch.innerHTML);
		}

		tot.innerHTML = '$' + round_decimals(val,2);
	
	}else{
		val = chq + credit + money;

		tot.innerHTML = '$' + round_decimals(val,2);
	}

}

function dhtmlHover(posObjId,parentObjId,text,show) {
	
	var obj = document.getElementById(posObjId);
	var objp = document.getElementById(parentObjId);
	var AdjustTop = -55;
	var AdjustLeft = 40;
	var div = document.getElementById('dhtmlHover');

	if (!obj) { return;}

	if (show == true) {
		// Show
		div.style.display = "";
		div.style.visibility = "visible";
		
		div.style.top = getY(obj) + AdjustTop - objp.scrollTop;
		div.style.left = getX(objp) + AdjustLeft;
		div.innerHTML = text;

	} else {
		// Hide
		div.style.display = "none";
		div.style.visibility = "collapse";
	}
}



function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}


function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}