/*         
Modifications:
	20080506 - ejh - added _lib_expand_shrink_textarea and added documentation to this critical file

Function List:	
	openWin(url,win,options)
	startTimeout()
	resetTimeout()
	clearSelect(obj)
	getSearchArray()
	showAllProps(obj)
	_onError(form_object, input_object, object_value, error_message)
	_checkdollar(object_value)
	Is ()
	imgOn(img)
	imgOff(img)
	trim(strText)
	replace(string,text,by)
	_checkPhone(obj_value, country)
	_checkZip(obj_value, country)
	confirmMessage(message)
	_select_list_value(selectObj, value)
	_lib_expand_shrink_textarea(hidediv, showdiv, textareaid, new_cols, new_rows, new_height, has_fcke_wysiwyg)
	_lib_resize_textarea_controls()		
		
Non-function JS code:		
	var is;
	var isIE3Mac = false;
	if ((navigator.appVersion.indexOf("Mac")!=-1) && (navigator.userAgent.indexOf("MSIE")!=-1) && (parseInt(navigator.appVersion)==3)) { isIE3Mac = true; }
	else { is = new Is(); }
	var browserOK = false;
	if (!isIE3Mac && (is.nav4up || is.ie4up)) { browserOK = true; }
	var currentSelectedImage = "";
	var timeoutHandle;	
	...plus all the userAgent stuff you see immediately below	
*/

var agt            = navigator.userAgent.toLowerCase();
var is_major       = parseInt(navigator.appVersion);
var is_minor       = parseFloat(navigator.appVersion);
var is_nav         = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2        = (is_nav && (is_major == 2));
var is_nav3        = (is_nav && (is_major == 3));
var is_nav4        = (is_nav && (is_major == 4));
var is_nav4up      = (is_nav && (is_major >= 4));
var is_navonly     = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) );
var is_nav6        = (is_nav && (is_major == 5));
var is_nav6up      = (is_nav && (is_major >= 5));
var is_gecko       = (agt.indexOf('gecko') != -1);
var is_ie          = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3         = (is_ie && (is_major < 4));
var is_ie4         = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
var is_ie4up       = (is_ie && (is_major >= 4));
var is_ie5         = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5       = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up       = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up     = (is_ie && !is_ie3 && !is_ie4 && !is_ie5);

/*
   example: openWin('mypage.html','popupwin','width=55,height=55')

   toolbar 		- displays the browser buttons (forward, back, home, print, etc) 
   location 	- displays the field that shows the URL for the window 
   directories 	- displays other web browser directory buttons 
   status 		- displays the browser status bar at the bottom 
   menubar 		- displays the web browser menu bar 
   resizable 	- allows user to change the size of the window 
   scrollbars 	- provides scroll bars if the content is larger than the window size 
   width=XX 	- specifies the width of the window when opened, in pixels 
   height=YY 	- specifies the height of the window when opened, in pixels 
*/
function openWin(url,win,options) {
	var popupwin = window.open(url,win,options);

	if (!popupwin) { alert("The popup window has been blocked."); }
	else { popupwin.focus(); }
}

var timeoutHandle;

function startTimeout() {  // Timeout after 1 hour and close "child" windows
  var winTimeout = 3600000;
  timeoutHandle = setTimeout("window.close()", winTimeout)
}

function resetTimeout() {
  clearTimeout(timeoutHandle);
}

function clearSelect(obj) {
  var objOptions = obj.options;
  for (var i=0; i<objOptions.length; i++)
    objOptions[i].selected = false;
}

function getSearchAsArray() {
  if (is_nav4up || is_ie4up) {
    var results = new Array();
    var input = unescape(location.search.substr(1));
    if (input) {
      var srchArray = input.split("&");
      var tempArray = new Array();
      for (var i=0; i<srchArray.length; i++) {
        tempArray = srchArray[i].split("=");
        results[tempArray[0]] = tempArray[1];
      }
    }
    return results;
  }
}

function showAllProps(obj) {
  var objName = "";
  var result = "";
  var objPropCount = 0;
  for (var i in obj) {
    objPropCount += 1;
  }
  if (objPropCount > 0) {
    objName = obj["name"];
    for (var p in obj) {
      result += objName + "." + p + " = " + obj[p] + "\t";
    }
    alert(result);
  }
}

function _onError(form_object, input_object, object_value, error_message) {
  alert(error_message);
  return false;
}

function _checkdollar(object_value) {
  // Returns true if value is a number or is NULL
  // otherwise returns false  
  if (object_value.length == 0)
    return true;

  // Returns true if value is a number defined as
  // having an optional leading + or -.
  // having at most 1 decimal point.
  // otherwise containing only the characters 0-9.
  var start_format = " .+-0123456789$";
  var number_format = " .0123456789";
  var check_char;
  var decimal = false;
  var trailing_blank = false;
  var digits = false;

  // The first character can be + - .  blank or a digit.
  check_char = start_format.indexOf(object_value.charAt(0))

  // Was it a decimal?
  if (check_char == 1) decimal = true;
  else if (check_char < 1)
    return false;

  // Remaining characters can be only . or a digit, but only one decimal.
  for (var i = 1; i < object_value.length; i++) {
    check_char = number_format.indexOf(object_value.charAt(i))
    if (check_char < 0)
      return false;
    else if (check_char == 1) {
      if (decimal) // Second decimal.
        return false;
      else
        decimal = true;
    }
    else if (check_char == 0) {
      if (decimal || digits) // ignore leading blanks
        trailing_blank = true;
    }
    else if (trailing_blank)
      return false;
    else
      digits = true;
  }

  // All tests passed, so...
  return true;
}

