$(document).ready(function(){
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
});


$(document).ready(function(){
	$(".rollover-gif").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","_over.gif#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.gif#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.gif#hover",".gif");
				$(this).attr("src",oldSrc);
			}
		}
	);
});


$(document).ready(function(){
	$(".calcrollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
				loadCalc($(this).attr('id'));
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	); 
	
	$(".calcrollover").click(
		function() {
			var oldSrc = $(this).attr("src").replace("_over.png#hover","_on.png#hover");
			$(this).attr("src",oldSrc);
		}
	);
});

function loadCalc(mode) {
	$(".calc").hide();
	$("#calc_"+mode).show();
}

function verifyForm() {
	d = document.frmRequest;
	var rf_fcbp2			= d.rf_fcbp2.checked;
	if (rf_fcbp2==false) 		{ alert('You must read and agree to our privacy policy'); return false; }
	
	input_box = confirm("Is all information correct?");	
	if (input_box == true)	
		return true;
	else	
		return false; 	
}


function signup() {
	var username = $("#signup_username").val();
	var email = $("#signup_email").val();
	if (!username || username == '') {
		alert('Please enter username ');
		return false;
	}
	if (validateEmail(email) == false) {
		alert('Please enter email address');
		return false;
	}
	if (username && email) {
		$.ajax({
		   type: "POST",
		   url: "/signup.php",
		   data: "username="+username+"&email="+email+"",
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   $("#signup_username").val('');
				   $("#signup_email").val('');
				   alert("Thank you");
			   } else if (data == 'EXISTS') {
				   $("#signup_username").val('');
				   $("#signup_email").val('');
				   alert("This email already exists in database");
			   } else {
				   alert(data);
			   }
		   }
		});	
	} 
}


function askquestion() {
	var name = $("#qname").val();
	var email = $("#qemail").val();
	var telephone = $("#qtelephone").val();
	var question = $("#qquestion").val();
	
	if (!name || name == '') {
		alert('Please enter your name ');
		return false;
	}
	if (validateEmail(email) == false) {
		alert('Please enter valid email address');
		return false;
	}
	
	if (!question || question == '') {
		alert('Please enter your question');
		return false;
	}
	
	if (name && email && question) {
		$.ajax({
		   type: "POST",
		   url: "/askquestion.php",
		   data: "name="+name+"&email="+email+"&telephone="+telephone+"&question="+question,
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   $("#qname").val('');
				   $("#qemail").val('');
				   $("#qtelephone").val('')
				   $("#qquestion").val('')
				   alert("Thank you");
			   } else if (data == 'ERROR') {
				   alert('Sorry, we cannot complete your request.\nPlease try again.');
			   } else {
				   alert(data);
			   }
		   }
		});	
	} 
}

function sendForm() {
	var name = $("#fname").val();
	var address1 = $("#faddress1").val();
	var address2 = $("#faddress2").val();
	var postcode = $("#fpostcode").val();
	var telephone = $("#ftelephone").val();
	var mobile = $("#fmobile").val();
	var email = $("#femail").val();
	var question = $("#fquestion").val();
	var timetocall = $("#timetocall").val();
	
	if (!name || name == '') {
		alert('Please enter your name');
		return false;
	}
	if (!address1 || address1 == '') {
		alert('Please enter your address: line 1');
		return false;
	}
	
	if (!address2 || address2 == '') {
		alert('Please enter your address: line 2');
		return false;
	}
	
	if (!postcode || postcode == '') {
		alert('Please enter postcode');
		return false;
	}
	
	if (!telephone || telephone == '') {
		alert('Please enter phone number');
		return false;
	}
	
	if (validateEmail(email) == false) {
		alert('Please enter valid email address');
		return false;
	}
	
	if (!question || question == '') {
		alert('Please enter your question');
		return false;
	}
	
	if (!timetocall || timetocall == '') {
		alert('Please enter best time to call');
		return false;
	}
	
	if (name && email && question && telephone && postcode && address1 && address2 && timetocall) {
		$.ajax({
		   type: "POST",
		   url: "/sendform.php",
		   data: "name="+name+"&email="+email+"&telephone="+telephone+"&question="+question+"&address1="+address1+"&address2="+address2+"&postcode="+postcode+"&mobile="+mobile+"&timetocall="+timetocall,
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   	$("#name").val('');
				   	$("#email").val('');
				   	$("#telephone").val('');
				   	$("#question").val('');
				   	$("#mobile").val('');
				   	$("#address1").val('');
				   	$("#address2").val('');
				   	$("#postcode").val('');
				   	$("#timetocall").val('')
				   
				   	$("#wrap").html('<p>Thank you for your request, someone will respond in due course</p>');
			   } else if (data == 'ERROR') {
				   	$("#wrap").html('Sorry, we cannot complete your request.\nPlease try again.');
			   } else {
				  	$("#wrap").html(data);
			   }
		   }
		});	
	} 
}





 
function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
	   return true;
   }
}


 
 
 $(document).ready(function(){ 
	$("ul.topnav li.parents:not(:last-child)").append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li.drop").mouseover(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).find("ul.subnav").fadeIn('fast').show(); //Drop down the subnav on click
 
		$(this).hover(function() {
		}, function(){
			$(this).find("ul.subnav").fadeOut('fast'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

}); 
  
 $(document).ready(function(){
		$('a.email_replace').each(function(){
			e = this.rel.replace('$$','@');
			this.href = 'mailto:' + e;
			//$(this).text(e);
		});
		$('a.email_replace2').each(function(){
			e = this.rel.replace('$$','@');
			this.href = 'mailto:' + e;
			$(this).text(e);
		});
	}); 

