﻿function PerformSearch() {
    var queryElement = document.getElementById('txtQuery');
    var ddl = jPixeliT.$("#ddlSuppliers");
    var sId = ddl.options[ddl.selectedIndex].value;
    if (queryElement) {
        if (queryElement.value.length < 3) {
            alert('Search query must be longer than 2 characters'); return;
        }
        else {
            location.href = 'results.aspx?q=' + encodeURIComponent(queryElement.value) + "&s=" + sId;
        }
    }
}
//
// Set the default button for the site
//
function ExecuteDefaultButton(event) {
    if (!event) var event = window.event;
    if (event.keyCode == 13) {
        event.preventDefault ? event.preventDefault() : event.returnValue = false; event.cancel = true;
        if (document.getElementById('btnSearch'))
            document.getElementById('btnSearch').click();
    }
}
function setEnterKey() {
    document.onkeydown = ExecuteDefaultButton;
    if (window.addEventListener) {
        window.addEventListener('keydown', ExecuteDefaultButton, false);
    } else if (window.attachEvent) {
        window.attachEvent('onkeydown', ExecuteDefaultButton);
    }
}

function InitDdl() {
    var s = jPixeliT.querystringParam("s");
    if (s) {
        var ddl = jPixeliT.$("#ddlSuppliers");
        for (var i = 0; i < ddl.options.length; i++) {
            if (ddl.options[i].value == s) {
                ddl.selectedIndex = i;
                if (!ddl.options[i].selected) {
                    ddl.options[i].selected = true;
                }
                return;
            }
        }
    }
}

// get selected product variant text
function getSelectedVariant(id) {
    if ($('div#variations' + id + ' input').length) {
        var sel = $('div#variations' + id + ' input:checked').val();
        if (typeof sel == 'undefined') {
            return "none";
        }
        else {
            return sel;
        }
    }
    return "";
}    

function addProduct(id) {
    var variant = getSelectedVariant(id);
    if (variant == 'none') {
        alert('יש לבחור מאפשרויות המוצר');
        return;
    }
    //pageUrl, pageMethodName, async, postData, callBackMethod, objectState
    setTimeout("document.location.href = 'mycart.aspx'", 1000);
    jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "AddItem", true, "compact=true&contentId=" + id + "&v=" + variant, reloadCompactView);
}

// Compact view
function reloadCompactView(text) {
    if (document.getElementById("compactOrder")) {
        jPixeliT.setInnerHTML("#divCompactOrder", text);
    }
}
function updateQuantityCompact(element) {
    jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "UpdateItemQuantity", true, String.Format("compact=true&contentId={0}&newQuantity={1}", element.getAttribute("itemId"), element.value), reloadCompactView);
}

function removeItemCompact(id) {
    jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "RemoveItem", true, "compact=true&contentId=" + id, reloadCompactView);
}

// Full view
function reloadFullView(text) {
    if (document.getElementById("fullOrder")) {
        jPixeliT.setInnerHTML("#spanOrderView", text);
    }
}
function updateQuantityFull(element) {
    jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "UpdateItemQuantity", true, String.Format("compact=false&contentId={0}&newQuantity={1}", element.getAttribute("itemId"), element.value), reloadFullView);
}
function removeItemFull(id) {
    jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "RemoveItem", true, "compact=false&contentId=" + id, reloadFullView);
}

