/*
**************************************************************************
To check if the string is a valid email
**************************************************************************
*/

function isEmail (s)
{
    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

/*
**************************************************************************
To check if the string has white space
**************************************************************************
*/

function isWhitespace (s)

{
	var whitespace = " \t\n\r";
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isnt whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/*
********************************************************************
Match spaces at beginning and end of text and replace
with null strings
********************************************************************
*/
function strtrim(str) {
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}



/*
**************************************************************************
To Check if the string is empty
**************************************************************************
*/

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function checkit() {
	var errorStr = "";
	//Country = document.getElementById("Country").value;
	firstName = document.getElementById("fname").value; 
	homePhone = document.getElementById("hPhone").value;
	emailID = document.getElementById("emailObj").value;
			
	if (strtrim(firstName) == "") {
		//errorStr += "First Name cannot be empty\n";
		alert ("Name cannot be empty")	
		document.getElementById("fname").focus()		
		return false;
	}
	if (strtrim(homePhone) == "") {
		//errorStr += "Enter a Phone Number\n";
		alert ("Phone Number cannot be empty")
		document.getElementById("hPhone").focus()
		return false;
	}
	if (strtrim(emailID) == "") {
		//errorStr += "Email Address cannot be empty\n";
		alert ("Email cannot be empty")
		document.getElementById("emailObj").focus()
		return false;
		//alert("Email Address cannot be empty");
		//return false;
	}
	if (strtrim(emailID) != "") {
		if( !isEmail(emailID)) {
			errorStr += "Enter Email Address of the form example@domain.com\n";
		}
	}
	
	if (errorStr != "") {
		alert(errorStr);
		return false;
	}
}
