/*****************************************************************************/
function IsFormValid(f)
{
	var valid = true;
	var companyType = f.company_type.options[document.f.company_type.selectedIndex].value;

	if(companyType == "Staff Adjuster")
	{
		valid=false;
		alert("XactAnalysis accounts for Staff Adjusters are created and\n"
			+ "maintained by a representative from the insurance carrier.\n"
			+ "Please contact your administrator to have your ID created.\n"
			+ "If you are unsure of the appropriate individual from your\n"
			+ "company to contact, you may contact Xactware Sales at\n"
			+ "1-800-424-9228.");
		f.company_type.focus();
	}
	else if(f.contact_first_name.value == "" || f.contact_last_name.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your first and last name.");
		f.contact_first_name.focus();
	}
	else if(f.contact_email.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your email address.");
		f.contact_email.focus();
	}
	else if(f.contact_email.value.indexOf("@") == -1)
	{
		valid = false;
		alert("I'm sorry, the format of your email address appears to be invalid.  Please re-enter it.");
		f.contact_email.focus();
	}
	else if(f.primary_contact.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your \"Primary Contact\".");
		f.primary_contact.focus();
	}
	else if(f.email.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your company email address.");
		f.email.focus();
	}
	else if(f.email.value.indexOf("@") == -1)
	{
		valid = false;
		alert("I'm sorry, the format of your company email address appears to be invalid.  Please re-enter it.");
		f.email.focus();
	}
	else if((f.company_name.value == "") && (companyType == "Contractor"))
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your \"Company Name\".");
		f.company_name.focus();
	}
	else if(f.address.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your \"Business Address\".");
		f.address.focus();
	}
	else if(f.city.value == "")
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered the \"City\" your business is located in.");
		f.city.focus();
	}
	else if(!CheckZip(f.zip_code,true))
	{
		valid = false;
		f.zip_code.focus();
	}
	else if(!CheckPhone(f.phone,true))
	{
		valid = false;
		f.phone.focus();
	}
	else if((f.xactnet_address.value == "") && (companyType == "Independent Adjuster"))
	{
		valid = false;
		alert("I'm sorry, we can't proceed until you've entered your xactnet address.");
		f.xactnet_address.focus();
	}
	return valid;
}
/*****************************************************************************/
function Submit()
{
	var f = document.f;

	if(IsFormValid(f))
	{
		f.submit();
	}
}

/*****************************************************************************/
function CheckZip(obj,required)
{
    if (obj.value == "" || obj.value == null )
	{
		if (required)
		{
			alert("I'm sorry, we can't proceed until you've entered a  \"ZIP/Postal Code\" for your business.");
			return false;
		}
		else
		{
			return true;
		}
	}
	var returnValue = CheckFormat(StripNonAlphaNumeric(obj.value),"NNNNN");
	if (returnValue == "~ERROR~")
	{
		//returnValue = CheckFormat(StripNonNumeric(obj.value),"NNNNN-NNNN");
		if (returnValue == "~ERROR~")
		{
			returnValue = CheckFormat(StripNonAlphaNumeric(obj.value.toUpperCase()),"ANA NAN");
			if (returnValue == "~ERROR~")
			{
				returnValue = CheckFormat(StripNonAlphaNumeric(obj.value.toUpperCase()),"ANA");
				if (returnValue == "~ERROR~")
				{
					//if (required)
					//{
						alert("Zip/Postal Code appears to be invalid.  Please make the necessary corrections before proceeding.\n"
						+ "(US zip codes should be 5 digits.  Canadian postal codes should be in the format \"ANA NAN\" where A is letter in the alphabet and N is number).");
					//}
					return false;
				}
			}
		}
	}
	obj.value = returnValue;
	return true;
}

// ***************************************************************************
function CheckFormat(str,formatString)
{
	//alert(str);
	//alert(formatString);

	var ch			= "";
	var strch		= "";
	var returnValue	= StripNonFormatting(formatString);
	if (str.length != returnValue.length)
	{
		return "~ERROR~";
	}

	var returnValue = "";
	if (str!=null)
	{
		for (var i=0; i < formatString.length; i++)
		{
			ch=formatString.substring(i,i+1);
			if (ch == 'A' || ch == 'N' || ch == 'T' || ch == '?')
			{
				strch = str.substring(0,1);
				str = str.substring(1,str.length);

				if (ch == 'A' && IsAlpha(strch))
				{
					returnValue+=strch;
				}
				else if (ch == 'N' && IsNumeric(strch))
				{
					returnValue+=strch;
				}
				else if (ch == 'T' && (IsAlpha(strch) || IsNumeric(strch)))
				{
					returnValue+=strch;
				}
				else if (ch == '?')
				{
					returnValue+=strch;
				}
				else
				{
					return "~ERROR~"
				}
			} 
			else
			{
				if (ch == '~')
				{	
					i++;
				}
				returnValue += formatString.substring(i,i+1);
			}
		}
	}
	return returnValue;
}

