﻿var x = new Pco.Page.XR();
var shopVal;

function add_basket(iid) {
    var PID = iid;
    var P = 0;
    var C = 1;
    insertProduct(PID, C, P);
    var c_cart = checkCount();
    $('cart_div').innerHTML = (c_cart != '0' ? '<a style="color:white;text-decoration:none;" href="http://' + window.location.host + '/__page?shc=1">' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + ' (' + c_cart + ')</a>' : sb.lang.mstr.shopping.viewcart[site.thm.lang] + ' (' + c_cart + ')')
}
function isDigit(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
function calculate(Count) {
    $('price').innerHTML = Count * 2000;
}
function getCartData() {
    x = new Pco.Page.XR();
    get_cookie('Cartck');
    Pco.Page.PXRFire(x, '../../Ajax?kzo=ShoppingCart', 'SPID=' + site.thm.pID + '&Params=' + shopVal, Cart_Result, false);
    //Pco.Page.PXRFire(x, '../XR/ShoppingCart.aspx', 'SPID=' + site.thm.pID + '&Params=' + shopVal, Cart_Result, false);
}
function Cart_Result() {
    if (x.readyState == 4) {
        $('result').innerHTML = '';
        if (x.responseText.split(',')[0] == "ok") {
            $('result').innerHTML = x.responseText.split(',')[1];
        }
        else if (x.responseText.split(',')[0] == "Msg") {
        if (x.responseText.split(',')[1] == 'EmptyCart')
            window.location = 'http://' + window.location.host;
        else
            fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], "<div class=\"flo\" style=\"margin:20px 1% 0;width:13%px;height:auto\"><img style=\"border:0\" src=\"http://pcocdn.net/club/img/caution.gif\"/></div><div class=\"flo txt_al_r\" style=\"margin:20px 2% 0;width:81%;height:auto;line-height:22px;text-align:justify\">" + x.responseText.split(',')[1] + "</div>", '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
        }
    }
}
function ePay_Result() {
    if (x.readyState == 4) {
        if (x.responseText.split(',')[0] == "ok") {
            postRefId(x.responseText.split(',')[1]);
        }
        else if (x.responseText.split(',')[0] == "Msg") {
            $('waiting').innerHTML = Pco.String.Empty;
            fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], "<div class=\"flo\" style=\"margin:20px 1% 0;width:13%px;height:auto\"><img style=\"border:0\" src=\"http://pcocdn.net/club/img/caution.gif\"/></div><div class=\"flo txt_al_r\" style=\"margin:20px 2% 0;width:81%;height:auto;line-height:22px;text-align:justify\">" + x.responseText.split(',')[1] + "</div>", '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
        }
    }
}
///این متد محتوای یک کوکی را بر می گرداند
function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results) {
        shopVal = unescape(results[2])
        return true;
    }
    else
        return null;
}
function isDigit(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
function calculateFormValues(id) {
    var i = id.toString().split('-')[1];
    if ($(id).value == '0' || $(id).value == '')
        $(id).value = '1';
    var count = $(id).value;
    $('ItemTotalPrice-' + i.toString()).innerHTML = count * parseInt($('ItemPrice-' + i.toString()).innerHTML);
    calculateFooter();
}

function calculateFooter() {
    var ic = document.getElementsByName('IC');
    var cnt = 0;
    for (var j = 0; j < ic.length; j++) {
        cnt += parseInt($(ic[j].id).value);
    }
    $('TotalCount').innerHTML = cnt;
    var itp = document.getElementsByName('ITP');
    var prc = 0;
    for (var k = 0; k < itp.length; k++) {
        prc += parseFloat($(itp[k].id).innerHTML);
    }
    $('TotalPrice').innerHTML = prc;
    $('Tax').innerHTML = prc * 0.04;
    $('PayAmount').innerHTML = prc + prc * 0.04;
}
function validateForm(Type) {
    $('message').innerHTML = '';
    s('message').display = 'none';
    if ($('txtName').value == null || $('txtName').value == '') { $('message').innerHTML += sb.lang.mstr.valid.nameforce[site.thm.lang] + '<br />'; }
    //if ($('txtLName').value == null || $('txtLName').value == '') { $('message').innerHTML += 'نام خانوادگی را وارد نکرده ايد' + '<br />'; }
    if ($('txtPhone').value == null || $('txtPhone').value == '') { $('message').innerHTML += sb.lang.mstr.valid.phoneforce[site.thm.lang] + '<br />'; }
    if ((!Pco.UI.Validate.Digital($('txtPhone').value) || $('txtPhone').value.length > 15 || $('txtPhone').value.length < 5) && ($('txtPhone').value != '' && $('txtPhone').value != null)) { $('message').innerHTML += sb.lang.mstr.valid.wrongphone[site.thm.lang] + '<br />'; }
    if ($('txtMobile').value == null || $('txtMobile').value == '') { $('message').innerHTML += sb.lang.mstr.valid.mobileforce[site.thm.lang] + '<br />'; }
    if ((!Pco.UI.Validate.Digital($('txtMobile').value) || $('txtMobile').value.length > 15 || $('txtMobile').value.length < 5) && ($('txtMobile').value != '' && $('txtMobile').value != null)) { $('message').innerHTML += sb.lang.mstr.valid.wrongmobile[site.thm.lang] + '<br />'; }
    //if ($('txtFax').value == null || $('txtFax').value == '') { $('message').innerHTML += sb.lang.mstr.valid.faxforce[site.thm.lang] + '<br />'; }
    if ((!Pco.UI.Validate.Digital($('txtFax').value) || $('txtFax').value.length > 15 || $('txtFax').value.length < 5) && ($('txtFax').value != '' && $('txtFax').value != null)) { $('message').innerHTML += sb.lang.mstr.valid.wrongfax[site.thm.lang] + '<br />'; }
    if ($('txtMail').value == null || $('txtMail').value == '') { $('message').innerHTML += sb.lang.mstr.valid.emailforce[site.thm.lang] + '<br />'; }
    if (!Pco.UI.Validate.Email($('txtMail').value)) { $('message').innerHTML += sb.lang.mstr.valid.notlikeemail[site.thm.lang] + '<br />'; }
    if ($('txtAddress').value == null || $('txtAddress').value == '') { $('message').innerHTML += sb.lang.mstr.valid.addressforce[site.thm.lang] + '<br />'; }
    if ($('message').innerHTML != '') { s('message').display = 'block'; }
    if ($('message').innerHTML == '') {
        if (Type == 'Invoice') showPreInvoiceBuy('i');
        else if (Type == 'Buy') { showPreInvoiceBuy('b'); s('Buy').backgroundColor = '#dfdede'; s('ePay').backgroundColor = '#dfdede'; Pco.UI.SetAttribute('ePay', 'onclick', 'return false;'); Pco.UI.SetAttribute('Buy', 'onclick', 'return false;'); }
        else if (Type == 'ePay') { showPreInvoiceBuy('e'); s('Buy').backgroundColor = '#dfdede'; s('ePay').backgroundColor = '#dfdede'; $('waiting').innerHTML = '<div style="width:90%; text-align:center; line-height:150px; height:100%; display:block; background-position:center center; background-repeat:no-repeat;background-image: url(http://pcocdn.net/club/img/u/LoadingProgressBar.gif);color:black;">' + sb.lang.mstr.shopping.contactingBank[site.thm.lang] + '</div>'; Pco.UI.SetAttribute('ePay', 'onclick', 'return false;'); Pco.UI.SetAttribute('Buy', 'onclick', 'return false;'); }
    }
}
var cntPar = '';
function showPreInvoiceBuy(Type) {
    var ic = document.getElementsByName('IC');
    cntPar = '';
    for (var j = 0; j < ic.length; j++) {
        cntPar += $(ic[j].id).value + ',';
    }
    cntPar = Pco.String.SubString(cntPar, 0, cntPar.length - 1);
    if (Type == 'i')
        Pco.Page.Open('', '', '', '', sb.lang.mstr.shopping.preInvoice[site.thm.lang], '../../__page?preInv=1');
    else {
        x = new Pco.Page.XR();
        Pco.Page.PXRFire(x, (Type == 'e' ? '../../Ajax?kzo=BuyOnline' : '../../Ajax?kzo=ShoppingCart'), 'SPID=' + site.thm.pID + '&Params=' + shopVal + "&Name=" + $('txtName').value + "&Phone=" + $('txtPhone').value + "&Mobile=" + $('txtMobile').value + "&Fax=" + $('txtFax').value + "&Email=" + $('txtMail').value + "&Address=" + $('txtAddress').value + "&Note=" + $('txtNote').value + "&Count=" + cntPar + (Type == 'b' ? '&Buy=true' : ''), (Type == 'e' ? ePay_Result : Cart_Result), false);
        //  Pco.Page.PXRFire(x, (Type == 'e' ? '../../XR/BuyOnline.aspx' : '../../XR/ShoppingCart.aspx'), 'SPID=' + site.thm.pID + '&Params=' + shopVal + "&Name=" + $('txtName').value + "&Phone=" + $('txtPhone').value + "&Mobile=" + $('txtMobile').value + "&Fax=" + $('txtFax').value + "&Email=" + $('txtMail').value + "&Address=" + $('txtAddress').value + "&Note=" + $('txtNote').value + "&Count=" + cntPar + (Type == 'b' ? '&Buy=true' : ''), (Type == 'e' ? ePay_Result : Cart_Result), false);

    }
}
///این متد برای اضافه کردن یک محصول به کوکی استفاده می شود
function insertProduct(ProductID, Count, Price) {
    var R = "PID=" + ProductID + ",Count=" + Count + ",Price=" + Price;
    var flag = 0;
    if (get_cookie('Cartck') == true) {
        var temp = new Array();
        temp = shopVal.split('~~');
        for (var i = 0; i < temp.length; i++) {
            var rec = temp[i].split(',');
            if (rec[0].toLowerCase() == "pid=" + ProductID) {
                fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px;text-align:justify\">' + sb.lang.mstr.shopping.existsInCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Exist.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 150px; margin:0px 2px;\" onclick=\"goViewCart()\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
                flag = 1;
                break;
            }
        }
        if (!flag) {
            if (temp.length < 20) {
                Pco.Browser.Deletecookie('Cartck');
                R += "~~" + shopVal;
                Pco.Browser.setCookie('Cartck', R);
                fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px;text-align:justify\">' + sb.lang.mstr.shopping.addedToCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Add.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 150px; margin:0px 2px;\" onclick=\"goViewCart()\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
            }
            else {
                fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px;text-align:justify\">' + sb.lang.mstr.shopping.fullCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Full.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 150px; margin:0px 2px;\" onclick=\"goViewCart()\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
            }
        }
        ////مدت زمان بقای این کوکی یک روز است
    }
    else {
        Pco.Browser.setCookie('Cartck', R);
        fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px;text-align:justify\">' + sb.lang.mstr.shopping.addedToCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Add.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 150px; margin:0px 2px;\" onclick=\"goViewCart()\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
    }
}

function deleteProduct(ProductID) {
    fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px\">' + sb.lang.mstr.shopping.delFromCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Del.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.cancel[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"delete_cookie_ByProductID(' + ProductID + ')\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
}

///این متد یک محصول را با شماره محصول مورد نظر دریافت شده از کوکی حذف می کند
function delete_cookie_ByProductID(ProductID) {
    get_cookie('Cartck');
    var temp = new Array();
    temp = shopVal.split('~~');
    shopVal = "";
    if (temp.length == 1) {
        Pco.Browser.Deletecookie('Cartck');
        window.location = 'http://' + window.location.host;
    }
    else {
        for (var i = 0; i < temp.length; i++) {
            var rec = temp[i].split(',');
            if (rec[0].toLowerCase() != "pid=" + ProductID)
                shopVal += temp[i] + '~~';
        }
        shopVal = shopVal.substring(0, shopVal.length - 2);
        Pco.Browser.Deletecookie('Cartck');
        Pco.Browser.setCookie('Cartck', shopVal);
        //fin(350, 200, sb.lang.mstr.shopping.attention[site.thm.lang], '<div class=\"flo txt_al_r\" style=\"margin:20px 2%;width:56%;height:auto;line-height:22px;text-align:justify\">' + sb.lang.mstr.shopping.delFromCart[site.thm.lang] + '</div><div class=\"flo txt_al_c\" style=\"margin:20px 2%;width:36%;height:auto;\"><img src=\"http://pcocdn.net/img/sb/manage/Cart_Del.png\" style=\"border:0\"/></div><div align=\"center\" class=\"flo\" style=\"width:100%;height:auto\"><input type=\"button\" value=\"' + sb.lang.mstr.shopping.ok[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 80px; margin:0px 2px;\" onclick=\"fout()\"/><input type=\"button\" value=\"' + sb.lang.mstr.shopping.cancel[site.thm.lang] + '\" class=\"Opt_Btn\" style=\"width: 150px; margin:0px 2px;\" onclick=\"goViewCart()\" /></div>', '5e5858', '80', 'eae9e9', '5177bd', 'fff', 'fff', 'r');
    }
    $('d' + ProductID).innerHTML = Pco.String.Empty;
    s('d' + ProductID).display = 'none';
    calculateFooter();
    fout();
    var c_cart = checkCount();
    $('cart_div').innerHTML = (c_cart != '0' ? '<a style="color:white;text-decoration:none;" href="http://' + window.location.host + '/__page?shc=1">' + sb.lang.mstr.shopping.viewcart[site.thm.lang] + ' (' + c_cart + ')</a>' : sb.lang.mstr.shopping.viewcart[site.thm.lang] + ' (' + c_cart + ')')
}

///این متد یک محصول را با شماره محصول مورد نظر دریافت شده را بر می گرداند
function get_cookie_ByProductID(ProductID) {
    get_cookie('Cartck');
    var temp = new Array();
    temp = shopVal.split('~~');
    shopVal = "";
    for (var i = 0; i < temp.length; i++) {
        var rec = temp[i].split(',');
        if (rec[0].toLowerCase() == "pid=" + ProductID)
            alert(temp[i]);
    }
}
function checkCount() {
    if (get_cookie('Cartck') == true) {
        var temp = new Array();
        temp = shopVal.split('~~');

        return temp.length;
    }
    else
        return '0';
}
function goViewCart() {
    window.location = 'http://' + window.location.host + '/__page?shc=1'
}
function checkCount() {
    if (get_cookie("Cartck") == true) {
        var temp = new Array();
        temp = shopVal.split('~~');

        return temp.length;
    }
    else
        return '0';
}


