//
// Shopping cart javascript functions
//

	function copyShToBi(flag){
        if(flag){
            document.editAddressesForm.blFirstName.value = document.editAddressesForm.firstName.value;
            document.editAddressesForm.blFirstName.setAttribute('readonly','readonly');
            document.editAddressesForm.blLastName.value = document.editAddressesForm.lastName.value;
            document.editAddressesForm.blLastName.setAttribute('readonly','readonly');
            document.editAddressesForm.blAddress1.value = document.editAddressesForm.street1.value;
            document.editAddressesForm.blAddress1.setAttribute('readonly','readonly');
            document.editAddressesForm.blAddress2.value = document.editAddressesForm.street2.value;
            document.editAddressesForm.blAddress2.setAttribute('readonly','readonly');
            document.editAddressesForm.blCompany.value = document.editAddressesForm.company.value;
            document.editAddressesForm.blCity.value = document.editAddressesForm.city.value;
            document.editAddressesForm.blCity.setAttribute('readonly','readonly');
            document.editAddressesForm.blCountry.selectedIndex = document.editAddressesForm.country.selectedIndex;
            document.editAddressesForm.blCountry.setAttribute('readonly','readonly');
            if(document.getElementById('sCntry').value=='us'){
            	noBlState(document.getElementById('blCountry'));
            	document.editAddressesForm.blState.selectedIndex = document.editAddressesForm.stateProvince.selectedIndex;
            }else{
            	noBlState(document.getElementById('blCountry'));
            	document.editAddressesForm.blState.value = document.editAddressesForm.stateProvince.value;
            }
            document.editAddressesForm.blState.setAttribute('readonly','readonly');
            document.editAddressesForm.blZip.value = document.editAddressesForm.zipPostalCode.value;
            document.editAddressesForm.blZip.setAttribute('readonly','readonly');
            document.editAddressesForm.blPhoneCode.value = document.editAddressesForm.phoneNumberCode.value;
            document.editAddressesForm.blPhoneCode.setAttribute('readonly','readonly');
            document.editAddressesForm.blPhonePart1.value = document.editAddressesForm.phoneNumberPart1.value;
            document.editAddressesForm.blPhonePart1.setAttribute('readonly','readonly');
            document.editAddressesForm.blPhonePart2.value = document.editAddressesForm.phoneNumberPart2.value;
            document.editAddressesForm.blPhonePart2.setAttribute('readonly','readonly');
            document.editAddressesForm.blEmail.value = document.editAddressesForm.email.value;
            document.editAddressesForm.blEmail.setAttribute('readonly','readonly');
		}
		else{
			 document.editAddressesForm.blFirstName.removeAttribute('readonly');
            document.editAddressesForm.blLastName.removeAttribute('readonly');
            document.editAddressesForm.blAddress1.removeAttribute('readonly');
            document.editAddressesForm.blAddress2.removeAttribute('readonly');
            document.editAddressesForm.blCity.removeAttribute('readonly');
            document.editAddressesForm.blState.removeAttribute('readonly');
            document.editAddressesForm.blCountry.removeAttribute('readonly');
            document.editAddressesForm.blZip.removeAttribute('readonly');
            document.editAddressesForm.blPhoneCode.removeAttribute('readonly');
            document.editAddressesForm.blPhonePart1.removeAttribute('readonly');
            document.editAddressesForm.blPhonePart2.removeAttribute('readonly');
             document.editAddressesForm.blEmail.removeAttribute('readonly');
		}
	}
    function submitCheckoutAddresses(isTempAccountForm) {
        
        if(document.editAddressesForm.shippingAddress.checked == true){
            copyShToBi(true);
        }
        if (isTempAccountForm && document.getElementById('createAccount').checked) {
            document.editAddressesForm.action = "checkout-create-account.do";
        }
        document.editAddressesForm.submit();
    }

    function showPasswords(isShow) {
        if (isShow) {
            document.getElementById('createAccountBlock').style.display = 'block';
        } else {
            document.getElementById('createAccountBlock').style.display = 'none';
        }
    }

function updateSubtotalSC() {
    var shoppingForm =  ns6? document.getElementById("shoppingCartForm") : document.shoppingCartForm;
    shoppingForm.submit();
}

function fedExChangeSC(index) {
    if(subAmount < 500) {
        var allTotal =  document.getElementById("grandTotal");
        var fedExAddElem =  document.getElementById("fedExAdd");

        fedExAddElem.innerHTML = "$0";
        totalAmount = subAmount + totalTaxes;
        totalAmount = totalAmount.toFixed(0);
        if (index == 0) {
            fedExAddElem.innerHTML = "$0";
        }
        else {
            totalAmount = subAmount + totalTaxes + fedExCost;
            fedExAddElem.innerHTML = "$15";
        }

        allTotal.innerHTML = "$" + totalAmount;
        calc(index);
    }
}

