﻿

//COUPON FORM
//===========
function validateCoupon() {
    return isNameValid('ctl00_ContentPlaceHolder1_ctl00_uiCart_txtCouponCode',3);
}

//COUPON INPUT ENTER EVENT
//==========================
function GetKeyPressForCouponTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiCart_btnSendCoupon").click();
    }
}

//FORGOT PASSWORD FORM
//====================
function validateForgotPasswordForm() {
    var isFormValid = true;
    //Step # 1 : Restart the output message label.
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_lblOutput').innerHTML = "* Required fields.";
    //Step # 2 : Determine the Fields by reverse order.
    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtEmail"))) {
        isFormValid = false;
    }

    //Step # 3 : Determine display of notification and errors.
    if (isFormValid) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_ctl00_btnLogin')
        .hide();
        $('#ajaxLoginLoaderForgot')
        .show();
        sleep(2000);*/
        return true;
    }
    else {

        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_lblOutput').innerHTML =
         "* Required fields.";
        return false;
    }
}

//FORGOT INPUT ENTER EVENT
//==========================
function GetKeyPressForForgotTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_ctl00_btnLogin").click();
    }
}


//LOGIN IN ORDER FORM
//===================
//validates the login form
function validateLoginFormInOrderForm() {
    var isFormValid = true;
    //Step # 1 : Restart the output message label.
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_lblOutput').innerHTML = "* Required fields.";
    //Step # 2 : Determine the Fields by reverse order.
    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_txtLoginEmail"))) {
        isFormValid = false;
    }

    //Validate Password
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_txtLoginPassword", 1))) {
        isFormValid = false;
    }

    //Step # 3 : Determine display of notification and errors.
    if (isFormValid) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_btnLogin')
        .hide();
        $('#ajaxLoginLoader')
        .show();
        sleep(500);*/
        return true;
    }
    else {

        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_lblOutput').innerHTML =
         "* Required fields.";
        return false;
    }
}

//LOGIN IN ORDER FORM INPUT ENTER EVENT
//=====================================
function GetKeyPressForLoginInOrderFormTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_uiLogin_btnLogin").click();
    }
}

//LOGIN
//=====
//validates the login form
function validateLoginForm() {
    var isFormValid = true;
    //Step # 1 : Clean the output message label.
    document.getElementById('ofvs').innerHTML = "* Required fields.";
    //Step # 2 : Determine the Fields by reverse order.
    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_uiLogin_txtLoginEmail"))) {
        isFormValid = false;
    }

    //Validate Password
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiLogin_txtLoginPassword", 1))) {
        isFormValid = false;
    }

    //Step # 3 : Determine display of notification and errors.
    if (isFormValid) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_uiLogin_btnLogin')
        .hide();
        $('#ajaxLoginLoader')
        .show();
        sleep(500);*/
        return true;
    }
    else {
        
        document.getElementById('ofvs').innerHTML =
         "* Required fields.";
        return false;
    }
}

//LOGIN INPUT ENTER EVENT
//==========================
function GetKeyPressForLoginTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiLogin_btnLogin").click();
    }
}


//QUESTION FORM FUNCTIONS
//======================
//validates the question form
function validateQuestionForm() {
    var isFormValid = true;
    //Step # 1 : Restart the output message label.
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_vsContactForm').innerHTML = "* Required fields.";

    //Step # 2 : Determine the Fields by reverse order.
    //Validate Message
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_txtMsg", 1))) {
        isFormValid = false;
    }


    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_txtEmail"))) {
        isFormValid = false;
    }

    //Validate Phone
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_txtPhone", 1))) {
        isFormValid = false;
    }


    //Validate Full Name
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_txtName", 1))) {
        isFormValid = false;
    }

    //Step # 3 : Determine display of notification and errors.
    if (isFormValid) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_btnSend')
        .hide();
        $('#QuestionLoader')
        .show();
        sleep(500);*/
        return true;
    }
    else {
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_vsContactForm').innerHTML =
         "* Required fields.";
        return false;
    }
}

