function isValidEmail(email, required)
{
    if (required==undefined)
	{   // if not specified, assume it's required
        required=true;
    }
    if (email==null)
	{
        if (required)
		{
			//alert("Invalid E-mail ID");
            return false;
        }
        return true;
    }
    if (email.length==0) 
	{  
        if (required) 
		{
			//alert("Invalid E-mail ID");
            return false;
        }
        return true;
    }
    if (! allValidChars(email))
	{  // check to make sure all characters are valid
     //alert("Invalid E-mail ID");
	  return false;
    }
    if (email.indexOf("@") < 1) 
	{ //  must contain @, and it must not be the first character
     //alert("Invalid E-mail ID");
	   return false;
    } 
	else if (email.lastIndexOf(".") <= email.indexOf("@"))
	{  // last dot must be after the @
      //alert("Invalid E-mail ID");
		return false;
    } 
	else if (email.indexOf("@") == email.length)
	{  // @ must not be the last character
       //alert("Invalid E-mail ID");
		return false;
    }
	else if (email.indexOf("..") >=0) 
	{ // two periods in a row is not valid
	//alert("Invalid E-mail ID");
	return false;
    } 
	else if (email.indexOf(".") == email.length)
	{  // . must not be the last character
	//alert("Invalid E-mail ID");
	return false;
    }
    return true;
}

function allValidChars(email)
{
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++)
  {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function validate()
{
    	//alert("hii");
	var status = true;
    var regName = "^[A-Z]+[a-zA-Z]*$";
    var cnameText = document.getElementById("txt_cname").value.replace(/[\(\)\.\-\ ]/g, '');
    var nameText = document.getElementById("txt_name").value.replace(/[\(\)\.\-\ ]/g, '');
//	var streetnoText = document.getElementById("txt_street_no").value;
	var email = document.getElementById("txt_email").value;
//	var stateText = document.getElementById("txt_state").value;
//	var postalText = document.getElementById("txt_jp").value;
//    var countryText = document.getElementById("txt_country").value;
	var phnoText = document.getElementById("txt_phno").value.replace(/[\(\)\.\-\ ]/g, '');
	var faxText = document.getElementById("txt_fax").value.replace(/[\(\)\.\-\ ]/g, '');
	var stripped = document.getElementById("txt_phno").value.replace(/[\(\)\.\-\ ]/g, '');
		function isEmpty( str )
	{
             var strRE = /^[\s ]*$/gi;
            return strRE.test( str );
        }
	
	
    if(cnameText == "")
    {
        status = false;
        alert("please enter your company name");
    }
	else if(nameText == "")
    {
        status = false;
        alert("please enter your name");
    }
	

	else if(!isValidEmail(email) )
	{
		status = false;
	alert("Invalid Email Id.");
	document.form1.txt_email.focus();
	
	}
	
	
	else if(phnoText == "")
    {
        status = false;
        alert("please enter your phone number ");
    }

	else if (isEmpty(phnoText))
	{
	status = false;
        alert("Please enter a  phone number ");
        document.getElementById("txt_phno").focus();
	}
	else if (isNaN(parseInt(stripped))) 
	{
	status = false;
        alert("The phone number contains illegal characters.\n");
        document.getElementById("txt_phno").focus();
        }
	else if (!(stripped.length == 10))
	{
	status = false;
        alert("The phone number is the wrong length. Make sure you included an area code.\n");
        document.getElementById("txt_phno").focus();
        }
	
	
	return status;
}


