function trim(inputString) 
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user

} // Ends the "trim" function


//Check to validate the email address
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function highlightTextField(field) {
		field.style.borderColor = 'red';
		field.style.borderStyle = 'solid';
		field.style.borderWidth = '1px;';
}

function nohighlightTextField(field) {
		field.style.borderColor = '';
		field.style.borderStyle = '';
		field.style.borderWidth = '';
}

function highlightSelectField(field) {
		field.style.backgroundColor = 'red';
		field.style.color = 'black';
}		

function nohighlightSelectField(field) {
		field.style.backgroundColor = '';
		field.style.color = '';
}		

function checkAppForm()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field;
	
	//Check company name
	trimmed_string = trim(document.leadsapp.company_name.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Company Name\n';
		highlightTextField(document.leadsapp.company_name);
		focus_field = "document.leadsapp.company_name.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.company_name);	}

	//Check address
	trimmed_string = trim(document.leadsapp.address1.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(document.leadsapp.address1);
		focus_field = "document.leadsapp.address1.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.address1);	}

	//Check city
	trimmed_string = trim(document.leadsapp.city.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(document.leadsapp.city);
		focus_field = "document.leadsapp.city.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.city);	}

	//Check State
	if (document.leadsapp.state.value == '') 
	{
		ErrorMsg = ErrorMsg + '- State\n';
		highlightSelectField(document.leadsapp.state);
		focus_field = "document.leadsapp.state.focus()";
	}
	else
	{	nohighlightSelectField(document.leadsapp.state); }

	//Check zipcode
	trimmed_string = trim(document.leadsapp.zip.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Zipcode\n';
		highlightTextField(document.leadsapp.zip);
		focus_field = "document.leadsapp.zip.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.zip);	}

	
	//Check Company Phone
	if( ( (document.leadsapp.comp_phone1.value.length != 3) || isNaN(document.leadsapp.comp_phone1.value) ) ||
	    ( (document.leadsapp.comp_phone2.value.length != 3) || isNaN(document.leadsapp.comp_phone2.value) ) ||
	    ( (document.leadsapp.comp_phone3.value.length != 4) || isNaN(document.leadsapp.comp_phone3.value) ) )	 
	{
		ErrorMsg = ErrorMsg + '- Company Phone\n';
		highlightTextField(document.leadsapp.comp_phone1);
		highlightTextField(document.leadsapp.comp_phone2);
		highlightTextField(document.leadsapp.comp_phone3);
		focus_field = "document.leadsapp.comp_phone1.focus()";
	}
	else
	{	
		nohighlightTextField(document.leadsapp.comp_phone1);	
		nohighlightTextField(document.leadsapp.comp_phone2);	
		nohighlightTextField(document.leadsapp.comp_phone3);	
	}

	//Check the Fax Number if it is not empty
	if( ( (document.leadsapp.comp_fax1.value.length != 3) || isNaN(document.leadsapp.comp_fax1.value) ) ||
	    ( (document.leadsapp.comp_fax2.value.length != 3) || isNaN(document.leadsapp.comp_fax2.value) ) ||
	    ( (document.leadsapp.comp_fax3.value.length != 4) || isNaN(document.leadsapp.comp_fax3.value) ) )	 
	{
		ErrorMsg = ErrorMsg + '- Company Fax\n';
		highlightTextField(document.leadsapp.comp_fax1);
		highlightTextField(document.leadsapp.comp_fax2);
		highlightTextField(document.leadsapp.comp_fax3);
		focus_field = "document.leadsapp.comp_fax1.focus()";
	}
	else
	{	
		nohighlightTextField(document.leadsapp.comp_fax1);	
		nohighlightTextField(document.leadsapp.comp_fax2);	
		nohighlightTextField(document.leadsapp.comp_fax3);	
	}

	//Check contact last
	trimmed_string = trim(document.leadsapp.contact_last.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Your Last Name\n';
		highlightTextField(document.leadsapp.contact_last);
		focus_field = "document.leadsapp.contact_last.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.contact_last);	}

	//Check contact first
	trimmed_string = trim(document.leadsapp.contact_first.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Your First Name\n';
		highlightTextField(document.leadsapp.contact_first);
		focus_field = "document.leadsapp.contact_first.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.contact_first);	}

	//Check contact title
	trimmed_string = trim(document.leadsapp.contact_title.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Title\n';
		highlightTextField(document.leadsapp.contact_title);
		focus_field = "document.leadsapp.contact_title.focus()";
	}
	else
	{	nohighlightTextField(document.leadsapp.contact_title);	}

	//Check the email address
	if (!isEmailAddr(document.leadsapp.contact_email.value))
	{
		ErrorMsg = ErrorMsg + '- Email Address\n';
		highlightTextField(document.leadsapp.contact_email);
		focus_field = "document.leadsapp.contact_email.focus()";
	}
	else if(document.leadsapp.contact_email.value != document.leadsapp.contact_email2.value)
	{
		ErrorMsg = ErrorMsg + '- Confirm Email Address\n';
		nohighlightTextField(document.leadsapp.contact_email); 
		highlightTextField(document.leadsapp.contact_email2);
		focus_field = "document.leadsapp.contact_email2.focus()";
	}
	else
	{	
		nohighlightTextField(document.leadsapp.contact_email); 
		nohighlightTextField(document.leadsapp.contact_email2); 
	}

	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;
	} 
	else 
	{
		return true;
	}
}

function CheckSignup2()
{
	var ErrorMsg = '';
	var trimmed_string;
	var con = '';

	//Check numleads
	if (document.signup2.numleads.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Leads wanted each day\n';
		document.signup2.numleads.focus();
	}

	//Check States
	var states_checked;
	for (var i = 0; i<document.signup2.elements.length; i++) 
	{
    	 if ((document.signup2.elements[i].name.indexOf('states') > -1)) 
		{
            if (document.signup2.elements[i].checked) 
			{
                states_checked += document.signup2.elements[i].value + ' ';
            }
        }
	}	
	if(!states_checked)
	{	ErrorMsg = ErrorMsg + '- States you would like leads from\n';	}

	//Check Credit
	var credit_checked;
	for (var i = 0; i<document.signup2.elements.length; i++) 
	{
    	 if ((document.signup2.elements[i].name.indexOf('credit') > -1)) 
		{
            if (document.signup2.elements[i].checked) 
			{
                credit_checked += document.signup2.elements[i].value + ' ';
            }
        }
	}	
	if(!credit_checked)
	{	ErrorMsg = ErrorMsg + '- Type of credit ratings\n';	}

	//Check House Types
	var houses_checked;
	for (var i = 0; i<document.signup2.elements.length; i++) 
	{
    	 if ((document.signup2.elements[i].name.indexOf('houses') > -1)) 
		{
            if (document.signup2.elements[i].checked) 
			{
                houses_checked += document.signup2.elements[i].value + ' ';
            }
        }
	}	
	if(!houses_checked)
	{	ErrorMsg = ErrorMsg + '- House Types\n';	}

	//Check loans
	var loans_checked;
	for (var i = 0; i<document.signup2.elements.length; i++) 
	{
    	 if ((document.signup2.elements[i].name.indexOf('loans') > -1)) 
		{
            if (document.signup2.elements[i].checked) 
			{
                loans_checked += document.signup2.elements[i].value + ' ';
            }
        }
	}	
	if(!loans_checked)
	{	ErrorMsg = ErrorMsg + '- Loan purposes\n';	}

	if(ErrorMsg == '') 
	{
		if (document.signup2.numleads.value == '0') 
		{
			con = confirmLink(this, 'WARNING: 0 Leads per day means you will not receive daily lead delivery. You can change this\nfrom the My Account->Edit lead preferences->Change Regular Criteria feature');
		}
	}

	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		return false;
	} 
	else 
	{
		if( ( con == true ) || ( parseInt( document.signup2.numleads.value) > 0) )
		{	return true;	}
		else
		{	return false;	}

	}

}

function Checkboxes(fieldname,value) 
{
	for(x=0;x<document.forms.signup2.elements.length;x++)	
	{
		if(document.forms.signup2.elements[x].name == fieldname)	
		{
			document.forms.signup2.elements[x].checked = value;
		}
	}
}


function checkCAA()
{
	var ErrorMsg = '';
	var con = '';

	//Check Numleads
	if (document.signup2.numleads.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Number of Leads per day\n';
		highlightSelectField(signup2.numleads);
		document.signup2.numleads.focus();
	}
	else
	{	nohighlightSelectField(signup2.numleads); }

	if(ErrorMsg == '') 
	{
		if (document.signup2.numleads.value == '0') 
		{
			con = confirmLink(this, 'WARNING: 0 Leads per day means you will not receive daily lead delivery. You can change this\nfrom the My Account->Edit lead preferences->Change Regular Criteria feature');
		}
	}

	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		return false;
	} 
	else 
	{
		//If they choose 0 leads and don't confirm
		if( ( con == true ) || ( parseInt( document.signup2.numleads.value) > 0) )
		{	return true;	}
		else
		{	return false;	}
	}

}

var confirmMsg  = 'Please Confirm the following';
function confirmLink(theLink, theSqlQuery)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(confirmMsg + ' :\n' + theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function


function checkCBA()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Check broker name
	trimmed_string = trim(document.cba.company_name.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Broker Name\n';
		highlightTextField(cba.company_name);
		focus_field = "document.cba.company_name.focus()";
	}
	else
	{	nohighlightTextField(cba.company_name);	}

	//Check contact first
	trimmed_string = trim(document.cba.contact_first.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Contact First Name\n';
		highlightTextField(cba.contact_first);
		focus_field = "document.cba.contact_first.focus()";
	}
	else
	{	nohighlightTextField(cba.contact_first);	}

	//Check contact last
	trimmed_string = trim(document.cba.contact_last.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Contact Last Name\n';
		highlightTextField(cba.contact_last);
		focus_field = "document.cba.contact_last.focus()";
	}
	else
	{	nohighlightTextField(cba.contact_last);	}

	//Check contact title
	trimmed_string = trim(document.cba.contact_title.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Contact Title\n';
		highlightTextField(cba.contact_title);
		focus_field = "document.cba.contact_title.focus()";
	}
	else
	{	nohighlightTextField(cba.contact_title);	}

	//Check address 1
	trimmed_string = trim(document.cba.address1.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(cba.address1);
		focus_field = "document.cba.address1.focus()";
	}
	else
	{	nohighlightTextField(cba.address1);	}

	//Check city
	trimmed_string = trim(document.cba.city.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(cba.city);
		focus_field = "document.cba.city.focus()";
	}
	else
	{	nohighlightTextField(cba.city);	}

	//Check zipcode
	trimmed_string = trim(document.cba.zipcode.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- ZIP\n';
		highlightTextField(cba.zipcode);
		focus_field = "document.cba.zipcode.focus()";
	}
	else
	{	nohighlightTextField(cba.zipcode);	}


	//Check Phone
	if( ( (document.cba.phone1.value.length != 3) || isNaN(document.cba.phone1.value) ) ||
	    ( (document.cba.phone2.value.length != 3) || isNaN(document.cba.phone2.value) ) ||
	    ( (document.cba.phone3.value.length != 4) || isNaN(document.cba.phone3.value) ) )	 
	{
		ErrorMsg = ErrorMsg + '- Phone\n';
		highlightTextField(cba.phone1);
		highlightTextField(cba.phone2);
		highlightTextField(cba.phone3);
		focus_field = "document.cba.phone1.focus()";
	}
	else
	{	
		nohighlightTextField(cba.phone1);	
		nohighlightTextField(cba.phone2);	
		nohighlightTextField(cba.phone3);	
	}

	//Check Fax
	if( ( (document.cba.fax1.value.length != 3) || isNaN(document.cba.fax1.value) ) ||
	    ( (document.cba.fax2.value.length != 3) || isNaN(document.cba.fax2.value) ) ||
	    ( (document.cba.fax3.value.length != 4) || isNaN(document.cba.fax3.value) ) )	 
	{
		ErrorMsg = ErrorMsg + '- fax\n';
		highlightTextField(cba.fax1);
		highlightTextField(cba.fax2);
		highlightTextField(cba.fax3);
		focus_field = "document.cba.fax1.focus()";
	}
	else
	{	
		nohighlightTextField(cba.fax1);	
		nohighlightTextField(cba.fax2);	
		nohighlightTextField(cba.fax3);	
	}
	
	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}


}

function checkEmailLogin()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//check email address
	if (!isEmailAddr(document.emaillogin.email_login.value))
	{
		ErrorMsg = ErrorMsg + '- Email Login\n';
		highlightTextField(emaillogin.email_login);
		focus_field = "document.emaillogin.email_login.focus()";
	}
	else
	{	nohighlightTextField(emaillogin.email_login);	}

	//Check Password
	trimmed_string = trim(document.emaillogin.new_pass1.value);
	if (trimmed_string.length == 0 || trimmed_string.length < 6) 
	{
		ErrorMsg = ErrorMsg + '- New Password ( must be at least 6 chars)\n';
		highlightTextField(emaillogin.new_pass1);
		focus_field = "document.emaillogin.new_pass1.focus()";
	}
	else if(trimmed_string != document.emaillogin.new_pass2.value)
	{
		ErrorMsg = ErrorMsg + '- Confirm New Password does not match the New Password\n';
		nohighlightTextField(emaillogin.new_pass1);	
		highlightTextField(emaillogin.new_pass2);
		focus_field = "document.emaillogin.new_pass2.focus()";
	}
	else
	{	
		nohighlightTextField(emaillogin.new_pass1);	
		nohighlightTextField(emaillogin.new_pass2);	
	}

	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}

}

