
//////////////////////////
var euroDate = 1;
//////////////////////////

function alerterr(formEl, emsg){
    var useAlerts = 0;
    
    if(useAlerts) alert(emsg);    
    else {
        var errEl = document.getElementById(formEl.name + "_err");
        errEl.innerHTML = emsg;
    }    
}

function clearerr(formEl){
    var errEl = document.getElementById(formEl.name + "_err");
    errEl.innerHTML = "";
}

function doslice(arg, idx) {
    var ret = Array();
    for (var i = idx; i < arg.length; i++) {
        ret.push(arg[i]);
    }
    return ret;
}

function Check(theForm, what, regexp, indices) {
    for (var i = 0; i < indices.length; i++) {
        var el = theForm.elements[indices[i]];
        if (el.value == "") continue;
        var avalue = el.value;
        if (!regexp.test(avalue)) {
            alerterr(el, what);  //  "Field is not a valid " + what
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckEmail(theForm) {
    var regexp = /^[0-9a-z\.\-_]+@[0-9a-z\-\_]+\..+$/i;    
    return Check(theForm, "Die angegebene Emailadresse ist nicht korrekt.", regexp, doslice(arguments, 1));
}

function CheckAlpha(theForm, what) {
    var regexp = /^[a-z]*$/i;
    return Check(theForm, what + " darf nur Buchstaben enthalten.", regexp, doslice(arguments, 2));
}

function CheckAlphaNum(theForm, what) {
    var regexp = /^[a-z0-9]*$/i;
    return Check(theForm, what + " darf nur Buchstaben und Ziffern enthalten.", regexp, doslice(arguments, 2));
}

function CheckNumeric(theForm, what) {
    for (var i = 2; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = parseInt(el.value);
        if (isNaN(avalue)) {
            alerterr(el, what + " ist keine Zahl.");    // "Field is not a valid integer number"
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckFloat(theForm, what) {
    for (var i = 2; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = parseFloat(el.value);
        if (isNaN(avalue)) {
            alerterr(el, what + " ist nicht korrekt.");    // "Field is not a valid floating point number"
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckDate(theForm) {
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = el.value;
//        if (isNaN(Date.parse(avalue))) {
        if (!isDate(avalue)) {
            alerterr(el, "Das angegebene Datum ist nicht korrekt.");        // "Field is not a valid date"
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckTime(theForm) {
    var regexp = /^[0-9]+:[0-9]+:[0-9]+/i;    
    if (!Check(theForm, "time", regexp,  doslice(arguments, 1)))
        return false;                 
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = el.value;
        if (isNaN(Date.parse("1/1/1970 " + avalue))) {
            alerterr(el, "Die angegebene Zeit ist nicht korrekt.");        // "Field is not a valid time"
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckRadio(theForm, el) {    
    var rchoice = false;
    for (var ct = 0; ct < el.length; ct++) {
        if (el[ct].checked) rchoice = true; 
    }
    if (rchoice != true) {
        alerterr(el[0], "Pflichtfeld wurde nicht ausgefüllt.");        // 
        el[0].focus();
        return false;
    }
    else clearerr(el[0]);
    return true;
}

function CheckRadioPositive(theForm, el) {    
    var rchoice = false;
    for (var ct = 0; ct < el.length; ct++) {
        if (el[ct].checked) {
            rchoice = true; 
            var val = el[ct].value;
        }    
    }
    if (rchoice != true || val == "Nein") {
        alerterr(el[0], "Pflichtfeld wurde nicht ausgefüllt.");        // 
        el[0].focus();
        return false;
    }
    else clearerr(el[0]);
    return true;
}

function CheckRequiredFields(theForm) {    
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value=="") {
            alerterr(el, "Pflichtfeld wurde nicht ausgefüllt.");            // "This field may not be empty"
            el.focus();
            return false;
        }
        else clearerr(el);
    }
    return true;
}

function CheckIfFieldsAreEqual(theForm, what) {    
        var el1 = theForm.elements[arguments[2]];
        var el2 = theForm.elements[arguments[3]];
        if (el1.value!=el2.value) {
            alerterr(el2, what + " stimmen nicht überein.");            // "This field may not be empty"
            el2.focus();
            return false;
        }
        else clearerr(el2);
    return true;
}

function CheckFieldMinLength(theForm, minLen, what) {    
        var el = theForm.elements[arguments[3]];
        var str = el.value;
        if (str.length<minLen) {
            alerterr(el, what + " muß mind. " + minLen + " Zeichen enthalten.");       // "This field may not be empty"
            el.focus();
            return false;
        }
        else clearerr(el);
    return true;
}

function ClearErrorFields(theForm) {    
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        clearerr(el);
    }
    return true;
}

function CheckFeedbackForm(theForm) {
    ClearErrorFields(theForm, 0, 1);
    return (
        CheckRequiredFields(theForm, 0, 1) &&
        CheckEmail(theForm, 1) 
    )
}

function CheckPassFeedbackForm(theForm) {
    ClearErrorFields(theForm, 0);
    return (
        CheckRequiredFields(theForm, 0) &&
        CheckEmail(theForm, 0) 
    )
}

function CheckEnrollmentPersForm(theForm) {
//    ClearErrorFields(theForm, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 18, 30, 37, 43, 46);
    ClearErrorFields(theForm, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 18, 30, 37, 38, 44, 47);
    return (
        CheckRequiredFields(theForm, 1, 3, 4
//                                            , 5   // birthdate not reqd !!!
                                                ) &&
        CheckDate(theForm, 5) &&
        CheckRequiredFields(theForm, 6, 7, 8, 9, 10, 11, 13) &&
        CheckEmail(theForm, 13) &&
        CheckRadio(theForm, document.forms.enrollment.Abc_Kurs_schon_besucht) &&        // 18
        CheckRequiredFields(theForm, 30, 38, 44) &&
        CheckRadioPositive(theForm, document.forms.enrollment.AGB_gelesen_und_akzeptiert)    // 46
    )
}

function CheckEnrollmentFirmForm(theForm) {
    ClearErrorFields(theForm, 1, 2, 3, 4, 5, 6, 7, 10, 20, 27, 33, 36);
    return (
        CheckRequiredFields(theForm, 1, 2, 3, 4, 5, 6, 7, 10) &&
        CheckEmail(theForm, 10) && 
        CheckRequiredFields(theForm, 20, 27, 33) &&
        CheckRadioPositive(theForm, document.forms.enrollment.AGB_gelesen_und_akzeptiert)    // 36
    )
}

function CheckLoginForm(theForm) {
    ClearErrorFields(theForm, 0, 1);
    return (
        CheckRequiredFields(theForm, 0, 1) 
    )
}

function CheckRegisterForm(theForm) {
    ClearErrorFields(theForm, 2, 3, 4);
    return (
        CheckRequiredFields(theForm, 2) &&
        CheckAlphaNum(theForm, 'Der Benutzername', 2) &&
        CheckRequiredFields(theForm, 3) &&
        CheckFieldMinLength(theForm, 6, 'Passwort', 3) && 
//        CheckAlphaNum(theForm, 3) &&
        CheckRequiredFields(theForm, 4) &&
//        CheckAlphaNum(theForm, 4) &&
        CheckIfFieldsAreEqual(theForm, 'Die Passwörter', 3, 4) 
    )
}

function CheckRegisterPersDataForm(theForm) {
    ClearErrorFields(theForm, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 18);
    return (
        CheckRequiredFields(theForm, 1, 3, 4
//                                            , 5   // birthdate not reqd !!!
                                                ) &&
        CheckDate(theForm, 5) &&
        CheckRequiredFields(theForm, 6, 7, 8, 9, 10, 11, 13) &&
        CheckEmail(theForm, 13) &&
        CheckRadio(theForm, document.forms.frmRegister.Abc_Kurs_schon_besucht) 			// 18
    )
}

function CheckRegisterFirmDataForm(theForm) {
    ClearErrorFields(theForm, 1, 2, 3, 4, 5, 6, 7, 10);
    return (
        CheckRequiredFields(theForm, 1, 2, 3, 4, 5, 6, 7, 10) &&
        CheckEmail(theForm, 10) 
    )
}

function CheckExtSearchForm(theForm) {
    ClearErrorFields(theForm, 7, 8, 9, 10, 12, 13);
    return (
        CheckDate(theForm, 7, 8) &&
        CheckNumeric(theForm, 'Der angegebene Wert', 9, 10, 12, 13) 
    )
}

function CheckSimpleSearchForm() {
    
    if(! document.forms.searchform.str.value) {
        document.forms.searchform.action = '../../../kurse-courses-navi/kurs-programm/wien-vienna/course-search-index.php';          
        }
    else {
        document.forms.searchform.action = '../../../kurse-courses-navi/kurs-programm/wien-vienna/course-search-result.php';         
        }    
    return true;
}

function EnumEls() {
	var theForm = document.forms[0];
	for(var i=0; i<theForm.elements.length; i++) 
		if(theForm.elements[i].type != 'hidden') theForm.elements[i].onfocus = ElOnFoc; 
}

function ElOnFoc(i) {
	var theForm = document.forms[0];
	for(var i=0; i<theForm.elements.length; i++) if(theForm.elements[i].name == this.name) break; 
	var al =  'Type: ' + this.type + ', Name: ' + this.name + ', Index: ' + i; //  
	alert(al);
}



