﻿var GetNodeValue = function(obj,IsAttrub)
{
    var str = "";
    if(window.ActiveXObject)    //判断是否IE浏览?
    {
        str = IsAttrub!=""?obj.getAttribute(IsAttrub):obj.text;
    }
    else //判断是否firefox浏览?
    {
        try
        {
            str = IsAttrub!=""?obj.getAttribute(IsAttrub):obj.childNodes[0].nodeValue;
        }
        catch(ex)
        {
           str = "";
        }
    }
    return str;
}

if(document.implementation && document.implementation.createDocument)
{
    XMLDocument.prototype.loadXML = function(xmlString)
{
var childNodes = this.childNodes;
for (var i = childNodes.length - 1; i >= 0; i--)
   this.removeChild(childNodes[i]);

var dp = new DOMParser();
var newDOM = dp.parseFromString(xmlString, "text/xml");
var newElt = this.importNode(newDOM.documentElement, true);
this.appendChild(newElt);
};

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }
    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
    aResult[i] = aItems.snapshotItem(i);
    }
    return aResult;
    }

    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
    if(this.ownerDocument.selectNodes)
    {
    return this.ownerDocument.selectNodes(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }
}

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
    // prototying the XMLDocument
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }
    var xItems = this.selectNodes(cXPathString, xNode);
    if( xItems.length > 0 )
    {
    return xItems[0];
    }
    else
    {
    return null;
    }
    }
   
    // prototying the Element
    Element.prototype.selectSingleNode = function(cXPathString)
    {   
    if(this.ownerDocument.selectSingleNode)
    {
    return this.ownerDocument.selectSingleNode(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }
}
}

function loadXML()
{
    var xmlHttp;
    var name;
    var strDiv = "";
    if(window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
    try
    {
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
                if (xmlHttp.status == 200)
                {
                    // 取得XML的DOM对象
                    var xmlDOM = xmlHttp.responseXML;
                    // 取得XML文档的根
                    var root = xmlDOM.documentElement;
                    try
                    {
                       //var msg=document.getElementById(id);
                       var items = root.selectNodes(xPathArr[path]);
                       for(var i=0;i<items.length;i++){
                        var strTitle = GetNodeValue(items[i],"");
                        var strTitleArr1=GetNodeValue(items[i],"cid");
                        strDiv += (i+1)%12==0 ? "<a id=\"city1\" href='javascript:setDivStr("+strTitleArr1+",1);' class=\"link_city\" >"+strTitle+"<\/a><br>" :"<a id=\"city1\" href='javascript:setDivStr("+strTitleArr1+",1);' class=\"link_city\" >"+strTitle+"<\/a>";
                       }
                       var str_title="";
                       switch(pactiveID){
                            case 0:
                                str_title="请选择国内毕业/就读学校";
                                break;
                            case 1:
                                str_title="请选择国内常住地";
                                break;
                            case 2:
                                if($("StudyJapan").style.display=="block"){
                                    str_title="请选择正在日本就读的院校";
                                }else{
                                    str_title="请选择准备留学的目标院校 ";
                                }
                                break;
                            case 3:
                                str_title="请选择准备升学的目标院校";
                                break;
                            case 4:
                                if($("StudyJapan").style.display=="block"){
                                    str_title="请选择留日常住地";
                                }else{
                                    str_title="请选择预计赴日后常住地";
                                }
                                break;
                       }
                       var str_top="";
                       str_top +="<div style='width:100%; text-align:left;height:25px; background-color:#66CCFF; color:#FFFFFF; '>";
                       str_top +="<div style='float:right; padding-right:5px;'><a href=\"#2\" onclick='closePost(\"post_box_cuvid\");'><img src=\"images/home/btn_close4.gif\" border=\"0\" title=\"关闭修改\" /></a></div>";
                       str_top +="<div style='padding-left:10px;font-size:14px; font-weight:bold; height:21px; margin-top:4px;'>" + str_title + "</div></div>"
                       document.getElementById(upDivID).innerHTML = str_top + "<div style=\"padding-left:10px; padding-top:0px; line-height:18px;\">"+ strDiv+"<\/div>";
                    }
                    catch(exception)
                    {
                        alert("error");
                    }
                }
            }
        }
        xmlHttp.open("GET", "common/FriendInfo.xml", true);
        xmlHttp.send(null);
    }
    catch(exception)
    {
      alert("您要访问的资源不存在!");
    }
    
}

