﻿function selectavatar(img) {
    var selectedimg = document.getElementById("ContentPlaceHolder1_hfiAvatarID").value;

    if (selectedimg != "") {
        document.getElementById(selectedimg).style.border = "0px solid #cccccc";
    }

    img.style.border = "2px solid #cccccc";
    document.getElementById("ContentPlaceHolder1_hfiAvatarID").value = img.id;
    document.getElementById("ContentPlaceHolder1_hfiAvatar").value = img.src;
}

function encrypt(img, png) {
    document.getElementById(img).src = 'Images/menu/' + png;
}

function decrypt(img, png) {
    document.getElementById(img).src = 'Images/menu/' + png;
}

function colapse(divtag, imgtag) {
    if (document.getElementById(divtag).style.display == '') {
        document.getElementById(divtag).style.display = 'none';
        document.getElementById(imgtag).src = 'Images/box/colapsed.png';
        document.getElementById(imgtag).alt = 'Colapsed';
    }
    else {
        document.getElementById(divtag).style.display = '';
        document.getElementById(imgtag).src = 'Images/box/expanded.png';
        document.getElementById(imgtag).alt = 'Expanded';
    }
}

dateFuture1 = new Date(2011, 11, 20, 0, 0, 0);
dateFuture2 = new Date(2011, 11, 13, 4, 0, 0);

function GetCount(ddate, iid) {

    dateNow = new Date();
    amount = ddate.getTime() - dateNow.getTime();
    delete dateNow;

    if (amount < 0) {
        document.getElementById(iid).innerHTML = "Now!";
    }
    else {
        days = 0; hours = 0; mins = 0; secs = 0; out = "";

        amount = Math.floor(amount / 1000);

        days = Math.floor(amount / 86400);
        amount = amount % 86400;

        hours = Math.floor(amount / 3600);
        amount = amount % 3600;

        mins = Math.floor(amount / 60);
        amount = amount % 60;

        secs = Math.floor(amount);

        if (days != 0) { out += days + " " + ((days == 1) ? "day" : "days") + ", "; }
        if (hours != 0) { out += hours + " " + ((hours == 1) ? "hour" : "hours") + ", "; }
        out += mins + " " + ((mins == 1) ? "min" : "mins") + ", ";
        out += secs + " " + ((secs == 1) ? "sec" : "secs") + ", ";
        out = out.substr(0, out.length - 2);
        document.getElementById(iid).innerHTML = out;

        setTimeout(function () { GetCount(ddate, iid) }, 1000);
    }
}

var x = (screen.availWidth / 2) - 150;
var y = (screen.availHeight / 2) - 200;

function messagebox(title, message, buttons) {

    if (document.getElementById('divMessageBox').style.display == 'none') {

        if (document.getElementById('divMessageBox').style.left == '') {
            document.getElementById('divMessageBox').style.left = x + 'px';
        }
        if (document.getElementById('divMessageBox').style.top == '') {
            document.getElementById('divMessageBox').style.top = y + 'px';
        }
        if (document.all) {
            document.getElementById('divMessageBoxTitle').innerText = title;
            document.getElementById('divMessageBoxMsg').innerText = message;
        } else {
            document.getElementById('divMessageBoxTitle').textContent = title;
            document.getElementById('divMessageBoxMsg').textContent = message;
        }

        document.getElementById('divMessageBox').style.display = '';

        javascript: scroll(0, 0);
    }

    return false;
}

function validateform(button) {
    if (button == "confirm") {
        if (document.getElementById('ContentPlaceHolder1_txtConfirm').value == '') {
            return messagebox('Alert', 'You must enter your Confirmation Code!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtConfirm').value != document.getElementById('ContentPlaceHolder1_hfiConfirm').value) {
            return messagebox('Alert', 'Your Confirmation Code does not match your application Confirmation Code!', 'ok');
            return false;
        }
    }
    if (button == "login") {
        if (document.getElementById('txtLoginUsername').value == '') {
            return messagebox('Alert', 'You must enter your TSO Username!', 'ok');
            return false;
        }
        if (document.getElementById('txtLoginPassword').value == '') {
            return messagebox('Alert', 'You must enter your TSO Password!', 'ok');
            return false;
        }
    }
    if (button == "verify") {
        if (document.getElementById('ContentPlaceHolder1_txtSWTORUsername').value == '') {
            messagebox('Alert', 'You must enter your SWTOR username!', 'ok');
            return false;
        }
    }
    if (button == "apply") {
        var iframe = document.getElementById('ContentPlaceHolder1_ifmVerifyMe');
        if (document.getElementById('ContentPlaceHolder1_txtSWTORUsername').value == '') {
            messagebox('Alert', 'You must enter your SWTOR username!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtTSOUsername').value == '') {
            messagebox('Alert', 'You must enter your TSO username!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtTSOPassword').value == '') {
            messagebox('Alert', 'You must enter your TSO password!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtTSOVerifyPassword').value == '') {
            messagebox('Alert', 'You must enter your TSO verification password!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtTSOVerifyPassword').value != document.getElementById('ContentPlaceHolder1_txtTSOPassword').value) {
            messagebox('Alert', 'Your verification password does not match your TSO password!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtEmail').value == '') {
            messagebox('Alert', 'You must enter your email!', 'ok');
            return false;
        }
        if (!document.getElementById('ContentPlaceHolder1_txtEmail').value.match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')) {
            messagebox('Alert', 'Your email format is incorrect!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtVerifyEmail').value == '') {
            messagebox('Alert', 'You must enter your verification email!', 'ok');
            return false;
        }
        if (!document.getElementById('ContentPlaceHolder1_txtVerifyEmail').value.match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')) {
            messagebox('Alert', 'Your verification email format is incorrect!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtVerifyEmail').value != document.getElementById('ContentPlaceHolder1_txtEmail').value) {
            messagebox('Alert', 'Your verification email does not match your email!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtFirstName').value == '') {
            messagebox('Alert', 'You must enter your first name!', 'ok');
            return false;
        }
        if (iframe.contentWindow.document.getElementById('txtVerifyMe').value == '') {
            messagebox('Alert', 'You must enter the verification code!', 'ok');
            return false;
        }
        if (iframe.contentWindow.document.getElementById('txtVerifyMe').value != decode64(iframe.contentWindow.document.getElementById('hfiVerifyMe').value)) {
            messagebox('Alert', 'The verification code does not match!', 'ok');
            return false;
        }
    }
    if (button == "commlink") {
        if (document.getElementById('ContentPlaceHolder1_txtCommlink_Name').value == '') {
            messagebox('Alert', 'You must enter your name!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtCommlink_Email').value == '') {
            messagebox('Alert', 'You must enter your email!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtCommlink_Subject').value == '') {
            messagebox('Alert', 'You must enter the subject!', 'ok');
            return false;
        }
        if (document.getElementById('ContentPlaceHolder1_txtCommlink_Message').value == '') {
            messagebox('Alert', 'You must enter the message!', 'ok');
            return false;
        }
    }
}

var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

function decode64(input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    var base64test = /[^A-Za-z0-9\+\/\=]/g;
    if (base64test.exec(input)) {
        alert("There were invalid base64 characters in the input text.\n" +
              "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
              "Expect errors in decoding.");
    }
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < input.length);

    return unescape(output);
}


