﻿

function PrinableView() { var title = "iCMS printable version"; s = "scrollbars=yes,toolbar=yes,width=600,height=400"; zedc = window.open("", "popFrameless", s); zedc.blur(); window.focus(); var frameString = "<html><head><title>" + title + "</title><body onload='window.print()' dir=rtl>" + document.getElementById("printable").innerHTML + "</body></html>"; zedc.document.open(); zedc.document.write(frameString); zedc.document.close(); zedc.focus(); }
function BookMark() { var title = document.title; var url = document.location.href; if (window.sidebar) window.sidebar.addPanel(title, url, ""); else if (window.opera && window.print) { var mbm = document.createElement('a'); mbm.setAttribute('rel', 'sidebar'); mbm.setAttribute('href', url); mbm.setAttribute('title', title); mbm.click() } else if (document.all) window.external.AddFavorite(url, title); }
function ShowShopMailBox() {
    var id = document.location.href;
    $('body').append('<div class="MailBoxBlocker"></div>');
    $('.MailBoxBlocker').css('opacity', '0.7').css('height', $(document).height() + 'px');
    $('body').append('<div class="MailBoxDiv"><iframe id="MailFrame" height="270" width="540"  marginwidth="0" marginheight="0" scrolling="no" border="0" frameborder="0" ></iframe><div>');
    $('.MailBoxDiv').css('left', (($(window).width() - 540) / 2) + 'px').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px');
    $('#MailFrame').attr('src', '/incs/SendMailBox.aspx?id=' + id + '&lang=fa' );
    $('.MailBoxDiv').css("z-index", parseInt(new Date().getTime() / 1000));
    $(window).scroll(function() { $('.MailBoxDiv').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px'); });
} //Handle textarea comment and URL description character count 
function textCounter(field, countfield, maxlimit) { if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit); else countfield.value = maxlimit - field.value.length }
// Menu Literal
if (document.getElementById) { document.write('<style type="text/css">\n'); document.write('.options{display: none;}\n'); document.write('</style>\n'); }
function SwitchMenu(obj) { try { if (document.getElementById) { var el = document.getElementById(obj); var ar = document.getElementById("masterdiv").getElementsByTagName("span"); if (el.style.display != "block") { for (var i = 0; i < ar.length; i++) { if (ar[i].className == "options") ar[i].style.display = "none"; } el.style.display = "block"; } else { el.style.display = "none"; } } } catch (e) { } }
function EnlargImages(path, pic, w, h) { var url = '/incs/ImageShow.aspx?p=' + path + '&pic=' + pic + "&w=" + w + "&h=" + h; var argin = 'images'; var option = "scrollbars=yes,toolbar=no,resizable=yes,width=" + w + ",height=" + h; window.open(url, argin, option) }
// Cart scripts
function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else { expires = ""; }
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; }
function deleteCart(name) { var result = true; var returnVal = ""; var cook = Left(readCookie('icmsshop'), readCookie('icmsshop').length - 1); var ca = cook.split(','); for (var i = 0; i < ca.length; i++) { var c = ca[i]; if (c == name) { result = false } else returnVal += c + ","; } if (result) { alert('مشخصات کالای مورد نظر شما در سبد خریدتان یافت نشد') } else { createCookie('icmsshop', returnVal, 7); } }
function eraseCookie(name) { createCookie(name, "", -1); }


function addToCart(val) {
    try {
        var answer = confirm("آیا مایلید این کالا به سبد خریدتان اضافه شود؟");
        if (answer) {
            var cookies;
            if (readCookie('icmsshop') == null || readCookie('icmsshop') == 'null') { cookies = val + ',' }
            else { cookies = readCookie('icmsshop') + val + ',' } createCookie('icmsshop', cookies, 7);
            var cook = Left(readCookie('icmsshop'), readCookie('icmsshop').length - 1);
            var ca = cook.split(',');
            $("#shoppingCartCount").text(ca.length);
        }
    }
    catch (e) { }
}
function Right(str, n) { if (n <= 0) return ""; else if (n > String(str).length) return str; else { var iLen = String(str).length; return String(str).substring(iLen, iLen - n) } }
function Left(str, n) { if (n <= 0) return ""; else if (n > String(str).length) return str; else return String(str).substring(0, n) }

/*New From Version 3.0.0.2*/

function TotalPriceWithShipWay(ID, price, Dest) {
    var CPrice = $('#' + ID).text().replace(new RegExp(",", "g"), "");
    $('#' + Dest).text(addCommas(eval(CPrice) + price));
}


