/*
function alert_price(){
	if(document.paypalform.book1.value == "15.00"){
		alert("hi");		
		var qty = parseInt(document.paypalform.qty.value);
		var tot_amnt = 0.00;
		tot_amnt = roundToPennies(qty*15.00);
		var valuespan = document.getElementById("qtot1");
		valuespan.innerText = tot_amnt;		
	}
	
}
*/

var order = new Object(); //Object to store order Items in
var root = new Object(); // Selection criteria
var on = true;
var off = false;

var cntr = 0; //Items in object
var opts = 5; // Number of options to allow
var shpr = function(wt){return 0;} //what to charge for shipping
var stxt = ""; // Shipping text;
var spos = -1; //Shipping position selector
var ttax = 5;  //Percent for taxes
var tpos = -1; //tax position selector
var tamt=0,tqty=0,twgt=0,thnd=0; //totals

/*
root.hamt =0; //Amount, below which handling charge applies
root.hand = 0; //Handling charge for orders less than  hamt
root.shp = off; //Shipping selection line
root.tax = off; //tax selection line
//Place for user-specific options
root.xx_can = "";		//Place for Paypal cancel retrun path
root.xx_cur = "US"; 	//Enter the defaul currency code (or null)
root.xx_id = "admin@domain.com";	//Live email goes here

root.xx_img = ""; //Image URL
root.xx_lc = "US";	//Enter default country code(or null)
root.xx_ret = "";	//Place for paypal return url path.
root.xx_sty = "";	//Place for paypal page style.
*/

/*
function CalcRoot(){
	if(root.shp) spos = 0;
	if(root.tax) tpos = 0;
}
*/
function Dollar(val){ //Force for a valid dollar amount
var str,pos,rnd=0;
	if(val<.995) rnd =1; //For old Netscape browsers
	str = escape(val*1.0 + 0.005001 + rnd); //Float,round,escape.
	pos = str.indexOf(".");	//Should be one, but OK if not
	if(pos>0) str = str.substring(rnd,pos+3);
	return str;		//return valid string.	

/*
 pennies = val * 100;
 pennies = Math.round(pennies);
 strPennies = "" + pennies;
 len = strPennies.length;
 return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);
*/
}

function GetOrder(id) {
    if ((id == "book1") && (document.getElementById("qty1").value > 0)) {
        var qty = parseInt(document.getElementById("qty1").value);
        var tot_amt = 0.00;
        tot_amt = Dollar(qty * 15.00);
        //		var valuestot = document.getElementById("qtot1");
        document.getElementById("qtot1").value = tot_amt;
    }  
    if ((id == "book2") && (document.getElementById("qty2").value > 0)) {
        var qty = parseInt(document.getElementById("qty2").value);
        var tot_amt = 0.00;
        tot_amt = Dollar(qty * 25.00);
        document.getElementById("qtot2").value = tot_amt;
    } 
    if ((document.getElementById("qty1").value == 0))
        document.getElementById("qtot1").value = 0;
    if ((document.getElementById("qty2").value == 0))
           document.getElementById("qtot2").value = 0;

//    tot1 = parseInt(document.getElementById("qtot1").value);
//    tot2 = parseInt(document.getElementById("qtot2").value);
//    var total = 0;
//    var shipping = 0;
//    var ship_euro = 0;
//    var cn = document.getElementById("country").value;
//    var qtybook1 = document.getElementById("qty1").value;
//    var qtybook2 = document.getElementById("qty2").value;
//    var qb1 =0 ;
//    var qb2 = 0;

    
}

