function clsValidate() {
	this.instance		= null;
	this.minPasswordLength	= 6;
	this.maxPasswordLength	= 13;
	this.fieldMethodArray	= new Object();
	this.fieldParamsArray	= new Object();
	this.messageArray	= new Object();
	this.focusedObject	= null;

	this._constructor = function() {
	}

	this.setMinPasswordLength = function(l) {
		this.minPasswordLength = l;
	}

	this.setMaxPasswordLength = function(l) {
		this.maxPasswordLength = l;
	}

	this.addFieldMethodPair = function(field, method, params) {
		this.fieldMethodArray[field] = method;
		if(params) {
			this.fieldParamsArray[field] = params;
		} else {
			this.fieldParamsArray[field] = null;
		}
	}

	this.validateGenericTextField = function(o) {
		if(trim(o.value).length == 0) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}
		return true;
	}

	this.validateEmailField = function(o) {
		if(trim(o.value).length == 0) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		var emailRegExp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
		if(!emailRegExp.test(o.value)) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validateCompareField = function(o, params) {
		if(trim(o.value).length == 0) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		result = (o.value == document.getElementById(params[0]).value);
		if(result != params[1]) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validatePasswordField = function(o) {
		var entryLength = trim(o.value).length;
		if(entryLength < this.minPasswordLength || entryLength > this.maxPasswordLength) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validateGenericComboBox = function(o) {
		if(o.selectedIndex == -1) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validateGenericSelectBox = function(o, params) {
		if(o.selectedIndex == -1 || o.selectedIndex == params[0]) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validateGenericCheckBox = function(o, params) {
		if(o.checked != params[0]) {
			if(this.focusedObject == null) this.focusedObject = o;
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}

		return true;
	}

	this.validateCustomValidation = function(o, customValidation) {
		return customValidation(o, this);
	}

	this.validateForm = function() {
		var result = true;

		this.focusedObject = null;
		for(var field in this.fieldMethodArray) {
			// Reset error message.
			var oErrSpan = document.getElementById(field + '_err');
			oErrSpan.style.display = "none";

			// Evaluate the validation function.
			eval('result = this.validate' + this.fieldMethodArray[field] + '(' +
				'document.getElementById("' + field + '"), ' +
				'this.fieldParamsArray["' + field + '"]) && result;');
//			if(!result) break;
		}

		if(this.focusedObject) this.focusedObject.focus();
		return result;
	}
}

function validateEmailByAjax(o, clsInstance) {
	// Hide this element in case it's not yet hidden.
	var oErrSpan = document.getElementById(o.id + '_exists_err');
	oErrSpan.style.display = "none";

	if(trim(o.value).length == 0) {
		var oErrSpan = document.getElementById(o.id + '_err');
		oErrSpan.style.display = "block";
		if(clsInstance.focusedObject == null) clsInstance.focusedObject = o;
		return false;
	}

	var emailRegExp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if(!emailRegExp.test(o.value)) {
		var oErrSpan = document.getElementById(o.id + '_err');
		oErrSpan.style.display = "block";
		if(clsInstance.focusedObject == null) clsInstance.focusedObject = o;
		return false;
	}

	var oAjax = new clsAjax();

	oAjax.method	= 'POST',
	oAjax.url	= '../ajax/ajax_checkmail.php';
	oAjax.addPostField('m', o.value);
	oAjax.fetchData();

	var mailDoesntExist = oAjax.responseText == "0" ? false : true;
	if(!mailDoesntExist) {
		var oErrSpan = document.getElementById(o.id + '_exists_err');
		oErrSpan.style.display = "block";
		if(clsInstance.focusedObject == null) clsInstance.focusedObject = o;
		return false;
	}

	return true;
}

function validateFCKEditor(o, clsInstance) {
	var oEditor = FCKeditorAPI.GetInstance(o.name);

	if(trim(oEditor.GetXHTML()).length == 0) {
		if(this.focusedObject == null) this.focusedObject = o;
		var oErrSpan = document.getElementById(o.id + "_err");
		oErrSpan.style.display = "block";
		return false;
	}

	return true;
}


function validateImageJPG(o, clsInstance) {

	if (o.value != '') {
		if ((o.value.slice(-3, o.value.length)).toLowerCase() != 'jpg'){
			var oErrSpan = document.getElementById(o.id + "_err");
			oErrSpan.style.display = "block";
			return false;
		}
	}

	return true;
}

var oIndividualRegValidation = new clsValidate();
oIndividualRegValidation.addFieldMethodPair('name', 'GenericTextField');
oIndividualRegValidation.addFieldMethodPair('lastname', 'GenericTextField');
oIndividualRegValidation.addFieldMethodPair('email', 'CustomValidation', validateEmailByAjax);
oIndividualRegValidation.addFieldMethodPair('emailverify', 'CompareField', new Array('email', true));
oIndividualRegValidation.addFieldMethodPair('pass', 'PasswordField');
oIndividualRegValidation.addFieldMethodPair('passverify', 'CompareField', new Array('pass', true));
oIndividualRegValidation.addFieldMethodPair('accept_terms', 'GenericCheckBox', new Array(true));

var oCorporateRegValidation = new clsValidate();
oCorporateRegValidation.addFieldMethodPair('name', 'GenericTextField');
oCorporateRegValidation.addFieldMethodPair('lastname', 'GenericTextField');
//oCorporateRegValidation.addFieldMethodPair('companyname', 'GenericTextField');
oCorporateRegValidation.addFieldMethodPair('email', 'CustomValidation', validateEmailByAjax);
oCorporateRegValidation.addFieldMethodPair('emailverify', 'CompareField', new Array('email', true));
oCorporateRegValidation.addFieldMethodPair('pass', 'PasswordField');
oCorporateRegValidation.addFieldMethodPair('passverify', 'CompareField', new Array('pass', true));
//oCorporateRegValidation.addFieldMethodPair('address', 'GenericTextField');
//oCorporateRegValidation.addFieldMethodPair('town', 'GenericTextField');
//oCorporateRegValidation.addFieldMethodPair('country', 'GenericTextField');
//oCorporateRegValidation.addFieldMethodPair('postcode', 'GenericTextField');
//oCorporateRegValidation.addFieldMethodPair('companyname', 'GenericTextField');
oCorporateRegValidation.addFieldMethodPair('accept_terms', 'GenericCheckBox', new Array(true));

oLoginValidation = new clsValidate();
oLoginValidation.addFieldMethodPair('login_email', 'GenericTextField');
oLoginValidation.addFieldMethodPair('login_password', 'PasswordField');

var oMailToFriendValidation = new clsValidate();
oMailToFriendValidation.addFieldMethodPair('username', 'GenericTextField');
oMailToFriendValidation.addFieldMethodPair('email', 'EmailField');
oMailToFriendValidation.addFieldMethodPair('friendsname', 'GenericTextField');
oMailToFriendValidation.addFieldMethodPair('sendto', 'GenericTextField');
oMailToFriendValidation.addFieldMethodPair('mailbody', 'GenericTextField');

var oPressReleaseValidation = new clsValidate();
oPressReleaseValidation.addFieldMethodPair('title', 'GenericTextField');
oPressReleaseValidation.addFieldMethodPair('subtitle', 'GenericTextField');
oPressReleaseValidation.addFieldMethodPair('body', 'CustomValidation', validateFCKEditor);
oPressReleaseValidation.addFieldMethodPair('imageupload', 'CustomValidation', validateImageJPG);
oPressReleaseValidation.addFieldMethodPair('categorylist1', 'GenericSelectBox', new Array('0'));

var ofeatureValidation = new clsValidate();
ofeatureValidation.addFieldMethodPair('title', 'GenericTextField');
ofeatureValidation.addFieldMethodPair('subtitle', 'GenericTextField');
ofeatureValidation.addFieldMethodPair('body', 'CustomValidation', validateFCKEditor);
ofeatureValidation.addFieldMethodPair('writtenBy', 'GenericTextField');
ofeatureValidation.addFieldMethodPair('category', 'GenericSelectBox', new Array(''));

var oEventValidation = new clsValidate();
oEventValidation.addFieldMethodPair('country', 'GenericSelectBox', new Array('0'));
oEventValidation.addFieldMethodPair('EventPlaceId', 'GenericSelectBox', new Array('0'));
oEventValidation.addFieldMethodPair('title', 'GenericTextField');
oEventValidation.addFieldMethodPair('briefDescription', 'GenericTextField');
oEventValidation.addFieldMethodPair('description', 'CustomValidation', validateFCKEditor);
oEventValidation.addFieldMethodPair('category', 'GenericSelectBox', new Array('0'));
oEventValidation.addFieldMethodPair('imageupload', 'CustomValidation', validateImageJPG);

var oEventPlaceValidation = new clsValidate();
oEventPlaceValidation.addFieldMethodPair('placeCountry', 'GenericSelectBox', new Array(''));
oEventPlaceValidation.addFieldMethodPair('placeCityTown', 'GenericTextField');
oEventPlaceValidation.addFieldMethodPair('placeTitle', 'GenericTextField');
oEventPlaceValidation.addFieldMethodPair('placeAddress', 'GenericTextField');


var oEventSubcategoryValidation = new clsValidate();
oEventSubcategoryValidation.addFieldMethodPair('Title', 'GenericTextField');

var oEventMediaPackValidation = new clsValidate();
oEventMediaPackValidation.addFieldMethodPair('mediaPackTitle', 'GenericTextField');


var oClassifiedValidation = new clsValidate();
oClassifiedValidation.addFieldMethodPair('title', 'GenericTextField');
oClassifiedValidation.addFieldMethodPair('categorylist1', 'GenericSelectBox', new Array('0'));
oClassifiedValidation.addFieldMethodPair('categorylist2', 'GenericSelectBox', new Array('0'));
oClassifiedValidation.addFieldMethodPair('country', 'GenericSelectBox', new Array(''));
oClassifiedValidation.addFieldMethodPair('email', 'EmailField');

var oPostCommentValidation = new clsValidate();
oPostCommentValidation.addFieldMethodPair('comment_message', 'GenericTextField');

var oForgottenPwdValidation = new clsValidate();
oForgottenPwdValidation.addFieldMethodPair('email', 'GenericTextField');