//QUESTION INPUT ENTER EVENT
//==========================
function GetKeyPressForQuestionTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiQuestionForm_btnSend").click();
    }
}

//CONTACT FORM FUNCTIONS
//======================
//validates the contact form
function validateContactForm() {
    var isFormValid = true ;
    //Step # 1 : Restart the output message label.
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiContactForm_vsContactForm').innerHTML = "* Required fields.";

    //Step # 2 : Determine the Fields by reverse order.
    //Validate Subject
    if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiContactForm_DdlContactSubject"))) {
        isFormValid = false;
    }

    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_uiContactForm_txtEmail"))) {
        isFormValid = false;
    }

    //Validate Phone
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiContactForm_txtPhone", 1))) {
        isFormValid = false;
    }

    //Validate Last Name
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiContactForm_txtName", 1))) {
        isFormValid = false;
    }

    //Step # 3 : Determine display of notification and errors.
    if (isFormValid) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_uiContactForm_btnSend')
        .hide();
        $('#ContactLoader')
        .show();
        sleep(500);*/
        $('#ctl00_ContentPlaceHolder1_ctl00_uiContactForm_vsContactForm').addClass('notDisplayed');
        return true;
    }
    else {
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiContactForm_vsContactForm').innerHTML =
         "* Required fields.";
        return false;
    }
}

//CONTACT INPUT ENTER EVENT
//==========================
function GetKeyPressForContactTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiContactForm_btnSend").click();
    }
}

//REGISTER FUNCTIONS
//==================
//validate the register form
function validateRegisterForm() {
    
    var isFormValid = true;
    //Step # 1 : Restart the output message label.
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_lblVsRegister').innerHTML = "";

    //Step # 2 : Determine the Fields of the billing information by reverse order.

    //Validate Country
    /*if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlCountry"))) {
        isFormValid = false;
    }
    else {
        if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlCountry').value == 265) {
            if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlState"))) 
                isFormValid = false;
        }
        else if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlCountry').value == 303) {
            if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlProvince"))) 
                isFormValid = false;
        }
    }*/

    //Validate Verify Password
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPasswordVerify", 1))) {
        isFormValid = false;
    }

    //Validate Password
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPassword", 1))) {
        isFormValid = false;
    }

     //Validate Verify = password
    var jqueryId = "#ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPassword";
    var jqueryId2 = "#ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPasswordVerify";
    if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPassword').value != 
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPasswordVerify').value)
    {
        isFormValid = false;
        $(jqueryId).addClass("fieldError");
        $(jqueryId2).addClass("fieldError");
    }
    else
    {
        if ((document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPassword').value != "") &&
        (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPasswordVerify').value != "")) {
            $(jqueryId).removeClass("fieldError");
            $(jqueryId2).removeClass("fieldError");
        }
    }


    //Validate Email
    if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtEmail"))) {
        isFormValid = false;
    }

    //Validate Phone
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtPhone", 1))) {
        isFormValid = false;
    }

    //Validate Last Name
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtLastName", 1))) {
        isFormValid = false;
    }
    //Validate First Name
    if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_ctl00_txtFirstName", 1))) {
        isFormValid = false;
    }

    //Step # 3 : If the Method type is credit then we need to validate

    //Step # 4 : Determine display of notification and errors.
    if (isFormValid) {

        /*$('#ctl00_ContentPlaceHolder1_ctl00_ctl00_btnRegister')
        .hide();
        $('#ajaxLoginLoaderRegister')
        .show();
        sleep(500);*/
        return true;
    }
    else {
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_lblVsRegister').innerHTML +=
         "* Required fields.";
        return false;
    }
}

//REGISTER INPUT ENTER EVENT
//==========================
function GetKeyPressForRegisterTextBox(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_ctl00_btnRegister").click();
    }
}

