﻿//只能输入数字
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; });
};


//获得URL中的查询
//示例： $().getParmByUrl($("#QueryID").val());
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;
}

//获取和设置Cookie值
//$.cookie('the_cookie'); // 获得cookie
//$.cookie('the_cookie', 'the_value'); // 设置cookie
//$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie
//$.cookie('the_cookie', '', { expires: -1 }); // 删除
//$.cookie('the_cookie', null); // 删除 cookie
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 GetRemainTime(showTimeID,beginTime,endTime)
{
    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 
    {
        if (nowTime < endTime) {
            $("#" + showTimeID).html("<span class='sales_timer_on'>抢购正在进行中，将于<strong>" + endTime.getHours() + ":" + endTime.getMinutes() + "</strong> 分结束 </span>");
      }
      else
      {
         $("#"+ showTimeID).html("<span class='sales_timer_off'>抢购已结束<span>");
      }
    }
}

function RemainTime()
{
   $(".sales_timer").each(function()
   {
      var id = $(this).attr("rel");
      var beginTime = ConvertToDate($("#sales_begintime_"+  id).val());
      var endTime = ConvertToDate($("#sales_endtime_"+ id).val());
      GetRemainTime("sales_timer_"+ id,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;
}

