// JavaScript Document

/*====================form validation===============================*/

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
function validEmail(email) {
	invalidChars = " /:,;"
	
	if (email == "") {// cannot be empty
				return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
	badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
		if (atPos == -1) {
			return false
		}
		if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
			return false
		}
	periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {			// and at least one "." after the "@"
			return false
		}
		if (periodPos+3 > email.length)	{	// must be at least 2 characters after the "."
			return false
		}
	return true
}
function validateMe() {
	var subject = document.frmEmail.subject;
	var comments = document.frmEmail.comments;
	var email = document.frmEmail.email;
	var email2 = document.frmEmail.email2;
	var name = document.frmEmail.name;
	
	if (subject.value == "") {
			alert("Please enter a Subject");
			subject.focus();
			return false;
	}
	
	if (comments.value == "") {
		alert("Please type your message.");
		comments.focus();
		return false;
	}
	if (!validEmail(email.value)) {
	alert("Please enter a valid e-mail address")
	email.focus();
	email.select();
	return false;
	}
	 if (email.value != email2.value){
		 alert("Your email addresses don't match \n Please re-enter your valid email address.");
		 email2.focus();
		 email2.select();
		 return false;
	 }	
	 if (name.value == "") {
		 alert("Please enter your name.");
		 name.focus();
		 return false;
	 }
	return true;
}