//update state display
function updateStateDisplay() {
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_pnlStateBox').style.display = 'none';
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_pnlProvinceBox').style.display = 'none';
    if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlCountry').value == 265)
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_pnlStateBox').style.display = 'block';
    if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_DdlCountry').value == 303)
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl00_pnlProvinceBox').style.display = 'block';
}

//SEARCH BOX FUNCTIONS
//====================
function updateSubCategoryDropDownForSearch() {
    //Clean the select options in the current sub category Drop down
    $('#ctl00_ucSearchBox_DdlSubCategories option').remove();

    //Add the standard "By Sub Category" element
    $('#ctl00_ucSearchBox_DdlSubCategories').append('<option id="0">By Sub Category</option>');

    //Remove the disable
    $('#ctl00_ucSearchBox_DdlSubCategories').removeAttr('disabled');


    //Iterate on the mainCategoriesCollection Array
    var i;
    for (i = 0; i < mainCategoriesCollection.length; i++) {
        //If the fatherId suits the selected id,
        if (mainCategoriesCollection[i].FatherId == $('#ctl00_ucSearchBox_DdlCategories').val()) {
            //Add the element to the sub category list
            var syntax = "<option value=" + mainCategoriesCollection[i].Id + ">" + mainCategoriesCollection[i].Name + "</option>";
            $('#ctl00_ucSearchBox_DdlSubCategories').append(syntax);
        }
    }

    //Return the disable if there are no items
    if (parseInt($('#ctl00_ucSearchBox_DdlSubCategories option').size())<2)
        $('#ctl00_ucSearchBox_DdlSubCategories').attr('disabled', 'disabled');
}


//SEARCH BOX CMS ENTER EVENT
//==========================
function GetKeyPressForCmsSearchTextBox(e) {
    var Key;

    if (window.event)        // IE
    {
        Key = window.event.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        Key = e.which
    }

    if ((Key == 13)&&(validateSearchForm())) {
        document.getElementById("ctl00_ucSearchBox_btnSearch").click();
    }
}

//validate the search form
function validateSearchForm(){
    var result = true;
    if (
            (
                ($('#ctl00_ucSearchBox_txtSearchQuery').val()=="Search")
                ||($('#ctl00_ucSearchBox_txtSearchQuery').val()=="")
            )
            &&($('#ctl00_ucSearchBox_DdlCategories').val()==0)
            &&($('#ctl00_ucSearchBox_DdlBudget').val()==0)
            &&($('#ctl00_ucSearchBox_DdlArtist').val()==0)
            )
    {
        result = false;
    }
    if (result == true) {
        /*$('#ctl00_ucSearchBox_btnSearch')
        .hide();
        $('#ajaxSearchLoader')
        .show();*/
        sleep(500);
        window.location = "/SearchResults.aspx?q=" + $('#ctl00_ucSearchBox_txtSearchQuery').val() +
        "&artistId=" + $('#ctl00_ucSearchBox_DdlArtist').val() + "&budgetChoice=" + $('#ctl00_ucSearchBox_DdlBudget').val() +
        "&categoryId=" + $('#ctl00_ucSearchBox_DdlCategories').val() + "&subCategoryId=" + $('#ctl00_ucSearchBox_DdlSubCategories').val();
    }
    return false;

}



//PRODUCT FORM FUNCTION
//=====================
//Validates the product form
function validateProductForm() {
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblVs').innerHTML =
     "";
    if (isValidInput()) {
        /*$('#ctl00_ContentPlaceHolder1_ctl00_uiProductForm_btnPurchaseButton')
        .hide();
        $('#productLoader')
        .show();
        sleep(500);*/
        return true;
    }
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblVs').innerHTML =
     "* Required fields";
    return false;
}

