var SYSTEM_CONFIG = {
    CliWidth: 800,
    CliHeight: 600,
    CookieOpt:{expires: 365,path: "/"}//cookie  period
}

//Ready
$(document).ready(function() {
    setUI();
});

function setUI() {

    SYSTEM_CONFIG.CliWidth = $(window).width();
    SYSTEM_CONFIG.CliHeight = $(window).height();

    //set sub search
    setSubSearchEvent();
    
    //set png
    //fixPng();

    //set main
    adjustMain();

    //Set LaodMask
    adjustLoad();

    //set miniCar
    //    adjustMiniCar();
    
    
}


/*
* set loading
*/
function setLoading(b) {

    var tp = (SYSTEM_CONFIG.CliHeight - parseInt($('#loadMask').height())) / 2 + document.documentElement.scrollTop;

    $('#loadMask').css('top', tp);

    if (b) $('#loadMask').show();
    else $('#loadMask').hide();
}

function setSubSearchEvent() {
    $('.navsearch').focus(function() { var o = $(this); if (o.val().Trim().toLowerCase() == 'product search') { o.val('') } else this.select(); }).blur(function() { if ($(this).val() == '') { $(this).val('Product Search'); $('.navsearchgo').attr('disabled', true); } else $('.navsearchgo').removeAttr('disabled') });
}
////UserFrom
function userFrom(callback) {
    $.getJSON("../Handler/SystemHandler.ashx?r=" + Math.random(),
    { func: 'GetUserFrom' },
    function(res) {
        if (res && res.table[0].rows.length > 0) {
            $.cookie('ptg_user_web_from', res.table[0].rows[0]);
        }

        if (callback) callback(getCountry());
    });
}
//UserLogin
function runLoginIn() {

}

function runLoginOut() {

}
function getCountry() {
    //1. from cookie
    var countrySelected = $.cookie('ptg_user_country_selected');
    //2. from login info
    if (countrySelected == null && $.cookie('ptg_user_info_login') != null)
        countrySelected = $.cookie('ptg_user_info_login').Country;
    //3. from page response
    if (countrySelected == null && $.cookie('ptg_user_web_from') != null)
        countrySelected = $.cookie('ptg_user_web_from').CountryName;

    return countrySelected;
}

function adjustLoad() {
    var lf = (SYSTEM_CONFIG.CliWidth - parseInt($('#loadMask').width())) / 2;
    var tp = (SYSTEM_CONFIG.CliHeight - parseInt($('#loadMask').height())) / 2;
    $('#loadMask').css('left', lf).css('top', tp);
}

function adjustMain(h) {
    if (h) $("#main").css("height", parseInt($("#main").css("height")) + h);
    else {
        var hf = window.location.href;
        var isd = (hf.indexOf("Default.aspx") > 0 && hf.indexOf("ReturnUrl") < 0) || hf.indexOf(".aspx") < 0;
      
        var hc = Math.max(parseInt($("#left_col")[0].offsetHeight), parseInt($("#right_col")[0].offsetHeight));
        if (!isd && hc < 380)
            hc = 380;
        $("#main").css("height", hc);
    }
}

//get Resquest
function getRequest(item) {
    var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
    return svalue ? svalue[1] : svalue;
}

function buildQueryStr(p) { var q = ""; for (var i = 0; i < p.length; i++) { q = q + (i > 0 ? "&" : "") + escape(p[i].name) + "=" + escape(p[i].value); } return q; }

String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); }
String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); }
String.prototype.Filter = function() {
    var re = /select|update|delete|exec|count|\'|\||;|"|=|\;|>|<|%/g;
    return this.replace(re, "");
}

