//====================================================================================
// FILE   : utilities.js                                                        
// AUTHOR : James A. Faulkner                                                    
// COMPANY: Citrus County Board of County Commissioners                          
// DATE   : 04/05/2001                                                           
//====================================================================================
// MODIFICATIONS:                                                                
//                                                                               
// DATE        MODIFICATION                                            INITIALS  
// ==========  ======================================================  ========  
// 04/06/2001  Added function to set the statusbar text, create masks
//             for user input, uppercase conversions, popup windows.      JF     
// 04/09/2001  Added function to show Internet Explorer's tooltip         JF     
// 04/10/2001  Added function to show a tooltip of unlimited size         JF     
// 04/11/2001  Added function to get a listbox's selected item            JF     
// 05/29/2001  Added function to limit the length of an input field       JF     
//                                                                    
//====================================================================================           


var offsetX;
var offsetY;

//====================================================================================
// showDate: Function to display today's date (according to the clients machine) 
//====================================================================================

function showDate() {
  
  months       = new Array("January", "February", "March", "April", "May", "June", 
                           "July", "August", "September", "October", "November", 
						   "December");
  dayOfWeek    = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                           "Saturday", "Sunday"); 
  todaysDate   = new Date();
  var thisYear = todaysDate.getYear();

  if (thisYear < 1000) thisYear += 1900;

  document.write(dayOfWeek[todaysDate.getDay() - 1] + "  " + 
                 months[todaysDate.getMonth()] + "  " + 
				 todaysDate.getDate() + ", " + thisYear);
}

//======================================================================================
// setStatus: Function to set the browser's status line with a message           
//======================================================================================

function setStatus(v_message) {

	// Set the browser's status to this new message
	window.status = v_message;
	return true;
}

//======================================================================================
// toolTip: Function to show a tooltip when called                               
//======================================================================================

function toolTip(v_message) {

	// Set the browser's status message to the same as the tooltip
	setStatus(v_message);
	
	// Set the IE generic tooltip message
	window.event.srcElement.title = v_message;
	return true;
}

//======================================================================================
// showToolTip: Function to show a tooltip when called                           
//======================================================================================

function showToolTip(v_offsetX, v_offsetY, v_message, v_event) {

	offsetX = v_offsetX;
	offsetY = v_offsetY;
	
	var toolTipHTML = '';
	
	// Build the actual 'table' that makes this tooltip possible -
	// Create 2 tables, the first one is all black (for the outline),
	// and the second one is a single cell with a yellow background
	toolTipHTML = '<table bgcolor="#000000" width="' + 200 +  
	                  '" cellspacing="0" ' + 'cellpadding="1" border="0"><tr><td>' +
					  '<table bgcolor="#FFFCCC" ' + 'width="100%" cellspacing="0" ' +
					  'cellpadding="1" border="0"><tr><td class="ToolTipText">' + 
					  v_message + '</td></tr></table></td></tr></table>';
					  
	
  	// Write the HTML at the specified coordinates and make it visible
	document.all.toolTipBox.innerHTML        = toolTipHTML;
	document.all.toolTipBox.style.pixelLeft  = v_event.x + offsetX;
	document.all.toolTipBox.style.pixelTop   = v_event.y + offsetY;
	
  	document.all.toolTipBox.style.visibility = "visible";
}

//======================================================================================
// hideToolTip: Function to hide the tooltip when called                         
//======================================================================================

function hideToolTip() {

	// Hide the tooltip
	document.all.toolTipBox.style.visibility = "hidden";
}

//======================================================================================
// followMouse: Function to keep the tooltip boxes moving with the mouse position
//======================================================================================

function followMouse() {

	// If we are showing the tooltip, then make it follow our mouse
	// movements, otherwise, don't...   
	if (document.all.toolTipBox.style.visibility == "visible") {
  	  document.all.toolTipBox.style.pixelLeft = event.x + offsetX;
	  document.all.toolTipBox.style.pixelTop  = event.y + offsetY;
	}
}

//======================================================================================
// limitLength: Function to limit the input length of an input field             
//======================================================================================

function limitLength(v_field, v_event, v_maxLength) {

	if ( (v_field.value.length > v_maxLength) && 
             (v_event.keyCode != 8) && 
             (v_event.keyCode != 9) && 
             (v_event.keyCode != 10) &&
             (v_event.keyCode != 13) ) {
	  v_event.keyCode = 0;
	  return false;
	}
	
	return true;
}

//======================================================================================
// shiftPressed: Function to determine whether the shift key was pressed         
//======================================================================================

function shiftPressed(v_event) {
  
  return ((window.Event) ? v_event.modifiers & Event.SHIFT_MASK : v_event.shiftKey);
}

//======================================================================================
// returnToTab: Function to capture keyboard input and convert a return to a tab 
//======================================================================================

function returnToTab(v_field, v_event) {

    // If this key is a 'return' or an 'enter', convert it to a tab so that
	// we just go to the next field instead of processing the form (if there
	// is a form)
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	}
	
	return true;
}

//======================================================================================
// noReturns: Function to keep you from pressing enter                           
//======================================================================================

