documentReady("addTalkToUsForm");
documentReady("hideTalkToUsFields");
documentReady("addTalkToUsValidation");

// The following add validation to the Talk To Us Form
var talkToUsSubmitButton = "[id$='talkToUsSubmit']";

var talkToUsPhoneID = "input[id$='_phoneInput']";
var talkToUsNameID = "input[id$='_nameInput']";
var countryFieldID = "select[id$='_CountryField']";

// The following hides those text areas the put the current site in the form
// simply makes the fields dissapear from user view via hide function
var siteFieldID = "[id$='siteField_scope']";
var codeFieldID = "[id$='_codeField_scope']";
function hideTalkToUsFields()
{
	$(siteFieldID).hide();
	$(codeFieldID).hide();
}

var talkToUsFooterFormHolder = "#talkToUsFooterFormHolder";
function addTalkToUsForm()
{
	// Attache a class to the containing div for CSS purposes
	$(talkToUsPhoneID).parent().addClass("inputHolder");
	$(talkToUsNameID).parent().addClass("inputHolder");
	
	var tempHolder = $(talkToUsFooterFormHolder).html();
	var newForm = "<form id='talkToUsFooterForm'></form>"
	$(talkToUsFooterFormHolder).html(newForm);
	$("#talkToUsFooterForm").html(tempHolder);
}

function removeTalkToUsForm()
{
	var tempNameValue = $(talkToUsNameID).attr('value');
	var tempPhoneValue = $(talkToUsPhoneID).attr('value');
	var tempHolder = $("#talkToUsFooterForm").html();
	$(talkToUsFooterFormHolder).html(tempHolder);
	
	// Reset the values after removing and replacing
	$(talkToUsPhoneID).attr('value', tempPhoneValue);
	$(talkToUsNameID).attr('value', tempNameValue);
}

function addTalkToUsValidation()
{
	if(isTalkForm())
		addTalkToUsRemoveDefault();
		
	// Make both fields required
	$(talkToUsPhoneID).addClass("phoneValidation");
    $(talkToUsNameID).addClass("nameValidation");
	
	setupValidationClassRules();
		
	// Now add an action listener when our submit form is hit
	$(talkToUsSubmitButton).click(
		function()
	    {
			 var validForm = validFormTalkToUs(); // return the result of validation
			 
			 if (isTalkForm()) {
			 	if ($(talkToUsNameID).attr('value') == "Your Name") 
			 		$(talkToUsNameID).attr('value', "");
			 	if ($(talkToUsPhoneID).attr('value') == "Your Phone") 
			 		$(talkToUsPhoneID).attr('value', "");
			 }	
			
		 	// Reset the default values after mesage shown
			 if (!validForm) 
			 {
				if (isTalkForm()) 
				{
					if ($(talkToUsNameID).attr('value') == "") 
						$(talkToUsNameID).attr('value', "Your Name");
					if ($(talkToUsPhoneID).attr('value') == "") 
						$(talkToUsPhoneID).attr('value', "Your Phone");
			 	}
			 }else if(validForm)
			 {
				removeTalkToUsForm(); // Strip out the temporary form holder if the submission is valid
				// Google Tracking
				
				trackTalkToUsForm($(this)); // Dependant on mainAnalytics.js for this call
			 }	
			 return validForm; // A false result will halt the submission
		}
	);	
}

// method to determine if we are on the footer form
function isTalkForm()
{
	var footerInput = $(talkToUsPhoneID).attr("id");
	if(footerInput != undefined && footerInput.indexOf("ctl10") > -1)// If on the homepage form
	{
		return true;
	}
	return false;
}

/** Method removes the default values of the fields when a user clicks into the input
 * 
 */
function addTalkToUsRemoveDefault()
{
	$(talkToUsPhoneID).focus(// When the field gains focus
		function()
	    {
			 if($(this).attr("value") == "Your Phone")
			 	$(this).attr("value", "")
		}
	).blur( // When the field loses focus
		function()
	    {
			 if($(this).attr("value") == "")
			 	$(this).attr("value", "Your Phone")
		}
	);
	
	$(talkToUsNameID).focus(// When the field gains focus
		function()
	    {
			 if($(this).attr("value") == "Your Name")
			 	$(this).attr("value", "")
		}
	).blur(// When the field loses focus
		function()
	    {
			 if($(this).attr("value") == "")
			 	$(this).attr("value", "Your Name")
		}
	);
}

/** Function adds the validation rules onto our form
 * 
 */
function validFormTalkToUs()
{
	 var talkToUsValidator = $("#talkToUsFooterForm").validate(
	 	{ignore: "[class*='contactUs']"}
	 );
	 var valid = talkToUsValidator.form();	
	 return valid;	
}


// Error classes correspond to actual class added above
function setupValidationClassRules()
{
 	 jQuery.validator.addClassRules({
	 	 nameValidation: 
		 {
		 	required: true,
		 	minlength: 2,
	 		maxlength: 30,
			messages: {
		 		required: "Please provide your name",
		 		minlength: "Name is to short",
		 		maxlength: "Name is to long"
		 	}
		 },
		 phoneValidation: 
		 {
		 	required: true,
		 	minlength: 8,
			maxlength: 15,
			phone: true,
			messages: 
			 {
			 	phone: "Phone number appears invalid",
				minlength: "Phone number is to short",
				maxlength: "Phone number is to long",
				required: "Please enter a phone number"
			 }
		 }
	 });
}


jQuery.validator.addMethod("phone", phoneValidator, "Phone number invalid");
function phoneValidator(value, element) 
{  	
	var myValue = whiteSpace(value).stripSpaces();
    return this.optional(element) || /^[-+]?[0-9]+$/.test(myValue);
}