function removeItem (form1) {
    var b = document.getElementsByTagName("form");
    var k = b.length;

    var removingForm;
    leaving = false;
    for(i = 0; i < k; i++) {
        if (b.item(i).getAttribute("name") == form1) {
            removingForm = b.item(i);
        }
    }

    removingForm.action = "delete-cart-item.do";
    removingForm.onSubmit = "window.alert('onSubmit');";

    removingForm.submit();
}

function submitProductForm(formName, pvUID) {
    leaving = false;
    var form2Submit = document.getElementById(formName);
    form2Submit.action = "update-ring-size.do";
    form2Submit.onSubmit = "";
    form2Submit.settingPVUID.value = pvUID;
    form2Submit.submit();
}

function enlarge(isEngRing, settingImage, diamondImage, diamondShape) {
    var imageDiv = document.getElementById('imageDiv');
    if (diamondShape == undefined) {
        diamondShape = " common";
    } else {
        diamondShape = " " + diamondShape.toLowerCase();
    }
    for (var i = 0; i < imageDiv.childNodes.length; i++) {
        imageDiv.removeChild(imageDiv.childNodes[0]);
    }
    var imDiv;
    if (isEngRing) {
        imDiv = document.createElement('div');
        imDiv.className = 'out_large';
        var sDiv = document.createElement('div');
        sDiv.className = 'setting_large';
        var sImage = document.createElement('img');
        sImage.src = settingImage;
        sImage.width = 590;
        sImage.height = 590;
        var dDiv = document.createElement('div');
        dDiv.className = 'diamond_large' + diamondShape;
        var dImage = document.createElement('img');
        dImage.src = diamondImage;
        dImage.width = 590;
        dImage.height = 590;
        sDiv.appendChild(sImage);
        dDiv.appendChild(dImage);
        imDiv.appendChild(sDiv);
        imDiv.appendChild(dDiv);
    } else {
        imDiv = document.createElement('div');
        imDiv.onMouseover = 'dragswitch=1;if (ns4) drag_dropns(showimage)';
        imDiv.onMouseout = 'dragswitch=0';
        var regImage = document.createElement('img');
        regImage.src = settingImage;
        regImage.width = 590;
        regImage.height = 590;
        regImage.className = 'prod';
        regImage.align = 'middle';
        imDiv.appendChild(regImage);
    }
    imageDiv.appendChild(imDiv);
    document.getElementById('cont_2').style.display='block';
    select_win('2');
}

/* ----------------------------------------------------------------------------------------------------------- */

//
// Payment Options javascript functions
//
function updateSubtotalPO(){
    persist();
    var shoppingForm =  ns6? document.getElementById("shoppingCartForm") : document.shoppingCartForm;
    shoppingForm.submit();
}

function persist() {
    var shoppingForm =  ns6? document.getElementById("shoppingCartForm") : document.shoppingCartForm;
    var paymentForm =  ns6? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;

    if (paymentForm.couponCode != null) {
        shoppingForm.couponCode.value = paymentForm.couponCode.value;
    }
    shoppingForm.FedEx.value = paymentForm.FedEx.value;

    shoppingForm.cardNumber.value = paymentForm.cardNumber.value;
    shoppingForm.cardNumber1.value = paymentForm.cardNumber1.value;
    shoppingForm.cardNumber2.value = paymentForm.cardNumber2.value;
    shoppingForm.cardNumber3.value = paymentForm.cardNumber3.value;

    shoppingForm.expMonth.value = paymentForm.expMonth.value;
    shoppingForm.expYear.value = paymentForm.expYear.value;

    shoppingForm.expMonth1.value = paymentForm.expMonth1.value;
    shoppingForm.expYear1.value = paymentForm.expYear1.value;

    shoppingForm.expMonth2.value = paymentForm.expMonth2.value;
    shoppingForm.expYear2.value = paymentForm.expYear2.value;

    shoppingForm.expMonth3.value = paymentForm.expMonth3.value;
    shoppingForm.expYear3.value = paymentForm.expYear3.value;

    shoppingForm.cardAmount1.value = paymentForm.cardAmount1.value;
    shoppingForm.cardAmount2.value = paymentForm.cardAmount2.value;
    shoppingForm.cardAmount3.value = paymentForm.cardAmount3.value;
}

