function Sanitize(fldStr)
{
  var outstr=fldStr;
  var reg=new RegExp('!',"g");
  outstr=outstr.replace(reg, '');
  var reg=new RegExp('<',"g");
  outstr=outstr.replace(reg, '');
  var reg=new RegExp('>',"g");
  outstr=outstr.replace(reg, '');
  var reg=new RegExp(String.fromCharCode(39),"g"); //single quote
  outstr=outstr.replace(reg, '');
  var reg=new RegExp(String.fromCharCode(34),"g"); //double quote
  outstr=outstr.replace(reg, '');  var reg=new RegExp('#',"g");
  outstr=outstr.replace(reg, '');
  var reg=new RegExp('$',"g");
  outstr=outstr.replace(reg, '');
  var reg=new RegExp('%',"g");
  outstr=outstr.replace(reg, ' Percent');
  var reg=new RegExp('&',"g");
  outstr=outstr.replace(reg, 'and');
  var reg=new RegExp('\\?',"g");
  outstr=outstr.replace(reg, '(Q)');
  return outstr;
}

function trim(sString)
{
 while (sString.substring(0,1) == ' ') sString = sString.substring(1, sString.length);
 while (sString.substring(sString.length-1, sString.length) == ' ') sString = sString.substring(0,sString.length-1);
 return sString;
}

function browserCompatible() 
 {
  // checkW3C DOM, then MSIE 4, then NN 4.
 
  if(document.getElementById) return true;
  if (document.all) return true;  
  if (document.layers) return true;
  return false;
 }
 
// function switchDiv()
//  this function takes the id of a div
//  and calls the other functions required
//  to show that div
//
function switchDiv(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideAll();
    changeObjectVisibility(div_id,"block");
  }
}

// function getStyleObject(string) -> returns style object
//  given a string containing the id of an object
//  the function returns the stylesheet of that object
//  or false if it can't find a stylesheet.  Handles
//  cross-browser compatibility issues.
//
function getStyleObject(objectId) 
{
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) 
{
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) 
	 {
	  styleObject.display = newVisibility;
	  return true;
     } 
	else
	 { 
      return false;
     }
}

function hideAll()
{
   changeObjectVisibility("house","none");
   changeObjectVisibility("coop","none");
   changeObjectVisibility("condo","none");
   changeObjectVisibility("comm","none");
   changeObjectVisibility("brown","none");      
}

function showPtype()  // if ptype is defined, show block
{
 for (pt=0;pt<5;pt++) if (document.efrm.ptype[pt].checked) break;
 if (pt<5)
  {
   switch(pt)
    {
     case 0:
      changeObjectVisibility("house","block");
	  break;
     case 1:
      changeObjectVisibility("coop","block");
	  break;
     case 2:
      changeObjectVisibility("condo","block");
	  break;
     case 3:
      changeObjectVisibility("comm","block");
	  break;
     case 4:
      changeObjectVisibility("brown","block"); 
	  break;
    }
  }
}

function checkForQuote()
{
 if (document.efrm.quote_yn.checked==true)
  {
   changeObjectVisibility("quoterequest","block");
   changeObjectVisibility("checkhere","none");   
   showPtype();
  }
 else
  {
   changeObjectVisibility("quoterequest","none");
   hideAll();
  }
}


function echeck(str) 
 {
	var at="@";
	var dot=".";
	var invchars=" ()/;:,<>"+'"';
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (lat==-1 || lat==0 || lat==lstr-1) //@ not there, @ at start, @ at end
	  {
	   alert("Invalid E-mail Format");
	   return false;
	  }
	if (ldot==-1) // no .
	  {
	    alert("Invalid E-mail Format");
	    return false;
	  }
	if (str.indexOf(at,(lat+1))!=-1) // @ after @
	  {
	    alert("Invalid E-mail Format");
	    return false;
	  }
 	if (str.charAt(0)==dot || str.charAt(lat-1)==dot || str.charAt(lat+1)==dot || str.charAt(lstr-1)==dot) 
	  {    // . at start, end, or right before or after @
	    alert("Invalid E-mail Format");
	    return false;
	  }
     for(i=0;i<invchars.length;i++)	
	   if (str.indexOf(invchars.charAt(i))!=-1) // invalid character in email address
	     {
	      alert("Invalid Character: "+invchars.charAt(i)+" not allowed");
	      return false;
	     }
	   
	 for(i=0;i<lstr;i++) 
	   if (str.charCodeAt(i)>127) // check each char for extended value 
	   	 {
	      alert("Invalid Character: "+str.charAt(i)+" not allowed");
	      return false;
	     }		 
		return true;	// email passes muster		
 }

