// JavaScript Document

$(document).ready(function() {

	/* ------ [ Adjust the labels ] ------ */
	$("#"+prefix+"form input").each(function() {
		if ($(this).attr('type') == 'text' && $(this).attr('name') != prefix+'captcha') {
			$(this).prev().prev().hide();
			$(this).val($(this).attr('title'));
		}
	});
	$("#"+prefix+"form select").each(function() {
		$(this).find("option").removeAttr("selected");						  
		//$(this).prepend('<option selected="selected" id="'+prefix+'selectFirst" value="'+$(this).prev().prev().attr('title')+'">'+$(this).prev().prev().attr('title')+'</option>');
		$(this).prev().prev().hide();
	});
	//var selector = document.getElementById(prefix+"requestACallback");
	//selector.selectedIndex= 0;
	
	/* ------ [ Reselect centre ] ------ */
	if (centre != '') {
		$("#"+prefix+"centreOfInterest option[value="+centre+"]").attr("selected","selected");
	}
	
	/* ------ [ Click clear ] ------ */
	$(".clickClear").focus(function(){
		if ($(this).val() == $(this).attr("title")) {$(this).val('');}								
	});
	$(".clickClear").blur(function(){
		if ($(this).val() == '') {$(this).val($(this).attr("title"));}								
	});
	
	/* ------ [ Hide captcha ] ------ */
	$("."+prefix+"captcha").remove();
	
	
	/* ------ [ Build/Hide onChange option labels  ] ------ */
	$("#"+prefix+"centreOfInterest,#"+prefix+"productOfInterest,#"+prefix+"requestACallback").find("option").removeAttr("selected");
	
	$("#"+prefix+"centreOfInterest").prepend($("<option>Centre of Interest..</option>").attr("class","label").attr("value","Centre of Interest.."));
	
	$("#"+prefix+"productOfInterest").prepend($("<option>Product of Interest..</option>").attr("class","label").attr("value","Product of Interest.."));
	$("#"+prefix+"requestACallback").prepend($("<option>Request a Callback?</option>").attr("class","label").attr("value","Request a Callback?"));
	$("#"+prefix+"centreOfInterest .label,#"+prefix+"productOfInterest .label,#"+prefix+"requestACallback .label").attr("selected","selected");
	
	$("#"+prefix+"centreOfInterest,#"+prefix+"productOfInterest,#"+prefix+"requestACallback").change(function(objEvent ){							   
				$(this).find(".label").remove();
	});
																									  
	

});

/* ------ [ Validate form ] ------ */
function validateGetInTouchForm() {
	var errors = false;
	
		//Validate generic
		$("#"+prefix+"form input").each(function() {
			if ($(this).attr('type') == 'text' && $(this).attr('name') != prefix+'captcha' && $(this).attr('name') != prefix+'telephone' && $(this).attr('name') != prefix+'company' && $(this).attr('name') != prefix+'email') {
				if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
					$(this).prev().html('Please enter a valid '+$(this).attr('name').replace(prefix,''));
						errors = true;
					}
						else {$(this).prev().html('');}
					}
					});
					
					// Validate telephone
					if ($("#"+prefix+"requestACallback").val() != 'no' && $("#"+prefix+"requestACallback").val() != 'Request a Callback?') {
					if ( !validate_phone( $('#'+prefix+'telephone').val() ) ) {
					$('#'+prefix+'telephone').prev().html('Please enter a valid telephone');
					errors = true;
					} else {
					$('#'+prefix+'telephone').prev().html('');
					}
					} else {
					$('#'+prefix+'telephone').prev().html('');
					}
					
					// Validate email
					if ($("#"+prefix+"requestACallback").val() == 'no' || $("#"+prefix+"requestACallback").val() == 'Request a Callback?') {
					if (!isValidEmail( $("#"+prefix+"email").val())) {
					$("#"+prefix+"email").prev().html('Please enter a valid email address').css('display','block'); 
					errors = true;
					} else {
					$("#"+prefix+"email").prev().html('');
					}
				} else {
			$("#"+prefix+"email").prev().html('');
		}
	
	if (errors) {return false;}
	else {
		pageTracker._trackPageview('/form-submission'); 
		otherTracker._trackPageview('/form-submission');
		return true;
	}
}

function isValidEmail(str) {
   return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(str);
}

////// Check if string is an integer //////
function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}

/////// Trim a string //////
function trim(s) {
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (c != " ") returnString += c;
	}
	return returnString;
}

////// Strip out characters in bag e.g. var bag = '@%$#'; //////
function stripCharsInBag(s, bag) {
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

////// Telephone Validation //////
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 7;

function validate_phone(strPhone) {
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1) {bracket=bracket+1;}
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket) {return false;}
	var brchr=strPhone.indexOf("(");
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")") {return false;}
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1) {return false;}
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}