/**
 * @author
 */
var user_errorcount;
var journal = false;

function checkLength(id, counter, praefix, minlen, maxlen){

    user_errorcount = "NO";
    
    $(praefix + counter).innerHTML = maxlen - $(id).textLength;
    
    if ($(id).textLength < minlen || $(id).textLength >= maxlen) {
        $(id).style.backgroundColor = '#EbC7C7';
        user_errorcount = "JO";
    }
    else {
        $(id).style.backgroundColor = '#fff';
        user_errorcount = "NO";
    }
    
    
    if (praefix.length > 0) {
        if (user_errorcount == "NO") {
            Element.setStyle(praefix + "sendbutton", {
                display: 'inline'
            });
        }
        else {
            Element.setStyle(praefix + "sendbutton", {
                display: 'none'
            });
        }
    }
    
    
}

function checkUser(id, praefix, language){
    var username = document.getElementById(id);
    
    var userexists_de = 'Username bereits vergeben';
    var userexists_en = 'Username is already in use';
    var shortname_de = 'Bitte mindestens 5 Zeichen';
    var shortname_en = 'Please use at least 5 characters';
    
    if ($('userexists')) {
        Element.remove('userexists');
    }
    
    user_errorcount = "NO";
    
    if (username.value.length >= 5) {
        check = new Ajax.Request('../root/checkUser.php?uname=' + username.value, {
            method: 'get',
            asynchronous: false,
            onSuccess: function(transport){
                if (transport.responseText == "0") {
                    username.style.backgroundColor = 'transparent';
                }
                else {
                    user_errorcount = "JO";
                    username.style.backgroundColor = '#EbC7C7';
                    new Insertion.Before(username, '<span id="userexists"><br>' + eval('userexists_' + language) + '<br></span>');
                }
                
            }
        });
        
        
        
    }
    else {
        new Insertion.Before(username, '<span id="userexists"><br>' + eval('shortname_' + language) + '<br></span>');
        user_errorcount = "JO";
    }
    
    
    //alert(user_errorcount);
    
    if (praefix.length > 0) {
        if (user_errorcount == "NO") {
            Element.setStyle(praefix + "sendbutton", {
                display: 'inline'
            });
        }
        else {
            Element.setStyle(praefix + "sendbutton", {
                display: 'none'
            });
        }
    }
    
}


function regexmail(mail){
    var regexist = false;
    var res = false;
    if (typeof(RegExp) == 'function') {
        var testregex = new RegExp('abc');
        if (testregex.test('abc') == true) {
            regexist = true;
        }
    }
    
    if (regexist == true) {
        reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-Z0-9\\-\\.]+)(\\.)([a-zA-Z]{2,4})$');
        res = (reg.test(mail));
    }
    else {
        res = (mail.search('@') >= 1 && mail.lastIndexOf('.') > mail.search('@') && mail.lastIndexOf('.') >= mail.length - 5)
    }
    return (res);
}


function sendForm(praefix){
    var errorcount = 0;
    
    
    var required = $$('.' + praefix + "required");
    var alerted = $$(".formwarning");
    var email = $$('.' + praefix + "email");
    var pass = $$('.' + praefix + "pass");
    var selected = $$('.' + praefix + "select");
    var captcha = $$('.' + praefix + 'captcha');
    
    var captcha_match;
    var captcha_valid = false;
    
    new Ajax.Request('templates/includes/captcha/check.php', {
        method: 'post',
        asynchronous: false,
        postBody: 'id=' + sessid,
        onSuccess: function(response){
            captcha_match = response.responseText;
        }
        
    });
    
    alerted.each(function(el){
        theid = el.getAttribute("id");
        Element.removeClassName(theid, "formwarning");
    });
    
    
    selected.each(function(el){
        if (el.lastChild.options.selectedIndex == 0) {
            theid = el.getAttribute("id");
            Element.addClassName(theid, "formwarning");
            errorcount++;
        }
        
    });
	
	
	
    
    
    required.each(function(el){
        if (el.lastChild.type == "text" || el.lastChild.type == "password" || el.lastChild.type == "textarea" || el.lastChild.type == "file") {
            if (el.lastChild.value == "") {
                theid = el.getAttribute("id");
                Element.addClassName(theid, "formwarning");
                errorcount++;
            }
            
        }
        else 
            if (el.lastChild.firstChild && el.lastChild.firstChild.type == "radio") {
                var the_form = $(praefix + 'form');
                var these_radios = the_form.getInputs('radio', el.lastChild.firstChild.name);
                
                var check = false;
                var is_checked = these_radios.each(function(val, key){
                    if (val.checked == true) {
                        check = true;
                    }
                });
                if (check == false) {
                    theid = el.getAttribute("id");
                    Element.addClassName(theid, "formwarning");
                    errorcount++;
                }
                
                
            }
            else {
            
                if (el.lastChild.checked == false) {
                    theid = el.getAttribute("id");
                    Element.addClassName(theid, "formwarning");
                    errorcount++;
                }
            }
    });
    
    email.each(function(el){
        if (el.lastChild.value != "") {
            valid = regexmail(el.lastChild.value);
            
            if ($('emailformat')) {
                Element.remove("emailformat");
            }
            
            if (valid == false) {
                theid = el.getAttribute("id");
                Element.addClassName(theid, "formwarning");
                new Insertion.Bottom($(theid).firstChild, '<span id="emailformat"><br/>Ihre E-Mail-Adresse scheint ein falsches Format zu haben</span>');
                errorcount++;
            }
        }
    });
    
    captcha.each(function(el){
				
        if (el.value != "") {
            thevalue = el.value.toUpperCase()
			if (captcha_match == thevalue.toString()) {
				captcha_valid = true;
			}
            
            if ($('captchavalue')) {
                Element.remove("captchavalue");
            }
            
            if (captcha_valid == false) {
                theid = el.getAttribute("id");
                Element.addClassName(theid, "formwarning");
                new Insertion.Bottom($('uploadMsg'), '<span id="captchavalue"><br/>Der Code wurde falsch eingegeben</span>');
                errorcount++;
            }
        }
    });
    
    
    
    //debugger;
    if (pass.length > 0) {
        if ($('passtest')) {
            Element.remove("passtest");
        }
        if (pass[0].lastChild.value.length > 0 && pass[1].lastChild.value.length > 0) {
        
        
            if (pass[0].lastChild.value != pass[1].lastChild.value) {
                theid0 = pass[0].getAttribute("id");
                theid1 = pass[1].getAttribute("id");
                pass[0].lastChild.value = "";
                pass[1].lastChild.value = "";
                Element.addClassName(theid0, "formwarning");
                Element.addClassName(theid1, "formwarning");
                new Insertion.Bottom($(praefix + "formwarning"), '<span class="formwarning" id="passtest"><br/>Ihre Passw&ouml;rter waren nicht identisch.</span>');
                errorcount++;
            }
            else {
            
            }
            
            
        }
        else {
            if ($('passtest')) {
                Element.remove("passtest");
            }
        }
    }
    
    //alert(errorcount);
    
    if (errorcount > 0) {
        Element.addClassName(praefix + "formwarning", "formwarning");
    }
    else {
        $(praefix + 'form').submit();
    }
    
    
}