function getNextSelectedProduct(contentId, cubeName, index) {
    jPixeliT.Cache["contentId"] = contentId;
    jPixeliT.Cache["cubeName"] = cubeName;
    if (jPixeliT.Cache.contains(index + "")) {
        getNextSelectedProductCallback(jPixeliT.Cache[index + ""], index + "");
        return;
    }
    var postData = String.Format("contentId={0}&cubeName={1}&index={2}", contentId, cubeName, index);
    jPixeliT.Ajax.executePageMethod("selectedproductshandler.aspx", "GetNextProduct", true, postData, getNextSelectedProductCallback, (index + ""));
}
//spanOrderView
function getNextSelectedProductCallback(text, index) {
    jPixeliT.Cache[index + ""] = text;
    var parts = text.split('|');
    var elm1 = jPixeliT.$("#divProd1");
    var elm2 = jPixeliT.$("#divProd2");
    if (elm1.style.display === 'none') {
        jPixeliT.$("#img1").src = "image.axd?src=~/" + parts[0] + "&w=108&h=102";
        jPixeliT.setInnerHTML("#lnk1", String.Format("<a href='product.aspx?pid={0}' class='leftTxt'>{1}</a>", parts[3], parts[1]));
        jPixeliT.fadeOut(elm2, 500,
         function() {
             elm2.style.position = 'absolute';
             elm1.style.position = '';
             jPixeliT.fadeIn(elm1, 500);
         });
    }
    else {
        jPixeliT.$("#img2").src = "image.axd?src=~/" + parts[0] + "&w=108&h=102";
        jPixeliT.setInnerHTML("#lnk2", String.Format("<a href='product.aspx?pid={0}' class='leftTxt'>{1}</a>", parts[3], parts[1]));
        jPixeliT.fadeOut(elm1, 500,
         function() {
             elm1.style.position = 'absolute';
             elm2.style.position = '';
             jPixeliT.fadeIn(elm2, 500);
         });
    }
    setTimeout(function() { getNextSelectedProduct(jPixeliT.Cache["contentId"], jPixeliT.Cache["cubeName"], parts[4]); }, 5000);
}

function showHideMemberMsg(chkElement) {
    if (!chkElement.checked)
        document.getElementById("spanMemberMsg").style.display = "block";
}

Date.prototype.getRealYear = function() {
    if (this.getFullYear)
        return this.getFullYear();
    else
        return this.getYear() + 1900;
};


function validateEUF() {
    if (!isInteger(jPixeliT.$("#txtYear").value)) {
        alert("Please enter valid birth year");
        return false;
    }
    else {
        if (parseInt(jPixeliT.$("#txtYear").value) > (new Date().getRealYear() - 5) || parseInt(jPixeliT.$("#txtYear").value) < 1900) {
            alert("Please enter logic birth year (Format: yyyy)");
            return false;
        }
    }
    if (jPixeliT.$("#ddlMonth").selectedIndex === 0) {
        alert("Please select a birth month");
        return false;
    }
    if (jPixeliT.$("#ddlDay").selectedIndex === 0) {
        alert("Please select a birth day");
        return false;
    }
    if (jPixeliT.$("#txtPass").value.length > 0 && jPixeliT.$("#txtPass").value !== jPixeliT.$("#txtPassConfirm").value) {
        alert("Passwords do not match.");
        return false;
    }
    return true;
}

