//=============================================================================
//     Miscellaneous utility functions.
//=============================================================================

var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all
var ns4=document.layers
var op7 = document.all && ( navigator.userAgent.indexOf("Opera/7.") >= 0 );


var UTIL_POPUP_WINDOW_NOT_CENTERED       = 1;
var UTIL_POPUP_WINDOW_CENTERED_ON_SCREEN = 2;
var UTIL_POPUP_WINDOW_CENTERED_ON_OPENER = 3;

/**
 *  Returns the browser-independent height of the screen.
 *
 *  @return int Height of browser's screen.
 */
function util_getScreenHeight() {

}

/**
 *  Returns the browser-independent width of the screen.
 *
 *  @return int Width of browser's screen.
 */
function util_getScreenWidth() {

}

/**
 *  Returns the browser-independent height ofWindow.
 *
 *  @param  ofWindow Window in question.
 *  @return int      Height ofWindow.
 */
function util_getWindowHeight(ofWindow) {

}

/**
 *  Returns the browser-independent width ofWindow.
 *
 *  @param  ofWindow Window in question.
 *  @return int      Width ofWindow.
 */
function util_getWindowWidth(ofWindow) {

}

/**
 *  Returns the browser-independent top location ofWindow on screen.
 *
 *  @param  ofWindow Window in question.
 *  @return int      Top location ofWindow.
 */
function util_getWindowTop(ofWindow) {

}

/**
 *  Returns the browser-independent left location ofWindow on screen.
 *
 *  @param  ofWindow Window in question.
 *  @return int      Left location ofWindow.
 */
function util_getWindowLeft(ofWindow) {

}

/**
 *  Opens a popup window, containing content from url, named name, of size
 *  width x height, with scrollbars if specified, and resizable if specified.
 *
 *  @param  url            Content to be displayed in popup window.
 *  @param  name           Name (DOM element name) of popup window.
 *  @param  width          Width (in pixels) of popup window.
 *  @param  height         Height (in pixels) of popup window.
 *  @param  showScrollbars Yes if popup is to be scrollable, No otherwise.
 *  @param  allowResize    Yes if popup is to be resizable, No otherwise.
 *  @param  center         One of {UTIL_POPUP_WINDOW_NOT_CENTERED, UTIL_POPUP_WINDOW_CENTERED_ON_SCREEN, UTIL_POPUP_WINDOW_CENTERED_ON_OPENER}.
 *  @return Window         Created popup window.
 */
function util_createPopupWindow(url, name, width, height, showScrollbars, allowResize, center) {

   var result = null;

   var theHeight     = (height         == null) ? "350"                          : height;
   var theWidth      = (width          == null) ? "330"                          : width;
   var useScrollbars = (showScrollbars == null) ? "no"                           : showScrollbars;
   var resizable     = (allowResize    == null) ? "yes"                          : allowResize;
   var centered      = (center         == null) ? UTIL_POPUP_WINDOW_NOT_CENTERED : center;

   switch (centered) {
      case UTIL_POPUP_WINDOW_CENTERED_ON_SCREEN:
         popupTop  = (util_getScreenHeight() / 2) - (theHeight / 2);
         popupLeft = (util_getScreenWidth()  / 2) - (theWidth  / 2);
         break;
      case UTIL_POPUP_WINDOW_CENTERED_ON_OPENER:
         popupTop  = util_getWindowTop(window)  + (util_getWindowHeight(window) / 2) - (theHeight / 2);
         popupLeft = util_getWindowLeft(window) + (util_getWindowWidth(window)  / 2) - (theWidth  / 2);
         break;

      default: //-- Handle as case UTIL_POPUP_WINDOW_NOT_CENTERED:
         popupTop  = 100;
         popupLeft = 100;         
   }
   
   var features =         
        "resizable="  + allowResize + ",scrollbars=" + showScrollbars + 
        ",statusbar=" + false       +
        ",height="    + height      + ",width="      + width          + 
        ",left="      + popupLeft   + ",screenX="    + popupLeft      + 
        ",top="       + popupTop    + ",screenY="    + popupTop;
                  
   result = window.open(url, name, features);

   if (result != null && result.opener != window) {
      result.close();
      result = window.open(url, name, features);
   }

   result.focus();
   return result;
}

/**
 *  Opens a popup window.
 *
 *  @see createPopupWindow
 *
 *  @param  url            Content to be displayed in popup window.
 *  @param  name           Name (DOM element name) of popup window.
 *  @param  width          Width (in pixels) of popup window.
 *  @param  height         Height (in pixels) of popup window.
 *  @param  showScrollbars Yes if popup is to be scrollable, No otherwise.
 *  @param  allowResize    Yes if popup is to be resizable, No otherwise.
 *  @param  center         One of {POPUP_WINDOW_NOT_CENTERED, POPUP_WINDOW_CENTERED_ON_SCREEN, POPUP_WINDOW_CENTERED_ON_OPENER}.
 */
