document.observe("dom:loaded", function() { 
    initDocument();
});

var oldCountry="";
var oldType="";



function initDocument() {
    $$(".topictreeSectionMain a").each( function(link) {
        new Tooltip(link, {
            mouseFollow: true
        });
    });

}

function setFeds(country, type) {
    if (oldCountry != "") {
        if ( oldType == "fed") {
            var checkBoxes = $$(".innerBox ."+oldCountry+"Check");
            for ( var i = 0; i < checkBoxes.length; i++ ) {
                checkBoxes[i].checked=false;
                checkBoxes[i].disabled=true;
            }
            var titles = $$(".innerBox ."+oldCountry+"Title");
            for ( var j = 0; j < titles.length; j++ ) {
                titles[j].addClassName("disTitle")
            }
        }
    }
    if ( country != "" && country != "zip") {
        if (type == "fed") {
            var checkBoxes = $$(".innerBox ."+country+"Check");
            for ( var i = 0; i < checkBoxes.length; i++ ) {
                checkBoxes[i].disabled=false;
            }
            var titles = $$(".innerBox ."+country+"Title");
            for ( var j = 0; j < titles.length; j++ ) {
                titles[j].removeClassName("disTitle")
            }
        }
        oldCountry = country;
        oldType=type;
    }
}

function focusField(field) {
    if ( field.hasClassName("inputPre") ) {
        field.value="";
        field.removeClassName("inputPre");
    }
}

function blurField(field) {
    if ( field.value == "" && field.title != "" ) {
        field.value = field.title;
        field.addClassName("inputPre");
    }
}

/**
 * Comment
 */
function checkMessageFields2(id, courseId, scheduleId) {
    $('contactWait').appear({ 
        duration: 0.1,
        from: 0,
        to: 1
    });
}

/**
 *
 */
function closeContactError() {
    $('contactOverlay').fade({ 
        duration: 0.5,
        from: 0.5,
        to: 0
    });
    $('contactError').fade({ 
        duration: 0.5,
        from: 1,
        to: 0
    });
//$('contactErrorText').update("");
}

function closeContactMsg() {
    $('contactOverlay').fade({ 
        duration: 0.5,
        from: 0.5,
        to: 0
    });
    $('contactMsg').fade({ 
        duration: 0.5,
        from: 1,
        to: 0
    });
    $('contactName').value="";
    $('contactFirstName').value="";
    $('contactEmail').value="";
    $('contactPhone').value="";
    $('contactStreet').value="";
    $('contactZip').value="";
    $('contactCity').value="";
    $('contactText').value="";
    $('male').checked = false;
    $('female').checked = false;
}

function checkMessageFields(id, courseId, scheduleId) {
    $('contactOverlay').appear({ 
        duration: 0.5,
        from: 0,
        to: 0.5
    });
    var errorText="";
    var errorPrefix="<li class=\"errorLi\">";
    var errorSuffix="<\/li>";
    if ( $('male').checked == false && $('female').checked == false ) {
        errorText+=errorPrefix+"Bitte die Anrede auswählen!"+errorSuffix;
    }
    if ( $('contactName').value == "" ) {
        errorText+=errorPrefix+"Bitte einen Namen angeben!"+errorSuffix;
    }
    if ( $('contactFirstName').value == "" ) {
        errorText+=errorPrefix+"Bitte einen Vornamen angeben!"+errorSuffix;
    }
    if ( $('contactEmail').value == "" ) {
        errorText+=errorPrefix+"Bitte eine Emailadresse angeben!"+errorSuffix;
    }
    if ( $('contactText').value == "" ) {
        errorText+=errorPrefix+"Bitte einen Text eingeben!"+errorSuffix;
    }
    if (errorText !='') {
        //alert(errorText);
        $('contactErrorText').update(errorText);
        $('contactError').appear({ 
            duration: 0.5,
            from: 0,
            to: 1
        });
        return false;
    }
    var gender = "";
    if ( $('male').checked == true) {
        gender='M';
    } else {
        gender = 'F';
    }
    var name = $('contactName').value;
    var firstName = $('contactFirstName').value;
    var email = $('contactEmail').value;
    var phone = $('contactPhone').value;
    var street = $('contactStreet').value;
    var zip = $('contactZip').value;
    var city = $('contactCity').value;
    var text = $('contactText').value;

    var url = "http://localhost:8080/AjaxMail";
    var ajaxObjhttp = new Ajax.Request(url, {
        method: 'POST',
        parameters: {
            gender: gender,
            name: name,
            firstName: firstName,
            email: email,
            phone: phone,
            street: street,
            zip: zip,
            city: city,
            text: text,
            coursesupplier_id: id,
            course_id: courseId, 
            schedule_id: scheduleId
        },
        onCreate: function(transport) {
            $('contactWait').appear({ 
                duration: 0.1,
                from: 0,
                to: 1
            });
        },
        onSuccess: function(transport) {
            $('contactWait').fade({ 
                duration: 0.1,
                from: 1,
                to: 0
            });
            var serverResponse = transport.responseText;
            if ( serverResponse == "success") {
                $('contactMsg').appear({ 
                    duration: 0.5,
                    from: 0,
                    to: 1
                });
            } else {
                $('contactErrorText').update(serverResponse);
                $('contactError').appear({ 
                    duration: 0.5,
                    from: 0,
                    to: 1
                });
            }
        //alert(serverResponse);
        },
        onFailure : function(response) {
            $('contactWait').fade({ 
                duration: 0.1,
                from: 1,
                to: 0
            });
            $('contactErrorText').update("Fehler bei der &Uuml;bertragung!");
            $('contactError').appear({ 
                duration: 0.5,
                from: 0,
                to: 1
            });
        }
    });
}