//Validates the drop downs
function isValidInput() {

    //DROP DOWNS
    var elms =
        $('#ctl00_ContentPlaceHolder1_ctl00_uiProductForm_pnlProductFeatures select');
    var result = true;
    elms.each
   (
       function () {
           var elm = $(this);
           elm.removeClass('fieldError');
           if (elm.val() == 0) {
               result = false;
               elm.addClass('fieldError');
           }
       }
   );

   //TEXT BOXES
   var elms =
        $('#ctl00_ContentPlaceHolder1_ctl00_uiProductForm_pnlProductFeatures input:text');
    elms.each
   (
       function () {
           var elm = $(this);
           elm.removeClass('fieldError');
           if (elm.val() == "") {
               result = false;
               elm.addClass('fieldError');
           }
       }
   );


    return result;
}

//calculate the attached price for an element id.
function getPriceForElement(attachedSelectedId) {
    var attachedPrice = 0;
    var i;
    for (i = 0; i < FeatureValuesCollection.length; i++) {
        //If the required id is in the collection
        if (FeatureValuesCollection[i].Id == attachedSelectedId) {
            //Get the new attached price
            //Determine the usage of the percent changer
            if (FeatureValuesCollection[i].Type == "percent")
                attachedPrice = parseFloat($('#ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblBasePricePerUnit').html()) 
                * (FeatureValuesCollection[i].Value / 100);
            else
                attachedPrice = FeatureValuesCollection[i].Value;
        }
    }
    //return the attached price
    return attachedPrice;
}

//calculate the added price
function CalculateAddedPrice(containerId) {
    //Get all of the elements in the container that are drop downs
    var elms = $('#'+containerId+' select');
    var totalAttachedPrice = 0;
    parseFloat(totalAttachedPrice);

    //Iterate on the elements and attach the added price
    elms.each
    (
        function () {
            var elm = $(this);
            totalAttachedPrice += parseFloat(getPriceForElement(elm.val()));
        }
    );
        return totalAttachedPrice;
    }

//updates the elements for the product price
    function CalculateProductFormTotals() {
        //Get the base price.
        var basePrice = $('#ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblBasePricePerUnit').html();
        parseFloat(basePrice);

        //get the new added price
        var newPrice = parseFloat(basePrice) +
        parseFloat(CalculateAddedPrice("ctl00_ContentPlaceHolder1_ctl00_uiProductForm_pnlProductFeatures"));

        //update the price label value
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblPricePerUnit').innerHTML = newPrice;

        //call the update amount function
        updateTotalPriceLabel();
    }


//FORM FUNCTIONS
//==============
//validates textbox field by the char length as a minimum 
//required fields
function isNameValid(id, charLength) {
    var jqueryId = "#" + id;
    if (document.getElementById(id).value.length > charLength) {
        $(jqueryId).removeClass("fieldError");
        return true;
    }
    else {
        $(jqueryId).addClass("fieldError");
        $(jqueryId).focus();
        return false;
    }
}
//Validates the email field
function isEmailValid(id) {
    var jqueryId = "#" + id;
    var filter = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+){1,4}$/;
    if (filter.test(document.getElementById(id).value)) {
        $(jqueryId).removeClass("fieldError");
        return true;
    }
    else {
        $(jqueryId).addClass("fieldError");
        $(jqueryId).focus();
        return false;
    }
}

//validates that the check box has a non default value
function isCheckBoxValid(id) {
    var jqueryId = "#" + id;
    if ($(jqueryId).val() == "0") {
        $(jqueryId).addClass("fieldError");
        $(jqueryId).focus();
        return false;
    }
    else {
        $(jqueryId).removeClass("fieldError");
        return true;
    }
}




//SEO FUNCTIONS
//=============

//REDIRECT THE USER VIA JAVASCRIPT TO KEEP THE STRENGTH OF ANOTHER LINK
function JSLink(url, target) {
    if (target == 0) {
        document.location.href = url;
    }
    else {
        window.open(url);
    }
}

