$(document).ready(function() {	
//   $('div.buttons', this).addClass("loading"); 
	$("form.validate").each(function() { 
			var onSubmitFn = $(this).attr("title")+'';
			$(this).attr({title: ""});
			$(this).validate(onSubmitFn);
	});
});

jQuery.fn.validate = function(onSubmitFn) {
	var validate = new jQuery.validator(onSubmitFn);
	return this.submit(validate.validateForm);
}

jQuery.validator = function(onSubmitFn) {
	var validator = this;
	this.errorList = {};
	this.currentForm;
	this.validateForm = function(submitEvent) {
		validator.errorList = {};
		validator.currentForm = this;
		$('label', this).removeClass("error")
		$(':input', this).removeClass("error")
		
		$(".required", validator.currentForm).each(function() {
			validator.validateRequired(this);
		});
		
		$(".email", validator.currentForm).each(function() {
			validator.validateEmail(this);
		});
		
		$(".number", validator.currentForm).each(function() {
			validator.validateNumber(this);
		});
		$(".checked", validator.currentForm).each(function() {
			validator.validateChecked(this);
		});
		if(!validator.isFormValid()){
			return false;
		}
		if(onSubmitFn != '' && onSubmitFn != 'undefined'){
			if(response = eval(onSubmitFn+'();')){
				if(response == 'submit'){
					this.target='_self';
					return true;	
				}
			}
			return false;
		}
		lockForm();
		return true;
	};
	
	this.validateEmail = function(element) {
		if(!(/(^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$)/i).test($(element).val())){
			this.errorList[element.id] = [];
		}
		
	}
	
	this.validateRequired = function(element) {
		if(!($(element).val())){
			this.errorList[element.id] = [];
		}
	}

	this.validateChecked = function(element) {
		if(!element.checked){
			this.errorList[element.id] = [];	
		}
		
	}

	this.validateNumber = function(element) {
		if(isNaN($(element).val())){
			this.errorList[element.id] = [];
		}
	}
	
	this.isFormValid = function(){
		var count = 0;
		for( i in this.errorList ) {
			count++;
		}
		if(count == 0) {
			return true;
		} else {
			this.showErrors();
			return false;
		}
	}
	
	this.showErrors = function(){
		var context = this.currentForm;
		for( elementID in this.errorList ) {
			var field = $('#'+elementID, context);
			var label = $('label', context).filter("[@for=" + elementID + "]");
			field.addClass("error");
			label.addClass("error");
		}
		showError();
	}
	
}
function lockForm(){
	$('div.buttons', this).addClass("loading"); 
	$("select").css('z-index', '-200'); 	
	lockPage();
}
function unlockForm(){
	$('div.buttons', this).removeClass("loading");
	unlockPage();
}
function lockPage(){
	$("body").append("<iframe id='comboLock'></iframe><div id='pageLock' ></div>");
	overlaySize();
}
function unlockPage(){
	$('#pageLock').remove();
	$('#comboLock').remove();
}

function showError(error){
	hideMessage();
	if(!error){
		error = generalError;
	}
	$('#errorBoxMsg').html(error);
	$('#errorBox').fadeIn(500);
}

function showMessage(message){
	hideError();
	$('#messageBoxMsg').html(message);
	$('#messageBox').fadeIn('slow');
}
function hideMessage(){
	if($("#messageBox").css('display')=='block'){	
		$('#messageBox').css("display","none");
	}
}
function hideError(){
	if($("#errorBox").css('display')=='block'){
		$('#errorBox').css("display","none");
	}
}
function overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {  
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	}else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	}else{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
	}
	xScroll = xScroll-10;
	$("#pageLock").css({"height": yScroll, "width": xScroll});
	$("#comboLock").css({"height": yScroll,"width": xScroll});
}