var msg_contact_name = '請填寫姓名';
var msg_contact_title = '請選擇稱謂';
var msg_contact_email = '請填寫正確的E-mail';
var msg_contact_phone = '請填寫聯絡人電話';
var msg_contact_mobile = '請填寫聯絡人手機';
var msg_contact_code = '請填寫驗證碼';
var msg_contact_success = '送出成功';
var msg_contact_captcha_invalid = '請確認您輸入的驗證碼';
///
$(document).ready(function(){
		initContactForm();
});

function resetContactForm()
{	
	var txt_name = $('#consult-name');
	var txt_email = $('#consult-email');
	var txt_mobile = $('#consult-mobile');
	var txt_phone = $('#consult-phone');
	var txt_captcha_input = $('#captcha_input');
	txt_name.val('');
	txt_email.val('');
	txt_mobile.val('');
	txt_phone.val('');
	txt_captcha_input.val('');
	
	//////
	var txt_destination = $('#consult-destination');
	var cb_title = $('#consult-title');
	var txt_departure_date = $('#consult-departure_date');
	var txt_days = $('#consult-days');
	var txt_company = $('#consult-company');
	var txt_budget = $('#consult-budget');
	var txt_ps = $('#consult-ps');
	
	txt_destination.val('');
	cb_title.val('');
	txt_departure_date.val('');
	txt_days.val('');
	txt_company.val('');
	txt_budget.val('');
	txt_ps.val('');
	
}
function clearHint()
{	
	$('#hint-name').hide('');
	$('#hint-title').hide('');
	$('#hint-email').hide('');
	$('#hint-mobile').hide('');
	$('#hint-phone').hide('');
	$('#hint-topic').hide('');
	$('#hint-destination').hide('');
	$('#hint-departure_date').hide('');
	$('#hint-company').hide('');
	$('#hint-budget').hide('');
	$('#hint-days').hide('');
	$('#hint-ps').hide('');
	$('#hint-captcha').hide('');
	
}
function initContactForm()
{
	resetContactForm();
	clearHint();
}
///
function sendContact()
{
	clearHint();	

	var cb_title = $('#consult-title');
	var hint_title = $('#hint-title');
	var title = cb_title.val();

	var txt_name = $('#consult-name');
	var hint_name = $('#hint-name');
	var name = txt_name.val();

	var txt_email = $('#consult-email');
	var hint_email = $('#hint-email');
	var email = txt_email.val();
	
	var txt_mobile = $('#consult-mobile');
	var hint_mobile = $('#hint-mobile');
	var mobile = txt_mobile.val();
	
	var txt_phone = $('#consult-phone');
	var hint_phone = $('#hint-phone');
	var phone = txt_phone.val();
	

	var cb_topic = $('#consult-topic');
	var hint_topic = $('#hint-topic');
	var topic = cb_topic.val();
	
	
	var txt_captcha_id = $('#captcha_id');
	var txt_captcha_input = $('#captcha_input');
	var hint_captcha = $('#hint-captcha');
	var captcha_id = txt_captcha_id.val();
	var captcha = txt_captcha_input.val();

	//
	var txt_destination = $('#consult-destination');
	var txt_departure_date = $('#consult-departdate');
	var txt_days = $('#consult-days');
	var txt_company = $('#consult-company');
	var txt_budget = $('#consult-budget');
	var txt_ps = $('#consult-ps');
	
	var destination = txt_destination.val();
	var departure_date = txt_departure_date.val();
	var days = txt_days.val();
	var company = txt_company.val();
	var budget = txt_budget.val();
	var ps = txt_ps.val();
	//
	var formValid = true;
	var focusObj = null;
	
	if(!isValidText(name))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = txt_name;
		}
		hint_name.html(msg_contact_name);
		hint_name.show();
	}
	
	if(!isValidComboBox(title))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = cb_title;
		}
		hint_title.html(msg_contact_title);
		hint_title.show();
	}
/*
	if(!isValidText(phone))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = txt_phone;
		}
		hint_phone.html(msg_contact_phone);
	}
*/

  if(!isValidText(mobile))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = txt_mobile;
		}
		hint_mobile.html(msg_contact_mobile);
		hint_mobile.show();
	}
	
	if(!isValidEmail(email))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = txt_email;
		}
		hint_email.html(msg_contact_email);
		hint_email.show();
	}
	
	if(!isValidText(captcha))
	{
		formValid = false;
		if(focusObj== null)
		{
			focusObj = txt_captcha_input;
		}
		hint_captcha.html(msg_contact_code);
		hint_captcha.show();
	}
	if(formValid)
	{
		var data = {
								captcha_id:captcha_id,
								captcha:captcha,
								name:name,
								title:title,
								phone:phone,
								mobile:mobile,
								topic:topic,
								email:email,
								destination:destination,
								departure_date:departure_date,
								days:days,
								company:company,
								budget:budget,
								ps:ps
								};

	
		$.post("/app/sendcontact", data,function(data){
			
			updateCaptcha();
			switch(data)
			{
				case 'create_success':
					initContactForm();
					alert('已送出您的資訊,我們會儘快與您聯絡');
				break;
				
				case 'captcha_invalid':
					hint_captcha.html(msg_contact_captcha_invalid);
					txt_captcha_input.focus();
				break;
			}
		});
	}
	else
	{
		focusObj.focus();
	}
	
}

function updateCaptcha()
{
		$.post("/app/updatecaptcha",{},function(data){
																						
			var txt_captcha_id = $('#captcha_id');
			var txt_captcha = $('.form .ckcode');	
			txt_captcha.attr('src','/captcha/'+data+'.png');
			txt_captcha_id.val(data);
		});
}
