// JavaScript Document
init = function() {
	// alert("init loaded");
	// Activate drop downs in IE
	if (document.all&&document.getElementById) {
		cssdropdownRoot = document.getElementById("main-menu");
		for (x=0; x<cssdropdownRoot.childNodes.length; x++) {
			node = cssdropdownRoot.childNodes[x];
			if (node.nodeName == "LI") {
				node.onmouseover = function() { this.className+=" over"; }
				node.onmouseout = function() { this.className = this.className.replace(" over", ""); }
			}
		}
	}
}

function Trim(s) {
	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')){
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')){
		s = s.substring(0,s.length-1);
	}
	return s;
}
	
function checkForm(thisForm){
	success = true;
	errorCount = 0;
	fieldList = "";
	
	if (Trim(thisForm.firstname.value).length == 0){
		errorCount += 1;
		fieldList = fieldList + ", First Name";
		}
		
	if (Trim(thisForm.lastname.value).length == 0) {
		errorCount += 1;
		fieldList += ", Last Name";
	}
	
	if (Trim(thisForm.email.value).length == 0) {
		errorCount += 1;
		fieldList += ", Email";
	}
	
	if (Trim(thisForm.phone.value).length == 0) {
		errorCount += 1;
		fieldList += ", Phone";
	}
	
	//extra code to handle the extra required fields on franchise request form
	if (thisForm.name == "_TFDCFranchiseRequest") {
		if (thisForm.startamount.selectedIndex == 0){
		errorCount += 1;
		fieldList += ", Liquid Capital";
		}
		
		if (thisForm.HowFind.selectedIndex == 0){
		errorCount += 1;
		fieldList += ", Where did you hear about us?";
		}
	}
	
	// if there are errors, popup an alert box
	if (errorCount > 0){
		//cheap hack to remove the first 2 characters
		fieldList = fieldList.substring(2,fieldList.length)
		success = false;
		alert('The following fields are required: ' + fieldList);
	}
	return success;
}

function validateDDayEmail(formName) {

	var formNum = getFormNum(formName);
	var strErrMsg = '';
	var strFocus = '';
	var strBadEmail = '';

	function validateTextField(formName, elementName, fieldLabel) {
		var formNum = getFormNum(formName);
		if (document.forms[formNum].elements[getElementNum(formName, elementName)].value == '') {
			if (strErrMsg != '') {
				strErrMsg = strErrMsg + '\n- ' + fieldLabel;
			}
			else {
				strErrMsg = '\n- ' + fieldLabel;
			}
			if (strFocus == '') {
				strFocus = elementName;
			}
		}
	}
	
	function validateListField(formName, elementName, fieldLabel) {
		var formNum = getFormNum(formName);
		if (document.forms[formNum].elements[getElementNum(formName, elementName)][document.forms[formNum].elements[getElementNum(formName, elementName)].selectedIndex].value == 0) {
			if (strErrMsg != '') {
				strErrMsg = strErrMsg + '\n- ' + fieldLabel;
			}
			else {
				strErrMsg = '\n- ' + fieldLabel;
			}
			if (strFocus == '') {
				strFocus = elementName;
			}
		}		
	}

	validateTextField(formName, 'email', 'Your E-mail Address');
	
	if(document.forms[formNum].elements[getElementNum(formName, 'email')].value != '' && !isEmail(document.forms[formNum].elements[getElementNum(formName, 'email')].value)) {
		strBadEmail = 'Your Email Address';
	}
	
	if (strBadEmail != '') {
		if (strErrMsg != ''){
			strErrMsg = strErrMsg + '\r\r*' + strBadEmail + ' is not valid.  Please make\rsure it was entered correctly and contains no spaces.';
		}
	}
	
	if (strErrMsg != '') {
		alert('Sorry...\rYou seemed to have forgotten to enter some information.\rPlease make sure you have entered the following:\r ' + strErrMsg);
		document.forms[formNum].elements[getElementNum(formName, strFocus)].focus();
		return false;
	}
	else if (!strBadEmail == '') {
		alert('Sorry...\r' + strBadEmail + ' is invalid.  Please make\rsure it is entered correctly and does not contain any spaces.');
		document.forms[formNum].elements[getElementNum(formName, 'txtEmail')].focus();
		return false;
	}
	else return true;
}

function getElementNum (formName, elementName) {
	var formNum = getFormNum(formName);
	var i;
	for (i = 0; i < document.forms[formNum].elements.length; i++){
		if (elementName == document.forms[formNum].elements[i].name) {
			formElement = i;
			break;
		}
	}
	return formElement;
}

function getFormNum(formName) {
	var formNum =-1;
	var i;
	for (i=0;i<document.forms.length;i++){
		tempForm = document.forms[i];
		if (formName == document.forms[i].name) {
			formNum = i;
			break;
		}
	}
	return formNum;
}

function isEmail(str) {
  // are regexps supported?
  var supported = 0;
  if (window.RegExp) {
	 var tempStr = "a";
	 var tempReg = new RegExp(tempStr);
	 if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)|(\\s)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function toggleImage(id){
	e = document.getElementById(id);
	if (e.src.indexOf("arrow-down") == -1){
		e.src = "images/arrow-down.gif";
	}
	else {
		e.src = "images/arrow-up.gif";
	}
}

function toggleStatus(id)
{
	el = document.getElementById(id);
	var display = el.style.display ? '' : 'none';
	el.style.display = display;
}