function noReturns(v_field, v_event) {

	if (v_event.keyCode == 13 || v_event.keyCode == 10) {
	  v_event.keyCode = 0;
	  return false;
	}
	
	return true;
}

//======================================================================================
// toUpper: Function to capture keyboard input and convert it to upper case -    
//          Also converts a return to a tab                                      
//======================================================================================

function toUpper(v_field, v_event) {

	var inputChar     = v_event.keyCode;
    var upperCaseChar = String.fromCharCode(inputChar).toUpperCase().charCodeAt(); 
    
	// No matter what key is pressed, convert it to uppercase
	v_event.keyCode = upperCaseChar;

    return true;
}

//======================================================================================
// maskSSN: Function to capture keyboard input and build a mask for a SSN        
// Format  : 123-45-6789                                                         
//======================================================================================

function maskSSN(v_field, v_event) {

    // If the shift key was pressed, then kick out the entry
	if (shiftPressed(v_event))
	  return false;

  	// If the user is using the number keys on top of the keyboard, force them
	// to be converted to the 10-key value
	if (v_event.keyCode > 47 && v_event.keyCode < 58)
	  v_event.keyCode = v_event.keyCode + 48;
	  
	// Check to make sure the key being pressed is either 0 - 9 or
	// is a backspace or an enter key
	if ((v_event.keyCode < 96 || v_event.keyCode > 105) && 
	    (v_event.keyCode != 8) && (v_event.keyCode != 9) &&
		(v_event.keyCode != 13)) {
	  v_event.keyCode = 0;
	  return false;
	 }
	 
	// If the key pressed was enter then convert it to a tab and return
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	  return true;
	}
	
	var fieldValue = v_field.value;

	// If the key is a backspace, then handle deleting - including the
	// '-'
	if (v_event.keyCode == 8) {
	  if (v_field.value.length == 4 || v_field.value.length == 7) {
	    v_field.value = fieldValue.substring(0, v_field.value.length - 1);
	  }
	  return true;
	}

	// If we are at a '-' position, then insert it for the user
	if (v_field.value.length == 3 || v_field.value.length == 6) {
	  v_field.value = fieldValue + '-';
	}
	else if (v_field.value.length == 4 || v_field.value.length == 7) {
      if (v_field.value.substring(v_field.value.length - 1) != '-')
	    v_field.value = fieldValue + '-';	
	}

    return true;
}

//======================================================================================
// maskSSN_NoDash: Function to capture keyboard input and build a mask for a     
//                 SSN, but with no dashes allowed                               
// Format  : 123-45-6789                                                         
//======================================================================================

function maskSSN_NoDash(v_field, v_event) {

    // If the shift key was pressed, then kick out the entry
	if (shiftPressed(v_event))
	  return false;

  	// If the user is using the number keys on top of the keyboard, force them
	// to be converted to the 10-key value
	if (v_event.keyCode > 47 && v_event.keyCode < 58)
	  v_event.keyCode = v_event.keyCode + 48;
	  
	// Check to make sure the key being pressed is either 0 - 9 or
	// is a backspace or an enter key
	if ((v_event.keyCode < 96 || v_event.keyCode > 105) && 
	    (v_event.keyCode != 8) && (v_event.keyCode != 9) &&
		(v_event.keyCode != 13)) {
	  v_event.keyCode = 0;
	  return false;
	 }
	 
	// If the key pressed was enter then convert it to a tab and return
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	  return true;
	}
	
	var fieldValue = v_field.value;

	// If the key is a backspace, then handle deleting - including the
	// '-'
	//if (v_event.keyCode == 8) {
	//  if (v_field.value.length == 4 || v_field.value.length == 7) {
	//    v_field.value = fieldValue.substring(0, v_field.value.length - 1);
	//  }
	//  return true;
	//}

	// If we are at a '-' position, then insert it for the user
	//if (v_field.value.length == 3 || v_field.value.length == 6) {
	//  v_field.value = fieldValue + '-';
	//}
	//else if (v_field.value.length == 4 || v_field.value.length == 7) {
        //if (v_field.value.substring(v_field.value.length - 1) != '-')
	//    v_field.value = fieldValue + '-';	
	//}

    return true;
}

//======================================================================================
// maskDate: Function to capture keyboard input and build a mask for a Date      
// Format  : mm/dd/yyyy                                                          
//======================================================================================

function maskDate(v_field, v_event) {

    if (shiftPressed(v_event))
	  return false;

  	if (v_event.keyCode > 47 && v_event.keyCode < 58)
	  v_event.keyCode = v_event.keyCode + 48;
	
	if ((v_event.keyCode < 96 || v_event.keyCode > 105) && 
	    (v_event.keyCode != 8) && (v_event.keyCode != 9) &&
		(v_event.keyCode != 13)) {
	  v_event.keyCode = 0;
	  return false;
	 }
	 
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	  return true;
	}
	
	var fieldValue = v_field.value;

	if (v_event.keyCode == 8) {
	  if (v_field.value.length == 3 || v_field.value.length == 6) {
	    v_field.value = fieldValue.substring(0, v_field.value.length - 1);
	  }
	  return true;
	}

	if (v_field.value.length == 2 || v_field.value.length == 5) {
	  v_field.value = fieldValue + '/';
	}
	else if (v_field.value.length == 3 || v_field.value.length == 6) {
      if (v_field.value.substring(v_field.value.length - 1) != '/')
	    v_field.value = fieldValue + '/';	
	}

    return true;
}