//CART FUNCTIONS
//==========================
//validate the cart form
function validateCartForm() {
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiCart_lblVSCart').innerHTML = "";
    $('#ctl00_ContentPlaceHolder1_ctl00_uiCart_lblVSCart').addClass("notDisplayed");
    if ($('.cartTotalValue input:radio:checked').length==0) {
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiCart_lblVSCart').innerHTML =
     'Please Select a delivery Method';
        $('#ctl00_ContentPlaceHolder1_ctl00_uiCart_lblVSCart').removeClass("notDisplayed");
        return false;
    }
    document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiCart_lblVSCart').innerHTML = "";
    /*$('#ctl00_ContentPlaceHolder1_ctl00_uiCart_btnPurchase')
        .hide();
    $('#ajaxOrderLoader')
        .show();
    sleep(500);*/
    return true;
}

//Check that the creditContainer is hidden, if the phoneOrder radio button is checked.
function CheckForPhoneCheck() {
    if (document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_radPhone").checked)
        switchToOrderPhone();
}

//Set Credit Elements to default values for the order phone.
function switchToOrderPhone() {
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardNumber").value = "4580111133334444";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardCVV").value = "000";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardOwnerName").value = "בוצעה הזמנה טלפונית";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardOwnerId").value = "בוצעה הזמנה טלפונית";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_creditContainer").style.display = "none";
    
}


//Set Credit Elements to default values for the order phone.
function switchToOrderCredt() {
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardNumber").value = "";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardCVV").value = "";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardOwnerName").value = "";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_txtCardOwnerId").value = "";
    document.getElementById("ctl00_ContentPlaceHolder1_uiOrderForm_creditContainer").style.display = "block";
}


//Updates the per unit price label.
function CalculateUnitPriceForCart(dropDownId,cartLineId) {
    //Step 1: get the value of the changed element.
    var AddedElementPriceToUnitPrice = PriceDictionary[parseInt(document.getElementById(dropDownId).value)];
    //Step 2: calculate the new currnet unit price.
    document.getElementById("unitPrice_" + cartLineId).innerHTML =
            parseInt(document.getElementById("baseUnitPrice_" + cartLineId).innerHTML) +
            AddedElementPriceToUnitPrice;
    //Step 3: Update the total price.
    updateTotalPriceForCartLine(cartLineId);
}

function updateTotalPriceForCartLine(cartLineId) {
    //Step 1: get the value of the quantity:
    var currentQuantity = parseInt(document.getElementById("quantity_" + cartLineId).value);
    //Step 2: get the value of the price per product:
    var currentPricePerUnit = parseInt(document.getElementById("unitPrice_"+cartLineId).innerHTML);
    //Step 3: Set the total price into the total price label:
    document.getElementById("totalPrice_" + cartLineId).innerHTML = currentQuantity * currentPricePerUnit;
}

//SHARE BAR FUNCTIONS
//===================

//FACEBOOK
function shareFacebook(url) {
    var urlToGo = "http://www.facebook.com/sharer.php?u=" + url;
    window.open(urlToGo, "FacebookShare", "height=436", "width=635", "left=200", "top=100", "location=no", "menubar=no", "resizable=yes",
 "scrollbars=yes", "status=no", "titlebar=no", "toolbar=no");
}

//BOOKMARK
function shareBookMark(title, url) {
    if (window.sidebar) window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) {
        var elem = document.createElement('a');
        elem.setAttribute('href', url); elem.setAttribute('title', title); elem.setAttribute('rel', 'sidebar'); elem.click();
    }
    else if (document.all) window.external.AddFavorite(url, title);
}

//REDIRECT TO PRINT FREINDLY PAGE
function sharePrint(url) {
    window.open(url, "",
 "height=800, width=1024, left=200, top=100, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=YES");
}

//SHARE A FRIEND
function shareWithAFriend(url) {
    window.open(url, "",
"height=400, width=550, left=200, top=100, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no");
}

//POP FUNCTIONS
//=============
//SHOWS AN ELEMENT
function ShowPop(id) {
    var popId = "pop" + id;
    document.getElementById(popId).style.display = "block";
}

//HIDES EN ELEMENT
function HidePop(id) {
    var popId = "pop" + id;
    document.getElementById(popId).style.display = "none";
}

