// flag for signalling that tag-related validation has not succeeded;
// this is necessary for mozilla browser in order to prevent BeanInfo-defined validation on submit event
var fieldNotValidated = false;

// BOI, followed by one or more WhiteSpace characters, followed by EOI.
var reWhiteSpace = /^\s+$/;


// BOI, followed by one lower or uppercase English letter, followed by EOI.
var reLetter = /^[a-zA-Z]$/;


// BOI, followed by one or more lower or uppercase English letters,
// followed by EOI.
var reAlphabetic = /^[a-zA-Z]+$/;


// BOI, followed by one or more lower or uppercase English letters
// or digits, followed by EOI.
var reAlphanumeric = /^[a-zA-Z0-9]+$/;


// BOI, followed by one digit, followed by EOI.
var reDigit = /^\d/;


// BOI, followed by one lower or uppercase English letter
// or digit, followed by EOI.
var reLetterOrDigit = /^([a-zA-Z]|\d)$/;


// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/;


// BOI, followed by an optional + or -, followed by one or more digits,
// followed by EOI.
var reSignedInteger = /^(\+|-)?\d+$/;


// BOI, followed by one of these two patterns:
// (a) one or more digits, followed by ., followed by zero or more digits
// (b) zero or more digits, followed by ., followed by one or more digits
// ... followed by EOI.
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;


// BOI, followed by an optional + or -, followed by one of these two patterns:
// (a) one or more digits, followed by ., followed by zero or more digits
// (b) zero or more digits, followed by ., followed by one or more digits
// ... followed by EOI.
var reSignedFloat = /^(((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))$/;

// BOI, followed by one or more characters, followed by @,
// followed by one or more characters, followed by .,
// followed by one or more characters, followed by EOI.
var reEmail = /^.+\@.+\..+$/;

// WhiteSpace characters as defined by this sample code
var WhiteSpace = " \t\n\r";


var reIZVCharacters = /^[\w ???????\(\)&\.,-\/+<>?;!"={}:_%*#?|\\ \$\^\[\]\'@]+$/;

var reKKPCharacters = /^[\w ???????\(\)&\.,-\/+<>]+$/;

// -----------------------------------------------------------------
// Ebpp erlaubte Characters
// chars von ascii 32 - 126 (space bis tilde)
var reEbppTextWithSpace = /^[ -~???????]+$/;
// chars von ascii 33 - 126 (rufzeichen bis tilde)
var reEbppTextWithoutSpace = /^[!-~???????]+$/;

// letter numbers, just for userid and password. new with 3.1
var reEbppLetterNumberWithoutSpace = /^[0-9A-Za-z]+$/;
var reEbppLetterNumberWithSpace = /^[ 0-9A-Za-z]+$/;


// ------------------------------------------------------------------
// Check whether string s is empty.
function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

// ------------------------------------------------------------------
// Returns true if string s is empty or
// WhiteSpace characters only.
function isWhiteSpace (s) {   // Is s empty?
    return (isEmpty(s) || reWhiteSpace.test(s));
}

// ------------------------------------------------------------------
// Return true if string s is integer
function isInteger (s, required) {
    if (! required && isWhiteSpace(s)) return true;
    return reInteger.test(s)
}

// ------------------------------------------------------------------
// Return true if string s is alphanumeric
function isAlphanumeric (s, required) {
    if (! required && isWhiteSpace(s)) return true;
    return reAlphanumeric.test(s)
}

// ------------------------------------------------------------------
// Returns true if string s is a phone number
function isPhone (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (isWhiteSpace(s)) return false;
    if (s.length < 10) return false;
    if (s.substring (0, 1) != "+") return false;
    if (! isInteger (s.substring (1), true)) return false;

    return true;
}

// ------------------------------------------------------------------
// Return true if string s is an amount
function isAmount (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    var reAmount = /^-?[\d\.]+(\,\d{0,2})?$/;
    return reAmount.test(s);
}

// ------------------------------------------------------------------
// Return true if string s is a valid year
function isYear (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s > 99 && s < 1000) return false; // 3-stellig
    if (s < 1 || s > 9999) return false;

    return true;
}

// ------------------------------------------------------------------
// erzeugt 4-stelliges Jahr aufgrund der Eingabe
function getYear (s) {
    if (s >= 1 && s <= 99) return 2000 + Number (s);
    return s;
}

// ------------------------------------------------------------------
// Return true if string s is a valid Month
function isMonth (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s < 1 || s > 12) return false;

    return true;
}

