// Search Website
function search()
{
    var s = new String (document.getElementById("searchBox").value);
    s = s.replace(" ",",")
    tempURL = "Products.aspx?Search=" + s;
    window.location=tempURL;
}

// Required Fields
function validate_required(field,alerttxt)
{
      if (field==null||field=="")
      {
        alert(alerttxt);return false;
      }
      else
      {
         return true;
      }
    
}

// Proper Email Address
function validate_email(field,alerttxt)
{
    apos=field.indexOf("@");
    dotpos=field.lastIndexOf(".");
    if (apos<1||dotpos-apos<2) 
      {alert(alerttxt);return false;}
    else {return true;}
    
}

// Validate Contact Form
function validate_form()
{
    nameField = document.getElementById("contact_name");
    emailField = document.getElementById("contact_email");
    
    if (validate_required(nameField.value,"Please enter in your name.")==false)
    {
        nameField.focus();
        return false;
    }
    else if (validate_email(emailField.value,"Please make sure your email address is correct.")==false)
    {
        emailField.focus();
        return false;
    }
    else
    {
        alert("Thank You! Your message has been sent.\nIf you have submitted a question, we will respond to you soon.");
        return true;
    }
}

// Validate Zipcode
function weblocator()
{
  var checkOK = "0123456789";
  tempZip = document.getElementById("zipcode");
  var checkStr = tempZip.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid || tempZip.value == "" || tempZip.value.length < 5)
  {
    alert("Please enter a 5-digit zip code.");
    tempZip.focus();
    
  }
  
  resultsIFrame = document.getElementById("dealerResults");
  tempURL = "http://www.zipwise.com/weblocator/results.php?a=1544&zip=" + tempZip.value;
  resultsIFrame.src= tempURL;
  resultsIFrame.style.display = "";
  document.getElementById("zipResultsPlaceholder").style.display = "none";
}


function submitHandler()
{
    //alert("Inside Submit Handler");
    if(document.getElementById("searchBox").value != "")
    {
        search();
        return false;
    }
    else if(document.getElementById("current_page").value == "dealers")
    {
        weblocator( );
        return false;
    }
    else if(document.getElementById("current_page").value == "contact")
    {
        return validate_form();
    }
}