function Is () {   
  var agt=navigator.userAgent.toLowerCase();

  this.major = parseInt(navigator.appVersion);
  this.minor = parseFloat(navigator.appVersion);

  this.nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));
  this.nav4up = (this.nav && (this.major >= 4));
  this.nav5up = (this.nav && (this.major >= 5));

  this.ie = (agt.indexOf("msie") != -1);
  this.ie3 = (this.ie && (this.major < 4));
  this.ie4 = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")==-1) );
  this.ie4up = (this.ie  && (this.major >= 4));
  this.ie5 = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
  this.ie5up = (this.ie  && !this.ie3 && !this.ie4);

  this.aol   = (agt.indexOf("aol") != -1);
  this.aol4up  = (this.aol && this.ie4up);

  this.win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
  this.mac = (agt.indexOf("mac")!=-1);
}

var is;
var isIE3Mac = false;

if ((navigator.appVersion.indexOf("Mac")!=-1) && (navigator.userAgent.indexOf("MSIE")!=-1) && (parseInt(navigator.appVersion)==3)) { isIE3Mac = true; }
else { is = new Is(); }

var browserOK = false;
if (!isIE3Mac && (is.nav4up || is.ie4up)) { browserOK = true; }
var currentSelectedImage = "";

function imgOn(img) {
  if (browserOK) {
    if (img != currentSelectedImage) {
      currentSelectedImage = img;
      var pathString = eval("document." + img + ".src");
      var pathArray = new Array();
      var fileNameArray = new Array();
      var tempString = "";
      pathArray = pathString.split("/");
      fileNameArray = pathArray[pathArray.length-1].split(".");
      for (var i=0; i<pathArray.length-1; i++) { tempString += pathArray[i] + "/"; }
      tempString += fileNameArray[0] + "_dn." + fileNameArray[1];
      eval("document." + img + ".src = '" + tempString + "'");
    }
  }
}

function imgOff(img) {
  if (browserOK) {
    if (img == currentSelectedImage) {
      currentSelectedImage = "";
      var pathString = eval("document." + img + ".src");
      var pathArray = new Array();
      var fileNameArray = new Array();
      var fileNameSubString = "";
      var tempString = "";
      pathArray = pathString.split("/");
      fileNameArray = pathArray[pathArray.length-1].split(".");
      fileNameSubString = fileNameArray[0].substring(0,fileNameArray[0].length-3);
      for (var i=0; i<pathArray.length-1; i++) { tempString += pathArray[i] + "/"; }
      tempString += fileNameSubString + "." + fileNameArray[1];
      eval("document." + img + ".src = '" + tempString + "'");
    }
  }
}

// Added on 04/29/2002
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

// Added on 11/18/2002

function replace(string,text,by) {
// Replaces text with by in string
    var i = string.indexOf(text);
    if (string.length == 0) return string;
    if ((!i) && (text != string.substring(0,text.length))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);

    return newstr;
}

function _checkPhone(obj_value, country) {
  var phone_regex;
  
  //check UK formatting
  if (country == "UK") 
  {
  	phone_regex = /^0\d{3}[\s-]\d{3}[\s-]\d{4}$/;
  
  	if ( obj_value.match(phone_regex) ) { return true; }
  	//alert("UK NO Match");
  	return false;
  }
  else
  {
  	phone_regex = /^(1[\s\-])?(\(\d{3}\)|\d{3})[-\s]?\d{3}[-\s]?\d{4}$/;
  	if ( obj_value.match(phone_regex) ) { return true; }
  	//alert("US NO Match");
  	return false;
  }

}

function _checkZip(obj_value, country) {

  if (country == "UK") 
  {
  	zip_regex = /^[a-zA-Z]{2}\d{2}[\s-]\d[a-zA-Z]{2}$/;
  	
  	if ( obj_value.match(zip_regex) ) { return true; }
  	//alert("UK NO Match");
  	return false;
  }
  else
  {
  	zip_regex = /^\d{5}([-\s]\d{4})?$/;
  	if ( obj_value.match(zip_regex) ) { return true; }
  	//alert("US NO Match");
  	return false;
  }

}

function confirmMessage(message) {
	if (confirm(message)) { return true; } else { return false; }
}

function _select_list_value(selectObj, value) {
	for (var i = 0; i < selectObj.options.length; i++) {
		if (selectObj.options[i].value == value) { selectObj.selectedIndex = i; break; }
	}
}

