$(document).ready(function() {
setLoginTimeout()
});
onerror=handleErr
function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
//alert(txt)
return true
}
 var timeoutTimer = null;
 var isTimedOut = true;
 var TIMEOUT_TIME = 900000; //Time in miiliseconds
function setLoginTimeout()
{
    timeoutTimer = setTimeout(onTimeout, TIMEOUT_TIME);
    isTimedOut = false;
}
function onTimeout() {
     isTimedOut = true;
 }
 
  function onWarning() 
 {
//alert("Session is going to time out in 1 minute");
 }
 function renewSession()
{
  //clearInterval(warningTimer);
  clearInterval(timeoutTimer);
  //warningTimer = setTimeout(onWarning, WARNING_TIME);
  $.post("/YouthZone/account.mvc/KeepSessionAlive");
  timeoutTimer = setTimeout(onTimeout, TIMEOUT_TIME);
}
function checkTimedOut() {
    if (isTimedOut && timeoutTimer) {
        window.location = "http://www.myherefordshire.com";
        return false;
    }
    else {
        renewSession();
        return true;
    }
}
 
 

function clear(obj, DefaultVal) {
    $("#" + obj.id).unbind('focus');
    $("#" + obj.id).css({ backgroundColor: "white", opacity: "1" });
    if ($("#" + obj.id).val() == DefaultVal) {
        $("#" + obj.id).val("");
    }
}
function check(obj, DefaultVal, isMandatory) {
    if ($("#" + obj.id).val().length == 0) {
        if (isMandatory == "true") {
            $("#" + obj.id).css({ backgroundColor: "#EE3E3E", opacity: ".6" });
        }
        else {
            $("#" + obj.id).css({ backgroundColor: "#dfd", opacity: ".6" });
        }
        $("#" + obj.id).val(DefaultVal)
        $("#" + obj.id).bind('focus', function(e) { clear(obj, DefaultVal); });
    }
}
function SearchUsers() {
    if (($("#Email").val().length > 2 && $("#Email").val() != "Enter Email")
    || ($("#Surname").val().length > 2 && $("#Surname").val() != "Enter Surname")
    || ($("#FirstName").val().length > 2 && $("#FirstName").val() != "Enter First Name")) {
        var Rand = Math.floor(Math.random() * 99999999999999);
        var FirstName = $("#FirstName").val();
        var Surname = $("#Surname").val();
        var Email = $("#Email").val();
        var YZOnly = $("#chkYZOnly").attr('checked');
        if (checkTimedOut()) {
            $.get('/YouthZone/admin.mvc/AjaxGetUsers', { rand: Rand, FirstName: FirstName, Surname: Surname, Email: Email, YZOnly: YZOnly }, function(data) {
                $("#UserList").html(data);
            });
        }
    }
}
function SelectUser(User) {
    $(".UserRows").css({ backgroundColor: "#0052A4" });
    $("#tr" + User).css({ backgroundColor: "#D5E131" });
    var Rand = Math.floor(Math.random() * 99999999999999);
    if (checkTimedOut()) {
        $.getJSON('/YouthZone/admin.mvc/AjaxGetUser', { rand: Rand, UserId: User }, function(data) {
            $("#lblName").html(data._firstName + " " + data._surname + "<br />");
            $("#lblEmail").html(data._email);
            $("lblDateCreated").html(data._dateCreted);
            $("#txtMusicQuota").val(data._musicQuota);
            $("#Save").unbind("click");
            $("#Save").bind('click', function(e) { UpdateUser(User); return false; });
            if (data._role == 'none') {
                $("input[@name='rdoRoles']").removeAttr("checked", "checked");
            }
            else {
                $("#" + data._role).attr("checked", "checked");
            }
            $("#ddlStatus").val(data._status)
            $("#UpdateUser").show();
            try {
                window.loadFirebugConsole();
                console.log(data);
            }
            catch (ex)
        { }
        });
    }
}
function UpdateUser(User) {
    var Status = $("#ddlStatus").val();
    var Role = $("input[@name='rdoRoles']:checked")[0].id;
    var MusicQuota = $("#txtMusicQuota").val();
    var JSONUser = "{\"_userId\":\"" + User + "\",\"_role\":\"" + Role + "\",\"_status\":\"" + Status + "\",\"_musicQuota\":\"" + MusicQuota + "\"}}";
    var Rand = Math.floor(Math.random() * 99999999999999);
    if (checkTimedOut()) {
        $.get('/YouthZone/admin.mvc/UpdateUser', { rand: Rand, JSON: JSONUser }, function(data) {
            //$("#UserList").html(data);
        });
    }
    return false;
}
function isEmail(email) {
    var emailVal = $(email).val();
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var isValid = emailRegEx.test(emailVal);
    if (isValid) {
        $(email).css({ backgroundColor: "white", opacity: "1" });
        return isValid;
    }
    else {
        $(email).css({ backgroundColor: "#EE3E3E", opacity: ".6" });
        return isValid;
    }
}
function isPostcode(postcode) {
    var postcodeVal = $(postcode).val();
    var postcodeRegEx = /[A-Z]{1,2}[A-Z0-9]{1,2} ?[0-9][A-Z]{2}/i;
    var isValid = postcodeRegEx.test(postcodeVal);
    if (isValid) {
        $(postcode).css({ backgroundColor: "white", opacity: "1" });
        return isValid;
    }
    else {
        $(postcode).css({ backgroundColor: "#EE3E3E", opacity: ".6" });
        return isValid;
    }
}