// JavaScript Document

var amount1, amount2, amount3, amount4, amount5, amount6 = 0.00;

// current prices - update as necessary
var price = 3.99;
var price2 = 11.95;
var taxes = 0.00;
var currentShipping = 0.00;
var isShipping = true;
var totalQuantity = 0;

//Money format function
function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function roundNumber(num) {
	var result = Math.round(num*Math.pow(10,2))/Math.pow(10,2);
	return result;
}


//set total quantity entered
function setTotalQuantity() {
	totalQuantity = 
		parseInt(document.form1.qty101s.value) + (4 * parseInt(document.form1.qty101p.value)) + 
		parseInt(document.form1.qty102s.value) + (4 * parseInt(document.form1.qty102p.value)) +
		parseInt(document.form1.qty103s.value) + (4 * parseInt(document.form1.qty103p.value)) +
		parseInt(document.form1.qty104s.value) + (4 * parseInt(document.form1.qty104p.value)) +
		parseInt(document.form1.qty105s.value) + (4 * parseInt(document.form1.qty105p.value)) +
		parseInt(document.form1.qty106s.value) + (4 * parseInt(document.form1.qty106p.value)) +
		parseInt(document.form1.qty107s.value) + (4 * parseInt(document.form1.qty107p.value)) +
		parseInt(document.form1.qty108s.value) + (4 * parseInt(document.form1.qty108p.value)) +
		parseInt(document.form1.qty109s.value) + (4 * parseInt(document.form1.qty109p.value)) +
		parseInt(document.form1.qty110s.value) + (4 * parseInt(document.form1.qty110p.value)) +
		parseInt(document.form1.qty111s.value) + (4 * parseInt(document.form1.qty111p.value)) +
		parseInt(document.form1.qty112s.value) + (4 * parseInt(document.form1.qty112p.value)) +
		parseInt(document.form1.qty113s.value) + (4 * parseInt(document.form1.qty113p.value)) +
		parseInt(document.form1.qty114s.value) + (4 * parseInt(document.form1.qty114p.value)) +
		parseInt(document.form1.qty115s.value) + (4 * parseInt(document.form1.qty115p.value));
}

//set shipping price based on province and quantity
function setShipping() {
	setTotalQuantity();
	
	//Quantity of items is under 25
	if (totalQuantity < 25) {
	
		document.getElementById("submit").disabled = false;
		
		if (document.form1.shipping.options[document.form1.shipping.selectedIndex].value == "Regular Mail") {	
			//If State selected isn't blank, Hawaii or Alaska our is outside Canada and USA
			if (document.form1.state.options[document.form1.state.selectedIndex].value == "" || document.form1.state.options[document.form1.state.selectedIndex].value == "AK" || document.form1.state.options[document.form1.state.selectedIndex].value == "HI") {
				if (totalQuantity <= 0) {currentShipping = 0.00;}
				else if (totalQuantity >= 1 && totalQuantity <= 12) {currentShipping = 7.99;}
				else if (totalQuantity >= 13 && totalQuantity <= 24) {currentShipping = 14.99;}
			}
			
			//If inside Continental USA or Canada
			else {
				if (totalQuantity <= 0) {currentShipping = 0.00;}
				else if (totalQuantity >= 1 && totalQuantity <= 12) {currentShipping = 3.99;}
				else if (totalQuantity >= 13 && totalQuantity <= 24) {currentShipping = 7.50;}
			}
		}
		
		else if  (document.form1.shipping.options[document.form1.shipping.selectedIndex].value == "Expedited / Priority Mail") {			
			currentShipping = 24.00;			
			
			//If State selected isn't blank, Hawaii or Alaska our is outside Canada and USA
			if (document.form1.state.options[document.form1.state.selectedIndex].value == "" || document.form1.state.options[document.form1.state.selectedIndex].value == "AK" || document.form1.state.options[document.form1.state.selectedIndex].value == "HI") {
				if (totalQuantity <= 0) {currentShipping = currentShipping + 0.00;}
				else if (totalQuantity >= 1 && totalQuantity <= 12) {currentShipping = currentShipping + 7.99;}
				else if (totalQuantity >= 13 && totalQuantity <= 24) {currentShipping = currentShipping + 14.99;}	
			}
			
			//If inside Continental USA or Canada
			else {
				if (totalQuantity <= 0) {currentShipping = currentShipping + 0.00;}
				else if (totalQuantity >= 1 && totalQuantity <= 12) {currentShipping = currentShipping + 3.99;}
				else if (totalQuantity >= 13 && totalQuantity <= 24) {currentShipping = currentShipping + 7.50;}	
			}					
		}
		
		else currentShipping = 0.00;
		
		//IF Method of payment = COD
		if (document.form1.payment.options[document.form1.payment.selectedIndex].value == "Cash on Delivery (COD) - Only Available in Canada") {	
			currentShipping = 0.00;
		}
	}
	
	//Quantity of items is greater than 25
	else {
		currentShipping = 0.00;
		alert("Please contact us to make an order of 25+ sheets\n1 .877.972.7636\n416.972.7636\ninfo@ibrowsers.ca");
		document.getElementById("submit").disabled = true;
	}
}

