//Stop form loading
function validate(){
	return false;
}
//Slide function
$(document).ready(function() {
	$("#loginProgress").animate({ height: 'hide', opacity: 'hide', width:'hide' }, 1 );
	//Login Function
	$('#LoginTrigger').click(function(){
		if(!document.getElementById("emailLogin").value || !document.getElementById("passwordLogin").value){
			var message = "You have not entered all required fields, please go back and complete the following.\n\n";
			
			if(!document.getElementById("emailLogin").value){
				message += "---Your E-mail\n";
			}
			
			if(!document.getElementById("passwordLogin").value){
				message += "---Your passwordLogin\n";
			}
			
			alert(message);
		}else{
			$("#loginStart").animate({ height: 'hide', opacity: 'hide', width:'hide' }, 'normal', function() {
				$("#loginProgress").animate({ height: 'show', opacity: 'show', width:'show' }, 'fast', function(){
						var strSend = "mail43456=" + encodeURIComponent(document.getElementById("emailLogin").value);
							  strSend += "&pass22314=" + encodeURIComponent(document.getElementById("passwordLogin").value);
					$.ajax({
						   type: "POST",
						   url: "parse/loginF.php",
						   data: strSend,
						   success: function(response){
							 if(response == "return_msg=1"){
								location.href = "teacher/index.php";
							}else if(response == "return_msg=2"){
								location.href = "admin/index.php";
							}else if(response == "return_msg=6"){
								location.href = "engineering";
							}else if(response == "return_msg=7"){
								location.href = "la";
							}else if(response == "return_msg=3"){
								alert("The E-mail you have used is not in our system. Please go back and check the E-mail you've typed in.");
								$("#loginProgress").animate({ height: 'hide', opacity: 'hide', width:'hide' }, 'fast', function (){
									$("#loginStart").animate({ height: 'show', opacity: 'show', width:'show' }, 'fast');
								});
								document.getElementById("emailLogin").focus();		
							}else if(response == "return_msg=4"){
								alert("The username / password combination you've entered is incorrect. Please try again.");
								$("#loginProgress").animate({ height: 'hide', opacity: 'hide', width:'hide' }, 'fast', function (){
									$("#loginStart").animate({ height: 'show', opacity: 'show', width:'show' }, 'fast');
								});
								document.getElementById("passwordLogin").focus();				
							}else if(response == "return_msg=5"){
								alert("Your login has not yet been authorised. Please contact your STA if you need to login urgently.");
								$("#loginProgress").animate({ height: 'hide', opacity: 'hide', width:'hide' }, 'fast', function (){
									$("#loginStart").animate({ height: 'show', opacity: 'show', width:'show' }, 'fast');
								});			
							}
						   }
						 });
					}
				);
				});
		}
    });
	
$('#stpLogo').hover(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
  $('#accreditationTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -400
    }, 200);
  });
$('#conferenceTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -800
    }, 200);
  });
$('#cyclingTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -1200
    }, 200);
  });
$('#parentsTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -1600
    }, 200);
  });
$('#resourcesTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -2000
    });
  });
$('#schoolsTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -2400
    }, 200);
  });
$('#scootingTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -2800
    }, 200);
  });
$('#walkingTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -3200
    }, 200);
  });
$('#registerTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -3600
    }, 200);
  });
$('#forgottenPasswordTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -4000
    }, 200);
  });
$('#roadSafetyTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -4400
    }, 200);
  });
$('#trafficTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -4800
    }, 200);
  });
$('#developmentTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -5200
    }, 200);
  });
$('#managementTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -5600
    }, 200);
  });
$('#sntTrigger').click(function() {
    $("#movingContent").animate({
      marginTop: -6000
    }, 200);
  });

