
defaultStatus = "lovemail";
var em_pop, em_pop2;

function em_popup (popurl, popopt)
{
    if (typeof(popurl) == "undefined") {
        popopt = "width=400, height=450, scrollbars=1,";
    }

    var tmp_option = new Array();
    tmp_option = popopt.split(",");
    for (var i = 0; i < tmp_option.length; i++) {
        if (tmp_option[i]) {
            eval(tmp_option[i]);
        }
    }

    if (popurl.indexOf("/(^|\/)_/") >= 0) {
        popurl += (popurl.indexOf("?") >= 0 ? "&" : "?")
                + "em_client=<?=$em_client?>"
                + "&em_subclient=<?=$em_subclient?>"
                + "&em_ssc=<?=$em_ssc?>";
    }
    popopt += ", left=" + Math.round((screen.availWidth - Number(width)) / 2);
    popopt += ", top=" + Math.round((screen.availHeight - Number(height)) / 2);
    if (window.name == "content") {
        if (!popurl.match("/print")) {
            self.location.href = popurl+"&back=1";
        } else {
            delete em_pop2;
            em_pop2 = open(popurl, "em_pop2", popopt);
            em_pop2.focus();
        }
    } else {
        if (top.name != "em_pop" &&
            typeof(em_pop) != "undefined" &&
            em_pop.closed == false &&
            navigator.platform.match(/Mac/) == null) {
            em_pop.close();
            delete em_pop;
        }

        if (typeof(popurl) != "undefined") {
            delete em_pop;
            em_pop = open(popurl, "em_pop", popopt);
            em_pop.focus();
        }
    }
}

function em_check_suche(form)
{
    if (form.such) {
        var test_such = form.such.value;
        test_such = test_such.replace(/(^| )(\+|-|AND|OR|NOT)( |$)/i,"");
        test_such = test_such.replace(/ /i,"");

        if (!test_such || form.such.value == "Suche") {
            alert("Geben Sie bitte einen Suchgbegriff ein!");
            form.such.focus(); return false;
        }

        if (test_such.length < 3) {
            alert("Geben Sie bitte mindestens 3 Zeichen ein!");
            form.such.focus();
            return false;
        }
    }
}

function em_field_focus (field,aktiv)
{
    if (field) {
        //field.style.border = aktiv ? "1px solid black" : "1px solid #BA458B";
        field.style.color  = aktiv ? "black" : "#BA458B";
    }
}

function em_myonline_check(form)
{
    if (form.my_user) {
        if (!form.my_user.value) {
            alert("Geben Sie bitte Ihren Benutzernamen ein!");
            form.my_user.focus(); return false;
        }

        if (!form.my_pass.value) {
            alert("Geben Sie bitte Ihr Passwort ein!");
            form.my_pass.focus(); return false;
        }

        if (form.my_pass.value.length < 3) {
            alert("Geben Sie bitte mindestens 6 Zeichen ein!");
            form.my_pass.focus(); return false;
        }
    }
}

if (top.name != "main" && top.name != "pop") {
    top.name = "main";
}

function em_img_resize(strID, nMaxWidth, nMaxHeight)
{
    var strDebug = "";
    var objImg = document.getElementById(strID);
    if (objImg) {
        if (typeof nMaxHeight == "undefined" || nMaxHeight == 0) {
            nMaxHeight = nMaxWidth;
        }

        strDebug += objImg.width + " x " + objImg.height + "\n";
        if (objImg.width > objImg.height) {
            objImg.style.width  = ((nMaxWidth >= nMaxHeight) ? nMaxWidth  + "px" : "");
            objImg.style.height = ((nMaxWidth <  nMaxHeight) ? nMaxHeight + "px" : "");
        } else {
            objImg.style.width  = ((nMaxWidth <  nMaxHeight) ? nMaxWidth  + "px" : "");
            objImg.style.height = ((nMaxWidth >= nMaxHeight) ? nMaxHeight + "px" : "");
        }
        strDebug += objImg.style.width + " x " + objImg.style.height + "\n";
//        alert(strDebug);
    }
}