function util_openPopupWindow(url, name, width, height, showScrollbars, allowResize, center) {
   var popup = util_createPopupWindow(url, name, width, height, showScrollbars, allowResize, center);
}

/**
 *  Opens an error dialog window.
 *
 *  @see createPopupWindow
 *
 *  @param  url  Content to be displayed in popup window.
 *  @param  name Name (DOM element name) of popup window.
 */
function util_openErrorDialog(url, name) {
   var popup = util_createPopupWindow(url, name, null, null, "no", "no", UTIL_POPUP_WINDOW_CENTERED_ON_OPENER);
}

/**
 *  Opens a new browser window.  Intended for non-trivial content.
 *
 *  @see createPopupWindow
 *
 *  @param  url  Content to be displayed in popup window.
 *  @param  name Name (DOM element name) of popup window.
 */
function util_openBrowserWindow(url, name) {
   var majorWindow = util_createPopupWindow(url, name, 800, 600, "yes", "no", UTIL_POPUP_WINDOW_NOT_CENTERED);
}

/**
 *  Returns the numeric value of str, or null if error.
 *
 *  @param  str String containing value matching [0-9]+.
 *  @return int Numeric value of str, or null if error.
 */
function util_numericValue(str) {
   if (str == null || str.length == 0) return null;

   var result = parseInt(str);

   if (isNaN(result)) return null;

   return result;
}


/**
 * Creates a button control named name, labelled value,
 * with on click action onClick.
 *
 * @param style      Class (style) to use for this button control.
 * @param name       Name & id of button control.
 * @param value      Value (label) of button control.
 * @param onClick    On click action of button control, or href if isDownload.
 * @param isDownload True if this is a download button, false otherwise.
 */
function util_makeButton(style, name, value, onClick, isDownload, isDisabled) {

   if ((typeof isDownload) != "undefined" && isDownload) {
      document.write("<a class='" + style + "'" +
                     " name='" + name + "'" + " id='" + name + "'" + " href='" + onClick + "'" + 
                     " onmouseover='JavaScript: this.style.backgroundColor = \'#B71A1C\'; this.style.color = \'White\';'" + 
                     " onmouseout= 'JavaScript: this.style.backgroundColor = \'White\';   this.style.color = \'#676767\';'>" + value + "</a>"); 
   } else {
   	  var strDisabled = "";
   	  if((typeof isDisabled) != "undefined" && isDisabled)	
   	  	strDisabled = " DISABLED ";

      document.write('<input class="' + style + '" type="submit"' + strDisabled + 
                     ' name="' + name + '" id="' + name + '" value="' + value + '"' + 
                     (onClick == null || onClick.length == 0 ? '' : ' onclick="' + onClick + '"') +
                     ' onmouseover="JavaScript: this.style.backgroundColor = \'#B71A1C\'; this.style.color = \'White\';"' + 
                     ' onmouseout= "JavaScript: this.style.backgroundColor = \'White\';   this.style.color = \'#676767\';"/>'); 
   }
}

/**
 * Generates a mailto link for the webmaster.
 */
function util_mailtoWebmaster() {

   document.write('<a href="mailto:nav_support@odysseynav.com" border="0" class="link">Webmaster</a>');
}

function clearHideDesc(){
	if (window.delayhide)
		clearTimeout(delayhide)
}

function delayHideDesc(){
	delayhide=setTimeout("hideDesc()",600)
}

function hideDesc(){
	if (window.descObj)
		descObj.thestyle.display = "none";
}

function displayDesc(){
	if (window.descObj)
		descObj.thestyle.display = "";
}

function showDesc(text, left_offset, top_offset){

	clearHideDesc();

	descObj = document.getElementById("popmenu");
	
	descObj.thestyle = descObj.style;

	descObj.innerHTML = text;
	descObj.thestyle.top = top_offset;
	descObj.thestyle.left = left_offset;
	descObj.thestyle.display = "";
	
	return false;	
}

function jsDV_strTrim(str){
	var i = 0;
	var j = str.length - 1;

	trimstr = "";
	if (j < 0) return trimstr;

	flagbegin = true;
	flagend = true;

	while (flagbegin == true){
		if (str.charAt(i) == ' ') i++;
		else flagbegin = false;
	}
	while (flagend == true){
		if (str.charAt(j) == ' ') j--;
		else flagend = false;
	}

	if (j < i) return trimstr;
	else trimstr = str.substring(i, j + 1);

	return trimstr;
}


function validEmail(str, msg){
	var emailErrorMsg = msg;
	
	if(emailErrorMsg == null) emailErrorMsg = "Please enter a valid email address. Email addresses must include the\n@ sign and at least one period. (e.g. friendname@abc.com)";

	if (!isValidEmail(str)){
		if(emailErrorMsg != "nomsg") alert(emailErrorMsg);
		return false;
	}
	return true;
}

