$(document).ready(function(){
	$(".rollover-jpg").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(){
	function findValueCallback(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?'>)/gi, '');
	}
	
	$("#keywords").autocomplete('/suggest.php', {
		max: 50, 
		multiple: false,
		matchContains: true,
		//formatItem: formatItem,
		formatResult: formatResult
	});
	$("#srcfrm").attr("autocomplete", "off");
	$("#btn1").attr("rel","div.overlayRed2");
	$("#btn2").attr("rel","div.overlayRed");
});



$(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(){
	$(".rollover-png").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 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();
	var address_id = $("#address_id").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+"&address_id="+address_id,
		   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('');
					$("#address_id").val('0')
				   
				   	$("#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 sendForm2() {
	var name = $("#fname").val();
	var address1 = $("#faddress1").val();
	var address2 = $("#faddress2").val();
	var postcode = $("#fpostcode").val();
	var telephone = $("#ftelephone").val();
	var faoi = $("#faoi").val();
	var faes = $("#faes").val();
	
	if (!name || name == '') {
		alert('Please enter your name');
		return false;
	}
	
	
	if (!postcode || postcode == '') {
		alert('Please enter postcode');
		return false;
	}
	
	if (name && postcode) {
		$.ajax({
		   type: "POST",
		   url: "/sendform2.php",
		   data: "name="+name+"&telephone="+telephone+"&faoi="+faoi+"&address1="+address1+"&address2="+address2+"&postcode="+postcode+"&faes="+faes,
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   	$("#name").val('');
				   	//$("#email").val('');
				   	$("#telephone").val('');
				   	$("#faes").val('');
				   	$("#address1").val('');
				   	$("#address2").val('');
				   	$("#postcode").val('');
				   
				   	$("#wrap").html('<br><p>Thank you for your request, someone will respond in due course</p>');
			   } else if (data == 'ERROR') {
				   	$("#wrap").html('<br><p>Sorry, we cannot complete your request.\nPlease try again.</p>');
			   } else {
				  	$("#wrap").html(data);
			   }
		   }
		});	
	} 
}

function sendForm3() {
	var name = $("#cfname").val();
	var address1 = $("#cfaddress1").val();
	var address2 = $("#cfaddress2").val();
	var postcode = $("#cfpostcode").val();
	var telephone = $("#cftelephone").val();
	var faoi = $("#cfaoi").val();
	var faes = $("#cfaes").val();
	
	if (!name || name == '') {
		alert('Please enter your name');
		return false;
	}
	
	
	if (!postcode || postcode == '') {
		alert('Please enter postcode');
		return false;
	}
	
	if (name && postcode) {
		$.ajax({
		   type: "POST",
		   url: "/sendform2.php",
		   data: "name="+name+"&telephone="+telephone+"&faoi="+faoi+"&address1="+address1+"&address2="+address2+"&postcode="+postcode+"&faes="+faes,
		   error: function(){
				alert('Sorry, we cannot complete your request.\nPlease try again.');
		   },
		   success: function(data){
			   if (data == "OK") {
				   	$("#cfname").val('');
				   	//$("#email").val('');
				   	$("#cftelephone").val('');
				   	$("#cfaes").val('');
				   	$("#cfaddress1").val('');
				   	$("#cfaddress2").val('');
				   	$("#cfpostcode").val('');
				   
				   	$(".overlayRed #wrap").html('<br><p>Thank you for your request, someone will respond in due course</p>');
			   } else if (data == 'ERROR') {
				   	$(".overlayRed #wrap").html('<br><p>Sorry, we cannot complete your request.\nPlease try again.</p>');
			   } else {
				  	$(".overlayRed #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;
   }
}

function launchVR() {
			$(".overlaybutton2").overlay({
				// custom expose settings
				expose: {
					color: '#000000',
					opacity: 0.4,
					closeSpeed: 1000
				},
				onBeforeLoad: function() { 
					$("#vr_1").click();
				} 
			});
		}
		
		
function launchFS() {
			$(".overlaybutton").overlay({
				// custom expose settings
				expose: {
					color: '#000000',
					opacity: 0.4,
					closeSpeed: 1000
				},
				onBeforeLoad: function() { 
					$("#vr_1").click();
				} 
			});
		}
		
		
		
$(document).ready(function(){
	$('a.ea').each(function(){
		e = this.rel.replace('$$','@');
		this.href = 'mailto:' + e;
		$(this).text(e);
	});
	$('a.ea2').each(function(){
		e = this.rel.replace('$$','@');
		this.href = 'mailto:' + e;
	});	
	//getStatues('GreenEnergyCare', 2);
});
	
function getStatues(username, maxItems)	{
	$.get("/getStatues.php",  function(xml){
		var i = 0;
		//var delay = 2500;
		$('item',xml).each(function(i){
			if (i < maxItems) {
				var title = $(this).find("title").text();
				title = title.substring(0, 200)+'...';
				title = title.replace(username+': ','');
				var pubDate = $(this).find("pubDate").text();
				var url = $(this).find("link").text();
				$("#twitter").append("<li><a href=\""+url+"\" target=_blank>"+title+"</a><span>"+pubDate+"</span></li>");
				i++;
			}
		});
		/*$('#twitter').cycle({ 
			fx:     'scrollRight', 
			timeout: 4000, 
			easing:  'backinout',
			next:   '#twitter',
			pause:   0,
			cleartype:  true,
    		cleartypeNoBg:  true
		});*/
	});	 
}