function checkCCA()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Amount to fund account
	trimmed_string = trim(document.cca.amount.value);	
	var err = 0;
	if (trimmed_string.length == 0) 
	{	err++;	}
	else
	{
		var decimal_format = ".";
		var check_char = trimmed_string.indexOf(decimal_format);
		if (check_char != -1)
		{	err++;	}
		else if( isNaN(trimmed_string) == true )
		{	err++;	}
		else if( document.cca.amount.value < 250 )
		{	err++;	}
	}
	if(err > 0)
	{
		ErrorMsg = ErrorMsg + '- Amount to fund (Minimum $250. Whole numbers only)\n';
		highlightTextField(document.cca.amount);
		focus_field = "document.cca.amount.focus()";
	}
	else
	{	nohighlightTextField(document.cca.amount);	}

	//Card Number
	if( (document.cca.cardnumber.value.length < 16) || isNaN(document.cca.cardnumber.value) )
	{
		ErrorMsg = ErrorMsg + '- Card Number\n';
		highlightTextField(document.cca.cardnumber);
		focus_field = "document.cca.cardnumber.focus()";
	}
	else
	{	nohighlightTextField(document.cca.cardnumber);	}
	
	//Card Verification Number
	if( (document.cca.cvmvalue.value.length < 3) || isNaN(document.cca.cvmvalue.value) )
	{
		ErrorMsg = ErrorMsg + '- Card Verification Number\n';
		highlightTextField(document.cca.cvmvalue);
		focus_field = "document.cca.cvmvalue.focus()";
	}
	else
	{	nohighlightTextField(document.cca.cvmvalue);	}

	//Check firstname
	trimmed_string = trim(document.cca.firstname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- First Name\n';
		highlightTextField(cca.firstname);
		focus_field = "document.cca.firstname.focus()";
	}
	else
	{	nohighlightTextField(cca.firstname);	}

	//Check lastname
	trimmed_string = trim(document.cca.lastname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Last Name\n';
		highlightTextField(cca.lastname);
		focus_field = "document.cca.lastname.focus()";
	}
	else
	{	nohighlightTextField(cca.lastname);	}

	//Check Address
	trimmed_string = trim(document.cca.address1.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(cca.address1);
		focus_field = "document.cca.address1.focus()";
	}
	else
	{	nohighlightTextField(cca.address1);	}

	//Check City
	trimmed_string = trim(document.cca.city.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(cca.city);
		focus_field = "document.cca.city.focus()";
	}
	else
	{	nohighlightTextField(cca.city);	}

	//Check State
	if (document.cca.state.value == '') 
	{
		ErrorMsg = ErrorMsg + '- State\n';
		highlightSelectField(cca.state);
		document.cca.state.focus();
	}
	else
	{	nohighlightSelectField(cca.state); }

	//Check Zip
	trimmed_string = trim(document.cca.zip.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Billing Zip\n';
		highlightTextField(cca.zip);
		focus_field = "document.cca.zip.focus()";
	}
	else
	{	nohighlightTextField(cca.zip);	}

	//Check Phone
	trimmed_string = trim(document.cca.phone.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Billing Phone\n';
		highlightTextField(cca.phone);
		focus_field = "document.cca.phone.focus()";
	}
	else
	{	nohighlightTextField(cca.phone);	}

	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}
}


