// JavaScript Document

function checkPayPal(){
	var amount=-1;
	var country="";
	var ship=-1;
	var act=-1;
	
	var total=0;
	var shipping=0;
	var shipDesc="";
	var itemCost=0;
	var itemDesc="";

	
	for(i=0;i<6;i++){
		if(window.document.orderForm.packs[i].checked==true){
			amount=window.document.orderForm.packs[i].value;
			break;
		}
	}
	ship    = (""+window.document.orderForm.ship.value);
	//Shipping costs
	if(ship==1){shipping=9.95; shipDesc="UPS Ground Shipping - $" + shipping + " - USA ONLY";}//Ground
	if(ship==2){shipping=16.95; shipDesc="UPS Priority Mail - $" + shipping + " - USA ONLY";}//Priority
	if(ship==4){shipping=34.95; shipDesc="INTERNATIONAL UPS Priority Mail - $" + shipping + "";}//International
	//item costs and item description to be passed to Paysystems.
	//alert(	amount + "\n"+ shipping + "\n" + window.document.orderForm.bulk.value);
	
	if(amount=="VPRX_1"){itemCost=59.95; itemDesc="1 Bottle of MacaEnhancer - $" + itemCost + " ";}
	if(amount=="VPRX_2"){itemCost=109.95;itemDesc="2 Bottles of MacaEnhancer - $" + itemCost + " ";}
	if(amount=="VPRX_3"){itemCost=154.95;itemDesc="3 Bottles of MacaEnhancer - $" + itemCost + " + 1 FREE Bonus Bottle - ";}
	if(amount=="VPRX_4"){itemCost=199.95;itemDesc="4 Bottles of MacaEnhancer - $" + itemCost + " + 1 FREE Bonus Bottle - ";}
	if(amount=="VPRX_5"){itemCost=245.95;itemDesc="5 Bottles of MacaEnhancer - $" + itemCost + " + 2 FREE Bonus Bottles  - ";}
	
	
	//window.document.orderForm.description.value=(itemDesc + "\n " + shipDesc);
	window.document.orderForm.amount.value=round_decimals((itemCost+shipping), 2)
	

}

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) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
