var focus_leftSize = 116;
var isgood_leftSize = 116;
var error_leftSize = 116;





$(function(){
		   
		   
		//   $('checkbox').highlight('label', 'green', 'focus', 'blur')
//				$('form').highlight('div', 'grey', 'focus', 'blur')
		   
// $("#design").click(function(){					 
// if (this.checked){$("#design").css({"background":"#00CC00"})
// }	else { $("#design").css({"background":"none"})}					   
// })
 
//  $("#hosting").click(function(){
// if (this.checked){$("#hosting").css({"background":"#00CC00"})}	else {$("#hosting").css({"background":"none"})}						   
// })
 
 //  $("#domain").click(function(){
// if (this.checked){$("#domain").css({"background":"#00CC00"})}	else //{$("#domain").css({"background":"none"})}						   
// })
   
//    $("#ecommerce").click(function(){
//if (this.checked){$("#ecommerce").css({"background":"#00CC00"})}	else {$("#ecommerce").css({"background":"none"})}						   
//})
 


// Grab each form element
				$("label[title]").each(function(){


												
						//if ($('.checkbox').is(':checked')){$(this).addClass("isgood");}	else {$(this).removeClass("isgood");}					
					$(this).append("<div class=\"infopop\">");	
					titletext = $(this).attr("title");
					$(this).removeAttr("title");
					$(".infopop",this).css({opacity:0}).html(titletext);
					
					
					$("textarea",this).focus(function(){   doFocusText(this);}).blur(function(){doBlurText(this);	});
					
					
					$("input",this).focus(function(){

											   
						// Mouseover
						doFocus(this);
					}).blur(function(){
						// MouseOut
						doBlur(this);
					});
				});
			});

			function doFocus(obj) {
				$(obj).addClass("active").parents("label").addClass("active").find(".infopop").animate({opacity:1,left:focus_leftSize},300);
			}
			
			function doFocusText(obj) {
				$(obj).addClass("active").parents("label").addClass("active").find(".infotext").animate({opacity:1,left:focus_leftSize},300);
			}
			function doBlurText(obj) {$(obj).removeClass("error").removeClass("active").parents("label").removeClass("error").removeClass("active").find(".infotext").removeClass("errorpop") }
		
			
			function doBlur(obj) {
				if (validate(obj)) {
					isGood(obj);
				}
			}
			
			function reportErr(obj, message) {
				$(obj).addClass("error").parents("label").removeClass("isgood").addClass("required").addClass("error").find(".infopop").html(message).addClass("errorpop").animate({opacity:1,left:error_leftSize},300);
			}
			
			function isGood(obj) {
				$(obj).removeClass("error").removeClass("active").parents("label").addClass("isgood").removeClass("error").removeClass("active").find(".infopop").removeClass("errorpop").animate({opacity:0,left:isgood_leftSize},300);
			} 	

			function validate(obj) {
			
		
				// Extend jQuery object to include Regular expression masks assigned to properties
				mask = jQuery.extend({pcfieldmask: /^[a-z0-9\.\s-]{5,}$/i,textfieldmask: /^[a-z\.\s-]{5,}$/i,phonemask: /^[0-9\(\)\+\.\s-]{8,}$/i,passwordmask: /^[a-z0-9\.\s-]{5,}$/, emailmask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/});
				// Extend jQuery object to include error messages assigned to properties
				errmsg = jQuery.extend({textfielderr:"Enter 5 or more letters",phoneerr: "Include dialling code",passworderr:"Code must match "+captcha_code,emailerr:"Invalid address",matcherr: "Code must match "+captcha_code});
			
				// Set up variables to hold details of which mask to use and whether the field should match another field
				var masktouse = null;
				var mustmatch = null;
				// Determine the type of mask we're going to validate against
				
				switch(obj.name) {
					case "design": 		masktouse=""; 			errtouse=""; 	break;
					case "name": 		masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "business": 	masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "address": 	masktouse="pcfieldmask"; 		errtouse="textfielderr"; 	break;
					case "town": 		masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "county": 		masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "postcode": 	masktouse="pcfieldmask"; 		errtouse="textfielderr"; 	break;
					
					case "phone": 		masktouse="phonemask"; 			errtouse="phoneerr"; 		break;
					case "username": 	masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "email": 		masktouse="emailmask"; 			errtouse="emailerr"; 		break;
					case "password": 	masktouse="passwordmask"; 		errtouse="passworderr"; 	mustmatch="verpassword"; 	break;
					case "captcha": 	masktouse="passwordmask"; 		errtouse="passworderr"; 	mustmatch=captcha_code; 	break;
				}
				// Check that the element is a required field before validating against it.
				
				
			//	if($(obj).parents("label").hasClass("checkbox")){
				
			//	value =$(obj) //.parents("value")
				
		//		alert("CHECKED"+value.val());
				//if($(obj).parents("value").is(':checked')) {alert("CHECKED ok");} 
				
		//		}
				if($(obj).parents("label").hasClass("required") && masktouse) {
					// Set up a quick way of accessing the object we're validating
					pointer = $(obj);
					
					// Test the value of the field against the Regular Expression
					if (mask[masktouse].test(pointer.val())) {
						// The field validated successfully!
						
						// Check to see if the field needs to match another field in the form
						if (mustmatch) {
							// It does need to match, so grab the object it needs to match
							matchobj = $("#"+mustmatch);
														 
							if (matchobj.val()!='' && matchobj.val()!=pointer.val()) {
								// The fields don't match, so report an error on both of them
								reportErr(obj,errmsg["matcherr"]);	
								reportErr(matchobj,errmsg["matcherr"]);
							}
							else {
								// Either the fields match, or the other field hasn't been completed yet
								// If the other field has been completed, call the isGood function to clear any error message showing
								if (matchobj.val()!='') { isGood(matchobj);}
								return true;
							}
						}
						else {
							// No match is required, so return true - validation passed!
							return true;
						} 
					}
					else { 
						// The field failed to validate against the Regular Expression
						reportErr(obj,errmsg[errtouse]);
						return false; 
					}
				} 
				else {	
					// This isn't a required field, so we won't validate it against anything			
					return true;
				}
			}
			