function checkCCAb()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';
	var err = 0;

	//Amount to fund account
	trimmed_string = removeCurrency(document.ccab.amount.value);	//remove currency
	if( isNaN(trimmed_string))										//Check if number
	{	err++;	}
	else
	{	
		//Check if there is a decimal
		var decimal_format = ".";
		var check_char = trimmed_string.indexOf(decimal_format);
		if (check_char != -1)
		{	
			var amount_array = trimmed_string.split(".");		
			//check for 2 decimal places
			if(amount_array[1].length != 2)
			{	err++ }
			//check if decimal is larger than 0
			else if( String(amount_array[1]) != '00' )
			{	err++;	}

		}
		
		if ( parseInt(trimmed_string) < document.ccab.minimum.value )
		{ err++; }
	}
	
	if(err > 0)
	{
		if(document.ccab.minimum.value == '500')
		{
			ErrorMsg = ErrorMsg + '- Amount to fund (Minimum $' + document.ccab.minimum.value + ' for initial deposit. Whole numbers only)\n';
		}
		else
		{
			ErrorMsg = ErrorMsg + '- Amount to fund (Minimum $' + document.ccab.minimum.value + ' deposit. Whole numbers only)\n';
		}			

		highlightTextField(document.ccab.amount);
		focus_field = "document.ccab.amount.focus()";
	}
	else
	{	nohighlightTextField(document.ccab.amount);	}

	//Card Number
	trimmed_string = trim(document.ccab.cardnumber.value);
	if( (trimmed_string.length == 0) || ( isNaN(document.ccab.cardnumber.value) ) )
	{
		ErrorMsg = ErrorMsg + '- Card Number\n';
		highlightTextField(document.ccab.cardnumber);
		focus_field = "document.ccab.cardnumber.focus()";
	}
	else
	{	nohighlightTextField(document.ccab.cardnumber);	}
	
	//Card Verification Number
	if( (document.ccab.cvmvalue.value.length < 3) || isNaN(document.ccab.cvmvalue.value) )
	{
		ErrorMsg = ErrorMsg + '- Card Verification Number\n';
		highlightTextField(document.ccab.cvmvalue);
		focus_field = "document.ccab.cvmvalue.focus()";
	}
	else
	{	nohighlightTextField(document.ccab.cvmvalue);	}

	//Check firstname
	trimmed_string = trim(document.ccab.firstname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- First Name\n';
		highlightTextField(ccab.firstname);
		focus_field = "document.ccab.firstname.focus()";
	}
	else
	{	nohighlightTextField(ccab.firstname);	}

	//Check lastname
	trimmed_string = trim(document.ccab.lastname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Last Name\n';
		highlightTextField(ccab.lastname);
		focus_field = "document.ccab.lastname.focus()";
	}
	else
	{	nohighlightTextField(ccab.lastname);	}

	//Check Address
	trimmed_string = trim(document.ccab.address1.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(ccab.address1);
		focus_field = "document.ccab.address1.focus()";
	}
	else
	{	nohighlightTextField(ccab.address1);	}

	//Check City
	trimmed_string = trim(document.ccab.city.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(ccab.city);
		focus_field = "document.ccab.city.focus()";
	}
	else
	{	nohighlightTextField(ccab.city);	}

	//Check State
	if (document.ccab.state.value == '') 
	{
		ErrorMsg = ErrorMsg + '- State\n';
		highlightSelectField(ccab.state);
		document.ccab.state.focus();
	}
	else
	{	nohighlightSelectField(ccab.state); }

	//Check Zip
	trimmed_string = trim(document.ccab.zip.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Billing Zip\n';
		highlightTextField(ccab.zip);
		focus_field = "document.ccab.zip.focus()";
	}
	else
	{	nohighlightTextField(ccab.zip);	}

	//Check Phone
	trimmed_string = trim(document.ccab.phone.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Billing Phone\n';
		highlightTextField(ccab.phone);
		focus_field = "document.ccab.phone.focus()";
	}
	else
	{	nohighlightTextField(ccab.phone);	}

	//Check errormessage
	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}
}


