﻿$psmagazine_form = {
	form : null,
	validator : null,
	button : null,
	buttonDisable : null,
	buttonLoader : null,
	buttonCancel : null,
	init : function(){
		var _this = this;
		this.setValue(0);
		$(".other-customer").hide();
		$(".customer").click(function(){
			$("#xml_customer").val(($(this).val()==0)?"ลูกค้าโครงการ":"บุคคลทั่วไป");
			$(".ps-customer")[($(this).val()==0)?"show":"hide"]();
			$(".other-customer")[($(this).val()==1)?"show":"hide"]();
			_this.setValue($(this).val());
		});
		
		if(!this.button)
			this.button = $(".btnSubmit");
		this.button.click(function(){
			_this.submit();
		});
		if(!this.form){
			this.form = $("#psmagazine-form");
			if(!this.validator){
				var msg="*";
				this.validator=this.form.validate({
					rules : {
						xml_firstname : {
							required: true
						},
						xml_lastname : {
							required: true
						},
						xml_email : {
							required: true,
							email : true
						},
						xml_project : {
							required: true
						},
						xml_plan : {
							required: true
						},
						xml_address : {
							required: true
						},
						xml_road : {
							required: true
						},
						xml_soi : {
							required: true
						},
						xml_district : {
							required: true
						},
						xml_amphur : {
							required: true
						},
						xml_province : {
							required: true
						},
						xml_zipcode : {
							required: true
						},
						xml_telephone : {
							required: true
						} 
					},
					messages: {
						xml_firstname:msg,
						xml_lastname:msg,
						xml_email:msg,
						xml_project:msg,
						xml_plan:msg,
						xml_address:msg,
						xml_road:msg,
						xml_soi:msg,
						xml_district:msg,
						xml_amphur:msg,
						xml_province:msg,
						xml_zipcode:msg,
						xml_telephone:msg
					},
					event : 'keyup'
				});
			}
	   }
	},
	initButtonElement:function(){
		if(!this.button) this.button = $(".btnSubmit");
		if(!this.buttonDisable) this.buttonDisable=$('.btnSubmit-Disable');
		if(!this.buttonLoader) this.buttonLoader=$('.button-loader');
		if(!this.buttonCancel) this.buttonCancel=$('.btnCancel');
		this.buttonCancel.click(function(){
			if(parent && parent.hs) parent.hs.close();
		});
	},
	submit:function(){
		var _this=this;
		if(!this.form) this.form = $("#appointment-form");
		var form=this.form;
		var successMsg = $('input:hidden[name=success-message]', form).val();
		var failureMsg = $('input:hidden[name=failure-message]', form).val();
		if(this.validator.form())
		{
			this.button.hide(); this.buttonDisable.show(); this.buttonLoader.show();
			this.form.ajaxSubmit({
				success : function(responseText){
					_this.button.attr("disabled",false).removeClass("button-loading");
					_this.button.show(); _this.buttonDisable.hide(); _this.buttonLoader.hide();
					_this.form[0].reset();

					// On success
					alert(successMsg);
					parent.hs.close();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown){
					alert(failureMsg);
				}
			});
		}
	},
	setValue:function(i){
		$("#xml_project").val((i==0)?"":"-");
		$("#xml_plan").val((i==0)?"":"-");
		$("#xml_address").val((i==0)?"-":"");
		$("#xml_road").val((i==0)?"-":"");
		$("#xml_soi").val((i==0)?"-":"");
		$("#xml_district").val((i==0)?"-":"");
		$("#xml_amphur").val((i==0)?"-":"");
		$("#xml_province").val((i==0)?"-":"");
		$("#xml_zipcode").val((i==0)?"-":"");
		$("#xml_telephone").val((i==0)?"-":"");
	}
};
$(function(){
	$psmagazine_form.initButtonElement();
	$psmagazine_form.init();
});