//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
/*
<!---

Author: Donny
Date: May 20, 2004
Desc: Contains standard validation functions for HTML forms (borrowed from LIN)

<in>

</in>
          
<out>

</out>

--->
*/


// Donny: Commented out functions, we don't need these any more? (July 8, 2004)
/*
//Initialize the validation message
var valMsg = "";

function an(word){
	word = word.toLowerCase();
	if(word.charAt(0) == 'a' || word.charAt(0) == 'e' || word.charAt(0) == 'i' || word.charAt(0) == 'o' || word.charAt(0) == 'u')
		return 'an ' + word;
	else
		return 'a ' + word;
}

function confirmAction(entName,entValue,actionName)
{
	//Used to confirm the deletion of an item
	return confirm("Are you sure you want to " + actionName + "\n" + entName + ": " + entValue);
}

function updateMessage(entType,entId)
{
	// Display a message informing the user that an update has been performed
	alert("Thank you. " + entType + " " + entId + " has been updated.")
}

*/

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//
function disableButton() {
  button = document.getElementById("submitbutton");
	if(button) {
    button.style.color = '#999999';
    button.disabled = true;
	}
}


function isRequired(theField)
{
	//Used to check for empty fields
	if (theField.value.length < 1 || theField.value == "")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isNumberKey(formField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive numbers
	//Donny: allow only 1 decimal
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	//kevin: allow bckspace and delete
	if (theKey ==8 || theKey ==46) return true;
	var theChar = String.fromCharCode(theKey);
	// Make sure there is no decimal if the user type in a decimal
	if (formField.value.indexOf(".") != -1 && theChar == "."){
		return false;
	// Other than decimal, allow no non-numeric char
	} else if (theChar != "." && (isNaN(theChar) || theChar == " ")){
		return false;
	}
	return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isPosNegNumberKey(formField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers (+ve and -ve)
	//Donny: allow only 1 decimal
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	//kevin: allow bckspace and delete
	if (theKey ==8 || theKey ==46) return true;
	var theChar = String.fromCharCode(theKey);
	
	// allow the char '-' if it is the first character the user type in (Donny: that should be char:45... what is char:189??)
	if (theKey==189 || theKey==45 || theKey==109 && formField.value.length == 0)
		return true;
	// Make sure there is no decimal if the user type in a decimal
	if (formField.value.indexOf(".") != -1 && theChar == "."){
		return false;
	// Other than decimal, allow no non-numeric char
	} else if (theChar != "." && (isNaN(theChar) || theChar == " ")){
		return false;
	}
	return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKey(evt)
 	{
 	       
 	        evt = (evt) ? evt : event;
 	        var theKey = (evt.which) ? evt.which : event.keyCode;
 	        var theChar = String.fromCharCode(theKey);
 	       
 	        if ((isNaN(theChar)) || theChar == " ")
 	                return false;
 	        else
 	                return true;
 	}
 	
/*function isIntNumberKey(obj, evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive integers
	evt = (evt) ? evt : event;
    var theKey = 0;
    if (window.event) theKey = window.event.keyCode;
    else if (evt) theKey = evt.which;
    var theChar = String.fromCharCode(theKey);
	if (((isNaN(theChar)) && theKey != 8) || theChar == " " || theChar < 1)
		return false;
	else
		return true;
}*/

function isNonZero(obj, evt) {
	//Used in the onKeyPress event of a form field, only allows the user to type positive integers
	evt = (evt) ? evt : event;
    var theKey = 0;
    if (window.event) theKey = window.event.keyCode;
    else if (evt) theKey = evt.which;
	if (theKey ==8 || theKey ==46) return true;
    var theChar = String.fromCharCode(theKey);
    var value = obj.value+theChar;
	if ((isNaN(theChar)) || theChar == " " || value < 1)
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKeyNeg(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive and negative integers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isPhoneKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "(" && theChar != ")" && theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isNotApostrophe(thisField,evt)
{
	var theKey;
	var theChar;
		
	if (window.event)
	   theKey = window.event.keyCode;
	else if (evt)
	   theKey = e.which;
	else
	   return true;
	
	theChar = String.fromCharCode(theKey);
	if (theChar == "'") {
		return false;
	}
	else
		return true;
}

function isDateKey(thisField,evt)
{
	var theKey;
	var theChar;
		
	if (window.event)
	   theKey = window.event.keyCode;
	else if (evt)
	   theKey = e.which;
	else
	   return true;
	
	theChar = String.fromCharCode(theKey);
	if (isNaN(theChar) && theChar != "/") {
		return false;
	}
	else
		return true;
}

function isDateKey2(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers and slashes in the correct position
	var theKey;
	var theChar;
	
	if (window.event)
	   theKey = window.event.keyCode;
	else if (evt)
	   theKey = e.which;
	else
	   return true;
	
	theChar = String.fromCharCode(theKey);
	var tokens=thisField.value.split("/");
	var fieldLength=tokens[tokens.length-1].length;
	if (document.selection.createRange().text.length && !isNaN(theChar))
		return true;	
	else if (isNaN(theChar) && theChar != "/") {
		return false;
	}
	else if (fieldLength == 0 && theChar == "/") {
		return false;
	}
	else if ( ((fieldLength ==2 && tokens.length !=3) || (fieldLength ==4 && tokens.length ==3)) && theChar !="/") {
		return false;
	}
	else
		return true;
	/*
	if (((thisField.value.length < 2) || (thisField.value.length < 5 && thisField.value.length > 2) || (thisField.value.length > 5)) && (isNaN(theChar)) || theChar == " ")
		return false;
	else if (((thisField.value.length == 2) || (thisField.value.length == 5)) && theChar != "/")
		return false;
	else
		return true; */
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function moveNextFld(thisField, fieldLen, nextField)
{
	//This function moves the focus to the next field if the current field is full
	if (thisField.value.length == fieldLen)
		nextField.focus();
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isChecked(theField)
{
	//Used to check for empty checkboxes
	if (!theField.checked){
		//valMsg = valMsg + "Please check the " + fieldName + " box.\n";
		return false;
	} else {
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isRange(theField,fromNo,toNo)
{
	//Used to check for numeric ranges in fields, does not check for blanks
	if ((theField.value.length > 0 || theField.value != "") && (theField.value < fromNo || theField.value > toNo)){
		//valMsg = valMsg + fieldName + " must be between " + fromNo + " and " + toNo + ".\n";
		return false;
	} else {
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function mustChoose(theField)
{
	//Used to force a selection from a drop-down (with a default value of 0 or nothing)
	if (theField.value == 0 || theField.value == ""){
		//valMsg = valMsg + "Please choose " + an(fieldName) + ".\n";
		return false;
	} else {
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isDateOrig(theField){
	var error  = true;
	var sdate = theField.value.split("/")	
	if(theField.value.length != 0){
	//if the field is empty, no error -> use isRequiredDate() for requiredDate Field
		if (!(theField.value.length == 8 || theField.value.length == 10))
			error = false;
		else if (sdate.length <3)
			error=false;
		else if (sdate[2].length <4)
			error=false;
	}
	if (error == true && theField.value.length > 0) {		
	//if the field is empty, no error -> use isRequiredDate() for requiredDate Field
		var chkDate=new Date(Date.parse(theField.value))
      var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(Math.abs(sdate[2]))
   	var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
		var year = Math.abs(sdate[2]);
		if (indate2!=cmpDate || (year > 99 && year < 1900)) //make sure if the year is 4 digit it has to be > 1900
		   error=false;
   	else {
   		if (cmpDate=="NaN/NaN/NaN")
				error = false;
	   }
	}
	return error;
}


/*
function isDate(theField){
	var error  = true;
	var sdate = theField.value.split("/")	
	if(theField.value.length != 0){
	//if the field is empty, no error -> use isRequiredDate() for requiredDate Field
		if (theField.value.length != 10){
			error = false;
			//alert("Num 1");
		}
		else {
				if (sdate.length <3){
					error=false;
					//alert("Num 2");
				}
				else {
		if (!(sdate[0].length == 2)){
			error=false;
			//alert("Num 3");
			}
		if (!(sdate[1].length == 2)){
			error=false;
			//alert("Num 4");
			}
		if (!(sdate[2].length == 4)){
			error=false;
			//alert("Num 5");
			}
			}
		}
	}
	if (error == true && theField.value.length > 0) {		
	//if the field is empty, no error -> use isRequiredDate() for requiredDate Field
		var chkDate=new Date(Date.parse(theField.value))
      var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(Math.abs(sdate[2]))
   	var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
		var year = Math.abs(sdate[2]);
		if (indate2!=cmpDate || (year > 99 && year < 1900)) //make sure if the year is 4 digit it has to be > 1900
		   error=false;
   	else {
   		if (cmpDate=="NaN/NaN/NaN")
				error = false;
	   }
	}
	return error;
}
*/

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(theField){

if(theField.value.length != 0) {
	var dtStr = theField.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
}
return true
}




function isRequiredDate(theField){
	if (theField.value.length == 0){
		return false;
	} else
		return isDate(theField);
}

function isNotRequiredDateNoAlert(theField) {
	if (!theField.value.length)
		return true;
	else
		return isDate(theField);
}

function isNotRequiredDate(theField) {
	if (!theField.value.length){
		return true;
	} 
	else {
		var check = isDate(theField);
		if (!check) {
			alert('You must enter a date in MM/DD/YYYY format');
			theField.focus();
			return false;
		}
		return true;
	}
		
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function checkVal()
{
	//Used in the onSubmit event to check for any error messages
	if (valMsg != "")
	{
		alert(valMsg);
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function getToday(theField)
{
	//Used to populate a form field with today's date
	var theDate = new Date();
	theField.value =  (padNumber(theDate.getMonth()+1,2)) + "/" + padNumber(theDate.getDate(),2) + "/" + theDate.getFullYear();
}
function padNumber(number,charNo){
	// Donny: I pad the number to certain character length.  e.g. 2 -> 02
	// for used in getToday(), need to pad month and day otherwise, isDate() validation wont work
	number = number+'';
	for(index=number.length; index<charNo; index++){
		number = '0'+number;
	}
	return number;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function textAreaMaxLength(theField,maxLength)
{
	//Used in the onKeyPress event of a textarea field, to provide a maxlength capability
	if (theField.value.length > maxLength - 1){
		//alert("You have reached the limit of " + maxLength + " characters allowed in this field");
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function textFieldMaxLength(theField,maxLength)
{	
	//Used in the onKeyPress event of a textarea field, to provide a maxlength capability
	if (theField.value.length > maxLength){
		//valMsg = valMsg + fieldName + " can only have " + maxLength + " characters.\n";
		return false;
	} else {
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isDependRequired(Determiner,theField)
{
	if (Determiner != '0' && Determiner != 0)
	{
		if (theField.value.length < 1 || theField.value == "") {
			//valMsg = valMsg + "Please enter " + an(fieldName) + ".\n";
			return false;
		}
	}
	return true
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isEmail(theField)
{
	//Check if there's a @ sign in emails (donny: should use regex to do more thorough validation)
	if (theField.value.length > 0 && theField.value.indexOf('@') < 0){
		//valMsg = valMsg + "Please enter a valid " + fieldName + ".\n";
		return false;
	} else {
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isMatchField(the1stField,the2ndField)
{
	//Check if the value of the 2 form field is matching (not case-sensitive)
	if (the1stField.value != the2ndField.value){
		//valMsg = valMsg + "Please enter a valid " + fieldName + ".\n";
		return false;
	} else {
		return true;
	}
}

function mustCheckOne(fieldArray){
	for (var i=0; i < fieldArray.length; i++) {
		if(fieldArray[i].checked) // as soon there one check box is check, return true
			return true;
	} 
	return false;
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->

function trim(myString) {
 // skip leading and trailing whitespace
 // and return everything in between
  var x=myString;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}


//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function compareDates(date1,date2)
{
	var result = false;
	var d1 = date1.split("/");
	var d2 = date2.split("/");
	
	if (d1[2] < d2[2])
		result = true;
	else if (d1[2] > d2[2])
		result = false;
	else {
		if (d1[0] < d2[0])
			result = true;
		else if (d1[0] == d2[0] && d1[1] < d2[1])
			result = true;
		else
			result = false;
	}
	return result;
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->

	// FRAME THIS! (Annotated Version 1.7)
	// script home location http://www.gmdstudios.com/ideas/frames/
	// Authors: GMD Studios (Andrew Cowan & Brian Clark)
	// Contact: inquiries@gmdstudios.com
	//
	// This script is copyright 1997-8 by GMD Studios. Permission to
	// use, modify, and distribute this is granted as long as authors'
	// copyright statements are left intact. All other use prohibited.
	//
	// note: use <body  onLoad="framethis()"> to detect if this is the top frame or not
	// VARIABLE ASSIGNMENT
	// Set real_location to the URL of your website.
	// note: borrow this from HIGHFIVE.org
var real_location = "http://stage.nativetrax.com:8080/";
function framethis () {
	// FIRST LOOP - Browsers that support top.location
	if (top.NTMain != null && top.NTMain.location) {
		if (self != top.NTMain)
			top.NTMain.location = self.location;
	} else {
	// SECOND LOOP - Browsers that don't support top.location or don't have NTMain frame
		if (parent.location != null && parent.location) {
			if (parent.location != real_location)
				parent.location = real_location;
		}
	}
}

function bugPopup(id) {
  var bugPopup=window.open('bug.cfm?id='+id,'bugPopup','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=200,left = 0,top = 0');
  bugPopup.focus();
}

