/*
 * Author (pyzy.net)Sta huzj
 * 2008-04-14
 * 
 * Update JamesXie(ezzz.5dblog.com)
 * add addKeyListen (up & down) 2008-04-23
 * add ajax support 2008-04-24
 */

//************通用函数（来自pyzy.net）Sta huzj 2008-4-12************//
//James游标
var numPoint=-1;
var thisObj;
var menuValue=[];
var myDivName="brandNewDiv";
var oldValues="";


function $self(fctId){
        return document.getElementById(fctId);
}

//取得某对象，若提供ID下的对象不存在则自动创建
function c$self(fctId,fctClassName){
        var varTempDivObj=$self(fctId);
        if(!varTempDivObj){
            GetPyzyIframe("ifm"+fctId);
            varTempDivObj=document.createElement("div");
            varTempDivObj.id=fctId;
            if(fctClassName && fctClassName!="")varTempDivObj.className=fctClassName;
            document.body.appendChild(varTempDivObj);
        }
        return varTempDivObj;
}

//取得某ID的iframe对象，若不存在该ID的对象则自动创建
function GetPyzyIframe(fctIfmId,fctVisibility,fctTop,fctLeft,fctWidth,fctHeight){
        var varTempIfmObj=$self(fctIfmId);
        if(!varTempIfmObj){
                varTempIfmObj=document.createElement("iframe");
                varTempIfmObj.id=fctIfmId;
                varTempIfmObj.style.position="absolute";
                varTempIfmObj.style.zIndex="1";
                varTempIfmObj.style.visibility="hidden";
                document.body.appendChild(varTempIfmObj);
        }
        if(fctTop)varTempIfmObj.style.top=fctTop+"px";
        if(fctLeft)varTempIfmObj.style.left=fctLeft+"px";
        if(fctWidth)varTempIfmObj.style.width=fctWidth+"px";
        if(fctHeight)varTempIfmObj.style.height=fctHeight+"px";
        if(fctVisibility)varTempIfmObj.style.visibility=(document.all?fctVisibility:"hidden");
        return varTempIfmObj;
}

//取得某对象的坐标位置、宽、高
function getPosition(obj){
        var top=0;
        var left=0;
        var width=obj.offsetWidth;
        var height=obj.offsetHeight;
        while(obj.offsetParent){
                top+=obj.offsetTop;
                left+=obj.offsetLeft;
                obj=obj.offsetParent;
        }
        return{"top":top,"left":left,"width":width,"height":height};
}

//取得编码存储框对象
function GetValueToInputObj(fctThisObj){
		
        if(!fctThisObj)return null;
        var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input")?fctThisObj.getAttributeNode("value_to_input").value:"");
        if(varThisObjAutoInput=="")return null;
        return $self(varThisObjAutoInput);
}

//取得编码存储框对象
function GetValueToInputObj1(fctThisObj){
		
        if(!fctThisObj)return null;
        var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input_1")?fctThisObj.getAttributeNode("value_to_input_1").value:"");
        if(varThisObjAutoInput=="")return null;
        return $self(varThisObjAutoInput);
}

//取得编码存储框对象
function GetValueToInputObj2(fctThisObj){
		
        if(!fctThisObj)return null;
        var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input_2")?fctThisObj.getAttributeNode("value_to_input_2").value:"");
        if(varThisObjAutoInput=="")return null;
        return $self(varThisObjAutoInput);
}

//取得编码存储框对象
function GetValueToInputObj3(fctThisObj){
		
        if(!fctThisObj)return null;
        var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input_3")?fctThisObj.getAttributeNode("value_to_input_3").value:"");
        if(varThisObjAutoInput=="")return null;
        return $self(varThisObjAutoInput);
}

//取得编码存储框对象
function GetValueToInputObj4(fctThisObj){
		
        if(!fctThisObj)return null;
        var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input_4")?fctThisObj.getAttributeNode("value_to_input_4").value:"");
        if(varThisObjAutoInput=="")return null;
        return $self(varThisObjAutoInput);
}

