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;
//            }
//        }
    //    }

    pwidth = img.width;          //取得图片宽度
    if (pwidth > width) {          //如果图片宽度大于指定宽度则缩小
        pheight = img.height;         //获取图片高度
        rheight = Math.ceil(pheight * (width / pwidth));    //按比例计算图片高度
        img.width = width;
        img.height = rheight;
    }
}

function copyLink() {
    try {
        var cText = document.location.toString(); if (window.clipboardData) { window.clipboardData.setData("Text", cText); alert("复制完成!"); } else if (window.netscape) {
            try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("您的浏览器设置为不允许复制！\n如果需要此操作，请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true',再重试复制操作!"); return false; }
            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) { return; }
            trans.addDataFlavor('text/unicode'); var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); str.data = cText; trans.setTransferData("text/unicode", str, cText.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); alert("复制完成!");
        }
    } catch (e) { }
}

function getCurrentStyle(obj, cssproperty, csspropertyNS) {
    if (obj.style[cssproperty]) {
        return obj.style[cssproperty];
    }
    if (obj.currentStyle) {// IE5+   
        return obj.currentStyle[cssproperty];
    } else if (document.defaultView.getComputedStyle(obj, null)) {// FF/Mozilla   
        var currentStyle = document.defaultView.getComputedStyle(obj, null);
        var value = currentStyle.getPropertyValue(csspropertyNS);
        if (!value) {//try this method   
            value = currentStyle[cssproperty];
        }
        return value;
    } else if (window.getComputedStyle) {// NS6+   
        var currentStyle = window.getComputedStyle(obj, "");
        return currentStyle.getPropertyValue(csspropertyNS);
    }
}


function SetTextBoxHeight(textBox) {
    var sh = textBox.scrollHeight;
    textBox.style.height = sh + "px";
    var hh = getCurrentStyle(textBox, 'height', 'height');
    var h = parseInt(hh.match(/[0-9]+/gi)[0]);
    h = window.ActiveXObject ? h + textBox.scrollTop - 10 : h + textBox.scrollTop;
    if (h < 32) {
        h = 32;
    }
    textBox.style.height = h + 'px';
}

function getElementPos(elementId) {
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof      
    var el = document.getElementById(elementId);

    if (el.parentNode === null || el.style.display == 'none') {
        return false;
    }
    var parent = null;
    var pos = [];
    var box;
    if (el.getBoundingClientRect)    //IE  
    {
        box = el.getBoundingClientRect();
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
        return { x: box.left + scrollLeft, y: box.top + scrollTop };
    }
    else if (document.getBoxObjectFor)    // gecko    
    {
        box = document.getBoxObjectFor(el);
        var borderLeft = (el.style.borderLeftWidth) ? parseInt(el.style.borderLeftWidth) : 0;
        var borderTop = (el.style.borderTopWidth) ? parseInt(el.style.borderTopWidth) : 0;
        pos = [box.x - borderLeft, box.y - borderTop];
    }
    else
    // safari & opera    
    {
        pos = [el.offsetLeft, el.offsetTop];
        parent = el.offsetParent;
        if (parent != el) {
            while (parent) {
                pos[0] += parent.offsetLeft;
                pos[1] += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
        if (ua.indexOf('opera') != -1 || (ua.indexOf('safari') != -1 && el.style.position == 'absolute')) {
            pos[0] -= document.body.offsetLeft;
            pos[1] -= document.body.offsetTop;
        }
    }
    if (el.parentNode) {
        parent = el.parentNode;
    }
    else {
        parent = null;
    }
    while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors       
        pos[0] -= parent.scrollLeft;
        pos[1] -= parent.scrollTop;
        if (parent.parentNode) {
            parent = parent.parentNode;
        }
        else {
            parent = null;
        }
    }
    return { x: pos[0], y: pos[1] };
}


function zoom(obj) {
    var sourcesrc = obj.getAttribute("sourcesrc");
    if (obj.src.indexOf("_160.", 1) > 0) {
        var div = document.createElement("div");
        div.style.textAlign = "right";
        div.innerHTML = "<a href='" + sourcesrc + "' target='_blank'>查看原图</a>";
        obj.parentNode.insertBefore(div, obj);
        var src = "http://file.saige.com/smallImage.aspx?width=460&height=460&url=" + encodeURI(sourcesrc);
        obj.src = src;
        obj.className = "pic2";
        obj.parentNode.className = "cen_home_say_img";
    }
    else {
        var div = obj.previousSibling;
        while (div != null) {
            div.parentNode.removeChild(div);
            div = obj.previousSibling; ;
        }
        obj.src = sourcesrc.replace(".", "_160.");
        obj.className = "pic";
        obj.parentNode.className = "";
    }
}



