//method for validating if a string is an e-mail address
//depends:  utilities.js

var defaultValueN = "First Name";
var defaultValue = "your@e-mail.com";

function isBlank(testValue) {
	if(testValue == "") {
		return true;
	} else {
		var allSpaces = new Boolean(true);
		//look through and make sure not all spaces
		findChars:
		for(i = 0;  i < testValue.length; i++) {
			if(testValue.charAt(i) != " ") {
				allSpaces = new Boolean(false);
				break findChars;
			}
		}
		
		//nothing but spaces
		if(allSpaces == true) {
			return true;
		}
	}
	
	return false;
}


//utility to find a character in a testValue
//sends back an array
function findChar(testValue,locateChar) {
	var returnArray = new Array();
	var currentIndex = 0;
	if (locateChar != "") {
		if(testValue != "") {
			for(i = 0; i < testValue.length; i++) {
				if(testValue.charAt(i) == locateChar) {
					returnArray[currentIndex++] = i;
				}
			}
		} else {
			alert("findChar() called with null testValue");
		}
	} else {
		alert("findChar() called with invalid locateChar");
	}
	
	return returnArray;
}

function isValidName(testValue) {
	if(isBlank(testValue) || testValue == defaultValueN) {
		return false;
	} else {
	return true;
	}
	
}

function isValidEmail(testValue) {
	//not blank
	if(isBlank(testValue) || testValue == defaultValue) {
		return false;
	} else {
		//contains an "@" and only one at at least one character from first char and three char from last char
		if(testValue.indexOf('@') >= 1 && testValue.lastIndexOf('@') == testValue.indexOf('@') && testValue.indexOf('@') <= (testValue.length-3)) {
		
			//find the dots
			var dotIndex = findChar(testValue,'.');
			var atIndex = testValue.indexOf('@');

			//no dots?
			if(dotIndex.length == 0) {
				return false;
			} else {
				//make sure the dots are in valid places
				//not on either side of the @ and not the last character
				if(testValue.charAt(atIndex-1) == '.' || testValue.charAt(atIndex+1) == '.' || testValue.charAt(testValue.length-1) == '.') {
					return false;
				}
				
				//at least one dot after the "@"
				if(testValue.substring(atIndex).indexOf('.') < 0) {
					return false;
				}
				
				//not next to each other
				if(dotIndex.length > 1) {
					for(i = 0; i < dotIndex.length; i++) {
						if(dotIndex[i] == dotIndex[i+1] -1) {
							return false;
						}
					}
				}

				//last dot too close to the end
				if(dotIndex[dotIndex.length-1] >= testValue.length -2) {
					return false;
				}

			}

			//at this point, you have a valid e-mail address

		} else {
			//no valid '@'
			return false;
		}
	}

	//if you got here, you're valid
	return true;

}

function validateNewsletter() {
	var name = document.getElementById("name").value;
	var emailAddress = document.getElementById("eAddress").value;
	var errorMessage = "Before continuing, please enter a name and valid e-mail address.";
	if(!isValidEmail(emailAddress) || !isValidName(name)) {
		document.getElementById("newsletterErrorContainer").innerHTML = errorMessage;
		document.getElementById("newsletterErrorContainer").style.display = "block";
		flashBorder("newsletterErrorContainer","","","red","white",5);
		return false;
	}
	
	return true;
}


function flashBorder(elementId, borderWidth, borderStyle, color1, color2, times, interval) {

	var defaultTimes = new Number(4);

	//use a real element
	if(!elementId) {
		alert("flashBorder() called with null elementId");
	} else if(document.getElementById(elementId) == false) {
		alert("flashBorder() called with invalid element");
	}
	
	//set borderWidth, borderStyle to passed in, then default to set value, then default to "1px"
	borderWidth = borderWidth ? borderWidth : (document.getElementById(elementId).style.borderWidth != "") ? document.getElementById(elementId).style.borderWidth : "1px";
	borderStyle = borderStyle ? borderStyle : (document.getElementById(elementId).style.borderStyle) ? document.getElementById(elementId).style.borderStyle : "solid";

	document.getElementById(elementId).style.borderWidth = borderWidth;
	document.getElementById(elementId).style.borderStyle = borderStyle;

	//borderColor
	color1 = color1 ? color1 : (document.getElementById(elementId).style.borderColor != "") ? document.getElementById(elementId).style.borderColor : "red";
	color2 = color2 ? color2 : "white";

	document.getElementById(elementId).style.borderColor = color1;

	//flash interval
	interval = interval ? interval : "200";

	//flash decrementer
	times = !isNaN(times) ? (times < 0 ? defaultTimes : times) : defaultTimes;


	//flash if flashTimes remain
	if(times > 0) {
		setTimeout("flashBorder('" + elementId +"','" + borderWidth + "','" + borderStyle + "','" + color2 + "','" + color1 +"'," + (times-1) + "," + interval + ");",interval);
	}
}

//clear out the defualt value
function checkDefaultName(element) {
	if (element.value == "First Name") {
		element.value = "";
	} else if (element.value == "") {
		element.value = "First Name";
	}
}
function checkDefaultEmail(element) {
	if (element.value == "your@e-mail.com") {
		element.value = "";
	} else if (element.value == "") {
		element.value = "your@e-mail.com";
	}
}