function initialize() {
    var shoppingForm =  ns6? document.getElementById("shoppingCartForm") : document.shoppingCartForm;
    var paymentForm =  ns6? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;

    if (shoppingForm == null || paymentForm == null) {
        return;
    }

//    if (paymentForm.couponCode != null && shoppingForm.couponCode != null) {
//        paymentForm.couponCode.value = shoppingForm.couponCode.value;
//    }
//    if (paymentForm.FedEx != null && shoppingForm.FedEx != null) {
//        paymentForm.FedEx.value = shoppingForm.FedEx.value;
//    }
    if (paymentForm.cardNumber != null && shoppingForm.cardNumber != null) {
        paymentForm.cardNumber.value = shoppingForm.cardNumber.value;
    }
    if (paymentForm.cardNumber1 != null && shoppingForm.cardNumber1 != null) {
        paymentForm.cardNumber1.value = shoppingForm.cardNumber1.value;
    }
    if (paymentForm.cardNumber2 != null && shoppingForm.cardNumber2 != null) {
        paymentForm.cardNumber2.value = shoppingForm.cardNumber2.value;
    }
    if (paymentForm.cardNumber3 != null && shoppingForm.cardNumber3 != null) {
        paymentForm.cardNumber3.value = shoppingForm.cardNumber3.value;
    }

    if (paymentForm.expMonth != null && shoppingForm.expMonth != null) {
        paymentForm.expMonth.value = shoppingForm.expMonth.value;
    }
    if (paymentForm.expYear != null && shoppingForm.expYear != null) {
        paymentForm.expYear.value = shoppingForm.expYear.value;
    }

    if (paymentForm.expMonth1 != null && shoppingForm.expMonth1 != null) {
        paymentForm.expMonth1.value = shoppingForm.expMonth1.value;
    }
    if (paymentForm.expYear1 != null && shoppingForm.expYear1) {
        paymentForm.expYear1.value = shoppingForm.expYear1.value;
    }

    if (paymentForm.expMonth2 != null && shoppingForm.expMonth2 != null) {
        paymentForm.expMonth2.value = shoppingForm.expMonth2.value;
    }
    if (paymentForm.expYear2 != null && shoppingForm.expYear2) {
        paymentForm.expYear2.value = shoppingForm.expYear2.value;
    }

    if (paymentForm.expMonth3 != null && shoppingForm.expMonth3 != null) {
        paymentForm.expMonth3.value = shoppingForm.expMonth3.value;
    }
    if (paymentForm.expYear3 != null && shoppingForm.expYear3) {
        paymentForm.expYear3.value = shoppingForm.expYear3.value;
    }

    if (paymentForm.cardAmount1 != null && shoppingForm.cardAmount1 != null) {
        paymentForm.cardAmount1.value = shoppingForm.cardAmount1.value;
    }
    if (paymentForm.cardAmount2 != null && shoppingForm.cardAmount2 != null) {
        paymentForm.cardAmount2.value = shoppingForm.cardAmount2.value;
    }
    if (paymentForm.cardAmount3 != null && shoppingForm.cardAmount3 != null) {
        paymentForm.cardAmount3.value = shoppingForm.cardAmount3.value;
    }
}

function fedExChangePO(index) {
    if (subAmount < 500)
    {
        var allTotal =  document.getElementById("grandTotal");
        var fedExAddElem =  document.getElementById("fedExAdd");
        var cardTopElem =  document.getElementById("cardTop");

        fedExAddElem.innerHTML = "$0";
        totalAmount = subAmount + totalTaxes;
        totalAmount = totalAmount.toFixed(0);
        if (index == 0) {
            fedExAddElem.innerHTML = "$0";
        }
        else{
            totalAmount = subAmount + totalTaxes + fedExCost;
            fedExAddElem.innerHTML = "$15";
        }

        allTotal.innerHTML = "$" + totalAmount;
        cardTopElem.innerHTML = "$" + totalAmount;
        calc(index);
        recalc1();
    }
}

function btnOnclick() {
    var numOfClicksElem = ns6 ? document.getElementById("numOfClicks") : document.all.numOfClicks;
    var optionsForm =  ns6 ? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;
    var count = numOfClicksElem.value;
    numOfClicksElem.value = parseInt(count) + 1;
    if (numOfClicksElem.value == 1) {
        optionsForm.submit();
    }
    document.getElementById('greyed').style.display = 'block';
    document.getElementById('overlayText').style.display = 'block';
}

