/*
required	 - checks the value attribute of the field to have some value.
date		 - checks the value attribute of the field to have a valid date
mask		 - Values can = the values below...
minlength    - Value must contain that number of characters
emailAddress - Value must have a '@' and a '.' and have no spaces.
militaryTime - 0001-2359 ("am"/"pm" not required)
standardTime	 - 12:00am-11:59pm ("am"/"pm" required)

date, standardTime, and militaryTime attributes require for the page to also include date_funcs.js
*/

var mask_number = "\\D.*";			// Numbers only
var mask_alpha = "[^A-Za-z]";			// Alpha characters only
var mask_alpha_sp = "[^A-Za-z ]";		// Alpha character with space
var mask_alpha_slash_sp = "[^A-Za-z/ ]";		 // Alpha characters, slash / and space
var mask_alpha_slash = "[^A-Za-z/]";		 // Alpha characters and slash / 	
var mask_alpha_number = "[^A-Za-z0-9]";		// Alpha characters with numbers
var mask_alpha_number_sp = "[^A-Za-z0-9 ]";	// Alpha characters with numbers and space	
var mask_alpha_number_hyp = "[^.@A-Za-z0-9 \-]"; // Alpha characters with numbers, hyphen
var mask_number_period = "[^.0-9]"              //For floating number
var mask_number_hyp = "[^0-9\-]"              //For numbers and hyphens
var mask_number_percent = "[^0-9\%]"              //For numbers and percent sign
var mask_number_sp_hyp = "[^0-9\- ]"        //For numbers with space and hyphen.
var mask_alpha_number_hyp_und = "[^.@A-Za-z0-9\-_]"; // Alpha characters with numbers, hyphen,underscore.
var mask_number_dlrsign_per_com = "[^0-9$.,]"; // numbers, dollarsigns, periods, commas
var mask_no_space  ="[^A-Za-z0-9\!\@\#\$\%\&\*\(\)\-\_\=\+\\\:\;\"\'\<\,\>\.\/\?]";
var mask_space  ="[^A-Za-z0-9\!\@\#\$\%\&\*\(\)\-\_\=\+\\\:\;\"\'\<\,\>\.\/\? ]";

var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;

function markFieldOk(item)
{
	item.style.backgroundColor = "";
	item.style.color = "";
}
	
function markFieldBad(item)
{
	item.style.backgroundColor = "#FF0000"; //<-- RED
	item.style.color = "#FFFFFF";
}

function attributeExists( item, attribName ) {
	// ns6 returns '' when attrib doesn't exist. ns7 and ie5+ return null;
	var exists = ( item.getAttribute(attribName) != '' && item.getAttribute(attribName) != null );
	return exists;
}
	