function em_get_browser_width()
{
    var x;

    // all except Explorer
    if (self.innerWidth) {
        x = self.innerWidth;
    // Explorer 6 Strict Mode
    } else if (document.documentElement.clientWidth) {
        x = document.documentElement.clientWidth;
    // other Explorers
    } else if (document.body) {
        x = document.body.clientWidth;
    }

    return x;
}

function em_get_browser_height()
{
    var y;

    // all except Explorer
    if (self.innerHeight) {
        y = self.innerHeight;
    // Explorer 6 Strict Mode
    } else if (document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    // other Explorers
    } else if (document.body) {
        y = document.body.clientHeight;
    }

    return y;
}

sprintfWrapper = {

    init : function () {

        if (typeof arguments == "undefined") { return null; }
        if (arguments.length < 1) { return null; }
        if (typeof arguments[0] != "string") { return null; }
        if (typeof RegExp == "undefined") { return null; }

        var string = arguments[0];
        var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
        var matches = new Array();
        var strings = new Array();
        var convCount = 0;
        var stringPosStart = 0;
        var stringPosEnd = 0;
        var matchPosEnd = 0;
        var newString = "";
        var match = null;

        while (match = exp.exec(string)) {
            if (match[9]) { convCount += 1; }

            stringPosStart = matchPosEnd;
            stringPosEnd = exp.lastIndex - match[0].length;
            strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

            matchPosEnd = exp.lastIndex;
            matches[matches.length] = {
                match: match[0],
                left: match[3] ? true : false,
                sign: match[4] || "",
                pad: match[5] || " ",
                min: match[6] || 0,
                precision: match[8],
                code: match[9] || "%",
                negative: parseInt(arguments[convCount]) < 0 ? true : false,
                argument: String(arguments[convCount])
            };
        }
        strings[strings.length] = string.substring(matchPosEnd);

        if (matches.length == 0) { return string; }
        if ((arguments.length - 1) < convCount) { return null; }

        var code = null;
        var match = null;
        var i = null;

        for (i=0; i<matches.length; i++) {

            if (matches[i].code == "%") { substitution = "%" }
            else if (matches[i].code == "b") {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == "c") {
                matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == "d") {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == "f") {
                matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == "o") {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == "s") {
                matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == "x") {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == "X") {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
            }
            else {
                substitution = matches[i].match;
            }

            newString += strings[i];
            newString += substitution;

        }
        newString += strings[i];

        return newString;

    },

    convert : function(match, nosign){
        if (nosign) {
            match.sign = "";
        } else {
            match.sign = match.negative ? "-" : match.sign;
        }
        var l = match.min - match.argument.length + 1 - match.sign.length;
        var pad = new Array(l < 0 ? 0 : l).join(match.pad);
        if (!match.left) {
            if (match.pad == "0" || nosign) {
                return match.sign + pad + match.argument;
            } else {
                return pad + match.sign + match.argument;
            }
        } else {
            if (match.pad == "0" || nosign) {
                return match.sign + match.argument + pad.replace(/0/g, " ");
            } else {
                return match.sign + match.argument + pad;
            }
        }
    }
}

sprintf = sprintfWrapper.init;

function em_mdnat(_objSelect, _bolInit)
{
    objRegion = document.getElementById("my_district");
    if (typeof(_objSelect) == "object"
    &&  typeof(objRegion) == "object") {
        if (_objSelect.selectedIndex < _objSelect.options.length) {
            // portugal
            if (_objSelect.options[_objSelect.selectedIndex].value == 163) {
                objRegion.disabled = false;
                if (!_bolInit) {
                    objRegion.selectedIndex = 0;
                }
            } else {
                objRegion.disabled = true;
                objRegion.selectedIndex = -1;
            }
        }
    }
}

/**
 * Check Profile Fields
 *
 */

function em_profile_check ()
{
    if (document.getElementById("my_suchen").value.length == 0)
    {
        alert("Please, select an option on the \"I search\" field.");
        document.getElementById("my_suchen").focus();
        return false;
    }
    return true;
}