function isValidEmail(str){
	var at;
	var i = 0;
	var j = str.length - 1;
	var count = 0;

	at = str.indexOf("@", 0);
	if (at <= 0 || at == j) return false;

	while (i < at){
		if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||
			(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') ||
			(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') ||
			str.charAt(i) == '.' || str.charAt(i) == '&' || 
			str.charAt(i) == '?' || str.charAt(i) == '#' || 
			str.charAt(i) == '$' || str.charAt(i) == '*' || 
			str.charAt(i) == '+' || str.charAt(i) == '!' || 
			str.charAt(i) == '%' || str.charAt(i) == '\'' || 
			str.charAt(i) == '^' || str.charAt(i) == '/' || 
			str.charAt(i) == '_' || str.charAt(i) == '-')
				i++;
		else return false;
	}

	i = at + 1;
	if (str.charAt(i) == '.' || str.charAt(j) == '.') return false;
	while (i <= j){
		if (str.charAt(i) == '.'){
			count++;
			if (str.charAt(i + 1) == '.') return false;
			else i++;
		}

		if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') ||
			(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || str.charAt(i) == '_' || str.charAt(i) == '-')
			i++;
		else return false;
	}
	return (count == 0) ? false : true;
}

function numericCheck(val, type, msg){
	var nr1 = val;
	var typeCheck = type;
	var flag = 0;
	var numberErrorMsg = msg;

	if(val == "") return false;
	if (numberErrorMsg == null) numberErrorMsg = "This entry must be a number.  Please remove all letters, special characters, and spaces.";

	switch(typeCheck){
		case 0: //int
			cmp = "0123456789";
			break;
		case 1: //int + commas
			cmp="0123456789,";
			break;
		case 2: //float
			cmp = "0123456789.,";
			break;
		case 3: //currency
			cmp = "0123456789.,$-";
			break;
		case 4: //int + point
			cmp = "0123456789.";
			break;
		case 5: //for zip codes  
			cmp = "0123456789-";
			break;
		default:
			cmp = "0123456789";
			break;
	}

	for (var i=0; i<nr1.length; i++){
		tst = nr1.substring(i,i+1);
		if ((cmp.indexOf(tst)<0) || (cmp.indexOf(" ") != -1)) flag++;
	}
	if (flag != 0){
		if(numberErrorMsg != "nomsg") alert(numberErrorMsg);
		return false;
	}
	return true;
}

function daysInFebruary (year){
	return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day){
	var daysInMonth = new Array(12);
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;

	if (month <1 || month > 12) return false;
	if (day <1 || day > 31) return false;
	var intYear = parseInt(year, 10);
	var intMonth = parseInt(month, 10);
	var intDay = parseInt(day, 10);
	if (intDay > daysInMonth[intMonth]) return false; 
	if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
	return true;
}

function validDate(val, textBox, msg, msg2){
	var dateErrorMsg = msg;
	var spaceErrorMsg = msg2;
	var indate = val;
	var flag = 0;

	if (indate == "") return false;
	if (dateErrorMsg == null) dateErrorMsg = "You have entered an invalid date or date format.  Please use the MM/DD/YYYY format without spaces.";
	if (spaceErrorMsg == null) spaceErrorMsg = "Please use the MM/DD/YYYY format without spaces.";

	if (indate.indexOf(" ")!=-1){
		if(dateErrorMsg != "nomsg") alert(spaceErrorMsg);
		return false;
	}
	if (indate.indexOf("-")!=-1){
		var delimeter = "-";
	}
	else if (indate.indexOf("/")!=-1){
		var delimeter = "/";
	}
	else if (indate.indexOf(".")!=-1){
		var delimeter = ".";
	}
	else {
		flag++;
	}
	var dateArray = indate.split(delimeter);
	if((dateArray.length != 3) || ((dateArray[2].length != 2) && (dateArray[2].length != 4)) ||
		(dateArray[0].length < 1) || (dateArray[0].length > 2) || (dateArray[1].length < 1) ||
		(dateArray[1].length > 2)){
			flag++;
	}
	else if ((numericCheck(dateArray[0], 0, dateErrorMsg)==false) || (numericCheck(dateArray[1], 0, dateErrorMsg)==false) || (numericCheck(dateArray[2], 0, dateErrorMsg)==false)){
		return false;
	}
	var intYear = parseInt(dateArray[2], 10);
	if ((intYear >= 0) & (intYear <= 29)){
		dateArray[2] = 2000 + intYear;
	}
	else if ((intYear >= 30) & (intYear <= 99)){
		dateArray[2] = 1900 + intYear;
	}
	if (isDate(dateArray[2], dateArray[0], dateArray[1])==false){
		flag++;
	}
	if(flag != 0){
		if(dateErrorMsg != "nomsg") alert(dateErrorMsg);
		return false;
	}
	if ((dateArray[2] > 99) & (dateArray[2] < 1753)){
		if(dateErrorMsg != "nomsg") alert("We do not support dates before 1753.  Please choose a later year and try again.");
		return false;
	}
	indate = dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2];
	textBox.value = indate;  // Set the date in the form to the modified date.
	return true;
}
