//Check form for mandatory fields before submit
function CheckForm() {
    var failed = true;
    //Remove error-class before checking
    $('input,select,a').removeClass('inputerror');
    
    //Begin checking elements
    //Check all input fields marked with 'mandatory'
    $('input:[ref=mandatory]').each(function() {
        var currentFieldValue = $(this).val();
        var currentFieldTitle = $(this).attr('title');
        if(currentFieldValue == "" || currentFieldValue.length < 1 || currentFieldValue == currentFieldTitle) {
            $(this).addClass('inputerror');
            failed = false;
        }
    });
    //Check all dropdowns marked with 'mandatory'
    $('select:[ref=mandatory] option:selected').each(function() {
        if($(this).val() == "") {
            var sbid = $(this).parent().attr('sb');
            $('#sbSelector_'+sbid).addClass('inputerror');
            failed = false;
        }
    });
    $('input:[ref=email]').each(function() {
        var regExMail = new RegExp("[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?");
        
        if(!regExMail.test($(this).val())) {
            $(this).addClass('inputerror');
            failed = false;
        } else  {
            $(this).removeClass('inputerror');
        } 
    });    
    
    
    if (failed == false) {
        return false;
    } else {
        $('.tpl_contact input').each(function() {
            var currentFieldValue = $(this).val();
            var currentFieldTitle = $(this).attr('title');
            if(currentFieldValue == currentFieldTitle) {
                $(this).val("");
            }
        });
        
        $('.tpl_contact textarea').each(function () {
                if($(this).val() == $(this).attr('title')) {
                    $(this).val("");
                }
            });
        $('.tpl_contact select option:selected').each(function () {
        });
        return true;
    }    
}
SlideUpNavi = function () {
    $('.top-navi-level-2').slideUp('fast');
    $('.top-navi-level-1-link').removeClass('top-navi-level-1-link-over');
};
SlideUpLanguageBox = function () {
    $('.toparea-selectbox-language ul').slideUp('fast');
};
jQuery(document).ready(function() {
               
    var helpTimeoutControl;
    var selLink;
    $(".top-navi-level-1 a").each(function (i) {                                        
        if($(this).hasClass("top-navi-level-1-link-selected")){
            selLink = $(this);
        }
    });
    $('.top-navi-level-1-link').mouseenter(function (){
        window.clearTimeout(helpTimeoutControl);
        
        $('.top-navi-level-1-link').removeClass('top-navi-level-1-link-over');
        $(this).addClass('top-navi-level-1-link-over');
        par = $(this).next();
        
        if(par.css('display')!='block'){
            $('.top-navi-level-2').hide();
            $(par).slideDown('fast');
        }
        
        $(par).mouseenter(function (){
            window.clearTimeout(helpTimeoutControl);
        });
    });
    $('.top-navi-level-2').mouseover(function (){
        $(this).next().show();
    });
    $('.top-navi-level-1-link').mouseleave(function (){
        helpTimeoutControl = window.setTimeout("SlideUpNavi()", 1000);
        par = $(this).next();
        
        if((!$(this).hasClass("top-navi-level-1-link-over")) && (!$(this).hasClass("top-navi-level-1-link-selected")) && ($(selLink).hasClass("top-navi-level-1-link-selected"))){
            $(par).slideUp('fast');
        }
        
    });            
    $('.top-navi-level-2').mouseleave(function (){
        helpTimeoutControl = window.setTimeout("SlideUpNavi()", 1000);
    });
    $('.zoom-icon, .zoom-icon-nomargin').colorbox();
    /*  Begin functions for language select box */
    $('.toparea-selectbox-language').click(function (){
        window.clearTimeout(helpTimeoutControl);
        par = $(this).children('ul').eq(0);
        $(par).slideDown('fast');
        $(par).mouseenter(function (){
            window.clearTimeout(helpTimeoutControl);
        });
    });
    $('.toparea-selectbox-language, .toparea-selectbox-language ul').mouseleave(function (){
        helpTimeoutControl = window.setTimeout("SlideUpLanguageBox()", 1000);
    });                
    /* End functions for language select box */
  
});