/*****************************************************************************/
function IsAlpha(ch)
{
	if ((ch.length == 1) && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')))
	{
		return true;
	}
	else
	{
		return false
	}
}

/*****************************************************************************/
function IsNumeric(ch)
{
	if ((ch.length == 1) && (ch >= '0' && ch <= '9'))
	{
		return true;
	}
	else
	{
		return false
	}
}

/*****************************************************************************/
function StripNonAlphaNumeric(str)
{
	var returnValue	= "";
	var ch			= "";
	if (str!=null)
	{
		for (var i=0; i < str.length; i++)
		{
			ch=str.substring(i,i+1);
			if (IsNumeric(ch) || IsAlpha(ch))
			{
				returnValue+=ch;
			} 
		}
	}
	return returnValue;
}

/*****************************************************************************/
function StripNonFormatting(str)
{
	var returnValue = "";
	var ch        = "";
	if (str!=null)
	{
		for (var i=0; i < str.length; i++)
		{
			ch=str.substring(i,i+1);
			if (ch == 'A' || ch == 'N' || ch == 'T' || ch == '?')
			{
				returnValue+=ch;
			} 
			if (ch == '~')
			{	
				i++;
			}
		}
	}
	return returnValue;
}

/*****************************************************************************/
function CheckPhone(obj,required)
{
    if (obj.value == "" || obj.value == null )
	{
		if (required)
		{
			alert("I'm sorry, we can't proceed until you've entered your primary business phone number.");
			return false;
		}
		else
		{
			return true;
		}
	}

	var len = StripNonNumeric(obj.value).length;

	if(len < 10 || len > 14)
	{
		alert("There appears to be a problem with the phone number you entered. \nPlease include the area code and seven digit number.");
		obj.focus();
		return false;
	}
	return true;
}

/*****************************************************************************/
function StripNonNumeric(str)
{
	var returnValue	= "";
	var ch			= "";
	if (str!=null)
	{
		for (var i=0; i < str.length; i++)
		{
			ch=str.substring(i,i+1);
			if (IsNumeric(ch))
			{
				returnValue+=ch;
			} 
		}
	}
	return returnValue;
}


/*****************************************************************************/
function ClearForm()
{
	var i=0;

	for(i=0;document.f.elements[i]; i++)
	{
		if(document.f.elements[i].type == "text")
		{
			document.f.elements[i].value = "";
		}
	}
}

/****************************************************************************/
function companyTypeChange()
{
	var selCompType = document.f.company_type.options[document.f.company_type.selectedIndex].value;

	if(selCompType == "Contractor")
	{
		if (document.images)
		{
			document.images["cn_asterisk"].src = "/cxa/images/asterisk_on.gif";
			document.images["xna_asterisk"].src = "/cxa/images/asterisk_off.gif";
		}
	}
	if(selCompType == "Independent Adjuster")
	{
		if (document.images)
		{
			document.images["cn_asterisk"].src = "/cxa/images/asterisk_off.gif";
			document.images["xna_asterisk"].src = "/cxa/images/asterisk_on.gif";
		}
	}
	if(selCompType == "Staff Adjuster")
	{
		alert("XactAnalysis accounts for Staff Adjusters are created and\n"
			+ "maintained by a representative from the insurance carrier.\n"
			+ "Please contact your administrator to have your ID created.\n"
			+ "If you are unsure of the appropriate individual from your\n"
			+ "company to contact, you may contact Xactware Sales at\n"
			+ "1-800-424-9228.");
	}
}


/****************************************************************************/
function printTotalEstimates()
{
	for (var i=0; i < estCtrTotalEstimates.length; i++)
	{
		var c = estCtrTotalEstimates.substring(i, i+1);
		if ("0123456789".indexOf(c) > -1)
			document.write("<img src='images/odom-" + c + ".gif'/>");
		if (c == ",")
			document.write("<img src='images/odom-comma.gif'/>");
	}
}
function trim(str)
{
	if (str == null || str == "")
		return "";
	while (str.substring(0,1)==" " || str.substring(0,1)=="\n" || str.substring(0,1)=="\t")
		str = str.substring(1);
	return str;
}
function displayTotalEstimates(base, product)
{
	var baseDir = "";
	var subDir = "";
	if (base != null && base != "")
	{
		baseDir = base;
	}
	if (product != null && product != "")
	{
		subDir = product;
	}
						
		document.write("As of " + estCtrDateTime.substring(0,estCtrDateTime.indexOf(" at")) + ",<br>");
		document.write("Xactware has processed<br>");
		document.write("<!-- " + estCtrTotalEstimates + " -->");
		document.write("<table align=center cellpadding=0 cellspacing=0 style='margin-top:5px;margin-bottom:3px;border:2px solid;border-color:black #CCCCCC #EEEEEE #444444;'><tr><td nowrap>");
		document.write("<img src='" + baseDir + "/images/" + subDir + "/odom-blank.gif'>");
		for (var i=0; i<estCtrTotalEstimates.length; i++)
		{
			var c = estCtrTotalEstimates.substring(i, i+1);
			if ("0123456789".indexOf(c) > -1)
				document.write("<img src='" + baseDir + "/images/" + subDir + "/odom-" + c + ".gif'>");
			if (c == ",")
				document.write("<img src='" + baseDir + "/images/" + subDir + "/odom-comma.gif'>");
		}
		document.write("</td></tr></table>");
		document.write("estimates, with a total value of<br>");
		document.write("$" + trim(estCtrTotalDollar) + "!");
}
