//This is the final function check.
function finalCheck(form) {
	if (form.firmemail.value != null && form.firmemail.value != "") {
		if (emailCheck(form.firmemail.value)) {
			return true;
		}
		return false;
	}
	if (confirm("You have not entered a valid email address in the Firm's Email Address field of this form." +
			    "  If you do not enter a valid email address, you will NOT be automatically emailed a copy of " +
				"this scheduling form.  If this is ok, please press 'ok' below.  If you would like to go back " +
				"and enter a valid email address, please click 'cancel'. Thank you!")) {
	return true;
	}
	return false;
}

////////// This function will disable the enter key
var nav4 = window.Event ? true : false;
var debugFlag = false;
var enterKeyCode = 13;
var targetObject;
var enterKeyPressed = false;

function checkWhich(evt) {
	var theKey;
	if (nav4) {
		theKey = evt.which;
	} else {
		if (evt.type == "keypress") {
			theKey = evt.keyCode;
		}
		targetObject = evt.srcElement;
	} // end if nav4
	
	if (debugFlag) alert ("theKey = " + theKey);
	if(theKey == enterKeyCode) {
		enterKeyPressed = true;
		if (debugFlag) {
			alert("targetObject.name = " + targetObject.name);
		}
	} // end if theKey
	return true;
}

function checkForm(formName) {
	if(debugFlag) {
		alert("checkform called, must have been a submit.");
	}
	
	if(enterKeyPressed == true) {
		enterKeyPressed = false;
		if(debugFlag) {
			alert("do not Submit, enter key pressed");
		}
		targetObject.focus();
		return false;
	}
	return finalCheck(formName);
}

////////// This function will check to make sure that an email address is valid.
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Please enter a valid email address in the Firm's Email Address field.")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Please enter a valid email address in the Firm's Email Address field.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Please enter a valid email address in the Firm's Email Address field.")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Please enter a valid email address in the Firm's Email Address field.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Please enter a valid email address in the Firm's Email Address field.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Please enter a valid email address in the Firm's Email Address field."
   alert(errStr)
   return false
}
return true;
}

function setCurrentDate() {
  
   curDate = new Date();
   numMonth = curDate.getMonth();
   numDay = curDate.getDate();
   numYear = curDate.getFullYear();
   numDayWeek = curDate.getDay();
   
   strMon = "";
   
   //Find the long version of the month
   //and then set it in the drop down
   if (numMonth == 0) {
   	strMon = "January";
   } else if (numMonth == 1) {
   	strMon = "February";
   } else if (numMonth == 2) {
   	strMon = "March";
   } else if (numMonth == 3) {
   	strMon = "April";
   } else if (numMonth == 4) {
   	strMon = "May";
   } else if (numMonth == 5) {
   	strMon = "June";
   } else if (numMonth == 6) {
   	strMon = "July";
   } else if (numMonth == 7) {
   	strMon = "August";
   } else if (numMonth == 8) {
   	strMon = "September";
   } else if (numMonth == 9) {
   	strMon = "October";
   } else if (numMonth == 10) {
   	strMon = "November";
   } else if (numMonth == 11) {
   	strMon = "December";
   }
   
   setCurrentMonth(strMon);
   
   //Add a '0' to the front of the date if the length < 2
   //and then set it in the drop down
   if (numDay.length < 2) {
   	numDay = "0" + numDay;
   }
   
   setCurrentNumDay(numDay);
   
   //Setup the year in the drop down
   setCurrentYear(numYear);
   
   var strDay = "";
   //Find the long version of the day of week
   //and then set it in the drop down
   if (numDayWeek == 0) {
   	strDay = "Sunday";
   } else if (numDayWeek == 1) {
   	strDay = "Monday";
   } else if (numDayWeek == 2) {
   	strDay = "Tuesday";
   } else if (numDayWeek == 3) {
   	strDay = "Wednesday";
   } else if (numDayWeek == 4) {
   	strDay = "Thursday";
   } else if (numDayWeek == 5) {
   	strDay = "Friday";
   } else if (numDayWeek == 6) {
   	strDay = "Saturday";
   }
   setCurrentDay(strDay);
}

function setCurrentMonth(month) {

var list = document.deposition.month;

for (i=0; i<list.length; i++) {
     if (list.options[i].value == month) {
	list.options[i].selected = true;
	break;
     }
}


}

function setCurrentNumDay(numDay) {

var list = document.deposition.day;

for (i=0; i<list.length; i++) {
     if (list.options[i].value == numDay) {
	list.options[i].selected = true;
	break;
     }
}

}

function setCurrentYear(numYear) {

var list = document.deposition.year;

for (i=0; i<list.length; i++) {
     if (list.options[i].value == numYear) {
	list.options[i].selected = true;
	break;
     }
}

}

function setCurrentDay(strDay) {

var list = document.deposition.weekday;

for (i=0; i<list.length; i++) {
     if (list.options[i].value == strDay) {
	list.options[i].selected = true;
	break;
     }
}


}

function calculateDay() {

var listYear = document.deposition.year;
var listMonth = document.deposition.month;
var listDay = document.deposition.day;

var year = listYear[listYear.selectedIndex].value;
var month = listMonth[listMonth.selectedIndex].value;
var day = listDay[listDay.selectedIndex].value;

//we have to get the month number
if (month == "January") {
	month = 0;
} else if (month == "February") {
	month = 1;
} else if (month == "March") {
	month = 2;
} else if (month == "April") {
	month = 3;
} else if (month == "May") {
	month = 4;
} else if (month == "June") {
	month = 5;
} else if (month == "July") {
	month = 6;
} else if (month == "August") {
	month = 7;
} else if (month == "September") {
	month = 8;
} else if (month == "October") {
	month = 9;
} else if (month == "November") {
	month = 10;
} else if (month == "December") {
	month = 11;
}

theDate = new Date(year,month,day);

var numDayWeek = theDate.getDay();

   var strDay = "";
   //Find the long version of the day of week
   //and then set it in the drop down
   if (numDayWeek == 0) {
   	strDay = "Sunday";
   } else if (numDayWeek == 1) {
   	strDay = "Monday";
   } else if (numDayWeek == 2) {
   	strDay = "Tuesday";
   } else if (numDayWeek == 3) {
   	strDay = "Wednesday";
   } else if (numDayWeek == 4) {
   	strDay = "Thursday";
   } else if (numDayWeek == 5) {
   	strDay = "Friday";
   } else if (numDayWeek == 6) {
   	strDay = "Saturday";
   }
   
   setCurrentDay(strDay);
   
}

function createRequestObject() {
  
  FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
  
  separator = ',';
    // The token used to separate data from multi-select inputs
  
  query = '' + this.location;
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
    
  query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
  
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  
  keypairs = new Object();
  numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
    
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
      // Split the query string at each '&', storing the left-hand side
      // of the split in a new keypairs[] holder, and chopping the query
      // so that it gets the value of the right-hand string.
  }

  keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.
  
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
    
    keyValue = unescape(keyValue);
      // Unescape non-alphanumerics
      
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }

  return FORM_DATA;
}
