
function CreateXmlHttpObject() {
     var xmlhttp = false;
     if (!xmlhttp && window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlhttp=false; } }
     if (!xmlhttp && window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlhttp=false; } }
     if (!xmlhttp && window.XMLHttpRequest) { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp=false; } }
     if (!xmlhttp && window.createRequest) { try { xmlhttp = window.createRequest(); } catch (e) { xmlhttp=false; } }
     return xmlhttp;
}

var enc_challenge;
var enc_rec_timer;

function execScript(url) {
     var js = document.createElement("script");
     js.type = "text/javascript";
     js.src = url;
     var head = document.getElementsByTagName("head")[0];
     head.appendChild(js);
}

function getCh() {
     enc_rec_timer = setTimeout('errCh()',15000);
     execScript(server_url + "servlet/Auth?act=getch&gc=" + (new Date()).getTime());
}

function setCh(value) {
     clearTimeout(enc_rec_timer);
     enc_challenge = value;
     document.getElementById('retch').style.color = '#888888'
     document.getElementById('encpass').style.color = '#5778FF'
     setTimeout('encPass(0)',1)
}

function errCh() {
     showEncProg(false);
     alert('Error occurred while trying to obtain challenge code.');
}


function doPost(params) {
    if (!xmlhttp_post) return;
    xmlhttp_post.abort();
    xmlhttp_post.onreadystatechange = handlePost;
    xmlhttp_post.open("POST", "/auth/register.php", true);
    xmlhttp_post.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp_post.setRequestHeader("Content-length", params.length);
    xmlhttp_post.send(params);
}

function handlePost() {
    if (xmlhttp_post.readyState == 4) {
        if (xmlhttp_post.status == 200) {
            //success sending
            var res = xmlhttp_post.responseText;
            if (res.indexOf("SUCCESS") == 0) {
            	document.getElementById('signupdiv').style.display = 'none';
            	document.getElementById('signupsuccdiv').style.display = 'block';
            } else if (res.indexOf("CONFIRM:") == 0) {
            	document.getElementById('loginStep1').style.display = 'none';
            	document.getElementById('loginStep2').style.display = 'block';
            	var regimg = document.getElementById('regcodeimg');
            	regimg.src = 'imgs/blank.gif';
            	regimg.src = '/auth/getimage.php?rnd='+(new Date()).getTime();
            	setSignupDesc(res.substring(8,res.length));
            	document.regform.regcode.focus();
            } else {
            	setSignupDesc(res);
            }
            document.getElementById('signupbut').disabled = false;
        } else  {
            //error
            setSignupDesc("An unexpected error occurred while signing up. Please try again.")
        }
    }
}

function group_params() {
    var params = ""
    for (var i = 0; i + 1 < arguments.length; i+=2) params += arguments[i] + '=' + encodeURIComp(arguments[i+1]) + '&'
    return params;
}

//Alternative code to built-in encodeURIComponent provided by Andrew Clover via Tim Powell
function UTF8(wide) {
    var c, s;
    var enc = "";
    var i = 0;
    while (i < wide.length) {
        c = wide.charCodeAt(i++);
        // handle UTF-16 surrogates
        if (c >= 0xDC00 && c < 0xE000) continue;
        if (c >= 0xD800 && c < 0xDC00) {
            if (i >= wide.length) continue;
            s = wide.charCodeAt(i++);
            if (s < 0xDC00 || c >= 0xDE00) continue;
            c = ((c - 0xD800) << 10) + (s - 0xDC00) + 0x10000;
        }
        // output value
        if (c < 0x80) enc += String.fromCharCode(c);
        else if (c < 0x800) enc += String.fromCharCode(0xC0 + (c >> 6), 0x80 + (c & 0x3F));
        else if (c < 0x10000) enc += String.fromCharCode(0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
        else enc += String.fromCharCode(0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
    }
    return enc;
}

var hexchars = "0123456789ABCDEF";
function toHex(n) {
    return hexchars.charAt(n >> 4) + hexchars.charAt(n & 0xF);
}

function encodeURIComp(s) {
    if (typeof encodeURIComponent == "function") return encodeURIComponent(s); // use built-in function instead if available
    var enc = "";
    var c;
    s = UTF8(s);
    for (var i = 0; i < s.length; i++) {
        c = s.charCodeAt(i); //valid URI chars: a-zA-Z0-9_-
        enc += ((c >= 97 && c <= 122) || (c >= 65 && c <= 90) || (c >= 48 && c <= 57) || (c == 45) || (c == 95)) ? s.charAt(i) : "%" + toHex(s.charCodeAt(i));
    }
    return enc;
}