function Regiontotal() {
    if ((document.getElementById("qty1").value == 0))
        document.getElementById("qtot1").value = 0;
    if ((document.getElementById("qty2").value == 0))
        document.getElementById("qtot2").value = 0;
        
    tot1 = parseInt(document.getElementById("qtot1").value);
    tot2 = parseInt(document.getElementById("qtot2").value);
    var total = 0;
    var shipping = 0;
    var ship_euro = 0;
    var cn = document.getElementById("country").value;
    var qtybook1 = document.getElementById("qty1").value;
    var qtybook2 = document.getElementById("qty2").value;
    var qb1 = 0;
    var qb2 = 0;
    if (cn == "Sweden") {
        if (qtybook1 > 0)
            qb1 = getShipping(qtybook1, cn);
        if (qtybook2 > 0)
            qb2 = getShipping(qtybook2, cn);     
    }
    else if (cn == "Europe") {
        if (qtybook1 > 0)
            qb1 = getShipping(qtybook1, cn);
        if (qtybook2 > 0)
            qb2 = getShipping(qtybook2, cn);
    }
    else if (cn == "rs") {
        if (qtybook1 > 0)
            qb1 = getShipping(qtybook1, cn);
        if (qtybook2 > 0)
            qb2 = getShipping(qtybook2, cn);
    }
    else if (cn == "us") {
        if (qtybook1 > 0)
            qb1 = getShipping(qtybook1, cn);
        if (qtybook2 > 0)
            qb2 = getShipping(qtybook2, cn);
    }
    document.getElementById("shipcost").value = (parseInt(qb1) + parseInt(qb2));
    
    var vat_cost = 0;
    if (cn == "Sweden" || cn == "Europe") {
        //vat_cost = Dollar((tot1 + tot2) * 6 / 100);
        var scost = parseInt(document.getElementById("shipcost").value);
        vat_cost = Dollar((tot1 + tot2 + scost) * (6 / 100));
        //total = tot1 + tot2 + parseFloat(vat_cost) + parseInt(document.getElementById("shipcost").value);
        total = tot1 + tot2 + parseFloat(vat_cost) + parseInt(document.getElementById("shipcost").value);
    }
    else {
        vat_cost = "0.00";
        total = tot1 + tot2 + parseInt(document.getElementById("shipcost").value);
    }
    //+ tot2 + tot3 + shipping;
    if (parseFloat(vat_cost) > 0 && parseFloat(vat_cost) < 1)
        vat_cost = "0" + vat_cost;
    document.getElementById("vatcost").value = vat_cost;
    document.getElementById("total").value = Dollar(total);

    if ((document.getElementById("qtot1").value == 0) && (document.getElementById("qtot2").value == 0))
        document.getElementById("total").value = 0;
    //    if ((document.getElementById("qtot2").value == 0))
    //        document.getElementById("total").value = 0;
}

function getShipping(quantity, cntry) {
    var shipping = 0;
    var ship_euro = 0;
       
    if (cntry == "Sweden") {
        if (quantity == 1)
            shipping = Dollar(36);
        else if ((quantity == 2) || (quantity == 3))
            shipping = Dollar(60);
        else if ((quantity > 3) && (quantity <= 7))
            shipping = Dollar(84);
        else if ((quantity > 7 && quantity <= 10))
            shipping = Dollar(150);
        else if ((quantity > 10 && quantity <= 30))
            shipping = Dollar(225);
        else if (quantity > 30)
            shipping = (((quantity - 1) * 9) + 43.6) / 11;
    }
    if (cntry == "Europe") {
        if (quantity == 1)
            shipping = Dollar(75);
        else if ((quantity == 2) || (quantity == 3))
            shipping = Dollar(152);
        else if ((quantity > 3) && (quantity <= 7))
            shipping = Dollar(232);
        else if ((quantity > 7 && quantity <= 10))
            shipping = Dollar(320);
        else if ((quantity > 10 && quantity <= 30))
            shipping = Dollar(530);
        else if (quantity > 30)
            shipping = (((quantity - 1) * 9) + 43.6) / 11;
    }
    if (cntry == "rs") {
        if (quantity == 1)
            shipping = Dollar(85);
        else if ((quantity == 2) || (quantity == 3))
            shipping = Dollar(182);
        else if ((quantity > 3) && (quantity <= 7))
            shipping = Dollar(277);
        else if ((quantity > 7 && quantity <= 10))
            shipping = Dollar(482);
        else if ((quantity > 10 && quantity <= 30))
            shipping = Dollar(1068);
        else if (quantity > 30)
            shipping = (((quantity - 1) * 9) + 43.6) / 11;
    }
    if (cntry == "us") {
        if (quantity == 1) 
            shipping = Dollar(4);
        else if ((quantity == 2) || (quantity == 3))
            shipping = Dollar(10);
        else if ((quantity > 3) && (quantity <= 5))
            shipping = Dollar(16);
        else if (quantity > 5) {
            shipping = (((quantity - 1) * 9) + 43.6) / 11;
        }
    }
    if ((quantity <= 30) && (cntry != "us"))
        ship_euro = (parseInt(shipping) + 9) / 11;
    else      
        ship_euro = parseInt(shipping);
    return Dollar(ship_euro);
}