function isInteger(s) {
    var i;
    if (jPixeliT.isEmpty(s)) return false;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function isDigit(c) {
    return ((c >= "0") && (c <= "9"))
}


function copyDest(chkElement) {
    if (chkElement.checked) {
        jPixeliT.$("#txtFirstNameDest").value = jPixeliT.$("#txtFirstName").value;
        jPixeliT.$("#txtLastNameDest").value = jPixeliT.$("#txtLastName").value;
        jPixeliT.$("#txtPhoneDest").value = jPixeliT.$("#txtPhone").value;
        jPixeliT.$("#txtCityDest").value = jPixeliT.$("#txtCity").value;
        jPixeliT.$("#txtAddressDest").value = jPixeliT.$("#txtAddress").value;
        jPixeliT.$("#txtPostalDest").value = jPixeliT.$("#txtPostalCode").value;

        jPixeliT.$("#txtFirstNameDest").readOnly = "readonly";
        jPixeliT.$("#txtLastNameDest").readOnly = "readonly";
        jPixeliT.$("#txtPhoneDest").readOnly = "readonly";
        jPixeliT.$("#txtCityDest").readOnly = "readonly";
        jPixeliT.$("#txtAddressDest").readOnly = "readonly";
        jPixeliT.$("#txtPostalDest").readOnly = "readonly";

    }
    else {
        jPixeliT.$("#txtFirstNameDest").readOnly = "";
        jPixeliT.$("#txtLastNameDest").readOnly = "";
        jPixeliT.$("#txtPhoneDest").readOnly = "";
        jPixeliT.$("#txtCityDest").readOnly = "";
        jPixeliT.$("#txtAddressDest").readOnly = "";
        jPixeliT.$("#txtPostalDest").readOnly = "";
    }
}

function validateNewUser() {
    if (jPixeliT.$("#txtPass").value !== jPixeliT.$("#txtPassConfirm").value) {
        alert("Passwords do not match.");
        return false;
    }
    if (jPixeliT.$("#chkAgreed").checked == false) {
        alert("You must confirm terms and conditions");
        return false;
    }
    if (!isInteger(jPixeliT.$("#txtYear").value)) {
        alert("Please enter valid birth year");
        return false;
    }
    else {
        if (parseInt(jPixeliT.$("#txtYear").value) > (new Date().getRealYear() - 5) || parseInt(jPixeliT.$("#txtYear").value) < 1900) {
            alert("Please enter logic birth year (Format: yyyy)");
            return false;
        }
    }
    if (jPixeliT.$("#ddlMonth").selectedIndex === 0) {
        alert("Please select a birth month");
        return false;
    }
    if (jPixeliT.$("#ddlDay").selectedIndex === 0) {
        alert("Please select a birth day");
        return false;
    }
    return true;
}

function copyDestNewUser(chkElement) {
    if (chkElement.checked) {
        var details = jPixeliT.Ajax.executePageMethod("orderhandler.aspx", "GetNewUserDetails", false);
        if (jPixeliT.isEmpty(details)) {
            return;
        }
        var parts = details.split('|');
        jPixeliT.$("#txtFirstName").value = parts[0];
        jPixeliT.$("#txtLastName").value = parts[1];
        jPixeliT.$("#txtPhone").value = parts[2];
        jPixeliT.$("#txtCity").value = parts[3];
        jPixeliT.$("#txtAddress").value = parts[4];
        jPixeliT.$("#txtPostalCode").value = parts[5];

        jPixeliT.$("#txtFirstName").readOnly = "readonly";
        jPixeliT.$("#txtLastName").readOnly = "readonly";
        jPixeliT.$("#txtPhone").readOnly = "readonly";
        jPixeliT.$("#txtCity").readOnly = "readonly";
        jPixeliT.$("#txtAddress").readOnly = "readonly";
        jPixeliT.$("#txtPostalCode").readOnly = "readonly";

    }
    else {
        jPixeliT.$("#txtFirstName").readOnly = "";
        jPixeliT.$("#txtLastName").readOnly = "";
        jPixeliT.$("#txtPhone").readOnly = "";
        jPixeliT.$("#txtCity").readOnly = "";
        jPixeliT.$("#txtAddress").readOnly = "";
        jPixeliT.$("#txtPostalCode").readOnly = "";
    }
}

function validatePayment() {
    if (!isInteger(jPixeliT.$("#txtId").value)) {
        alert("Please enter valid id");
        return false;
    }
    if (!isInteger(jPixeliT.$("#txtCvv").value)) {
        alert("Please enter valid cvv");
        return false;
    }
    if (!isInteger(jPixeliT.$("#txtCardNum").value)) {
        alert("Please enter valid card number");
        return false;
    }

    return true;
}
function getContentText(contentId) {
    if (jPixeliT.Cache.contains("cont_" + contentId)) {
        showTermInfo(jPixeliT.Cache["cont_" + contentId]);
        return;
    }
    var postData = "contentId=" + contentId;
    jPixeliT.Ajax.executePageMethod("OrderHandler.aspx", "GetContentText", true, postData, getContentTextCallback, contentId);
}
function getContentTextCallback(text, contentId) {
    var parts = text.split('|');
    var html = String.Format("<div><b><u>{0}</u></b></div><br /><div>{1}</div><div>{2}</div>", parts[0], parts[1], parts[2]);
    jPixeliT.Cache["cont_" + contentId] = html;
    showTermInfo(html);
}
function showTermInfo(html) {
    jPixeliT.setInnerHTML("#divTermInfo", html);
}
