// whitespace characters
var whitespace = " \t\n\r";

// A function to warn the user not to leave the page without Submitting it
// or they will lose their form data. Warning occurs when the browser does not
// support preventing leaving the page.
//
function warnUserToStayOnPage() {
	if (is_nav4up) {
		alert( "BEWARE!\n\nIf you leave this page without submitting the form, " +
				"you will lose any data you enter.\nPlease submit the " +
				"form before going to another web page.")
	}
}

// A script to prevent changing pages if the form is dirty. Allows user to cancel
// changing pages if they want to.
//
function preventPageChange() {
	var msgText = "Please note your changes were not saved.\n\nTo save your changes, press Cancel then submit your changes.";
	if (document.popupActive) {
		return;
	}
	if (document.pageChangeMsg) {
		msgText = document.pageChangeMsg;
	}
	if (isFormDirty()) {
		event.returnValue = msgText;
	}
}


// This function sets a property on the form to the value passed (true or false)
// Add as the setDirty() method on the form object.
//
function formDirty(dirtyFlag) {
	this.dirtyFlag = dirtyFlag;
}

//------------------------------------------------
// Date related functions
//------------------------------------------------
// today()
//
// Returns a string containing the date of the local machine.
//
function today() {
  var d, s = "";
  d = new Date();
  s = (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getFullYear();
  return(s);
}

// parseDateString(String date)
//
// Given valid formatted date string (m[m]/d[d]/yyyy),
// returns array of integers with [0]=month, [1]=day, [2]=year
//
function parseDateString( theDate ) {
	newDateParts = new Array(3);
	i1 = theDate.indexOf("/", 0);
	var tempString = theDate.slice(0,i1);
	if (tempString.charAt(0) == "0") tempString = tempString.slice(1)
	newDateParts[0] = parseInt(tempString);
	i2 = theDate.indexOf("/", i1 + 1);
	tempString = theDate.slice(i1 + 1, i2);
	if (tempString.charAt(0) == "0") tempString = tempString.slice(1)
	newDateParts[1] = parseInt(tempString);
	newDateParts[2] = parseInt(theDate.slice(i2 + 1));
	return newDateParts;
}

// compareDateByParts( INT ARRAY dateParts1, INT ARRAY dateParts2)
//
// Compares dates passed in by their three parts.
// Returns:	-1 for first date < second date,
//			0 for dates equal,
//			1 for first date > second date
// Arrays have [0]=month, [1]=day, [2]=year.
// Should be pre-validated dates.
//
function compareDateByParts( dp1, dp2) {
	// Compare years first...
	cmp = ((dp1[2] < dp2[2]) ? -1 : ((dp1[2] > dp2[2]) ? 1 : 0));
	if (cmp == 0) {
		// Years are equal, check months...
		cmp = ((dp1[0] < dp2[0]) ? -1 : ((dp1[0] > dp2[0]) ? 1 : 0));
		if (cmp == 0) {
			// Month and year are equal, check day...
			cmp = ((dp1[1] < dp2[1]) ? -1 : ((dp1[1] > dp2[1]) ? 1 : 0));
		}
	}
	return cmp;
}

// compareDateStrings( string date1, string date2 )
//
// Compares dates passed in as strings in the format: "m[m]/d[d]/yyyy".
// Returns:	-1 for first date < second date,
//			0 for dates equal,
//			1 for first date > second date
// Should be pre-validated dates.
//
function compareDateStrings(dt1, dt2) {

	dp1 = parseDateString( dt1 );
	dp2 = parseDateString( dt2 );
	return compareDateByParts( dp1, dp2 );
}


//-------------------------------------------------------------------
// Whitespace utilities
//-------------------------------------------------------------------

// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripInitialWhitespace (s)
{   var i = 0;
//	while ((i < s.length) && charInString(s.charAt(i), whitespace))
	while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
		i++;

    return s.substring (i, s.length);
}
// Removes final (trailing) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripFinalWhitespace (s) {

	var i = s.length - 1;
//    while ((i >= 0) && charInString (s.charAt(i), whitespace)) {
    while ((i >= 0) && (whitespace.indexOf(s.charAt(i)) != -1)) {
       i--;
	}
    return s.substring (0, i + 1);
}

