function validate_username(username) {
    $.ajax({
            url: "/_ajax/validate_username.php",
            type: "POST",
            data: ({username : username}),
            dataType: "html",
            success: function(html){
                //alert(html);
                $("p#err_username").html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //alert(textStatus);
            }
    });
}

function validate_password(password) {
    $.ajax({
            url: "/_ajax/validate_password.php",
            type: "POST",
            data: ({password : password}),
            dataType: "html",
            success: function(html){
                //alert(html);
                $("p#err_password").html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //alert(textStatus);
            }
    });
}

function validate_password_match(password, repeat_password) {
    $.ajax({
            url: "/_ajax/validate_password_match.php",
            type: "POST",
            data: ({password : password, repeat_password : repeat_password}),
            dataType: "html",
            success: function(html){
                //alert(html);
                $("p#err_password_repeat").html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //alert(textStatus);
            }
    });
}

function validate_email(email) {
    $.ajax({
            url: "/_ajax/validate_email.php",
            type: "POST",
            data: ({email : email}),
            dataType: "html",
            success: function(html){
                //alert(html);
                $("p#err_email").html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //alert(textStatus);
            }
    });
}

function buy_ticket(ticket_number) {
    $.ajax({
            url: "/_ajax/buy_ticket.php",
            type: "POST",
            data: ({ticket_number : ticket_number, jackpot_mode: $("input#jackpot_mode").attr("value")}),
            dataType: "xml",
            success: function(xml){
                //alert(xml);
                var reload = $(xml).find("reload").text();
                if(reload == 1) {
                    document.location.href = document.URL;
                }
                var validation_report = $(xml).find("validation_report").text();
                if(validation_report) {
                    $("div.validator_report_off").html(validation_report);
                    $("div.validator_report_off").css("display", "inline");
                }else {
                    $("div.validator_report_off").css("display", "none");
                }
                $("td#tickets_left").html($(xml).find("tickets_left").text());
                $("span#balance").html($(xml).find("balance").text());
                var number = "";
                var cls = "";
                var td_id = "";
                $(xml).find("ticket").each(function() {
                    number = $(this).find("number").text();
                    cls = $(this).find("class").text();
                    $("td#ticket" + number).attr("class", cls);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //alert(textStatus);
            }
    });
}

function checkLogin(login_focused, password_focused) {
    if(login_focused) {
        $("input#roof_login").attr("class", "text_login");
    } else {
        if($("input#roof_login").attr("value") == "") {
            $("input#roof_login").attr("class", "text_login_empty");
        } else {
            $("input#roof_login").attr("class", "text_login");
        }
    }
    if(password_focused) {
        $("input#roof_password").attr("class", "text_password");
    } else {
        if($("input#roof_password").attr('value') == "") {
            $("input#roof_password").attr("class", "text_password_empty");
        } else {
            $("input#roof_password").attr("class", "text_password");
        }
    }
}

jQuery.fn.secCountdown = function() {
        var counter = this;
        $.timer(1000, function(timer) {
            timeleft = $(counter).text();
            len = timeleft.length;
            s = parseInt(timeleft.substr(len - 2, 2), 10);
            m = parseInt(timeleft.substr(len - 5, 2), 10);
            h = parseInt(timeleft.substr(0, len - 6), 10);
            if(s == 59) {
                if(m == 59) {
                    if(h == 23) {
                        //do postback
                        //$("form:first").submit();
                        //location.reload();
                        //document.location.href = document.URL;
                        h = 0;
                        m = 0;
                        s = 0;
                    }else {
                        h = h + 1;
                        m = 0;
                        s = 0;
                    }
                }else {
                    m = m + 1;
                    s = 0;
                }
            }else {
                s = s + 1;
            }
            
            if(s < 10) {
                s = '0' + s;
            }
            if(m < 10) {
                m = '0' + m;
            }
            if(h < 10) {
                h = '0' + h;
            }
            
            timeleft = h + ':' + m + ':' + s;

            $(counter).text(timeleft);
            //timer.stop();
        });
};