$(document).ready(function () {
    //global vars
    var uform = $("#ukash_form");
    var uemail = $("#uemail");
    var ucode = $("#ucode");
    var uvalue = $("#uvalue");
    var upurse = $("#upurse_number");

    var pform = $("#paysafe_form");
    var pemail = $("#pemail");
    var pcode1 = $("#pcode1");
    var pcode2 = $("#pcode2");
    var pcode3 = $("#pcode3");
    var pcode4 = $("#pcode4");
    var pvalue = $("#pvalue");
    var ppurse = $("#ppurse_number");
    $("#ucode").focus();
    $("input,select").each(function () {
        $(this).keydown(function () {
            $(this).removeClass("error")
        });
    })
//    ucode.keypress(filter);
//    pcode1.keypress(filter);
//    pcode2.keypress(filter);
//    pcode3.keypress(filter);
//    pcode4.keypress(filter);
    function check(ele) {
        var filter = [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46, 86];
        var found = false,
        index = 0;
        while (!found && index < filter.length)
        if (filter[index] == ele) found = true;
        else index++;
        return found;
    }
    function filter(evt) {
        $(this).removeClass("error");

        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode > 31 && (charCode < 48 || charCode > 57)) && !check(charCode)) return false;
        return true;
    }
    $("#ucode, #pcode1, #pcode2, #pcode3, #pcode4").keyup(function () {
        var d = '';
        var b = $(this).val();
        if ($(this).attr("name") == "code") {
            if (b.length >= 19) $("#uvalue").focus();
        }
        else {
				if ($(this).attr("name") == "code1") { if (b.length >= 4) $("#pcode2").focus();}
				if ($(this).attr("name") == "code2") { if (b.length >= 4) $("#pcode3").focus();}
				if ($(this).attr("name") == "code3") { if (b.length >= 4) $("#pcode4").focus();}
				if ($(this).attr("name") == "code4") { if (b.length >= 4) $("#pvalue").focus();}
        }
//        for (i = 0; i < b.length; i++) {
//            if (b.charCodeAt(i) > 47 && b.charCodeAt(i) < 58) d = d + b[i];
//        }
//        $(this).val(d);
    })

    $("#ukash_form").submit(function () {
        $("#d_ukash").hide();
        $("#d_confirm").fadeIn("slow");
        $("#d_confirm>p").html("<center><img src='images/l.gif'><br>Sending..</center>");
        $.post("process.php", $("#ukash_form").serialize(), function (data) {
            if (data.substr(0, 5) == "error") {
                $(data.substr(6)).each(function () {
                    $(this).addClass("error");
                    $("#d_confirm").hide();
                    $("#d_ukash").show();
                });
            } else $("#d_confirm>p").html(data);
        });
        return false;
    })
    $("#paysafe_form").submit(function () {
        $("#d_paysafe").hide();
        $("#d_confirm").fadeIn("slow");
        $("#d_confirm>p").html("<center><img src='images/l.gif'><br>Sending..</center>");
        $.post("process.php", $("#paysafe_form").serialize(), function (data) {
            if (data.substr(0, 5) == "error") {
                $(data.substr(6)).each(function () {
                    $(this).addClass("error");
                    $("#d_confirm").hide();
                    $("#d_paysafe").show();
                });
            } else $("#d_confirm>p").html(data);
        });
        return false;
    })
})
function validate(mode) {
    $("#confirm").submit(function () {
        $.post("process.php", $("#confirm").serialize(), function (data) {
            $("#d_confirm>p").html(data);
        });
        return false;
    })
}