// ------------------------------------------------------------------
// Return true if string s is a valid Month
function isDay (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s < 1 || s > 31) return false;

    return true;
}

// ------------------------------------------------------------------
// Return true if s is a positiv number
function isPositivAmount (s, greaterZeroOnly, required) {
        if (! required && isWhiteSpace(s)) return true;

    if (! isAmount(s, true)) return false;

    var numS = getNumberIgnoreComma(s);
    if (numS < 0) return false;
    if (greaterZeroOnly && numS == 0) return false;

    return true;
}

// ------------------------------------------------------------------
function isBLZ(s)
{
    var reBLZ = /^\d{5}$/;
    if (isEmpty(s))
        return true;
    return reBLZ.test(s);
}

// ------------------------------------------------------------------
function isValidCharacter(s)
{
    var reAmount = /^[\w ???????\(\)&\.,-\/+<>]+$/;

    if(isEmpty(s)) return true;
    return reAmount.test(s);
}

// ------------------------------------------------------------------
function isAccount(s)
{
    var reAmount = /^\d+$/;
    var i;

    if (isEmpty(s))
        return true;

    return reAmount.test(s);
}

// ------------------------------------------------------------------
// Return Number (s), extract only 0-9-characters and '-'
function getNumberIgnoreComma (s) {
    var sNew = '';

    var i = 0;
    for (i = 0; i < s.length; i++) {
        if (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '-') {
            sNew = sNew + s.charAt(i);
        }
    }

    if (sNew.length > 0) return parseFloat(sNew);
    return 0;
}

// ------------------------------------------------------------------
function selection_getSelectedValue(myForm, mySelbox) {
    var idx = document.forms[myForm].elements[mySelbox].selectedIndex;
    var value = document.forms[myForm].elements[mySelbox].options[idx].value;

    return value
}

// ------------------------------------------------------------------
function cnvVal(x)
{
  var xx, x_i, y, z;

  // delete all ".", woraround weil x.replace (".", "") im Netscape NICHT funktioniert
  xx = '';
  for (x_i = 0; x_i < x.length; x_i ++) {
      if (x.charAt(x_i) != '.') {
          xx = xx + x.charAt(x_i);
      }
  }

  y = xx.split(",");
  z = Number(y[0] * 100);
  if( y.length > 1 )
  {
    if( y[1].length == 1 ){
      z += Number(y[1] * 10);
    }else if( y[1].length > 1 ) {
      z += Number(y[1].substr(0,2));
     }
  }
  //nbalert( "From:" + x + "\nto:" + z );
  return( z );
}

// ------------------------------------------------------------------
function cnvVal2(x)
{
  return( x.substr( 0, x.length - 2 ) );
}

// ------------------------------------------------------------------
function fmtDat(jjjjmmtt)
{
  return jjjjmmtt.substring(6,8)+'.'+jjjjmmtt.substring(4,6)+'.'+jjjjmmtt.substring(0,4)
}

// ------------------------------------------------------------------
// function onCurrencyChange(cur) {
//    onCurrencyChange(cur, "");
// }

