// -------------------- library functions
function isIE6() {
  var av = navigator.appVersion;
  var i = av.indexOf('MSIE 6');
  return (i < 50 && i > -1);
}
//
function hideSelects() {
  if (isIE6()) {
    for(var m = 0; m < document.all.tags("SELECT").length; ++m) {
        document.all.tags("SELECT")[m].style.visibility='hidden';
    }
  }
}
//
function showSelects() {
  if (isIE6()) {
    for(var s = 0; s < document.all.tags("SELECT").length; ++s) {
        document.all.tags("SELECT")[s].style.visibility='visible';
    }
  }
}
//
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
//
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
//--------------------------------------
function getBounds(ele) {
  var res = { x:0, y:0, w:0, h:0 }
  if (typeof(ele) == 'string') { var el = document.getElementById(ele); } else { el = ele; }
  if (el) {
    res.x = el.offsetLeft; res.y = el.offsetTop;  res.h = el.offsetHeight;  res.w = el.offsetWidth;
    while((el=el.offsetParent) != null) {
      res.x += el.offsetLeft+(el.clientLeft ? el.clientLeft : 0);
      res.y += el.offsetTop+(el.clientTop ? el.clientTop : 0);
    }
  }
  return res;
}
//--------------------------------------
function xOnPage(x,w,centerIt,ele,fixed) {
  var sw = -1;
  if (self.innerWidth){sw = self.innerWidth;}
  else if (document.documentElement && document.documentElement.clientWidth){sw = document.documentElement.clientWidth;}
  else if (document.body){sw = document.body.clientWidth;}
  var wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : (window.pageXOffset ? window.pageXOffset : 0));
  if (typeof(wmin) != 'number' || wmin < 0) { wmin = 0; }

  if (isIE6()) { fixed = false; }
  if (fixed) { wmin = 0; }

  var sw = sw + wmin;
  if (centerIt) { x = (Math.round((sw - wmin - w) / 2)) + wmin; }
  if ((sw > -1)) {	if (x<0) { x=0; }		if ((x + w) > sw - 10) {	x = sw - w - 10;	} }
		if (x < wmin) { x = wmin; }
		return x;
}
function yOnPage(y,h,centerIt,ele,fixed) { // pass ele to make sure max height is not exceeded
  var sh = -1;
  if (self.innerHeight){sh = self.innerHeight;}
  else if (document.documentElement && document.documentElement.clientHeight){sh = document.documentElement.clientHeight;}
  else if (document.body){sh = document.body.clientHeight;}
  var hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : (window.pageYOffset ? window.pageYOffset : 0));
  if (typeof(hmin) != 'number' || hmin < 0) { hmin = 0; }

  if (isIE6()) { fixed = false; }
  if (fixed) { hmin = 0; }

  var sh = sh + hmin;
  if (centerIt) { y = (Math.round((sh - hmin - h) / 2)) + hmin; }
  if ((sh > -1)) {	if (y<0) { y=0; }		if ((y + h) > sh - 10) {	y = sh - h - 10 ; } }
		if (y < hmin) { y = hmin; }
  if (ele && h > sh) { ele.height = sh; }
		return y;
}
//--------------------------------------
// popup.... are routines to use if you want a mouse listener and optional hotspot.
// use: call popupInit(closefunc, hotid) at the start of your popup show function and pass it your hide/close function.
// hotId is the id of the click-to-close area - pass '' for anywhere on the page or !divid to close anywhere EXCEPT divid.
// If you want to manually close the popup, call popupClose('','myresult') NOT your hide/close function
// 'myresult' will be put in global var popupResult so you can control what to do in the hide/close function
var popupSafeToClose = false;
var popupOMD = null;
var popupDiv = '';
var popupCloseFunc = null;
var popupResult = '';
var popupClosing = false;
var popupPending = null;
var popupClosedTime = 0;
//
function popupInit(closeFunc, hotId) {
  if (popupClosing) { return false;  }
  popupPending = null;
  // tidy up any open popup
  if (popupSafeToClose) { popupRemoveListener(); if (popupCloseFunc) { popupCloseFunc();} popupCloseFunc = null;  popupSafeToClose = false; }
  // prep the listener for the new popup
  popupCloseFunc = closeFunc;  popupDiv = hotId;  popupAttachListener();  hideSelects();  popupSafeToClose = true;
  return true;
}
//
function popupClose(e,closeResult) {
  if (popupSafeToClose) {
    if (popupDiv.match('!')) { var pd = popupDiv.substring(1,100); var inpd = false; } else { var pd = popupDiv; var inpd = true; }
    if (pd == '' || (pd != '' && (e == '' || e == 'timer' || (inpd && popupInDiv(e,pd)) || (!inpd && !popupInDiv(e,pd))))) {
      popupClosing = true;
      popupResult = (closeResult==undefined ? null : closeResult);
//      popupResult = ((closeResult == '') || (closeResult==null) || (closeResult==undefined) ? '' : closeResult);
      popupRemoveListener();
      popupSafeToClose = false;
      if (popupCloseFunc) { popupCloseFunc();}
      popupCloseFunc = null;
      showSelects();
      popupClosing = false;
      popupClosedTime = Date.parse(new Date());

      if (popupPending) { popupPending(); popupPending = null; }
    }
  }
}
function popupJustClosed() {
  var now = Date.parse(new Date());
//  alert(now+' '+popupClosedTime);
  var i = Math.abs(now - popupClosedTime);
//  alert(i);
  popupClosedTime = 0;
  return (i < 300);
}
//
function popupAttachListener() {
   	if (document.layers) {	document.captureEvents(Event.MOUSEDOWN);	}
   	popupOMD = document.onmousedown;
   	if (popupOMD != null) {	document.onmousedown = function(event) { popupOMD(event); popupClose(event); } }
    else { document.onmousedown = function(event) { popupClose(event); } }
}
//
function popupRemoveListener() {
   	if (popupOMD != null) {	document.onmousedown = popupOMD; } else { document.onmousedown = null; }
}
//
function popupInDiv(e, divID) {
  if (!e) var e = window.event; // make sure IE has the event in e
  if (!divID) { divID = popupDiv; }
  if (document.layers) {
			var clickX = e.pageX;
			var clickY = e.pageY;
			var t = document.layers[divID];
			if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) {
				return true;
			}
		}
		else if (e && e.srcElement) {
			var t = e.srcElement;
			while (t && t.parentElement != null) {
				if (t.id==divID) {	return true; }
				t = t.parentElement;
			}
		}
		else if (e && e.originalTarget) {
			var t = e.originalTarget;
			while (t && t.parentNode != null) {
				if (t.id==divID) { return true; }
				t = t.parentNode;
			}
		}
}
// end popup listener
//--------------------------------------
function getCookie( check_name ) {
   var a_all_cookies = document.cookie.split( ';' );
   var a_temp_cookie = '';
   var cookie_name = '';
   var cookie_value = '';
   var b_cookie_found = false; // set boolean t/f default f

   for ( i = 0; i < a_all_cookies.length; i++ )
   {
      a_temp_cookie = a_all_cookies[i].split( '=' );
      cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');  // trim
      if ( cookie_name == check_name )
      {
         b_cookie_found = true;
         if ( a_temp_cookie.length > 1 ) { cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); }
//alert(document.cookie);
         return cookie_value;
         break;
      }
      a_temp_cookie = null;
      cookie_name = '';
   }
   if ( !b_cookie_found ) { return ''; }
}
//--------------------------------------
// expires is in days
function setCookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
  var expires_date = new Date( today.getTime() + (expires) );

  var s = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
  document.cookie = s;
}
//--------------------------------------
function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
     ( ( path ) ? ";path=" + path : "") +
     ( ( domain ) ? ";domain=" + domain : "" ) +
     ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
