<!-- Peter_Javascript.js -->

var windowname;

function ChangePage(Pg)
{
	document.location = Pg;
}
function loadDefault(){

	var defaultAt, lenURL, theLoc;

	// Tested with IR site

	theLoc = window.parent.location.toString();


	lenURL = theLoc.length;


	defaultAt = theLoc.lastIndexOf('default.html',lenURL);

	//	alert(defaultAt);


	if (defaultAt < 0)
	{  
	//	alert("reloading");
	//	window.parent.location = "default.html";
		ChangePage("default.html");
	}	

}
function SubmitForm(FormIn)
{

	var x,strForm;
	
	strForm = "document.forms['" + FormIn + "']";
	
	x = eval(strForm);
	
	x.submit();

}
// ****** Verification Functions ******
function CalculateShipping(formname)
{

	var x,d,cutoff,qty,US0,US1,US2,CN0,CN1,CN2,MX0,MX1,MX2,RW0,RW1,RW2,msg;

	// Tested with IR site

	x = document.forms[formname];
	
	qty=x.elements["totalquantity"].value;	
		
	d = qty.lastIndexOf('.',x.elements["totalquantity"].length);
	
	if (d>0)
	{
		msg="Quantity entered cannot be a fraction";
		alert(msg);
		return;
	}
	
	if ((qty==null) || (qty==""))
	{
		msg="Record quantity needed to view adjustment";
		alert(msg);
		return;
	}	
	if (!(checkNumerical(qty)))
	{
		msg="Quantity entered must be a positive number";
		alert(msg);
		return;
	}
	

	//Amounts from form for shipping first, additional items	
	
	cutoff=x.elements["cutoff"].value;
	cutoff = parseFloat(cutoff);
	
	qty = parseFloat(qty);

	US1=x.elements["US1"].value;
	US1 = parseFloat(US1);
	
	US2=x.elements["US2"].value;	
	US2 = parseFloat(US2);
		
	CN1=x.elements["CN1"].value;
	CN1 = parseFloat(CN1);	
	
	CN2=x.elements["CN2"].value;
	CN2 = parseFloat(CN2);	
	
	MX1=x.elements["MX1"].value;
	MX1 = parseFloat(MX1);	
	
	MX2=x.elements["MX2"].value;
	MX2 = parseFloat(MX2);	
	
	RW1=x.elements["RW1"].value;
	RW1 = parseFloat(RW1);	
	
	RW2=x.elements["RW2"].value;
	RW2 = parseFloat(RW2);	

	//d now stands for the basic Paypal shipping amount;
	//we calculate this by pretending cutoff=quantity;
	d = calcShippingTotal(US1,US2,qty,qty);	
	
	US0 = calcShippingTotal(US1,US2,qty,cutoff);
	x.elements["USShip"].value = "" + OutputCurrency(US0);
	CN0 = calcShippingTotal(CN1,CN2,qty,cutoff);
	x.elements["CNShip"].value =  "" + OutputCurrency(CN0);
	MX0 = calcShippingTotal(MX1,MX2,qty,cutoff);
	x.elements["MXShip"].value = "" + OutputCurrency(MX0);
	RW0 = calcShippingTotal(RW1,RW2,qty,cutoff);
	x.elements["RWShip"].value = "" + OutputCurrency(RW0);

	x.elements["USBasic"].value = "" + OutputCurrency(d);
	x.elements["CNBasic"].value = "" + OutputCurrency(d);
	x.elements["MXBasic"].value = "" + OutputCurrency(d);
	x.elements["RWBasic"].value = "" + OutputCurrency(d);
	
	
	x.elements["USAdj"].value= "US$ " + OutputCurrency(calcAdjustment(US0,d));	
	x.elements["CNAdj"].value= "US$ " + OutputCurrency(calcAdjustment(CN0,d));	
	x.elements["MXAdj"].value= "US$ " + OutputCurrency(calcAdjustment(MX0,d));	
	x.elements["RWAdj"].value= "US$ " + OutputCurrency(calcAdjustment(RW0,d));	
	
	
	if(qty > cutoff)
	{
	msg = "The adjustment reflects a shipping charge rebate for the quantity beyond " + cutoff;
	alert(msg);
	}
	
	return;	
	

}
function PaypalPost(formname,itemname,amt)
{

	var x,y,z,a;
	
	x = document.forms[formname];
	
	
	if(( formname == '' ) || ( formname == null ))
	{
		return false;
	}
	
	CalculateShipping(formname);
	
	
	y = x.elements["totalquantity"].value;
	
	if(( y == '' ) || ( y == null ))
	{
		alert("Enter a record quantity to calculate adjustment.");
		return false;
	}
	
	z = itemname + " for " + y + " records";	
	
	x.elements["item_name"].value = z;
	
	a = x.elements[amt].value;
	
	//alert(a.length);
	
	a = a.substr(4,a.length);
	
	//alert(parseFloat(a));
	
	if( parseFloat(a) > 0 )
	{
	

		x.elements["amount"].value = parseFloat(a);
		return true;	
	}
	
	if( parseFloat(a) < 0 )
	{
		alert("Cannot post a negative amount to Paypal.");
	}
	return false;
	
}
// ****** Verification Functions ******
function checkNumerical(pplAmt)
{

	if ((pplAmt==null) || (pplAmt==""))
	{
		return true;
	}
	if (pplAmt < 0)
	{
		alert("Negative number not valid: " + pplAmt);
		return false;
	}
	if (isNaN(pplAmt)==true)
	{
		alert(pplAmt + " is not a number.");
		return false;
	}

	
	if (pplAmt > 0)
	{
		return true;
	}
	else
	{

		return false;
	}


}
// ****** Calculation Sub-Functions ******
function calcShippingTotal(first,additional,quantity,cutoff)
{

	var retVal,downAmt;
	
	downAmt = 0;
	
	retVal = quantity * additional;
	
	retVal = retVal - additional;
	
	retVal = retVal + first;
	
	if (quantity>cutoff)
	{
	
		downAmt = calcDownAmt(quantity,cutoff,additional);	
	}	
	
	retVal = retVal - downAmt;
		
	return retVal;

}
function calcAdjustment(shipping,basic)
{

	var retVal;
	
	retVal =0;
	
	
	if (shipping!=basic)
	{
		retVal = shipping-basic;
	}
	
	
	return retVal;
	
}
function calcDownAmt(quantity,cutoff,additional)
{

	var retVal,downQty;
	
	downQty = quantity-cutoff;
	
	retVal = downQty * additional;

	return retVal;
	
}
// ****** Output Formatting Functions ********
function OutputCurrency(amtIn)
{
	var amtOut,thedot;
	
	if(isNaN(amtIn)==false)
	{
		amtOut = parseFloat(amtIn);
	}
	if(isNaN(amtOut)==true)
	{
		amtOut = "";
		return amtOut;
	}

	amtOut= amtOut * 100;
	amtOut= Math.round(amtOut);
	amtOut= amtOut / 100;

	amtOut = amtOut.toString();

	thedot = 0
	thedot = amtOut.lastIndexOf('.',amtOut.length);

	//	alert("the amount is " + amtOut);
	//	alert("the dot " + thedot);
	//	alert("the length is " + amtOut.length);

	if (thedot < 0)
	{
		amtOut = amtOut.concat('.00');
	}
	thedot = amtOut.lastIndexOf('.',amtOut.length);
	if (amtOut.length < (thedot + 3))
	{
		amtOut= amtOut.concat('0');
	}
	if (amtOut.length < (thedot + 2))
	{
		amtOut= amtOut.concat('0');
	}
	if (thedot > 3)
	{
		amtOut= amtOut.substr(0,thedot + 3);
	}

	return amtOut;
}