function showMultipleCards() {
    var optionsForm = ns6 ? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;

    if (((optionsForm.cardNumber.value != null) && (optionsForm.cardNumber.value != "")) &&
        ((optionsForm.cardNumber1.value == null) || (optionsForm.cardNumber1.value == ""))) {
        optionsForm.cardNumber1.value = optionsForm.cardNumber.value;
        optionsForm.cardCvv21.value = optionsForm.cardCvv2.value;
        optionsForm.expMonth1.value = optionsForm.expMonth.value;
        optionsForm.expYear1.value = optionsForm.expYear.value;
    }
    if ((optionsForm.cardNumber1.value == null) || (optionsForm.cardNumber1.value == ""))
        optionsForm.cardAmount1.value = totalAmount;
    optionsForm.useMultipleCards.value = '1';

    recalc1();

    document.getElementById('cont_1').style.display = 'block';
}

function hideMultipleCards(useMult) {
    var optionsForm = ns6 ? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;
//    am1 = ns6 ? optionsForm.cardAmount1 : document.all.cardAmount1;
//    am2 = ns6 ? optionsForm.cardAmount2 : document.all.cardAmount2;
//    am3 = ns6 ? optionsForm.cardAmount3 : document.all.cardAmount3;
    am1 = optionsForm.cardAmount1;
    am2 = optionsForm.cardAmount2;
    am3 = optionsForm.cardAmount3;

    var summ = 0;
    if (!isNaN(am1.value))
        summ = Number(am1.value);
    if (!isNaN(am2.value))
        summ += Number(am2.value);
    if (!isNaN(am3.value))
        summ += Number(am3.value);
    if (useMult == '1') {
        if (totalAmount == summ) {
            optionsForm.useMultipleCards.value = useMult;
            btnOnclick();
        } else {
            alert("Summary amount $" + summ + " is not equal to order total $" + totalAmount);
        }
    } else {
        optionsForm.useMultipleCards.value = useMult;
        document.getElementById('cont_1').style.display = 'none';
    }
}

function recalc1() {
   var optionsForm = ns6 ? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;
//   am1 = ns6 ? optionsForm.cardAmount1 : document.all.cardAmount1;
//   am2 = ns6 ? optionsForm.cardAmount2 : document.all.cardAmount2;
   am1 = optionsForm.cardAmount1;
   am2 = optionsForm.cardAmount2;

//   rem2 = ns6 ? document.getElementById("remain2") : document.all.remain2;
//   rem3 = ns6 ? document.getElementById("remain3") : document.all.remain3;
   rem2 = document.getElementById("remain2");
   rem3 = document.getElementById("remain3");
   rem2.innerHTML = "Amount remaining: $" + (totalAmount - am1.value);
   rem3.innerHTML = "Amount remaining: $" + (totalAmount - am1.value - am2.value);
}

function recalc2() {
   var optionsForm = ns6 ? document.getElementById("paymentOptionsForm") : document.paymentOptionsForm;
//   am1 = ns6 ? optionsForm.cardAmount1 : document.all.cardAmount1;
//   am2 = ns6 ? optionsForm.cardAmount2 : document.all.cardAmount2;
   am1 = optionsForm.cardAmount1;
   am2 = optionsForm.cardAmount2;

//   rem3 = ns6 ? document.getElementById("remain3") : document.all.remain3;
   rem3 = document.getElementById("remain3");
   rem3.innerHTML = "Amount remaining: $" + (totalAmount - am1.value - am2.value);
}

function showCSCNote() {
    document.getElementById('cont_2').style.display = 'block';
}

function hideCSCNote() {
    document.getElementById('cont_2').style.display = 'none';
}

//
// Common javascript functions
//
function calc(sel) {
   totalAmount = subAmount + totalTaxes;
   if(sel != 0) {
      totalAmount += fedExCost;
   }
}

//
// Misc functions
//
function hideAwayPopup() {
    document.getElementById('cont_4').style.display = 'none';
}

function checkLinks() {
    var links = document.getElementsByTagName("a");
    for(var i = 0; i < links.length; i++) {
        var link = links[i];
        if (link.onclick == null) {
            link.onclick = function () {
                checkUrl(this.href);
            }
        }
    }
}

function checkUrl(href) {
    var hostName = window.location.hostname;
    leaving = (href.indexOf(hostName) < 0);
}

function showAbandonPopup() {
    if (leaving) {
      //  window.open('abandon.html','popup','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,width=415,height=515');
    }
}

function fixLabels(id){
   document.getElementById(id).checked = document.getElementById(id).checked==true?false:true;
}