function _lib_expand_shrink_textarea(hidediv, showdiv, textareaid, new_cols, new_rows, new_height, has_fcke_wysiwyg) {

	// supports normal DIV textarea as well as iFrame used by FCKEditor Wysiwyg HTML toolbar
	HM_IsSafari = ((parseInt(navigator.productSub)>=20020000)&& (navigator.vendor.indexOf("Apple Computer")!=-1));
	

	document.getElementById(hidediv).style.display = 'none'; 
	document.getElementById(showdiv).style.display = 'block'; 

	if (has_fcke_wysiwyg!=true) {
		document.getElementById(textareaid).cols = new_cols; 
		if (hidediv.indexOf("expand")!=-1)
		{	document.getElementById(textareaid).rows = (document.getElementById(textareaid).scrollHeight/13)+ 1;
		}
		else{
			document.getElementById(textareaid).rows = new_rows;
		}
		if(HM_IsSafari == true){
			document.getElementById(textareaid).rows = new_rows;
		}
	}
	if (new_height > 0) document.getElementById(textareaid).style.height=new_height+'px';
}

function _lib_resize_textarea_controls() {
	// loop through all textarea fields and 'open' (resize) them to a height that displays the text they've entered, no more, no less
	// need a bug fix for Safari
	for(var i=0;i<document.getElementsByTagName('textarea').length;i++)
	{	
		var textareaid = document.getElementsByTagName('textarea')[i].name;
		if(document.getElementById(textareaid) != null){
			document.getElementById(textareaid).rows = (document.getElementById(textareaid).scrollHeight/13)+ 1;
		}
	}
}


function _addDaystoDate(num_days, formObj, destObj, displayformat) {
// handles US dates by default if displayformat variable is not passed or blank
// can handle UK date if displayformat variable is passed as "dd/mm/yyyy"

	date_initial = formObj.value;
	
	if (displayformat === undefined) {
		displayformat = "mm/dd/yyyy";  //if blank, default format to mm/dd/yyyy
	} else {
		if (displayformat == "") {
			displayformat = "mm/dd/yyyy";  //if blank, default format to mm/dd/yyyy
		}
	}
	
	if (displayformat == "dd/mm/yyyy") {
		var day_initial   = date_initial.substring(0,2);
		var month_initial = date_initial.substring(3,5);
	} else {
		var day_initial   = date_initial.substring(3,5);
		var month_initial = date_initial.substring(0,2);
	}
	var year_initial  = date_initial.substring(6,10);	
	var month_current, day_current , year_current;
	
	if(num_days == ""){
		num_days = 1;
	}

	//convert string fields into ints
	month_initial = parseInt(month_initial,10);
	day_initial   = parseInt(day_initial,10);
	year_initial  = parseInt(year_initial,10);	
	num_days = parseInt(num_days);
	
	//alert("Month= " + month_initial + ":: Day= " + day_initial + ":: Year= " + year_initial);
	
	month_current = month_initial;
	day_current = day_initial;
	year_current = year_initial;
	
	var num_days_remaining = num_days;
	var month_change = true;
	var days_in_month;
	
	while (month_change == true) {
	
	//alert ("day_current = " + day_current + "num_days_remaining = " + num_days_remaining);
		day_current = day_current + num_days_remaining;
		month_change = false;
		year_change = false;
		
		
		//check for month change (Sept, April, June, Nov)
		if ( ((month_current == 9) || (month_current == 4) || (month_current == 6) || (month_current == 11)) && (day_current > 30)) {
			month_change = true; //alert("MONTH CHANGE" + month_initial);
			days_in_month = 30;
		}
		//check for month change (Feb) 
		if (month_current == 2)
		{
			//check for leap year conditions
			var leap_year;
			if ( (year_current % 4 == 0) && (( year_current % 100 != 0) || (year_current % 400 == 0))){ leap_year = true; }
			else { leap_year = false; }
			
			if ( ((leap_year == true) && (day_current > 29)) || ((leap_year == false) && (day_current > 28)) ) {
				month_change = true; //alert("MONTH CHANGE FEB");
			}
			
			if (leap_year == true) {days_in_month = 29;}
			else {days_in_month = 28;}		
		}
		//is a month with 31 days contained
		if ((month_current != 9) && (month_current != 4) && (month_current != 6) && (month_current != 11) && (month_current != 2) && (day_current > 31)) {
				month_change = true; //alert("MONTH CHANGE 31");
				days_in_month = 31;		
		}
	
		//check for year change
		if ((month_change == true) && (month_current == 12))
		{
			//num_days_remaining = (days_in_month - day_initial) - num_days_remaining;
			num_days_remaining = (day_current - days_in_month);
						
			year_current = year_current + 1;
			month_current = 1;
			day_current = 0;		
		}
		//increment the month
		else {
			if (day_current > days_in_month) { num_days_remaining = (day_current - days_in_month); }
			else { num_days_remaining = 0; }
			if (month_change == true) {	month_current = month_current + 1; day_current =  0; }
		}
		//alert("loop date: " +  month_current + "/" + day_current + "/" + year_current);
	}			
	
	if (month_current < 10) { month_current = "0" + month_current; }
	if (day_current < 10) { day_current = "0" + day_current; } 	
	
	
	new_date =  day_current + "/" + month_current + "/" + year_current;	
	destObj.value = new_date;
}

