var isIE = document.compatMode && document.all;
var isIE5 = isIE && /msie 5\.0/i.test(navigator.userAgent);
var isOpera = /opera/i.test(navigator.userAgent);
var isKHtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
String.prototype.trim = function() { 
    return (this.replace(/\s+$/,"").replace(/^\s+/,"")); 
}
Element.addMethods({
    selectedValue : function(e) { return e.options[e.selectedIndex].value; },    
    setText : function(e, v) { if (e.firstChild) e.firstChild.nodeValue = v; },
    show : function(e, v) { e.style.display = v; return e; },
    removeChildren : function(e) { e.childElements().invoke('remove'); },
    center : function(e) {
        var h = document.viewport.getHeight(), w = document.viewport.getWidth();
        if (isIE && browserVersion < 7) {
            h = document.body.clientHeight; w = document.body.clientWidth; }
        e.style.top = ((h - e.getHeight()) / 2) + 'px';
        e.style.left = ((w - e.getWidth()) / 2) + 'px';
        return e;
    },
    isRelated : function(e, ev) {
        var r = ev.relatedTarget;
        if (!r) {
            if (ev.type == 'mouseover') r = ev.fromElement;
            else if (ev.type == 'mouseout') r = event.toElement; 
        }
        while (r) {
            if (r == e) return true;
            r = r.parentNode;
        }
        return false;
    },
    getAbsPos : function(e) {
        var scrollLeft = 0, scrollTop = 0;
        if (/^div$/i.test(e.tagName)) {
            scrollLeft = e.scrollLeft;
            scrollTop = e.scrollTop;
        }
        var r = { x: e.offsetLeft - scrollLeft, y: e.offsetTop - scrollTop };
        if (e.offsetParent) {
            var t = e.offsetParent.getAbsPos();
            r.x += t.x;
            r.y += t.y;
        }
        return r;
    },
    append : function(e, tag) {
        var t = $(document.createElement(tag));
        e.insert(t);
        return t;
    },
    switchClass : function(e, c, c2) { 
        return e.removeClassName(c).addClassName(c2);
    }
});
Element.addMethods('SELECT', {
    selectValue : function(e, v) {
        for (var i = 0; i < e.options.length; i++)
            e.options[i].selected = e.options[i].value == v;
    },
    selectText: function(e, v) {
        for (var i = 0; i < e.options.length; i++)
            e.options[i].selected = e.options[i].text == v;
    },
    selectedText : function(e) { return e.options[e.selectedIndex].text; },
    clear : function(e) { e.options.length = 1; },
    clearAll : function(e) { e.options.length = 0; },
    add : function(e, t, v, s, ds) {
        e.options[e.options.length] = new Option(t, v, ds || false, s || false);
    } 
});
Element.addMethods('TEXTAREA', {
    addLineHeight : function(e, v) {
        if (!v) v = 1;
        e.style.height = (e.getHeight() + e.getLineHeight() * v) + 'px';
    },
    removeLineHeight : function(e, v) {
        if (!v) v = 1;
        e.style.height = (e.getHeight() - e.getLineHeight() * v) + 'px';
    },
    getHeight : function(e) {
        return parseInt(e.style.height.substr(0, e.style.height.length - 2));
    },
    getLineHeight : function(e) {
        return parseInt(e.getHeight() / e.value.split('\n').length);
    }
})
function GetLabel(key) {
    for (x in BASELabels)
        if (x.toLowerCase() == key.toLowerCase()) return BASELabels[x];
    return '';
}
function PostBack(method, params) {
    $('BASE__postBackForm__method').value = method;
    $('BASE__postBackForm__params').value = Object.toJSON(params);
    $('BASE__postBackForm').submit();
}
function OpenDialog(url, name, w, h) {
    options = '';
    if (w > 0 && h > 0) {
        if (isIE) w += 15;
        options = 'height=' + h + ',width=' + w + ',';
    }
    pleft = (screen.width - w) / 2;
    ptop = (screen.height - h) / 2;
    options += 'top=' + ptop + ',left=' + pleft + ',';
    options += 'scrollbars=yes,status=yes,location=no,toolbar=no,menubar=no';
    window.open(url, name, options);
}
function GetUrl(url) { document.location.href = url; }
function GetBlankUrl(url) { window.open(url, '_blank'); }
function GetModeUrl(m) { GetUrl('index.php?mode=' + m); }
function LoadingInfo(){}
LoadingInfo.Show = function(v) { 
    $('loadingInfoText').innerHTML = v; 
    $('loadingInfo').center().show('block');
}
LoadingInfo.Hide = function() { $('loadingInfo').hide(); }
Form.Validate = function(f) {
    for (var i = 1; i < arguments.length; i++) {
        var e = f[arguments[i]];
        if (!Form.ValidateRequired(e, GetLabel('FieldIsMandatory'))) {
            e.focus();
            return false;
        }
    }
    return true;
}
Form.ValidateRequired = function(e, msg) {
    if (e.value == null || e.value == "") {
        alert(msg);
        return false;
    }
    return true;
}
function ValidateEmail(s) {
   var f = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   return f.test(s);
}
function Dialog(title, params, callback) {
   if (!callback) callback = ShowDialog;
   $('dialog_title').innerHTML = GetLabel(title);  
   BA.Update('dialog_content', { parameters: params, onComplete: callback });    
}
function ShowDialog() {
   $('dialog').show('block');
}
function ValidateContactForm()
{
    var valid = true;

    if ($F('cfName') == '')
    {
        $('cfName').addClassName('missing');
        valid = false;
    }
    else
    {
        $('cfName').removeClassName('missing');
    }
    
    if ($F('cfEmail') == '')
    {
        $('cfEmail').addClassName('missing');
        valid = false;
    }
    else
    {
        $('cfEmail').removeClassName('missing');
    }
    
    if ($F('cfMessage') == '')
    {
        $('cfMessage').addClassName('missing');
        valid = false;
    }
    else
    {
        $('cfMessage').removeClassName('missing');
    }
    
    if (valid)
    {
        BA.Call({ parameters: { service: 'Ctrl.mail.send', name: $F('cfName'), 
            email: $F('cfEmail'), message: $F('cfMessage') }, 
            onComplete: OnContactFormSent });
    }
    
    return valid;
}
function OnContactFormSent(t)
{
    alert("Ihre Nachricht wurde erfolgreich an Eat2day gesendet.");
}
function ToggleDiv(id, tabid) {
    var e = $(id);
    if (!e) return;
    if (e.style.display == 'none') {
        if (e.innerHTML == '') {
            BA.Update(id, { parameters: { service: 'Ctrl.contentPane.load', tabid: tabid, cp: 'text' }});
        }
        e.show('block');
    } else {
        e.hide();
    }
}