function SendCart() {
    if (document.getElementById("total").value == 0) {
        alert("Please enter the quantity");
        document.getElementById("qty1").focus();
    }
//    if ((document.getElementById("qty1").value > 30) || (document.getElementById("qty2").value > 30)) {
//        document.aspnetForm.action = "http://www.lotscompany.com/EN/books.aspx?page=inquiry";
//        document.aspnetForm.submit();
//    }
//    
    else {
        total = Dollar(document.getElementById("total").value);

        /*Individual values for each and every book */
        book1 = Dollar(15.00);
        book2 = Dollar(25.00);
        //book3 = Dollar(20.00);
        /* Quantity values to be passed from the form*/
        //alert(document.getElementById("qty1").value);
        var q1 = document.getElementById("qty1").value;
        var q2 = document.getElementById("qty2").value;
        if ((q1 != "") && (q1 !== null))
            qt1 = parseInt(document.getElementById("qty1").value);
        else
            qt1 = 0;
        //var q2 = document.getElementById("qty2").value;
        if ((q2 != "") && (q2 !== null))
            qt2 = parseInt(document.getElementById("qty2").value);
        else
            qt2 = 0;
        //        var q3 = document.getElementById("qty3").value;
        //        if ((q3 != "") && (q3 !== null))
        //            qt3 = parseInt(document.getElementById("qty3").value);
        //        else
        //            qt3 = 0;
        /*Seller email address */
        business = "lotscompany@7g.se";
        //business = "emsell_1236843564_biz@7g.se";
        /*Various book titles to be passed to the paypal */
        itm1 = "Did You Know?";
        itm2 = "Good Return on Reflections";
        //itm3 = "Reflections of Arabia";
        ship = document.getElementById("shipcost").value;
        tax = document.getElementById("vatcost").value;
        //alert(ship);
        var str = ""
        var i = 1; // paypal only allows in the order 1,2 and 3 order only..without 1 it wont allow 2 
        if (qt1 > 0) {
            str = "&item_name_" + i + "=" + itm1 + "&amount_" + i + "=" + book1 + "&quantity_" + i + "=" + qt1;
            i=i+1;
        }
        if(qt2 > 0){
            str = str + "&item_name_" + i + "=" + itm2 + "&amount_" + i + "=" + book2 + "&quantity_" + i + "=" + qt2;
        //i=i+1;
        }
        //        if (qt3 > 0){
        //            str = str + "&item_name_"+i+"=" + itm3 + "&amount_"+i+"=" + book3 + "&quantity_"+i+"=" + qt3;
        //            i=i+1;
        //            }
        /*
        alert("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1" + "&business=" + business + str + "&shipping=" + ship);
        */
        document.aspnetForm.action = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1" + "&business=" + business + str + "&shipping_1=" + ship + "&currency_code=EUR&tax_cart=" + tax;
        //alert("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1" + "&business=" + business + str + "&shipping=" + ship);
        document.aspnetForm.submit();
    }
}