//======================================================================================
// File: cross_browser.js
// Author: Jim Faulkner
// Copyright (c) 2003 Citrus County Board of County Commissioners
//======================================================================================

//======================================================================================
// getElement: Function to get an object by it's name
//======================================================================================

function getElement(elementID) {

  var el;

  if (document.all) {
    // IE4+
    el = document.all[elementID];
  }
  else if (document.layers) {
    // NS4+
    el = document.layers[elementID];
  }
  else if (document.getElementById) {
    // IE5+ and NS6+
    el = document.getElementById(elementID);
  }

  return(el);
}

//======================================================================================
// getSelection: Function to get the selected value from a listbox
//======================================================================================

function getSelection(listBox) {
  
  if (listBox != null) 
    return (listBox.options[listBox.selectedIndex].value);
  else return('');

}

//======================================================================================
// getKeyPressed: Function to return the numeric value of the key pressed
//======================================================================================

function getKeyPressed(v_event) {

  if (!v_event)
      v_event = window.event;
  
  if (v_event.keyCode) 
    return (v_event.keyCode);
  else if (v_event.which)
    return (v_event.which);

  return (-1);

}

//======================================================================================
// setKeyCode: Function to set/override the key presses
//======================================================================================

function setKeyCode(v_event, code) {

  if (!v_event)
      v_event = window.event;
  
  if (v_event.keyCode) 
    v_event.keyCode = code;
  else if (v_event.which)
    v_event.which = code;

}

//======================================================================================
// leftTrim: Function to "left trim" spaces
//======================================================================================

function leftTrim(str) {

 for (var i = 0; str.charAt(i) <= " "; i++);

 return (str.substring(i, str.length));
}

//======================================================================================
// rightTrim: Function to "right trim" spaces
//======================================================================================

function rightTrim(str) {

 for (var i = (str.length - 1); str.charAt(i) <= " "; i--);

 return (str.substring(0, i + 1));
}

//======================================================================================
// trim: Function to "trim" all spaces
//======================================================================================

function trim(str) {

 return (leftTrim(rightTrim(str)));
}


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

function setStatus(msg) {

  window.status = msg;
  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);
}

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

function toUpper(v_field) {

  v_field.value = v_field.value.toUpperCase();

  return true;
}

//======================================================================================
// isLeapYear: Function to determine if a year is a leap year
//======================================================================================

function isLeapYear(v_year) {

  if (((v_year % 4 == 0) && (v_year % 100 != 0)) || (v_year % 400 == 0))
    return (true);
  else return (false);

}

//======================================================================================
// isDateValid: Function to determine if a date is valid (mm/dd/yyyy)
//======================================================================================

function isDateValid(v_field) {

  var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var day, month, year;

  if (v_field.value.length != 10)
    return (false);

  if ( (v_field.value.charAt(2) != '/') && (v_field.value.charAt(5) != '/') )
    return (false);

  month = parseInt(v_field.value.substring(0, 2), 10);

  if (month == NaN)
    return (false);
  else if ( (month < 1) || (month > 12) )
    return (false);

  day = parseInt(v_field.value.substring(3, 5), 10);

  if (day == NaN)
    return (false);

  year = parseInt(v_field.value.substring(6, 10), 10);

  if (year == NaN)
    return (false);

  if (day < 1)
    return (false);

  if (day > daysInMonth[month - 1] && !isLeapYear(year))
    return (false);

  if ( (month == 2) && (isLeapYear(year)) && (day > (daysInMonth[month - 1] + 1)) )
      return (false);

  return (true);  

}

//======================================================================================
// limitLength: Function to limit the length of the input (for textareas)
//======================================================================================

function limitLength(v_field, v_event, v_maxLength) {

  var code = getKeyPressed(v_event);

  if ( (v_field.value.length >= v_maxLength) && (code != 8) && ( code != 9) && 
       (code != 10) && (code != 13) ) 
    return false;

 	
  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) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);

  if (v_field.value.length >= 11 && code != 8)
    return (false); 

  if ((code < 48 || code > 58) && (code != 8) && (code != 9) && (code != 13)) 
    return (false);
	 
  if (code == 9 || code == 13)
    return (true);
	
  var fieldValue = v_field.value;

  if (code == 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 (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) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);

  if (v_field.value.length >= 10 && code != 8)
    return (false); 

  if ((code < 47 || code > 58) && (code != 8) && (code != 9) && (code != 13)) 
    return (false);
	 
  if (code == 9 || code == 13)
    return (true);
	
  var fieldValue = v_field.value;

  if (code == 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 Number
// Format  : (352)555-1212
//======================================================================================

function maskPhone(v_field, v_event) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);

  if (v_field.value.length >= 13 && code != 8)
    return (false); 

  if ((code < 48 || code > 58) && (code != 8) && (code != 9) && (code != 13)) 
    return (false);
	 
  if (code == 9 || code == 13)
    return (true);
	
  var fieldValue = v_field.value;

  if (code == 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  : 12345-0000
//======================================================================================

function maskZip(v_field, v_event) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);

  if (v_field.value.length >= 10 && code != 8)
    return (false); 

  if ((code < 48 || code > 58) && (code != 8) && (code != 9) && (code != 13)) 
    return (false);
	 
  if (code == 9 || code == 13)
    return (true);
	
  var fieldValue = v_field.value;

  if (code == 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);
}

