/*
 * ----------------------------------------------------------------------------
 * Copyright 2004 by The Swiss Post, PostFinance - all rights reserved
 * This software is the proprietary information of PostFinance.
 * Use is subject to license terms.
 * ----------------------------------------------------------------------------
 */
// *** browser dedection *** //
function Browser() {
    var b=navigator.appName
    if(b=="Netscape")this.b="ns"
    else if(b=="Microsoft Internet Explorer")this.b="ie"
    else this.b=b
    this.version=navigator.appVersion
    this.v=parseInt(this.version)
    this.vv=parseFloat(this.version);
    this.ns=(this.b=="ns"&&this.v>=4)
    this.ns4=(this.b=="ns"&&this.v==4)
    this.ns5=(this.b=="ns"&&this.v==5)
    this.ie=(this.b=="ie"&&this.v>=4)
    this.ie4=(this.version.indexOf('MSIE 4')>0)
    this.ie5=(this.version.indexOf('MSIE 5')>0)
    this.ie55=(this.version.indexOf('MSIE 5.5')>0)
    this.dom=((document.createRange&&(document.createRange().createContextualFragment))?true:false)
    this.min=(this.ns||this.ie)
    var ua=navigator.userAgent.toLowerCase()
    if(ua.indexOf("win")>-1)this.platform="win32"
    else if(ua.indexOf("mac")>-1)this.platform="mac"
    else this.platform="other"
}
is=new Browser()

// This function checks if the width/height requested fit in the browser window size.
// If not, the width/height is adjusted to the max. size available (monitor resolution)
function wopenFitScreen(inputUrl,popupname,pWidth,pHeight,attributes) {
  if ((screen.availHeight - 31) < pHeight) {
    pHeight = screen.availHeight - 31;
  }
  if ((screen.availWidth - 8) < pWidth) {
    pWidth  = screen.availWidth - 8;
  }
  var screenParams = "width=" + pWidth + ",height=" + pHeight;
  var params = screenParams + ',' + attributes;
  wopen(inputUrl,popupname,params);
}

// *** open a new window with defined params (popup) *** //
function wopen(inputUrl,popupname,attributes){
    var newurl;
    var connector='&';
    var indexOfString=inputUrl.indexOf("?");
    var http= inputUrl.substring(0, 4);
    if(is.platform=='mac'){
        menu=',menubar=yes'
        attributes=attributes+menu}
    if(http=='http'){
      newurl = inputUrl;
    }
    else{
      if(indexOfString==-1){
        connector='?';
      }
      newurl = inputUrl+connector+"popup=true";
    }
    newurl = reArrangeAnchor(newurl);
    win=window.open(newurl,popupname,attributes);
    win.focus();
}

// *** open a new window including a pdf *** //
function openPDF(url)
{
    var title = "popup_gross";
    var menu = "no";
    var scroll = "yes";
    var resize = "yes";

    if (is.platform == "mac") {
      menu = "yes";
    }

    win = window.open(url, title, "width=630,height=490,scrollbars=" + scroll + ",resizable=" + resize + ",menubar=" + menu);
}

// *** go to another window out of a popup and close the popup *** //
function goToUrl(url,close){
    if(close==1)self.close();
    if(url!="")opener.location.href=url;
}

// *** get date *** //
function holeDatum(){
    var datum=new Date();
    var tag=datum.getDate();
    var monat=datum.getMonth();
    var jahr=datum.getYear();
    if(is.ns)jahr+=1900;
    var ausgabe=(""+tag+"."+(monat+1)+"."+jahr+"");
    return ausgabe;
}

// *** send function for formulars *** //
function SendFormOld(pUrl,pName){
    document.forms[pName].action=pUrl;
    document.forms[pName].submit();
}

function SendForm(url, form) {
    if(document.forms[form].method.toLowerCase().match("get") && url.indexOf('?')>=0)
    { // url contains already a query
      // get form content as query string
      srchString = form2ArrayString(document.forms[form]);
      url += "&" + srchString;
      location.href = url;
    }
    else
    { // url does not contain a query or is post
      SendFormOld(url, form);
    }
}

