var newWindow = null
var reQuantity = /^[0-9]{1,}\.?[0-9]{0,1}$/;
var rePrice = /^[0-9]{1,}\.?[0-9]{0,2}$/;
var reNumber = /^[0-9]{1,}$/;
var reNo = /^[0-9a-zA-Z]+$/;

function CheckAll(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name != 'SelAll') {
            e.checked = form.SelAll.checked;
        }
    }
}

function CheckOne(obj) {
    if (obj.checked == false) {
        document.getElementsByName("SelAll")[0].checked = obj.chcked;
        document.getElementsByName("SelAll2")[0].checked = obj.chcked;
    }
}


function MM_openBrWindow(theURL, winName, features) { //v2.0
    newWindow = window.open(theURL, winName, features);
    newWindow.focus();
}


function Delete(form, msg, url) {
    if (confirm(msg)) {
        form.action = url;
        form.submit();
    }
}

function sel_ad_img(ad_img, ProductPictureID) {
    document.all.ad_img.src = ad_img;
}


var cartWindow = null
function OpenCartWindow(theURL) { //v2.0
    cartWindow = window.open(theURL, "ShoppingCart", "top=0,left=0,width=720,height=500,scrollbars=no");
    cartWindow.focus();
}

function SelectOne(tagobj, productID) {
    for (i = 0; i < tagobj.length; i++) {
        if (tagobj(i).value == productID) {
            tagobj(i).checked = true;
        }
        else {
            tagobj(i).checked = false;
        }
    }
}

function CheckCheckBox(form) {
    var cked = false;
    for (var i = 0; i < form.SelCheckbox.length; i++) {
        if (form.SelCheckbox[i].checked) {
            cked = true;
            break;
        }
    }
    if (!cked) {
        alert("Please select one!");
        return false;
    }
    else {
        return true;
    }
}

function FixImgWin(url) {
    var imgwin = window.open('', 'img', 'top=100,left=100,width=50,height=50');
    imgwin.focus();
    var HTML = "<html>\r\n<head>\r\n<title>Picture</title>\r\n</head>\r\n<body leftmargin=\"0\" topmargin=\"0\">\r\n<img src=\"" + url + "\" onload=\"window.resizeTo(this.width+10,this.height+29);window.moveTo((screen.width-this.width)/2,(screen.height-this.height)/2)\">\r\n</body>\r\n</html>";
    var doc = imgwin.document;
    doc.open("text/html", "replace");
    doc.write(HTML);
    doc.close();
}


function RowClick(obj) {
    //obj.style.backgroundColor=obj.style.backgroundColor=='#d7e8fa'?'#ffffff':'#d7e8fa';
}
function RowChange(obj, bChange) {
    if (obj.style.backgroundColor != '#d7e8fa') {
        if (bChange == "over")
            obj.style.backgroundColor = "#ffffe1";
        if (bChange == "out")
            obj.style.backgroundColor = "#eff6fd";
    }
}

function Loading() {
    document.write("<div id=\"Loading\" style=\"width=100%;height=100%;\">");
    document.write("<table width=\"100%\" height=\"100%\" bgcolor=\"#ffffff\"><tr><td vAlign=\"middle\">");
    document.write("<table align=\"center\"><tr>");
    document.write("<td><IMG src=\"../../Images/loading.gif\" border=\"0\"></td>");
    document.write("<td>Loading...</td>");
    document.write("</tr></table>");
    document.write("</td></tr></table>");
    document.write("</div>");
}

function UnLoading() {
    document.all['Loading'].style.display = "none";
}

function CheckNull(object, msg) {
    if (object.value == "") {
        alert(msg);
        object.focus();
        return false;
    }
    else {
        return true;
    }
}

function CheckCompare(object, object2, msg) {
    if (object.value != object2.value) {
        alert(msg);
        object.focus();
        return false;
    }
    else {
        return true;
    }
}

