// Scripts for UI Menu from Bankers

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// ----------------------------------------------------------------------
// Custom Javascript routines.
// Author: David Ness
//
// http://www.David-Ness.com/
//
// ----------------------------------------------------------------------



function removeCommas(str) {
// I verified that this function is called within app. David Ness 8/18/2006
// Strip any commas that may be in string. 
// Must be recognized as a type string or indexOf function won't work
	str = String(str);
	while (str.indexOf(',') >= 0) {
		var temp = '';
		
		// if the comma is the first character, create a substring of everything after
		if (str.indexOf(',') == 0 && str.length > 1) {
			temp = str.substring(1, str.length);
			// if teh comma is not first or last character, join all before and all after comma
		} else if (str.indexOf(',') > 0 && str.indexOf(',') < str.length-1) {
			temp = str.substring(0, str.indexOf(',')) + str.substring(str.indexOf(',')+1, str.length);
			// if the comma is the last character, create a substring of everything before
		} else if (str.indexOf(',') > 0 && str.indexOf(',') == str.length-1) {
			temp = str.substring(0, str.length - 1);
		} 
		// save changes
		str = temp;
	}
	return str;
}


function formatAsMoney(mnt) {
// I verified that this function is called within app. David Ness 8/18/2006
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
};

function formatCurrency(num) {
// dness added 2/2/2009
// similar to the above, except returns dollar sign and commas
// Used on warranty limit override
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}