//Accreditation Page
$('#accreditedSchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
$('#accreditationCaseStudies').click(function() {
    $("#movingContent").animate({
      marginTop: -400
    }, 200);
  });
$('#awardsCeremonies').click(function() {
    $("#movingContent").animate({
      marginTop: -800
    }, 200);
  });

	//Parents Page
	$('#parentsModeOfTravel').click(function() {
    $("#movingContent").animate({
      marginTop: -400
    }, 200);
  });
$('#parentsAccidentStats').click(function() {
    $("#movingContent").animate({
      marginTop: -800
    }, 200);
  });
$('#parentsCrashStats').click(function() {
    $("#movingContent").animate({
      marginTop: -1200
    }, 200);
  });
$('#parentsSNT').click(function() {
    $("#movingContent").animate({
      marginTop: -1600
    }, 200);
  });

//Schools Page

$('#allSchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
$('#primarySchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
$('#secondarySchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
$('#senSchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });
$('#independentsSchools').click(function() {
    $("#movingContent").animate({
      marginTop: -0
    }, 200);
  });

$('#schoolsKey').click(function() {
    $("#movingContent").animate({
      marginTop: -400
    }, 200);
  });
});


if($.browser.msie){}else{
 $('#content').corner("5px");
}
$('#loginBar').corner("5px");
$('#contactsMenu').corner("5px");
	
function mouseOver(z){
	document.getElementById(z).src="assets/"+z+"_(Hover).jpg";
}
function mouseOut(z){
	document.getElementById(z).src='assets/'+z+'.jpg';
}

//Resetting the users password
function resetPassword(){
	if(!document.getElementById("passwordReminder").value){
		alert("You haven't entered your E-mail address.");
		document.getElementById("passwordReminder").focus();
	}else{
		document.getElementById("passwordResetBTN").innerHTML = "<img src='assets/ajaxLoading.gif' alt='Loading...'/>&nbsp;&nbsp;&nbsp;&nbsp;Please Wait...";
		var strSend = "email23903=" + encodeURIComponent(document.getElementById("passwordReminder").value);
	$.ajax({
		   type: "POST",
		   url: "admin/AJAX/resetP.php",
		   data: strSend,
		   success: function(response){
			 if(response == "return_msg=success"){
				alert("Thank you, a new password has been sent to your E-mail. You can change it to something easier after logging in.");
				document.getElementById("passwordResetBTN").innerHTML = "";
				document.getElementById("emailLogin").value = document.getElementById("passwordReminder").value;
				document.getElementById("passwordReminder").value = "";
				showHideResetPassword();
			}else if(response == "return_msg=error"){
				alert("There was an error verifying your E-mail. Please check the E-mail you have typed in an try again or contact your STA with details of this error message.");
				document.getElementById("passwordResetBTN").innerHTML = "<img src=\"assets/submitNew.png\" width=\"69\" height=\"43\" alt=\"Submit\" onclick=\"resetPassword();\" style=\"cursor:pointer;\" />";
			}
		   }
		 });
	}
}

function sumbitForm(){
//First find any empty fields
if(document.getElementById("interestInSchoolR").value == "Administrator" || document.getElementById("interestInSchoolR").value =="Cyclists"){
	if(!document.getElementById("firstNameR").value || !document.getElementById("lastNameR").value || !document.getElementById("telephoneR").value || document.getElementById("interestInSchoolR").value=="" || !document.getElementById("passwordR").value || !document.getElementById("reTypePasswordR").value){
	
	var returnMsg = "All fields are mandatory and must be completed to register. The following fields have been missed.\n\n\n";
	
	if(!document.getElementById("firstNameR").value){
		returnMsg += "---Your First Name.\n";	
	}
	if(!document.getElementById("lastNameR").value){
		returnMsg += "---Your Last Name.\n";	
	}
	if(document.getElementById("schoolSelectR").value==""){
		returnMsg += "---You did not select a school.\n";	
	}
	if(!document.getElementById("telephoneR").value){
		returnMsg += "---Your Telephone.\n";	
	}
	if(!document.getElementById("passwordR").value){
		returnMsg += "---A password.\n";	
	}
	if(!document.getElementById("reTypePasswordR").value){
		returnMsg += "---Your password re-typed.\n";	
	}	
	
	alert(returnMsg);
	}else if(!document.getElementById("adminUserLogin").value || !document.getElementById("adminUserPassword").value){
	var returnMessage1 = "You have selected your Interest as an administrator but have not entered the following login details for an exisiting administrator.\n\n";
	
	if(!document.getElementById("adminUserLogin").value){
		returnMessage1 += "---Administrators Login.\n";	
	}
	
	if(!document.getElementById("adminUserPassword").value){
		returnMessage1 += "---Administrators Password.\n";	
	}
	
	alert(returnMessage1);
	}else{
	document.getElementById("submitRegistrationBTN").innerHTML = "<img src='assets/ajaxLoading.gif' alt='Loading...'/>&nbsp;&nbsp;&nbsp;&nbsp;Please Wait...";
		var strSend = "1029_firstname=" + encodeURIComponent(document.getElementById("firstNameR").value);
			strSend += "&1029_lastname=" + encodeURIComponent(document.getElementById("lastNameR").value);
			strSend += "&1029_telephone=" + encodeURIComponent(document.getElementById("telephoneR").value);
			strSend += "&1029_email=" + encodeURIComponent(document.getElementById("emailR").value);
			strSend += "&1029_password=" + encodeURIComponent(document.getElementById("passwordR").value);
			strSend += "&1029_memberType=" + encodeURIComponent(document.getElementById("interestInSchoolR").value);
			strSend += "&login=" + encodeURIComponent(document.getElementById("adminUserLogin").value);
			strSend += "&password=" + encodeURIComponent(document.getElementById("adminUserPassword").value);
		$.ajax({
			   type: "POST",
			   url: "parse/registration.php",
			   data: strSend,
			   success: function(response){
				 alert(response);
				if(response == "The administrator details you have entered are incorrect or invalid. Please re-check them and try again." || response == "There was a problem registering you. Please contact your webmaster with details of what caused this error."){
					document.getElementById("submitRegistrationBTN").innerHTML = "<input name='Submit' type='button' value='Submit' onclick='sumbitForm();'/>";
				}else{
					document.getElementById("submitRegistrationBTN").innerHTML = "";
				}
			   }
		 });
	}
}else if(document.getElementById("interestInSchoolR").value == "Engineer" || document.getElementById("interestInSchoolR").value == "la"){
	if(!document.getElementById("firstNameR").value || !document.getElementById("lastNameR").value || !document.getElementById("telephoneR").value || document.getElementById("interestInSchoolR").value=="" || !document.getElementById("passwordR").value || !document.getElementById("reTypePasswordR").value){
	
	var returnMsg = "All fields are mandatory and must be completed to register. The following fields have been missed.\n\n\n";
	
	if(!document.getElementById("firstNameR").value){
		returnMsg += "---Your First Name.\n";	
	}
	if(!document.getElementById("lastNameR").value){
		returnMsg += "---Your Last Name.\n";	
	}
	if(document.getElementById("schoolSelectR").value==""){
		returnMsg += "---You did not select a school.\n";	
	}
	if(!document.getElementById("telephoneR").value){
		returnMsg += "---Your Telephone.\n";	
	}
	if(!document.getElementById("passwordR").value){
		returnMsg += "---A password.\n";	
	}
	if(!document.getElementById("reTypePasswordR").value){
		returnMsg += "---Your password re-typed.\n";	
	}	
	
	alert(returnMsg);
	}else if(document.getElementById("passwordR").value != document.getElementById("reTypePasswordR").value){
		alert("Your Passwords do not match, please check them and try again.");
	}else{
	document.getElementById("submitRegistrationBTN").innerHTML = "<img src='assets/ajaxLoading.gif' alt='Loading...'/>&nbsp;&nbsp;&nbsp;&nbsp;Please Wait...";
		var strSend = "1029_firstname=" + encodeURIComponent(document.getElementById("firstNameR").value);
			strSend += "&1029_lastname=" + encodeURIComponent(document.getElementById("lastNameR").value);
			strSend += "&1029_telephone=" + encodeURIComponent(document.getElementById("telephoneR").value);
			strSend += "&1029_email=" + encodeURIComponent(document.getElementById("emailR").value);
			strSend += "&1029_password=" + encodeURIComponent(document.getElementById("passwordR").value);
			strSend += "&1029_memberType=" + encodeURIComponent(document.getElementById("interestInSchoolR").value);
			strSend += "&login=" + encodeURIComponent(document.getElementById("adminUserLogin").value);
			strSend += "&password=" + encodeURIComponent(document.getElementById("adminUserPassword").value);
		$.ajax({
			   type: "POST",
			   url: "parse/registration.php",
			   data: strSend,
			   success: function(response){
				 alert(response);
				if(response == "There was a problem registering you. Please contact your webmaster with details of what caused this error."){
					document.getElementById("submitRegistrationBTN").innerHTML = "<input name='Submit' type='button' value='Submit' onclick='sumbitForm();'/>";
				}else{
					document.getElementById("submitRegistrationBTN").innerHTML = "";
				}
			   }
		 });
	}
}else if(!document.getElementById("firstNameR").value || !document.getElementById("lastNameR").value || document.getElementById("schoolSelectR").value=="" || !document.getElementById("telephoneR").value || document.getElementById("interestInSchoolR").value=="" || !document.getElementById("passwordR").value || !document.getElementById("reTypePasswordR").value){
	
	var returnMsg = "All fields are mandatory and must be completed to register. The following fields have been missed.\n\n\n";
	
	if(!document.getElementById("firstNameR").value){
		returnMsg += "---Your First Name.\n";	
	}
	if(!document.getElementById("lastNameR").value){
		returnMsg += "---Your Last Name.\n";	
	}
	if(document.getElementById("schoolSelectR").value==""){
		returnMsg += "---You did not select a school.\n";	
	}
	if(!document.getElementById("telephoneR").value){
		returnMsg += "---Your Telephone.\n";	
	}
	if(document.getElementById("interestInSchoolR").value==""){
		returnMsg += "---Your Interest in the school.\n";	
	}
	if(!document.getElementById("passwordR").value){
		returnMsg += "---A password.\n";	
	}
	if(!document.getElementById("reTypePasswordR").value){
		returnMsg += "---Your password re-typed.\n";	
	}	
	
	alert(returnMsg);
}else if(echeck(document.getElementById("emailR").value)==false){//Validate email
	emailR.focus();
	alert("Please enter a valid E-mail address");
}else if(document.getElementById("passwordR").value != document.getElementById("reTypePasswordR").value){//Validate if passwords are the same
	passwordR.focus();
	alert("Your passwords do not match, please try again.");
}else{
	document.getElementById("submitRegistrationBTN").innerHTML = "<img src='assets/ajaxLoading.gif' alt='Loading...'/>&nbsp;&nbsp;&nbsp;&nbsp;Please Wait...";
	var strSend = "1029_firstname=" + encodeURIComponent(document.getElementById("firstNameR").value);
    	strSend += "&1029_lastname=" + encodeURIComponent(document.getElementById("lastNameR").value);
    	strSend += "&1029_school=" + encodeURIComponent(document.getElementById("schoolSelectR").value);
    	strSend += "&1029_telephone=" + encodeURIComponent(document.getElementById("telephoneR").value);
    	strSend += "&1029_email=" + encodeURIComponent(document.getElementById("emailR").value);
    	strSend += "&1029_password=" + encodeURIComponent(document.getElementById("passwordR").value);
    	strSend += "&1029_memberType=" + encodeURIComponent(document.getElementById("interestInSchoolR").value);
	$.ajax({
		   type: "POST",
		   url: "parse/registration.php",
		   data: strSend,
		   success: function(response){
			 alert(response);
			if(response == "Thank you for registering, your account will be authorised within 1 working day."){
				document.getElementById("submitRegistrationBTN").innerHTML = "";	
			}else{
				document.getElementById("submitRegistrationBTN").innerHTML = "<input name='Submit' type='button' value='Submit' onclick='sumbitForm();'/>";
			}
		   }
		 });
	}
}//Close the opening function for form validation

function validateInterest(){
	if(document.getElementById("interestInSchoolR").value=="Administrator"){
		document.getElementById("adminUserTxt").style.display = "inline";
		document.getElementById("schoolRegistrationDiv").style.display = "none";
	}else if(document.getElementById("interestInSchoolR").value =="Engineer" || document.getElementById("interestInSchoolR").value =="la"){
		document.getElementById("adminUserTxt").style.display = "none";
		document.getElementById("schoolRegistrationDiv").style.display = "none";
	}else{	
		document.getElementById("adminUserTxt").style.display = "none";	
		document.getElementById("schoolRegistrationDiv").style.display = "inline";
	}
}

//Telephone validation to ensure only digits, hypens or spaces are used in the telephone field
function validatephone(xxxxx) {
	 var maintainplus = '';
 	var numval = xxxxx.value
 	if ( numval.charAt(0)=='+' ){ var maintainplus = '+';}
 	curphonevar = numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
 	xxxxx.value = maintainplus + curphonevar;
 	var maintainplus = '';
 	xxxxx.focus;
}

//Email validation function
function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
