var message = "";

// Mask types
var NUMBER = 1;
var STRING = 2;
var CURRENCY = 4;
var FLOAT = 8;
var DATE = 16;
var TIME = 32;
var PHONE = 64;
var YEAR = 128;
var PASSWORD = 256;
var EMAIL = 512;
var REQUIRED = true;
var UNREQUIRED = false;

/****************************************************************************
* Trims spaces from a string value
*****************************************************************************/
function trimStr(varTrim)
{
	var Len;
	var Idx;
	var Chr;
	var Str;
	var Sgn;

	Str = varTrim;

	Len = Str.length;
	for (Idx = 0; Idx < Len; Idx++)
	{
		Chr = Str.charAt(Idx);
		if (Chr != ' ') break;
	}

	Str = Str.substring(Idx, Len);
	Len = Str.length;

	for (Idx = Len - 1; Idx >= 0; Idx--)
	{
		Chr = Str.charAt(Idx);
		if (Chr != ' ') break;
	}
	return Str.substring(0, Idx + 1);
}

/****************************************************************************
* Trims spaces from a number value
*****************************************************************************/
function trimNum(varTrim)
{
	var Len;
	var Idx;
	var Chr;
	var Str;
	var Sgn;

	Str = trimStr(varTrim);
	Len = Str.length;

	if (Str.charAt(0) != '-')
	{
		Idx = 0;
		Sgn = '';
	}
	else
	{
		Idx = 1;
		Sgn = '-';
	}

	if (Str.substring(Idx, Len) != '')
	{
		for ( ; Idx < Len; Idx++)
		{
			Chr = Str.charAt(Idx);
			if (Chr != '0') break;
		}
		Str = Str.substring(Idx, Len);

		if (Str == '') Str = '0';
	}
	return Sgn + Str;
}

/****************************************************************************
* This all spaces the value ie.. a b c returns abc
*****************************************************************************/
function removeBlanks(val)
{
	var Len;
	var Idx;
	var Chr = "";
	var Str;
	var Sgn;
	var retVal = "";

	Str = val;

	Len = Str.length;
	for (Idx = 0; Idx < Len; Idx++)
	{
		Chr = Str.charAt(Idx);
		if (Chr != ' ')
		{
			retVal = retVal + Chr;
		}
	}

	return retVal;
}

/****************************************************************************
* This verifies that the value is a number
*****************************************************************************/
function isNumber(varNumber)
{
	var Str;
	Str = trimNum(varNumber);
	return Str == parseInt(Str) + '';
}

/****************************************************************************
* This verifies that the value is a number
*****************************************************************************/
function isFloat(varFloat)
{
	var Str;
	Str = trimNum(varFloat);
	return Str = parseFloat(Str) + '';
}

/****************************************************************************
* This verifies that the value is a valid date
*****************************************************************************/
function isDate(varDate)
{
	var dd;
	var mm;
	var yy;
	var Len;
	var Idx;
	var Chr;
	var Cnt;
	var mdy = new Array('','',''); //mm dd yy

	Len = varDate.length;

	for (Cnt = 0, Idx = 0; Idx < Len; Idx++)
	{
		Chr = varDate.charAt(Idx);
	
		if (Chr == '/' || Chr == '-')
	   	{
	        	Cnt++;
	        	if (Cnt > 2)
	        	{
	            		return false;
	        	}
	   	}
	   	else
	   	{
			if (Chr != ' ')
	        	{
	        		mdy[Cnt] = mdy[Cnt] + Chr;
	        	}
	   	}
	}

	mm = mdy[0];
	dd = mdy[1];
	yy = mdy[2];

	if (!isNumber(mm) || !isNumber(dd) || !isNumber(yy))
     	{
        	return false;
     	}

	if (dd < 1 || mm < 1 || mm > 12 || yy < 0 || (yy > 99 && yy < 1850) || yy > 2100 )
	{
		return false;
	}

	if (mm == 2)
	{
		if (dd < 1 || dd > 29)
		{
			return false;
		}
		else
		{
			if (dd == 29 && yy % 4 != 0)
			{
				return false;
			}
		}
	}
	else
	{
		if (mm == 4 || mm == 6|| mm == 9 || mm == 11)
	  	{
	       		if (dd < 1 || dd > 30)
	       		{
	           		return false;
	       		}
	  	}
	  	else
	  	{
	       		if (dd < 1 || dd > 31)
	       		{
	           		return false;
	       		}
	  	}
	}

	if (yy < 50)
	{
		yy = 2000 + parseInt(yy);
	}
	else
	{
		if (yy < 100)
		{
			yy = 1900 + parseInt(yy);
		}
	}

	yy = '0000' + yy;
	Len = yy.length;
	yy = yy.substring(Len - 4, Len);
	
	mm = '00' + mm;
	Len = mm.length;
	mm = mm.substring(Len - 2, Len);
	
	dd = '00' + dd;
	Len = dd.length;
	dd = dd.substring(Len - 2, Len);
	
	return '' + yy + mm + dd;
}

function formatDate(varDate)
{
	if (varDate.length == 8)
	{
		return varDate.substring(4, 6) + '/' + varDate.substring(6, 8) + '/' + varDate.substring(0, 4);
	}
	else
	{
		return false;
	}
}


function formatfld(fld,type)
{
	return;
}