//======================================================================================
// maskPhone: Function to capture keyboard input and build a mask for a phone #  
// Format  : (352)555-5555                                                       
//======================================================================================

function maskPhone(v_field, v_event) {

    if (shiftPressed(v_event))
	  return false;

	if (v_event.keyCode > 47 && v_event.keyCode < 58)
	  v_event.keyCode = v_event.keyCode + 48;
	
	if ((v_event.keyCode < 96 || v_event.keyCode > 105) && 
	    (v_event.keyCode != 8) && (v_event.keyCode != 9) &&
		(v_event.keyCode != 13)) {
	  v_event.keyCode = 0;
	  return false;
	 }
	 
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	  return true;
	}
	
	var fieldValue = v_field.value;

	if (v_event.keyCode == 8) {
	  if (v_field.value.length == 2 || v_field.value.length == 6 || 
	      v_field.value.length == 10) {
	    v_field.value = fieldValue.substring(0, v_field.value.length - 1);
	  }
	  return true;
	}

	if (v_field.value.length == 0) {
	  v_field.value = fieldValue + '(';
	}
	else if (v_field.value.length == 4) {
	  v_field.value = fieldValue + ')';
	 }
	else if (v_field.value.length == 8) {
      if (v_field.value.substring(v_field.value.length - 1) != '-')
	    v_field.value = fieldValue + '-';	
	}

    return true;
}

//======================================================================================
// maskZip : Function to capture keyboard input and build a mask for a zip code  
// Format  : 34461-0000                                                          
//======================================================================================

function maskZip(v_field, v_event) {

    if (shiftPressed(v_event))
	  return false;

	if (v_event.keyCode > 47 && v_event.keyCode < 58)
	  v_event.keyCode = v_event.keyCode + 48;
	
	if ((v_event.keyCode < 96 || v_event.keyCode > 105) && 
	    (v_event.keyCode != 8) && (v_event.keyCode != 9) &&
		(v_event.keyCode != 13)) {
	  v_event.keyCode = 0;
	  return false;
	 }
	 
	if (v_event.keyCode == 13) {
	  v_event.keyCode = 9;
	  return true;
	}
	
	var fieldValue = v_field.value;

	if (v_event.keyCode == 8) {
	  if (v_field.value.length == 6) {
	    v_field.value = fieldValue.substring(0, v_field.value.length - 1);
	  }
	  return true;
	}

	if (v_field.value.length == 5) {
	  v_field.value = fieldValue + '-';
	}

    return true;
}

//======================================================================================
// centerWindow: Function to center a window 'popup' after a resize              
//======================================================================================

function centerWindow(v_popUpWin, v_width, v_height) {
  
	// Find the popup windows position on the screen - centered in the window
  	var winTop          = ((screen.height - 44) - v_height) / 2;
  	var winLeft         = (screen.width - v_width) / 2;
	
	v_popUpWin.moveTo(winLeft, winTop);
  
}

//======================================================================================
// popUp: Function to make a window 'popup'                                      
//======================================================================================

function popUp(v_url, v_win_name, v_width, v_height, v_scroll, v_resize) {
  
	// Find the popup windows position on the screen - centered in the window
  	var winTop          = ((screen.height - 44) - v_height) / 2;
  	var winLeft         = (screen.width - v_width) / 2;
	var expandWidth     = 1;
	var expandHeight    = 1;
	var horizontalSpeed = 10;
	var verticalSpeed   = 5;
  
  	// Set the popup windows options
  	var popUpOptions = "scrollbars=" + v_scroll + 
    	               ",resizable=" + v_resize + 
		  		  	   ",top=" + winTop +
					   ",left=" + winLeft +
					   ",width=" + v_width +
					   ",height=" + v_height;
  
  	// Create the window
  	popUpWin = window.open(v_url, v_win_name, popUpOptions);
		  
  	// If the window exists, then just make it the focus
  	if (parseInt(navigator.appVersion) >= 4) { 
      popUpWin.window.focus(); 
  	}
	
	// Create the effect of the window dropping down like a menu
	//for (expandWidth = 1; expandWidth <= v_width; expandWidth += horizontalSpeed) {
    // 	popUpWin.resizeTo(expandWidth, expandHeight);
	//}
	//for (expandHeight = 1; expandHeight <= v_height; expandHeight += verticalSpeed) {
    //	popUpWin.resizeTo(expandWidth, expandHeight);
	//}
				
	return popUpWin;
}

//======================================================================================
// getSelection: Function to get a listbox's selected item                       
//======================================================================================

function getSelection(v_list) {
 
	return (v_list.options[v_list.selectedIndex].value);	

}

