
var zipcodeClass=new Object();

zipcodeClass=function(){
	this.zipcodeArea=ID("zipcode");
	this.searchValue=CLASS(this.zipcodeArea, "searchValue")[0];
	this.buttonSearch=CLASS(this.zipcodeArea, "buttonSearch")[0];
	this.list=CLASS(this.zipcodeArea, "list")[0];
	this.listArea=TAG(this.list, "table")[0];
	this.exist;
	this.existContent;
	this.existContentCount;
	this.nothing=CLASS(this.list, "nothing")[0];
	this.listFrame=CLASS(this.list, "listFrame")[0];
	this.zipcodeTargetInput;
	this.addressTargetInput;

	this.searchValueKeyupFunc=bindAsListener(this.searchValueKeyup, this);
	this.zipcodeLoadFunc=bindAsListener(this.zipcodeLoad, this);
	this.zipcodeLoadResultFunc=bindAsListener(this.zipcodeLoadResult, this);

	addListener(this.searchValue, "keyup", this.searchValueKeyupFunc);
	addListener(this.buttonSearch, "click", this.zipcodeLoadFunc);
}

zipcodeClass.prototype={
	searchValueKeyup:function(e){
		var event=window.event || e;

		if(event.keyCode=="13"){
			this.zipcodeLoadFunc();
		}
	},

	zipcodeLoad:function(){
		var params="searchValue="+encodeURIComponent(this.searchValue.value);
		new ajax.Request("/zipcode/load.php", params, this.zipcodeLoadResultFunc, "POST");
		return false;
	},

	zipcodeLoadResult:function(req){
		if(req.readyState==4){
			if(req.status==200){
				this.exist=CLASS(this.list, "exist")[0];
				if(this.exist) this.exist.parentNode.removeChild(this.exist);
				
				var result=req.responseXML;
				result=TAG(result, "result")[0];

				var whether=TAG(result, "whether")[0].firstChild.nodeValue;
				whether=eval(whether);

				if(whether){
					var zipcodeInfo=TAG(result, "zipcodeInfo");
					var zipcodeInfoCount=zipcodeInfo.length;
					if(zipcodeInfoCount>0){
						this.zipcodeWrite(zipcodeInfo);
						this.nothing.style.display="none";
					}else{
						this.nothing.style.display="block";
					}
				}else{
					var error=TAG(result, "error")[0].firstChild.nodeValue;
					var message=TAG(result, "message")[0].firstChild.nodeValue;
					
					if(error=="searchValue"){
						alert(message);
					}

					this.searchValue.focus();
				}
			}else{
				var error=req.status;
				switch(error){
					case 403 :	var msg="접근이 거부되었습니다.";		break;
					case 404 :	var msg="페이지가 없습니다.";			break;
					case 404 :	var msg="서버 오류가 발생하였습니다.";	break;
				}
				alert(msg);
			}
		}
	},

	zipcodeWrite:function(zipcodeInfo){
		var zipcodeInfoCount=zipcodeInfo.length;
		var exist=document.createElement("tbody");
		exist.className="exist";

		for(var i=0; i<zipcodeInfoCount; i++){
			var zipcodeInfoArray=eval(zipcodeInfo[i].firstChild.nodeValue);
			var zipcodeText=document.createTextNode(zipcodeInfoArray["zipcode"]);
			var addressText=document.createTextNode(zipcodeInfoArray["address"]);
			var bunjiText=document.createTextNode(" "+zipcodeInfoArray["bunji"]);

			var listFrameContent=TAG(this.listFrame, "tr")[0].cloneNode(true);
			listFrameContent.overColor="#D3E8ED";
			listFrameContent.outColor="none";
			var listOver=bindAsListener(this.listOver, listFrameContent);
			addListener(listFrameContent, "mouseover", listOver);
			var listOut=bindAsListener(this.listOut, listFrameContent);
			addListener(listFrameContent, "mouseout", listOut);

			var	listFrameContentTd=TAG(listFrameContent, "td");
			var a=document.createElement("a");
			a.href="#";
			a._target=this;
			a.zipcode=zipcodeInfoArray["zipcode"];
			a.address=zipcodeInfoArray["address"];
			var zipcodeInputFunc=bindAsListener(this.zipcodeInput, a);
			addListener(a, "click", zipcodeInputFunc);

			a.appendChild(addressText);
			a.appendChild(bunjiText);
			listFrameContentTd[0].appendChild(zipcodeText);
			listFrameContentTd[1].appendChild(a);
			exist.appendChild(listFrameContent);
		}
		this.listArea.appendChild(exist);
	},

	listOver:function(){
		this.style.background=this.overColor;
	},

	listOut:function(){
		this.style.background=this.outColor;
	},

	zipcodeInput:function(){
		this._target.zipcodeTargetInput.value=this.zipcode;
		this._target.addressTargetInput.value=this.address;
		this._target.zipcodeDefault();
		zipcodeCover.off();
		return false;
	},

	zipcodeDefault:function(){
		this.searchValue.value="";
		this.exist=CLASS(this.list, "exist")[0];
		if(this.exist) this.exist.parentNode.removeChild(this.exist);
		this.nothing.style.display="none";
	}
}


var zipcodeSystem;
var zipcodeCover;

function indexLoad(){
	zipcodeSystem=new zipcodeClass();
	zipcodeCover=new cover(ID("zipcode"));
}

addListener(window, "load", indexLoad);