
jQuery(document).ready(function()
{
     
    jQuery('a[rel*=facebox]').facebox();
            
	jQuery("#show_enquiryForm").livequery('click', function(e) 
	{
		jQuery.facebox(function()
		{
			//action="<?php bloginfo('template_url'); ?>/custom_php/enquiry_form_email.php
			jQuery.facebox("<div><form method=\"post\" id=\"myForm\"><div id=\"dialog2\" class=\"window\" style=\"display:block\">"+
			"<h2>To find out more about Microwind fill out the form below.</h2>"+
			"<table>"+
			"<tr>"+
			"<td>"+
				"<table>"+
				"<tr>"+
				"<input type=\"hidden\" size=\"25\" value=\"<?php the_title(); ?>\" name=\"page_title\">"+
				"<td>Name: <b id=\"MynameStar\" class=\"compulsory\">*</b></td><td><input type=\"text\" size=\"25\" name=\"name\" id=\"Myname\"></td>"+
				"</tr>"+
				"<tr>"+
				"<td>Email: <b id=\"MyemailStar\"  class=\"compulsory\">*</b></td><td><input type=\"text\" size=\"25\" name=\"recipient\" id=\"Myemail\"></td>"+
				"</tr>"+
				"<tr>"+
				"<td>Postcode:</td><td><input type=\"text\" size=\"25\" name=\"postcode\" id=\"Mypostcode\"></td>"+
				"</tr>"+
				"<tr>"+
				"<td>Phone:</td><td><input type=\"text\" size=\"25\" name=\"phone\" id=\"Mytelephone\"></td>"+
				"</tr>"+
				"<tr>"+
				"<td>Enquiry: <b id=\"MymessageStar\" class=\"compulsory\">*</b></td><td><textarea cols=\"19\" rows=\"5\" name=\"text\" id=\"Mytext\"></textarea></td>"+
				"</tr>"+
				"<tr>"+
				"<td colspan=\"2\" align=\"right\"><input type=\"button\" id=\"make_enquiry_next\" value=\"Make Enquiry\"></td>"+
				"</tr>"+
				"</table>"+
			"</td>"+
			"<td valign=\"top\">"+
				"<table>"+
				"<tr>"+
				"<td>or call one of our renewable enrgy experts"+
					"07843 615 139</td>"+
				"</tr>"+
				"</table>"+

			"</td>"+
			"</tr>"+
			"</table>"+
			 "<center><div id=\"MyerrorMessage\">* Please fill in all fields with *</div></center>"+
			"</div>"+
			"<!-- Mask to cover the whole screen -->"+
			  "<div id=\"mask\"></div>"+
			"</div>");
		});
	});


	
	jQuery("#make_enquiry_next").livequery('click', function(e) 
	{
		var name = jQuery("#Myname").val();
		var email = jQuery("#Myemail").val();
		var postcode = jQuery("#Mypostcode").val();
		var telephone = jQuery("#Mytelephone").val();
		var query = jQuery("#Mytext").val();

		if (validEnquiry(name,email,query))
		{
			jQuery.ajax({
				type: 		"POST",
				url:  		"",
				dataType: 	"html",
				cache: 		false,
				data:		"name="+name+"&email="+email+"&postcode="+postcode+"&telephone="+telephone+"&text="+text,
				success:	function(result){
									jQuery.facebox(result);
							}
			});
		}else
		{
			
		}
	});
	
	 jQuery('#myForm').ajaxForm({ 
	 beforeSubmit: validate, //validate before submitting
	 success: function(result) 
	 {

		// check the result returned by the server
		if (result == "OK2")
		{
			var name = jQuery("#name").val();
			var email = jQuery("#email").val();
			var postcode = jQuery("#postcode").val();
			var telephone = jQuery("#telephone").val();
			
			var div = "Name: "+name+"<br/>";
			div +=  "Telephone: "+telephone+"<br/>";
			div +=  "Postcode: "+postcode+"<br/>";
			div += "Email: "+email+"<br/>";

			// keep original data of popup for resetting
			originalContent = jQuery("#dialog").html();

			// update view
			//jQuery("#dialog").html("<div id=\"exitModal\"><input type=\"button\" id=\"close_modal_popup\" value=\"X\"></div> <div id=\"enquirytop\">Thanks for your enquiry in to <Energy Type> we will be in touch shortly.Cavity Wall Promo Video from the Mark Group If you are looking at making your house more energy efficent one of the most effective ways you can do this is through insulating your home to find out more watch the short video below.</div>");
			//jQuery("#dialog").append("<div id=\"enquiryright\"><img src=\"http://www.fuellesspower.com/paypal/WindMillBlade.jpg\"></div>");
			//jQuery("#dialog").append("<div id=\"enquiryleft\">Find Out About Insulation If you would like the Mark Group to contact you about home insulation please click below. <br/><br/>"+div+" <br/><div class=\"findoutmorebutton\"><input type=\"button\" value=\"Find out more\" id=\"go_find_out_more\"></div>");
			
			jQuery.facebox("<div id=\"exitModal\"><input type=\"button\" id=\"close_modal_popup\" value=\"X\"></div> <div id=\"enquirytop\">Thanks for your enquiry in to <Energy Type> we will be in touch shortly.Cavity Wall Promo Video from the Mark Group If you are looking at making your house more energy efficent one of the most effective ways you can do this is through insulating your home to find out more watch the short video below.</div>"+
			"<div id=\"enquiryright\"><img src=\"http://www.fuellesspower.com/paypal/WindMillBlade.jpg\"></div>"+
			"<div id=\"enquiryleft\">Find Out About Insulation If you would like the Mark Group to contact you about home insulation please click below. <br/><br/>"+div+" <br/><div class=\"findoutmorebutton\"><input type=\"button\" value=\"Find out more\" id=\"go_find_out_more\"></div>");
			
			
		}else
		{
			// an error has occured, display an error message to the user
			jQuery.facebox("An error has occurred and your query cannot be processed, please use Contact Us to get in touch with us");
		}
           
      }  

	 }); 
	 
	function ShowRequest(formData, jqForm, options) 
	 {	
		}
	
	jQuery("#close_modal_popup").livequery('click', function(e) 
	{

			//Cancel the link behavior
			e.preventDefault();
			
			jQuery('#mask').hide();
			jQuery('.window').hide();
	});
	
			
	/* MODULAR POPUP*/
	//select all the a tag with name equal to modal
	jQuery('a[name=modal]').click(function(e) {
	
			if (originalContent != "")
		{
			jQuery("#dialog").html(originalContent);
		}
	
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = jQuery(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		jQuery('#mask').fadeIn(1000);	
		jQuery('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
              
		//Set the popup window to center
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
	
		//transition effect
		jQuery(id).fadeIn(2000); 
	
	});
	

	
	jQuery('#show_enquiry_form').click(function(e) {
		

		
	if (originalContent != "")
	{
		jQuery("#dialog").html(originalContent);
	}
		
	jQuery("#dialog").css("display", "block"); 
	//Cancel the link behavior
	e.preventDefault();
		
	//Get the A tag
	var id = jQuery(this).attr('href');
	
	//Get the screen height and width
	var maskHeight = jQuery(document).height();
	var maskWidth = jQuery(window).width();
	
	//Set heigth and width to mask to fill up the whole screen
	jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
	//transition effect		
	jQuery('#mask').fadeIn(1000);	
	jQuery('#mask').fadeTo("slow",0.8);	
	
	//Get the window height and width
	var winH = jQuery(window).height();
	var winW = jQuery(window).width();
              
	//Set the popup window to center
	jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
	jQuery(id).css('left', winW/2-jQuery(id).width()/2);
	
	//transition effect
	jQuery(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	jQuery('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		jQuery('#mask').hide();
		jQuery('.window').hide();
	});		
	
	//if mask is clicked
	jQuery('#mask').click(function () {

		jQuery(this).hide();
		jQuery('.window').hide();
	});		

	//if make enquir button is clicked
	//if mask is clicked
	jQuery('#make_enquiry').livequery('click', function(event) 
	{
		var name = jQuery("#name").val();
		var email = jQuery("#email").val();
		var postcode = jQuery("#postcode").val();
		var telephone = jQuery("#telephone").val();
		
		var div = "Name: "+name+"<br/>";
		div +=  "Telephone: "+telephone+"<br/>";
		div +=  "Postcode: "+postcode+"<br/>";
		div += "Email: "+email+"<br/>";

		// keep original data of popup for resetting
		originalContent = jQuery("#dialog").html();
		
		//alert(div);
		//jQuery("#dialog").html(div);
		jQuery("#dialog").html("<div id=\"enquirytop\">Thanks for your enquiry in to <Energy Type> we will be in touch shortly.Cavity Wall Promo Video from the Mark Group If you are looking at making your house more energy efficent one of the most effective ways you can do this is through insulating your home to find out more watch the short video below.</div>");
		jQuery("#dialog").append("<div id=\"enquiryright\"><img src=\"http://www.fuellesspower.com/paypal/WindMillBlade.jpg\"></div>");
		jQuery("#dialog").append("<div id=\"enquiryleft\">Find Out About Insulation If you would like the Mark Group to contact you about home insulation please click below. <br/><br/>"+div+" <br/><div class=\"findoutmorebutton\"><input type=\"button\" value=\"Find out more\" id=\"go_find_out_more\"></div>");
		
	});
	
	var originalContent = "";



	jQuery('#go_find_out_more').livequery('click', function(event) 
	{
		jQuery("#dialog").html("<h2>Cheers mate!</h2>");
	});
	
	
	/**
	* Check if valid email address
	*/
	function isValidEmail(str)
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		{
			testresults=true;
		}
		else
		{
			testresults=false;
		}
			return (testresults);
	}
	
	/**
	* Displaying errors for validation of enquiry form
	*/
	function showErrorMessage(error)
	{
		jQuery("#MyerrorMessage").html(error);
	}
	
	/**
	* Validation for the enquiry form
	*/
	function validate(formData, jqForm, options) { 
		 var form = jqForm[0]; 
		 var name = form.name.value;
		 var email = form.email.value;
		 var message = form.text.value;
		 
		 showCompulsoryStar(""); // hide all compulsory stars

		 // check name, email and message
		 if (name == "")
		 {
			showCompulsoryStar("name");
			showErrorMessage("Please enter your name!");
			return false;
		 }else
		  if(!isValidEmail(email))
		 {
			showCompulsoryStar("email");
			showErrorMessage("Please enter a valid email address!");
			return false;
		 }else
		 if (message == "")
		 {
			showCompulsoryStar("message");
		 	showErrorMessage("Please enter your query in the message box!");
			return false;
		 }
		
	}
	
		/**
	* Validation for the enquiry form
	*/
	function validEnquiry(name,email,message) { 
	
	//alert("name:" + name + "email:" + email + " message: " + message);

		 showCompulsoryStar(""); // hide all compulsory stars

		 // check name, email and message
		 if (name == "")
		 {
			showCompulsoryStar("name");
			showErrorMessage("Please enter your name!");
			return false;
		 }else
		  if(!isValidEmail(email))
		 {
			showCompulsoryStar("email");
			showErrorMessage("Please enter a valid email address!");
			return false;
		 }else
		 if (message == "")
		 {
			showCompulsoryStar("message");
		 	showErrorMessage("Please enter your query in the message box!");
			return false;
		 }
		 
		 return true;
		
	}
	
	/**
	* Shows/Hides the compulsory field red stars to warn user to complete them
	*/
	function showCompulsoryStar(st)
	{
		jQuery("#MynameStar").hide();
		jQuery("#MyemailStar").hide();
		jQuery("#MymessageStar").hide();
		if (st != "")
			jQuery("#My"+st+"Star").show();
	}
	
	jQuery('.main_menu').livequery('click', function(event) 
	{
		alert("replace");
	});
	
	//jQuery("#feedincontent").countdown({until: liftoffTime, format: 'dHM'});
	
	//$('#tabs').countdown({ until: new Date(2012, 8 - 1, 8)}); 
	//$('#tabs').countdown({ until: jQuery('#tabs')..countdown.UTCDate(+10, 2012, 1 - 1, 26)}); 
	//$('#tabs').countdown({until: +600}); 
	//$('#tabs').countdown({until: '+1m -1d'});
	
	var shortly = new Date(2010, 3, 1); 
	//shortly.setSeconds(shortly.getSeconds() + 5.5);

	//	jQuery('#feedintariff_countdown').countdown({layout:'<li><p>{dn}</p></li>'+ 
   //'<li class="formatted"><p id="hours_box">{hn}</p></li> <li class="formatted"><p id="mins_box">{mn}</p></li> <li class="formatted"><p id="secs_box">{sn}</p></li>'});
	
	//jQuery(#feedintariff_countdown).countdown({layout:'<b>{d<}{dn} {dl} and {d>}'+ 
    //'{hn} {hl}, {mn} {ml}, {sn} {sl}</b>'});
	jQuery('#feedintariff_countdown').countdown({until: shortly});	

		
	
});