/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";


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(dtStr, minYear, maxYear){
	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 (dtStr.length == 0) {
		return true	
	}
	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){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if( minYear == maxYear ) {
			alert("The date must be in the year "+minYear+"  " )
		} else {
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		}
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


function isDateClosing(dtStr){
	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 (dtStr.length == 0) {
		return true	
	}
	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){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0){
		alert("Please enter a valid 4 digit year.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}



function isDateNoAlert(dtStr, minYear, maxYear){
	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 compareDates(value1, value2) {
/*	A simple function that takes two well formed dates.
	The function will compare the dates and return:

		 0 if the dates are same
		-1 if the first one is an earlier date
		 1 if the first one is a later date
*/
   var date1, date2;
   var month1, month2;
   var year1, year2;

   month1 = value1.substring (0, value1.indexOf (dtCh));
   date1 = value1.substring (value1.indexOf (dtCh)+1, value1.lastIndexOf (dtCh));
   year1 = value1.substring (value1.lastIndexOf (dtCh)+1, value1.length);

   month2 = value2.substring (0, value2.indexOf (dtCh));
   date2 = value2.substring (value2.indexOf (dtCh)+1, value2.lastIndexOf (dtCh));
   year2 = value2.substring (value2.lastIndexOf (dtCh)+1, value2.length);
   
   if(month1.length == 1) {month1 = '0' + month1}
   if(date1.length == 1) {date1 = '0' + date1}
   if(month2.length == 1) {month2 = '0' + month2}
   if(date2.length == 1) {date2 = '0' + date2}

   if (year1 > year2) return 1;
   else if (year1 < year2) return -1;
   else if (month1 > month2) return 1;
   else if (month1 < month2) return -1;
   else if (date1 > date2) return 1;
   else if (date1 < date2) return -1;
   else return 0;
} 


// example on calling the function isDate()
function ValidateDate(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }



// ---------------------------------------------------------------------------- 
// GetElementWithNextTabIndex 
// 
// Description: returns the element with the next highest tab index or 
//    the first tabindex if passed the highest element (it wraps) 
// 
// Arguments : 
//    elCurrent               : element with current tab index 
// 
// Dependencies : none 
// 
// History : 
// 2006.09.20 - WSR : created 
//                    based on code from Jefferson Scher (http://jscher2000.home.att.net/webdev/Enter-Does-Tab.html) 
// 
function GetElementWithNextTabIndex ( elCurrent ) 
   { 

   var numIndex = 0; 
   var numTabIndex = 0; 

   // get index of this control 
   var arrElements = elCurrent.form.elements; 
   for ( var i = 0; i < arrElements.length; i++ ) 
      { 

      if ( arrElements[i] == elCurrent ) 
         { 
         numIndex = i; 
         break; 
         } 

      } 

   numTabIndex = elCurrent.tabIndex; 

   // if control has tabindex 
   if ( numTabIndex != 0 ) 
      { 

      // find next highest tabindex 
      var numNextTabIndex = 99999999; 
      var numNextIndex = -1; 
      var numFirstTabIndex = numTabIndex; 
      var numFirstIndex = numIndex; 
      for ( var k = 0; k < arrElements.length; k++ ) 
         { 

         // if not the target element and it isn't disabled and it isn't hidden 
         if ( k != numIndex && arrElements[k].disabled == false && arrElements[k].type != 'hidden' ) 
            { 

            // if it has a greater tab index than the target element and is less than the previous candidate tab index 
            if ( arrElements[k].tabIndex >= numTabIndex && arrElements[k].tabIndex < numNextTabIndex ) 
               { 

               // update candidate tab index value & save element index 
               numNextTabIndex = arrElements[k].tabIndex; 
               numNextIndex = k; 

               } 
            // if it has a lower tab index than the target element and is less than the previous minimum tab index 
            else if ( arrElements[k].tabIndex < numTabIndex && arrElements[k].tabIndex < numFirstTabIndex ) 
               { 

               // update first tab index value & save element index 
               numFirstTabIndex = arrElements[k].tabIndex; 
               numFirstIndex = k; 

               } 

            }                        

         } 

      // if we found one higher 
      if ( numNextIndex > -1 ) 
         { 

         return arrElements[numNextIndex]; 

         } 
      // if we didn't find one higher but found a different first one 
      else if ( numFirstIndex != numIndex ) 
         { 
      
         return arrElements[numFirstIndex]; 

         } 

      } 
   // if no tab index 
   else 
      { 

      // find next highest by natural order 
      for ( var j = numIndex + 1; j < arrElements.length; j++ ) 
         { 

         // if we found one 
         if ( arrElements[j].tabIndex == 0 && arrElements[j].disabled == false && arrElements[j].type != 'hidden' ) 
            { 

            return arrElements[j]; 

            }    

         } 

      // we didn't find next by natural order so find first 
      for ( var f = 0; f < numIndex; f++ ) 
         { 

         // if we found one 
         if ( arrElements[f].tabIndex == 0 && arrElements[f].disabled == false && arrElements[f].type != 'hidden' ) 
            { 

            return arrElements[f]; 

            } 

         } 


      } 

   } 
// 
// GetElementWithNextTabIndex 
// ---------------------------------------------------------------------------- 


// ---------------------------------------------------------------------------- 
// FocusElementWithNextTabIndex 
// 
// Description: focuses the element with the next highest tab index or 
//    the first tabindex if passed the highest element (it wraps) 
//    does this in a try catch with the catch recursively calling 
//    so we keep trying focus till there is something we can focus 
// 
// Arguments : 
//    elCurrent               : element with current tab index 
// 
// Dependencies : 
//    GetElementWithNextTabIndex 
// 
// History : 
// 2006.09.20 - WSR : created 
//                    based on code from Jefferson Scher (http://jscher2000.home.att.net/webdev/Enter-Does-Tab.html) 
// 
function FocusElementWithNextTabIndex ( elCurrent ) { 

var elNext = GetElementWithNextTabIndex(elCurrent); 
	try
	  {

	  // focus next element
	  elNext.focus();

	  }
	catch ( objException )
	  {

	  // recursive call
	  FocusElementWithNextTabIndex(elNext);

	  }
}
// 
// FocusElementWithNextTabIndex 
// ---------------------------------------------------------------------------- 


// ---------------------------------------------------------------------------- 
// frmRequest_KeyPress 
// 
// Description: event handler for request form key press event 
//    cancels returns on form elements that would prematurely submit the form 
// 
// Arguments : 
//    e - the event object 
// 
// Dependencies : none 
// 
// History : 
// 2006.07.13 - WSR : adapted to this project 
// 2006.09.20 - WSR : revised for enter does tab behaviour 
//                    based on code from Jefferson Scher (http://jscher2000.home.att.net/webdev/Enter-Does-Tab.html) 
// 
function frmRequest_KeyPress( e ) 
   { 

	var numCharCode; 
	var elTarget = null; 
	var strType = ''; 
	var arrElements = null; 
	var numIndex = 0; 
	var numTabIndex = 0; 
	var elName = ''; 
	var elCurrent = null; 
	

	// get event if not passed 
	if (!e) var e = window.event; 
	
	// get character code of key pressed 
	if (e.keyCode) numCharCode = e.keyCode; 
	else if (e.which) numCharCode = e.which;
	
	   
	// trap backspace key on Menus
	if ( numCharCode == 8 ) { 

		// get target 
		if (e.target) elTarget = e.target; 
		else if (e.srcElement) elTarget = e.srcElement; 
	
		elName = elTarget.nodeName.toUpperCase(); 
		if ( elName == 'INPUT' ) 
			if ( elTarget.getAttribute('type') ) 
            	elName = elTarget.getAttribute('type').toUpperCase();
			
		if( elName == 'HTML' ) return false;
	
		// alert( elName );
		// based on type 
		switch ( elName ) { 
			 case 'CHECKBOX' : 
			 case 'RADIO' : 
			 case 'SELECT' : 
	
				// focus element with next tab index 
				// FocusElementWithNextTabIndex(elTarget); 
	
				// if we got this far we couldn't find the next item to tab to - but cancel anyway 
				// cancel event to prevent form submission 
				return false; 
	
				break; 
		} 
	} 

   
   // if the enter key (optimization as we only process enter key) 
   if ( numCharCode == 13 ) 
      { 

      // get target 
      if (e.target) elTarget = e.target; 
      else if (e.srcElement) elTarget = e.srcElement; 

      elName = elTarget.nodeName.toUpperCase(); 
      if ( elName == 'INPUT' ) 
         if ( elTarget.getAttribute('type') ) 
            elName = elTarget.getAttribute('type').toUpperCase(); 

      // based on type 
      switch ( elName ) 
         { 
         case 'CHECKBOX' : 
         case 'RADIO' : 
         case 'TEXT' : 
         case 'SELECT' : 

            // focus element with next tab index 
            FocusElementWithNextTabIndex(elTarget); 

            // if we got this far we couldn't find the next item to tab to - but cancel anyway 
            // cancel event to prevent form submission 
            return false; 

            break; 
                
         } 

      } 

   // process default action 
   return true; 

   } 
// 
// frmRequest_KeyPress 
// ---------------------------------------------------------------------------- 



/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	return true					
}
	

function stripSpaces( fieldref ) {
	if( !fieldref ) {
		return
	} else {
    	var x = fieldref.value;
    	fieldref.value = (x.replace(/^\W+/,'')).replace(/\W+$/,'');
	}
}




// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj) {
		//alert('1');
		return "";
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		//alert('2');
		if(radioObj.checked) {
			//alert('3');
			return radioObj.value;
		} else {
			// not a radio object, but a regular form object, so return value.
			//alert('4');
			return radioObj.value;
		}
	}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
		// Radio mode, and a value was found to be selected
			//alert('5');
			return radioObj[i].value;
		}
	}
	// Radio mode, but no radios selected.
	//alert('6');
	return "";
}


function displayTRCCAlert() {
	// dness 8-31-2009: This functions should never be called, and has been disabled throughout the solution.
	/* GCAlertText =	'Alert - The TRCC Registration we have on file for you has expired.\n' +
					'Please contact our Builder Services Department once you\n' +
					'can provide your updated TRCC Registration information.\n\n' +
					'You will not be able to submit new enrollment forms\n' +
					'until this requirment has been met.';
	alert(GCAlertText);
	*/
}