$(document).ready(function() {

})
function ReloadShoppingCartShipWay() {
    var SelectedShipWay = readCookie('icmsshopshipway');
    $("input[@type='radio']", '.Shop_ShipWay_List').each(function() {
        if (eval($(this).val().split(',')[1]) == SelectedShipWay) {
            $(this).attr("checked", "checked");
        }
        UpdateListInfo();
    });
}
function MyShopHistoryDelFileListRow(obj, id) {
    if (confirm("آیا با حذف موافقید؟")) {
        DelteMyShopHistoryItem(id, obj);
    }
}
function DelFileListRow(obj, id) {
    if (confirm("آیا با حذف موافقید؟")) {
        $(obj).parent().parent().remove();
        if (UpdateCookieValue(id)) {
            UpdateListInfo();
        }
        else {
            document.location.reload();
        }
    }
}
function UpdateCookieValue(id) {
    var CookieVal = readCookie('icmsshop');
    if (CookieVal != null || CookieVal != 'null') {
        var CookieArray = CookieVal.split(',');
        var ReturnArray = "";
        for (var i = 0; i < CookieArray.length - 1; i++) {
            if (CookieArray[i] != '') {
                if (CookieArray[i] != id)
                    ReturnArray += CookieArray[i] + ",";
            }
        }
        if (ReturnArray != "") { createCookie('icmsshop', ReturnArray, 7) }
        else { eraseCookie('icmsshop'); }
    }
    if (readCookie('icmsshop') == '' || readCookie('icmsshop') == ',' || readCookie('icmsshop') == 'null' || readCookie('icmsshop') == null) {
        return false
    }
    return true;
};

function UpdateCookieValueByCount(id, count) {
    var CookieVal = readCookie('icmsshop');
    if (CookieVal != null || CookieVal != 'null') {
        var CookieArray = CookieVal.split(',');
        var ReturnArray = "";
        for (var i = 0; i < CookieArray.length - 1; i++) {
            if (CookieArray[i] != id) {
                ReturnArray += CookieArray[i] + ",";
            }
        }
        for (var j = 0; j < count; j++) {
            ReturnArray += id + ",";
        }
        if (ReturnArray != "") { createCookie('icmsshop', ReturnArray, 7); UpdateListInfo(); };
    }
};

function UpdateListInfo() {
    var Total = 0;
    $('.Shop_CheckOut_Box .Shop_Item_Count').each(function() {
        var Count = $(this).val();
        var Price = eval($('.Shop_Item_Price', $(this).parent().parent()).text().replace(new RegExp(",", "g"), ""));
        Total += Count * Price;
    });
    $('.Shop_CheckOut_Total span').text(addCommas(Total));
    $("input[@type='radio']", '.Shop_ShipWay_List').each(function() {
        if ($(this).is(":checked")) {
            $('.Shop_ShipWay_Total').text(addCommas(Total + eval($(this).val().split(',')[0])));
        }
    });
};

function addCommas(num) {
    var tmpNumStr = new String(num);
    if (num >= 1000 || num <= -1000) {
        var iStart = tmpNumStr.length;
        iStart -= 3;
        while (iStart >= 1) {
            tmpNumStr = tmpNumStr.substring(0, iStart) + "," + tmpNumStr.substring(iStart, tmpNumStr.length)
            iStart -= 3;
        }
    }
    return tmpNumStr;
};

function PayShipMethodSetCookie(id, name) {
    createCookie(name, id, 7);
}

$(document).ready(function() {

    $('.Shop_Item_Count').each(function() {
        $(this).change(function() {
            var val = $(this).val();
            var id = eval($('.Shop_Item_Id', $(this).parent().parent()).text());
            var max = eval($('.Shop_Item_Quantity', $(this).parent().parent()).text());
            if (val > max) { val = max }
            if (val < 1) { val = 1 }
            $(this).val(val)
            var price = eval($('.Shop_Item_Price', $(this).parent().parent()).text().replace(new RegExp(",", "g"), ""));
            $('.Shop_Item_SumPrice', $(this).parent().parent()).text(addCommas(val * price));
            UpdateCookieValueByCount(id, val)
        });

        $(this).keypress(function(e) {
            if (e.which > 47 && e.which < 58)
                if ($(this).val().length < 3)
                return true;
            return false;
        });
    });
});

