var objAjax=new JAjax();
function createObject(){
	return function(){
		this.loading.apply(this,arguments);	
	}
}
var userAdderessObject=null;
function loadUserShopAddress(){
	if(arguments.length==1){
		userAdderessObject=new createUserAddressObject(1);
	}else{
		userAdderessObject=new createUserAddressObject();	
	}
}

var createUserAddressObject=new createObject();
createUserAddressObject.prototype.loading=function(){
	this.loadDate();
	if(arguments.length==1){
		this.showType=arguments[0];
	}
}

//加载数据信息
createUserAddressObject.prototype.loadDate=function(){
	var out=objAjax.getText("post","../../wxProgram/server/wx_shop.aspx?type=readShopAddress",null,null);
	this.addressArray=new Array();
	if(out=="no"||out.indexOf(c2)>-1){
		if(out.indexOf(c2)>-1){
			out=out.split(c3);
			for(var i=0;i<out.length;i++){
				var tSplit=out[i].split(c2);
				var s=new Array();
				for(var j=0;j<tSplit.length;j++){
					s.push(tSplit[j].split(c1));	
				}
				this.addressArray.push(s);
			}
		}
	}else{alert(out);}
}

//删除地址信息
createUserAddressObject.prototype.delAddress=function(obj){
	if(confirm("你确认要删除该送货地址信息吗?")){
		var objResponse=new JHttpResponse();
		objResponse.write("objid",obj.id);
		var out=objAjax.getText("post","../../wxProgram/server/wx_shop.aspx?type=deShopAddress",objResponse,null);
		if(out==obj.id){
			obj.parentElement.removeNode(true);
		}else{
			alert(out);	
		}
	}
}

//输出送货地址
createUserAddressObject.prototype.writeToAddress=function(obj){
	this.saveObject=obj;
	this.saveObject.innerHTML="";
	this.loadDate();
	var cDiv=document.createElement("div");
	if(this.addressArray.length>0){
		for(var i=0;i<this.addressArray.length;i++){
			var d=document.createElement("div");
			var value=returnValue(this.addressArray[i],"id","0");
			var name=returnValue(this.addressArray[i],"name","&nbsp;");
			var address=returnValue(this.addressArray[i],"address","&nbsp;");
			d.style.cssText='border-bottom:solid 1px #fffbf4;width:100%;';
			if(i==0){
				d.innerHTML="<span style='float:left;text-align:left;'><input type='radio' onclick='userAdderessObject.clickWriteAddress(this)' checked='checked' name='GetAddressMsgFormRadio' id='addressRadio_"+(i+1)+"' value='"+value+"''><label for='addressRadio_"+(i+1)+"'>"+name+"<span style='color:#ff0000;padding-left:10px;'>"+address+"</span></label></span><span class='delCss' title='删除' id='"+value+"' onclick='userAdderessObject.delAddress(this)'>删除</span>";
			}else{
				d.innerHTML="<span style='float:left;text-align:left;'><input type='radio' onclick='userAdderessObject.clickWriteAddress(this)' name='GetAddressMsgFormRadio' id='addressRadio_"+(i+1)+"' value='"+value+"''><label for='addressRadio_"+(i+1)+"'>"+name+"<span style='color:#ff0000;padding-left:10px;'>"+address+"</span></label></span><span class='delCss' title='删除' id='"+value+"' onclick='userAdderessObject.delAddress(this)'>删除</span>";
			}
			cDiv.appendChild(d);
		}
		d=document.createElement("div");
		if(this.addressArray.length==0){
			if(!this.showType){
				d.innerHTML="<input type='radio' onclick='userAdderessObject.clickWriteAddress(this)' name='GetAddressMsgFormRadio' checked='checked' id='addressRadio_0' value='0''><label for='addressRadio_0'>使用其他地址</label>";		
				cDiv.appendChild(d);
			}
			this.saveObject.appendChild(cDiv);
			this.writeToAddressMsg()
		}else{
			if(!this.showType){
				d.innerHTML="<input type='radio' onclick='userAdderessObject.clickWriteAddress(this)' name='GetAddressMsgFormRadio' id='addressRadio_0' value='0''><label for='addressRadio_0'>使用其他地址</label>";
				cDiv.appendChild(d);
			}
			this.saveObject.appendChild(cDiv);
			this.writeToAddressMsg(this.addressArray[0])
		}	
	}else{
		this.saveObject="无数据";
	}
}

createUserAddressObject.prototype.clickWriteAddress=function(obj){
	var id=obj.value;
	if(id=="0"){
		this.writeToAddressMsg();
	}else{
		for(var i=0;i<this.addressArray.length;i++){
			if(returnValue(this.addressArray[i],"id","0")==id){
				this.writeToAddressMsg(this.addressArray[i]);
				break
			}
		}
	}
}