function getErrors( obj )
{
	var i;
	var itemCount;
	var item;
	var msg = "";
	var ns7check = false;
	
	itemCount = obj.elements.length;
	
	// first mark all fields as valid ...
	for ( i=0; i<itemCount; i++) 
		markFieldOk( obj.elements[i] );
		
	for ( i=0; i<itemCount; i++ )
	{
		item = obj.elements[i];

		if ( attributeExists( item, 'disabled') )
			continue;

		if ( attributeExists( item, 'required') )
		{
			if ( item.value.length == 0 || item.value == 'NUL')
			{
				markFieldBad(item);
				msg += "<li>" + item.getAttribute('friendlyName') + " is a required field." + "</li>";
				continue;
			}
		}

		if ( attributeExists( item, 'minlength') )
		{
			if ( item.value.length < item.getAttribute('minlength') )
			{
				markFieldBad(item);
				msg += "<li>" + "A minimum of " + item.getAttribute('minlength') + " characters is required for the " + item.getAttribute('friendlyName') + " field."  + "</li>";
				continue;
			}
		}
		
		if ( attributeExists( item, 'emailAddress') )
		{ 
		    if ( item.value.length==0 ) 
				continue;

			var emailRegExp = new RegExp();
			emailRegExp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
			if ( item.value.match( emailRegExp ) ) {
				// do nothing
			} else {
				markFieldBad(item);
				msg += "<li>" + "Invalid email address"  + "</li>";
				continue;
			}
		}	 
			                                                       
		if ( attributeExists( item, 'date') )
		{
		    if ( item.value.length==0 ) 
				continue;
			    
		    if ( isValidDate(item.value) == false )
		    {
				markFieldBad(item);
				msg += "<li>" + item.getAttribute('friendlyName') + ": the date is not valid (mm/dd/yyyy)" ;
				continue;
			}
		}
			
		if ( attributeExists( item, 'standardTime') )
		{
		    if ( item.value.length==0 )
				continue;
			    
		    var bMilitaryTime = false;
		    if ( isValidTime(item.value, bMilitaryTime) == false )
		    {
				markFieldBad(item);
				msg += "<li>" + item.getAttribute('friendlyName') + ": the time is not valid (hh:mi am|pm)." ;
				continue;
			}
		} 
			
		if ( attributeExists( item, 'militaryTime') )
		{
		    if ( item.value.length==0 )
				continue;
			    
		    var bMilitaryTime = true;
		    if ( isValidTime(item.value, bMilitaryTime) == false )
		    {
				markFieldBad(item);
				msg += "<li>" + item.getAttribute('friendlyName') + ": the time is not valid (hh:mi)." ;
				continue;
			}
		}

		if ( attributeExists( item, 'mask') )
		{
		    var exp = new RegExp(eval(item.getAttribute('mask')));
		    var arr = exp.exec(item.value);
				
			if (arr != null)
			{	
				markFieldBad(item);
				if (item.getAttribute('mask') == "mask_no_space")
					msg += "<li>The " + item.getAttribute('friendlyName') + " field cannot contain ";
				else
					msg += "<li>The " + item.getAttribute('friendlyName') + " field can only contain ";
					 
				if (item.getAttribute('mask') == "mask_number") msg += "numbers (0-9).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_number_hyp") msg += "numbers (0-9) and hyphens (-).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_number_percent") msg += "numbers (0-9) and percent sign (%).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha") msg += "letters (A-Z).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_number") msg += "letters (A-Z) and numbers (0-9).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_sp") msg += "letters (A-Z) and spaces.\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_number_sp") msg += "letters (A-Z), numbers (0-9) and spaces.\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_number_hyp") msg += "letters (A-Z), numbers (0-9) and hyphens (-).\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_slash") msg += "letters (A-Z) and a slash (/) between the last name and first name.\n\n" +"</li>";
				if (item.getAttribute('mask') == "mask_alpha_slash_sp") msg += "letters (A-Z) and a slash (/) between the last name and first name.\n\n" + "</li>";
				if (item.getAttribute('mask') == "mask_number_period") msg += "numbers (0-9) and a decimal point (.)." + "</li>";
				if (item.getAttribute('mask') == "mask_number_sp_hyp") msg += "numbers (0-9), hyphen (-) and space." + "</li>";
				if (item.getAttribute('mask') == "mask_alpha_number_hyp_und") msg += "numbers (0-9), hyphens (-), underscores (_), period (.) and space." + "</li>";
				if (item.getAttribute('mask') == "mask_number_dlrsign_per_com") msg += "numbers (0-9), dollar signs ($), periods (.) and commas (,)." + "</li>";
				if (item.getAttribute('mask') == "mask_no_space") msg += "spaces." + "</li>";

				continue;
			}
		}		
	}
							
	return msg;		
}

function DisplayErrors(errMsg)
{
	var errHTML = "<font color=red>Please correct the following errors:</font><ul>" + errMsg + "</ul>";
	document.getElementById('errTextSpan').innerHTML = errHTML;
	openWindow("/dialog.html", 350, 300);
}

document.write("<span style='display:none' id='errTextSpan'></span>");

