﻿var QQTop;
$(document).ready(function() {
    basePage.closeQQ();
    QQTop = parseInt($("#divMenuQQ").css("top"));
    window.setTimeout(basePage.moveQQ, 300);
});
//自定义有关String的方法
String.prototype.Trim = function() { return this.replace(/^\s+|\s+$/g, ""); }
String.prototype.IsNullOrEmpty = function() { var m = /\s+/; return m.test(this.toString().Trim()) && this.toString() == ""; }

var basePage = {
    //加入收藏通用方法
    addFavorite: function(title, url) {
        if (url.indexOf("http") < 1)
            url = "http://" + url;
        if (window.sidebar) {
            window.sidebar.addPanel(title, url, "");
        } else if (document.all) {
            window.external.AddFavorite(url, title);
        } else if (window.opera && window.print) {
            return true;
        }
    },
    //客户留言客户端验证
    validateMessage: function(l) {
        var yourname = $("input[name='yourname']").val().Trim();
        var email = $("input[name='email']").val().Trim();
        var country = $("#cbmcountry option:selected").val().Trim();
        var tel = $("input[name='telephone']").val().Trim();

        var content = $("#content").val().Trim();
        var erroString = false;
        if (yourname.IsNullOrEmpty()) {
            if (l) erroString = "请填写您的姓名!";
            else
                erroString = "Please Enter Your Name !";
        }
        else if (email.IsNullOrEmpty()) {
            if (l) erroString = "请填写您的邮箱!";
            else
                erroString = "Please enter your email!";
        }
        else if (country == 0) {
            if (l) erroString = "请选择国家!";
            else
                erroString = "Please choice a country!";
        }
        else if (tel.IsNullOrEmpty()) {
            if (l) erroString = "请填写您的联系电话!";
            else
                erroString = "Please enter your telephone!";
        }
        //        else if (subject.IsNullOrEmpty()) {
        //            if (l) erroString = "请填写主题!";
        //            else
        //                erroString = "Please enter subject!";
        //        }

        else if (content.length < 1) {
            if (l)
                erroString = "请填写内容!";
            else
                erroString = "Please enter content!";
        }
        else if (!basePage.testEmail(email)) {
            if (l)
                erroString = "请输入正确的邮箱，以便我们及时回复！";
            else
                erroString = "Please enter your Email!";
        }

        if (erroString != false) {
            alert(erroString);
            return false;
        }
        else
            $("form").submit();
    },
    //验证邮箱
    testEmail: function(s) {
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        return reg.test(s);
    },
    //站外搜索
    LookChemSearch: function() {
        var key = $("#searchkeys").val().Trim();
        if (!key.IsNullOrEmpty()) {
            window.open("http://www.lookchem.com/search.aspx?type=productname&k=" + encodeURI(key));
        }
        else
        { alert("请输入关键词!"); }
    },
    //导航条的选中
    selectCurrentNav: function(c) {
        var a = $("ul li a:contains('" + c + "')");
        //a.parent("span").toggleClass("onlick");
        a.parents("li").toggleClass("on");
    },
    //QQ移动
    moveQQ: function() {
        var QQmenu = $("#divMenuQQ");
        var NowTop = parseInt(QQmenu.css("top"));
        var MoveTo = (document.documentElement.scrollTop + QQTop);
        timeoutNextCheck = 300
        if (NowTop != MoveTo) {
            yOffset = Math.ceil(Math.abs(NowTop - MoveTo) / 20);
            if (NowTop > MoveTo) { yOffset = -yOffset; }
            QQmenu.css("top", (NowTop + yOffset) + "px");
            timeoutNextCheck = 10;
        }
        window.setTimeout(basePage.moveQQ, timeoutNextCheck);
    },
    //关闭
    closeQQ: function() {
    $("#divMenuQQ img:first").click(function() {        
            $("#divMenuQQ").hide();
            return false;
        });
    },
    //验证order
    validateOrder: function(l) {
        var postcode = $("input[name='postcode']").val().Trim();
        var fax = $("input[name='fax']").val().Trim();
        var companyname = $("input[name='companyname']").val().Trim();
        var contacter = $("input[name='contacter']").val().Trim();
        var address = $("input[name='address']").val().Trim();
        var telephone = $("input[name='telephone']").val().Trim();
        var productname = $("input[name='productname']").val().Trim();
        var casno = $("input[name='casno']").val().Trim();
        var _package = $("input[name='package']").val().Trim();
        var quantity = $("input[name='quantity']").val().Trim();
        var Delivery = $("input[name='Delivery']").val().Trim();
        var freight = $("input[name='freight']").val().Trim();
        var shipto = $("input[name='shipto']").val().Trim();
        var payment = $("input[name='payment']").val().Trim();
        var delivery_data = $("input[name='delivery_data']").val().Trim();
        var requirments = $("#requirments").val().Trim();
        if (postcode.length > 2) {

            if (isNaN(postcode)) {
                if (l) {
                    alert("请填写正确的邮编!");
                } else {
                    alert("Please fill in the correct zip code!");
                }
                return false;
            }
        }
        else if (companyname.IsNullOrEmpty()) {
            if (l) {
                alert("请填写公司名称！");
            }
            else {
                alert("Please fill out the company name!");
            }
            return false;
        }
        else if (contacter.IsNullOrEmpty()) {
            if (l) {
                alert("请填写联系人！");
            }
            else {
                alert("Please fill in the contact!");
            }
            return false;
        }
        else if (telephone.IsNullOrEmpty()) {
            if (l) {
                alert("请填写联系电话！");
            }
            else {
                alert("Please fill out the contact phone!");
            }
            return false;
        }
        else if (productname.IsNullOrEmpty()) {
            if (l) {
                alert("请填写产品名称！");
            } else {
                alert("Please fill in the product name!");
            }
            return false;
        }
        return true;

    }
};
//产品分类
function menucheck(menunum, imgone, imgtwo, hhhnum) {
    var ids ="";
    var reg = /\d+/;     
    $("div[id^='sqamenu']").each(function(i){

        ids +=reg.exec($(this).attr("id"))+",";
    });
    ids = ids.split(",");    
    var a = document.getElementById(menunum + hhhnum);
    var d = document.getElementById(imgone + hhhnum);
    var e = document.getElementById(imgtwo + hhhnum);
    var b = hhhnum;
    var c;
    for(j = 0;j<ids.length;j++){    
        i = ids[j]; 
        if (i == b) { if (a.className == 'undis') { a.className = 'dis'; d.src = '/images/fopen.gif'; e.src = '/images/f2open.gif'; } else { a.className = 'undis'; d.src = '/images/fclose.gif'; e.src = '/images/f2close.gif'; } }
        else
        { c = document.getElementById(menunum + i); f = document.getElementById(imgone + i); g = document.getElementById(imgtwo + i); if (c != null) { c.className = 'undis'; f.src = '/images/fclose.gif'; g.src = '/images/f2close.gif'; } }
    }
}
//产品分类
function openmenu(menunum, imgone, imgtwo, hhhnum) {       
    var a = document.getElementById(menunum + hhhnum);
    var d = document.getElementById(imgone + hhhnum);
    var e = document.getElementById(imgtwo + hhhnum);
    var b = hhhnum;
    if (a != null) {
        a.className = 'dis'; d.src = '/images/fopen.gif'; e.src = '/images/f2open.gif';
    }
}
//产品分类
function SetSelected(ob) {
    document.getElementById("proclass" + ob).className = "nav3_h";
}

//图片等比例缩小
function DrawImage(ImgD, FitWidth, FitHeight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= FitWidth / FitHeight) {
            if (image.width > FitWidth) {
                ImgD.width = FitWidth;
                ImgD.height = (image.height * FitWidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        } else {
            if (image.height > FitHeight) {
                ImgD.height = FitHeight;
                ImgD.width = (image.width * FitHeight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}
