/*********************************************************************************************/
function initFormFields(f) {
    for (var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
		e.prevValue = e.value;
	}

//	*********Add validation text fields below in the format*********
//	*********      f.ObjectName.FieldName = "The Name"		********
//	*********      f.ObjectName.Numeric = true;				********
//	*********      f.ObjectName.Min = 1;					********
//	*********      f.ObjectName.Max = 99999;				********
//  ****************************************************************

	if (f.name == 'frmRegistration') {

	} else if (f.name == 'frmApplication') {

		f.FirstName.FieldName = "First Name";
		f.LastName.FieldName = "Last Name";
		f.Email.FieldName = "Email Address";
		f.Phone.FieldName = "Telephone Number";
		f.Address.FieldName = "Mailing Address";
		f.City.FieldName = "City";
		f.State.FieldName = "State or Province";
		f.Zip.FieldName = "Zip or Postal Code";
		f.Country.FieldName = "Country";

		f.Industry.FieldName = "Your Primary Industry";
		f.Type.FieldName = "Type of your business";


		f.SVCode.FieldName = "Anti-Spam Confirmation Code";

	}


//		f.City.FieldName = "City";
//		f.State.FieldName = "State";
//
//		f.Phone.FieldName = "Phone Number";
//		f.Phone.Phone = true;
//
//		f.Zip.FieldName = "Zip Code";
//		f.Zip.ZipCode = true;
//
//		f.SSN.FieldName = "Social Security Number";
//		f.SSN.SSN = true;
//		f.DOB.FieldName = "Date of your Birth";
//		f.DOB.Date = true;
//		f.Pass1.FieldName = "Password";
//		f.Pass2.FieldName = "Retype Password";
//		f.Pass1.Password = true;		//	This property specifyes that this object is the password which should be compared to
//		f.Pass1.CompareTo = f.Pass2;	//	If property CompareTo exist, then the content of this object will be compared to
		// the content of the object stored inside of this property.


//	if (Mode == 'single') {
//		f.Q1.FieldName = 'Security question 1';
//		f.Q2.FieldName = 'Security question 2';
//		f.A1.FieldName = 'Answer 1';
//		f.A2.FieldName = 'Answer 2';
//		f.I1.FieldName = 'Primary Client Initials';
//		if (f.I2 != null) f.I2.FieldName = 'Spouse Initials';
//	}
//
//	if (Mode == 'agreement') {
//		f.Initials.FieldName = 'Initials';
//	}
//
//	if (Mode == 'contract') {
//		f.Initials.FieldName = 'Initials';
//	//	f.AttName.FieldName = 'Attorney Name';
//	//	f.Rep.FieldName = 'Representation';
//	//	alert(f.Rep.type);
//	}
}
/*********************************************************************************************/
function submitHandler(f) {

	var sBuf = "";
	var sFieldDelim = "";
	var sAttribDelim = "";
	var sChgFlg = "";
	f.ChangedFields.value = sBuf;
	if (validateForm(f)) {
	    for (var i = 0; i < f.elements.length; i++) {
			var e = f.elements[i];
			sBuf += (sFieldDelim + e.name );
			sFieldDelim = ";"
			sBuf += ( ","  + e.value);
		}

		f.ChangedFields.value = sBuf;
	//	f.submit()
		return true
	}
	return false
}
/*********************************************************************************************/
function ValidateDate(field){
	var ObjName = eval('Err' + field.name);
	var i, day, month, year, leap = 0, err = 0;
	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var Separator = "/";
	DateValue = DateField.value;

	for (i = 0; i < DateValue.length; i++) {									// Delete all chars except 0..9
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) DateTemp = DateTemp + DateValue.substr(i,1);
	}
	DateValue = DateTemp;
	// Always change date to 8 digits - string. If year is entered as 2-digit / always assume 20xx
	if (DateValue.length == 6) DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2);
	if (DateValue.length != 8) err = 19;
	year = DateValue.substr(4,4);
	if (year == 0) err = 20;														// year is wrong if year = 0000
	month = DateValue.substr(0,2);												// Validation of month
	if ((month < 1) || (month > 12)) err = 21;

	day = DateValue.substr(2,2);													// Validation of day
	if (day < 1) err = 22;
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) leap = 1;		// Validation leap-year / february / day
	if ((month == 2) && (leap == 1) && (day > 29)) err = 23;
	if ((month == 2) && (leap != 1) && (day > 28)) err = 24;
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
	  err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) err = 26;
	if ((day == 0) && (month == 0) && (year == 00)) {							// if 00 ist entered, no error, deleting the entry
	  err = 0; day = ""; month = ""; year = ""; Separator = "";
	}
	if (err == 0) {																// if no error, write the completed date to Input-Field (e.g. 01/31/2006)
		DateField.value = month + Separator + day + Separator + year;
		ObjName.style.visibility = 'hidden';
		return false;
	} else {			// Otherwise, display error
		ObjName.style.visibility = 'visible';
	//	DateField.select();
	//	DateField.focus();
		return true;
	}
}
/*********************************************************************************************/
function ValidateZipCode(field) {
	var ObjName = eval('Err' + field.name);
	var err = false;
	var Digits = "0123456789";
	var Out = "", FieldValue = field.value;
	for (var i = 0; i < FieldValue.length; i++) {						//	Delete all chars except 0..9
		if (Digits.indexOf(FieldValue.substr(i, 1)) >= 0) {
			Out += FieldValue.substr(i, 1);
		}
	}
	if ((Out.length !=5) && (Out.length !=9)) {
		ObjName.style.visibility = 'visible';
		return true;
	}


	if ((Out < 1) || (Out > 999999999)) {
		ObjName.style.visibility = 'visible';
		return true;
	} else {
		if (Out.length == 5) {
			field.value = Out;
			ObjName.style.visibility = 'hidden';
			return false;
		} else if (Out.length = 9) {
			field.value = Out.substr(0, 5) + "-" + Out.substr(5, 4);
			ObjName.style.visibility = 'hidden';
			return false;
		} else {
			ObjName.style.visibility = 'visible';
			return true;
		}
	}
}
/*********************************************************************************************/
function ValidateSSN(field) {
	var ObjName = eval('Err' + field.name);
	var err = false;
	var Digits = "0123456789";
	var Out = "", FieldValue = field.value;
	for (var i = 0; i < FieldValue.length; i++) {						//	Delete all chars except 0..9
		if (Digits.indexOf(FieldValue.substr(i, 1)) >= 0) {
			Out += FieldValue.substr(i, 1);
		}
	}

	if ((Out < 100000000) || (Out > 999999999)) {
	//	alert("Social Security Number is incorrect!");
	//	field.select();
	//	field.focus();
		ObjName.style.visibility = 'visible';
		return true;
	} else {
		field.value = Out.substr(0, 3) + "-" + Out.substr(3, 2) + "-" + Out.substr(5, 4);
		ObjName.style.visibility = 'hidden';
		return false;
	}
}
/*********************************************************************************************/
function ValidatePhone(field, Allow0Length) {
	var ObjName = eval('Err' + field.name);
	var err = false;
	var Digits = "0123456789";
	var Out = "", FieldValue = field.value;
	for (var i = 0; i < FieldValue.length; i++) {						//	Delete all chars except 0..9
		if (Digits.indexOf(FieldValue.substr(i, 1)) >= 0) {
			Out += FieldValue.substr(i, 1);
		}
	}

	if (Allow0Length) {
		if (Out.length == 0) {
			ObjName.style.visibility = 'hidden';
			field.value = '';
			return false;
		}
	}

	if ((Out < 2000000000) || (Out > 9989999999)) {
		ObjName.style.visibility = 'visible';
		return true;
	} else {
		field.value = "(" + Out.substr(0, 3) + ") " + Out.substr(3, 3) + "-" + Out.substr(6, 4);
		ObjName.style.visibility = 'hidden';
		return false;
	}
}
/*********************************************************************************************/
function ValidateCheckBoxes(f) {
//	for (var i = 0; i < f.elements.length; i++) {
	for (var i = 1; i <= 15; i++) {
		box = eval(f.name + ".C" + i);
		if (box.checked == false) {
		//	f.Initials.ErrMsg = "Please check all boxes to accept.";
		return true;
		}
	}
	return false;
}
/*********************************************************************************************/
function ValidateRadioButtons(f) {
	var RadSelected = false;
	for (var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
		if (e.type == "radio") {
//			alert(e.name + '(' + e.type + ')=' + e.value);
			if (e.checked) return false;
		}
	}
	return true;
}
/*********************************************************************************************/
function validateForm(f) {
	var msg = "";
	var empty_fields = "";
	var errors = "";

	for (var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
	//	alert(e.name + '(' + e.type + ')=' + e.value);
		if (((e.type == "text") || (e.type == "textarea") ||
			(e.type == "password") || (e.type == "select-one")) && !e.optional) {
			if (e.value == "") {				// first check if the field is empty
				if (e.FieldName != null){			// Next check to see if the fields are required
					empty_fields += "\n          " + e.FieldName ;
					continue;
				}
			}


			if (e.Numeric || (e.min != null) || (e.max != null)) {		// Now check for fields that are supposed to be numeric.
				var v = parseFloat(e.value);
				if (isNaN(v) || ((e.Min != null) && (v < e.Min)) || ((e.Max != null) && (v > e.Max))) {
					errors += "- The field " + e.name + " must be a number";
					if (e.Min != null) errors += " that is greater than " + e.Min;
					if (e.Max != null && e.Min != null)
						errors += " and less than " + e.Max;
					else if (e.Max != null)
						errors += " that is less than " + e.Max;
					errors += ".\n";
				}
			}

			if (e.SSN)		if (ValidateSSN(e)) errors += "- Social Security Number is invalid.\n"
			if (e.Date)		if (ValidateDate(e)) errors += "- Date is invalid.\n"
			if (e.Phone)	if (ValidatePhone(e, false)) errors += "- Phone number is invalid.\n"
			if (e.ZipCode)	if (ValidateZipCode(e)) errors += "- Zip Code is invalid.\n"
			if (e.CompareTo != null) {
				if(e.value != e.CompareTo.value) errors += "- Passwords do not match\n";
			}
		}
	}
	if (f.Mode.value == 'agreement') {
		if (ValidateCheckBoxes(f)) errors += "- Please check all boxes to accept\n";
		if (ValidateRadioButtons(f)) errors += "- Please select representation option\n";
	}
//		alert(f.Rep.type);

    /*	Now, if there were any errors, then display the messages, and
		return true to prevent the form from being submitted.  Otherwise
		return false	*/
    if (!empty_fields && !errors) return true

    msg  = "The form has not been submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "The following required field(s) are empty:" + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false
}
/*********************************************************************************************/
function LTrim( pAString ) {
/*
** Return the contents of pAString with any leading spaces removed.
** The original version of pAString is left intact.
*/

	var vTemp = "";
	var vLen  = pAString.length + 0;
	var vInc;

	for (vInc = 0; vInc < vLen; vInc++ )
	{
		if ( pAString.charAt( vInc ) != " " )
			break;
	}

	if ( vInc < vLen )
		vTemp = pAString.substring( vInc, vLen );

	return vTemp;
}
/*********************************************************************************************/
function RTrim( pAString ) {
/*
** Return the contents of pAString with any trailing spaces removed.
** The original version of pAString is left intact.
*/

	var vTemp = "";
	var vLen  = pAString.length + 0;
	var vInc;

	for (vInc = vLen - 1; vInc >= 0; vInc-- )
	{
		if ( pAString.charAt( vInc ) != " " )
			break;
	}

	if ( vInc >= 0 )
		vTemp = pAString.substring( 0, vInc + 1 );

	return vTemp;
}
/*********************************************************************************************/
function isLetter( pCheckVal ) {
/*
** Return true if the string contained in pCheckVal consists entirely of
** alpha characters, false otherwise.
*/

	var vAlphaNumerics = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var vCheckVal      = pCheckVal.toUpperCase();

	for ( i=0; i < vCheckVal.length; i++ )
	{
		if ( vAlphaNumerics.indexOf( vCheckVal.charAt( i ) ) == -1 )
		{
			return false;
		}
	}

	return true;
}
/*********************************************************************************************/
function isNumber( pCheckVal ) {
/*
** Return if the string contained in pCheckVal consists entirely of
** numeric characters, false otherwise. Does not handle exponential
** notation.
*/
	var vNumbers	= "0123456789";
	var vCheckVal	= LTrim( RTrim( pCheckVal ) );
	var vIsOkay	= true;

	for ( i=0; i < vCheckVal.length; i++ )
	{
		if ( vNumbers.indexOf( vCheckVal.charAt( i ) ) == -1 )
		{
			vIsOkay = false;
		   //	break;
		}
	}

	return vIsOkay;
}

