﻿// JScript 文件

var InstallDir = "/";

///选择已设置的收货地址
function selectAddressID(obj)
{
    var ContacterName = document.getElementById("ContacterName");
    var txtAreaID_dropArea = document.getElementById("txtAreaID_dropArea");
    var Address = document.getElementById("Address");
    var ZipCode = document.getElementById("ZipCode");
    var Phone = document.getElementById("Phone");
    var Mobile = document.getElementById("Mobile");
    
    if(obj.value > 0)
    {
        ContacterName.disabled = true;
        txtAreaID_dropArea.disabled = true;
        Address.disabled = true;
        ZipCode.disabled = true;
        Phone.disabled = true;
        Mobile.disabled = true;
        
        ContacterName.className = "disabled";
        txtAreaID_dropArea.className = "disabled";
        Address.className = "disabled";
        ZipCode.className = "disabled";
        Phone.className = "disabled";
        Mobile.className = "disabled";
    }
    else
    {
        ContacterName.disabled = false;
        txtAreaID_dropArea.disabled = false;
        Address.disabled = false;
        ZipCode.disabled = false;
        Phone.disabled = false;
        Mobile.disabled = false;
        
        ContacterName.className = "";
        txtAreaID_dropArea.className = "";
        Address.className = "";
        ZipCode.className = "";
        Phone.className = "";
        Mobile.className = "";
    }
}
// 选择已设置的收货地址结束

///用户名检测
function checkusername(objvalue,msgid)
{
    if(objvalue!="")
    {
        var xmlReq = CreateXMLHttpRequest();
        var url = InstallDir + "Ajax/CheckReg.aspx?username=" + escape(objvalue) + "&rnd="+Math.random();
        xmlReq.open("GET", url, false);
        xmlReq.onreadystatechange = function() 
        {
            switch(xmlReq.readyState)
            {
                case 4:
                    var arrresult = xmlReq.responseText.split(",");
                    switch(arrresult[0])
                    {
                        case "0":
                            document.getElementById(msgid).innerText = "用户名可以注册。";
                            break;
                        case "1":
                            document.getElementById(msgid).innerText = "对不起！用户名已被注册。";
                            break;
                        case "2":
                            document.getElementById(msgid).innerText = "最大长度不能超过 "+arrresult[1]+" 个字符！";
                            break;
                        case "3":
                            document.getElementById(msgid).innerText = "长度必须大于 "+arrresult[1]+" 个字符！";
                            break;
                    }
                    break;
                default:
                    break;
            }
        } 
        xmlReq.send(null);
    }
}

//用户名检测

///处理地区下拉筐
function txtAreaID_onchange(obj,partentdiv,div,selectname)
{
    var CurrentValue = document.getElementById('txtAreaID');
    if(!CurrentValue){ alert("浏览器不支持多级地区下拉筐，该操作可能丢失数据，建议使用IE浏览器访问本站！\n\n需要更多帮助请联系网站管理员。"); return;}
    CurrentValue.value = obj.value;
    txtAreaIDremovediv(partentdiv,div);
    if(obj.value == ''||obj.value == '0')
    {
        var per = document.getElementById(obj.id.substr(0,(obj.id).length-2)); 
        if(per){CurrentValue.value = per.value;} 
        return;
    }
    var spanobj = document.createElement('span'); spanobj.id=div;
    spanobj.innerHTML = "Loading...";
    if(document.readyState=="complete")
        document.getElementById(partentdiv).appendChild(spanobj);
    var xmlReq = CreateXMLHttpRequest();
    if(xmlReq == null){ return; }
    var url = InstallDir + "Ajax/Area.aspx?parentid=" + obj.value + "&rnd="+Math.random();
    xmlReq.open("GET", url, false);
    xmlReq.onreadystatechange = function() 
    {
        switch(xmlReq.readyState)
        {
            case 4:
                var myXML = CreateDOMDocument();
                if (myXML.load(xmlReq.responseXML))
                {
                    txtAreaIDSetOption(myXML,partentdiv,div,selectname);
                } 
                break;
            default:
                break;
        }
    } 
    xmlReq.send(null);
}
function txtAreaIDSetOption(myXML,partentdiv,div,selectname)
{
    var str = '';
    var node = myXML.documentElement.childNodes;
    for(var i=0; i < node.length; i++)
    {
        str += '<option value=' + node[i].childNodes[0].text + '>' + node[i].childNodes[1].text + '</option>'; 
    }
    if(str != '')
    {
        var spanobj = document.getElementById(div);
        if(!spanobj)
        {
            spanobj = document.createElement('span'); 
            spanobj.id=div; 
        }
        spanobj.innerHTML = '<select name="'+selectname+'_s" id="'+selectname+'_s" onchange="return txtAreaID_onchange(this,\''+div+'\',\''+div+'_n\',\''+selectname+'_s\')" ><option value="" >请选择</option>' + str + '</select>';
        if(document.readyState=="complete")
            document.getElementById(partentdiv).appendChild(spanobj);
    }
    else
    {
        document.getElementById(partentdiv).removeChild(document.getElementById(div));
    }
}
function txtAreaIDremovediv(partentdiv,div)
{
    var divobj = document.getElementById(div); 
    if(divobj)
    { 
        document.getElementById(partentdiv).removeChild(divobj);
    } 
}
///处理地区下拉筐结束

