/* Validate.js, version 1.0.2
 *  (c) 2006 achraf bouyakhsass <mutation[at]mutationevent.com>
 *
 *  This software is licensed under the CC-GNU GPL
 *  http://creativecommons.org/licenses/GPL/2.0/
 *
 *  For more details
 *  http://www.mutationevent.com/project/validate.js
 *
 *  Package to validate various data :
 *  isEqual
 *  hasValidChars
 *  isSimpleIP
 *  isAlphaLatin
 *  isNotEmpty
 *  isIntegerInRange
 *  isNum
 *  isEMailAddr
 *  isZipCode
 *  isDate
 *  isMD5
 *  isURL
 *  isGuid
 *  isISBN
 *  isSSN
 *  isDecimal
 *  isplatform
 *  addRules
 *  Apply
 * --------------------------------------------------------------------------*/
var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		};

	}

};

function getValue(s) {
	return document.getElementById(s).value;
}

var Validate = Class.create();
Validate.prototype = {
	/*--------------------------------------------------------------------------*/
	initialize: function() {
		this.error_array = [];
		this.rules_array = [];
		this.e = true;
	},

	/*--------------------------------------------------------------------------*/
	isEqual: function(string1, string2) {
		if(string1 == string2) {
			return true;
		}
		else {
			return false;
		}
	},

	/*--------------------------------------------------------------------------*/
	hasValidChars: function(s, characters, caseSensitive) {
		function escapeSpecials(s) {
			return s.replace(new RegExp("([\\\\-])", "g"), "\\$1");
		}

		return new RegExp("^[" + escapeSpecials(characters) + "]+$",(!caseSensitive ? "i" : "")).test(s);
	},

	/*--------------------------------------------------------------------------*/
	isSimpleIP: function(ip) {
		var ipRegExp = /^(([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+))$/;
		return ipRegExp.test(ip);
	},

	/*--------------------------------------------------------------------------*/
	isCC: function(cc) {
		var ipRegExp = /^((4\d{3})|(5[1-5]\d{2})|(6011))(-| )?\d{4}(\5\d{4}){2}$/;
		//ipRegExp = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/
		return ipRegExp.test(cc);
	},

	/*--------------------------------------------------------------------------*/
	isAlphaLatin: function(string) {
		var alphaRegExp = /^[0-9a-z]+$/i;
		return alphaRegExp.test(string);
	},

	/*--------------------------------------------------------------------------*/
	isNotEmpty: function (string) {
		return (/\S/).test(string);
	},

	/*--------------------------------------------------------------------------*/
	isEmpty: function(s) {
		return !/\S/.test(s);
	},

	/*--------------------------------------------------------------------------*/
	isEmptyDropDown: function(id, n) {
		var ret = false;
		if(n===0) {
			ret = true;
		}
		else {
			ret = false;
		}
		return ret;
	},

	/*--------------------------------------------------------------------------*/
	isEmptyRadio: function(id, n) {
		var ret = false;
		//alert(id+':'+n+':'+ret);
		return ret;
	},

	/*--------------------------------------------------------------------------*/
	isIntegerInRange: function(n,Nmin,Nmax) {
		var num = Number(n);
		if(isNaN(num)) {
			return false;
		}
		if(num != Math.round(num)) {
			return false;
		}
		return (num >= Nmin && num <= Nmax);
	},

	/*--------------------------------------------------------------------------*/
	isNum: function(number) {
		var numRegExp = /^[0-9]+$/;
		return numRegExp.test(number);
	},

	/*--------------------------------------------------------------------------*/
	isEMailAddr: function(string) {
		//emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/
		var emailRegExp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA/-Z]{2,9})$/;
		return emailRegExp.test(string);
	},

	/*--------------------------------------------------------------------------*/
	isZipCode: function(zipcode, country) {
		if(!zipcode) {
			return false;
		}
		if(!country) {
			country = 'US';
		}
		var zpcRegExp;
		switch(country) {
			case'US':
				zpcRegExp = /^\d{5}$|^\d{5}-\d{4}$/;
				break;
			case'MA':
				zpcRegExp = /^\d{5}$/;
				break;
			case'CA':
				zpcRegExp = /^[A-Z]\d[A-Z] \d[A-Z]\d$/;
				break;
			case'DU':
				zpcRegExp = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/;
				break;
			case'FR':
				zpcRegExp = /^\d{5}$/;
				break;
			case'Monaco':
				zpcRegExp = /^(MC-)\d{5}$/;
				break;
		}
		return zpcRegExp.test(zipcode);
	},

	/*--------------------------------------------------------------------------*/
	isDate: function(date,format) {
		if(!date) {
			return false;
		}
		if(!format) {
			format = 'FR';
		}
		var RegExpformat;
		switch(format) {
			case'FR':
				RegExpformat = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/([2][0]|[1][9])\d{2})$/;
				break;
			case'US':
				RegExpformat = /^([2][0]|[1][9])\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/;
				break;
			case'SHORTFR':
				RegExpformat = /^([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/\d{2}$/;
				break;
			case'SHORTUS':
				RegExpformat = /^\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/;
				break;
			case'dd MMM yyyy':
				RegExpformat = /^([0-2]\d|[3][0-1])\s(Jan(vier)?|F√©v(rier)?|Mars|Avr(il)?|Mai|Juin|Juil(let)?|Aout|Sep(tembre)?|Oct(obre)?|Nov(ember)?|Dec(embre)?)\s([2][0]|[1][19])\d{2}$/;
				break;
			case'MMM dd, yyyy':
				RegExpformat = /^(J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(((Sept|Nov|Dec)em)|Octo)ber)\s([0-2]\d|[3][0-1])\,\s([2][0]|[1][9])\d{2}$/;
				break;
		}

		return RegExpformat.test(date);
	},

	/*--------------------------------------------------------------------------
	 TODO: FINISH mbn
	 */
	isFutureCCDate: function(date) {
		if (!date) {
			return false;
		}
		var RegExpformat = /^((0[1-9])|(1[0-2]))(\d{4})$/;
		return RegExpformat.test(date);
	},

	/*--------------------------------------------------------------------------*/
	isMD5: function(string) {
		if (!string) {
			return false;
		}
		var md5RegExp = /^[a-f0-9]{32}$/;
		return md5RegExp.test(string);
	},

	/*--------------------------------------------------------------------------*/
	isURL: function(string) {
		if(!string) {
			return false;
		}
		string = string.toLowerCase();
		var urlRegExp = /^(((ht|f)tp(s?))\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/;
		return urlRegExp.test(string);
	},

	/*--------------------------------------------------------------------------*/
	isGuid: function(guid) {//guid format : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx or xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
		if(!guid) {
			return false;
		}
		var GuidRegExp = /^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$/;
		return GuidRegExp.test(guid);
	},

	/*--------------------------------------------------------------------------*/
	isISBN: function(number) {
		if(!number) {
			return false;
		}
		var ISBNRegExp = /ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$/;
		return ISBNRegExp.test(number);
	},

	/*--------------------------------------------------------------------------*/
	isSSN: function(number) {//Social Security Number format : NNN-NN-NNNN
		if(!number) {
			return false;
		}
		var ssnRegExp = /^\d{3}-\d{2}-\d{4}$/;
		return ssnRegExp.test(number);
	},

	/*--------------------------------------------------------------------------*/
	isDecimal: function(number) {// positive or negative decimal
		if(!number) {
			return false;
		}
		var decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/;
		return decimalRegExp.test(number);
	},

	/*--------------------------------------------------------------------------*/
	isplatform: function(platform) {
		//win, mac, nix
		if (!platform) {
			return false;
		}
		var os;
		var winRegExp = /\win/i;
		if (winRegExp.test(window.navigator.platform)) {
			os = 'win';
		}

		var macRegExp = /\mac/i;
		if (macRegExp.test(window.navigator.platform)) {
			os = 'mac';
		}

		var nixRegExp = /\unix|\linux|\sun/i;
		if (nixRegExp.test(window.navigator.platform)) {
			os = 'nix';
		}

		if (platform == os) {
			return true;
		}
		else {
			return false;
		}
	},

	/*--------------------------------------------------------------------------*/
	getValue: function(id) {
		return document.getElementById(id).value;
	},

	/*--------------------------------------------------------------------------*/
	addRules: function(rules) {
		this.rules_array.push(rules);
	},

	/*--------------------------------------------------------------------------*/
	check: function() {
		this.error_array = [];
		this.e = true;
		for(var i=0;i<this.rules_array.length;i++) {

			switch(this.rules_array[i].option) {
				/*--------------------------------------------------------------------------*/
				case'ValidChars':
					if(!this.hasValidChars(getValue(this.rules_array[i].id),this.rules_array[i].chars,false)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'AlphaLatin':
					if (this.isAlphaLatin(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'required':
					if (this.isEmpty(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'requiredDropDown':
					if (this.isEmptyDropDown(this.rules_array[i].id,document.getElementById(this.rules_array[i].id).selectedIndex)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'requiredRadio':
					if (this.isEmptyRadio(this.rules_array[i].id,document.getElementById(this.rules_array[i].id).checked)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'range':
					if (!this.isIntegerInRange(getValue(this.rules_array[i].id),this.rules_array[i].Min,this.rules_array[i].Max)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'number':
					if (!this.isNum(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'email':
					if (!this.isEMailAddr(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'ccnum':
					if (!this.isCC(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'postal':
					if (!this.isZipCode(getValue(this.rules_array[i].id),this.rules_array[i].country)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'date':
					if(!this.isDate(getValue(this.rules_array[i].id),this.rules_array[i].format)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'url':
					if(!this.isURL(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'decimal':
					if(!this.isDecimal(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'isEqual':
					if(!this.isEqual(getValue(this.rules_array[i].id),getValue(this.rules_array[i].to))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
				case'ishuman':
					if (this.isEmpty(getValue(this.rules_array[i].id))) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else if(this.isEmpty(8)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else if(!this.isEqual(getValue(this.rules_array[i].id),8)) {
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					break;
				/*--------------------------------------------------------------------------*/
			}
		}
	},

	/*--------------------------------------------------------------------------*/
	Apply: function(el) {

		if(document.getElementById('country')) {
			if(document.getElementById('country').value != 'United States' && document.getElementById('country').value != '') {
				document.getElementById('state').value = '--';
			}
		}

		/*
		 if(document.getElementById('rt1')) {
		 if(document.getElementById('rt1').checked==true){
		 this.addRules({id:'company',option:'required',error:'10'});
		 this.addRules({id:'phone1',option:'required',error:'11'});
		 }
		 }
		 */
		this.check();
		if(this.e) {
			// always remember email address
			setCookie( 'email', document.getElementById('email').value, 30, '/', '', '' );
			//if(document.getElementById('remember').checked==true) {
			//	setCookie( 'email', document.getElementById('email').value, 30, '/', '', '' );
			//}
			return true;
		}
		else {

			var endMsg = this.error_array;
			if(!el) {
				alert(this.error_array.toString().replace(/\,/gi,"\n"));
			}
			else {
				for (var i=0;i<=this.rules_array.length-1;i=i+1) {  // clear the input boxes and hide the divs
					document.getElementById(this.rules_array[i].id).style.border="2px inset #ccc";
				}

				for (i=0;i<=this.error_array.length-1;i=i+1) {

					for (var j=0;j<=this.rules_array.length-1;j=j+1) {

						if(this.rules_array[j].error.toString() == this.error_array[i].toString()) {
							document.getElementById(this.rules_array[j].id+'_error').style.display="block";
							document.getElementById(this.rules_array[j].id+'_error').style.color="red";
							document.getElementById(this.rules_array[j].id).style.border="1px solid red";
						}
					}
				}
			}

			document.getElementById('global_error').style.display="block";
			document.getElementById('global_error').innerHTML = 'There is a problem with the field(s). Please re-check your information and re-submit.';
			return false;
		}
	}

	/*--------------------------------------------------------------------------*/
}
