$(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);
			}
		}
	);
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(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 (email && username) {
		$.ajax({
		   type: "POST",
		   url: "/signup.php",
		   data: "email="+email+"&username="+username,
		   error: function(){
				alert('Error. 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("Such email already exists in our database");
			   } else {
				   alert(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;
   }
}

function loadTwitter() {
		$.ajax({
		   type: "POST",
		   url: "/twitter.php",
		   error: function(){
				$("#twitter").html('Twitter not available. Please try later.');
		   },
		   success: function(data){
			$("#twitter").html(data);
		}
	});	 
}


function sendForm() {
	var firstname = $("#ffirstname").val();
	var surname = $("#fsurname").val();
	var email = $("#fsubject").val();
	var email = $("#femail").val();
	var question = $("#fquestion").val();
	var subject = $("#fsubject").val();
	var nl  = $("#fnewsletter").attr('checked');
	var newsletter;
	
	if (nl == true) {
		newsletter = 1;	
	} else {
		newsletter = 0;	
	}
	
	if (!firstname || firstname == '') {
		alert('Please enter your firstname');
		return false;
	}
	if (!surname || surname == '') {
		alert('Please enter your surname');
		return false;
	}
	
	if (validateEmail(email) == false) {
		alert('Please enter valid email address');
		return false;
	}
	
	if (!subject || subject == '') {
		alert('Please enter message subject');
		return false;
	}
	
	if (!question || question == '') {
		alert('Please enter your message');
		return false;
	}

	
	if (firstname && surname && email && question && subject && newsletter) {
		$.ajax({
		   type: "POST",
		   url: "/sendform.php",
		   data: "firstname="+firstname+"&email="+email+"&surname="+surname+"&question="+question+"&subject="+subject+"&newsletter="+newsletter,
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   	$("#ffirstname").val('');
				   	$("#email").val('');
				   	$("#fsurname").val('');
				   	$("#fquestion").val('');
				   	$("#fsubject").val('');	  
				   
				   	$(".cBoxRight").html('<p>Thank you for your request.<br>Someone will respond in due course</p>');
			   } else if (data == 'ERROR') {
				   	$(".cBoxRight").html('Sorry, we cannot complete your request.<br>Please try again.');
			   } else {
				  	$(".cBoxRight").html(data);
			   }
		   }
		});	
	} 
} 


function send2friend() {
	var e2f_femail = $("#e2f_femail").val();
	var e2f_name = $("#e2f_name").val();
	var e2f_yemail = $("#e2f_yemail").val();
	var e2f_message = $("#e2f_message").val();
	
	if (validateEmail(e2f_femail) == false) {
		alert("Please enter valid friend's email address");
		return false;
	}
	
	if (!e2f_name || e2f_name == '') {
		alert('Please enter your name');
		return false;
	}
	
	if (validateEmail(e2f_yemail) == false) {
		alert('Please enter your valid email address');
		return false;
	}
	
	if (!e2f_message || e2f_message == '') {
		alert('Please enter your message');
		return false;
	}

		$.ajax({
		   type: "POST",
		   url: "/send2friend.php",
		   data: "e2f_femail="+e2f_femail+"&e2f_name="+e2f_name+"&e2f_yemail="+e2f_yemail+"&e2f_message="+e2f_message,
		   error: function(){
				alert('Sorry, we cannot send your email.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") { 
				   	$("#e2f_femail").val('');
				   	$("#e2f_name").val('');
				   	$("#e2f_yemail").val('');
				   	$("#e2f_message").val('');

				   	alert("Thank you for your request.\nSomeone will respond in due course");
			   } else if (data == 'ERROR') {
				   	alert("Sorry, we cannot complete your request.\nPlease try again.");
			   } else {
				  	alert(data);
			   }
		   }
		});	
	
} 

