﻿// JScript File

function validatepage()
{
	if (document.getElementById("txtName").value == "")
	{
		alert("Please enter Name");
		document.getElementById("txtName").focus();
		return false;
	}
	if(!(document.getElementById("txtPhone").value) == "")
	{
	    return validateUsPhone(document.getElementById("txtPhone"));
	    return false;
	}
	if (document.getElementById("txtEmail").value == "")
	{
		alert("Please enter Email Address");
		document.getElementById("txtEmail").focus();
		return false;
	}
	if(checkmail(document.getElementById("txtEmail").value))
	    return true;
	else
   {
	    alert("Please enter a valid email address");
	    document.getElementById("txtEmail").focus();
	    return false;
	} 
	
	return true;
}

function checkmail(str) 
{
    //var str = document.getElementById("txtEmail").value;
	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");
	   document.getElementById("txtEmail").focus();
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   //alert("Invalid E-mail ID");
	   document.getElementById("txtEmail").focus();
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	    //alert("Invalid E-mail ID");
	    document.getElementById("txtEmail").focus();
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1)
	 {
	    //alert("Invalid E-mail ID");
	    document.getElementById("txtEmail").focus();
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	 {
	    //alert("Invalid E-mail ID");
	    document.getElementById("txtEmail").focus();
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1)
	 {
	    //alert("Invalid E-mail ID");
	    document.getElementById("txtEmail").focus();
	    return false;
	 }
	
	 if (str.indexOf(" ")!=-1)
	 {
	    //alert("Invalid E-mail ID");
	    document.getElementById("txtEmail").focus();
	    return false;
	 }

	 return true;				
}

function validateUsPhone (elem) 
{
    var usPhExp  = /(^\d{3}-\d{3}-\d{4}$)/;
    if(elem.value.match(usPhExp))
    {
        return true;
    }
    else
    {
        alert("Please enter a valid Phone Number(eg.999-999-9999)");
        elem.value='';
        elem.focus();
        return false;
    }
}