///显示地区面板
function zlx_showdeliverarea(obj)
{
    var xmlReq = CreateXMLHttpRequest();
    if(xmlReq == null){ return; }
    var url = InstallDir + "Ajax/Area.aspx?parentid=0&rnd="+Math.random();
    xmlReq.open("GET", url, false);
    xmlReq.onreadystatechange = function() 
    {
        switch(xmlReq.readyState)
        {
            case 4:
                var myXML = CreateDOMDocument();
                if (myXML.load(xmlReq.responseXML))
                {
                    deliver_SetOption(myXML,obj);
                } 
                break;
            default:
                break;
        }
    } 
    xmlReq.send(null);
}
function deliver_SetOption(myXML,obj)
{
    var str = "";
    var node = myXML.documentElement.childNodes;
    for(var i=0; i < node.length; i++)
    {
        str += "<div Author=zlx onclick=\"return zlx_selectdeliver('','"+node[i].childNodes[1].text+"')\">" + node[i].childNodes[1].text + "</div>"; 
    }
    
    var objtop  = obj.offsetTop;     //TT控件的定位点高
    var objheight  = obj.clientHeight;  //TT控件本身的高
    var objleft = obj.offsetLeft;    //TT控件的定位点宽
    var objtype  = obj.type;          //TT控件的类型
    while (obj = obj.offsetParent){objtop += obj.offsetTop; objleft += obj.offsetLeft;}
    
    if(document.getElementById("zlxArea"))
    {
        document.body.removeChild(document.getElementById("zlxArea"));
    }
    else
    {
        var zlxArea = document.createElement('div');
        zlxArea.id = "zlxArea";
        zlxArea.setAttribute("Author","zlx");
        zlxArea.className = "areabox";
        zlxArea.style.position = "absolute";
        zlxArea.style.left = objleft;
        zlxArea.style.top = (objtop + 20);
        zlxArea.style.zIndex = 100 ;
        zlxArea.innerHTML = str;
        if(document.readyState=="complete")
            document.body.appendChild(zlxArea);
    }
    
    event.returnValue=false;
}
///显示地区面板结束

