﻿function verify() 
{
	var myForm = document.Form1;
	var themessage = "You are required to complete the following fields: ";
	var isError = false;

	var re = /\s/g; //Match any white space including space, tab, form-feed, etc.

	var strCompanyName = myForm.companyname.value.replace(re, "");
	if (strCompanyName.length == 0) 
	{
	themessage = themessage + "\n - Company Name";
	isError = true;
	}

	var strContact = myForm.contact.value.replace(re, "");
	if (strContact.length == 0) 
	{
	themessage = themessage + "\n - Contact";
	isError = true;
	}

	var strCity = myForm.city.value.replace(re, "");
	if (strContact.length == 0) 
	{
	themessage = themessage + "\n - City";
	isError = true;
	}

	var strState = myForm.state.value.replace(re, "");
	if (strState.length == 0) 
	{
	themessage = themessage + "\n - State";
	isError = true;
	}

	var strPhone = myForm.phone.value.replace(re, "");
	if (strPhone.length == 0) 
	{
	themessage = themessage + "\n - Phone #";
	isError = true;
	}

	var strEmail = myForm.email.value.replace(re, "");
	if (strEmail.length == 0) 
	{
	    themessage = themessage + "\n - Email Address";
	    isError = true;
	}
	else if(echeck(strEmail)==false)
	{
	    themessage = themessage + "\n - Invalid Email Address";
	    isError = true;	       		
	}

	//alert if fields are empty and cancel form submit
	if (!isError) 
	{				
		 myForm.submit();
	}
	else 
	{
		alert(themessage);
	}
}

function echeck(str) 
{

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at) == -1)
    {
     //  alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
    {
      // alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
    {
       // alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1)
     {
      // alert("Invalid E-mail ID")
        return false
     }

     if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot)
     {
      //  alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(dot, (lat + 2)) == -1)
     {
      //  alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(" ") != -1)
     {
      //  alert("Invalid E-mail ID")
        return false
     }

     return true					
}			

