var old = null;
function mouseOverWithDisplayNone(id){
    if(old){
        document.getElementById(old).style.display = "none";
    }
    if(document.getElementById(id)){
        document.getElementById(id).style.display = "block";
        old = id;
    }
}
function mouseOutWithDisplayNone(id){
    if(document.getElementById(id)){
        document.getElementById(id).style.display = "none";
    }
}

function validIntranet(){
    var form    = document.loginIntranet;
    var valid   = true;

    AtPos   = form.loginnaam.value.indexOf("@");
    StopPos = form.loginnaam.value.lastIndexOf(".");
    if(form.loginnaam.value=="" || (StopPos == -1 || AtPos == -1 || StopPos < AtPos)){
        alert("Please fill in a valid login.");
        valid = false;
    }
    if(form.wachtwoord.value=="" && valid){
        alert("Please fill in a password.");
        valid = false;
    }

    if(valid){
        form.action = "http://intranet.celsusdrinks.com/custom/login.php";
        form.method = "post";
        form.submit();
    }
}
function validChange(){
    var form    = document.change;
    var valid   = true;

    if(form.old_password.value == "" && valid){
        alert("Please fill in the current password. ");
        valid   = false;
    }
    if(form.new_password.value == "" && valid){
        alert("Please fill in the new password.");
        valid   = false;
    }
    if(form.confirm_password.value == "" && valid){
        alert("Please fill in repeat the new password.");
        valid   = false;
    }

    if(valid){
        form.action = "/custom/change.php";
        form.method = "post";
        form.submit();
    }
}
function validForget(){
    var form    = document.forget;
    var valid   = true;

    AtPos   = form.email.value.indexOf("@");
    StopPos = form.email.value.lastIndexOf(".");
    if(form.email.value=="" || (StopPos == -1 || AtPos == -1 || StopPos < AtPos)){
        alert("Please fill in a correct e-mail address. ");
        valid = false;
    }

    if(valid){
        form.action = "/custom/forget.php";
        form.method = "post";
        form.submit();
    }
}

function tracerValid(redirect,_form, message){
    var valid   = true;
    var form    = document[_form];
    var code    = trim(form.tracecode.value);
    var pieces  = code.split("");

    if(pieces.length == 10){
        for(var i=0;i<pieces.length;i++){
            if((pieces[i]/1)!=(parseInt(pieces[i])/1)){
                valid = false;
            }
        }
    }
    else{
        valid = false;
    }

    if(valid){
        url = base64_encode("/xml/batch/"+code);
        document.location = redirect+"?tc="+url;
    }
    else{
        alert(message);
    }
}

function base64_encode (data) {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];

    if (!data) {
        return data;
    }

    data = this.utf8_encode(data+'');

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    enc = tmp_arr.join('');

    switch (data.length % 3) {
        case 1:
            enc = enc.slice(0, -2) + '==';
            break;
        case 2:
            enc = enc.slice(0, -1) + '=';
            break;
    }
    return enc;
}
function utf8_encode ( argString ) {
    var string = (argString+'');
    var utftext = "";
    var start, end;
    var stringl = 0;
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}
function trim (str, charlist) {
    var whitespace, l = 0, i = 0;
    str += '';

    if (!charlist) {
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }

    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }

    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
