function isEmail (email)
{  // if (isEmpty(s)) 
   //    if (isEmail.arguments.length == 1) return defaultEmptyOK;
   //    else return (isEmail.arguments[1] == true);  
   
    // is s whitespace?
   // if (isWhitespace(s)) return false;
    var s=email.value;
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) { 
	   alert("Please enter valid email address"); 
	   email.focus();
	    return false;
	 }  
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) { alert('Please enter valid email address.'); email.focus(); return false; }
    else return true;
}

function IsNumeric(element)
{
   sText = element.value
   var ValidChars = '0123456789';
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
		alert('Enter integer values, e.g., 2');
       // element.value="";
	    element.focus();
	     return false;		
         }
      }
	 return true;
}

function IsNumericWithDot(element)
{
   sText = element.value
   var ValidChars = '0123456789.';
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
		 alert('Enter numeric values, e.g., 2.4');
		// element.value="";
		 element.focus();
		 return false;	
         }
      }
    return true;
}
function IsNumericWithDotNoAlert(element)
{
   sText = element.value
   var ValidChars = '0123456789.';
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
//		 alert('Enter numeric values, e.g., 2.4');
		// element.value="";
//		 element.focus();
		 return false;	
         }
      }
    return true;
}
function isMoney(element){
   sText = element.value
   var ValidChars = '0123456789.,';
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
		 alert('Enter numeric values, e.g., 2.4');
		// element.value="";
		 element.focus();
		 return false;	
         }
      }
    return true;
}

