//Checks to see whether an input value contains "@" and "."
function isAValidEmail(inputValue) {
	//var foundAt = false
	//var foundDot = false
//we need to check the email address contains @mnsu.edu 
if(inputValue.match("@*.edu"))
{
	return true;
}
return false;
/*for (var i=0; i<=inputValue.length; i++) {
	if(inputValue.charAt(i) == "@") {
		foundAt = true
		}
	else if(inputValue.charAt(i) == ".") {
		foundDot = true
		}
	}
if(foundAt & foundDot){
	return true
	}
	else {
	return false
	}*/
}
function maxChar(inputValue) {
    /*Validate textarea length before submitting the form*/
    var maxChar = 225;
    if (inputValue.length > maxChar) {
        diff=inputValue.length - maxChar;
        if (diff>1)
            diff = diff + " characters";
        else
            diff = diff + " character";

        alert("The area field is limited to " + maxChar + " characters\n" + "Please reduce the text by " + diff);
        return (false);
    }
	return true;
}

//Checks for the existance of characters.
function exists(inputValue){

	var aCharExists = false
	for(var i=0; i<=inputValue.length; i++){
		if(inputValue.charAt(i) != " " && inputValue.charAt(i)!="") {
			aCharExists = true
			break
		}
	}
	return aCharExists
}

//Performs all dependent field validation
function validateForm(){
	var rc=true
	var errorstring = ""
	var lasterrorstring = "" //handles the message formatting
	var comma = false
	if(!document.Postcard.senderFirstName.value)
	{
		errorstring = "Please type in your First Name"
		comma = true
	    rc = false
	}
	if(!document.Postcard.senderLastName.value)
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" your Last Name" 
		}
		else
		{
				errorstring = "Please type in your Last Name"
		}
		comma = true
	    rc = false
	}
	if(!isAValidEmail(document.Postcard.senderEmail.value))
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" your mnsu.edu Email Address" 
		}
		else
		{
				errorstring = "Please type your mnsu.edu Email Address"
		}
		comma = true
	    rc = false
	}
	if(!document.Postcard.recipientFirstName.value)
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" the recipient's First Name" 
		}
		else
		{
				errorstring = "Please type in the recipient's First Name"
		}
		comma = true
	    rc = false
	}
	if(!document.Postcard.recipientLastName.value)
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" the recipient's Last Name" 
		}
		else
		{
				errorstring = "Please type in the recipient's Last Name"
		}
		comma = true
	    rc = false
	}
	if(!isAValidEmail(document.Postcard.recipientEmail.value))
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" a valid mnsu.edu email address for the primary recipient" 
		}
		else
		{
				errorstring = "Please type a valid mnsu.edu email address for the primary recipient"
		}
		comma = true
	    rc = false
	}
	//only require name if they CC email address
	if(exists(document.Postcard.ccEmail.value))
	{
	if(!exists(document.Postcard.ccFirstName.value))
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" a First Name for the secondary recipient" 
		}
		else
		{
				errorstring = "Please type a First Name for the secondary recipient"
		}
		comma = true
	    rc = false

		}
	}
	//only require name if they CC email address
	if(exists(document.Postcard.ccEmail.value))
	{
	if(!exists(document.Postcard.ccLastName.value))
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" a Last Name for the secondary recipient" 
		}
		else
		{
				errorstring = "Please type a Last Name for the secondary recipient"
		}
		comma = true
	    rc = false

		}
	}
	//only valididate the CC if they entered a name or put a value in the email address
	if(exists(document.Postcard.ccEmail.value) ||
			  exists(document.Postcard.ccFirstName.value) ||
			  exists(document.Postcard.ccLastName.value))
	{
		if(!isAValidEmail(document.Postcard.ccEmail.value))
		{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				lasterrorstring =" a valid mnsu.edu email address for the secondary recipient" 
		}
		else
		{
				errorstring = "Please type a valid mnsu.edu email address for the secondary recipient"
		}
		comma = true
	    rc = false
		}
	}
	if(!document.Postcard.message.value)
	{
	    if(comma == true)
		{
				if(lasterrorstring != "")
				{
						errorstring += ","
				}
				errorstring += lasterrorstring
				//jazz up the error message a little bit by adding the name of the recipient
				if(document.Postcard.recipientFirstName.value)
				{
				lasterrorstring = " a message for " + document.Postcard.recipientName.value
				}
				else
				{
					lasterrorstring = " a message for the recipient"
				}
		}
		else
		{
				//dont need to add a special case here like above, because if they get here, 
				//everything above has validated
				errorstring = "Please type a message for " + document.Postcard.recipientName.value
		}
		comma = true
	    rc = false
	}
	//validate the text area
	if(!maxChar(document.Postcard.message.value))
	{
		rc = false;
	}
			   
	if(errorstring != "")
	{
		if(lasterrorstring != "")
		{
				errorstring += ", and"
				errorstring += lasterrorstring
		}
		alert(errorstring + ".  Thanks!")
	}
	return rc
	}