/*
Generic Validation Functions
*/

function validateTextLength(str,minlen,maxlen) {
	if(typeof minlen == typeof 2) {
		if(str.length < minlen) { return false;}
	}
	if(typeof maxlen == typeof 2) {
		if(str.length > maxlen) { return false;}
	}
	return true;
}

function validateInteger(str,minval,maxval) {
	var mnv = minval;
	var mxv = maxval;
	var intval = parseInt(str,10);
	if(!isNaN(intval)) {
		if(typeof minval == typeof 2 && intval < mnv) { return false; }
		if(typeof maxval == typeof 2 && intval > mxv) { return false; }
		return true;
	}
	return false;
}

function validateNumber(str,minval,maxval) {
	var mnv = minval;
	var mxv = maxval;
	var intval = parseFloat(str,10);
	if(!isNaN(intval)) {
		if(typeof minval == typeof 2 && intval < mnv) { return false; }
		if(typeof maxval == typeof 2 && intval > mxv) { return false; }
		return true;
	}
	return false;
}

function validatePhoneNumber(ph,type) {
	//var numonly = ph.replace(/[^0-9]/g,'');
	//return (numonly.length >= 10);
	var rePhone = /^((\(\d{3}\) ?)|(\d{3}[- \.]))?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$/i;
	if(type === 2) {
		rePhone = /^\(\d{3}\) \d{3}-\d{4}$/i
	}
	return rePhone.test(ph);
}

function validateEmailAddress(email,multiple) {
	var reEmail = /^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/i;
	if(multiple === true) {
		reEmail = /^(([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+,?)+$/ig;
	}
	return reEmail.test(email);
}

function validateDate(d) {
	return !isNaN(Date.parse(d));
}

function checkHTML(str) {
	var reHTML = /([<>]|(\&lt)|(\&gt))/g;
	return reHTML.test(str);
}

function limitText(field,display,maxlength) {
	if (field.value.length > maxlength) {
		field.value = field.value.substring(0,maxlength);
	} else {
		if(typeof display == "string") {
			display = document.getElementById(display);
		}
		if(display !== null) {
			display.innerHTML = "" + (maxlength-field.value.length) + " characters left";
		}
	}
}

function hasChecked(field,num) {
	var numrequired = 1;
	var total = 0;
	if(typeof field != 'undefined') {
		if(typeof field.length == 'undefined') {
			if(field.checked === true) { total += 1; }
		} else {
			if(typeof num == typeof 2) { numrequired = num; }
			for(var i=0; i<field.length; i++) {
				if(field[i].checked === true) { total += 1; }
			}
		}
		return (total >= numrequired);
	} else {
		return false;
	}
}

/* HIGHLIGHT FUNCTIONS */

function highlightError(field,color,disableUN) {
	if(typeof field != 'undefined') {
		var hcolor = "red";
		if(typeof color == typeof "color") { hcolor = color; }
		if(typeof field.length == 'undefined' || field.nodeName === "SELECT") {
			field.style.backgroundColor = hcolor;
			if(disableUN !== false) {
				field.onfocus = function() { unhighlightError(this); }
			}
		} else {
			for(var i=0; i<field.length; i++) {
				field[i].style.backgroundColor = hcolor;
				if(disableUN !== false) {
					field[i].onclick = function() { unhighlightError(this); }
				}
			}
		}
	}
}

function unhighlightError(field,color) {
	hcolor = "";
	if(typeof color == typeof "color") { hcolor = color; }
	if(typeof field.length == 'undefined' || field.nodeName === "SELECT") {
		field.style.backgroundColor = hcolor;
	} else {
		for(var i=0; i<field.length; i++) {
			field[i].style.backgroundColor = hcolor;
		}
	}
}

/* MISC FUNCTIONS */

function getCheckedValueList(field,delimiter) {
	var list = "";
	if(typeof field != 'undefined') {
		if(typeof field.length == 'undefined') {
			if(field.checked === true) {
				list = field.value;
			}
		} else {
			if(typeof num == typeof 2) { numrequired = num; }
			for(var i=0; i<field.length; i++) {
				if(field[i].checked === true) {
					list += ((list.length > 0) ? "," : "") + field[i].value;
				}
			}
		}
	}
	return list;
}