/*
 * Copyright 2001 - 2004, Humanizing Technologies, Inc.  All rights reserved.
 * How to use this script:
 * Make any input elements look like the one below, adding the new parameters
 * pattern - uses regular expressions to match text against a pattern
 * errorMsg - message displayed to user whan an invalid statement is entered
 * example
 * First Name: <input type="text" name="fname" maxsize="20" size="20"
 *       pattern="[^A-Za-z]+" errorMsg="Invalid character found in first name"
 *       onfocus="changeColor(this);"  found in FmoCommonScripts.js/>
 */
function validate(p_bFlag){
	var msgsObj = new Object;
	var bIsFirst = true;
	msgsObj.str = "";
	var elements = document.getElementsByTagName('input');
	for(var i = 0; i < elements.length; i++){
		var bIsNotValid = validateField(elements.item(i), msgsObj, p_bFlag);
		if(bIsNotValid){
			if(bIsFirst){
				elements.item(i).focus();
				bIsFirst = false;
			}
		}
	}
	return validationShow(msgsObj.str);
}
function validateField(p_theElement, p_strError, p_bFlag){
	var bInvalid = false;
	var type = p_theElement.getAttribute('type');
	var strTheElementId = p_theElement.id;
	if (strTheElementId == "Password2"){
		strTheElementId = "Verify Password";
	}	
	if (strTheElementId == "Firstname"){
		strTheElementId = "First Name";
	}
	if (strTheElementId == "Lastname"){
		strTheElementId = "Last Name";
	}
	if (strTheElementId == "PostalCode"){
		strTheElementId = "Postal Code";
	}
	if (strTheElementId == "Email"){
		strTheElementId = "E-mail";
	}
	if (type == "hidden"){
		return;
	}
	var val = trimString(p_theElement.value);
	var min_reqs = p_theElement.getAttribute('min_reqs');
	if(min_reqs){
		min_reqs = parseInt(min_reqs);
	}
	if( val == "" || val == null){
		if(min_reqs != null){
			var ct = CHARACTER_TEXT;
			if(min_reqs > 1){
				ct = CHARACTERS_TEXT;
			}
			p_strError.str += '<li>'+ strTheElementId +' requires at least ' + (min_reqs) + ct;
			p_theElement.style.background = "red";
			bInvalid = true;
		}
		return bInvalid;
	}
	if (min_reqs != null){
		var value = val.length;
		if (value < min_reqs){
			p_strError.str += '<li>'+strTheElementId +' requires at least ' + (min_reqs) + ' characters.';
			p_theElement.style.background = "red";
			bInvalid = true;
		}
	}
	var max_reqs = p_theElement.getAttribute('max_reqs');
	if(max_reqs){
		max_reqs = parseInt(max_reqs);
	}	
	if (max_reqs != null){
		var value = val.length;
		if (value > max_reqs){
			var ct = CHARACTER_TEXT;
			if (max_reqs > 1){
				ct = CHARACTERS_TEXT;
			}
			p_strError.str += '<li>'+strTheElementId +' requires a maximum of ' + (max_reqs) + ct;
			p_theElement.style.background = "red";
			bInvalid = true;
		}
	}
	var pattern = p_theElement.getAttribute('pattern');
	var my=/\S/g;
	if(!my.test( val ) && pattern == null){
		if (p_bFlag==true){
			return true;
		}
		p_strError.str += '<li>'+"A character needs to be entered before putting in a space. Please re-name " + strTheElementId + ".";
		p_theElement.style.background = "red";
		bInvalid = true;
	}
	else{
		var id = p_theElement.getAttribute('id');
		if (pattern != null){
			var userInput = val.substring(0,1);
			if(userInput == " "){
				p_strError.str += FORM_VALIDATION_PREFIX + strTheElementId + ".";
				p_theElement.style.background = "red";
				bInvalid = true;
			}
			var min_reqs = p_theElement.getAttribute('min_reqs');
			if((id == "email" || id == "Email"  && min_reqs > 0) || (id == "AltEmail" && val.length > 0)){
				var emailpattern=/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}$/i;
				if (val.search(emailpattern) == -1){
					p_strError.str += "<li>You must enter a valid e-mail address.";
					p_theElement.style.background = "red";
					bInvalid = true;
				}
			}
			else if (id == "Username" || id == "username"){
				var nonnamepattern = /\W/;
				if(val.search(nonnamepattern) != -1){
					p_strError.str += "<li>You must enter a valid user name.";
					p_theElement.style.background = "red";
					bInvalid = true;
				}
			}
			else{
				var offendingChar = val.match(pattern);
				if((offendingChar != null) || (val.length == 0 && min_reqs != null && min_reqs != 0)){
					if (p_theElement.getAttribute('errorMsg')){
						p_strError.str += "<li>"+p_theElement.getAttribute('errorMsg') + ".";
					}
					else{
						p_strError.str += "<li>The character '" + offendingChar + "' is invalid in " + strTheElementId + ".";
					}
					p_theElement.style.background = "red";
					bInvalid = true;
				}
			}
		}
	}
	return bInvalid;
}
function validationShow(str){
	if (str != ""){
		return str;
	}
	else{
 		return true;
	}
}
function trimString(sInString){
	sInString = sInString.replace( /^\s+/g, "" );
	return sInString.replace( /\s+$/g, "" );
}