function update_ccab_amount()
{
	var leadvalue = document.ccab.leadcost.value;
	var leadsamount = document.ccab.prefund.value;
	
	document.ccab.amount.value = leadsamount * leadvalue;

}

function CalculatePremium()
{
	var totalamt = '';

	for(x=0; x<document.forms.cab.elements.length; x++)
	{

		if(document.forms.cab.elements[x].checked)
		{
			if( (document.forms.cab.elements[x].name == 'propvalue') || 
				(document.forms.cab.elements[x].name == 'loanamount') ||
				(document.forms.cab.elements[x].name == 'ltv') ||
				(document.forms.cab.elements[x].name == 'mortrate'))
			{
				totalamt = totalamt + parseInt(document.cab.additionalprice.value);			
				showNotes(document.forms.cab.elements[x].name);
			}
		}
		else
		{	
			hideNotes(document.forms.cab.elements[x].name);	
		}

		if(document.forms.cab.elements[x].name == 'exclusive')
		{
			if(document.forms.cab.exclusive.value == 'yes')
			{
				totalamt = parseInt(totalamt + document.cab.exclusiveprice.value);
				showNotes('exclusive');
			}
			if(document.forms.cab.exclusive.value == 'no')
			{
				totalamt = parseInt(totalamt + document.cab.baseprice.value);
				showNotes('nonexclusive');
			}							
		}
	}

	document.getElementById('LeadTotalPremium').innerHTML = parseInt(totalamt);
}