///列出商品图片
function listproductimages(strpath,installdir,userid,imglinktype)
{
    var userdir = "";
    if(userid>0){userdir = userid+"/";}
    
    var arrpath = strpath.split('|');
    document.write("<table class='imglist'><tr><td class='l'><img src=\""+installdir+"Images/switch_left.gif\" onclick=\"return moveimglist('l')\" /></td><td class='m'><div id='prodlistimgbar'><div id='listimgdiv'>")
    for(var i=0;i<arrpath.length;i++)
    {
        var arrpicpath = arrpath[i].split(';');
        var smallimg = installdir + "UploadFile/" + userdir + arrpicpath[0];
        var midimg = installdir + "UploadFile/" + userdir + arrpicpath[1];
        var bigimg = installdir + "UploadFile/" + userdir + arrpicpath[2];
        if(imglinktype=="1")
        {
            smallimg = arrpicpath[0];
            midimg = arrpicpath[1];
            bigimg = arrpicpath[2];
        }
        classstr = "";
        if(i==0)
            classstr = "class=\"defaultimg\"";
        document.write("<img src=" + smallimg + " "+classstr+" id='productlistimage' onerror=\"this.src='"+installdir+"images/noproductpic.gif'\" onclick=\"return showselect(this,'"+midimg+"','"+bigimg+"')\" /> ");
    }
    document.write("</div></div></td><td class='r'><img src=\""+installdir+"Images/switch_right.gif\" onclick=\"return moveimglist('r')\" /></td></tr></table>")
}
function showselect(obj,midpath,bigpath)
{
    if(document.getElementById("productmainimage"))
        document.getElementById("productmainimage").src = midpath;
    if(document.getElementById("butbigimg"))
        document.getElementById("butbigimg").href=bigpath;
    
    var productlistimage = document.getElementsByName("productlistimage");
    if(productlistimage)
    {
        if(!productlistimage.length)
            productlistimage.className = "";
        else
        {
            for(var i=0;i<productlistimage.length;i++)
            {
                productlistimage[i].className = "";
            }
        }
    }
    obj.className = "defaultimg";
}
function moveimglist(x){
    var listimgdiv = document.getElementById("listimgdiv");
    if(listimgdiv)
    {
        if(x == "l")
        {
            if(listimgdiv.offsetLeft<0)
                listimgdiv.style.left = listimgdiv.offsetLeft+53;
        }else if(x == "r")
        {
            if(listimgdiv.offsetLeft>(0-listimgdiv.offsetWidth) && listimgdiv.offsetWidth>210)
                listimgdiv.style.left = listimgdiv.offsetLeft-53;
        }
    }
    
}
///列出商品图片结束

//显示/隐藏 预览图
function showimgbox(obj,path)
{
    var str = "<div class=\"Picture\" onmouseover=\"return keepshowimgbox()\" onmouseout=\"return hiddenimgbox()\"><span></span>";
    str += "<div><a href='"+path+"' target=\"_blank\"><img src=\"" + path + "\" border=0 onerror=\"this.src='" + InstallDir + "Images/noproductpic.gif'\" id=\"previewImage\" onload=\"return ResizeImage(this,200,200)\" /></a></div></div>";
    var objtop  = obj.offsetTop;     //TT控件的定位点高
    var objheight  = obj.clientHeight;  //TT控件本身的高
    var objwidth  = obj.clientWidth;  //TT控件本身的宽
    var objleft = obj.offsetLeft;    //TT控件的定位点宽
    var objtype  = obj.type;          //TT控件的类型
    while (obj = obj.offsetParent){objtop += obj.offsetTop; objleft += obj.offsetLeft;}
    
    if(document.getElementById("PreviewBox"))
    {
        var imgbox = document.getElementById("PreviewBox");
        imgbox.style.position = "absolute";
        imgbox.style.display="";
        imgbox.style.left = (objtype == "image") ? (objleft + objwidth) : (objleft + objwidth);
        imgbox.style.top = objtop;
        imgbox.innerHTML = str;
        return;
    }
    
    var imgbox = document.createElement('div');
    imgbox.id = "PreviewBox";
    imgbox.setAttribute("Author","zlx");
    imgbox.className = "PreviewBox";
    imgbox.style.position = "absolute";
    imgbox.style.left = (objtype == "image") ? (objleft + objwidth) : (objleft + objwidth);
    imgbox.style.top = objtop;
    imgbox.style.zIndex = 100 ;
    imgbox.innerHTML = str;
    if(document.readyState=="complete")    
        document.body.appendChild(imgbox);
}
function keepshowimgbox()
{
    if(document.getElementById("PreviewBox"))
        document.getElementById("PreviewBox").style.display="";
}
function hiddenimgbox()
{
    if(document.getElementById("PreviewBox"))
        document.getElementById("PreviewBox").style.display="none";
}
//显示/隐藏 预览图

