
var g_focusElement = null;

function focusOn(o)
{
    g_focusElement = o.id;
}

function clearfocus(o)
{
    g_focusElement = null;
}

function handleSubmit(e) {
    if (e.keyCode == "13") {
        var element = e.target;
        var id = element.id;
        alert(id);
        if (id == "loginemail" || id == "loginpw") {
            validatelogin();
        }
        else if (id == "login2email" || id == "login2password") {
            validatelogin2();
        }
        else {
            validateform();
        }
    }
    return false;
}

function validatelogin()
{
    $("loginerr").innerHTML = "";

    var u = trim($("loginemail").value);
    var p = trim($("loginpw").value);

    $("loginerr").innerHTML = "<img src=\"/sysimages/workingwheel.gif\" width=\"14\" height=\"14\" >";

    return new AJAXRequest("post", "x_login.asp", "op=login&u="+encode(u)+"&p="+encode(p)+"&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("loginerr").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    $("loginerr").innerHTML = d.status;
                    if (d.status == "") {
                        location.href = "http://www.myclasspro.com/affiliates/account.asp?sid=" + d.sid;
                    }
                }
            }
        }, true);


    return true;
}

function validatelogin2()
{
    $("login2err").innerHTML = "";

    var u = trim($("login2email").value);
    var p = trim($("login2password").value);

    $("login2err").innerHTML = "<img src=\"/sysimages/workingwheel.gif\" width=\"14\" height=\"14\" >";

    return new AJAXRequest("post", "x_login.asp", "op=login&u="+encode(u)+"&p="+encode(p)+"&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("login2err").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    if (d.status == "") {
                        location.href = "http://www.myclasspro.com/affiliates/account.asp?sid=" + d.sid;
                    }
                    else {
                        $("login2err").innerHTML = d.status;
                    }
                }
            }
        }, true);


    return true;
}

function forgotpassword()
{
    if (g_focusElement) {
        validatelogin();
        return true;
    }

    $("forgoterr").innerHTML = "";

    var u = trim($("forgotemail").value);

    $("forgoterr").innerHTML = "<img src=\"/sysimages/workingwheel.gif\" width=\"14\" height=\"14\" >";

    return new AJAXRequest("post", "x_login.asp", "u="+encode(u)+"&op=fp&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("forgoterr").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    $("forgoterr").innerHTML = d.status;
                    $("forgotemail").value = "";
                }
            }
        }, true);


    return true;
}

function createaccount()
{
    var p = getFormValues("registrationform");

    $("signuperr").innerHTML = "<img src=\"/sysimages/workingwheel.gif\" width=\"14\" height=\"14\" >";

    return new AJAXRequest("post", "x_login.asp", p + "&op=create&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("signuperr").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    if (d.status != "") {
                        $("signuperr").innerHTML = d.status;
                    }
                    else {
                        //location.href = "http://www.myclasspro.com/affiliates/account.asp?sid=" + d.sid;
                        location.href = "http://www.myclasspro.com/affiliates/welcome.asp?sid=" + d.sid;
                    }
                }
            }
        }, true);


    return true;
}

function clearpayment()
{
    $("paymentid").value=""
    $("paymentamount1").value="";
    $("paymenttype1").selectedIndex = 0;
    $("transactionid1").value = "";

    $("paymentamount2").value="";
    $("paymenttype2").selectedIndex = 0;
    $("transactionid2").value = "";
}

function deletepayment(o)
{
    $("paymentstatus").innerHTML = "";

    if (! confirm("Are you sure you want to delete this payment?\n\nThis operation cannot be undone")) {
        return true;
    }

    $("paymentid").value=o.recno;

    var p = getFormValues("paymentform");

    return new AJAXRequest("post", "/affiliates/x_payment.asp", p + "&op=delete&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("paymentstatus").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    $("paymentstatus").innerHTML = d.status;
                    if (d.status == "") {
                        $("paymentlistarea").innerHTML = d.payments;
                    }
                    $("paymentbalance").innerHTML = d.balance;
                    clearpayment();
                    yyy();
                }
            }
        }, true);

    return true;
}