//自动触发下一个对象的Act事件
function AutoNextInputAct(fctThisObj,fctAct){
        var varNextInput=fctThisObj.getAttributeNode("nextinput");
        if(varNextInput && varNextInput!=""){
                if(document.all){
                        eval("$self('"+varNextInput.value+"')."+fctAct+"()");
                }else{
                        var evt = document.createEvent("MouseEvents");
                        evt.initEvent(fctAct,true,true);
                        $self(varNextInput.value).dispatchEvent(evt);
                }
                $self(varNextInput.value).focus();
        }
}

//给某对象的某事件增加处理函数AddFunToObj(document,"onclick","alert('1');")
function AddFunToObj(fctObj,fctAct,fctFunction){
        if(fctObj.addEventListener){ //!IE
                fctObj.addEventListener(fctAct.replace("on",""),function(e){
                        e.cancelBubble=!eval(fctFunction);
                },false);
        }else if(fctObj.attachEvent){ //IE
                fctObj.attachEvent(fctAct,function(){
                        return eval(fctFunction);
                });
        }
}

//************通用函数 End huzj 2008-4-12************//

/**********城市列表功能 Sta huzj 2008-4-14*************/
//输出城市列表菜单CSS样式 huzj 2008-4-12
document.write('\
<style type="text/css">\
#divAddressMenu {position:absolute;visibility:hidden;z-index:1;overflow:hidden;width:200px;height:268px;font-size:12px;font-family:arial;background:#fff;}\
#divAddressMenu #brandNewDiv {height:266px;padding:0 4px;border:1px solid #7F9DB9;overflow:hidden;}\
#divAddressMenu #brandNewDiv h4 {height:18px;margin:0;padding:4px 6px 0;font-size:12px;color:#666;font-weight:normal;border-bottom:1px dashed #ccc;}\
#divAddressMenu #brandNewDiv a {display:block;cursor:pointer;margin:0;padding:3px 6px 0;height:20px;color:#0055AA;text-decoration:none;background-color:none;}\
#divAddressMenu #brandNewDiv a:hover {overflow:hidden;height:18px;line-height:18px;border-top:1px solid #7F9DB9;border-bottom:1px solid #7F9DB9;background:#E7F1FD;}\
#divAddressMenu #brandNewDiv a span {overflow:hidden;display:block;float:left;width:80px;height:20px;line-height:22px;}\
#divAddressMenu #brandNewDiv .pagination {display:none;height:18px;margin:0;padding:4px 4px 0;text-align:center;border-top:1px dashed #ccc;}\
</style>\
');
var varAddress="";
var varPageId=0;
//生成并显示出城市下拉菜单
function GetCityList(fctThisObj,evt){
		//showDiv();
		//James
		thisObj=fctThisObj;
        var varMenuObj=c$self("divAddressMenu");
        var varThisObj=fctThisObj;
        if(varThisObj.id=="menuPageS"||varThisObj.id=="menuPageE"){
                varThisObj=varMenuObj.obj;
        }else{
                varPageId=0;
        }
        //清除已选城市Value
        var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
        //if(varThisObjAutoInput)varThisObjAutoInput.value="";
        
        var varThisObjAutoInput1=GetValueToInputObj1(varThisObj); //取得城市编码值存储对象
        //if(varThisObjAutoInput1)varThisObjAutoInput1.value="";
        
        var varThisObjAutoInput2=GetValueToInputObj2(varThisObj); //取得城市编码值存储对象
        //if(varThisObjAutoInput2)varThisObjAutoInput2.value="";
        
        var varThisObjAutoInput3=GetValueToInputObj3(varThisObj); //取得城市编码值存储对象
        //if(varThisObjAutoInput3)varThisObjAutoInput3.value="";
        
        var varThisObjAutoInput4=GetValueToInputObj4(varThisObj); //取得城市编码值存储对象
        //if(varThisObjAutoInput4)varThisObjAutoInput4.value="";

        //取得城市数据并拆解为数组
        var varObjValue=varThisObj.value;
        var varThisObjAdd=(varThisObj.getAttributeNode("mod_address_suggest")?varThisObj.getAttributeNode("mod_address_suggest").value:"");
        var varData=(varObjValue==""?(varThisObjAdd==""?varAddress:varThisObjAdd):varAddress);
        
        var varHtmlStr="",varCityDataSplit=varData.split("@"),varCityDataSplitI,varCityDataSplitIu,varNextPageStr="";
        
        //存储当前操作对象
        varMenuObj.obj=varThisObj;
        var varPageRCount=(varThisObj.getAttributeNode("pagecount")?parseInt(varThisObj.getAttributeNode("pagecount").value,10):11);
        var varThisPageI=0
        for(var i=1;i<varCityDataSplit.length-1;i++){
                varCityDataSplitI=varCityDataSplit[i];
                if(varCityDataSplitI.toUpperCase().indexOf(varObjValue.toUpperCase())>=0 || varObjValue=="" || i==varObjValue){ // || varCityDataSplitI.toLowerCase().indexOf(varObjValue.toLowerCase())>=0
                        varThisPageI+=1;
                        if(varThisPageI>varPageId*varPageRCount && varThisPageI<=(varPageId+1)*varPageRCount){
                                varCityDataSplitISplit=varCityDataSplitI.split("|");
                                
                                varHtmlStr+="<a href='javascript:;' onclick='WriteCity("+varThisPageI+")' id='menuA"+varThisPageI+"' title='"+varCityDataSplitI+"'>";
                        		varHtmlStr+="<span>"+varCityDataSplitISplit[1]+"</span>"+varCityDataSplitISplit[0]+"</a>";
                        }
                }
        }
        if(varThisPageI>varPageRCount){
                varNextPageStr="<div class='pagination'><font id=menuPageS style="+(varPageId>0?"cursor:pointer;color:#05a;":"color:#666;")+">上一页</font>&nbsp;&nbsp;"
                varNextPageStr+="<font id=menuPageE style="+(varThisPageI>(varPageId+1)*varPageRCount?"cursor:pointer;color:#05a":"color: #666;")+">下一页</font></div>";
        }
        var varThisObjPosition=getPosition(varThisObj); //取得事件发生处控件坐标
        with(varMenuObj){
                style.top=varThisObjPosition.top+varThisObjPosition.height+"px";
                style.left=varThisObjPosition.left+"px";
                style.visibility="visible";
                
                //输出内容
                var varHtmlCode = "<div id='"+myDivName+"'>";          
                varHtmlCode += "<h4>" +(varHtmlStr== ""?"对不起，找不到："+varObjValue+"。":"请输入拼音/中文或按↑↓键选择。")+"</h4>";
                varHtmlCode += varHtmlStr + varNextPageStr; //分页
                varHtmlCode += "</div>"               
                innerHTML=varHtmlCode;

        }
        GetPyzyIframe("ifm"+varMenuObj.id,"visible",(varThisObjPosition.top+varThisObjPosition.height),varThisObjPosition.left,varMenuObj.offsetWidth,varMenuObj.offsetHeight); //取Iframe
		
		//James增加按键监听
        addKeyListen(evt);

		//AddFunToObj(document.getElementById("divAddressMenu"),"onkeyup","addKeyListen(event)")

        return false;
}