//----
function cssStyle(el, property) {
//alert('1 '+el.style[property]);
//alert('2 '+(el.currentStyle ? el.currentStyle[property] : 'none'));
//try {
//alert('3 '+window.getComputedStyle(el,'').getPropertyValue(property));
//} catch(err) { alert('3 none'); }
  var res = el.style[property];
  if (!res) {
    if (el.currentStyle && el.currentStyle[property]) {
      res = el.currentStyle[property];
    } else {
      // this method uses 'background-image' not the passed format of 'backgroundImage'
      prop = "";
      for (var i=0; i < property.length; i++) {
        if (property.charAt(i) == property.charAt(i).toUpperCase()) {
          prop += '-' + property.charAt(i).toLowerCase(); }
        else {
          prop += property.charAt(i); }
      }
//      alert(prop);
      res = getComputedStyle(el,'').getPropertyValue(prop);
    }
  }
  if (!res) { res = ''; }
  res = res.replace('px','');
  return res;
}
//------------
// use this to create a dynamic div -- avoiding the IE6 hidden stuff problem
var isModal = true;
var modalCover = null;

function createModalCover( ) {
  var div = document.getElementById('modalCover');
  if (!div) {
    var div = document.createElement('div');
    div.setAttribute('id', 'modalCover');
    div.setAttribute('name', 'modalCover');
    div.style.display = 'none';
    document.body.appendChild(div);
  }
  return div;
}

