﻿//只能输入数字
jQuery.fn.onlypressnum = function() {
    $(this).css({ imeMode: "disabled", '-moz-user-select': "none" });
    $(this).bind("keypress", function(e) {
        /*alert(e.which);
        $.each(e,function(i,val){
        alert(i+"|"+val);
        });
        ialert(e.ctrlKey);*/
        if (e.ctrlKey == true || e.shiftKey == true)
            return false;
        if ((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 || e.which == 8 || e.which == 46)
            return true;
        else if (e.ctrlKey == true && (e.which == 99 || e.which == 118))
            return false;
        else
            return false;
    })
.bind("contextmenu", function() { return false; })
//.bind("selectstart", function() { return false; })
.bind("paste", function() { return false; });
};

 
jQuery.getParmByUrl = function(o) {
            var url = window.location.toString();
            var tmp;
            if (url && url.indexOf("?")) {
                var arr = url.split("?");
                var parms = arr[1];
                if (parms && parms.indexOf("&")) {
                    var parmList = parms.split("&");
                    jQuery.each(parmList, function(key, val) {
                        if (val && val.indexOf("=")) {
                            var parmarr = val.split("=");
                            if (o) {
                                if (typeof (o) == "string" && o == parmarr[0]) {
                                    tmp = parmarr[1] == null ? '' : parmarr[1];
                                }
                            }
                            else {
                                tmp = parms;
                            }
                        }
                    });
                }
            }
            return tmp;
 }

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


// 搜索

function Search() {
    if ($("#txtKeyword").val() != '') {
        location.href = "/search.aspx?Keyword=" + $("#txtKeyword").val();
    } else {
        alert("请输入搜索关键字");
    }
}

// 倒计时
function GetRemainTime(showTimeID, beginTime, endTime) {
   beginTime = ConvertToDate(beginTime);
    var nowTime = new Date();
    var nMS = beginTime.getTime() - nowTime.getTime();
    var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));
    var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
    var nM = Math.floor(nMS / (1000 * 60)) % 60;
    var nS = Math.floor(nMS / 1000) % 60;
    if (nD >= 0) {
        var showHtml = "还有 ";
        showHtml += "<strong>" + nD + "</strong> 天 ";
        showHtml += "<strong>" + nH + "</strong> 时 ";
        showHtml += "<strong>" + nM + "</strong> 分 ";
        showHtml += "<strong>" + nS + "</strong> 秒 ";
        $("#" + showTimeID).html(showHtml);
    }
    else {     
        //var nowTimeStr = nowTime.getFullYear() + "-" + nowTime.getMonth()+1 + "-" + nowTime.getDate() + " " + nowTime.getHours() + ":" + nowTime.getMinutes() + ":" + nowTime.getMilliseconds();
   
        if (nowTime < ConvertToDate(endTime)) {
            $("#" + showTimeID).html("<span class='sales_timer_on'>抢购正在进行中……</span>");
        }
        else {
            $("#" + showTimeID).html("<span class='sales_timer_off'>抢购已结束<span>");
        }
    }
}

function RemainTime() {
    $(".sales_timer").each(function() {
        var goodsID = $(this).attr("rel");
        var showID = $(this).attr("id");
        var strBeginTime = $("#sales_begintime_" + goodsID).val();
        var strEndTime = $("#sales_endtime_" + goodsID).val();
        var beginTime = ConvertToDate(strBeginTime);
        var endTime = ConvertToDate(strEndTime);
        GetRemainTime(showID, beginTime, endTime);
    });
}


// 字符转成日期时间，字符格式：2010-08-12 12:23:23
function ConvertToDate(strDateTime)
{
  var arrDT = strDateTime.split(" ");  
  var arrDate = arrDT[0].split("-");  
  var arrTime = arrDT[1].split(":"); 
  // 不知道为何月份要减1
  var result =  new Date(arrDate[0],arrDate[1] - 1,arrDate[2],arrTime[0],arrTime[1],arrTime[2]); 
  return result;
}

