

input_checker();



function openWin(name,actionName){
    var fenster = window.open(actionName,name,"width=600,height=400,menubar=1,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}


function openDatenAnsicht(actionName){
    var fenster = window.open(actionName,"Daten","width=600,height=400,menubar=1,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}

function openHistory(action){
    var fenster = window.open(action,"Historie","width=330,height=250,menubar=1,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}

function openInfo(action){
    var fenster = window.open(action,"Information","width=330,height=380,menubar=1,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}

function openDetail(action){
    var fenster = window.open(action,"Detail","width=330,height=250,menubar=1,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}


function openhelp(actionName){
    var fenster = window.open(actionName,"Help","width=400,height=350,status=1,resizable=1,scrollbars=1");
    if(fenster!=null){
        fenster.focus();
    }
}


function checkFreigabe(checkControl) {
    checkBoxes(checkControl,"freigabe");
}

function checkMail(checkControl) {
    checkBoxes(checkControl,"mail");
}

function checkExport(checkControl) {
    checkBoxes(checkControl,"export");
}

function checkShow(checkControl) {
    checkBoxes(checkControl,"show");
}

function checkExportCsv(checkControl) {
    checkBoxes(checkControl,"exportcsv");
}



function checkBoxes(checkControl, endswith) {
    for(var j=0;j<window.document.forms.length;j++){
        for(var i=0;i<window.document.forms[j].elements.length;i++){
            var e = window.document.forms[j].elements[i];  
            if(e.name==checkControl.name){
                if(checkControl==true){
                    e.checked = true;
                } else if(checkControl==false){
                    e.checked = false;
                } else {
                    e.checked = checkControl.checked;
                }
            } else {
                var words = e.name.split(".");
                if(words.length>=2){
                    var s = words[words.length-1];
                    if(s==endswith){                    
                        if(checkControl==true){
                            e.checked = true;
                        } else if(checkControl==false){
                            e.checked = false;
                        } else {
                            e.checked = checkControl.checked;
                        }
                    }
                }
            }
        }
    }
}


function test(){
    alert("test called");
}


function language(){
    var ret = "";
    if(navigator.appName=="Microsoft Internet Explorer"){
        ret = navigator.userLanguage;
    } else if(navigator.appName=="Netscape"){
        ret = navigator.language;
        // ret = "en-EN";
    } else {
        ret = navigator.language;
    }
    if(ret==""){
        ret = "de-DE";
    }
    return ret;
}


/* this object make input fields checking
   for different input fields types
     numeric
     zero numeric
     float
     data
   Also it creates hotkeys.
 */


// constructor

function input_checker() {
    this.Netscape = new Boolean();
    this.Netscape = navigator.appName == "Netscape";
    
    this.processHotKeys = process_hotkeys;
    this.hotkeys = input_hotkeys;
    this.hotKeyCode = new Array();
    this.hotKeyAction = new Array();
    this.addHotKey = add_hotkey;
    
    this.uppercase = check_uppercase;
    this.isUppercase = is_uppercase;
    
    this.numeric = check_numeric;
    this.isNumeric = is_numeric;
    
    this.znumeric = check_znumeric;
    this.isZNumeric = is_znumeric;
    
    this.comma = ",";               // defines decimal delimiter
    this.commaCode = 44;            // defines key-code of decimal delimiter
    this.numfloat = check_float;
    this.isFloat = is_float;
    
    this.dot = ".";                 // defines date delimiter
    this.dotCode = 46;              // defines key-code of date delimiter
    this.days = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
    this.year0 = 70;                // defines the year limit 1970, so 69 we interpret as 2069 and 71 as 1971
    this.date = check_date;
    this.isADate = is_date;
}




// event handler for uppercase checking
// receive Event ev
// only converts to uppercase all letters

function check_uppercase(ev) {
    this.hotkeys(ev);
    var key = 0;
    
    if(this.Netscape) key = ev.which;
    else key = ev.keyCode;
    
    if((key>=97) && (key<=122) || key==228 || key==246 || key==252) {
        if(this.Netscape) ev.which = (key-32);
        else ev.keyCode = (key-32);
    }
    
    return true;
}

// checks if the string is uppercase
// receive input-field obj
// convert it to uppercase

function is_uppercase(obj) {
    var s = new String(obj.value);
    obj.value = s.toUpperCase();
    return true;
}

// tests if argument is a code of numeric key
// int argument

function isKeyNumeric(key) {
    return (key>=48) && (key<=57);
}


// event handler for numeric fields
// receive Event ev

function check_numeric(ev) {
    this.hotkeys(ev);
    var key = 0;
    
    if(this.Netscape) key = ev.which;
    else key = ev.keyCode;
    
    return isKeyNumeric(key) || (key==8);
}

// checks each char of obj.value if it is numeric
// returns false if not, true if all field is numeric

function is_numeric(obj) {
    var res = true;
    var s = new String(obj.value);
    
    for(i=0;i<s.length;i++) {
        if(!isKeyNumeric(s.charCodeAt(i))){
            res = false;
            break;
        }
    }
    
    if(!res) {
        alert("Feld " + obj.name + " muss numerisch sein!");
        //obj.select();
    }
    return res;
}

// checks each char of obj.value if it is numeric and if the length is correct.
// returns false if not, true if all field is numeric

function is_numeric(obj,len) {
    var res = true;
    var s = new String(obj.value);
    if(s==""){
        s = "0";
        obj.value = "0";
    }
    if((len>0)&&(s.length!=len)&&(s!="0")){
        res = false;
        alert("Feld " + obj.name + " muss " + len + "-stellig sein!");
        //obj.select();
    } else {  
        for(i=0;i<s.length;i++) {
            if(!isKeyNumeric(s.charCodeAt(i))){
                res = false;
                break;
            }
        }
        if(!res) {
            alert("Feld " + obj.name + " muss numerisch sein!");
            //obj.select();
        }
    }
    return res;
}


// event handler for zero-numeric fields
// receive Event ev

function check_znumeric(ev,obj,maxlen) {
    this.hotkeys(ev);
    var key = 0;
    var s = new String(obj.value);
    
    if(this.Netscape) key = ev.which;
    else key = ev.keyCode;
    
    return ((s.length<maxlen) && isKeyNumeric(key)) || (key==8);
}

// checks each char of obj.value if it is numeric
// also checks for max length of field, adds zeroes on beginning
// returns false if not numeric, true if field is z-numeric

function is_znumeric(obj,maxlen) {
    var res = true;
    var s = new String(obj.value);
    
    for(i=0;i<s.length;i++) {
        res = res && isKeyNumeric(s.charCodeAt(i));
    }
    
    var i2 = maxlen - s.length;
    res = res && (i2>=0);
    
    if(res) {
        if(i2>0) {
            for(i=0;i<i2;i++) s = "0"+s;
            obj.value = s;
        }
    }
    
    if(!res) {
        alert("Field "+obj.name+" must be numeric!"+
        "Maxlength is "+maxlen);
        //obj.select();
    }
    return res;
}

// event handler for zero-numeric fields
// receive Event ev, input-field obj and precision pr


function check_float(ev,obj,pr) {
    this.hotkeys(ev);
    if(pr<1) return this.numeric(ev);
    
    var key = 0;
    
    if(this.Netscape) key = ev.which;
    else key = ev.keyCode;
    
    var res = isKeyNumeric(key) || (key==8);
    var s = new String(obj.value);
    var i = s.indexOf(this.comma);
    if(i == -1) res = res || (key==this.commaCode);
    return  res;
}


// checks each char of obj.value if it is numeric or comma
// also checks for precision, correct it if needed
// returns false if not float, true if field is float

function is_float(obj,pr) {
    if(pr<1) return this.isNumeric(obj);
    
    var res = true;
    var s = new String(obj.value);
    var commaCount = 0;
    
    for(i=0;i<s.length;i++) {
        var key = s.charCodeAt(i);
        res = res && ( isKeyNumeric(key) || (key==this.commaCode) );
        if(key==this.commaCode) commaCount++;
    }
    res = res && (commaCount<2);
    
    if(!res) {
        alert("Fileld "+obj.name+" must be float!");
        //obj.select();
    }
    else if(s.length>0){
        var z = ""; while (z.length<pr) z += "0";
        var i = s.indexOf(this.comma);
        if(i > -1) {
            s += z;
            obj.value = s.substring(0,i+pr+1);
        }
        else obj.value = s + this.comma + z;
    }
    
    return res;
}


// event handler for date fields
// receive Event ev, input-field obj

function check_date(ev,obj) {
    this.hotkeys(ev);
    var res = true;
    var key = 0;
    var s = new String(obj.value);
    
    if(this.Netscape) key = ev.which;
    else key = ev.keyCode;
    
    res = res && ((s.length<10) || key==8);
    
    res = res && ( isKeyNumeric(key) || (key==8) || (key==this.dotCode) );
    
    var dotCount = 0;
    for(i=0;i<s.length;i++)
        if(s.charCodeAt(i)==this.dotCode) dotCount++;
    
    if(key==this.dotCode) {
        res = res && dotCount<2;
    }
    else
        if( res && (s.charCodeAt(s.length-1)!=this.dotCode)
        && !this.Netscape
        && (dotCount<2)
        && (s.length==2 || s.length==5) ) obj.value += this.dot;
    
    return res;
}


// checks if the date has right format,
// also add needed numbers to format dd.mm.yyyy
// returns false if date format incorrect

function is_date(obj) {
    var s = new String(obj.value);
    if(s == "-"){
        return true;
    }
    var res = (s.length==0) || (s.length>=6);
    var errMsg = "Feld " + obj.name + " muss im Datumsformat sein.\nFormat tt.mm.jjjj";
    
    while(res && (s.length>0)) {
        var d = "";
        var m = "";
        var y = "";
        var t, di,mi,yi;
        
        var i=0;
        t = s.charCodeAt(i);
        res = res && isKeyNumeric(t);
        if(!res) break;
        d += String.fromCharCode(t);
        i++;
        t = s.charCodeAt(i);
        
        res = (isKeyNumeric(t) || (t==this.dotCode));
        if(!res) break;
        if(isKeyNumeric(t)) {
            d += String.fromCharCode(t);
            i++;
            t = s.charCodeAt(i);
            res = ((isKeyNumeric(t)) || (t==this.dotCode));
            if(!res) break;
            if(isKeyNumeric(t)) i--;
        } else d = "0" + d;
        i++;
        t = s.charCodeAt(i);
        res = res && isKeyNumeric(t);
        if(!res) break;
        m += String.fromCharCode(t);
        
        i++;
        t = s.charCodeAt(i);
        res = res && (isKeyNumeric(t) || (t==this.dotCode) );
        if(!res) break;
        if(isKeyNumeric(t)) {
            m += String.fromCharCode(t);
            i++;
            t = s.charCodeAt(i);
            res = res && (isKeyNumeric(t) || (t==this.dotCode));
            if(!res) break;
            if(isKeyNumeric(t)) i--;
        } else m = "0" + m;
        i++;
        y = s.substring(i);
        res = res && (y.length==2 || y.length==4);
        if(!res) break;
        for(j=0;j<y.length;j++)
            res = res && isKeyNumeric(y.charCodeAt(j));
        if(!res) break;
        if(y.length==2) {
            yi = parseInt(y);
            y = (yi<this.year0 ? "20" : "19") + y;
        }
        
        di = Number(d);
        mi = Number(m);
        yi = Number(y);
        
        res = res && (mi>=1) && (mi<=12);
        if(!res) {
            errMsg = "Monat " + mi + " ist ung&uuml;ltig " + d + " " + m + " " + y;
            break;
        }
        res = res && (di>=1) && (di<=this.days[mi]);
        if( (mi==2) && !( (yi%4==0) && (yi%400==0 || yi%100!=0) ) ) res = res && (di<=28);
        if(!res) {
            errMsg = "Tag ist ung&uuml;ltig";
            break;
        }
        
        obj.value = d+this.dot+m+this.dot+y;
        break;
    }
    if(!res) {
        alert(errMsg);
        setTimeout(function(){obj.focus()}, 10);
        //obj.focus();
        obj.select();
    }
    return res;
}

// hotkey handler for input fields in Netscape
// in IE all event go thru all objects so we get it from document

function input_hotkeys(ev) {
    if(this.Netscape) this.processHotKeys(ev);
}

// hot key processor -
// receive Event ev and if it is hotkey, executes
// corresponding function set by user in html document

function process_hotkeys(ev) {
    if(this.hotKeyCode.length==0) return;
    
    var key = 0;
    if(this.Netscape) key = ev.which; else key = ev.keyCode;
    
    if( (!this.Netscape && ev.ctrlKey && ev.altKey) ||
    (this.Netscape && ev.modifiers & Event.ALT_MASK && ev.modifiers & Event.ALT_MASK) ) {
        var i;
        for(i=0;i<this.hotKeyCode.length;i++)
            if(key==this.hotKeyCode[i])
                window.eval(this.hotKeyAction[i]);
    }
}


// adds new hot-key to seek for
// aCode - int code of the key, use Ctr+Alt+Key
// anAction - string containing any JavaScript code to execute

function add_hotkey(aCode, anAction) {
    var a = new Array();
    a[0] = aCode;
    var a1 = new Array();
    a1 = this.hotKeyCode.concat(a);
    this.hotKeyCode = a1;
    
    a = new Array();
    a[0] = anAction;
    a1 = new Array();
    a1 = this.hotKeyAction.concat(a);
    this.hotKeyAction = a1;
}

var check = new input_checker();



function checkDate(o, format){
    var str = o.value;
    if(str=="-")
        str = "";
    var res = isDate(str,format);
    if(!res){
        alert("Feld " + o.name + 
        " muss im Datumsformat sein.\nFormat " + format);
        setTimeout(function(){o.focus()}, 10);        
        o.select();
        res = false;
    }
    return res;
}

//////////////////////
// date time functions
function DateToString(d){
    var i = d.getDate();
    var s = "";
    if(i<10) s = "0";
    s += i + ".";
    i = d.getMonth();
    if(i<10) s += "0";
    s += i + ".";
    return s + getYear2K(d);         
}


function getYear2K(d){
    var y = d.getYear();
    if(y<200)
        y += 1900;
    return y;     
}


function getToday(){    
    return DateToString(new Date());     
}


function getNow(){
    var d = new Date();
    return "" + padLeft(d.getHours(),"0",2) + ":" + padLeft(d.getMinutes(),"0",2) + ":" + padLeft(d.getSeconds(),"0",2);
}


///////////////////
// string functions
function padRight(s,ps,len){
    var str = "" + s;
    while(str.length<len){
        str += ps; 
    }
    return str;
}


function padLeft(s,ps,len){
    var str = "" + s;
    while(str.length<len){
        str = ps + str; 
    }
    return str;
}


function toFloat(s){
    var ret = parseFloat(NaN);
    var s2 = "";
    for(var i=0;i<s.length;i++){
        if(s.charAt(i)==','){
            s2 += '.';
        } else {
            s2 += s.charAt(i);
        }
    }
    ret = parseFloat(s2);
    return ret;
}

// golf specific

function selectLanguage(lang){
    var frm = document.forms['loginForm'];
    if(frm!=null){
        frm.operation.value='selectLanguage'; 
        frm.language.value = lang;
        frm.submit();
    }
}


function doLogin(){
    var frm = document.forms['loginForm'];
    if(frm!=null){
        frm.operation.value='login'; 
        frm.submit();
    }
    return false;
}


function doLogout(){
    var frm = document.forms['loginForm'];
    if(frm!=null){
        frm.operation.value='logout'; 
        frm.submit();
    }
}

function onEnterLogin(event){
    if (event.keyCode == 13) {
        doLogin();
    }
}

function onEnterLogout(event){
    if (event.keyCode == 13) {
        doLogout();
    }
}


function openLegendDetail(legendLabel,textLabelName){
    var fenster = window.open('legenddetail?legendLabel=' + legendLabel + '&textLabel=' + textLabelName,"Legend",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=235,height=115,left=500px,top=200px');
    if(fenster!=null){
        fenster.focus();
    }
    return false;
}

function showWaitForNext() {
    if (document.getElementById)
        document.getElementById("waitForNext").style.visibility = "visible";
}

function hideWaitForNext() {
    if (document.getElementById)
        document.getElementById("waitForNext").style.visibility = "hidden";
}

/******/
	

	

function save_false(){
						var frm = document.forms['addClub'];      
						frm.operation.value = 'save';
						frm.submit();
						return false;    
						}				
				