function setDivStr(cid,pageindex)
{
    var xmlHttp;
    var name;
    var page=0;
    var strDiv = "";
    if(window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
    try
    {
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
                if (xmlHttp.status == 200)
                {
                    // 取得XML的DOM对象
                    var xmlDOM = xmlHttp.responseXML;
                    // 取得XML文档的根
                    var root = xmlDOM.documentElement;
                    try
                    {
                        var items = root.selectNodes(xPathArr[path2]);
                        var str = "";
                        var count=0;
                        var totalCount = 0;
                        for(var i=0;i<items.length;i++){
                            var strTitle = GetNodeValue(items[i],"");
                            var strTitleArr1=GetNodeValue(items[i],"pid");
                            if(strTitleArr1==cid){totalCount++;page++;}
                            if(strTitleArr1==cid && page<=(pageindex*40) && page>=(pageindex-1)*40){
                                var strTitleArr2=GetNodeValue(items[i],"cid");
                                str +="<td width=\"50%\"><div style=\"padding-right:10px; padding-top:2px; padding-bottom:2px;\"><img src=\"images/home/dian.jpg\" /><a href='javascript:UpdateCUV("+strTitleArr2+",\""+strTitle+"\");' class=\"link_city_uv\" onmousemove='this.style.color=\"#FFFFFF\"' onmouseout='this.style.color=\"#006699\"'>"+strTitle+"<\/div><\/td>";
                                count = count+1;
                                if(count==2){strDiv +="<tr>"+str+"<\/tr>";str="";count=0;}
                            }
                            if(i==items.length-1 && count==1 && totalCount!=1){str +="<td></td>";strDiv +="<tr>"+str+"</tr>";}
                       }
                       if(totalCount==1){str +="<td></td>";strDiv +="<tr>"+str+"</tr>";}
                       var pageCount = totalCount%40==0 ? totalCount/40 : totalCount/40 +1;
                        var pageStr="";
                        for(var j=1;j<=pageCount;j++){
                            if(j!=pageindex)
                                pageStr +="<a style=\"padding-left:5px; padding-right:5px;\" href='javascript:setDivStr("+cid+","+j+");'>"+j+"</a>";
                            else
                                pageStr +="<a style=\"padding-left:5px; padding-right:5px;\">"+j+"</a>";
                        }
                       pageStr="<div style=\"margin-bottom:10px;text-align:center;\">"+pageStr+"</div>";
                       document.getElementById(lowDivID).innerHTML = "<table id=\"CUVList\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse; width:400px;\">"+ strDiv+"<\/table>"+pageStr; 
                    }
                    catch(exception)
                    {
                        alert("error");
                    }
                }
            }
        }
        xmlHttp.open("GET", "common/FriendInfo.xml", true);
        xmlHttp.send(null);
    }
    catch(exception)
    {
      alert("您要访问的资源不存在!");
    }
    
}

var xPathArr=["//root//chinaccity/ccity","//root//chinapcity/pcity","//root//school/jschool","//root//chinashool/cschool","//root//Location/uvLocation","//root//jpcity/jcity","//root//jplocation/jlocation"];
var activeArr=["cuvid","cityID2","uvid","suvid","uvLocationID"];
var path=0,path2=0,luid=0,pactiveID=0;
var id = "",oDivID="post_box_cuvid",upDivID="cuvidCcity",lowDivID="cuvidPcity";
function openDiv(o,xPath,xPath2,tdcuvid,cid,uid,activeID,dwidth,dheight){
    clostPostMaster();
    clostPostHome();
    $(upDivID).innerHTML = "";
    $(lowDivID).innerHTML = "";
    OpenLeftTop(oDivID,dwidth,dheight);
    FloatShow();
    div_id=oDivID;
    width=dwidth;
    height=dheight;
    path=xPath;path2=xPath2;id=tdcuvid;luid=uid;pactiveID=activeID;
    loadXML();
    setDivStr(cid,1);
    $(upDivID).style.display = "block";
    $(lowDivID).style.display = "block";
}

function UpdateCUV(cuvid,str){
    document.getElementById(id).innerHTML = str;
    var url="/ajax_home.aspx?active="+activeArr[pactiveID]+"&uid="+luid+"&cuvid=" + cuvid;
    document.getElementById(oDivID).style.display = "none";
    getData(url);
    FloatClose();
}