// Removes leading and trailing whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
	function trim(myString)
	{
		if (myString == null) return("");
		else 
		{
			myString = myString.toString(); //make sure it is a string
			return(myString.replace(/^\s+|\s+$/g, "")); //remove leading and trailing white space
		}
	}

//-------------------------------------------------------------------
// EMail Function
//-------------------------------------------------------------------
// Automatically fill in email field based on the person's
// name value. In other words, If "Scott Daggett" is the name
// passed in, then the email field will automatically get
// "scott.daggett@capmark.funb.com" if there is not already a value
// in the email field. (You can pass in 4th parameter to indicate
// what to use instead of "@capmark.funb.com".)
//
function setEmail (theName, theEmail, theNextfocus, emailService) {
	var s, e, fn, ln, i, cnt = 0;
	var ems = "@capmark.funb.com";
	var temp = "";

	if (setEmail.arguments.length == 4) {
		ems = emailService;
	}
	// parse name based on 1 embedded space (if > 1, dont autofill email address,
	// since we dont know the name parts for sure)
	//
	s = trim(theName.value);
	for (i = 0; i < s.length; i++) {
		if (s.charAt(i) == ' ') {
			cnt++;
		}
	}

	// check if first and last name were typed normally, i.e. space btw the 2 pieces
	//
	if (cnt == 1) {
		// make sure something was not already typed in the email field
		//
		e = trim(theEmail.value);
		if (e.length == 0) {
			fn = s.substr(0,s.indexOf(' '));
			ln = Trim(s.substr(s.indexOf(' ')+1, s.length));
			temp = fn + "." + ln + ems;
			theEmail.value = temp.toLowerCase();
			theNextfocus.focus();
		}
	}
}


/// This function will be called after new formatting (document.updForm.format() function) 
/// ($ sign etc) has been applied to textboxes. This function will re-set the default 
/// values of each textboxes.
function setDefaultFormValues(eForm)
{
	var iNumElems = eForm.elements.length;
	for (var i=0;i<iNumElems;i++)
	{
		var eElem = eForm.elements[i];
		if ("text" == eElem.type || "TEXTAREA" == eElem.tagName)
		{
			if (eElem.value != eElem.defaultValue){
				eElem.defaultValue = eElem.value;			
			}
		}else if ("SELECT" == eElem.tagName)
		{
			var cOpts = eElem.options;
			var iNumOpts = cOpts.length;
			for (var j=0;j<iNumOpts;j++)
			{
				var eOpt = cOpts[j];
				if (eOpt.selected != eOpt.defaultSelected)
				{
					eOpt.defaultSelected = true;
					j = iNumOpts ; //break out of loop										
				}
			}
		}
		
	}
}


//-------------------------------------------------------------------
// Checks and see if passed form is actually dirty or not!
//-------------------------------------------------------------------
// It receives form object (document.form[0]) as a parameter and 
// walks through each element of the form object and checks to see
// if any of the elements where changed from their default value.
// This way if user dirties the form and then undo the changes, it will
// not consider form as dirty and user will not see annoying message boxes
// unless form was dirty.
//

if (document.IsDirty == null)
{
	function IsDirty(eForm)
	{
		var iNumElems = eForm.elements.length;
		for (var i=0;i<iNumElems;i++)
		{
			var eElem = eForm.elements[i];
			if ("text" == eElem.type || "TEXTAREA" == eElem.tagName)
			{
					if (eElem.value != eElem.defaultValue) return true;
			}
			else if ("checkbox" == eElem.type || "radio" == eElem.type)
			{
				if (eElem.checked != eElem.defaultChecked) return true;
			}
			else if ("SELECT" == eElem.tagName)
			{
				var cOpts = eElem.options;
				var iNumOpts = cOpts.length;
				for (var j=0;j<iNumOpts;j++)
				{
					var eOpt = cOpts[j];
					if (eOpt.selected != eOpt.defaultSelected) return true;
				}
			}
		}
		return false;
	}
}