function documentSize() {
  if (window.innerHeight && window.scrollMaxY) {// Firefox
    var y = window.innerHeight + window.scrollMaxY;
    var x = window.innerWidth + window.scrollMaxX;
//  } else if (document.documentElement.clientHeight) {  // dumb opera return the window size not the document
//    var x = document.documentElement.clientHeight;
//    var y = document.documentElement.clientHeight;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    var y = document.body.scrollHeight;
    var x = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    var y = document.body.offsetHeight;
    var x = document.body.offsetWidth;
  }
  if (y == 0)  y = 5000;
  return { 'width':x, 'height':y };
}

function showModalCover( ) {
  var div = document.getElementById('modalCover');
  if (div) {
    div.style.left = '0px';
    div.style.width = '100%';
    div.style.top = '0px';

    var sh = -1;
    if (self.innerHeight){sh = self.innerHeight;}
    else if (document.documentElement && document.documentElement.clientHeight){sh = document.documentElement.clientHeight;}
    else if (document.body){sh = document.body.clientHeight;}

    var size = documentSize();
    var h = size.height;
    if (h < sh) { h = sh; }
    div.style.height = h+'px';
    div.style.display = 'block';
  }
}
function hideModalCover( ) {
  var div = document.getElementById('modalCover');
  if (div) {
    div.style.display = 'none';
  }
}

function createDynamicPopup(id, modal ) {
  if (modal) { var cover = createModalCover(); }

//  var div = document.getElementById(id);
// 	if (div) { div.parentNode.removeChild(div);

  var div = document.getElementById(id);
  if (!div) {
    var div = document.createElement('div');
    div.setAttribute('id', id);
    div.setAttribute('name', id);
    div.style.display = 'none';
    div.style.zIndex = 10000;
    document.body.appendChild(div);
//    if (modal) { cover.appendChild(div); } else { document.body.appendChild(div); }
  }
  div.style.zIndex = 10000;
  return div;
}
// -------------------- web site functions
function initForecastInfo(modal,titlediv,bodydiv) {
  var id = 'forecastInfo';
  var div = createDynamicPopup(id, modal);
  s = '';
  s += '<div id="forecastInfoIn">';
  s += ' <div id="forecastInfoHead">'+document.getElementById(titlediv).innerHTML+'</div>';
  s += ' <img id="forecastInfoCloseBtn" src="assets/img/closebtn1.jpg" alt="close" title="close" />';
  s += ' <div id="forecastInfoText">';
  s += '   <p>';
  s += document.getElementById(bodydiv).innerHTML
  s += '   </p>';
  s += ' </div>';
  s += ' <div id="forecastInfoFoot">';
  s += '	<a href="close" onClick="popupClose(\'\',\'close\'); return false;" alt="close" title="close">Close</a>';
  s += ' </div>';
  div.innerHTML = s;
  return div;
}
function showForecastInfo(el,titlediv,bodydiv,wid) {
  div = initForecastInfo(!isModal,titlediv,bodydiv);
  popupInit(hideForecastInfo,'forecastInfoCloseBtn');
  pos = getBounds(el);
  div.style.display = 'block';
  if (wid) {
//    div.style.width = wid + 'px';
    document.getElementById('forecastInfoIn').style.width = (wid-14) + 'px';
  }
  var x = xOnPage(pos.x-200,div.offsetWidth);
  var y = yOnPage(pos.y,div.offsetHeight);
  div.style.left = x + 'px';
  div.style.top = y + 'px';
}
function hideForecastInfo() {
  var el = document.getElementById('forecastInfo');
  if (el) { el.style.display = 'none'; }
}
//
function addLoadEvent(func, first) {  // set first to true to add this event at the front of the queue
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
  window.onload = function() {
      if (first) { func(); }
      if (oldonload) { oldonload(); }
      if (!first) { func(); } // an error here usually means you have passed the func with ()
    }
  }
}
//
function checkLock() {
  var el = document.getElementById('pagelock');
  if (el) {
    if (el.value=='LOCKED') {
      document.body.innerHTML = '';
      location = window.location.href;
    }
    else { var xx = window.setTimeout('checkLock2()',1); }
  }
}
function checkLock2() {
  var el = document.getElementById('pagelock');
  if (el) {
//    alert('onloadtimer'+el.value)
    if (el.value=='OK') { el.value = 'LOCKED'; }
    else {
      document.body.innerHTML = '';
      location = window.location.href;
    }
  }
}
//
//addLoadEvent(postLoadImages);
//