function showNotes(field)
{
	switch(field) 
	{
	    case 'nonexclusive':
			document.getElementById('exclusive_html').innerHTML = '+ $' + document.cab.baseprice.value + '.00  -  Non-Exclusive Leads may be sold to up to 3 Brokers.';
			break;
	    case 'exclusive':
			document.getElementById('exclusive_html').innerHTML = '+ $' + document.cab.exclusiveprice.value + '.00  -  Exclusive Leads are only sold to you.';
			break;
	    case 'propvalue':
			document.getElementById('propvalue_html').innerHTML = '+ $' + document.cab.additionalprice.value + '.00  -  The Value of the Property.';
			break;
	    case 'loanamount':
			document.getElementById('loanamount_html').innerHTML = '+ $' + document.cab.additionalprice.value + '.00  -  The Amount of the Loan that the Borrower applied for.';
			break;
	    case 'ltv':
			document.getElementById('ltv_html').innerHTML = '+ $' + document.cab.additionalprice.value + '.00  -   The Percentage of the Property Value Borrowed. (Loan Amount / Property Value = LTV).';
			break;
	    case 'mortrate':
			document.getElementById('mortrate_html').innerHTML = '+ $' + document.cab.additionalprice.value + '.00  -   The Interest Rate of the First Mortgage.';
			break;
	}

}