function isBlank(p)
{ 
  var val = p.value
  var len = val.length;
  var i;
  for(i = 0; i < len ; ++i)
    { 
    if (val.charAt(i) != ' '){return false;}
    }

	return true;
}
function validateEmpty(form){
  //  alert('hello');   
var x=form;
if (x.title.value=='0'){ 
   alert('Please select the contact\'s title.');
   x.title.focus();
  return false;
 }
if (isBlank(x.firstName)) {
    alert('Please enter the contact\'s first name.');
    x.firstName.focus();
    return false;
}
if (isBlank(x.lastName)) {
    alert('Please enter the contact\'s last name.');
    x.lastName.focus();
    return false;
}
if (isBlank(x.areaCode) ) {
    alert('Please enter the contact\'s complete phone number.');
    x.areaCode.focus();
    return false;
}
if(!IsNumeric(x.areaCode)){   
    return false;
}

if (isBlank(x.phoneF)) {
     alert('Please enter the contact\'s complete phone number.');
    x.phoneF.focus();
    return false;
}
if( !IsNumeric(x.phoneF)){   
    return false;
}

if (isBlank(x.phoneL)) {
    alert('Please enter the contact\'s complete phone number.');
    x.phoneL.focus();
    return false;
}
if(!IsNumeric(x.phoneL)){   
    return false;
}

if (isBlank(x.email)) {
    alert('Please enter the contact\'s e-mail address.');
    x.email.focus();
    return false;
}
if(!isEmail(x.email)){
    return false;
}

if (isBlank(x.agencyName)) {
    alert('Please enter the agency\'s name.');
    x.agencyName.focus();
    return false;
}
if (isBlank(x.address1)) {
    alert('Please enter the agency\'s address.');
    x.address1.focus();
    return false;
}
if (isBlank(x.city)) {
    alert('Please enter the agency\'s city.');
    x.city.focus();
    return false;
}
if (x.state.value=='0') {
    alert('Please select a state.');
    x.state.focus();
    return false;
}
if (isBlank(x.zip)) {
    alert('Please enter the agency\'s zip code.');
    x.zip.focus();
    return false;
}
if(!IsNumeric(x.zip)){   
    return false;
}

if(!(x.zip.value.length==5)){
   alert('Please enter 5 digits for zip code');
   x.zip.focus();
   return false;
}

if (isBlank(x.areaCodeFax)) {
    alert('Please enter the agency\'s complete fax number.');
    x.areaCodeFax.focus();
    return false;
}

if(!IsNumeric(x.areaCodeFax)){   
    return false;
}
if (isBlank(x.phoneFFax)) {
    alert('Please enter the agency\'s  complete fax number.');
    x.phoneFFax.focus();
    return false;
}
if(!IsNumeric(x.phoneFFax)){   
    return false;
}
if (isBlank(x.phoneLFax)) {
    alert('Please enter the agency\'s  complete fax number.');
    x.phoneLFax.focus();
    return false;
}
if(!IsNumeric(x.phoneLFax)){   
    return false;
}
if (x.estMonth.value=='0') {   
    alert('Please enter the complete date the agency was established.');
    x.estMonth.focus();
    return false;
}

if (x.estDay.value=='0' || x.estDay.value=='') {
    if(x.estMonth.value!='0'){
	   creatOptions(findDays(x.estMonth.value),x.estDay);
	}
	alert('Please enter the complete date the agency was established.');       
 	x.estDay.focus();
    return false;
}

if (isBlank(x.estYear)) {
    alert('Please enter the complete date the agency was established.');
    x.estYear.focus();
    return false;
}
if(!IsNumeric(x.estYear)){   
    return false;
}
if (isBlank(x.odpp)) {
    alert('Please enter the agency\'s total staff. (Enter numeric values, e.g., 2).');
    x.odpp.focus();
    return false;
}
if(!IsNumeric(x.odpp)){   
    return false;
}
if (isBlank(x.empProd)) {
    alert('Please enter the agency\'s total staff. (Enter numeric values, e.g., 2).');
    x.empProd.focus();
    return false;
}
if(!IsNumeric(x.empProd)){   
    return false;
}
if (isBlank(x.nonEmpProd)) {
    alert('Please enter the agency\'s total staff. (Enter numeric values, e.g., 2).');
    x.nonEmpProd.focus();
    return false;
}
if(!IsNumeric(x.nonEmpProd)){   
    return false;
}
if (isBlank(x.custServRep)) {
    alert('Please enter the agency\'s total staff. (Enter numeric values, e.g., 2).');
    x.custServRep.focus();
    return false;
}
if(!IsNumeric(x.custServRep)){   
    return false;
}
if (isBlank(x.othClerStaff)) {
    alert('Please enter the agency\'s total staff. (Enter numeric values, e.g., 2).');
    x.othClerStaff.focus();
    return false;
}
if(!IsNumeric(x.othClerStaff)){   
    return false;
}
if (isBlank(x.prePCPV)) {
    alert('Please provide the property and casualty premium volume for the previous 12 months. (Enter numeric values, e.g., 2).');
    x.prePCPV.focus();
    return false;
}
if(!isMoney(x.prePCPV)){   
    return false;
}
if (isBlank(x.prvPCR)) {
    alert('Please provide the property and casualty revenue for the previous 12 months. (Enter numeric values, e.g., 2).');
    x.prvPCR.focus();
    return false;
}
if(!isMoney(x.prvPCR)){   
    return false;
}
if (isBlank(x.preLHR)) {
    alert('Please provide the Life and Health revenue for the previous 12 months. (Enter numeric values, e.g., 2).');
    x.preLHR.focus(); 
    return false;
}
if(!isMoney(x.preLHR)){   
    return false;
}
if (isBlank(x.estPCPV)) {
    alert('Please provide the estimated property and casualty premium volume for the next 12 months. (Enter numeric values, e.g., 2).');
    x.estPCPV.focus();
    return false;
}
if(!isMoney(x.estPCPV)){   
    return false;
}
if (isBlank(x.estPCR)) {
    alert('Please provide the estimated property and casualty revenue for the next 12 months. (Enter numeric values, e.g., 2).');
    x.estPCR.focus();
    return false;
}
if(!isMoney(x.estPCR)){   
    return false;
}
if (isBlank(x.estLHR)) {
    alert('Please provide the estimated Life and Health revenue for the next 12 months. (Enter numeric values, e.g., 2).');
    x.estLHR.focus();
    return false;
}
if(!isMoney(x.estLHR)){   
    return false;
}

//if (isBlank(x.agent)) {
//	x.agent.focus();
//	 return false;
//}  


if(!IsNumericWithDot(x.agent)){   
    return false;
}

//if (isBlank(x.broker)) { 
//	x.broker.focus();
//	 return false;
//}

if(!IsNumericWithDot(x.broker)){   
    return false;
}
 
//if (isBlank(x.comLine)) {
//	x.comLine.focus();
//	 return false;
//}

if(!IsNumericWithDot(x.comLine)){   
    return false;
}

//if (isBlank(x.perLine)) {
//	x.perLine.focus();
//	 return false;
//}

if(!IsNumericWithDot(x.perLine)){   
    return false;
}
if(isBlank(x.best)){
   alert('Please indicate the percentage of business placed with carriers rated A- or better by A.M. Best. (Enter numeric values, e.g., 2)');
   x.best.focus();
   return false;
}
if(!IsNumericWithDot(x.best)){   
    return false;
}
if(x.radioValue.value=='0'){
   alert('Please indicate if any claim or suit has been brought against the agency during the past five years.');
   x.suitAgency[0].focus();
   return false;
}
if(isBlank(x.claim) && x.radioValue.value=='yes'){
   alert('You have indicated that a claim has been made or a suit has been brought against the agency during the past five years. Please enter the total number of claims. (Enter numeric values, e.g., 2)');
   x.claim.focus();
   return false;
}
if(!isBlank(x.claim) && !IsNumeric(x.claim)){   
    return false;
}
if(isBlank(x.totalPaid) && x.radioValue.value=='yes'){
   alert('You have indicated that a claim has been made or suit has been brought against the agency during the past five years. Please enter the total dollar amount paid or reserved including damages and expenses for the claims. (Enter numeric values, e.g., 2)');
   x.totalPaid.focus();
   return false;
}
if(!isBlank(x.totalPaid) && !isMoney(x.totalPaid)){   
    return false;
}

if(isBlank(x.errInsure)){
   alert('Please enter the number of years of continuous errors and omissions insurance coverage for the agency. (Enter numeric values, e.g., 2)');
   x.errInsure.focus();
   return false;
}
if(!IsNumericWithDot(x.errInsure)){   
    return false;
}
if(x.liability.value=='0'){
   alert('Please select limit of liability desired.');
   x.liability.focus();
   return false;
}
if(x.deduct.value=='0'){
   alert('Please select deductible/retention desired.');
   x.deduct.focus();
   return false;
}
if(x.effMonth.value=='0'){
   alert('Please select your desired effective date.');
   x.effMonth.focus();
   return false;
}

if(x.effDay.value=='0' || x.effDay.value==''){
  if(x.effMonth.value!='0'){
   creatOptions(findDays(x.effMonth.value),x.effDay);
 }
   alert('Please select your desired effective date.');   
 //  x.effMonth.click();  
   x.effDay.focus();
   return false;
}

if(isBlank(x.effYear)){
   alert('Please select your desired effective date.');
   x.effYear.focus();
   return false;
 }
 if(!IsNumeric(x.effYear)){   
    return false;
}
 if(!checkTotal()){ return false;}
}