//UI Functions
function colorIcon(id, src) {
    document.getElementById(id).src = src;
}


//PRODUCTS FUNCTIONS
//==================
    //Updates the per unit price label.
        function CalculateUnitPrice(dropDownId) {
            //Step 1: get the value of the changed element.
            var AddedElementPriceToUnitPrice = PriceDictionary[parseInt(document.getElementById(dropDownId).value)];
            //Step 2: calculate the new currnet unit price.
            document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_lblPricePerUnit").innerHTML =
            parseInt(document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_lblBasePricePerUnit").innerHTML) +
            AddedElementPriceToUnitPrice;
            //Step 3: Update the total price.
            updateTotalPriceLabel();
        }

    //Updates the total price label.
    function updateTotalPriceLabel() {
        //Step 1: get the value of the quantity:
        var currentQuantity = parseInt(document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiProductForm_DdlQuantity").value);
        //Step 2: get the value of the price per product:
        var currentPricePerUnit = parseInt(document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblPricePerUnit").innerHTML);
        //Step 3: Set the total price into the total price label:
        document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiProductForm_lblTotalPrice").innerHTML = currentQuantity * currentPricePerUnit;
    }

    //Updates the final price label.
    function updateFinalPrice() {
        //Step 1: get the value of the delivery price:
        var deliveryPrice = parseInt(document.getElementById("ctl00_ContentPlaceHolder1_uiCart_DdlDeliveryTypes").value);
        //Step 2: get the value of the price per product:
        var cartTotalPriceBeforeDelivery = parseInt(document.getElementById("ctl00_ContentPlaceHolder1_uiCart_lblCartMiddleSum").innerHTML);
        //Step 3: Set the total price into the total price label:
        document.getElementById("ctl00_ContentPlaceHolder1_uiCart_lblCartFinalPrice").innerHTML = deliveryPrice + cartTotalPriceBeforeDelivery;
    }

    //Adds a cart line through usage of AJAX and WCF
    function AddToCartViaAjax(sessionId) {
        //Step 1: Prepare all the variables to send to the WCF web service.
        var cmsId = document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_lblProductCmsId").innerHTML;
        var color = document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_DdlColorChoice").value;
        var size = document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_DdlSizeChoice").value;
        var type = document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_DdlTypeChoice").value;
        var quantity = document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_DdlQuantity").value;
        //Step 2: Prepare the Ajax Object and send the request.
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
       xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("ctl00_ContentPlaceHolder1_uiProductForm_lblAddToCartLineSpan").innerHTML = '<img src=\"Resources/Store/AddToCartDoneButton.png\" alt=\"המוצר בסל\"/>';
                document.getElementById("ctl00_ucPersonalZone_cartLineLink").innerHTML = xmlhttp.responseText;
            }
        }
        var wcfUrl = "WebServices/ShoppingService.svc/AddCartLine?cmsId=" + cmsId + "&color=" + color + "&size=" + size + "&type=" + type + "&quantity=" + quantity + "&sessionId=" + sessionId;
        xmlhttp.open("GET", wcfUrl, true);
        xmlhttp.send();


    }

    //Upates a cart via AJAX and WCF
    function updateCartLine(cartLineId,cartId,isRefresh) {
        
        //Step 1: Prepare all the variables to send to the WCF web service.
        var color = document.getElementById("color_"+cartLineId).value;
        var size = document.getElementById("size_"+cartLineId).value;
        var type = document.getElementById("type_"+cartLineId).value;
        var quantity = document.getElementById("quantity_"+cartLineId).value;
        wcfUrl = "WebServices/ShoppingService.svc/UpdateCartLine?cartLineId=" + cartLineId + "&color=" + color + "&size=" + size + "&type=" + type +
         "&quantity=" + quantity + "&sessionId=" + cartId;

        //Step 2: Prepare the Ajax Object and send the request.
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
       xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("updateSpan_" + cartLineId).src = "/Resources/Cart/updateCartLineDoneButton.png";
                document.getElementById("ctl00_ucPersonalZone_cartLineLink").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", wcfUrl, true);
        xmlhttp.send();
        //if (isRefresh)
           // location.href = "/cms.aspx?nameUrl=Cart";
    }

    //Removes a cart line via AJAX and WCF
    function removeCartLine(cartLineId) {
        if (confirm("Are you sure you wish to remove this product?")) {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    //Update where we at.
                    location.href = "/Cart";
                }
            }
            var wcfUrl = "WebServices/ShoppingService.svc/RemoveCartLine?cartLineId="+cartLineId;
            xmlhttp.open("GET", wcfUrl, false);
            xmlhttp.send();
            
           
        }//End confirmation check
    }

    //ORDER FORM FUNCTIONS
    //====================
    function validateOrderForm() {
        var isFormValid = true;
        //Step # 1 : Restart the output message label.
        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML = "* Required fields.";

        //Step # 2 : Determine the Fields of the billing information by reverse order.

        //Validate Dif Address
        if ($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_chkIsDif').is(":checked")) {

            //Validate Zip
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtDifZip", 1)))
                isFormValid = false;

            //Validate Country
            if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifCountry"))) {
                isFormValid = false;
            }
            else {
                if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifCountry').value == 265) {
                    if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifState")))
                        isFormValid = false;
                }
                else if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifCountry').value == 303) {
                    if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifProvince")))
                        isFormValid = false;
                }
            }

            //Validate City
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtDifCity", 1))) {
                isFormValid = false;
            }

            //Validate Address
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtDifAddress", 1))) {
                isFormValid = false;
            }

            //Validate Last Name
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtDifLastName", 1))) {
                isFormValid = false;
            }
            //Validate First Name
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtDifFirstName", 1))) {
                isFormValid = false;
            }


        }//End Validate Dif Address memebers

        //Validate Terms of use
        if (!($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_chkTerms').is(":checked"))) {
            document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML = 
            "* Please Accept the Terms of use.</br>";
            isFormValid = false;
        }

        //Validate Zip
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtZip", 1)))
            isFormValid = false;

        //Validate Country
        if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCountryId"))) {
            isFormValid = false;
        }
        else {
            if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCountryId').value == 265) {
                if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlState")))
                    isFormValid = false;
            }
            else if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCountryId').value == 303) {
                if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlProvince")))
                    isFormValid = false;
            }
        }

        //Validate City
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCity", 1))) {
            isFormValid = false;
        }

        //Validate Address
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtAddress", 1))) {
            isFormValid = false;
        }

        //Validate Email
        if (!(isEmailValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtEmail"))) {
            isFormValid = false;
        }

        //Validate Phone
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtPhone", 1))) {
            isFormValid = false;
        }

        //Validate Last Name
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtLast", 1))) {
            isFormValid = false;
        }
        //Validate First Name
        if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtFirstName", 1))) {
            isFormValid = false;
        }

        //Step # 3 : If the Method type is credit then we need to validate
        if ($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_radCreditCard').is(":checked")) {

                //Validate Credit Number
            if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCardNumber", 7))) {
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML += 
                "* Please Enter at least 7 digits in Card Number.</br>";
                    isFormValid = false;
                }
                else {

                    //Validate Credit Number as number
                    if (!(isNumber(document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCardNumber').value))) {
                        isFormValid = false;
                        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML += "* Please Enter a numeric input in Card Number.</br>";
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCardNumber').addClass("fieldError");
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCardNumber').focus();
                    }
                    else {
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCardNumber').remove("fieldError");
                    }

                }

                //Validate Credit Type
                if (!(isCheckBoxValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCreditType"))) {
                    isFormValid = false;
                }

                //Validate Name on card
                if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditName", 1))) {
                    isFormValid = false;
                }


                //Validate CVV
                if (!(isNameValid("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditCvv", 2))) {
                    isFormValid = false;
                }
                else {
                    
                    //Validate Credit Number as number
                    if (
                        !(
                        isNumber(
                        document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditCvv').value
                        )
                        )) {
                        isFormValid = false;
                        document.getElementById('ctl00_ContentPlaceHolder1_uiOrderForm_orderFormVs').innerHTML +=
                 "* Please Enter a numeric input in CVV.</br>";
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditCvv').addClass("fieldError");
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditCvv').focus();
                    }
                    else {
                        $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_txtCreditCvv').remove("fieldError");
                    }
                }

                

                //Validate Expiration Date
                var d1 = new Date();
                var d2 = new Date(Number($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlYear').val()),
                 Number($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlMonth').val()-1), 27, 1, 1, 1);
                //Check month
                if (d1 > d2) {
                    $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlMonth').addClass("fieldError");
                    $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlYear').addClass("fieldError");
                    isFormValid = false;
                }
                else {
                    $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlMonth').removeClass("fieldError");
                    $('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlYear').removeClass("fieldError");
                }
            }
            //Step # 4 : Determine display of notification and errors.
            if (isFormValid) {
                /*$('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_btnOrder')
                .hide();
                $('#orderLoading')
                .show();
                sleep(500);*/
                return true;
            }
            else {
                if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML == "")
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_orderFormVs').innerHTML +=
         "* Required fields.";
                return false;
            }
        }


        //ORDER INPUT ENTER EVENT
        //==========================
        function GetKeyPressForOrderTextBox(inField, e) {
            var charCode;

            if (e && e.which) {
                charCode = e.which;
            } else if (window.event) {
                e = window.event;
                charCode = e.keyCode;
            }

            if (charCode == 13) {
                document.getElementById("ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_btnOrder").click();
            }
        }





        //update state display
        function updateOrderFormStateDisplay() {
            document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlStateBox').style.display = 'none';
            document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlProvinceBox').style.display = 'none';
            if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCountryId').value == 265)
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlStateBox').style.display = 'block';
            if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlCountryId').value == 303)
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlProvinceBox').style.display = 'block';
        }

        //update state display for dif address
        function updateOrderFormStateDisplayDif() {
            document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlDifState').style.display = 'none';
            document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlDifProvince').style.display = 'none';
            if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifCountry').value == 265)
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlDifState').style.display = 'block';
            if (document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_DdlDifCountry').value == 303)
                document.getElementById('ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_pnlDifProvince').style.display = 'block';
        }

        //Shows/Hides the credit box dependent on payment method selected value
        function updateCreditBoxDisplay() {
            if ($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_radCreditCard').is(":checked"))
                $('#creditCardBox').slideDown();
            else
                $('#creditCardBox').slideUp();
        }

        //Show/Hides the different address div
        function slideDifferentAddress() {
            if ($('#ctl00_ContentPlaceHolder1_ctl00_uiOrderForm_chkIsDif').is(":checked")) {
                $('#differentAddress').slideDown();
                $('.middleUOV').css("height", "900px")
            }
            else {
                $('#differentAddress').slideUp();
                $('.middleUOV').css("height", "550px")
            }


        }


    //GENERAL USABILITY
    var numb = '0123456789';
    var lwr = 'abcdefghijklmnopqrstuvwxyz';
    var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    function isValid(parm, val) {
        if (parm == "") return true;
        for (i = 0; i < parm.length; i++) {
            if (val.indexOf(parm.charAt(i), 0) == -1) return false;
        }
        return true;
    }

    function isNumber(parm) { return isValid(parm, numb); }
    function isLower(parm) { return isValid(parm, lwr); }
    function isUpper(parm) { return isValid(parm, upr); }
    function isAlpha(parm) { return isValid(parm, lwr + upr); }
    function isAlphanum(parm) { return (parm, lwr + upr + numb); }

    /**
    * Delay for a number of milliseconds
    */
    function sleep(delay) {
        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
    }
