//
// check-form.js
//
// validate forms
//
// (c) 2009 G Jenkins / www.2below0.com
//

function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// Returns True If The String Is A Valid Email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// Returns True If The String Only Contains Characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}

// Returns True If The String Only Contains Characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// Returns True If The String Only Contains Characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

// Returns True If The String'S Length Equals "len"
function isLength(str, len){
	return str.length == len;
}

// Returns True If The String'S Length Is Between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}

// returns true if the string is a US phone number formatted as...
// (00)0000-0000, (00) 0000-0000, 00-0000-0000, 00.0000.0000, 00 0000 0000, 0000000000
function isPhoneNumber(str){
  var re = /^\(?[0-9]\d{1}[\)\.-]?\s?\d{4}[\s\.-]?\d{4}$/
  return re.test(str);
}

// Returns True If The String Is A Valid Date Formatted As...
// dd mm yyyy, dd/mm/yyyy, dd.mm.yyyy, dd-mm-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var y = parseInt(result[3]);
	var m = parseInt(result[2]);
	var d = parseInt(result[1]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2) {
		var days = ((y % 4) == 0) ? 29 : 28;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
	    var days = 30;
	} else {
	    var days = 31;
	}
	return (d >= 1 && d <= days);
}

// Returns True If "str1" Is The Same As The "str2"
function isMatch(str1, str2){
	return str1 == str2;
}

// Returns True If The String Contains Only Whitespace
// Cannot Check A Password Type Input For Whitespace
function isWhitespace(str){ 
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}

// Removes Any Whitespace From The String And Returns The Result
// The Value Of "Replacement" Will Be Used To Replace The Whitespace (optional)
function stripWhitespace(str, replacement) {
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1) {
		result = str.replace(re, replacement);
	}
	return result;
}

// Validate The Form
function validateForm(f, preCheck, newClass, alerttype) {
	var errors = '';
	var errorsa = '';
	if(preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++) {
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.id;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea') {
	
			if(isEmpty(v)) {
				errors += n+errormsg[1]+ '<br>';
				e.className=newClass;
				continue;
			} else {
				e.className='checkit';
			}
			if(v == e.defaultValue) {
				errors += n+errormsg[2]+ '<br>';
				e.className=newClass;
				continue;
			} else {
			e.className='checkit';
			}
			
			if(e.isAlpha) {
				if(!isAlpha(v)) {
					errors += n+errormsg[3]+ '<br>';
					overlib('eaaaa');
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
			
			if(e.isNumeric){
				if(!isNumeric(v)) {
					errors += n+errormsg[4]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
			
			if(e.isAlphaNumeric) {
				if(!isAlphaNumeric(v)) {
					errors += n+errormsg[5]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
			
			if(e.isEmail) {
				if(!isEmail(v)) {
					errors += n+errormsg[6]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
			
			if(e.isLength != null) {
				var len = e.isLength;
				if(!isLength(v,len)) {
					errors += n+errormsg[7]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
	      	}
	      	
			if(e.isLengthBetween != null) {
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)) {
					errors += n+errormsg[8]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}

			if(e.isPhoneNumber) {
				if(!isPhoneNumber(v)) {
					errorsa += n+errormsg[9]+'<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
      
      			
			if(e.isDate) {
				if(!isDate(v)) {
					errors += n+errormsg[10]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
			
			if(e.isMatch != null) {
				if(!isMatch(v, e.isMatch)) {
					errors += n+errormsg[11]+ '<br>';
					e.className=newClass;
					continue;
				} else {
					e.className='checkit';
				}
			}
		
		}
		
		if(t.indexOf('select') != -1) {
			if(isEmpty(e.options[e.selectedIndex].value)) {
				errors += n+errormsg[12]+ '<br>';
				e.className=newClass;
				continue;
			} else {
				e.className='checkit';
			}
		}
		
		if(t == 'file') {
			if(isEmpty(v)) {
				errors += n+errormsg[13]+ '<br>';
				e.className=newClass;
				continue;
			} else {
				e.className='checkit';
			}
		}
		
	}
  
	var el = document.forms[0].elements;
	for(var i = 0 ; i < el.length ; ++i) {
		if(el[i].type == "radio") {
			var radiogroup = el[el[i].name]; // whole set of radio buttons.
			var itemchecked = false;
			for(var j = 0 ; j < radiogroup.length ; ++j) {
				if(radiogroup[j].checked) {
	 				itemchecked = true;
	 				break;
				}
			}
			if(!itemchecked) { 
				errorsa  = el[i].name + ' : ' + n+errormsg[14] ;
			}
		}
	}
 
 
	div = document.getElementById('errordiv');
	
	return dispErr(errors, errorsa, div);
	
	div.style.display="none";
	
	return errors == '';
	return errorsa == '';
	
}

dispErr = function(error, errorsa, divo) {
	if (error || errorsa ) {
		divo.style.display="block";
		divo.innerHTML = errorsa;
		divo.innerHTML += error;
		return false;
	} else {
		div.style.display="none";
		return errors == '';
		return errorsa == '';
		return true;
	}
}

var errormsg = new Array();
	errormsg[0] 	= 'Select at least one checkbox!<br>';
	errormsg[1] 	= ' : Cannot be empty!';
	errormsg[2] 	= ' : Cannot use the default value!';
	errormsg[3] 	= ' : Can only contain characters A-Z a-z!';
	errormsg[4] 	= ' : Can only contain characters 0-9!';
	errormsg[5] 	= ' : Can only contain characters A-Z a-z 0-9!';
	errormsg[6] 	= ' : Is not a valid email address!';
	errormsg[7] 	= ' : Character number must be less than ';
	errormsg[8] 	= ' : Character number must be between ';
	errormsg[9] 	= ' : Not a valid phone number!';
	errormsg[10] 	= ' : Is not a valid date!';
	errormsg[11] 	= ' : Does not match!';
	errormsg[12] 	= ' : Needs an option selected!';
	errormsg[13] 	= ' : Needs a file to upload!';
	errormsg[14] 	= 'Select at least one option!<br>';
	errormsg[99] 	= 'All form information will be erased!';
	errormsg[100] 	= 'Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

