// JavaScript Document
String.prototype.trim = strtrim;

function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function ProcessForm() {
var errorMsg = "";

	if (document.ContactForm.firstname.value.trim() == "")
		{
			errorMsg = errorMsg+"First Name, ";
		}
	if (document.ContactForm.lastname.value.trim() == "")
		{
			errorMsg = errorMsg+"Last Name, ";
		}

	if (document.ContactForm.city.value.trim() == "")
		{
			errorMsg = errorMsg+"City, ";
		}	
	if (document.ContactForm.country.value.trim() == "")
		{
			errorMsg = errorMsg+"Country, ";
		}	

	if (document.ContactForm.question.value.trim() == "")
		{
			errorMsg = errorMsg+"Question, ";
		}	
		hasEmail1 = true;
		hasEmail2 = true;
	if (isblank(document.ContactForm.email1.value.trim()))
		{
			errorMsg = errorMsg+"E-mail address, ";
			hasEmail1 = false;
			
		}
	if (isblank(document.ContactForm.email2.value.trim()))
		{
			errorMsg = errorMsg+"E-mail address Confirmation, ";
			hasEmail2 = false;
		}
	if (hasEmail1 && hasEmail2) {   		
		if (document.ContactForm.email1.value != document.ContactForm.email2.value)
			{
				errorMsg = errorMsg+"<br>You typed two different e-mail addresses.<br>Please make sure you have typed the same e-mail address in both spaces below. Then, submit the form again.<br>";
			}
		else if (isEmail(document.ContactForm.email1.value) != true && document.ContactForm.email1.value.length > 0)
			{
				errorMsg = errorMsg+"<br>The format of the e-mail address you supplied is not correct. You may be missing an &quot;@&quot; or a &quot;.&quot; or you may have accidently included a space. Please provide your full e-mail address and be sure there are no spaces, e.g.teacher@aol.com.<br><br>Please fix the e-mail address below. Then, submit the form again.";
			}
			
	}
	if( errorMsg.trim()!="")
	{		
		writeError("Some required information is missing:<br>"+errorMsg+"<br><br>Please make sure you have provided all required information below. Then, submit the form again.", "ErrorMsg");
		errorMsg="";
		document.location="#error";
		
	}
	else
	{
		document.ContactForm.action = "processContact.asp";
		document.ContactForm.submit();
	}
}


function writeError(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<div class="' + id + '" id="' + id + '">' + text + '</div>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}
function isblank(svalue)
{
	svalue1 = new String(svalue);
	for(var i = 0; i < svalue1.length; i++) 
	{
		var c = svalue1.charAt(i);
		if((c != ' ') && (c != '\n') && (c!='\t')) return false;
	} 
	return true;
}
function isEmail (s)
{   if (isEmpty(s)) return false;
   
    // is s whitespace or space?
    if (isWhitespace(s) || isSpace(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;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace(s)

{   var i;
	var whitespace = " \t\n\r";

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
function isSpace(s)

{   var i;

    // Search through string's characters one by one
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't a space.
        var c = s.charAt(i);

        if (c == ' ') return true;
    }

    // All characters are spaces.
    return false;
}
