var theDate = new Date();
var end, t1, t2, t3, t4, t5, t6, t7, t8, t8, t0;
var SID;
var timer;
var timer_should_run = 1;

function setSID(id) {
    SID = id;
}

function setExpire(numSeconds) {
    theDate = new Date();
    end = theDate.getTime() + (numSeconds * 1000);
    timer_should_run = 1;
}

function doHTMLTime() {
    if (timer_should_run) {
        var now = new Date();
        try {
            var expiry = Math.round((end - (now.getTime())) / 1000);
            if (expiry <= 0) {
                timer_should_run = 0;
                location.href = "/timeout.php?" + SID;
            }
            if (isNaN(expiry)) {
                document.getElementById("thetimer").innerHTML = "--:--";
                timer_should_run = 0;
            } else {
                var min = Math.floor(expiry / 60);
                var minLeft = Math.floor(min / 10);
                var minRight = Math.floor(min % 10);
                var sec = Math.floor(expiry % 60);
                var secLeft = Math.floor(sec / 10);
                var secRight = Math.floor(sec % 10);
                document.getElementById("thetimer").innerHTML = "" + minLeft + minRight + ":" + secLeft + secRight;
            }
        } catch (error) {
        }
        timer = window.setTimeout("doHTMLTime();", 1000);
    }
}

function stopHTMLTime() {
    try {
        document.getElementById("thetimer").innerHTML = "--:--";
        window.clearTimeout(timer);
    } catch (error) {
    }
}

function doGoing() {
    var now = new Date();
    var text = document.getElementById("going").innerHTML;
    if (text.length >= 30) {
        text = " . ";
    } else {
        text += " . ";
    }
    document.getElementById("going").innerHTML = text;
    newgoing = window.setTimeout("doGoing();", 1000);
}

var showMode = 'table-cell';
if (document.all) showMode='block';
function toggleVis(thaID, show) {
    cells = document.getElementsByName(thaID);
    if (show) mode = showMode;
    else mode = 'none';
    for(j = 0; j < cells.length; j++) cells[j].style.display = mode;
}

function passwordCheck(passwordForm) {
    var desc = new Array();
    desc[0] = "Very Weak";
    desc[1] = "Weak";
    desc[2] = "Better";
    desc[3] = "Medium";
    desc[4] = "Strong";
    desc[5] = "Strongest";

    var score = 0;

    var password = passwordForm.password.value;
    var passwordConfirm = passwordForm.password2.value;

    // if password bigger than 0 give 1 point
    if (password.length > 0) score++;

    // if password bigger than 7 give 1 point
    if (password.length >= 8) score++;

    // if password has lowercase characters give 1 point
    if ( password.match(/[a-z]/) ) score++;

    // if password has uppercase characters give 1 point
    if ( password.match(/[A-Z]/) ) score++;

    // if password has at least one number give 1 point
    if (password.match(/\d+/)) score++;

    for ( var i = 1; i <= 5; i++) {
        if (score >= 5)
            document.getElementById("passwordMeter" + i).className = "strength5";
        else if (i <= score)
            document.getElementById("passwordMeter" + i).className = "strength" + i;
        else
            document.getElementById("passwordMeter" + i).className = "strength" + 0;
    }

    if (password.length > 0 && (score < 5 || password != passwordConfirm || passwordForm.name == 'changePassword' && passwordForm.oldPassword.value.length < 1)) {
        passwordForm.go.disabled = true;
    } else {
        passwordForm.go.disabled = false;
    }
}

$(document).ready(function() {
    if ($("#eventCategories a").length) {
        Categories.init();
    } else {
        $("#lookingFor").remove();
    }
});

var Categories = {
    init : function() {
        this.initEventCategories();
        this.initParseHash();
    },
    initEventCategories : function() {
        var validnav = [];
        /*  Gather a list of event categories displayed on the page 
         * so you don't have empty navigation categories displayed
         */
        $("tr[class^='eventcat_']").each(function() {
            var myclass = "#" + $(this).attr("class");
            validnav.push(myclass);
            if ($(this).attr('class') != 'eventcat_' && typeof $(myclass).attr("parent_id") != "undefined") {
                validnav.push("#" + $("#eventCategories a[category_id='" + $(myclass).attr("parent_id") + "']").attr("id"));
            }
        });
        $(validnav.join()).addClass("valid");
        $("#eventCategories a").each(function() {
            if (!$(this).hasClass("valid")) {
                $(this).remove();
            }
            $(this).click(function() {
                if (!$(this).hasClass("active")) {
                    $("tr[class^='eventcat_']").hide();
                    $("." + this.id).show();
                    $("#eventCategories a").removeClass("active");
                    $("#eventCategoriesChildren a[parent_id!=" + $(this).attr("parent_id") + "]").hide();
                    $("#eventCategoriesChildren a[parent_id=" + $(this).attr("category_id") + "]").each(function() {
                        $("." + this.id).show();
                    }).show();
                    $(this).addClass("active");
                    document.location.hash = $(this).attr("href");
                }
                else {
                    $("#eventCategoriesChildren a[parent_id!=" + $(this).attr("parent_id") + "]").hide();
                    if (typeof $(this).attr("parent_id") != "undefined") {
                        $("#eventCategoriesChildren a[parent_id=" + $(this).attr("parent_id") + "]").hide();
                    }
                    $("table.eventOnSale tr").show();
                    $("#eventCategories a").removeClass("active");
                    document.location.hash = '';
                }
                return false;
            });
        });
        // If no categories are showing remove the "looking for" message
        if ($("#eventCategories a").length == 0) {
            $("#lookingFor, #eventCategories").remove();
        }
    },
    initParseHash : function() {
        var hash = document.location.hash.replace(/^\#/, "");
        // If this is a child category show this and any sibling elements with the same parent
        if (typeof $("#eventcat_" + hash).attr("parent_id") != "undefined") {
            $("#eventCategoriesChildren a[parent_id=" + $("#eventcat_" + hash).attr("parent_id") + "]").show();
        }
        $("#eventcat_" + hash).click();
    }
};