//set tax based on Province
function setTax() {
		
	if (document.form1.state.options[document.form1.state.selectedIndex].value == "AB")
		taxes = 0.05;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "BC")
		taxes = 0.12;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "SK")
		taxes = 0.10;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "MB")
		taxes = 0.11;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "ON")
		taxes = 0.13;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "QC")
		taxes = 0.15;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "PE")
		taxes = 0.15;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "NS")
		taxes = 0.15;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "NL")
		taxes = 0.13;
	else if (document.form1.state.options[document.form1.state.selectedIndex].value == "NB")
		taxes = 0.13;	
	else
		taxes = 0.00;
}

function changeProvince() {
	setTax();
	setShipping()
	grandTotal();
}


// calculate all single ticket fields
function calcTotal(choice){
	
	var amountS = "qty" + choice + "s";
	var amountP = "qty" + choice + "p";
	var currentTotal = "total" + choice;
		
		if (document.getElementById(amountS).value != 0 || document.getElementById(amountS).value != "" || document.getElementById(amountP).value != 0 || document.getElementById(amountP).value != "")
		{	document.getElementById(currentTotal).value = roundNumber((formatAsMoney(document.getElementById(amountS).value)* price) + (formatAsMoney(document.getElementById(amountP).value) * price2));	
			setShipping(); grandTotal();
		}
		else {
			
			document.getElementById(currentTotal).value = 0.00;
			setShipping(); grandTotal();
		}	
	
}

//calcualte total amount of all fields thoughout form
function grandTotal(){
	document.form1.subtotal.value = formatAsMoney(parseFloat(document.form1.total101.value) + parseFloat(document.form1.total102.value) + parseFloat(document.form1.total103.value) + parseFloat(document.form1.total104.value) + parseFloat(document.form1.total105.value) + parseFloat(document.form1.total106.value) + parseFloat(document.form1.total107.value) + parseFloat(document.form1.total108.value) + parseFloat(document.form1.total109.value) + parseFloat(document.form1.total110.value) + parseFloat(document.form1.total111.value) + parseFloat(document.form1.total112.value) + parseFloat(document.form1.total113.value) + parseFloat(document.form1.total114.value) + parseFloat(document.form1.total115.value));
	document.form1.tax.value = formatAsMoney(taxes * document.form1.subtotal.value);
	document.form1.shipping2.value = formatAsMoney(currentShipping);
	document.form1.grandtotal.value = formatAsMoney(parseFloat(document.form1.subtotal.value) + parseFloat(document.form1.tax.value) + parseFloat(document.form1.shipping2.value));
}


// Required fields validation
/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("name", "email", "phone", "address", "city", "zip", "country", "payment", "shipping", "grandtotal", "recaptcha_response_field");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Name", "Email", "Phone", "Address", "City", "Zip/Postal Code", "Country", "Method of Payment", "Shipping", "Select at least 1 product","Word Verification");
	// dialog message
	var alertMsg = "Please complete the following fields:\n\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "Select one..." || obj.options[obj.selectedIndex].text == "Select State/Province (Canada and USA only)"){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null || obj.value == "0.00" || obj.value == "10.00" || obj.value == "24.00" || obj.value == "20.00" || obj.value == "30.00" || obj.value == "45.00"){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (document.getElementById("country").value == "USA" || document.getElementById("country").value == "Canada") {
		if (document.getElementById("state").value == "") {
			alertMsg += " - State/Province \n";
		}
	}
	
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