function makepayment()
{
    $("paymentstatus").innerHTML = "";

    var p = getFormValues("paymentform");

    return new AJAXRequest("post", "/affiliates/x_payment.asp", p + "&op=make&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("paymentstatus").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {

                    var d = eval ( "(" + AJAX.responseText + ")" );
                    $("paymentstatus").innerHTML = d.status;
                    if (d.status == "") {
                        $("paymentlistarea").innerHTML = d.payments;
                    }
                    $("paymentbalance").innerHTML = d.balance;
                    clearpayment();
                    yyy();
                }
            }
        }, true);

    return true;
}

function updatepayment()
{
    $("paymentstatus").innerHTML = "";

    var p = getFormValues("paymentform");

    return new AJAXRequest("post", "/affiliates/x_payment.asp", p + "&op=update&z="+Math.floor(Math.random()*10000),
        function ( AJAX )
        {
            if (AJAX.readyState == 4) {
                if (AJAX.status !=200) {
                    $("paymentstatus").innerHTML = "system error.  please try again later";
                }

                if (AJAX.status == 200) {
                    var d = eval ( "(" + AJAX.responseText + ")" );
                    $("paymentstatus").innerHTML = d.status;
                    if (d.status == "") {
                        $("paymentlistarea").innerHTML = d.payments;
                    }
                    $("paymentbalance").innerHTML = d.balance;
                    clearpayment();
                    togglepayment();
                    yyy();
                }
            }
        }, true);

    return true;

}

function cancelpayment()
{
    togglepayment();
}

function editpayment(o)
{
    $("paymentid").value=o.recno;
    $("paymentamount2").value = o.amt;
    $("transactionid2").value = o.tid;

    if (parseInt(o.pt) == 0) {
        $("paymenttype2").selectedIndex = 1;
    }
    else if (parseInt(o.pt) == 1) {
        $("paymenttype2").selectedIndex = 2;
    }
    else {
        $("paymenttype2").selectedIndex = 0;
    }

    togglepayment();
}

function togglepayment()
{
    if($("epf").style.display=="none") {
        $("epf").style.display="block";
        $("mpf").style.display="none";
    }
    else {
        $("mpf").style.display="block";
        $("epf").style.display="none";
    }
}

function yyy()
{
    var id = "";
    var editid = "";
    var att = "";

    var tags = $("paymentlistarea").getElementsByTagName("SPAN");
    for (var i = 0; i < tags.length; i++) {
        if (tags[i].getAttribute("dtag")) {
            id = tags[i].getAttribute("dtag");
            tags[i].recno = id
            tags[i].style.cursor = "pointer";
            tags[i].innerHTML = "<img style=\"position:relative;top:5px;\" src=\"sysimages/delete_x.gif\" width=\"18\" height=\"18\" />";
            tags[i].onclick = function() { deletepayment(this); return false; };
        }

        if (tags[i].getAttribute("etag")) {
            editid = tags[i].getAttribute("etag");
            tags[i].recno = editid

            att = tags[i].getAttribute("atag").split("|");
            tags[i].pt = att[0];
            tags[i].amt = att[1];
            tags[i].tid = att[2];

            tags[i].style.cursor = "pointer";
            tags[i].innerHTML = "<img style=\"position:relative;top:5px;\" src=\"sysimages/edithelp_clr.gif\" width=\"8\" height=\"18\" />";
            tags[i].onclick = function() { editpayment(this); return false; };
        }
    }
}

function toggle(id)
{
    if (document.getElementById(id).style.display == 'none') {
        document.getElementById(id).style.display = 'block';
    }
    else {
        document.getElementById(id).style.display = 'none';
    }
}


