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

$(document).ready(function() {
	$("#source").change(function() {
		var x = $(this).val();
		if (x == 'other') {
			$("#other_det").val('').show();	
		} else {
			$("#other_det").val('').hide();	
		}
	});
});


function sendForm() {
	var name = $("#name").val();
	var email = $("#email").val();
	var estabilishment = $("#estabilishment").val();
	var phone = $("#phone").val();
	
	var address1 = $("#address1").val();
	var address2 = $("#address2").val();
	var address3 = $("#address3").val();
	var county = $("#county").val();
	var postcode = $("#postcode").val();
	var children_on_roll = $("#children_on_roll").val();
	
	var heard_before = $("input[@name='heard_before']:checked").val();
	var source = $("#source").val();
	var other_det = $("#other_det").val();
	
	//alert(heard_before);
	
	var a1 = $("#agree1").attr('checked');
	var a2 = $("#agree2").attr('checked');
	var a4 = $("#agree4").attr('checked');
	var a3 = $("#agree3").attr('checked');
	if (a1 == true) {	var agree1 = 1;	}  else {var agree1 = 0;	}
	if (a2 == true) {	var agree2 = 1;	}  else {var agree2 = 0;	}
	if (a3 == true) {	var agree3 = 1;	}  else {var agree3 = 0;	}
	if (a4 == true) {	var agree4 = 1;	}  else {var agree4 = 0;	}
	
	if (name=='') 		{ alert('Please enter you name'); return false; }
	if (validateEmail(email) == false) 		{ alert('Please enter valid email address'); return false; }
	if (estabilishment=='') 		{ alert('Please enter estabilishment'); return false; }
	if (postcode=='') 		{ alert('Please enter postcode'); return false; }
	
	
	if (source == '0') {
		alert('Please, tell us where did you hear about us'); return false;
	}
	
	if (source == 'other' && other_det == '') {
		alert('Please specify where exactyly did you hear about us'); return false;	
	}
	
	
	
	input_box = confirm("Is all information correct?");	
	if (input_box == true)	{
		$.ajax({
		   type: "POST",
		   url: "/send_form.php",
		   data: "name="+name+"&email="+email+"&estabilishment="+estabilishment+"&address1="+address1+"&address2="+address2+"&address3="+address3+"&county="+county+"&postcode="+postcode+"&phone="+phone+"&heard_before="+heard_before+"&children_on_roll="+children_on_roll+"&agree1="+agree1+"&agree2="+agree2+"&agree3="+agree3+"&agree4="+agree4+"&source="+source+"&other_det="+other_det,
				 
		   // on success
		   error: function(){
				alert('ooops! - nasty error. Please try again.')
		   },
		   success: function(data){
			    if (data == 'OK') {
					$("#name").val('');
					$("#email").val('');
					$("#estabilishment").val('');
					$("#phone").val('');
					$("#children_on_roll").val('');
					
					$("#address1").val('');
					$("#address2").val('');
					$("#address3").val('');
					$("#county").val('');
					$("#postcode").val('');
					
					$("#source").val('0');
					$("#other_det").val('');
					
					$("#agree1").attr('checked',false);
					$("#agree2").attr('checked',false);
					$("#agree3").attr('checked',false);
					$("#agree4").attr('checked',false);
					$(".reg-boxes").hide();
					
					$("#register_form").hide();
					$("#dialog").addClass('reg_ok');
					
					$(".reg-result").fadeIn();
					
				} else {
			   		alert('ooops! - nasty error. Please try again.');
		   		}
		   } 
		});
		return true;
	}
	else {	
		return false; 	
	}
}

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 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(){ 
	if($.browser.msie){
		// don't use this crap
	} else {
		$("img.rm").fadeTo("fast", 0.7);  
		$("img.rm").hover(function(){  
			$(this).fadeTo("slow", 1.0);  
		},function(){  
			$(this).fadeTo("slow", 0.7);  
		});  
	}
}); 

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

$().ready(function() {
	  $('#dialog').jqm();
	});  