function CalculatePremium2()
{
	var totalamt = '';

	for(x=0; x<document.forms.cab.elements.length; x++)
	{

		if(document.forms.cab.elements[x].checked)
		{
			if( (document.forms.cab.elements[x].name == 'propvalue') || 
				(document.forms.cab.elements[x].name == 'loanamount') ||
				(document.forms.cab.elements[x].name == 'ltv') ||
				(document.forms.cab.elements[x].name == 'mortrate'))
			{
				totalamt = totalamt + parseInt(document.cab.additionalprice.value);			
				showMiniNotes(document.forms.cab.elements[x].name);
			}
		}
		else
		{	
			hideNotes(document.forms.cab.elements[x].name);	
		}

		if(document.forms.cab.elements[x].name == 'exclusive')
		{
			if(document.forms.cab.exclusive.value == 'yes')
			{
				totalamt = parseInt(totalamt + document.cab.exclusiveprice.value);
				showMiniNotes('exclusive');
			}
			if(document.forms.cab.exclusive.value == 'no')
			{
				totalamt = parseInt(totalamt + document.cab.baseprice.value);
				showMiniNotes('nonexclusive');
			}							
		}
	}

	document.getElementById('LeadTotalPremium').innerHTML = parseInt(totalamt);
}

function showMiniNotes(field)
{
	switch(field) 
	{
	    case 'nonexclusive':
			document.getElementById('exclusive_html').innerHTML = '<font color=\"#000000\"><strong>+ $' + document.cab.baseprice.value + '.00  -  Non-Exclusive Leads.</strong></font>';
			break;
	    case 'exclusive':
			document.getElementById('exclusive_html').innerHTML = '<font color=\"#000000\"><strong>+ $' + document.cab.exclusiveprice.value + '.00  -  Exclusive Leads.</strong></font>';
			break;
	    case 'propvalue':
			document.getElementById('propvalue_html').innerHTML = '<font color=\"#FFFFFF\"><strong>+ $' + document.cab.additionalprice.value + '.00  -  The Value of the Property.</strong></font>';
			break;
	    case 'loanamount':
			document.getElementById('loanamount_html').innerHTML = '<font color=\"#FFFFFF\"><strong>+ $' + document.cab.additionalprice.value + '.00  -  The Amount of the Loan.</strong></font>';
			break;
	    case 'ltv':
			document.getElementById('ltv_html').innerHTML = '<font color=\"#FFFFFF\"><strong>+ $' + document.cab.additionalprice.value + '.00  -   The Loan to Value Ratio.</strong></font>';
			break;
	    case 'mortrate':
			document.getElementById('mortrate_html').innerHTML = '<font color=\"#FFFFFF\"><strong>+ $' + document.cab.additionalprice.value + '.00  -   The Interest Rate of the First Mortgage.</strong></font>';
			break;
	}

}

function hideNotes(field)
{
	switch(field) 
	{
	    case 'nonexclusive':
			document.getElementById('exclusive_html').innerHTML = '';
			break;
	    case 'exclusive':
			document.getElementById('exclusive_html').innerHTML = '';
			break;
	    case 'propvalue':
			document.getElementById('propvalue_html').innerHTML = '';
			break;
	    case 'loanamount':
			document.getElementById('loanamount_html').innerHTML = '';
			break;
	    case 'ltv':
			document.getElementById('ltv_html').innerHTML = '';
			break;
	    case 'mortrate':
			document.getElementById('mortrate_html').innerHTML = '';
			break;
	}
}