// Convert a passed form reference to a string formatted like
// a JavaScript array of objects
function form2ArrayString(form) {
    var elem;
    var output = "";
    for (var i = 0; i < form.elements.length; i++) {
        elem = form.elements[i];
        if (elem.name) {
            var str = formObj2String(form.elements[i]);
            if(str!=null)
              output += str + "&";
        }
    }
    //output = output.substring(0, output.length-1);
    return output;
}

// Read the name, id, type, and value of one form control element
// as requested by form2ArrayString(element)
function formObj2String(element) {
    var output = element.name + "=";
    //alert(element.name + " " + element.type + " " + element.value);
    switch (element.type) {
        case "radio":
            if(element.checked)
              output += element.value;
            else
              output = null;
            break;
        case "checkbox":
            if(element.checked){
              if(element.value==null)
                output += "true";
              else
                output += element.value;
            }
            else
              output = null;
            //  output += "false";
            break;
        case "select-one":
            output += element.value;
            break;
        case "select-multiple":
            output += element.value;
            break;
        case "text":
            output += escape(element.value);
            break;
        case "textarea":
            output += escape(element.value);
            break;
        case "password":
            output += escape(element.value);
            break;
        case "hidden":
            output += escape(element.value);
            break;
        default:
            output += "";
    }
    return output;
}

// *** send function for formulars including event *** //
function SendFormWithEvent(pUrl,pEvent,pName,pQuestion){
  if(pQuestion!=null && pQuestion!='null'){
    if( window.confirm(pQuestion)==false ){
      return;
    }
  }
  formdata=document.forms[pName];
  if(pEvent!=null){
    // overwrite first input field in formula
    if(pEvent!=null){
    formdata.elements[0].value=pEvent;
    formdata.elements[0].name="wfEvent";
    }
  }
  SendForm(pUrl,pName);
}

// *** like SendForm but out of a div or layer element *** //
function SendFormLayer(pUrl,pName,pLayer){
    formdata=document.forms[pName]
    formdata.action=pUrl;
    formdata.submit();
}

// *** send formular generator formular *** //

function SendFGForm(pFormName,pUrl,pPath,pValue,pQuestion){
    formdata=document.forms[pFormName]
        if(pQuestion!=null && pQuestion!='null'){
          if( window.confirm(pQuestion)==false ){
            return;
          }
        }
        if(pUrl!=null){
      formdata.action=pUrl;
    }
    if (pValue!=null || pPath!=null) {
      elmval = ""
      if(pValue!=null){
        //formdata.elements[0].value=pValue;
        elmval=elmval+pValue;
      }
      elmval=elmval + '|'
      if(pPath!=null){
        //formdata.elements[0].name=pPath;
        elmval=elmval+pPath
      }
      formdata.elements["hiddenControl"].value=elmval
    }

    formdata.submit();
}

function changeColor(objectId,colour){
       if (document.layers)
           window.document.layers[objectId].borderColor = colour;
       else if (document.all)
           window.document.all[objectId].style.borderColor  = colour;
}

// *** print function (needed in popups) *** //
function drucken(){
    if(is.platform=='mac'){
      alert('Your Browser doesn\'t support this function.\n Please press Apple + P to print this page.');
    }
    else {

    if(window.print){
      window.print();
    }
    else {
      alert('Your browser does not support this function. You can either update your browser or press "Ctrl" + "P" to print this page.');
    }
  }
}

function clearField(attribute) {
  attribute.value = "";
}

// *** find next field in form field (special function for FondBasket.jsp) *** //
function getNext(me){
    if(me == null) return null;
    var frm=me.form;
    var i=0;
    var n=frm.elements.length;
    while(i<n){
        if(frm.elements[i]==me){
            if(i+1<n) return frm.elements[i+1];
            break;
        }
        i++;
    }
    return null;
}

// *** find previous field in form field (special function for FondBasket.jsp) *** //
function getPrevious(me){
    if(me == null) return null;
    var frm=me.form;
    var i=0;
    var n=frm.elements.length;
    while(i<n){
        if(frm.elements[i]==me){
            if(i-1>=0) return frm.elements[i-1];
            break;
        }
        i++;
    }
    return null;
}

