// For finding the actual pixel positioning of an element (obj):
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


/* For dynamic evaluation of submission forms. 
	The Form must have a submit button named "Submit", and any required fields must contain a "req=r" attribute. 
	Just pass the Form's Name with a return and the script below will Validate for any required fields and check that there is at least one field with data.
	Script will then return to the form and Submit it to it's Action passing all fields as normal.
*/
function validateme(fn) {
	
	var newFields=[];
	var reqFields=[];
	var fields = document.forms[fn].elements;
	var err = "Required:";

	for(f=0;f<fields.length;f++){
		if( ((fields[f].value != "") && (fields[f].value != null)) && (fields[f].name.toUpperCase() != "SUBMIT") ){
			newFields.push(fields[f]);
		}
	}
	n = document.getElementById(fn).elements;
	for(f=0;f<n.length;f++){
		nn = n[f];
		//alert(nn.attributes['req'].value);
		if(nn.attributes['req']){
			if( (nn.attributes['req'].value == 'r') && (fields[f].name.toUpperCase() != "SUBMIT") ){
				if((nn.value == "") || (nn.value == null)){
					reqFields.push(fields[f]);
				}
			}
		}
	}

	if((newFields.length < 1) || (reqFields.length > 0)){
		if(reqFields.length > 0) {
			for(f=0;f<reqFields.length;f++){
				err += "\n - "+reqFields[f].name+"";
			}
		}
		
		if(newFields.length < 1) {
			err += "\nYou must provide a search criteria in at least one field.";
		}
		
		alert(err);
		return false;
	
	} else {
		//document.PredSrchFrm.submit;
		return;
	}
	
}


/*
The following Validation script will validate a form dynamically by looking through each field for an attrib named "req=" indicating a required field, or "opt=" indicating an optional field.
If a required field is found, it looks for the value of the "req=" attrib and evaluates it to find out which type of validation is needed. If it is blank, it returns "validationmsg+ is required."
If an Optional field is found, it only evaluates it if it is not blank. The "validationmsg=" attrib is used to pass the name as it is to be shown on the Alert() box.
Example field: <input type="text" id="Date" name="Date" class="" value="" opt="date" validationmsg="Date of Incident">
Example call: onclick="javascript:validateme2('frmTips');"

	req=""				A value other than Null is required
	req="nnn"		Field is required, "nnn" would be one of the required formats below:
	opt="nnn"		Field is optional, "nnn" would be one of the required formats below:
	
	"num"		A numeric value
	"cc"			A 16-digit Credit Card number is required
	"zip"			A 5-digit Zip Code is required
	"cvv2"		A 3 or 4-digit CVV2 Credit Card Code is required
	"date"		A date in "mm/dd/yyyy" format
	"email"		An email address in "name@domain.xxx" format

	Additionally, a combine feature can be used by inserting "combine|nnn,mmm" in the value attrib of the field.
	This will concat the values of fields "nnn" and "mmm" into one value in the field where this is inserted.
	Example: <input type="hidden" name="CCexp" id="CCexp" value="combine|CCexp1,CCexp2" req="" validationmsg="Exp Date">
*/
function validateme2(formname) {
	var f=document.forms[formname];
	var msg = "";
	
	function val(r){
		if(r == "num"){
			if (f.elements[i].value.search(/^\d+$/)){
				msg += f.elements[i].getAttribute('validationmsg')+" requires a numeric value\n";
			}
		} else if(r == "cc"){
			if (f.elements[i].value.search(/^\+?[\d\s]{16}$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" requires a 16-digit credit card number\n";
			}
		} else if(r == "zip"){
			if (f.elements[i].value.search(/^\+?[\d\s]{5}$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" requires a 5-digit postal code\n";
			}
		} else if(r == "cvv2"){
			if (f.elements[i].value.search(/^\+?[\d\s]{3,4}$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" requires a 3 or 4-digit security code number\n";
			}
		} else if(r == "date"){
			if (f.elements[i].value.search(/^([1-9]|0[1-9]|1[012])\D([1-9]|0[1-9]|[12][1-9]|3[01])\D(19[0-9][0-9]|20[0-9][0-9])$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" must be in a 'mm/dd/yyyy' format.\n";
			}
		} else if(r == "email"){
			if (f.elements[i].value.search(/^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" must be in a 'name@domain' format.\n";
			}
		} else if(r == "phone"){
			if (f.elements[i].value.search(/^(\d{3}-){1,2}\d{4}x\d+$|^(\d{3}-){1,2}\d{4}$/) == -1){
				msg += f.elements[i].getAttribute('validationmsg')+" must be 7 or 10 digits in \"nnn-nnn-nnnn\" format.\n";
			}
		}
		return;
	}
	
	
	for(i=0;i<f.elements.length;i++){
		if(f.elements[i].nodeName != "FIELDSET"){ // Skip Fieldsets since they don't contain any value we need.
        
		if(f.elements[i].value.match(/combine/)){
			stringArray = f.elements[i].value.split("|");
			stringArray = stringArray[1];
			stringArray = stringArray.split(",");
			selMonth = f.elements[stringArray[0]].value;
			selYear = f.elements[stringArray[1]].value;
			f.elements[i].value = selMonth.concat(selYear);
			f.elements[stringArray[0]].value = "";
			f.elements[stringArray[1]].value = "";
			//alert(f.elements[i].value);
		}

		if(f.elements[i].getAttributeNode('req')){ // if this field is Required:
			var required = f.elements[i].getAttribute('req');			
			if(f.elements[i].value == ""){
				msg += f.elements[i].getAttribute('validationmsg')+" is required\n";
                // Hightlight and outline errored fields to make them obvious to User:
                f.elements[i].style.backgroundColor = "#FFFFCC";
                f.elements[i].style.border = "1px solid #CC0000";
			} else {
				val(required);
			}
		} else if( (f.elements[i].getAttributeNode('opt')) && (f.elements[i].value != "") ){ // if this field is optional:
			var required = f.elements[i].getAttribute('opt');
			val(required);
		}
		
        }
	}
			
	if (msg != ""){
		alert("Please correct the following and re-submit:\n\n" + msg + "\nThe fields are highlighted and may be outlined in red.");
		return false;
	} else {
		//var frm = document.getElementById(formname).id;
		//document.forms[frm].submit();
		return true;
	}
			
}