// ------------------------------------------------------------------
function onCurrencyChange(cur, myParamString) {
    if (myParamString == null) myParamString = "";

    var url = window.location.href;
    var curPos = url.indexOf('cur=');

    if (curPos > 0) {
        url = url.substring(0, curPos-1);
    }

    if (url.indexOf('?') > 0) {
        url = url + "&cur=" + cur;
    } else {
        url = url + "?cur=" + cur;
    }

    if (url.indexOf("&ss=") < 0 && url.indexOf("?ss=") < 0) {
        url += "&ss=" + GLOBAL_sessionId;
    }

    if (url.indexOf("&zz=") < 0 && url.indexOf("?zz=") < 0) {
        url += "&zz=" + GLOBAL_zufallsZahl;
    } else {
       var urlpre = url.substr(0, url.indexOf("zz=") + 3);
       var urlpost= url.substr(url.indexOf("&ss="), url.length);

       url = urlpre + GLOBAL_zufallsZahl + urlpost;
    }



    if (myParamString.length > 0) {
        if (myParamString.charAt (0) != '&') {
            myParamString += "&";
        }
    }
    url += myParamString;

    window.location.href = url;
}

// ------------------------------------------------------------------
// Check whether string s is empty.
function isValidIZVCharacter(s) {
    if(isEmpty(s)) return true;
    return reIZVCharacters.test(s);
}

function isValidFileName(s) {
    if(isEmpty(s) || isWhiteSpace(s) || s.length >255)
        return false;
    else
        return true;
}


function isValidFileBezeichnung(s) {
    if(isEmpty(s) || isWhiteSpace(s) || s.length >50)
        return false;
    else
        return true;
}


function isValidEbppCharacterWithSpace(s) {
    return reEbppTextWithSpace.test(s);
}

function isValidEbppCharacterWithoutSpace(s) {
    return reEbppTextWithoutSpace.test(s);
}

function isValidEbppLetterNumberWithoutSpace(s) {
    return reEbppLetterNumberWithoutSpace.test(s);
}

function isValidEbppLetterNumberWithSpace(s) {
    return reEbppLetterNumberWithSpace.test(s);
}

// ---------------------------------------------------------

function validmail(reply){

        if(isWhiteSpace(reply)){
            return false;
        }
        var klammeraffe=reply.indexOf('@');
        var klammeraffelast= reply.lastIndexOf('@');
        var punktlast= reply.lastIndexOf('.');

        if(punktlast >= (reply.length -2)){
            return false;

        }
        if(punktlast==-1){
            return false;
        }
        if(klammeraffe!=klammeraffelast){
            return false;
        }
        if(punktlast< klammeraffe){
            return false;
        }
        if(klammeraffe==-1){
            return false;
        }

        if(klammeraffe==0){
            return false;
        }
        if((reply.length - 1) == klammeraffe){
            return false;
        }
        return true;
    }


function isValidKKPCharacter(s) {
    if(isEmpty(s)) return true;
    return reKKPCharacters.test(s);
}


function getRadioButtonSelectedIdx(rd) {
    if (rd.length==undefined) return rd.checked?0:-1;
    for (var i = 0; i < rd.length; i++)
        if (rd[i].checked)
            return i;
    return -1;
}

function getRadioButtonSelectedValue(myForm, myRadioButton) {
    var rd = document.forms[myForm].elements[myRadioButton];
    var i=getRadioButtonSelectedIdx(rd);
    return i>=0?rd[i].value : "";
}

function setRadioButtonSelectedValue(myForm, myRadioButton, newVal) {
    var rd=document.forms[myForm].elements[myRadioButton];
    for (var i = 0; i < rd.length; i++) {
        if (rd[i].value == newVal) {
            rd[i].checked = true;
            break;
        }
    }
}


// ------------------------------------------------------------------
function getMsgRequiredField(feldName) {
    return (setMessageParams(msgInsertField, feldName));
}

// ------------------------------------------------------------------
function getMsgValueTooShort(feldName, minVal) {
    return (setMessageParams(msgValueTooShort, feldName, minVal));
}

// ------------------------------------------------------------------
function getMsgValueTooLong(feldName, maxVal) {
    return (setMessageParams(msgValueTooLong, feldName, maxVal));
}

// ------------------------------------------------------------------
function getMsgValidRange(feldName, min, max) {
    return (setMessageParams(msgValidRange, feldName, min, max));
}