//======================================================================================
// maskNumeric: Function to capture keyboard input and build a mask allowing only numbers
// Format  : 1234567890
//======================================================================================

function maskNumeric(v_field, v_event) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);


  if ((code < 48 || code > 58) && (code != 8) && (code != 9) && (code != 13)) 
    return (false);
	 
  if (code == 8 || code == 9 || code == 13)
    return (true);
	

  return (true);
}

//======================================================================================
// maskCurrency: Function to capture keyboard input and build a mask allowing only numbers
//               and a single '.'
// Format  : 1234567890
//======================================================================================

function maskCurrency(v_field, v_event) {

  var code = getKeyPressed(v_event);
  
  if (shiftPressed(v_event) && code != 9)
    return (false);

  if (code == 9 || code == 10 || code == 13)
    return (true);

  if ((code < 48 || code > 58) && (code != 8) && (code != 9) && (code != 13) && (code != 46)) 
    return (false);
	 
  if (code == 8 || code == 9 || code == 13)
    return (true);
	
  var fieldValue = v_field.value;

  if ( (code == 46) && (fieldValue.indexOf(".") != -1))
    return (false);

  return (true);
}

//======================================================================================
// maskNoSpaces: Function to capture keyboard input and convert spaces to '_'
// Format  : N/A                                                          
//======================================================================================

function maskNoSpaces(v_field, v_event) {

  var code = getKeyPressed(v_event);

  if (code == 32) {
    v_field.value = v_field.value + '_';  
	return (false);
  }

  return (true);
}

//======================================================================================
// getFormattedDate: Function to return a formatted date
// Format  : mm/dd/yyyy
//======================================================================================

function getFormattedDate() {
  
  var todaysDate = new Date();
  var thisYear   = todaysDate.getYear();
  var thisDay    = todaysDate.getDate();
  var thisMonth  = todaysDate.getMonth() + 1;
  var dateStr    = "";

  if (thisYear < 1000) 
    thisYear += 1900;

  if (thisMonth < 10)
    dateStr = "0" + thisMonth + "/";
  else dateStr = thisMonth + "/";

  if (thisDay < 10)
    dateStr += "0" + thisDay + "/";
  else dateStr += thisDay + "/";

  dateStr += thisYear;

  return (dateStr);
}

//======================================================================================
// showToolTip: Function to show a tooltip when called  
// **Note: You MUST define the toolTipBox div as follows:
// <div id="toolTipBox" style="position: absolute; width: 0px; height: 0px; 
//  z-index: 500; visibility: hidden;"></div>                         
//======================================================================================

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

  var toolTipBox = getElement('toolTipBox');
  var newX, newY;

  if (!v_event)
    v_event = window.event;
	
  var toolTipHTML = '';
	
  toolTipHTML = '<table bgcolor="#000000" width="150" ' +
                'cellspacing="0" ' + 'cellpadding="1" border="0"><tr><td>' +
                '<table bgcolor="#FFFCCC" ' + 'width="100%" cellspacing="0" ' +
                'cellpadding="1" border="0"><tr><td class=' + v_class + '>' + 
                v_message + '</td></tr></table></td></tr></table>';

  toolTipBox.innerHTML        = toolTipHTML;

  newX = v_event.clientX + v_offsetX;
  newY = v_event.clientY + v_offsetY;

  if (document.body.innerWidth) {
    if ( (newX + 166) > document.body.innerWidth)
      newX = document.body.innerWidth - 166;
  }
  else if (document.body.clientWidth) {
    if ( (newX + 166) > document.body.clientWidth)
      newX = document.body.clientWidth - 166;
  }
  
  if (toolTipBox.style.pixelLeft) {
    toolTipBox.style.pixelLeft  = newX;
    toolTipBox.style.pixelTop   = newY; 
  }
  else {
    toolTipBox.style.left  = newX;
    toolTipBox.style.top   = newY; 
  }
	
  toolTipBox.style.visibility = "visible";

}

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

function hideToolTip() {

  getElement('toolTipBox').style.visibility = "hidden";
}

//======================================================================================
// getURLParameter: Function to retrieve a parameter from the URL in a GET 
//======================================================================================

function getURLParameter(v_parameter) {
	var url_args = location.search.slice(1).split('&');
    var val = '';
    
	for (var i = 0; i < url_args.length; i++) {
        if (url_args[i].slice(0, url_args[i].indexOf('=')) == v_parameter) {
            val = url_args[i].slice(url_args[i].indexOf('=') + 1);
            break;
        }
    }
	
    return (val.length > 0 ? unescape(val).split(',') : '')
}



