﻿function showLeaseTerms() {
    window.open('/lease-agreement.aspx', 'leaseagreement', 'location=0,status=1,scrollbars=1,width=580,height=500,top=100,left=200, toolbar=1');
}

function CheckLeaseTotal() {

    try {
    
        //if refreshing page and ship-to-home is checked, show $15.00 charge checkbox.
        if (document.getElementById("shipCharge").value == "15.00")
            document.getElementById("shipContainer").style.display = "block";

        //now see what items are checked and update total price accordingly.
        for (i = 0; i < document.instrument.elements.length; i++) {
            if (document.instrument.elements[i].type == "checkbox" && !document.instrument.elements[i].checked && document.instrument.elements[i].id != "termsAgree") {

                if (document.instrument.elements[i].id == "cbLossFee") {
                    totalPrice = (parseFloat(document.getElementById("totalPrice").innerHTML) + 100) - parseFloat(document.getElementById(document.instrument.elements[i].id).parentNode.nextSibling.innerHTML.replace('$', '')).toFixed(2);
                }
                else {
                    totalPrice = parseFloat(document.getElementById("totalPrice").innerHTML) - parseFloat(document.getElementById(document.instrument.elements[i].id).parentNode.nextSibling.innerHTML.replace('$', '')).toFixed(2);
                }

                document.getElementById("totalPrice").innerHTML = totalPrice.toFixed(2);

               
            }
        }
    }
    catch(Err) {}
}

function CheckTax() {
    var tempTax = 0;
    var theItem;
    var theName;
    var somethingChecked = 0;
    //find taxable items and calculate
    for (i = 0; i < document.instrument.elements.length; i++) {
        if (document.instrument.elements[i].type == "checkbox" && document.instrument.elements[i].id.indexOf("accessory") > -1 && document.instrument.elements[i].checked) {
            theItem = document.instrument.elements[i].id.substring(document.instrument.elements[i].id.indexOf("y") + 1, document.instrument.elements[i].id.length);
            somethingChecked = 1;
            tempTax += parseFloat(Math.ceil((document.getElementById("prod" + theItem + "price").value * 0.065) * 100) / 100);
            document.getElementById("salesTax").value = tempTax.toFixed(2);
            document.getElementById("salesTaxValue").innerHTML = tempTax.toFixed(2);

            //totalPrice = parseFloat(totalPrice - (salesTax - tempTax));
            
        }

    }
    
    //if no taxable items after everything has been unchecked, set tax to zero.
    if (somethingChecked == 0) {
        tempTax = 0.00;
        document.getElementById("salesTax").value = tempTax.toFixed(2);
        document.getElementById("salesTaxValue").innerHTML = tempTax.toFixed(2);
        totalPrice = parseFloat(totalPrice - salesTax);
    }
}

function AddSubtractCheckBox(boxID) {
    var boxValue = parseFloat(document.getElementById(boxID.id).parentNode.nextSibling.innerHTML.replace('$', ''));
    
    //alert(checked);
    //alert(boxValue);

    var salesTax = parseFloat(document.getElementById("salesTaxValue").innerHTML);
    var tempTax = parseFloat(boxValue * 0.065);
    //alert(salesTax);

    if (boxID.checked == true) {
        totalPrice = parseFloat((totalPrice + boxValue) + tempTax);
        document.getElementById("salesTaxValue").innerHTML = document.getElementById("salesTax").value = (salesTax + tempTax).toFixed(2);
        //if user had de-selected coverage fee and has changed their mind, remove deposit and reinstate coverage fee.
        if (boxID.id == "cbLossFee" && document.getElementById("lossFeeDeposit").value == "100") {
            // totalPrice = parseFloat((totalPrice + boxValue) - 100) - boxValue;
            totalPrice = parseFloat(totalPrice - 100);
            document.getElementById("lossFeeDeposit").value = "0";
        }
    }
    else {
        totalPrice = parseFloat((totalPrice - boxValue) - tempTax);
        document.getElementById("salesTaxValue").innerHTML = document.getElementById("salesTax").value = (salesTax - tempTax).toFixed(2);
        //if user de-selects coverage fee, then add $100 for deposit.
        if (boxID.id == "cbLossFee" && (document.getElementById("lossFeeDeposit").value == "0" || document.getElementById("lossFeeDeposit").value == ""))
        {
            $('#dialog').jqmShow();
            totalPrice = parseFloat(totalPrice + 100);
            document.getElementById("lossFeeDeposit").value = "100";
        }

    }

    //round out the decimal
    totalPrice = Math.round(totalPrice * 100) / 100;


    lossFeeDeposit = parseFloat(document.getElementById("lossFeeDeposit").value == "" ? "0" : document.getElementById("lossFeeDeposit").value);
    enrollmentFee = parseFloat(document.getElementById("enrollFee").value);
    //salesTax = (((totalPrice - enrollmentFee) - lossFeeDeposit)) * .065;
    
    //totalPrice = totalPrice + salesTax;
    
    document.getElementById("totalPrice").innerHTML = totalPrice.toFixed(2);

}