function ValidateForm()
 {
  document.efrm.email.value=Sanitize(document.efrm.email.value);
  document.efrm.loc.value=Sanitize(document.efrm.loc.value);
  document.efrm.howold0.value=Sanitize(document.efrm.howold0.value);
  document.efrm.howold1.value=Sanitize(document.efrm.howold1.value);
  document.efrm.howold2.value=Sanitize(document.efrm.howold2.value);
  document.efrm.howold3.value=Sanitize(document.efrm.howold3.value);
  document.efrm.usage.value=Sanitize(document.efrm.usage.value);
  document.efrm.howold4.value=Sanitize(document.efrm.howold4.value);
  document.efrm.address.value=Sanitize(document.efrm.address.value);
  document.efrm.fname.value=Sanitize(document.efrm.fname.value);
  document.efrm.comments.value=Sanitize(document.efrm.comments.value);

  var emailID=document.efrm.email;
  var Vptype=document.efrm.ptype;  
  var Vcounty=document.efrm.county;
  var Vloc=document.efrm.loc;
 
  // house block
  var Vbeds0=document.efrm.beds0;
  var Vbrs0=document.efrm.brs0;
  var Vkits0=document.efrm.kits0;
  var Vage0=document.efrm.howold0;  
  var Vsts0=document.efrm.sts0;
  var Vsf0=document.efrm.sf0;
  var Vcs0=document.efrm.cs0;
  var Vbsmt0=document.efrm.bsmt0;

// co-op block  
  var Vbrs1=document.efrm.brs1;
  var Vage1=document.efrm.howold1;  
  var Vsf1=document.efrm.sf1;
 //condo block
  
  var Vbeds2=document.efrm.beds2;
  var Vbrs2=document.efrm.brs2;
  var Vkits2=document.efrm.kits2;
  var Vage2=document.efrm.howold2;  
  var Vsf2=document.efrm.sf2;
 //comm block
     
  var Vage3=document.efrm.howold3;  
  var Vsts3=document.efrm.sts3;
  var Vsf3=document.efrm.sf3;
  var Vusage=document.efrm.usage;
  var Vcs3=document.efrm.cs3;
  var Vbsmt3=document.efrm.bsmt3;   
  
 // brownstone block
  var Vbrs4=document.efrm.brs4;
  var Vkits4=document.efrm.kits4;
  var Vage4=document.efrm.howold4;  
  var Vsts4=document.efrm.sts4;
  var Vcs4=document.efrm.cs4;
  var Vbsmt4=document.efrm.bsmt4;   
  var Vaddress=document.efrm.address
	 
  var Vfname=document.efrm.fname; 
  var county = new Array( "Brooklyn", "Bronx", "Fire Island", "Nassau", "Manhattan", "Queens", "Staten Island", "Suffolk", "Westchester");

if (document.efrm.quote_yn.checked==true)
 {
  for (pt=0;pt<5;pt++) if (Vptype[pt].checked) break; // check property type
  for (ct=0;ct<9;ct++) if (Vcounty[ct].checked) break; // check county

  if (Vfname.value=="") 
   {
    alert("Please your first name to keep with the quote");
	Vfname.focus();
	return false;
   }
  if (ct==9) 
   {
    alert("Please choose a borough or county");
	Vcounty[0].focus();
	return false;
   }
  if (Vloc.value=="") 
   {
    if (county[ct]=="Manhattan" || county[ct]=="Bronx" || county[ct]=="Brooklyn" || county[ct]=="Staten Island" || county[ct]=="Queens") 
	  alert("Please specify the neighborhood within "+county[ct])
    else 
      alert("Please specify the town within "+county[ct]);
	Vloc.focus();
	return false;
   }
  if (pt==5)
   {
    alert("Please choose a property type");
	Vptype[0].focus();
	return false;
   }
  switch(pt) // validate form based on ptype
   {
    case 0: // house
     if (Vbeds0.value=="" || isNaN(Vbeds0.value)) 
      {
       alert("Please specify total number of bedrooms");
	   Vbeds0.focus();
	   return false;
      }
     if (Vbrs0.value=="" || isNaN(Vbrs0.value)) 
      {
       alert("Please specify total number of bathrooms");
	   Vbrs0.focus();
	   return false;
      }
     if (Vkits0.value=="" || isNaN(Vkits0.value)) 
      {
       alert("Please specify total number of kitchens");
       Vkits0.focus();
	   return false;
      }
     if (Vsf0.value=="" || isNaN(Vsf0.value)) 
      {
       alert("Please enter the actual or estimated interior square footage of the space");
	   Vsf0.focus();
	   return false;
      }
     if (Vsts0.value=="" || isNaN(Vsts0.value)) 
      {
       alert("Please enter the number of stories from the ground floor up");
	   Vsts0.focus();
	   return false;
      }
     if (Vage0.value=="") 
      {
       alert("Please enter the actual or estimated age of the building or '?' if you're not sure");
	   Vage0.focus();
	   return false;
      } 
     for (i=0;i<3;i++) if (Vbsmt0[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a basement");
	   Vbsmt0[0].focus();
	   return false;
      }
     for (i=0;i<3;i++) if (Vcs0[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a crawl space");
	   Vcs0[0].focus();
	   return false;
      }
	 break;
	 //---------------------------------------------------------------------------------------------------------
    case 1: // co-op
     if (Vsf1.value=="" || isNaN(Vsf1.value)) 
      {
       alert("Please enter the actual or estimated interior square footage of the space");
	   Vsf1.focus();
	   return false;
      }    
     if (Vbrs1.value=="" || isNaN(Vbrs1.value)) 
      {
       alert("Please specify total number of bathrooms");
	   Vbrs1.focus();
	   return false;
      }
     if (Vage1.value=="") 
      {
       alert("Please enter the actual or estimated age of the building or '?' if you're not sure");
	   Vage1.focus();
	   return false;
      } 
	 break;
	 //---------------------------------------------------------------------------------------------------------
    case 2: // condo
     if (Vsf2.value=="" || isNaN(Vsf2.value)) 
      {
       alert("Please enter the actual or estimated interior square footage of the space");
	   Vsf2.focus();
	   return false;
      }    
     if (Vbeds2.value=="" || isNaN(Vbeds2.value)) 
      {
       alert("Please specify total number of bedrooms");
	   Vbeds2.focus();
	   return false;
      }
     if (Vbrs2.value=="" || isNaN(Vbrs2.value)) 
      {
       alert("Please specify total number of bathrooms");
	   Vbrs2.focus();
	   return false;
      }
     if (Vkits2.value=="" || isNaN(Vkits2.value)) 
      {
       alert("Please specify total number of kitchens");
       Vkits2.focus();
	   return false;
      }
     if (Vage2.value=="") 
      {
       alert("Please enter the actual or estimated age of the building or '?' if you're not sure");
	   Vage2.focus();
	   return false;
      } 
	 break;
	 //---------------------------------------------------------------------------------------------------------
    case 3: // commercial
     if (Vsf3.value=="" || isNaN(Vsf3.value)) 
      {
       alert("Please enter the actual or estimated interior square footage of the space");
	   Vsf3.focus();
	   return false;
      }
	 if (Vusage.value=="") 
      {
       alert("Please describe what the property is currently being used for");
	   Vusage.focus();
	   return false;
      }  
     if (Vsts3.value=="" || isNaN(Vsts3.value)) 
      {
       alert("Please enter the number of stories from the ground floor up");
	   Vsts3.focus();
	   return false;
      }
     if (Vage3.value=="") 
      {
       alert("Please enter the actual or estimated age of the building or '?' if you're not sure");
	   Vage3.focus();
	   return false;
      } 
     for (i=0;i<3;i++) if (Vbsmt3[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a basement");
	   Vbsmt3[0].focus();
	   return false;
      }
     for (i=0;i<3;i++) if (Vcs3[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a crawl space");
	   Vcs3[0].focus();
	   return false;
      }
	 break; 
	 //---------------------------------------------------------------------------------------------------------	  
    case 4: // brownstone
     if (Vbrs4.value=="" || isNaN(Vbrs4.value)) 
      {
       alert("Please specify total number of bathrooms");
	   Vbrs4.focus();
	   return false;
      }
     if (Vkits4.value=="" || isNaN(Vkits4.value)) 
      {
       alert("Please specify total number of kitchens");
       Vkits4.focus();
	   return false;
      }
     if (Vsts4.value=="" || isNaN(Vsts4.value)) 
      {
       alert("Please enter the number of stories from the ground floor up");
	   Vsts4.focus();
	   return false;
      }  
     if (Vage4.value=="") 
      {
       alert("Please enter the actual or estimated age of the building or '?' if you're not sure");
	   Vage4.focus();
	   return false;
      } 
     for (i=0;i<3;i++) if (Vbsmt4[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a basement");
	   Vbsmt4[0].focus();
	   return false;
      }
     for (i=0;i<3;i++) if (Vcs4[i].checked) break; 
     if (i==3)
      {
       alert("Please specify if there's a crawl space");
	   Vcs4[0].focus();
	   return false;
      }
	 if (Vaddress.value=="") 
      {
       alert("Please enter the street address or at least the street name and nearest corner");
	   Vaddress.focus();
	   return false;
      }  
	 break;
    }   
}
  if ((emailID.value==null)||(emailID.value==""))
   {
	alert("Please Enter your Email Address");
	emailID.focus();
	return false;
   }
  if (trim(document.efrm.comments.value)=='' && document.efrm.quote_yn.checked==false)
   {
	alert("Please explain the purpose of your email");
	document.efrm.comments.focus();
	return false;
   }
   
  if (echeck(emailID.value)==false)
   {
 	alert("That does not look like a valid email address.  Please try again.");  
	emailID.value="";
	emailID.focus();
	return false;
   }
  return true;
 }