// ------------------------------------------------------------------
function getMsgInvalidField(feldName) {
    return (setMessageParams(msgInvalidField, feldName));
}

// ------------------------------------------------------------------
function getMsgInvalidCharacters(feldName,validCharacters){
    if(isEmpty(validCharacters)){
        return (setMessageParams(msgInvalidCharacters,feldName));
    }else{
        return (setMessageParams(msgValidCharacters, feldName,validCharacters));
    }
}

// ------------------------------------------------------------------
function getMsgInvalidIZVCharacters(feldName) {
    return (setMessageParams(msgValidIZVCharacters, feldName));
}

// ------------------------------------------------------------------
function getMsgInvalidKKPCharacters(feldName) {
    return (setMessageParams(msgValidKKPCharacters, feldName));
}

// ------------------------------------------------------------------
function getMsgInvalidBLZ() {
    return (msgBLZFormat);
}

//---------------------------------------------------------------------
function getMsgInvalidAccountCharacters() {
    return (msgInvalidAccountCharacters);
}

//---------------------------------------------------------------------
function getMsgBetragTooSmall(min) {
    return (setMessageParams(msgBetragTooSmall, min));
}

//---------------------------------------------------------------------
function getMsgBetragTooBig(max) {
    return (setMessageParams(msgBetragTooBig, max));
}

// ------------------------------------------------------------------
function setMessageParams(message, param1, param2, param3, param4, param5) {
    var newMessage = message;
    if (param1 != null) {
        newMessage = newMessage.replace("{0}", param1);
    }
    if (param2 != null) {
        newMessage = newMessage.replace("{1}", param2);
    }
    if (param3 != null) {
        newMessage = newMessage.replace("{2}", param3);
    }
    if (param4 != null) {
        newMessage = newMessage.replace("{3}", param4);
    }
    if (param5 != null) {
        newMessage = newMessage.replace("{4}", param5);
    }
    return newMessage;
}

// ------------------------------------------------------------------
function isNavigator() {
    return (navigator.appName == "Netscape");
}

// ------------------------------------------------------------------
// sets returns value.length zero string
function setZero(value) {
    var zeros = '';
    for(i=0;i<value.length;i++) {
        zeros += '0';
    }
    return zeros;
}

// ------------------------------------------------------------------
function nbalert(val) {
    alert(htmlToJsConversion(val));
}

// ------------------------------------------------------------------
function nbconfirm(val) {
    return confirm(htmlToJsConversion(val));
}

function doFocus(fld, msg) {
    if (!isEmpty(msg)){
        nbalert(msg);
    }
    fld.focus();
    return false;
}

function isPageNotLoaded() {
        if(pageLoaded    == false) {
            nbalert(msgPageNotLoaded);
            return true;
        } else {
            return false;
        }
}

function date2alter(dateAsString) {
    var tag = dateAsString.substring(0,2) * 1;
    var monat = dateAsString.substring(3,5) * 1;
    var jahr = dateAsString.substring(6,10) * 1;

    var heute = new Date();
    var heute_tag = heute.getDate()*1;
    var heute_monat = heute.getMonth()*1 + 1;
    var heute_jahr = heute.getFullYear()*1;

    if(monat < heute_monat || (monat == heute_monat && tag <= heute_tag)) {
        return heute_jahr - jahr;
    } else {
        return heute_jahr - jahr - 1;
    }
}

function getCurrForm(inpElement){
    for(var i=0; i<document.forms.length; ++i){
        var frm=document.forms[i];
        for(var j=0; j<frm.elements.length; ++j){
            var el=frm.elements[j];
            if (el==inpElement)
                return frm;
        }
    }
    return null;
}

//function openContactWindow(eMail)  deleted, use <sportal:popupLink

function setFldFocus(msg, fld){
    nbalert(msg);
    fld.focus();
    return false;
}
function redirect(msg,link){    Check = nbconfirm(msg);    if (Check == true){      window.location.href=link    }}