function fillShipping(boxID) {
    if (boxID.checked) {
        document.getElementById("shippingContainer").style.display = "none";
        /*
        document.instrument.shipFirstName.value = document.instrument.billFirstName.value;
        document.instrument.shipLastName.value = document.instrument.billLastName.value;
        document.instrument.shipAddress1.value = document.instrument.billAddress1.value;
        document.instrument.shipAddress2.value = document.instrument.billAddress2.value;
        document.instrument.shipCity.value = document.instrument.billCity.value;
        document.instrument.shipState.value = document.instrument.billState.value;
        document.instrument.shipZip.value = document.instrument.billZip.value;
        document.instrument.shipPhone.value = document.instrument.billPhone.value;
        */
    }
    else {
        document.getElementById("shippingContainer").style.display = "block";
        /*
        document.instrument.shipFirstName.value = "";
        document.instrument.shipLastName.value = "";
        document.instrument.shipAddress1.value = "";
        document.instrument.shipAddress2.value = "";
        document.instrument.shipCity.value = "";
        document.instrument.shipState.value = "";
        document.instrument.shipZip.value = "";
        document.instrument.shipPhone.value = "";
        */
    }
}

function checkDelivery(boxID) 
{
    if (boxID.id == "deliverySchool") {
        document.getElementById("shippingParentContainer").style.display = "none";
        if (document.getElementById("shipCharge").value == "15.00") 
        {
            document.getElementById("totalPrice").innerHTML = (totalPrice - 15).toFixed(2);
            totalPrice = parseFloat(totalPrice - 15);
            document.getElementById("shipCharge").value = "0.00";
            document.getElementById("shipContainer").style.display = "none";
            
        }
    }
    else if (boxID.id == "deliveryShip") {
        document.getElementById("shippingParentContainer").style.display = "block";
        if (document.getElementById("shipCharge").value == "0.00") 
        {
            document.getElementById("totalPrice").innerHTML = (totalPrice + 15).toFixed(2);
            totalPrice = parseFloat(totalPrice + 15);
            document.getElementById("shipCharge").value = "15.00";
            document.getElementById("shipContainer").style.display = "block";
        }
    }
}

/**
* Automatically tab to next field.
*/
function autoTab(input, len, e) {
    var keyCode = e.keyCode;
    var filter = [0, 8, 9, 16, 17, 18, 35, 36, 37, 38, 39, 40, 46];
    if (input.value.length >= len && !containsElement(filter, keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input) + 1) % input.form.length].focus();
    }

    function containsElement(arr, ele) {
        var found = false, index = 0;
        while (!found && index < arr.length)
            if (arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }

    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input) index = i;
        else i++;
        return index;
    }
    return true;
}

/*
var nTotalLines = 0 ;
funct
ion Init()
{
  if (parent != window)
      parent.setIFrameHeight();
}
function NewRentalForm_Validator(theForm)
{
  submitonce(theForm);
  var message = "Please do not leave the following fields Blank: ";
  var numDeliveryLinesChecked = 0;
  var len = 0;
  var name = "";
  var fieldempty = "false";
  for (i=0;i<theForm.elements.length;i++)
  {
    name = theForm.elements[i].name;
    len = name.length;
    if (theForm.elements[i].type == "checkbox" && !theForm.elements[i].checked)
    {
       fieldempty = "true";
    }
    else if (theForm.elements[i].value.length == 0)
    {
       fieldempty = "true";
    }
    else 
    {
       fieldempty = "false";
    }
    if (name.substring(0,4).toUpperCase() == "C2B_" && name.substring(len-4).toUpperCase() == "_REQ" && fieldempty == "true")
    {
       message = message + "\n   " + name.substring(4,len-4);
    }
    if (name.substring(0,11).toUpperCase() == "TXTDELIVERY" && eval('document.NewRentalForm.chk'+theForm.elements[i].value+'.checked') == true) 
    {
       numDeliveryLinesChecked++;
    }
  }
  if (nTotalDeliveryLines > 0 && numDeliveryLinesChecked != 1)
  {
    alert('Please Choose One Delivery Option.');
    submitenabled(theForm);
    return (false);
  }
  if (message.length > 48)
  {
    alert(message);
    submitenabled(theForm);
    return (false);
  }
  return (true);
}
function SetnTotalLines(nTotLines, nTotDeliveryLines)
{
  nTotalLines = nTotLines;
  nTotalDeliveryLines = nTotDeliveryLines;
}
function Redirect(cRef)
{
    alert(cref);
  document.location.href = eval(cRef)
}
function AddSubtractAmt()
{
	var nTotal = parseFloat(document.NewRentalForm.txtDownPay.value) ;
	var cAccessoryString = '' ;
	var i = 0 ;
	for (i=1;i<=nTotalLines;i++)
	{
		if(eval('document.NewRentalForm.chk'+String(i)+'.checked') == true)
		{
			nTotal += parseFloat(eval('document.NewRentalForm.chk'+String(i)+'.value'));
			cAccessoryString += eval('document.NewRentalForm.txtRacPK'+String(i)+'.value')+'|';
		}
	}
  var ts = new String(nTotal.toFixed(2));
	if(ts.indexOf('.') < 0) { ts += '.00';}
	if(ts.indexOf('.') == (ts.length - 2)) { ts += '0';}
	document.NewRentalForm.txtC2BAmtToPay.value = ts;
	document.NewRentalForm.txtAccessoryString.value = cAccessoryString;
}
*/