function checkCAB()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Checking the property value
	if(document.cab.propvalue.checked == true)
	{
		if( (document.cab.minprop.value == "") || (document.cab.maxprop.value == "") )
		{
			ErrorMsg = ErrorMsg + '- Property Value Range\n';
			highlightSelectField(document.cab.minprop);
			highlightSelectField(document.cab.maxprop);
			focus_field = "document.cab.minprop.focus()";
		}
		else if( parseInt(document.cab.minprop.value) > parseInt(document.cab.maxprop.value) )
		{
			ErrorMsg = ErrorMsg + '- Property Value Range\n';
			highlightSelectField(document.cab.minprop);
			highlightSelectField(document.cab.maxprop);
			focus_field = "document.cab.minprop.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minprop);	
			nohighlightSelectField(document.cab.maxprop);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minprop);	
		nohighlightSelectField(document.cab.maxprop);	
	}

	//Checking the Requested Loan Amount
	if(document.cab.loanamount.checked == true)
	{
		if( (document.cab.minloan.value == "") || (document.cab.maxloan.value == "") )
		{
			ErrorMsg = ErrorMsg + '- Requested Loan Amount\n';
			highlightSelectField(document.cab.minloan);
			highlightSelectField(document.cab.maxloan);
			focus_field = "document.cab.minloan.focus()";
		}
		else if( parseInt(document.cab.minloan.value) > parseInt(document.cab.maxloan.value) )
		{
			ErrorMsg = ErrorMsg + '- Requested Loan Amount\n';
			highlightSelectField(document.cab.minloan);
			highlightSelectField(document.cab.maxloan);
			focus_field = "document.cab.minloan.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minloan);	
			nohighlightSelectField(document.cab.maxloan);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minloan);	
		nohighlightSelectField(document.cab.maxloan);	
	}

	//Checking the LTV
	if(document.cab.ltv.checked == true)
	{
		if(document.cab.maxltv.value == "")
		{
			ErrorMsg = ErrorMsg + '- LTV\n';
			highlightSelectField(document.cab.maxltv);
			highlightSelectField(document.cab.maxltv);
			focus_field = "document.cab.maxltv.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.maxltv);	
			nohighlightSelectField(document.cab.maxltv);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.maxltv);	
		nohighlightSelectField(document.cab.maxltv);	
	}

	//Checking the 1st Mortgage Rate
	if(document.cab.mortrate.checked == true)
	{
		if( (document.cab.minrate.value == "") || (document.cab.maxrate.value == "") )
		{
			ErrorMsg = ErrorMsg + '- 1st Mortgage Rate\n';
			highlightSelectField(document.cab.minrate);
			highlightSelectField(document.cab.maxrate);
			focus_field = "document.cab.minrate.focus()";
		}
		else if( parseInt(document.cab.minrate.value) > parseInt(document.cab.maxrate.value) )
		{
			ErrorMsg = ErrorMsg + '- 1st Mortgage Rate\n';
			highlightSelectField(document.cab.minrate);
			highlightSelectField(document.cab.maxrate);
			focus_field = "document.cab.minrate.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minrate);	
			nohighlightSelectField(document.cab.maxrate);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minloan);	
		nohighlightSelectField(document.cab.maxloan);	
	}

	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}


}

function checkCAB1()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Checking the property value
	if(document.cab.propvalue.checked == true)
	{
		if( (document.cab.minprop.value == "") || (document.cab.maxprop.value == "") )
		{
			ErrorMsg = ErrorMsg + '- Property Value Range\n';
			highlightSelectField(document.cab.minprop);
			highlightSelectField(document.cab.maxprop);
			focus_field = "document.cab.minprop.focus()";
		}
		else if( parseInt(document.cab.minprop.value) > parseInt(document.cab.maxprop.value) )
		{
			ErrorMsg = ErrorMsg + '- Property Value Range\n';
			highlightSelectField(document.cab.minprop);
			highlightSelectField(document.cab.maxprop);
			focus_field = "document.cab.minprop.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minprop);	
			nohighlightSelectField(document.cab.maxprop);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minprop);	
		nohighlightSelectField(document.cab.maxprop);	
	}

	//Checking the Requested Loan Amount
	if(document.cab.loanamount.checked == true)
	{
		if( (document.cab.minloan.value == "") || (document.cab.maxloan.value == "") )
		{
			ErrorMsg = ErrorMsg + '- Requested Loan Amount\n';
			highlightSelectField(document.cab.minloan);
			highlightSelectField(document.cab.maxloan);
			focus_field = "document.cab.minloan.focus()";
		}
		else if( parseInt(document.cab.minloan.value) > parseInt(document.cab.maxloan.value) )
		{
			ErrorMsg = ErrorMsg + '- Requested Loan Amount\n';
			highlightSelectField(document.cab.minloan);
			highlightSelectField(document.cab.maxloan);
			focus_field = "document.cab.minloan.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minloan);	
			nohighlightSelectField(document.cab.maxloan);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minloan);	
		nohighlightSelectField(document.cab.maxloan);	
	}

	//Checking the LTV
	if(document.cab.ltv.checked == true)
	{
		if(document.cab.maxltv.value == "")
		{
			ErrorMsg = ErrorMsg + '- LTV\n';
			highlightSelectField(document.cab.maxltv);
			highlightSelectField(document.cab.maxltv);
			focus_field = "document.cab.maxltv.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.maxltv);	
			nohighlightSelectField(document.cab.maxltv);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.maxltv);	
		nohighlightSelectField(document.cab.maxltv);	
	}

	//Checking the 1st Mortgage Rate
	if(document.cab.mortrate.checked == true)
	{
		if( (document.cab.minrate.value == "") || (document.cab.maxrate.value == "") )
		{
			ErrorMsg = ErrorMsg + '- 1st Mortgage Rate\n';
			highlightSelectField(document.cab.minrate);
			highlightSelectField(document.cab.maxrate);
			focus_field = "document.cab.minrate.focus()";
		}
		else if( parseInt(document.cab.minrate.value) > parseInt(document.cab.maxrate.value) )
		{
			ErrorMsg = ErrorMsg + '- 1st Mortgage Rate\n';
			highlightSelectField(document.cab.minrate);
			highlightSelectField(document.cab.maxrate);
			focus_field = "document.cab.minrate.focus()";
		}
		else
		{	
			nohighlightSelectField(document.cab.minrate);	
			nohighlightSelectField(document.cab.maxrate);	
		}
	}
	else
	{	
		nohighlightSelectField(document.cab.minloan);	
		nohighlightSelectField(document.cab.maxloan);	
	}

	if(document.cab.terms.checked == false)
	{
		ErrorMsg = ErrorMsg + '- You must agree to the terms of service.\n';
		highlightTextField(document.cab.terms);
		focus_field = "document.cab.terms.focus()";
	}
	else
	{	nohighlightTextField(document.cab.terms);	}

	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}


}

