/*------ enterprise.js ------*/
/*------ Author: Steven Whyte ------*/

/*------ Modifications ------*/
/*			Updates:
				v. 1.2, 4/29/09 - appended the forms validation code to this file
								- bug fix: if document.forms[0] doesn't exist, skip forms validation
								- bug fix: email & phone validation functions didn't return focus to those fields
				v. 1.1, 4/22/09 - these fuctions are now called when window.onload occurs
*/


/*------ Functions for dropMenus.html ------*/
/* Workaround for IE6's li:hover bug */
/* Attach mouseover & mouseout events */

function startList() {
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById("nav");
		var node;
		for (var i = 0; i < navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " over";
				};
				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				};
			}
		}
	}
}

/*------ Functions for pop-up windows ------*/
function openPopup(anchorObj) {
	var URL = anchorObj.getAttribute("href");
	var win = window.open(URL,"myWin");
	win.focus();
}

function setupLinks() {
	var pageLinks = document.links;
	for (var i = 0; i < pageLinks.length; i++) {
		if (pageLinks[i].className == "popup") {
			pageLinks[i].onclick = function() {
				openPopup(this);
				return false;
			};
		}
	}
}

/*------ Functions for forms validation ------*/
function fieldIsEmpty(formField, message) {
	if (formField.value === "") {
		alert(message);
		formField.focus();
		return true;
	}
	else {
		return false;
	}
}

function emailIsInvalid(email, message) {
	var apos = email.value.indexOf("@");
	var dotpos = email.value.lastIndexOf(".");
	var lastpos = email.value.length - 1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		if (message) {
			alert(message);			
		} 
		email.focus();
		return true;
	}
	else {
		return false;
	}

}

function phoneIsInvalid(phone, max, min, message) {
	var regex = /[\(\)\-]/g;
	var tempstr = phone.value.replace(regex, "");
	var regexp_digit=/^\d{5,11}$/;
	if ((tempstr.length < min) || (tempstr.length > max) || (tempstr.search(regexp_digit) == -1)) {
		if (message !== "") {
			alert(message);
		} 
		phone.focus();
		return true;
	}
	else {
		return false;
	}
}

function checkForm_onsubmit () {
	var myForm = document.forms[0];
	var formE = myForm.elements;
	var i;
	for ( i = 0; i < formE.length; i++) {
		if(formE[i].id == "salesTeamSize") {
			if (fieldIsEmpty(formE[i],"Please enter the number of sales people.")) {
				return false;
			}
		}
		
		switch (formE[i].name) {
		case "first_name":
			if (fieldIsEmpty(formE[i],"Please enter your first name.")) {
				return false;
			}			
			break;

		case "last_name": 
			if (fieldIsEmpty(formE[i],"Please enter your last name.")) {
				return false;
			}
			break;

		case "email":
			if (fieldIsEmpty(formE[i],"Please enter your email address.")) {
				return false;
			}
			else if (emailIsInvalid(formE[i],"Please enter a valid email address.")) {
				return false;
			}
			break;

		case "phone":
			if (fieldIsEmpty(formE[i],"Please enter your phone number.")) {
				return false;
			}				
			else if (phoneIsInvalid(formE[i],11,10,"Please enter a valid phone number, like: (415)555-1212 or 415-555-1212")) {
				return false;
			}
			break;

		case "title": 
			if (fieldIsEmpty(formE[i],"Please enter your title.")) {
				return false;
			}
			break;
		
		case "company": 
			if (fieldIsEmpty(formE[i],"Please enter your company.")) {
				return false;
			}
			break;

		case "state":
			if (fieldIsEmpty(formE[i],"Please enter your state or province.")) {
				return false;
			}
			break;

		case "country":
			if (fieldIsEmpty(formE[i],"Please enter your country.")) {
				return false;
			}
			break;

		case "description":
			if (fieldIsEmpty(formE[i],"Please provide a little more information.")) {
				return false;
			}
			break;
		}  // end of switch
	}  // end of for loop
	return true;
}

/* Register the onsubmit event and assign a function to it: */
function setupForm() {
	if (document.forms[0]) {
		document.forms[0].onsubmit = checkForm_onsubmit;
	}
}

/*------ Function to attach multiple functions to the window.onload event ------*/
/* Courtesy of Simon Willison */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}

addLoadEvent(startList);
addLoadEvent(setupLinks);
addLoadEvent(setupForm);