//选择某城市
function WriteCity(fctI){
        var varMenuObj=c$self("divAddressMenu");
		//alert("选择某城市");
        var varThisObj=varMenuObj.obj;
        var varMenuValue=$self("menuA"+fctI).title;

		//alert(">>> "+varThisObj.name);

        varMenuValue=varMenuValue.split("|");
        //James 其他项目赋值用
        menuValue=varMenuValue;
        //James
		//不强制复制
		varThisObj.value=varMenuValue[1];
       
        var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
        if(!varThisObjAutoInput)varThisObjAutoInput=varThisObj;
        
        var varThisObjAutoInput1=GetValueToInputObj1(varThisObj); //取得城市编码值存储对象
        if(!varThisObjAutoInput1)varThisObjAutoInput1=varThisObj;
        
        var varThisObjAutoInput2=GetValueToInputObj2(varThisObj); //取得城市编码值存储对象
        if(!varThisObjAutoInput2)varThisObjAutoInput2=varThisObj;
        
        var varThisObjAutoInput3=GetValueToInputObj3(varThisObj); //取得城市编码值存储对象
        if(!varThisObjAutoInput3)varThisObjAutoInput3=varThisObj;
        
        var varThisObjAutoInput4=GetValueToInputObj4(varThisObj); //取得城市编码值存储对象
        if(!varThisObjAutoInput4)varThisObjAutoInput4=varThisObj;
		//James
        
		
		varThisObjAutoInput1.value=varMenuValue[0];
		varThisObjAutoInput2.value=varMenuValue[1];
		varThisObjAutoInput3.value=varMenuValue[2];
		varThisObjAutoInput4.value=varMenuValue[3];
		
		varThisObjAutoInput.value=varMenuValue[1];
		
        if(typeof(fctI)=="number")AutoNextInputAct(varThisObj,"click");
        //隐藏菜单
        //_Hidden("1");
		//清理游标
		numPoint=-1;

}

