/*******************************************************************************
* library_ger.js - General JavaScript Library for moonedge.icc (English)
*
* $Source: H:\\CVS\\cvsrepo/Moonedge\040Javascripts/library_eng.js,v $
* $Revision: 1.11 $
* $Date: 2007/09/21 10:02:54 $
* $Author: Markus $
*
* Copyright (C) 2004 Moonedge E-Business GmbH, http://www.moonedge.ch
*******************************************************************************/
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');

// Browser Detection
try {
  isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
  NS4 = (document.layers) ? true : false;
  IEmac = ((document.all)&&(isMac)) ? true : false;
  IE4plus = (document.all) ? true : false;
  IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
  IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
  IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
  IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? true : false;
  ver4 = (NS4 || IE4plus) ? true : false;
  NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
  IE5plus = IE5 || IE6;
  IEMajor = 0;
  
  if (IE4plus) {
    var start = navigator.appVersion.indexOf("MSIE");
    var end = navigator.appVersion.indexOf(".",start);
    IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
    IE5plus = (IEMajor>=5) ? true : false;
  }
} catch(e) {
  IEmac = false;
}  
isIE = IE4 || IE5 || IE6 ||  IE7 || IEmac

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f) {
	if (IEmac && IE4) { // IE 4.5 blows out on testing window.onload
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	} else if (window.onload) {
		if (window.onload != SafeOnload) {
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	} else
		window.onload = f;
}
function SafeOnload() {
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}

// Set field focus to a specified field
var fldFocus;
function SetFocus() {
  if (location.hash == '' && fldFocus != null) {
    switch (fldFocus.type) {
      case "select-multiple":
      case "select-one":
      case "select":
        fldFocus.focus();
        break;
      case "text":
      case "textarea":
      case "password":
      case "file":
        fldFocus.focus();
        fldFocus.select();
        break;
      default:
        break;
    }
  }
}

// Confirm a command
function ConfirmCommand(txt,ctx) {
  if (typeof(ctx) != 'undefined') {
    if (ctx != '') {
      return (confirm(ctx+'\n\nDo you really want to execute the command "'+txt+'"?'))
    } else {
      return (confirm('Do you really want to execute the command "'+txt+'"?'))
    }
  } else {
    return (confirm('Do you really want to execute the command "'+txt+'"?'))
  }
}

// Execute a command
function ExecuteCommand(fld,win) {
  if ((fld.options[fld.selectedIndex].value != '') && (fld.options[fld.selectedIndex].value != 'SELECT')) {
    if (fld.options[fld.selectedIndex].value.charAt(0) == "?") {
      if (confirm('Do you really want to execute the command "'+fld.options[fld.selectedIndex].text+'"?')) {
        win.location.href = fld.options[fld.selectedIndex].value.slice(1)
      }
    } else {
      if (fld.options[fld.selectedIndex].value != 'SELECT') {
        win.location.href = fld.options[fld.selectedIndex].value
      }  
    }
  }
}

// go to a given URL
function GotoURL(fld,win) {
  if ((fld.options[fld.selectedIndex].value != '') && (fld.options[fld.selectedIndex].value != 'SELECT')) {
    if (fld.options[fld.selectedIndex].value != 'SELECT') {
      win.location.href = fld.options[fld.selectedIndex].value
    }  
  }
}

// Function for field validating
function CheckNumber(fld,decpoint,minval,maxval,fldname)
{
  var checkOK = "0123456789-";
  var checkStr = fld.value;
  var allValid = true;
  var allNum = "";

  if (decpoint) {
    checkOK += ".,";
  }

  for (i=0; i<checkStr.length; i++) {
    ch = checkStr.charAt(i);
    for (j=0; j<checkOK.length; j++)
      if (ch==checkOK.charAt(j))
        break;
    if (j==checkOK.length) {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid) {
    if (!decpoint)
      alert("Only numbers are allowed in field '"+fldname+"'.");
    else
      alert("Only numbers and a decimal point are allowed in field '"+fldname+"'.");
    return (false);
  }

  var chkVal = allNum;
  if (chkVal != "") {
    var prsVal = parseFloat(allNum);
    if (minval != "") {
      var prsMin = parseFloat(minval)
      if (prsVal < prsMin) {
        alert("Value must be greater or equel to "+minval+" in field '"+fldname+"'.");
        return (false);
      }
    }
    if (maxval != "") {
      var prsMax = parseFloat(maxval)
      if (prsVal > prsMax) {
        alert("Value must be smaller or equal to "+maxval+" in field '"+fldname+"'.");
        return (false);
      }
    }
  }

  return (true);
}

function CheckLength(fld,minval,maxval,fldname)
{
  var checkStr = fld.value;
  if (minval != "") {
    var prsMin = parseFloat(minval)
    if ((checkStr.length == 0) & (prsMin > 0)) {
      alert("Enter a value in field '"+fldname+"'.");
      return (false);
    }
    if (checkStr.length < prsMin) {
      alert("Value must be longer or equal to "+minval+" characters in field '"+fldname+"'.");
      return (false);
    }
  }
  if (maxval != "") {
    var prsMax = parseFloat(maxval)
    if (checkStr.length > prsMax) {
      alert("Value must be shorter or equal to "+maxval+" characters in field '"+fldname+"'.");
      return (false);
    }
  }

  return (true);
}

function MenuExec(url,win,w,h,txt,ctx) {
  if (txt != "") {
    if (ConfirmCommand(txt,ctx) == false) { return(false); }
  }
  if (url != 'SELECT') {
    if (w != 0 || h != 0) { // its a dialog
      ShowPopup(url,win,w,h);
    } else {
      if (win != '') { // use another window
        window.open(url,win)
      } else {
        window.location.href = url
      }  
    }
  }  
  return(true);
}

function getCenter(w,h) {
  dx = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
  dy = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  ih = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
  iw = (window.innerWidth) ? window.innerWidth : document.body.clientWidth;
  return { x: (iw-w)/2-dx, y: (ih-h)/2+dy }
}

// show a popup window. the window is centered
function ShowPopup(url,win,w,h) {
  winleft=(screen.availWidth-w)/2; wintop=(screen.availHeight-h)/2;
  window.open(url,win,'menubar=0,width='+w+',height='+h+',screenX='+winleft+',screenY='+wintop+',left='+winleft+',top='+wintop+'');
}

// show a calendar popup window
var fldPopupCalendar;
function PopupCalendar(fld) { 
  var now = new Date(); now.setDate(now.getDate()-1)
  var cal = new CalendarPopup();
  cal.offsetX = 0;
  cal.offsetY = 25;
  cal.setMonthNames('January','February','March','April','May','June','July','August','September','October','November','December');
  cal.setMonthAbbreviations('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  cal.setDayHeaders('Su','Mo','Tu','We','Th','Fr','Sa');
  cal.setWeekStartDay(1);
  cal.setTodayText("Today");
  cal.showNavigationDropdowns();
  cal.setReturnFunction("returnPopupCalendar");
//  cal.showYearNavigation();
  cal.select(fld,fld.name,'MM/dd/yyyy');
  fldPopupCalendar = fld;
}
function returnPopupCalendar(y,m,d) {
  var dat = new Date(y,m-1,d); 
  fldPopupCalendar.value=formatDate(dat,"MM/dd/yyyy"); fldPopupCalendar.focus();
}

// show a popup window. the window is centered
function Popup(url,win,w,h) { 
  var pop = new PopupWindow();
  pop.setSize(w,h); 
  off = getCenter(w,h);
  pop.offsetX = off.x;
  pop.offsetY = off.y;
  pop.autoHide();
  pop.setUrl(url);  
  pop.setWindowProperties("scrollbars=1")  
  pop.showPopup("popupanchor")
}

// 
function AutoComplete(fld,sel) {
	var found = false;
	for (var i = 0; i < sel.options.length; i++) {
	  if (sel.options[i].value.toUpperCase().indexOf(fld.value.toUpperCase()) == 0) {
		  found=true; break;
		}
	}
	if (found) { 
	  sel.selectedIndex = i; 
	} else { 
	  sel.selectedIndex = -1; 
	}
	if (fld.createTextRange) {
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = fld.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? sel.options[i].value : oldValue;
			if (newValue != fld.value) {
				fld.value = newValue;
				var rNew = fld.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}

//
function SpamSafe(nam,srv,cls,txt) {
  document.write('<a href="mailto:'+nam+'@'+srv+'" class="'+cls+'">'+((txt) ? txt : nam+'@'+srv)+'</a>')
}

function CheckAll(pat,chk) {
	if (document.getElementsByTagName) {
   	var nodeList = document.getElementsByTagName('input');
	  for (var i = 0; i < nodeList.length; i++) {
      if (nodeList[i].id.indexOf(pat) == 0 && !(nodeList[i].disabled)) { nodeList[i].checked = chk; }
		}
  }
  return false;
}

//------------------------------------------------------------------------------
// cookie functions
//------------------------------------------------------------------------------
if (typeof ICC == 'undefined') { // use new cookie object if ICC is defined
  var Cookie = {
    get : function(nam) {
      var prefix = nam + "=";
      var begin = document.cookie.indexOf(prefix);
      if (begin == -1) return null;
      var end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin + prefix.length, end));
    },
    
    set : function(nam,val,exp,pat,dom,sec) {
      document.cookie = nam + "=" + escape(val) +
                        ((exp) ? "; expires=" + exp.toGMTString() : "") +
                        ((pat) ? "; path=" + pat : "") +
                        ((dom) ? "; domain=" + dom : "") +
                        ((sec) ? "; secure" : "");
    },
    
    remove : function(nam,pat,dom) {
      if (Cookie.get(nam)) {
        document.cookie = nam + "=" +
                          ((pat) ? "; path=" + pat : "") +
                          ((dom) ? "; domain=" + dom : "") +
                          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    }  
  }
}

//------------------------------------------------------------------------------
// convert the ASP session cookie into a persistant cookie
//------------------------------------------------------------------------------
if (typeof ICC == 'undefined') { // use new session cookie object if ICC is defined
  var SessionCookie = {
    fix : function(timeout) {
      if (isIE) {
        var cookies = document.cookie.split('; ')
        for (i = 0; i < cookies.length; i++) {
          var arrTmp = cookies[i].split('=')
          if (arrTmp[0].substring(0,12) == 'ASPSESSIONID') {
            var exp = new Date()
            exp.setTime((timeout * 60000) + exp.getTime())
            Cookie.set(arrTmp[0],arrTmp[1],exp)
          }
        }
      }  
    },
    
    remove : function() {
      if (isIE) {
        var cookies = document.cookie.split('; ')
        for (i = 0; i < cookies.length; i++) {
          var arrTmp = cookies[i].split('=')
          if (arrTmp[0].substring(0,12) == 'ASPSESSIONID') {
            Cookie.remove(arrTmp[0])
          }
        }
      }  
    }
  }  
}

//------------------------------------------------------------------------------
// make periodical calls to a page
//------------------------------------------------------------------------------
var WakeUp = {
  url: '',
  started: false,
  start : function(url,iv) {
    if ((typeof(Ajax) != 'undefined') && (!this.started) && (url != '')) {
      this.url = url;
      setInterval('WakeUp.call();',iv);
      this.started = true;
    }  
  },
  
  call : function() {
    if (typeof(Ajax) != 'undefined') {
      new Ajax.Request(this.url);
    }
  }
}  

//------------------------------------------------------------------------------
// toggle the class of an element and store the actual state in a cookie
//------------------------------------------------------------------------------
if (typeof ICC == 'undefined') { // use new toggler object if ICC is defined
  var Toggler = {
    getState : function(id) {
      var c = Cookie.get('classToggler'); if (!c) { return(''); }
      var arr = c.split('|'); var len = arr.length;
      for (var i = 0; i < len; i++) {
        fld = arr[i].split('=');
        if (fld[0] == id) { return(fld[1]); }
      }
      return('');
    },
    setState : function(id,val) {
      var c = Cookie.get('classToggler'); if (!c) { Cookie.set('classToggler',id+'='+val); return; } 
      var arr = c.split('|'); var len = arr.length;
      for (var i = 0; i < len; i++) {
        fld = arr[i].split('=');
        if (fld[0] == id) { 
          arr[i] = id+'='+val; 
          Cookie.set('classToggler',arr.join('|'));
          return;
        }
      }
      arr[arr.length] = id+'='+val;
      Cookie.set('classToggler',arr.join('|'));  
    },
    
    init : function(id,def) {
    	if (document.getElementById) {
  	  	var el = document.getElementById(id);
  		  if (el) {
          var cls = Toggler.getState(id);
    			el.className = (cls != null && cls != '') ? cls : def;
          Toggler.setState(id,el.className)
  		  }
  	  }
  	  return false;
    },
  
    toggle : function(id,on,off) {
    	if (document.getElementById) {
  	  	var el = document.getElementById(id);
  		  if (el) {
  			  el.className = (el.className == on) ? off : on;
          Toggler.setState(id,el.className)
  	  	}
  	  }
  	  return false;
    },
    
    setall : function(typ,pat,on) {
    	if (document.getElementsByTagName) {
  	  	var nodeList = document.getElementsByTagName(typ);
  		  for (var i = 0; i < nodeList.length; i++) {
          if (nodeList[i].id.indexOf(pat) == 0 && Toggler.getState(nodeList[i].id)) {
    			  nodeList[i].className = on;
            Toggler.setState(nodeList[i].id,on)
          }
  		  }
  	  }
  	  return false;
    },
    
    setparents : function(typ,pat,id,on) {
      var par
    	if (document.getElementById) {
  	  	var el = document.getElementById(id);
  		  while (el) {
          if (el.id) {
            if (el.id.indexOf(pat) == 0 && Toggler.getState(el.id)) {
      			  el.className = on;
              Toggler.setState(el.id,on);
            }
          }  
          el = el.parentNode
  		  }
  	  }
  	  return false;
    }  
  } 
}

var DivPopup = {
  init : function(id,x,y) {
  	if (document.getElementById) {
	  	var el = document.getElementById(id);
		  if (el) {
		    el.__Popup = new PopupWindow(id);
        el.__Popup.offsetX = x;
        el.__Popup.offsetY = y;
        el.__Popup.autoHide();
		  }
	  }
	  return false;
  },

  show : function(a,id) {
  	if (document.getElementById) {
	  	var el = document.getElementById(id);
		  if (el) {
		    a.id = "anchor-"+id;
		    a.name = "anchor-"+id;
		    el.__Popup.showPopup(a.id);
		  }
	  }
	  return false;
  }
}