/*********************************************************************************************/
function isNumeric( pCheckVal ) {
/*
** Return if the string contained in pCheckVal consists entirely of
** numeric characters, false otherwise. Does not handle exponential
** notation.
*/
	var vNumbers	= "-0123456789.";
	var vCheckVal	= LTrim( RTrim( pCheckVal ) );
	var vIsOkay	= true;

	for ( i=0; i < vCheckVal.length; i++ )
	{
		if ( vNumbers.indexOf( vCheckVal.charAt( i ) ) == -1 )
		{
			vIsOkay = false;
		   //	break;
		}
	}

	return vIsOkay;
}
/*********************************************************************************************/
function isMoney( pCheckVal ) {
/*
** Return true if the string contained in pCheckVal consists of
** a money representation, false otherwise. A null value
** for pCheckVal returns true.
*/

	var vCheckVal   = LTrim( RTrim( pCheckVal ) );
	var vIsOkay	= true;

	// Remove any leading "-" and "$".

	if ( vCheckVal.charAt( 0 ) == "-" )
		vCheckVal = vCheckVal.substring( 1 );

	if ( vCheckVal.charAt( 0 ) == "$" )
		vCheckVal = vCheckVal.substring( 1 );

	// Split apart at any "." and validate the dollars
	// and cents portions separately.

	var vDollarsAndCents = vCheckVal.split( '.' );

	vIsOkay = ( vDollarsAndCents.length <= 2 );	// Can't have more than one decimal

	if ( vIsOkay )
	{
		if ( vDollarsAndCents.length == 2 )
		{
			// Make sure the cents is exactly two digits and is numeric.

			vIsOkay = ( ( vDollarsAndCents[ 1 ].length == 2 ) && ( isNumber( vDollarsAndCents[ 1 ] ) ) );
		}

		if ( vIsOkay )
		{

			// Cents portion is okay or have no cents portion.
			// Split the dollars portion at any "," and validate.

			var vDollars = vDollarsAndCents[ 0 ].split( "," );

			if ( vDollars.length > 1 )
			{
				// Has at least one comma. Make sure each part is numeric and <= 999.
				// If not the left most comma-delimited field, make sure it's exactly
				// 3 chars long.

				for ( var i = 0; i < vDollars.length; i++ )
				{
					vIsOkay &= ( ( i == 0 ) || ( vDollars[ i ].length == 3 ) );
					vIsOkay &= ( ( isNumber( vDollars[ i ] ) ) && ( vDollars[ i ] <= 999 ) );
				}

			}
			else
			{
				// No commas. Make sure that what's left is numeric.

				vIsOkay = isNumber( vDollars[ 0 ] )
			}
		}
	}

	return vIsOkay;
}
/*********************************************************************************************/