//隐藏城市列表
function _Hidden(e){
        e=e?e:event;
        var varMenuObj=c$self("divAddressMenu");
        var varThisObj=varMenuObj.obj;
        if(varMenuObj.style.visibility!="hidden"){
                if(e){
                        var EventOBJ=(e.srcElement?e.srcElement:e.target);
                        if(EventOBJ.id=="menuPageS" && EventOBJ.style.color=="#05a"){ //如果点的是“上一页”则向上翻页
                                varPageId=varPageId-1;
                                GetCityList(EventOBJ);
                        }
                        if(EventOBJ.id=="menuPageE" && EventOBJ.style.color=="#05a"){ //如果点的是“下一页”则向下翻页
                                varPageId=varPageId+1;
                                GetCityList(EventOBJ);
                        }
                        if(varThisObj==EventOBJ || EventOBJ.id.indexOf("menuPage")==0 || EventOBJ.id.indexOf("divAddressMenu")==0) return false;
                }
               
                var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
                if($self("menuA1")){ //如果下拉菜单中存在第一个城市
                        if(!varThisObjAutoInput)varThisObjAutoInput=varThisObj;
                        if(varThisObjAutoInput.value=="" || varThisObjAutoInput==varThisObj){ //编码框中无值
                                //james 
								//非输入框出按下按钮时在无需默认选中第一项1
								//WriteCity("1");
                        }
                }else if(EventOBJ.id.indexOf("menuA")<0){ //如果下拉菜单中不存在满足条件的城市
						//james 
						//当没有搜索到结果时不强制删除内容
                        //if(varThisObj)varThisObj.value="";
                }
                varMenuObj.style.visibility="hidden";
                GetPyzyIframe("ifm"+varMenuObj.id,"hidden");
        }
		numPoint=-1;
}

/**********城市列表功能 End huzj 2008-4-14*************/

/**********城市列表功能 Begin JamesXie 2008-4-25*************/
//用Enter选中城市
function _Enter(_e,obj1){
    myevt=_e?_e:event;
	var pKeyCode = myevt.keyCode ? myevt.keyCode : myevt.which;
	if (pKeyCode==13){
		if (numPoint!=-1){
			//alert('检测到了'+numPoint);
			//alert(varCityDataSplitI)
			//document.getElementById('aa').click();

			//varCityDataSplit=varData.split("@")
			//AddFunToObj(document,"onclick","_Hidden("+(document.all?"":"e")+");");
		}
	}
}


//James
//模拟上下按钮指针
function addKeyListen(_e){
	_e=_e?_e:event;
	//alert("_e.keyCode " +_e.keyCode);
	//alert("_e.which " +_e.which);
	//alert(_e.keyCode);
	//alert(_e.which);
	var e = _e.keyCode ? _e.keyCode : _e.which;
	if((e==38 || e==40 || e==13)){
		//alert("Key "+e);
		selectATag('divAddressMenu','a',e);
	}else{
		numPoint=-1;
	}
}


