/**********************************************************************************************
IE6 Drop down menu fix 
***********************************************************************************************/

sfHover = function() {
	var sfEls = document.getElementById("main-navigation").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/** for input buttons **/
inputActive = function() {
	var inputEls = document.getElementsByTagName("input");
	for (var i=0; i<inputEls.length; i++) {
		inputEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		inputEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
		inputEls[i].onmousedown=function() {
			this.className+=" active";
			this.className=this.className.replace(new RegExp(" hover\\b"), "");			
		}
		inputEls[i].onmouseup=function() {
			this.className=this.className.replace(new RegExp(" active\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", inputActive);


/**********************************************************************************************
Form validation (requires jQuery)
***********************************************************************************************/

// error message text
var emailMissing = "Please enter your email address";
var emailInvalid = "Please enter a valid email address";

var postcodeMissing = "Please enter your postcode";
var postcodeInvalid = "Postcode must be 4 numbers";

/*******************************
onFocus field clearers
********************************/
$(document).ready(function(){
						   
/** Email **/
/*	$('.email input').focus(function(){
				if($(this).val() == emailMissing || $(this).val() == emailInvalid){
					$(this).val('');
				}
	});*/
	
/** Postcode **/
/*	$('.postcode input').focus(function(){
				if($(this).val() == postcodeMissing || $(this).val() == postcodeInvalid){
					$(this).val('');
				}
	});*/

});



// function to validate the form fields
function validate(){
var alertChecks = false;
var i = 0;

$('span.formerror').css("display", "none");
$('.form .form-item input').removeClass("error");
$('.form .form-item select').removeClass("error");

while(i < $('.form .form-item').length) {
	
formItem = $('.form-item:eq('+i+')');

if(formItem.hasClass('email')){
	
/*******************************
Email validation
********************************/

		var email = $('.form .form-item:eq('+i+') input');
		
		//if email is required or is not empty, then check if it is empty(if required) and validate it
		if(formItem.hasClass("required") || email.val() != ''){
			if(alertChecks){ alert("checking email on form item "+i+".."); }
			
			//check if email has missing error msg or if it is empty
			if(email.val() == ''){
				email.addClass("error");
				$('<span class="formerror">'+emailMissing+'</span>').insertAfter(email);
				return false;
			}
		
			//check if email is valid
			var emailCheck = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test(email.val());
			if (!emailCheck) {
				email.addClass("error");
				$('<span class="formerror">'+emailInvalid+'</span>').insertAfter(email);
				return false;
			}
			
			if(alertChecks){ alert("checked email on form item "+i); }
			
		}
		
} else if(formItem.hasClass('postcode')) {
/*******************************
Postcode validation
********************************/


		var postcode = $('.form .form-item:eq('+i+') input');
		
		//if email is required or is not empty, then check if it is empty(if required) and validate it
		if($(formItem).hasClass("required") || postcode.val() != ''){
			if(alertChecks){ alert("checking post on form item "+i+".."); }
			
			//check if email has missing error msg or if it is empty
			if(postcode.val() == ''){
				postcode.addClass("error");
				$('<span class="formerror">'+postcodeMissing+'</span>').insertAfter(postcode);
				return false;
			}
			
			//check if it is only numbers
			var postCheck = /^\d{4}$/.test(postcode.val());
			if (!postCheck) {
				postcode.addClass("error");
				$('<span class="formerror">'+postcodeInvalid+'</span>').insertAfter(postcode);
				return false;
			}
			
			if(alertChecks){ alert("checked post on form item "+i); }
			
		}
		
}

if(alertChecks){ alert("item "+i+" checked"); }
i++
if(i >= $('.form .form-item').length){
	return true;
}//end length check

}//end while loop 

	
}//end function



$(document).ready(function() {

    try {
        if ($('#gallery-preview ul li a, #gallery-preview ul li a')) {
            //add names onto the gallery links
            $('#gallery-preview ul li a, #gallery-preview ul li a').addClass('thickbox');
            $('#gallery-preview ul li a, #gallery-preview ul li a').attr('rel', 'gallery');
			
			for(i = 0; i < $('.result-list-item-image').length; i++){
				$('.image-holder:eq('+i+') a').attr('rel', 'gal'+i);
				$('.image-holder:eq('+i+') a:eq('+(i+1)+')').css("display", "none")
			}
        }

    } catch (Exception) { }

});