function validate_form (){

if (document.quote.name.value == "") 
	{ 
	alert_display("Please enter a contact name.");    
    document.quote.name.focus();
    return false ;
	}
	
if (document.quote.business.value == "") 
	{ 
	alert_display("Please enter a business name.");    
    document.quote.business.focus();
    return false ;
	}
	
if (document.quote.address.value == "") 
	{ 
	alert_display("Please enter an address.");    
    document.quote.address.focus();
    return false ;
	}	

if (document.quote.town.value == "") 
	{ 
	alert_display("Please enter a town.");    
    document.quote.town.focus();
    return false ;
	}	
	
if (document.quote.postcode.value == "") 
	{ 
	alert_display("Please enter a Post Code.");    
    document.quote.postcode.focus();
    return false ;
	}	

if (document.quote.email.value == "") 
	{ 
	alert_display("Please enter your email address.");    
    document.quote.email.focus();
    return false ;
	}
	
if (document.quote.phone.value == "") 
	{ 
	alert_display("Please enter a contact telephone number.");    
    document.quote.phone.focus();
    return false ;
	}	
if (document.quote.captcha.value == "") 
	{ 
	alert_display("Please enter the code.");    
    document.quote.captcha.focus();
    return false ;
	}
   
   
   
   return true;
}


function alert_display(msg){alert(msg); return;}

var rand_no = Math.ceil(5*Math.random())
//var captacha = 'captchas/'+rand_no+'.gif';
var captacha = "<img src='captchas/"+rand_no+".gif'>";
//alert(rand_no);
if (rand_no == 1){var captcha_code = "H59L2";}
if (rand_no == 2){var captcha_code = "U64K1";}
if (rand_no == 3){var captcha_code = "1ARH8";}
if (rand_no == 4){var captcha_code = "T3FQ9";}
if (rand_no == 5){var captcha_code = "67DE7";}

function highlight(box, obj)
{
var color1 = '#00CC00';
var color2 = '#DBF9C7';

document.getElementById(obj).style.background = (box.checked ? color1 : color2);	


}