// *** get next or previous field of me (special function for FondBasket.jsp) *** //
function getField(me,mode){
    if(mode==null || me == null) return null;
    if(mode.toLowerCase()=="previous") return getPrevious(me);
    else if(mode.toLowerCase()=="next") return getNext(me);
    else return null;
}

// *** init next or previous field of me with value (special function for FondBasket.jsp) *** //
function setValueOf(me,mode,val){
    if(me == null || me.value=='') return;
    var fieldToSet=getField(me,mode);
    if(fieldToSet != null) fieldToSet.value=val;
}

/*
 * new popup functions from crealogix
 * developer: ehra [CLX]
 * edited by tel [BSI]
 *
 */

function wopenPopNormGross(url,scr,size){
  var scroll, resize, menu, newurl;
  var connector='&';
  var indexOfString=url.indexOf("?");
  if (is.platform == "win32"){
  menu = "no"
  }
  if (is.platform == "mac"){
  menu = "yes"
  }
  scroll = (scr  == "" || scr == "yes" ) ? "yes" : "no";
  resize = (size == "" || size == "yes") ? "yes" : "no";
  if(indexOfString==-1){
    connector='?';
  }
  newurl = url+connector+"popup=true";
  newurl = reArrangeAnchor(newurl);
  FipoLarge = window.open(newurl,"popup_gross","width=633,height=490,scrollbars="+scroll+",resizable="+resize+",menubar="+menu);
  FipoLarge.focus();
}

function wopenPopNormKlein(url,scr,size){
  var scroll, resize, menu, newurl;
  if (is.platform == "win32"){
  menu = "no"
  }
  if (is.platform == "mac"){
  menu = "yes"
  }
  scroll = (scr  == "" || scr == "yes" ) ? "yes" : "no";
  resize = (size == "" || size == "yes") ? "yes" : "no";
  newurl = url+"&popup=true";
  newurl = reArrangeAnchor(newurl);
  FipoSmall = window.open(newurl,"popup_klein","width=461,height=490,scrollbars="+scroll+",resizable="+resize+",menubar="+menu);
  FipoSmall.focus();
}