//Open blank pages - show leads
function load(lead_id) {
var target = "lms.php?showlead=" + lead_id;
var load = window.open(target,'','scrollbars=no,height=450,width=500,resizable=no,toolbar=no,location=no,status=no,menubar=no');
}

function showComplaint(lead_id) {
var target = "lms.php?complaint=" + lead_id;
var load = window.open(target,'','scrollbars=no,height=197,width=550,resizable=no,toolbar=no,location=no,status=no,menubar=no');
}


//Dreamweaver pop up window
function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}

function checkContact()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Check Company Name
	trimmed_string = trim(document.ContactForm.cname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Company Name\n';
		highlightTextField(ContactForm.cname);
		focus_field = "document.ContactForm.cname.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.cname);	}

	//Check First Name
	trimmed_string = trim(document.ContactForm.fname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- First Name\n';
		highlightTextField(ContactForm.fname);
		focus_field = "document.ContactForm.fname.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.fname);	}

	//Check Last Name
	trimmed_string = trim(document.ContactForm.lname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Last Name\n';
		highlightTextField(ContactForm.lname);
		focus_field = "document.ContactForm.lname.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.lname);	}

	//Check the email address
	if (!isEmailAddr(document.ContactForm.email.value))
	{
		ErrorMsg = ErrorMsg + '- Email Address\n';
		highlightTextField(ContactForm.email);
		focus_field = "document.ContactForm.email.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.email); }

	//Check Company Phone
	trimmed_string = trim(document.ContactForm.companyphone.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Company Phone\n';
		highlightTextField(ContactForm.companyphone);
		focus_field = "document.ContactForm.companyphone.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.companyphone);	}

	//Check subject
	if (document.ContactForm.subject.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Subject\n';
		highlightSelectField(ContactForm.subject);
		focus_field = "document.ContactForm.subject.focus()";
	}
	else
	{	nohighlightSelectField(ContactForm.subject); }

	//Comments
	trimmed_string = trim(document.ContactForm.comments.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Comments\n';
		highlightTextField(ContactForm.comments);
		focus_field = "document.ContactForm.comments.focus()";
	}
	else
	{	nohighlightTextField(ContactForm.comments);	}



	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}

}

function checkFrontLogin()
{
	var ErrorMsg = '';
	var trimmed_string;
	var focus_field = '';

	//Check the email address
	if (!isEmailAddr(document.frontlogin.email.value))
	{
		ErrorMsg = ErrorMsg + '- Email Address\n';
		highlightTextField(frontlogin.email);
		focus_field = "document.frontlogin.email.focus()";
	}
	else
	{	nohighlightTextField(frontlogin.email); }

	//Password
	if (document.frontlogin.password.value.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Password\n';
		highlightTextField(frontlogin.password);
		focus_field = "document.frontlogin.password.focus()";
	}
	else
	{	nohighlightTextField(frontlogin.password);	}

	//Terms and conditions
	if(document.frontlogin.terms.checked == false)
	{
		ErrorMsg = ErrorMsg + '- You must agree to the terms of service.\n';
		highlightTextField(frontlogin.terms);
		focus_field = "document.frontlogin.terms.focus()";
	}
	else
	{	nohighlightTextField(frontlogin.terms);	}
	

	if(ErrorMsg != '') 
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		eval(focus_field);
		return false;		
	} 
	else 
	{
		return true;
	}

}

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS:
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid
  numeric value in the rounded to 2 decimal
  places.  If not, returns original value.
*************************************************/
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else
      return strValue;
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
  strValue - source string containing commas.

RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is
  returned.

REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match,
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}

function update_prefund()
{
	var prefund_amount = document.ccab.leadcost.value * document.ccab.prefund.value;
	prefund_amount = addCurrency(prefund_amount + '.00');
	document.ccab.amount.value = prefund_amount;
}

function toggleBoxes(boxname) {
	var boxes = window.document.getElementsByName(boxname);

	for (i=0; i<boxes.length; i++) {
		(boxes[i].checked == true) ? boxes[i].checked = false : boxes[i].checked = true;
	}
	
}