//Contact us form processing
function ContactUs(strContactType) {

	//Declare local variables
	var objForm			= '';
	var objInput		= '';
	var objSubmit		= '';
	var strError		= '';
	var arrErrorMsgs	= new Array();

	//Init error messages
	arrErrorMsgs['missing_value'] = '';
	arrErrorMsgs['invalid_value'] = '';

	//Do type depend things
	switch (strContactType) {

		case 'CallBack':

			objForm		= $('form#idCallBackForm');
			objInput	= $('input#idCallBackPhone');
			objSubmit	= $('input#idCallBackSubmit');

			arrErrorMsgs['missing_value'] = 'Please enter your telephone number.';
			arrErrorMsgs['invalid_value'] = 'Please enter a valid telephone number.';

			break;

		case 'RequestInfo':

			objForm		= $('form#idRequestInfoForm');
			objInput	= $('input#idRequestInfoEmail');
			objSubmit	= $('input#idRequestInfoSubmit');

			arrErrorMsgs['missing_value'] = 'Please enter your email address.';
			arrErrorMsgs['invalid_value'] = 'Please enter a valid email address.';

			break;

		case 'PayCalculator':

			objForm		= $('form#idPayCalculatorForm');
			objInput	= $('input#idPayCalculatorRate');
			objSubmit	= $('input#idPayCalculatorSubmit');

			arrErrorMsgs['missing_value'] = 'Please enter your hourly rate.';
			arrErrorMsgs['invalid_value'] = 'Please enter a valid hourly rate.';

			break;

	}

	//Check the form inputs
	if (objInput.val() == '' || objInput.val() == objInput.attr('title')) {
		strError = arrErrorMsgs['missing_value'];
	} else if (strContactType != 'PayCalculator' && objInput.val().length < 7) {
		strError = arrErrorMsgs['invalid_value'];
	} else if (strContactType == 'PayCalculator' && isNaN(parseFloat(objInput.val()))) {
		strError = arrErrorMsgs['invalid_value'];
	}

	//Process the form
	if (strError == '' && strContactType == 'PayCalculator') {

		//Everything is fine, submit the form
		objForm.submit(function () {
			window.location = '/index.php/pay-calculator?rate='+objInput.val()+'&Illustration=true#Calculator'; //We bound the return false to the form
			return false;
		});


	//Process the form
	} else if (strError == '' && strContactType == 'CallBack') {

		//Everything is fine, submit the form
		objForm.submit(function () {
			window.location = '/index.php/request-a-call?ibal_sub_tel='+objInput.val(); //We bound the return false to the form
			return false;
		});


	//Process the form
	} else if (strError == '' && strContactType == 'RequestInfo') {

		//Everything is fine, submit the form
		objForm.submit(function () {
			window.location = '/index.php/request-information?ibal_sub_email='+objInput.val(); //We bound the return false to the form
			return false;
		});



	} else {

		//Write the error
		$('#id'+strContactType+'Error').show();
		$('#id'+strContactType+'Error').attr("innerHTML",strError);

	}

	//So it won't submit
	objForm.submit(function () { return false; });


}

//Add methods to the contact forms
$(document).ready(function() {

	$('div#Right form input:text').focus(function () {

		//Add onclick class
		$(this).addClass('onclick');

		//Empty value if it equals to the default
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}

		//Hide the exists contact errors
	//	$('#id'+strContactType+'Error').hide();

    });

	$('div#Right form input:text').blur(function () {

		//If it's empty or it equals to the default
		if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {

			//Remove onclick class 
			$(this).removeClass('onclick');

			//Set the default value
			$(this).val($(this).attr('title'));

		}

    });

});

// Is email address correcly formatted
function isEmailAddress(s)
{
	var pattern = new RegExp(/^\s*[a-zA-Z0-9\-\._]+@[\.a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*)\.[a-zA-Z]{2,4}\s*$/);

	if(pattern.test(s)) { return true; }

	return false
}

//Close facebox function
function closeFacebox() {
	$.facebox.close();
}