// JavaScript Document

//Class: 						XValidator.js 
//Author: 						Jodi Curtis
//Created:						29/11/2007
//Modified:						13/05/2008
//Status:						R1.0.1
//Copyright: 					Xatrox Ltd All Rights Reserved
//Information and Licensing:	Contact Jodi Curtis at Xatrox Ltd or email jcurtis@xatroxsoftware.com

function XValidator() {

	 this.validateHex = function(val) {
		if ( (val == "") || (val.length <= 0) ) {
			return false;
		}
		
		var pat = new RegExp("^[0-9a-f]+$","i");
		return (val.match(pat)) ? true : false;
	},
	
	this.validateRGBColor = function(val) {
		return ( (val.length == 6) && (validateHex(val) == true ) ) ? true : false;
	},
	
	this.validateDateTime = function(val, comment) {
		var enableOutput = true;
		
		if (typeof(val) != "string") {
			return false;
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( (val == "") || (val.length != 19) ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid date and time';
			}
			return false;
		}
		
		var pat =  new RegExp("^([0-3]{1}[0-9]{1}){1}-([0-1]{1}[0-9]{1}){1}-([0-9]{4}){1} ([0-2]{1}[0-9]{1}){1}:([0-5]{1}[0-9]{1}){1}:([0-5]{1}[0-9]{1}){1}$","i");
		var err = !(val.match(pat));
		
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a date using the following format DD\\MM\\YYYY HH:MM:SS';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		return !err;
	},
	
	this.validateDate = function(val, comment) {
		var enableOutput = true;
		
		if (typeof(val) != "string") {
			return false;
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( (val == "") || (val.length != 10) ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid date';
			}
			return false;
		}
		
		var pat =  new RegExp("^([0-3]{1}[0-9]{1}){1}-([0-1]{1}[0-9]{1}){1}-([0-9]{4}){1}$","i");
		var err = !(val.match(pat));
		
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a date using the following format DD\\MM\\YYYY';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		return !err;
	},
	
	this.validateTime = function(val, comment) {
		var enableOutput = true;
		
		if (typeof(val) != "string") {
			return false;
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( (val == "") || (val.length != 8) ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid time';
			}
			return false;
		}
		
		var pat =  new RegExp("^([0-2]{1}[0-9]{1}){1}:([0-5]{1}[0-9]{1}){1}:([0-5]{1}[0-9]{1}){1}$","i");
		var err = !(val.match(pat));
		
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a date using the following format HH:MM:SS';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		return !err;
	},
	
	this.validateString = function(minlen, maxlen, val, comment) {
	
		var enableOutput = true;

		
		if ( (typeof(minlen) != "number") || (typeof(maxlen) != "number") || (typeof(val) != "string") ) {
			return false;
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( ( (val == "") && (minlen > 0) ) || ( (val.length <= 0) && (minlen > 0) ) || (val.length < minlen) ) {
			if (enableOutput == true) {		
				document.getElementById(comment).innerHTML = 'You must enter text containing at least '+minlen+' characters';
			}
			return false;
		}
		
		if (val.length > maxlen) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter text containing at most '+maxlen+' characters';
			}
			return false;
		}
		
		if (enableOutput == true) {
			document.getElementById(comment).innerHTML = '';
		}
		return true;
	},
	
	this.validateNumber = function(minval, maxval, val, comment) {
		
		var enableOutput = true;
		
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
	
		if ( (typeof(minval) != "number") || (typeof(maxval) != "number") || (typeof(val) != "number" )  ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a numberic value only';
			}
			return false; 
		}

		if ( ( (val <= 0) && (minval > 0) ) || (val < minval) || (isNaN(val)) ) {
			alert(minval);
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a number higher than '+minval;
			}
			return false;
		}
		
		if (val > maxval) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a number lower than '+maxval;
			}
			return false;
		}
		
		/*
		var pat =  new RegExp("^[0-9]$","i");
		var err = !(val.match(pat));
		
		alert(err);
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a numberic value only';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}*/
		return true;
	},
	
	this.validateTelno = function(val, comment) {
		
		var enableOutput = true;
	
		if ( (typeof(val) != "string") && (typeof(val) != "number") ) {
			return false; 
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		
		if ( (val == "") || (val.length <= 0) ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a telephone number';
			}
			return false;
		}
		
		if (val.length > 30) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a telephone number';
			}
			return false;
		}
		
		var pat =  new RegExp("^(([\+]{1})([\(]{0,1})([1-9]{1})(((-){1}[0-9]{3})|([0-9]{1,4}))?([\)]{0,1}) )?([0-9]*)?([0-9]{1,})(-| ?[0-9]{2,7})+$","i");
		var err = !(val.match(pat));
		
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a number using the following format +(44) 02081 9876543';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		return !err;
	},
	
	this.validatePostcode = function(val, comment) {
	
		var enableOutput = true;
	
		if ( (typeof(val) != "number") && (typeof(val) != "string") ) {
			return false; 
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( (val == "") || (val.length <= 2) ) {
			if (enableOutput == true) {		
				document.getElementById(comment).innerHTML = 'You must enter a valid UK or US postcode';
			}
			return false;
		}
		
		if (val > 12) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid UK or US postcode';
			}
			return false;
		}
	
		var pat =  new RegExp("^((([a-z]{1,2}[0-9]{1,2}){1}([ ]?)([0-9]{1,2}[a-z]{2}){1})|([a-z]{2}[ ]{0,1}[0-9]{5}))$","i");
		var err = !(val.match(pat));
		
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter a postcode using the following formats MA54 4PY or CA54321';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		return !err;
	
	},
	
	this.validateEmail = function(val, comment) {
		
		var enableOutput = true;
	
		if (typeof(val) != "string") {
			return false;
		}
	
		if ( (typeof(comment) != "string") || (comment.length == 0) ){
			enableOutput = false;
		}
		
		if ( (val == "") || (val.length <= 0) ) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid email address';
			}
			return false;
		}
		
		if (val.length > 30) {
			if (enableOutput == true) {
				document.getElementById(comment).innerHTML = 'You must enter a valid email address';
			}
			return false;
		}
		
		
		var pat =  new RegExp("^([_a-z0-9-]+)((\.[_a-z0-9-]+)*)(@([_a-z0-9-]+)+)(([\.]{1}[_a-z0-9-]+)*)(([\.]{1}[_a-z0-9-]{2,4})+)(([\.]{1}[_a-z0-9-]{2,4})*)$","i");
		var err = !(val.match(pat));
	
		if (enableOutput == true) {
			if (err) {
				document.getElementById(comment).innerHTML = 'You must enter text in the format name@domain.ext';
			} else {
				document.getElementById(comment).innerHTML = '';
			}
		}
		
		return !err;
	}
};