// JavaScript Document

window.onload = initForms;

function initForms() {
	for (var i=0; i< document.forms.length; i++) {
		if(document.forms[i].id == "contact")
		   document.forms[i].onsubmit = function() {return validForm();}
		else if(document.forms[i].id == "newsletter")
			document.forms[i].onsubmit = function(){return validShortForm();}
	}
	
	rolloverInit();
}

function validateName(name) {
	if(name == "") {
		return false;
	}
	else {
	return true;
	}
}

	// Email Validation Javascript
	// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd
	
	// You have permission to copy and use this javascript provided that
	// the content of the script is not changed in any way.
	
function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   if (db) alert('email address is mandatory');
	   return false;
	}
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
	return true;
}


function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");
	var message = "Please fix the following and resubmit:\n";

	for (var i=0; i <allTags.length; i++) {
		if (allTags[i].tagName == "INPUT") {
			if(allTags[i].id == "first_name") {
				if(!validateName(allTags[i].value)) {
					allGood = false;
					message += "\n First Name Cannot Be Blank.";
				}
			}
			if(allTags[i].id == "last_name") {
				if(!validateName(allTags[i].value)) {
					allGood = false;
					message += "\n Last Name Cannot Be Blank.";
				}
			}
			if(allTags[i].id == "email") {
				if(!validateEmail(allTags[i].value, true, false)) {
					allGood = false;
					message += "\n Please Enter Valid Email Address.";
				}
			}			
		}
	}
	if (allGood == true) {
		message = "Thank you for your submission.";
	}
	alert(message);	
}

function validShortForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");
	var message = "Please fix the following and resubmit:\n";
	for (var i=0; i <allTags.length; i++) {
		if (allTags[i].tagName == "INPUT") {
			if(allTags[i].id == "email2") {
				if(!validateEmail(allTags[i].value, true, false)) {
					allGood = false;
					message += "\n Please Enter Valid Email Address.";
				}
			}			
		}
	}			
	if (allGood == true) {
		message = "Thank you for your submission.";
	}
	alert(message);	
}

function rolloverInit() {
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.tagName == "A" && document.images[i].parentNode.parentNode.id == "nav") {
			setupRollover(document.images[i]);
		}
	}
	
}

function setupRollover(thisImage) {
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = rollOut;

	thisImage.overImage = new Image();
	thisImage.overImage.src = "images/" + thisImage.id + "_over.jpg";
	thisImage.onmouseover = rollOver;	
}

function rollOver() {
	this.src = this.overImage.src;
}

function rollOut() {
	this.src = this.outImage.src;
}