//缩放图片
function ResizeImage(obj, MaxW, MaxH)
{
    if (obj != null) imageObject = obj;
    
    var state=imageObject.readyState;
    if(state==undefined)
        state=imageObject.complete;
    
	if(state!='complete' && state!=true) {
        setTimeout("ResizeImage(null,"+MaxW+","+MaxH+")",50);
		return;
    }
    var oldImage = new Image();
    oldImage.src = imageObject.src;
    var dW=oldImage.width; var dH=oldImage.height;
    if(dW>MaxW || dH>MaxH) {
        a=dW/MaxW; b=dH/MaxH;
        if(b>a) a=b;
        dW=dW/a; dH=dH/a;
    }
    if(dW > 0 && dH > 0) {
        imageObject.width=dW;
		imageObject.height=dH;
	}
}
//缩放图片

//添加商品对比
function doCompare()
{
    var productids = "";
    var obj = document.getElementsByName("productids");
    if(obj)
    {
        if(!obj.length)
        {
            if(obj.checked)
                productids = obj.value;
        }
        else
        {
            for(var i=0;i<obj.length;i++)
            {
                if(obj[i].checked)
                    productids += ","+obj[i].value;
            }
            if(productids!="")
            {
                productids = ","+productids;
                productids = productids.replace(",,","");
            }
        }
    }
    if(productids!="")
    {
        window.open(InstallDir+"ProductCompare.aspx?ids="+productids);
    }else{alert("请选择对比的商品！");}
}
//添加商品对比

///添加查询参数
function addParam(name,value)
{
    var nowurl = location.href;

    if(nowurl.indexOf("?"+name+"=")==-1 && nowurl.indexOf("&"+name+"=")==-1)
    {
        if(nowurl.indexOf("?")==-1)
        {
            if(nowurl.indexOf("#")>=0)
            {   
                var nowurl1 = nowurl.split("#")[0];
                var nowurl2 = nowurl.split("#")[1];
                nowurl = nowurl1 + "?"+name+"="+value + "#" + nowurl2;
            }    
            else
                nowurl = nowurl+"?"+name+"="+value;
        }
        else
        {
            if(nowurl.indexOf("#")>=0)
            {
                var nowurl1 = nowurl.split("#")[0];
                var nowurl2 = nowurl.split("#")[1];
                nowurl = nowurl1 + "&"+name+"="+value + "#" + nowurl2;
            }else
                nowurl = nowurl+"&"+name+"="+value;
        }
    }
    else
    {
        var array;
        var m_index = nowurl.indexOf("?"+name+"=");
        if(m_index>=0){
            array = nowurl.split("?"+name+"=");
            nowurl = nowurl.substring(0,m_index)+"?"+name+"="+value;
        }
        else{
            m_index = nowurl.indexOf("&"+name+"=");
            array = nowurl.split("&"+name+"=");
            nowurl = nowurl.substring(0,m_index)+"&"+name+"="+value;
        }
        var tempString = array[1];
        if(tempString.indexOf("&")!=-1)
        {
           nowurl+=tempString.substring(tempString.indexOf("&"),tempString.length);
        }
        if(tempString.indexOf("#")!=-1)
        {
           nowurl+=tempString.substring(tempString.indexOf("#"),tempString.length);
        }
    }
        self.location.href = nowurl
}
///添加查询参数


function CreateXMLHttpRequest() { 
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }catch (e) { } 
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }catch (e) { } 
    try { return new XMLHttpRequest(); }catch(e) {} 
    alert("Error：不支持 ajax ！");
    return null;
}
function CreateDOMDocument() { 
    try { return new ActiveXObject("Msxml2.DOMDocument"); } catch (e) {} 
    try { return new ActiveXObject("Microsoft.DOMDocument"); } catch (e) {} 
    alert("Error：不支持 ajax ！");
    return null;
}