function sendFormEcard(praefix){

    var errorcount = 0;
    
    var required = $$('.' + praefix + "required");
    var alerted = $$(".formwarning");
    var email = $$('.' + praefix + "email");
    var textar = $$('.' + praefix + "textarea");
    
    
    
    
    
    alerted.each(function(el){
        theid = el.getAttribute("id");
        Element.removeClassName(theid, "formwarning");
    });
    
    
    required.each(function(el){
        if (el.nextSibling.lastChild.value == "") {
            theid = el.getAttribute("id");
            Element.addClassName(theid, "formwarning");
            errorcount++;
        }
        
    });
    
    textar.each(function(el){
        if (el.value == "") {
            theid = el.getAttribute("id");
            Element.addClassName(theid + '_label', "formwarning");
            errorcount++;
        }
        
    });
    
    email.each(function(el){
        if (el.nextSibling.lastChild.value != "") {
            valid = regexmail(el.nextSibling.lastChild.value);
            
            if ($('emailformat')) {
                Element.remove("emailformat");
            }
            
            if (valid == false) {
                theid = el.getAttribute("id");
                Element.addClassName(theid, "formwarning");
                new Insertion.Bottom($(praefix + 'formwarning'), '<span id="emailformat"><br/>Ihre E-Mail-Adresse scheint ein falsches Format zu haben</span>');
                errorcount++;
            }
        }
    });
    
    if (errorcount > 0) {
        Element.addClassName(praefix + "formwarning", "formwarning");
    }
    else {
        $(praefix + 'form').submit();
    }
    
    
}


function journalSwitch(mode, praefix){

    var jrequired = $$('.' + praefix + "jrequired");
    var jnotrequired = $$('.' + praefix + "jnotrequired");
    
    if (mode == 'on' && journal == false) {
    
    
    
    
        new Effect.BlindUp('ff4', {
            duration: 0.5
        });
        
        $('ff24_data').value = $('ff4_data').value;
        
        new Effect.BlindDown('journalfield', {
            duration: 0.5
        });
        
        jrequired.each(function(el){
            theid = el.getAttribute("id");
            Element.addClassName(theid, praefix + "required");
        });
        
        jnotrequired.each(function(el){
            theid = el.getAttribute("id");
            Element.removeClassName(theid, praefix + "required");
        });
        
        journal = true;
    }
    else 
        if (mode == "off" && journal == true) {
            new Effect.BlindDown('ff4', {
                duration: 0.5
            });
            
            $('ff4_data').value = $('ff24_data').value;
            
            new Effect.BlindUp('journalfield', {
                duration: 0.5
            });
            
            jrequired.each(function(el){
                theid = el.getAttribute("id");
                Element.removeClassName(theid, praefix + "required");
            });
            
            jnotrequired.each(function(el){
                theid = el.getAttribute("id");
                Element.addClassName(theid, praefix + "required");
            });
            
            journal = false;
        }
    
    
    
    
}