// *** puts a parameter with an anchor declaration in the given url at the end of the url. *** //
// match: http://localhost:7001/pf/ref/de/seg/popups/alles_rund_ums_zahlen.na_popups.popup.595.html#onlineset?popup=true
// replace: http://localhost:7001/pf/ref/de/seg/popups/alles_rund_ums_zahlen.na_popups.popup.595.html?popup=true#onlineset
// do not match:
// http://localhost:7001/pf/ref/de/seg/popups/alles_rund_ums_zahlen.na_popups.popup.595.html?popup=true#onlineset
// http://localhost:7001/pf/ref/de/seg/popups/alles_rund_ums_zahlen.na_popups.popup.595.html?popup=true
function reArrangeAnchor(pUrl){
  retUrl = pUrl;
  var re = /^(.+)(#[^?]+)(.+)$/;

  if (re.test(pUrl)) {
    retUrl = pUrl.replace(re, "$1$3$2");
  }
  return retUrl;
}

function writeDateOnly(){

    jetzt = new Date();
    Tag = jetzt.getDate();
    Monat = jetzt.getMonth();
    Jahr = jetzt.getYear();
    var RealMonat = Monat+1;

    AktJahr = (is.ns) ? Jahr + 1900 : Jahr;
    AktTag = ((String(Tag)).length < 2) ? eval("'0'+Tag") :  Tag;
    AktMonat = ((String(RealMonat)).length < 2) ?  eval("'0'+RealMonat") : RealMonat;
    document.write(AktTag+'.'+AktMonat+'.'+AktJahr);
}

// *** ----------- Bilder-Preload und Wechsel fuer Home ----------- *** //

function preload(imgObj,imgSrc){
    eval(imgObj+'= new Image()');
    eval(imgObj+'.src= "' + imgSrc+'"');
}

function changeImage(imgName,imgObj){
    if(is.ns4) eval('document.images["' + imgName + '"].src= '+ imgObj + '.src');
    else document.images[imgName].src= eval(imgObj + ".src");
}

function urlencode(string) {
  return escape(string)
}

// *** Flash management functions -- Begin *** //
  // Browsercheck
  var browser;
  var system;

  function checkBrowser() {
    if (navigator.appName == 'Netscape') {
        if (parseInt(navigator.appVersion) == 4) browser = 'Netscape4';
        if (parseInt(navigator.appVersion) == 5) browser = 'Netscape6';
      }
      else if (navigator.appName == 'Microsoft Internet Explorer') {
        if (navigator.appVersion.indexOf('MSIE 4') > 0) browser = 'MicrosoftInternetExplorer4';
        if (navigator.appVersion.indexOf('MSIE 5') > 0) browser = 'MicrosoftInternetExplorer5';
        if (navigator.appVersion.indexOf('MSIE 6') > 0) browser = 'MicrosoftInternetExplorer6';
      }
      else browser = 'other'
    //
    system = "other";
    if (navigator.appVersion.lastIndexOf('Mac') != -1) system = 'Macintosh';
      if (navigator.appVersion.lastIndexOf('Linux') != -1) system = 'Linux';
    if ((navigator.appVersion.lastIndexOf('OS/2') != -1) || (navigator.appVersion.lastIndexOf('OS2') != -1)) system = 'OS2';
    if ((navigator.appVersion.lastIndexOf('Windows 95') != -1) || (navigator.appVersion.lastIndexOf('Win95') != -1)) system = 'Windows95';
    if ((navigator.appVersion.lastIndexOf('Windows 98') != -1) || (navigator.appVersion.lastIndexOf('Win98') != -1)) system = 'Windows98';
    if ((navigator.appVersion.lastIndexOf('Windows NT') != -1) || (navigator.appVersion.lastIndexOf('WinNT') != -1)) system = 'WindowsNT';
    if ( ((navigator.appVersion.lastIndexOf('Windows NT') != -1) || (navigator.appVersion.lastIndexOf('WinNT') != -1)) && (navigator.appVersion.lastIndexOf('5.0') != -1) ) system = 'Windows2000';
    if ( ((navigator.appVersion.lastIndexOf('Windows NT') != -1) || (navigator.appVersion.lastIndexOf('WinNT') != -1)) && (navigator.appVersion.lastIndexOf('5.1') != -1) ) system = 'WindowsXP';
  }


  // --------------------
  // Popup
  function openFlashPopUp(url,popName){
    var windowWidth = "";
    var windowHeight = "";
    var top = 0;
    var left = 0;
    var toolbar ="0";
    var scrollbars ="0";
    var resizable ="1";
    var location ="0";
    var directories ="0";
    var status="0"
    var menubar ="0";

    checkBrowser();

    if(browser == "MicrosoftInternetExplorer4" || browser == "MicrosoftInternetExplorer5" || browser == "MicrosoftInternetExplorer6") {
      var windowWidth = screen.availWidth - 12;
      var windowHeight = screen.availHeight - 31;
    } else if(browser == "Netscape4") {
      var windowWidth = screen.availWidth - 12;
      var windowHeight = screen.availHeight - 31;
    } else if(browser == "Netscape6") {
      var windowWidth = screen.availWidth - 8;
      var windowHeight = screen.availHeight - 27;
    }
    if(screen.availWidth < 800 || screen.availHeight < 570) {
      var scrollbars = "1";
    }
    if(window.popup){
      if (!(popup.closed)) {
        popup.close();
      }
      openFlashWin(url,popName,windowWidth,windowHeight,top,left,toolbar,scrollbars,resizable,location,directories,status,menubar);
     }
     else{
      openFlashWin(url,popName,windowWidth,windowHeight,top,left,toolbar,scrollbars,resizable,location,directories,status,menubar);
    }
  }

  function openFlashWin(url,popName,windowWidth,windowHeight,top,left,toolbar,scrollbars,resizable,location,directories,status,menubar){
    popup = window.open(url,popName,"toolbar="+toolbar+",location="+location+",directories="+directories+",status="+status+",menubar="+menubar+",scrollbars="+scrollbars+",resizable="+resizable+",width="+windowWidth+",height="+windowHeight+",top="+top+",left="+left);
  }

  // --------------------
  // Resize Windows
  function resizeWin() {
    if (browser == 'Netscape4') {
      var windowWidth = screen.availWidth-12;
      if (system == 'WindowsXP') {
        var windowHeight = screen.availHeight-72;
      } else {
        var windowHeight = screen.availHeight-31;
      }
    } else {
      var windowWidth = screen.availWidth;
      var windowHeight = screen.availHeight;
    }
    if (browser != 'Netscape4') {
      window.resizeTo(windowWidth,windowHeight);
      window.moveTo(0,0);
      window.focus;
    }
  }
  // --------------------
  // Reload Windows
  function reloadWin() {
    if (browser == 'Netscape4') {
      document.location.reload();
    }
  }

// *** Flash management functions -- End *** //

//
// This function is used to open a popup window for the Preisrechner UC from
// static content.
//
function openPreisrechnerPopup(lang) {
  var windowWidth  = 633;
  var windowHeight = 825;

  if ((screen.availHeight - 31) < windowHeight) {
    windowHeight = screen.availHeight - 31;
  }
  if ((screen.availWidth - 8) < windowWidth) {
    windowWidth  = screen.availWidth - 8;
  }
  var screenParams = "width=" + windowWidth + ",height=" + windowHeight;
  var params = screenParams + ",scrollbars=yes,resizable=yes,status=no,top=0";
  var url = 'evtPreisrechner.do';
  if (lang != null && lang.length > 0) {
    url = url + '?nls=' + lang;
  }
  wopen(url,'popup_preisrechner',params);
}

//
// Function called from all Info-Links in Preisrechner UC
// If any parameters change, they are in one place
//
function openPrInfoPopup(url) {
  var scrollbars='yes';
  var resizable='yes';
  wopenPopNormGross(url,scrollbars,resizable);
}

//
// This function is used to open a URL in the FIPO main window from a popup.
//
function openUrlInMain(url) {
  var popupOpener = window.opener;
  popupOpener.location = url;
  popupOpener.focus();
}

//
// This function performs a local reset of the fields. !! The model is not changed !!
// If this reset is not enough, you must implement an event that reset the
// model and generate a new page.
//
function resetForm(formName) {
  var radioName = "";
  for(i=0; i<document.forms[formName].elements.length; i++) {
    element = document.forms[formName].elements[i];
    if (element.type == "text") {
      element.value="";
    }
    else if (element.type == "password") {
      element.value="";
    }
    else if (element.type == "hidden") {
    }
    else if (element.type == "textarea") {
      element.value="";
    }
    else if (element.type == "checkbox") {
      element.checked='';
    }
    else if (element.type == "select-one") {
      element.options[0].selected='true';
    }
    else if (element.type == "radio") {
      if (radioName != element.name) {
        element.checked = true;
        radioName = element.name;
      }
      else {
        element.checked=''
      }
    }
  }
}

// This function open links from a dropdown list in a new window
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  var index = selObj.selectedIndex;
  selObj.selectedIndex=0;
  eval(targ+".location='"+selObj.options[index].value+"'");
}

// This function formats an integer in groups, e.g. -25000 => -25'000, 1000000 => 1'000'000
function formatIntegerInGroups(number) {
  var sign = '';
  if (!isNaN(number)) {
    if (number < 0) {
      sign = '-';
    }
    number = Math.abs(Math.floor(number)).toString();
    for (var i = 0; i < Math.floor((number.length - (1 + i)) / 3); i++) {
      number = number.substring(0, number.length - (4 * i + 3)) + '\'' + number.substring(number.length - (4 * i + 3));
    }
  }
  return sign + number;
}

function setERMSInfos(formName, resolutionField, osField, browserNameField) {
  ourForm = document.forms[formName];
  ourForm.elements[osField].value = navigator.platform;
  ourForm.elements[browserNameField].value = navigator.userAgent
  ourForm.elements[resolutionField].value = screen.width+"*"+screen.height;
}

function setChecked(pForm, pFieldName, pIndex) {
  document.forms[pForm].elements[pFieldName][pIndex].checked=true;
}

function setLocation(url) {
  window.location.href=url
}