﻿$contact_form = {
	isCompany : false,
	form : null,
	validator : null,
	button : null,
	buttonDisable : null,
	buttonLoader : null,
	buttonCancel : null,
	init : function(){
		var loadProject = function(){
			var brandid=$("#xml_brandId").val();
			$("#xml_projectId > option").remove();
			$.get(__siteroot+"/psweb/server/loadprojects.rhtml",{brandId:brandid},function(json){
				var projects=eval("("+json+")");
				$.each(projects,function(i,o){
						$("#xml_projectId").append("<option value='"+o.id+"' "+((__projectid == o.id)?"selected":"")+">"+o.name+"</option>");
				});
				$("input[name=xml_project]").val($("#xml_projectId > option:selected").text());
			});
		};
		var loadBrand = function(){
			$.get(__siteroot+"/psweb/server/loadbrands.rhtml",{id:__brandid},function(json){
				var brands=eval("("+json+")");
				$.each(brands,function(i,o){
					$("#xml_brandId").append("<option value='"+o.id+"'>"+o.name+"</option>");
				});
					loadProject();
				$("input[name=xml_brand]").val($("#xml_brandId > option:selected").text());
			});
		};
		$("#xml_brandId").change(function(){
			loadProject();
			$("input[name=xml_brand]").val($("#xml_brandId > option:selected").text());
		});
		$("#xml_projectId").change(function(){
			$("input[name=xml_project]").val($("#xml_projectId > option:selected").text());

			// Update xml_projectCode field
			var pVal=$('select#xml_projectId').val();
			$.each(__productCodeMap,function(i,o){
				if(o.Id==pVal){
					$('input#xml_projectCode').val(o.Code);
				};
			});
		});
		$(".contact-house").click(function(){
			this.isCompany = false;
			$(".contact-company-item").hide();
			if(__brandid!="0" && __brandid!="1" && __brandid!="2" && __brandid != "$brandId")
			{	
				$(".contact-choice").hide();
				$("#xml_brandId > option,#xml_projectId > option").remove();
				$(".contact-house-item").show();
				loadBrand();
			}
		}).click();
		$(".contact-company").click(function(){
			this.isCompany = true;
			$(".contact-house-item").hide();
			$("#xml_brandId > option,#xml_projectId > option").remove();
			$("#xml_brandId , #xml_projectId").append("<option value='-1'>Corporate</option>");
			$("input[name=xml_brand],input[name=xml_project]").val("Corporate");
			$(".contact-company-item").show();
		});
		$("#xml_aboutContact").change(function(){
			if($(this).val()=="สมัครงาน")
				window.open("http://www.superhris.com/listrequest?type=15&idEmp=945");
		});
		
		
		var _this = this;
		this.button.click(function(){
			_this.submit();
		});
		
		if(!this.form){
			this.form = $("#contact-form");
			if(!this.validator){
				var msg="*";
				this.validator=this.form.validate({
					rules : {
						xml_title : {
							required: true
						},
						xml_detail : {
							required: true
						},
						xml_firstname : {
							required: true
						},
						xml_lastname : {
							required: true
						},
						xml_email : {
							required: true,
							email : true
						},
						xml_telephone : {
							number:true
						}
					},
					messages: {
						xml_title:msg,
						xml_detail:msg,
						xml_firstname:msg,
						xml_lastname:msg,
						xml_email:msg,
						xml_telephone:msg
					},
					//showErrors : function(){},
					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(){
			pageTracker._trackPageview('/virtual-event/touchpoint/contact/click-cancel');
			hsCloseConfig.contact.close(__brandId, true);
		});
	},
	submit:function(){
		// ##### Begin Track
		pageTracker._trackPageview('/virtual-event/touchpoint/contact/click-submit');
		// ##### End Track
		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.attr("disabled",true).addClass("button-loading");
			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);
					// #### HS CLOSE CONFIG #### //
					hsCloseConfig.contact.close(__brandId);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown){
					alert(failureMsg);
				}
			});
		}
	}
};
$(function(){
	// Init Button
	$contact_form.initButtonElement();
	$contact_form.init();
});