/*****************************************************************
******************************************************************
	dynValid object:
	===============
	Encapsulates validation functions for common form elements
	
******************************************************************
*****************************************************************/

// Define the constructor
// ======================
function dynValid() { }

// Create methods
// --------------
dynValid.prototype.getObject = function(formvalue, idvalue) {
	// Parameters:
	//	formvalue = id value of the form this control is in
	//	idvalue = id value of the item to return
	
	var obj;
	// Do a browser check 
	var br = this.getBrowser();
	switch(br) {
		case 0: //this is a "modern" browser that uses DOM model
			obj = document.getElementById(idvalue);
			break;
		case 1: //this is an IE 5 browser
			obj = document.all(idvalue);
			break;
		case 2: //this is a legacy browser like Netscape < 5
			if(document.layers[idvalue])
				obj = document.layers[idvalue];
			else {
				tmp = eval("document" + "." + formvalue);
				obj = eval(tmp + "." + idvalue);
			}
			break;
		default:
			alert("Could not get proper browser version or unknown");
			obj = null;
			break;
	}
	return(obj);
}
dynValid.prototype.validObject = function(idvalue) {
	
	var obj;
	// Do a browser check 
	var br = this.getBrowser();
	switch(br) {
		case 0: //this is a "modern" browser that uses DOM model
			if (document.getElementById(idvalue)) { obj = true; }
			break;
		case 1: //this is an IE 5 browser
			if (document.all(idvalue)) { obj = true; }
			break;
		case 2: //this is a legacy browser like Netscape < 5
			if(document.layers[idvalue])
				obj = true;
			else {
				obj = true;
			}
			break;
		default:
			obj = false;
			break;
	}
	return(obj);
}
dynValid.prototype.getBrowser = function() {
	if (document.getElementById) { return 0; }
	if (document.all) { return 1; }
	return 2;
}

dynValid.prototype.getMessages = function(msgary) {
	var out = "";
	if (msgary.length > 0) {
		out = "The following invalid items must be addressed:\n";
		for(x=0;x<msgary.length;x++)
			out += "\t * " + msgary[x] + "\n";
	}
	return(out);
}

// Validation checks
dynValid.prototype.runRequiredText = function(obj, name) {
	// Parameters
	//	obj = object to test for text
	//	name = label name for item
	var testval = obj.value
	while (testval.substring(0,1) == ' ') 
		testval = testval.substring(1, testval.length);
	while (testval.substring(testval.length-1,testval.length) == ' ')
		testval = testval.substring(0, testval.length-1);
	if (testval=="")
		return("'" + name  + "' requires text");
	return(null);
}

dynValid.prototype.runNumericText = function(obj, name, isReq) {
	// Parameters
	//	obj = object to test
	//	name = label name for item
	//	isReq = flags whether item is required or not.
	var ptr = /[0-9]+/;
	var res = this.runRequiredText(obj,name);
	if (res != null) { 
		if (isReq)
			return(res);
		else
			return(null);
	} else {
		valid = this.runRegExprCheck(obj.value,ptr);
		if (!valid) { return("'" + name + "' must be a numeric value"); }
	}
	return(null);
}

dynValid.prototype.runEmailText = function(obj, name, isReq) {
	// Parameters
	//	obj = object to test
	//	name = label name for item
	//	isReq = flags whether item is required or not.
	var ptr = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var res = this.runRequiredText(obj,name);
	if (res != null) { 
		if (isReq)
			return(res);
		else
			return(null);
	} else {
		valid = this.runRegExprCheck(obj.value,ptr);
		if (!valid) { return("'" + name + "' requires a valid email address"); }
	}
	return(null);
}

dynValid.prototype.runUSZipText = function(obj, name, isReq, noext) {
	// Parameters
	//	obj = object to test
	//	name = label name for item
	//	isReq = flags whether item is required or not.
	//	noext = do not allow four digit extended zip: 5 digit only
	if (noext)
		var ptr = /^\d{5}$/;
	else
		var ptr = /^\d{5}([\-]\d{4})?$/;
	var res = this.runRequiredText(obj,name);
	if (res != null) { 
		if (isReq)
			return(res);
		else
			return(null);
	} else {
		valid = this.runRegExprCheck(obj.value,ptr);
		if (!valid) { 
			if (noext)
				return("'" + name + "' requires a valid 5-digit US Zip Code (#####)"); 
			else
				return("'" + name + "' requires a valid US Zip Code (##### or #####-####)");
		}
	}
	return(null);
}

dynValid.prototype.runPhoneText = function(obj, name, isReq) {
	// Check a text string for various phone formats
	// Parameters
	//	obj = object to test against
	//	name = display text
	//	isReq = if true must have text, then test, else ignore (return OK)
	var ptr;
	var res = this.runRequiredText(obj,name);
	if (res != null) { 
		if (isReq)
			return(res);
		else
			return(null);
	} else {
		// Determine number of chars, this will signal what expressions to check against
		var cnt = obj.value.length;
		var valid = true;
		switch(cnt) {
			case 7:
				ptr = /[0-9]{7}/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 8:	//may have a dash or space
				ptr = /^[0-9]{3}(\s|\-|\.){1}[0-9]{4}$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 10: // incl area code no spaces
				ptr = /^[0-9]{10}$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 11: // space between areacode and number
				ptr = /^[0-9]{3}(\s|\-|\.){1}[0-9]{7}$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 12: // Dash or spaces between ac and numbers or parens around ac
				ptr = /^([0-9]{3}(\s|\-|\.){1}[0-9]{3}(\s|\-|\.){1}[0-9]{4})|(\([0-9]{3}\)[0-9]{7})$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 13: // ac parens and space between numbers, no space between ac and numbers
				ptr = /^\([0-9]{3}\)[0-9]{3}(\s|\-|\.){1}[0-9]{4}$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			case 14: // ac parens, spaces between groups
				ptr = /^\([0-9]{3}\)(\s|\-|\.){1}[0-9]{3}(\s|\-|\.){1}[0-9]{4}$/;
				valid = this.runRegExprCheck(obj.value,ptr);
				break;
			default:
				// invalid
				valid = false;
				break;
		}
		if (!valid) { return("'" + name + "' is not a recognized phone number format.  Try (###)###-####"); }
	}
	return(null);
}

