﻿// JScript File

// Email form validation functions
function IsEmailFormValid()
{
    // clear previous errors
    ClearEmailErrorMessages();

    var yourName = document.getElementById('ctl00_EmailFriendControl_txtYourName').value;
    var yourEmail = document.getElementById('ctl00_EmailFriendControl_txtYourEmail').value;
    var friendName = document.getElementById('ctl00_EmailFriendControl_txtFriendsName').value;
    var friendEmail = document.getElementById('ctl00_EmailFriendControl_txtFriendsEmail').value;
    
    var formIsValid = true;
    
    if (yourName.length <= 0)
    {
        document.getElementById('txtYourNameMsg').style.display = 'block';
        formIsValid = false;
    }
    
    if (yourEmail.length <= 0)
    {
        document.getElementById('txtYourEmailMsg').style.display = 'block';
        formIsValid = false;
    } 
    else if (IsEmailValid(yourEmail) == false)
    {
        document.getElementById('txtYourEmailMsgInvalid').style.display = 'block';
        formIsValid = false;
    }
    
    if (friendName.length <= 0)
    {
        document.getElementById('txtFriendsNameMsg').style.display = 'block';
        formIsValid = false;
    }            
    
    if (friendEmail.length <= 0)
    {
        document.getElementById('txtFriendsEmailMsg').style.display = 'block';
        formIsValid = false;
    }
    else if (IsEmailValid(friendEmail) == false)
    {
        document.getElementById('txtFriendsEmailMsgInvalid').style.display = 'block';
        formIsValid = false;        
    }             

    return formIsValid;
}

function ClearEmailErrorMessages()
{
    document.getElementById('txtYourNameMsg').style.display = 'none';
    document.getElementById('txtYourEmailMsg').style.display = 'none';
    document.getElementById('txtYourEmailMsgInvalid').style.display = 'none';
    document.getElementById('txtFriendsNameMsg').style.display = 'none';
    document.getElementById('txtFriendsEmailMsg').style.display = 'none';
    document.getElementById('txtFriendsEmailMsgInvalid').style.display = 'none';
}

function IsEmailValid(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
    if (str.indexOf(at,(lat+1))!=-1){
        return false
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
    }
    if (str.indexOf(dot,(lat+2))==-1){
        return false
    }
    if (str.indexOf(" ")!=-1){
        return false
    }

	return true					
}