createUserAddressObject.prototype.writeToAddressMsg=function(){
	var sA=new Array()
	if(arguments.length>0){
		sA=arguments[0];
	}
	var cTab=document.createElement("table");
	cTab.setAttribute("border","0");
	cTab.setAttribute("cellSpacing","0");
	cTab.setAttribute("cellPadding","0");
	cTab.style.cssText="width:100%";
	var tr=cTab.insertRow(-1);
	var td=tr.insertCell(-1);
	td.innerHTML="收货地址:";
	td.className="leftTdCss";
	td=tr.insertCell(-1);
	td.innerHTML="<input type='text' isV=1 notNull=1 min=5 max=200 title='收货地址' value='"+returnValue(sA,"address","&nbsp;")+"' id='address' name='address' style='width:100%;border:solid 1px #6596b8;'>";
	td=tr.insertCell(-1);
	td.className="rightTdCss";
	td.innerHTML="必填";	
	
	tr=cTab.insertRow(-1);
	td=tr.insertCell(-1);
	td.innerHTML="收货人姓名:";
	td.className="leftTdCss";
	td=tr.insertCell(-1);	
	td.innerHTML="<input title='收货人姓名' type='text' isV=1 notNull=1 min=2 max=10 value='"+returnValue(sA,"name","&nbsp;")+"' id='name' name='name' style='width:100%;border:solid 1px #6596b8;'>";	
	td=tr.insertCell(-1);
	td.className="rightTdCss";
	td.innerHTML="必填";	
	
	tr=cTab.insertRow(-1);
	td=tr.insertCell(-1);
	td.innerHTML="邮编编码:";
	td.className="leftTdCss";
	td=tr.insertCell(-1);	
	td.innerHTML="<input type='text' isV=1 notNull=1 min=6 max=7  title='邮编编码' value='"+returnValue(sA,"code","&nbsp;")+"' id='code' name='code' style='width:100%;border:solid 1px #6596b8;'>";	
	td=tr.insertCell(-1);
	td.className="rightTdCss";
	td.innerHTML="必填";	
	
	tr=cTab.insertRow(-1);
	td=tr.insertCell(-1);
	td.innerHTML="手机号码:";
	td.className="leftTdCss";
	td=tr.insertCell(-1);
	td.innerHTML="<input type='text' isV=1 notNull=1 title='手机号码' isMobile=1 value='"+returnValue(sA,"Phone","&nbsp;")+"' id='Phone' name='Phone' style='width:100%;border:solid 1px #6596b8;'>";
	td=tr.insertCell(-1);
	td.className="rightTdCss";
	td.innerHTML="必填";	
	
	tr=cTab.insertRow(-1);
	td=tr.insertCell(-1);
	td.innerHTML="固话号码:";
	td.className="leftTdCss";
	td=tr.insertCell(-1);	
	td.innerHTML="<input type='text' isV=1 notNull=1 title='固话号码' min=6 max=20 id='TEL' value='"+returnValue(sA,"tel","&nbsp;")+"' name='TEL' style='width:100%;border:solid 1px #6596b8;'>";
	td=tr.insertCell(-1);
	td.className="rightTdCss";
	td.innerHTML="必填";	
	if(this.saveObject.childNodes.length==2){
		this.saveObject.childNodes[1].removeNode(true);	
	}
	
	if(this.showType){
		tr=cTab.insertRow(-1);
		td=tr.insertCell(-1);
		td.innerHTML="&nbsp;";
		td.className="leftTdCss";
		var ipt=document.createElement("input");
		ipt.setAttribute("type","button");
		ipt.setAttribute("value","保存数据");
		ipt.style.cssText="width:60px;height:20px;border:solid 1px #9E701B;text-align:center;background-color:#d0b88c;font-size:12px;margin:3px;";
		ipt.onclick=function(){
			var v=new validate();
			v.form=GetAddressMsgForm;
			if(v.start()){
				var objResponse=new JHttpResponse();
				//---------提交当前的送货地址编号
				var GetAddressMsgFormRadio=document.getElementsByName("GetAddressMsgFormRadio");
				for(var i=0;i<GetAddressMsgFormRadio.length;i++){
					if(GetAddressMsgFormRadio[i].checked){
						objResponse.write("GetAddressMsgFormRadio",GetAddressMsgFormRadio[i].value);
						break;
					}
				}
				//--------提交当前选中送货地址的详细
				for(var i=0;i<GetAddressMsgForm.elements.length;i++){
					if(GetAddressMsgForm.elements[i].type.toLowerCase()=="text"){
						objResponse.write(GetAddressMsgForm.elements[i].id,GetAddressMsgForm.elements[i].value);
					}
				}
				var out=objAjax.getText("post","../../wxProgram/server/wx_shop.aspx?type=upaddress",objResponse,null);
				switch(out){
					case "1":{
						alert("地址更新成功");
						userAdderessObject.writeToAddress(userAdderessObject.saveObject,1);	
					};break;
					case "0":alert("没有找到数据");break;
					default:alert(out);break;
				}
			}
		}
		td=tr.insertCell(-1);	
		td.setAttribute("colSpan","2");
		td.appendChild(ipt);
	}
	var cForm=document.createElement("form");
	cForm.setAttribute("id","GetAddressMsgForm");
	cForm.appendChild(cTab);
	this.saveObject.appendChild(cForm);
}

//获取二位数组中的对应的键值
//                     数组    键   默认值
function returnValue(arrayLst,key,mValue){
	var value="";
	try{
		if(mValue){
			value=mValue;	
		}
		if(arrayLst.length>0){
			for(var i=0;i<arrayLst.length;i++){
				if(arrayLst[i][0].toLowerCase()==key.toLowerCase()){
					value=arrayLst[i][1];
					break;
				}
			}
		}
	}catch(e){}
	return value;
}