function calTotalBusi(){
   var x = document.requestForm;
   var agent = x.agent;
   var broker = x.broker;
   if(!isBlank(agent) && IsNumericWithDotNoAlert(agent) && !isBlank(broker) && IsNumericWithDotNoAlert(broker)){
       x.totalBusiPlacedDis.value= parseFloat(agent.value)+ parseFloat(broker.value);
   }
   else{
       x.totalBusiPlacedDis.value="";
   }
   return true;
}


function calTotalMix(){
   var x = document.requestForm;
   var com = x.comLine;
   var per = x.perLine;
   if(!isBlank(com) && IsNumericWithDotNoAlert(com) && !isBlank(per) && IsNumericWithDotNoAlert(per)){
       x.totalBusiMixDis.value= parseFloat(com.value)+ parseFloat(per.value);
   }
   else{
       x.totalBusiMixDis.value="";
   }
   return true;
}
function checkTotal(){
    var x = document.requestForm; 
    if ( parseFloat(x.totalBusiPlacedDis.value) != 100 || isBlank(x.totalBusiPlacedDis)) {
           alert('Please indicate the percentage of business placed as an Agent and/or Broker. The total must equal 100%. (Enter numeric values, e.g., 2).');
	      x.agent.focus();
	      return false;
   }  
   else if ( parseFloat(x.totalBusiMixDis.value) != 100 || isBlank(x.totalBusiMixDis)) {
         alert('Please indicate the agency\'s mix of business for Commercial Lines and Personal Lines. The total must equal 100%. (Enter numeric values, e.g., 2)');
	     x.comLine.focus();
	    return false;
   }
   x.totalBusiPlaced.value = x.totalBusiPlacedDis.value;
   x.totalBusiMix.value = x.totalBusiMixDis.value; 
   return true;
}

function  enableInput(){
     document.requestForm.radioValue.value='yes';
     var x=document.requestForm.claim;
     x.disabled = false;
     var y=document.requestForm.totalPaid;
     y.disabled = false;
     document.requestForm.claim.focus(); 
     return true; 
 }
 
 function disableInput(){
     document.requestForm.radioValue.value='no';
     var x=document.requestForm.claim;
	 x.value="";
     x.disabled = true;
     var y=document.requestForm.totalPaid;
     y.value="";
	 y.disabled = true;     
	 document.requestForm.liability.focus();
     return true; 
 }

function findDays(month){
 if(month=='01' || month =='03' || month=='05' || month=='07' || month=='08' || month=='10' || month=='12'){
     return 31;
 }
 if(month == '04' || month == '06' || month == '09' ||  month=='11'){
      return 30;
 }
 if(month == '02' ) {
      return 29;
 }
 else return 0;
}

function creatOptions(days,list){
   for(i=1; i<=days; i++) {	 
	    var text;
	    if(i<10) { text = '0'+i;}
	    else {text = i+''; } 	 
	     list.options[list.options.length]=new Option(text,text);
	 }  
}

function setDays(ele){
var x, days;
if(ele.name == 'estMonth'){
    x = document.requestForm.estDay;
}
if(ele.name == 'effMonth'){
	  x = document.requestForm.effDay;
}
x.options.length=1;	  
days = findDays(ele.value);
if(days==0) { return false; }
creatOptions(days,x);  return true;
}
