
var counselClass=new Object();

counselClass=function(){
	this.formArea=CLASS(ID("quickCounsel"), "form")[0];
	this.inputName=CLASS(this.formArea, "inputName")[0];
	this.inputTel=CLASS(this.formArea, "inputTel")[0];
	this.inputContent=CLASS(this.formArea, "inputContent")[0];
	this.request=CLASS(this.formArea, "request")[0];
	
	this.formSubmitFunction=bindAsListener(this.formSubmit, this);
	this.formSubmitResultFunction=bindAsListener(this.formSubmitResult, this);

	addListener(this.formArea, "submit", this.formSubmitFunction);
	addListener(this.request, "click", this.formSubmitFunction);
}

counselClass.prototype={
	formSubmit:function(){
		var params="indexkey="+encodeURIComponent(this.indexkey)
				+"&name="+encodeURIComponent(this.inputName.value)
				+"&tel="+encodeURIComponent(this.inputTel.value)
				+"&content="+encodeURIComponent(this.inputContent.value);

		new ajax.Request("/include/counselAdd.php", params, this.formSubmitResultFunction, "POST");
		return false;
	},

	formSubmitResult:function(req){
		if(req.readyState==4){
			if(req.status==200){
				var result=req.responseXML;
				result=TAG(result, "result")[0];

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

				if(whether){
					alert("상담신청이 완료되었습니다.");
					this.formArea.reset();
				}else{
					var error=TAG(result, "error")[0].firstChild.nodeValue;
					var message=TAG(result, "message")[0].firstChild.nodeValue;
					alert(message);

					if(error=="name"){
						this.inputName.focus();
					}else if(error=="tel"){
						this.inputTel.focus();
					}else if(error=="content"){
						this.inputContent.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);
			}
		}
	}
}


function contentsLoad(){
	new counselClass();
}

addListener(window, "load", contentsLoad);