var formLock = false;
var mouseOver = false;

$(document).ready(function(){

    //Preparations for send to mobile form
    

	    $('#mobileForm').hide();
	    
	    $("#sendToMob").hover(
	    	function () {
				mouseOver = true;
	        	$('#mobileForm').slideDown("fast")
	    	}, 
	    	function () {
				   mouseOver = false;
				
				if(!formLock){
					t = setTimeout("moderator()",1000);  
	        		
        		}
	      	}
	    );
	    
	    $('#phonenumber').focus( function(){
		   	formLock = true;
	    });
	    
		 $('#phonenumber').blur( function(){
		    formLock = false;
			if(!mouseOver){
		    	$('#mobileForm').slideUp("fast");
			}
			
	    });
	    
	   // $('#sendToMob a').click( function(){
		   // return false;
	    //});
	    
	//Preparations for tabbed navigation
	
		$('#mainNav li.notCurrent').hover(
			function(){
				$(this).children().css("padding-bottom","10px")	
				$(this).css("margin-top","-5px")					
			},
			function(){
				$(this).children().css("padding-bottom","5px")	
				$(this).css("margin-top","0px")	
			}				
				
		);

	//Adding show all/hide all button
	
		$('#doyou').after("<p id='extraLinks'><a id='showAll' href='#'>Show all answers</a> | <a id='hideAll' href='#'>Hide all answers</a></p>");
		
		$('#showAll').click( function(){
			$("#questionsNav div").show();
		});
		
		$('#hideAll').click( function(){
			$("#questionsNav div").hide();
		});		
    
 	$('#netLink').toggle( function(){
		   $('#netMessage').show();
			return false;
	    },
		function(){
		$('#netMessage').hide();
			return false;
	    }
		
	);
});



function moderator(){
	if(!formLock && !mouseOver){
		$('#mobileForm').slideUp("fast");
	}
}


//Developed by Chris Mulholland.

var pass;

//Update the fields below, following this format. 

var errorSet = {
			'emptyError': 'Please enter a mobile number.',
			'invalidError': 'Please enter a valid mobile number.'
		};

//Add the validation functions below. Full list of functions available can be found in genForm.js
function checkMobileNumber(myform){
	//sets pass variable to true. Do not remove.
	pass = true;
	//Call a function for each field. Modify this
	checkTelNo('phonenumber');
	//Sets pass variables. Do not remove.
	if(pass){
		//ajaxMagic();
	}
	if(!pass){
	return false;
	}
}

function checkTelNo(fieldName, req){
	var	formField = document.getElementById(fieldName);
	var regexp2 = /^07/; 
	var phoneNum = formField.value;
	if(phoneNum.match(regexp2)){
		phoneNum = phoneNum.replace(/^07/,"+447");
		phoneNum = phoneNum.replace(/ /g,"");
	}
	document.getElementById(fieldName).value = phoneNum;
		//debugging
	if(!formField){
		alert("checkTelNo: The field with id '" +fieldName +"' doesn't exist");
		return false;
	}
	var regexp =  /^[+0-9 ]{12,}$/;  //matches thesse chars [+0-9 ] at least 9 chars long
	if (!formField.value && req != false){
		writeError(fieldName, 'emptyError');
		fail(fieldName);
	}else if(formField.value && !formField.value.match(regexp)) {
		writeError(fieldName, 'invalidError');
		fail(fieldName);
	}else{
		clearError(fieldName);
	}
}
function fail(fieldName){
	//called when any formfield fails validation
	//if(pass == true) setFocus(fieldName); //sets focus to the first field that has an error
	pass=false;
}

function passed(){
	var submitButton = document.getElementById('submitButton');
	if(submitButton) {
		//submitButton.value = "Please Wait...";
		submitButton.disabled = true;
	}
}
function writeError(fieldName, errorType){
	//change colour of field
	var formField = document.getElementById(fieldName);
	if(formField&&formField.name == formField.id)formField.className += " errorField";
	
	//write error
	var myDiv = document.getElementById(fieldName + 'Error');
	if(myDiv){
		myDiv.className="error";
		myDiv.style.display="block";
		myDiv.style.clear="both";
		myDiv.style.padding="3px 0 0";
		myDiv.style.fontWeight="bold";
		myDiv.style.color="#F00";
		myDiv.innerHTML = errorSet[errorType];
	}else{//debugging
		alert("The span with id '" +fieldName+ "Error doesn't exist")
	}
}
function clearError(fieldName){
	//Remove error colour from field
	var formField = document.getElementById(fieldName);
	if(formField)formField.className = formField.className.replace(/( ?errorField)*$/,"");

	//Remove error
	var myDiv = document.getElementById(fieldName + 'Error');
	if(myDiv){
		myDiv.className="";
		myDiv.style.display="inline";
		myDiv.style.padding="0";
		myDiv.innerHTML = "";
	}else{//debugging
		alert("The span with id '" +fieldName+ "Error doesn't exist")
	}
}



function ajaxMagic(myformID){
	phoneNum = $('#phonenumber').val();
	
	$.get("http://md2.yell.com/pro/web/yell/ppy2/registered", { phone: phoneNum },
		function(data){
		if(data==0){
			$('#ajaxResult').html("<p>Success, check your phone!</p>");
			$('#ajaxResult').show("slow")
		}
		if(data==1){
			$('#ajaxResult').html("<p>System error SMS could not be sent.</p>");
			$('#ajaxResult').show("slow")
		}
		if(data==2){
			$('#ajaxResult').html("<p>Max. limit of user tries reached for this user ("+phoneNumber+").</p>");
			$('#ajaxResult').show("slow")
		}
		if(data==3){
			$('#ajaxResult').html("<p>Max. limit of requests reached from same origin.</p>");
			$('#ajaxResult').show("slow")
		}
		if(data==4){
			$('#ajaxResult').html("<p>Phone number entered is not correct.</p>");
			$('#ajaxResult').show("slow")
		}				
	});

}


//Developed by Chris Mulholland.
//Used in conjunction with genForm.js

//*****Set-up variables. Do not change or remove.
var pass;
var initFocus;

//*****Customized fields
// Enter field name to focus on when form loads. Comment out if not desired.
//initFocus = "accountID"; 


//Update the fields below, following this format. 

var validationSet = {
		'gen': {// Do not remove this. <span id="genError"></span> should be added to top of the document
			'genError': "There's a problem with some of the details you've entered.<br />Please re-enter the information where indicated below."
		},
		'messageBox': {
			'emptyError': 'Please enter a message into the box.'
		},
		'emailBox': {
			'emptyError': 'Please enter your e-mail address.',
			'invalidError': 'Please enter a valid e-mail address.'
		},
		'nameBox': {
			'emptyError': 'Please enter your name into the box.'
		}
	};

//Add the validation functions below. Full list of functions available can be found in genForm.js
function checkForm(myform){
	//sets pass variable to true. Do not remove.
	pass = true;
	
	//Call a function for each field. Modify this
	notEmpty('messageBox');
	checkEmail('emailBox');
	notEmpty('nameBox');

	//Sets pass variables. Do not remove.
	if(!pass){
		failed();
		return false
	}else{passed()}
}