function externalLinks(){if(!document.getElementsByTagName){return }var C=document.getElementsByTagName("a");for(var B=0;B<C.length;B++){var A=C[B];if(A.getAttribute("href")&&A.getAttribute("rel")=="external"){A.target="_blank"}}}$(document).ready(function () { externalLinks() });

$(function(){
	$('.hi-five').click(function(e){
		var type = $(this).attr('rel');
		$('#fi-overlay').fadeIn(function(){
			$('#fi-content').fadeIn();
			$.post("inc/switch.php", { 'type' : type}, function(data){
				$('#popupcontainer').html(data);
			});
		});
		e.preventDefault();
	});
	
	$('.hi-five-close').click(function(e){
		$('#fi-content').fadeOut(function(){
			$('#fi-overlay').hide();
			$('#popupcontainer').html('');
		});
		e.preventDefault();
	});	   
		   
	$('div#navigation ul li').hover(function(){
		$(this).addClass('nav-over');
		}, function() {
		$(this).removeClass('nav-over');
	});
	
	function checkRegexp(o,regexp,n){
		if(!(regexp.test(o.val()))){
			o.addClass("ui-state-error");
			updateTips(n);
			return false;
		}else{
			return true;
		}
	}
	
	function updateTips(t){
		tips
		.text(t)
		.addClass("ui-state-highlight");
		setTimeout(function(){
			tips.removeClass("ui-state-highlight", 1500);
		}, 500);
	}
	
	function checkLength(o, n) {
		if (o.val() == null || o.val().length < 1){
			o.addClass("ui-state-error");
			updateTips(n + " is required");
			return false;
		}else{
			return true;
		}
	}
	
	
	
	/** QUOTE REQUEST FORM **/
	
	var name = $("#name"),
		phone = $("#phone"),
		email = $("#email"),
		postcode = $("#postcode"),
		information = $("#information"),
		allFields = $([]).add(name).add(email),
		tips = $(".validateTips");

	function postForm(){	
		$.post("inc/postform.php",{ name: name.val(), phone: phone.val(), email: email.val(), postcode: postcode.val(), information: information.val() },function(){
			$('#dialog').dialog("close");
			window.location = "http://www.cleanandrestore.co.uk/thank-you/?request=quote"
		});
	}

	$("#dialog").dialog({
			autoOpen: false,
			height: 420,
			width: 500,
			modal: true,
			buttons: {
				"Request a quote": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength(name, "Name");
					bValid = bValid && checkLength(phone, "Phone");
					bValid = bValid && checkLength(email, "Email");
					bValid = bValid && checkLength(postcode, "Post code");
					bValid = bValid && checkLength(information, "Information");

					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please enter a valid e-mail address (eg. office@cleanandrestore.co.uk)" );

					if ( bValid ) {
						// POST DATA
						postForm();
					}
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			},
			close: function(){
				allFields.val("").removeClass("ui-state-error");
			}
		});

		$("#request-quote")
			.click(function(e){
			$("#dialog").dialog("open");
			e.preventDefault();
		});
			
			
		/** STAIN GUIDE REQUEST FORM **/
		
		var name2 = $("#name2"),
		phone2 = $("#phone2"),
		email2 = $("#email2"),
		location = $("#location"),
		allFields = $([]).add(name2).add(email2),
		tips = $(".validateTips");
		
		function postStainForm(){	
			$.post("inc/postform.php",{ name: name2.val(), phone: phone2.val(), email: email2.val(), location: location.val() },function(){
				$('#stain-guide-dialog').dialog("close");
				window.location = "http://www.cleanandrestore.co.uk/thank-you/?request=stain"
				//alert("Your request for a FREE stain removal guide has successfully been sent");
			});
		}
			
		$("#stain-guide-dialog").dialog({
			autoOpen: false,
			height: 420,
			width: 500,
			modal: true,
			buttons: {
				"Request a FREE stain guide": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength(name2, "Name");
					bValid = bValid && checkLength(phone2, "Phone");
					bValid = bValid && checkLength(email2, "Email");
					bValid = bValid && checkLength(location, "Location");

					bValid = bValid && checkRegexp( email2, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please enter a valid e-mail address (eg. office@cleanandrestore.co.uk)" );

					if ( bValid ) {
						// POST DATA
						postStainForm();
					}
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			},
			close: function(){
				allFields.val("").removeClass("ui-state-error");
			}
		});	
			
		$("#stainGuide").click(function(e){
			$("#stain-guide-dialog").dialog("open");
			e.preventDefault();
		});
		
		
		/*** FREE BOTTLE OF SPOTTER REQUEST FORM ***/
		
		var name3 = $("#name3"),
		phone3 = $("#phone3"),
		email3 = $("#email3"),
		address = $("#address"),
		postcode2 = $("#postcode2"),
		allFields = $([]).add(name3).add(email3),
		tips = $(".validateTips");
		
		function postSpotterForm(){	
			$.post("inc/postform.php",{ name: name3.val(), phone: phone3.val(), email: email3.val(), address: address.val(), postcode: postcode2.val() },function(){
				$('#spotter-dialog').dialog("close");
				window.location = "http://www.cleanandrestore.co.uk/thank-you/?request=spotter"
				//alert("Your request for a FREE bottle of spotter has successfully been sent");
			});
		}
			
		$("#spotter-dialog").dialog({
			autoOpen: false,
			height: 420,
			width: 500,
			modal: true,
			buttons: {
				"Request a FREE bottle of spotter": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength(name3, "Name");
					bValid = bValid && checkLength(phone3, "Phone");
					bValid = bValid && checkLength(email3, "Email");
					bValid = bValid && checkLength(address, "Address");
					bValid = bValid && checkLength(postcode2, "Postcode");
		

					bValid = bValid && checkRegexp( email3, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please enter a valid e-mail address (eg. office@cleanandrestore.co.uk)" );

					if ( bValid ) {
						// POST DATA
						postSpotterForm();
					}
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			},
			close: function(){
				allFields.val("").removeClass("ui-state-error");
			}
		});	
			
		$("#spotter").click(function(e){
			$("#spotter-dialog").dialog("open");
			e.preventDefault();
		});
			
			

});