function validate(fld)
{
	var retval = true;
	var val = trimStr(fld.value);
	var len = val.length;
	var ndate;
	var apos;
	var dotpos;
	var lastpos;

	if ( NUMBER == fld.type )
	{
		if ( !isNumber(val) ) 
		{
			fld.errormsg = "Please enter a valid value in " + fld.label + ".\n";
			retval = false;
		}
	}
	else if ( STRING == fld.type ) 
	{
		val = trimStr(fld.value);	
	}
	else if ( CURRENCY == fld.type ) 
	{
	}
	else if ( FLOAT == fld.type )
	{
		if ( !isFloat(val) ) 
		{
			fld.errormsg = "Please enter a valid value in " + fld.label + ".\n";
			retval = false;
		}
	}
	else if ( DATE == fld.type )
	{
		ndate = isDate(val);
		if ( !ndate ) 
		{
			fld.errormsg = "Please enter a valid date (mm/dd/yyyy) in " + fld.label + ".\n";
			retval = false;
		}
		else
		{
			val = formatDate(ndate);
		}
	}
	else if ( TIME == fld.type )
	{
	}
	else if ( PHONE == fld.type )
	{
		val = removeBlanks(val);
	}
	else if ( YEAR == fld.type )
	{
		ndate = isDate("01/01/" + val);
		
		if ( !ndate ) 
		{
			fld.errormsg = "Please enter valid Year in " + fld.label + ".\n";
			retval = false;
		}
		else
		{
			val = ndate.substring(0, 4);	
		}
	}
	else if ( PASSWORD == fld.type )
	{
		if ( val.length < 5 )
		{
			fld.errormsg = "Please enter at least 5 characters in the " + fld.label + "field.\n";
			retval = false;
		}
	}
	else if ( EMAIL == fld.type )
	{
		apos = val.indexOf("@"); 
		dotpos = val.lastIndexOf(".");
		lastpos = val.length-1;

		if ( apos < 1 || dotpos - apos < 2 || lastpos - dotpos > 3 || lastpos - dotpos < 2) 
		{	
			fld.errormsg = "Please enter a valid " + fld.label + ".\n";
			retval = false;
		}

	}
	fld.value = val;
	
	if ( len > 0 )
	{		
		return retval;
	}
	else
	{
		fld.errormsg = "";
		return true;
	}
}

/****************************************************************************
* This class provides field validation for all control types.
*****************************************************************************/
function Field(label, ctl, req, type)
{
	// Set object properties
	this.ok = true; // Indicates if the field passes validation
	this.errormsg = "";
	this.label = label;
	this.type = type;
	this.hasdata = true;
	this.ctltype = ctl.type;
	this.format = formatfld;
	this.value = ctl.value;
	this.name = ctl.name;
	this.selectedIndex = ctl.selectedIndex;

	// Check if control has been populated
	if ( this.ctltype == "text" || this.ctltype == "textarea" || this.ctltype == "password" || this.ctltype == "file" || this.ctltype == "hidden")
	{
		// If it is required and has no data set the error message string
		// and the ok property
		if ( req && this.value.length < 1 )
		{
			this.errormsg = "Please enter " + label  + ".\n" ;
			this.ok = false;
			this.hasdata = false;
		}
		else if ( !validate( this ) )
		{
			this.ok = false;
		}
		else if ( this.value.length < 1 ) 
		{
			this.hasdata = false;
		}
	}
	else if ( this.ctltype == "checkbox" || this.ctltype == "radio" )
	{
		if ( ( req && !ctl.checked ) || ( !ctl.checked ) ) 
		{
			this.ok = false;
		}
		else 
		{
			this.ok = true;
			this.value = ctl.value;
		}
	}
	else if ( this.ctltype == "select-one" )
	{
		this.value = ctl[this.selectedIndex].value;
		if ( req && ( this.value.length < 1 || this.value == -1 ) )
		{
			this.errormsg = "Please select a " + label  + ".\n" ;
			this.ok = false;
			this.hasdata = false;
		}
	}
	
	ctl.value = this.value;
	return;
}

/****************************************************************************
* This class provides check box validation with an "other text box".
*****************************************************************************/
function MultiCheckBoxWithOther(label,ctl,req,count,otherctl)
{
	var i;
	var ctlArray = new Array(count);
	var other = new Field("Other "+ label, otherctl, UNREQUIRED, STRING);
	this.errormsg = "";
	this.label = label;

	// Fill array with Field objects
	for ( i = 0; i < count; i++ )
	{
		ctlArray[i] = new Field(label, ctl[i], UNREQUIRED, NUMBER);
	}

	// If it is required make sure at least one is checked
	if ( req )
	{
		for ( i = 0; i < count; i++ )
		{
			if ( !ctlArray[i].ok )
			{
				this.errormsg = "Please select at least one " + label + ".\n";
				this.value = "";
			}
			else 
			{
				this.errormsg = ""; 
				this.value = ctlArray[i].value;
				break;
			}
		}
	}

	// The last element is always the "other" checkbox
	// if it is checked make suer data is entered in the textbox
	if ( ctlArray[count-1].ok && !other.hasdata )
	{
		this.errormsg = "Please enter the Other " + label + ".\n";
	}

	return;
}

function validateDOB( dob, fldlabel )
{	
	var today = new Date;
	var dtdob = new Date(dob);
	var errstr = "";
	var tdate, sdate;

	tdate = today.getYear();
	sdate = dtdob.getYear();

	if ( sdate >= tdate )
	{
		errstr = "Please enter a valid Date of Birth in " + fldlabel + ".";
	}
	
	return errstr;
}


  if(window.attachEvent)
    window.attachEvent("onload",setListeners);
  
  function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
    }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#a0d0ff"){
      event.srcElement.style.backgroundColor = ""; /* color of choice for AutoFill */
     // document.all['googleblurb'].style.display = "block";
    }
  }