function CheckFormat(object, msg, re) {
    if (object.value != "") {
        if (!re.test(object.value)) {
            alert(msg);
            object.focus();
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

function CheckEmail(object, msg) {
    if (object.value != "") {
        if (!isEmail(object.value)) {
            alert(msg);
            object.focus();
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

function isEmail(theStr) {
    var atIndex = theStr.indexOf('@');
    var doIndex = theStr.indexOf('.', atIndex);
    var flag = true;
    theSub = theStr.substring(0, doIndex + 1)
    if ((atIndex < 1) || (atIndex != theStr.lastIndexOf('@')) || (doIndex < atIndex + 2) || (theStr.length <= theSub.length)) {
        flag = false;
    }
    else {
        flag = true;
    }
    return (flag);
}

function SetValueAndCloseWin(strValue) {
    opener.document.all.ProductNo.value = strValue;
    opener.document.all.ProductNo.focus();
    window.close();
}

function resizeImage(img, width, height) {
    var image = new Image();
    image.src = img.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= width / height) {
            if (image.width > width) {
                img.width = width;
                img.height = (image.height * width) / image.width;
            } else {
                img.width = image.width;
                img.height = image.height;
            }
        }
        else {
            if (image.height > height) {
                img.height = height;
                img.width = (image.width * height) / image.height;
            } else {
                img.width = image.width;
                img.height = image.height;
            }
        }
    }
}

/*=========================================================================*/

/*倒计时*/
var diff=null;
function RemainTime(spanRT,dt1) {
        today=(new Date()).getTime(); 
        
        msPerDay=24*60*60*1000;
        msPerHour=60*60*1000;
        msPerMinute=60*1000;
        
        timeOld=(dt1-today)+diff;
  
        daysOld=Math.floor(timeOld/msPerDay);
        
        e_daysOld=timeOld-daysOld*msPerDay;
        
        hourOld=Math.floor(e_daysOld/msPerHour);
                
        e_hourOld=e_daysOld-hourOld*msPerHour;
        
        minuteOld=Math.floor(e_hourOld/msPerMinute);
        
        e_minuteOld=e_hourOld-minuteOld*msPerMinute;
        
        secondOld=Math.floor(e_minuteOld/1000);
        
    if (timeOld<0)
        {
            if(typeof(TimeEndHandle)=="undefined")
            {
                window.location.reload();
            }
            else
            {
                TimeEndHandle();
            }
        }
    else
        document.getElementById(spanRT).innerHTML =daysOld+"天"+hourOld+"时"+minuteOld+"分"+secondOld+"秒" ;
}

function RunRemainTime(startTime, endTime, spanRT) {
    tmp= startTime.split(/ /);
    datearray=tmp[0].split(/-/);

    timearray=tmp[1].split(/:/);
    var servertime;
    servertime=new Date(datearray[0],datearray[1]-1,datearray[2],timearray[0],timearray[1],timearray[2]).getTime(); 
    if(diff==null)
    {
        diff=(new Date()).getTime()-servertime;
    }

    var dt1;
    var yyyy,mm,d,h,m,s;
    tmp= endTime.split(/ /);
    datearray=tmp[0].split(/-/);
    timearray=tmp[1].split(/:/);
        
    dt1=new Date(datearray[0],datearray[1]-1,datearray[2],timearray[0],timearray[1],timearray[2]).getTime(); 
    window.setInterval(function() { RemainTime(spanRT,dt1) }, 1000);
}




//调用例子如下：
//RunRemainTime('3/12/2008 13:00:00', '3/13/2008 0:00:00')
//<div id="divRemainTime"></div>
/*=========================================================================*/


/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format) {
    /*
    * eg:format="YYYY-MM-dd hh:mm:ss";
    */
    var o = {
        "M+": this.getMonth() + 1,  //month
        "d+": this.getDate(),     //day
        "h+": this.getHours(),    //hour
        "m+": this.getMinutes(),  //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

/*
使用方法:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
*/

function strToDate(str) {
    var val = Date.parse(str);
    var newDate = new Date(val);
    return newDate;
} 