dynValid.prototype.runRadioChecked = function(containerID, name, form, rdoPrefix, rdoCnt) {
	// Function will check if using DOM: if so, use the container name to drill the DOM
	// for all radio instances, then loop for checked properties.  If checked false for all then fail
	// If not DOM: use prefix and count to iterate all or form to check radios
	var br = this.getBrowser()
	var isCheck = false;
	if (br==0) {
		// DOM
		var cont = document.getElementById(containerID);
		var radios = cont.getElementsByTagName("input");
		for (x=0;x<radios.length;x++) {
			if(radios[x].type=="radio") {
				isCheck = (radios[x].checked || isCheck);
			}
		}
		if (!isCheck) { return("'" + name + "' must have at least one checked item"); } else { return(null); }
	} else {
		// Not DOM
		var frm = document.forms[form];
		for(x=0;x<rdoCnt;x++) {
			var rdo = frm.elements[rdoPrefix + x];
			isCheck = (rdo.checked || isCheck);
		}
		if (!isCheck) { return("'" + name + "' must have at least one checked item"); } else { return(null); }
	}
}

dynValid.prototype.runCheckboxChecked = function(containerID, name, minCheck, form, cbxPrefix, cbxCnt) {
	// Function will check if using DOM: if so, use the container name to drill the DOM
	// for all checkbox instances, then loop for checked properties.  if numchecked < minCheck then fail
	// If not DOM: use prefix and count to iterate all or form to check checkboxes	
	var br = this.getBrowser();
	var xcnt = 0;
	var s = (minCheck!=1) ? "s" : "";
	if (br==0) {
		// DOM
		var cont = document.getElementById(containerID);
		var cbxs = cont.getElementsByTagName("input");
		for (x=0;x<cbxs.length;x++) {
			if(cbxs[x].type=="checkbox") {
				xcnt += (cbxs[x].checked) ? 1 : 0;
			}
		}
		if (xcnt < minCheck) { return("'" + name + "' must have at least " + minCheck + " checked item" + s); } else { return(null); }
	} else {
		// Not DOM
		var frm = document.forms[form];
		for(x=0;x<cbxCnt;x++) {
			var cbx = frm.elements[cbxPrefix + x];
			count += (cbx.checked) ? 1 : 0;
		}
		if (xcnt < minCheck) { return("'" + name + "' must have at least " + minCheck + " checked item" + s); } else { return(null); }
	}
}

dynValid.prototype.runSelectValid = function(obj, name, invalidIdx) {
	// Checks to see if a dropdown is set to an invalid value passed by the property.  If no
	// value is passed a default index of 0 is assumed.
	if (!invalidIdx) { invalidIdx = 0; }
	if (obj.selectedIndex==invalidIdx)
		return("'" + name + "' must have a valid selection");
	else 
		return(null);
}

dynValid.prototype.runRegExprCheck = function(val,pattern) {
	// Runs a regular expression check on the passed pattern for the value of the passed object	
	
	if (val.match(pattern)) { return(true); } else { return(false); }
}

dynValid.prototype.setScreenNotice = function(objid,mode) {
	// This requires a span tag in the format val_<objid>.
	
	if (mode) {
		document.getElementById("val_" + objid).style.display = "none";
		// We need to check types - transp. for block, white for inputs
		if (document.getElementById(objid).nodeName=="INPUT")
			document.getElementById(objid).style.backgroundColor="#fff";
		else
			document.getElementById(objid).style.backgroundColor="transparent";
	} else {
		document.getElementById("val_" + objid).style.display = "inline";
		document.getElementById(objid).style.backgroundColor="#fee";
	}
}
dynValid.prototype.resetNotices = function(form) {
	var br = this.getBrowser();
	if (br==0) {
		fe = document.getElementById(form);
		spans = fe.getElementsByTagName("span");
		for(x=0;x<spans.length;x++) {
			if (spans[x].id.match(/val_+/)) {
				// this is a validation item, hide it
				spans[x].style.display = "none";
			}
		}
		for(x=0;x<fe.childNodes.length;x++) {
			if (fe.childNodes[x].style) {
				if (fe.childNodes[x].nodeName=="INPUT")
					fe.childNodes[x].style.backgroundColor="#fff";
				else
					fe.childNodes[x].style.backgroundColor="transparent";
			}
		}
	} else {
		fe = document.forms[form].elements;
		for(x=0;x<fe.length;x++) {
			if (fe[x].id.match(/val_+/)) {
				// this is a validation item, hide it
				fe[x].style.display = "none";
			}
			if (fe[x].nodeName=="INPUT")
				fe[x].style.backgroundColor="#fff";
			else
				fe[x].style.backgroundColor="transparent";
		}
	}
}