//James
//遍历某html元素中的符合某条件的html元素
//pWhere = type || className and so on
function selectATag(pid,pTarget,pKeyCode){

	var fmId=document.getElementById(pid);
	var fmId2=fmId.getElementsByTagName(pTarget);
	//alert("未选中!");
	if (fmId2.length>0){
		//alert("选中!");
		for(i=0;i<fmId2.length;i++){
			onCancel(fmId2[i]);
		}
	
		//向上
		if (pKeyCode==38){
			if (numPoint<1){
				numPoint=fmId2.length-1;
			}else{
				numPoint--;
			}
		}
		//向下
		if (pKeyCode==40){
			if (numPoint>=fmId2.length-1){
				numPoint=0;
			}else{
				numPoint++;
			}
		}


		//回车
		if (pKeyCode==13 && numPoint!=-1){
			var strFTitle = fmId2[numPoint].title;
			var strFName=strFTitle.split("|")[1].toString();
			var strFCode=strFTitle.split("|")[2].toString();
			
			menuValue=strFTitle.split("|");
			
			
			var varThisObjAutoInput=GetValueToInputObj(thisObj); //取得城市编码值存储对象
	        if(!varThisObjAutoInput)varThisObjAutoInput=thisObj;
	        
	        var varThisObjAutoInput1=GetValueToInputObj1(thisObj); //取得城市编码值存储对象
	        if(!varThisObjAutoInput1)varThisObjAutoInput1=thisObj;
	        
	        var varThisObjAutoInput2=GetValueToInputObj2(thisObj); //取得城市编码值存储对象
	        if(!varThisObjAutoInput2)varThisObjAutoInput2=thisObj;
	        
	        var varThisObjAutoInput3=GetValueToInputObj3(thisObj); //取得城市编码值存储对象
	        if(!varThisObjAutoInput3)varThisObjAutoInput3=thisObj;
	        
	        var varThisObjAutoInput4=GetValueToInputObj4(thisObj); //取得城市编码值存储对象
	        if(!varThisObjAutoInput4)varThisObjAutoInput4=thisObj;
			//James
	        
			
			varThisObjAutoInput1.value=menuValue[0];
			varThisObjAutoInput2.value=menuValue[1];
			varThisObjAutoInput3.value=menuValue[2];
			//alert("menuValue "+menuValue[3]);
			varThisObjAutoInput4.value=menuValue[3];

			//赋值
			document.getElementById(thisObj.name).value=strFName;
			thisObj.parentNode.click();
		}
	

		if (numPoint!=-1)
		{
			onChoose(fmId2[numPoint],pKeyCode);
		}

	}
	

	//return false;
}

//James
//选择
function onChoose(obj){
	obj.style.backgroundColor="#C8E3FC";
}

//James
//取消选择
function onCancel(obj){
	obj.style.backgroundColor="#FFFFFF";
}

//James
//删除DIV
function hiddenDiv(){
	var fDiv=document.getElementById(myDivName);
	fDiv.style.visibility="hidden";
	fDiv.parentNode.style.visibility="hidden";

}


//James
//删除DIV
function showDiv(){
	var fDiv=document.getElementById(myDivName);
	fDiv.style.visibility="visible";
	fDiv.parentNode.style.visibility="visible";
}


//James 中文传参转码
function enCode(chineseStr) {
	return escape(chineseStr,'utf-8');
}


//James Ajax code
var NumkeyCode = 0;
var myValue="";

function xh(){
	if (window.XMLHttpRequest){ 
		//Mozilla 浏览器
		return new XMLHttpRequest();
	}else if (window.ActiveXObject){
		var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++){
			try{
				return new ActiveXObject(msxmls[i]);
			}catch (e){
			}
		}
	}
	return null;
}

var xmlHttp = xh();

/**
 * callAjaxMain
 * fctThisObj 目标
 * evt 事件
 * url_param1 参数1
 * url_param2 加密参数2
 */
function callAjaxMain(fctThisObj,evt,url_param1,url_param2) {
	
	if (fctThisObj.value==""||oldValues==fctThisObj.value){
		//alert("不使用");
		//不使用Ajax
		GetCityList(fctThisObj,evt);
	}else{
		oldValues=fctThisObj.value;
		//alert("使用"+oldValues+" "+fctThisObj.value);
		//使用Ajax
		var targetURL = url_param1+enCode(fctThisObj.value);
		xmlHttp.open("GET", targetURL, true);
		xmlHttp.onreadystatechange = function(){callAjaxOk(fctThisObj,evt)};
		xmlHttp.send(null);
	}
}


function callAjaxOk(fctThisObj,evt){
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status==200){
			var response = xmlHttp.responseText;
			varAddress=response;
			GetCityList(fctThisObj,evt);
		}else{
		}
	}
}

/**********城市列表功能 End JamesXie 2008-4-25*************/