String.prototype.endWith = function(s) {
    var p = this.lastIndexOf(s);
    if (p + s.length == this.length) {
        return true;
    } else
        return false;
}
//format number "#" int,"#.00" float,"#.00#","#.0%":present
Number.prototype.format = function(f) {
    var n = this;

    if (f == null) f = "#.00";
    var isPercent = false;
    if (f.endWith("%")) {
        isPercent = true;
        n = n * 100;
        f = f.substring(0, f.length - 1);
    }
    var v;
    if (f == "#") {
        v = String(Math.round(n));
    } else {
        var c = 0;
        if (f.endWith("#")) {
            c = f.length - 3;
            v = "" + n;
        } else {
            c = f.length - 2;
            var p = Math.pow(10, c);
            var t = p * n;
            t = Math.round(t);
            v = String(t / p);
        }
        if (c > 0) {
            if (v.indexOf(".") === -1) {
                v = v + ".0";
            }
            var p = v.indexOf(".") + 1;
            if (v.length - p < c) {
                var l = c - (v.length - p);
                for (var i = 0; i < l; i++) {
                    v = v + "0";
                }
            }
        }
    }
    if (isPercent === true) {
        v = v + "%";
    }
    return v;
}

//Json
function json2str(o){
   var r = [];
   if(typeof o == "string" || o == null) {
     return "'"+o+"'";
   }
   if(typeof o == "object"){
     if(!o.sort){
       r[0]="{"
       for(var i in o){
         r[r.length]=i;
         r[r.length]=":";
         r[r.length] = json2str(o[i]);
         r[r.length]=",";
       }
       r[r.length-1]="}"
     }else{
       r[0]="["
       for(var i =0;i<o.length;i++){
           r[r.length] = json2str(o[i]);
         r[r.length]=",";
       }
       r[r.length-1]="]"
     }
     return r.join("");
   }
   return o.toString();
} 

//string ת json object
function str2json(str){
    return eval("(" + str + ")");
}

function switchKey() {
    if (window.event.keyCode == 13) window.event.keyCode = 9
}


/*
* SmartGrid Extent Method
*/
function GetSelectedRows(gridID) {
    aryItems = yy_GetDateForItems(gridID);

    if (aryItems == null) return 0;
    if (aryItems && aryItems.length == 1) {
        aryItem = aryItems[0].split(itemSeparator);
        var count = 0;
        for (var j = 0; j < aryItem.length; j++) {
            if (yy_GetObject(aryItem[j]) && yy_GetObject(aryItem[j]).checked) {
                count++;
            }
        }
        return count;
    }
    return 0;
}
function IsSelectedOne(gridID) {
    return GetSelectedRows(gridID) == 1;
}
function IsNotSelected(gridID) {
    return GetSelectedRows(gridID) == 0;
}
/*
* SmartGrid Extent Method End
*/

//only num
function clearNoNum(obj) {
    obj.value = obj.value.replace(/[^\d]/g, "");
}
//only num and letter
function clearNoNumLet(obj) {
    obj.value = obj.value.replace(/[^\d\w]/g, "");
}

function doPrint(title, type) {
    bdhtml = window.document.body.innerHTML;
    sprnstr = "<!--startprint-->";
    eprnstr = "<!--endprint-->";
    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));

    prnhtml = prnhtml.replace(/<!--ignorestart-->(.*)<!--ignoreend-->/g, "");

    //prnhtml = RemoveAH(prnhtml);
    ifmPrint.document.title = title;
    ifmPrint.document.body.innerHTML = prnhtml;
    ifmPrint.focus();
    ifmPrint.todo();
}

//delete Tag
function RemoveHTML(strText) {
    var regEx = /<[^>]*>/g;
    return strText.replace(regEx, "");
}
//delete A and Text
function RemoveA(strText) {
    var regEx = /<(A|a)[^>]*>[^>]*<[^>](A|a)>/g;
    return strText.replace(regEx, "");
}
//delete A
function RemoveAH(strText) {
    var regEx = /(<(A|a)[^>]*>)|(<[^>](A|a)>)/g;
    return strText.replace(regEx, "");
}

function openwindow(url, name, iWidth, iHeight) {
    var url; var name; var iWidth; var iHeight; 
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; 
    window.open(url, name, 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',toolbar=no,menubar=yes,scrollbars=yes,resizable=yes,location=no,status=no');
}

//set form readonly intput
function setReadOnly(frm) {
    $(frm).find('input[readOnly!=""]').css('border-color', '#fff');
} 