if (!fs) {
    var fs = {controller:{}}
}

fs.controller.CheckNewProjectForm = {

    companyName: null,
    companyCity: null,
    companyContactPerson:  null,
    mailContactPerson: null,
    telNrContactPerson: null,

    errorBox: null,
    errorString: "",

    initialize:function() {
        this.companyName = dojo.byId("companyName");
        this.companyCity = dojo.byId("companyCity");
        this.companyContactPerson = dojo.byId("companyContactPerson");
        this.mailContactPerson = dojo.byId("mailContactPerson");
        this.telNrContactPerson = dojo.byId("telNrContactPerson");

        this.errorBox = dojo.byId("errorsNewProjectJS");
    },
    check:function() {
        //reset
        dojo.removeClass(this.companyName.parentNode, "errors");
        dojo.removeClass(this.companyCity.parentNode, "errors");
        dojo.removeClass(this.companyContactPerson.parentNode, "errors");
        dojo.removeClass(this.mailContactPerson.parentNode, "errors");
        dojo.removeClass(this.telNrContactPerson.parentNode, "errors");

        this.errorString = "";

        //check
        var isOk = true;
        isOk &= this.checkEmptyValues();
        isOk &= this.checkEmail();
        isOk &= this.checkPhone();
        isOk &= this.checkLength();
        this.showErrorString()
        return isOk > 0 ? true : false;
    },
    checkEmptyValues:function() {
        var isOk = true;

        if (dojo.trim(this.companyName.value) == "") {
            dojo.addClass(this.companyName.parentNode, "errors");
            isOk = false;
            this.errorString += 'Feld "Firmenname" darf nicht leer sein.' + "<br/>";
        }
        if (dojo.trim(this.companyCity.value) == "") {
            dojo.addClass(this.companyCity.parentNode, "errors");
            isOk = false;
            this.errorString += 'Feld "Firmensitz" darf nicht leer sein.' + "<br/>";
        }
        if (dojo.trim(this.companyContactPerson.value) == "") {
            dojo.addClass(this.companyContactPerson.parentNode, "errors");
            isOk = false;
            this.errorString += 'Feld "Ansprechpartner" darf nicht leer sein.' + "<br/>";
        }
        if (dojo.trim(this.mailContactPerson.value) == "" && dojo.trim(this.telNrContactPerson.value) == "") {
            dojo.addClass(this.mailContactPerson.parentNode, "errors");
            isOk = false;
            this.errorString += 'Bitte, tragen Sie Ihre Email oder Telefonnummer ein.' + "<br/>";
        }
        return isOk;
    },
    checkEmail:function() {
        var isOk = true;
        if (dojo.trim(this.mailContactPerson.value) != "" && !this._testMail(dojo.trim(this.mailContactPerson.value))) {
            dojo.addClass(this.mailContactPerson.parentNode, "errors");
            isOk = false;
            this.errorString += 'Ihre Eingabe im Feld "Ansprechpartner Email" ist keine gültige E-Mail Adresse.' + "<br/>";
        }
        return isOk;
    },

    _testMail: function(str) {
        var mailExpr = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (str.match(mailExpr)) {
            return true;
        } else {
            return false;
        }
    },

    checkPhone: function() {
        var isOk = true;

        if (dojo.trim(this.telNrContactPerson.value) != "" && !this._testPhone(dojo.trim(this.telNrContactPerson.value))) {
            dojo.addClass(this.telNrContactPerson.parentNode, "errors");
            isOk = false;
            this.errorString += 'Ihre Eingabe im Feld "Ansprechpartner Telefonnummer" ist keine gültige Telefonnummer.' + "<br/>";
        }
        return isOk;
    },

    _testPhone: function(str) {
        var phoneNr = /^([0-9\s\(\)\+\-\/]{9,30})*$/;
        if (str.match(phoneNr)) {
            return true;
        } else {
            return false;
        }
    },

    checkLength: function() {
        var isOk = true;
        var longest = 300;
        var compLength = dojo.trim(this.companyName.value).length;
        if (longest < compLength || compLength < 3) {
            dojo.addClass(this.companyName.parentNode, "errors");
            isOk = false;
            this.errorString += 'Firmenname darf zwischen 3 und 300 Zeichen lang sein.' + "<br/>";
        }
        var cityLength = dojo.trim(this.companyCity.value).length;
        if (longest < cityLength || cityLength < 3) {
            dojo.addClass(this.companyCity.parentNode, "errors");
            isOk = false;
            this.errorString += 'Firmensitz darf zwischen 3 und 300 Zeichen lang sein.' + "<br/>";
        }
        return isOk;
    },

    showErrorString: function() {
        if (this.errorString != "") {
            this.errorBox.innerHTML = this.errorString;
            this.errorBox.style.display = "block";
        }
    }
}

fs.projectFormChecker = fs.controller.CheckNewProjectForm;
fs.projectFormChecker.initialize();

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}


function changeVisibility(section, counter1, counter2) {

    //set hidden blocks
    for (i = 1; i < counter1; i++) {

        document.getElementById('sectioNdiv_' + i).style.display = "none";
        document.getElementById('sectioNdiv_' + i).style.zIndex = 1;
        document.getElementById('sectioNdiv_' + i).style.zIndex = 2;
    }

    //set hidden links
    for (i = 1; i < counter2; i++) {

        document.getElementById('sectioNlist_' + i).style.borderRightColor = "#FFFFFF";
        document.getElementById('sectioNlist_' + i).style.backgroundColor = "#FFFFFF";
    }

    // set visible
    document.getElementById('sectioNdiv_' + section).style.display = "block";
    document.getElementById('sectioNlist_' + section).style.borderRightColor = "#FF7517";
    document.getElementById('sectioNlist_' + section).style.backgroundColor = "#eeeeee";
}







