$(document).ready(function() {
    loginanim = $("<img alt='Please Wait: Logging in.'>").attr("src", "/images/ajax-loader.gif");
    loadinganim = $("<img alt='Please Wait: Redirecting to member area'>").attr("src", "/images/ajax-loader2.gif");
    
    if (hasCSS()){
        $('#logincontainer').prepend('<div id="logindialog" class="hidden"><div id="loginform"><div id="logintitle">Please enter your details</div><div id="loginfields"><div class="field"><label for="txtusrname">Username</label><input type="text" id="txtusrname" /></div><div class="field"><label for="txtpasswd">Password</label><input type="password" id="txtpasswd"" /></div><a id="loginbtn" class="button-a" href="#" ><span>Log in</span></a><a class="button-a" id="cancelbtn" href="#" ><span>Cancel</span></a></div><div id="msg"></div><a href="/forgotten.aspx" id="forgottenlink">Forgotten Password</a></div></div>');
        $('#cancelbtn').bind("click", cancel);
        $('#loginbtn').bind("click", login);
        
        $('#memberlogin').bind("click", showlogin);
        $('#memberlogout').bind("click", logout);
        $('#logindialog').jqm({modal: true});
    }
    

	$("form input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			login();
			return false;
		} else {
            if ((e.which && e.which == 27) || (e.keyCode && e.keyCode == 27)) {
			    cancel();
			    return false;
		    } else {
			    return true;
		    }
		}
    });

    
});

function hasCSS() {
    var _d = document.createElement('div')
    _d.id = 'css_test'
    $('body').append(_d)
    $('#css_test').css({width:'1px',height:'1px',display:'none'})
    var _v = ($('#css_test').width() != 1) ? false : true
    $('#css_test').remove()
    return _v
}

function showlogin(event){
    $('#logincontainer').fadeIn();
    $('#logindialog').jqmShow();
    return false;
}
function cancel(event){
    $('#logindialog').jqmHide();
    $('#msg').html("");
    return false;
}
function login(event){
    $('#msg').html(loginanim);
    err = false;
    
    if ($.trim($("#txtpasswd").val()) == ""){
        $("#msg").html("<img src='/images/icons/warning.gif' alt='!' />Please enter password");
        err = true;
        $("#txtpasswd").focus();
    }
    if ($.trim($("#txtusrname").val()) == ""){
        $("#msg").html("<img src='/images/icons/warning.gif' alt='!' />Please enter username");
        err = true;
        $("#txtusrname").focus();
    }

    if (!err){
    $.ajax({
        type: "POST",
        data: "{id:'" + $("#txtusrname").val() + "',pw:'" + $("#txtpasswd").val() + "'}",
        url: "/login.aspx/loguserin",
        cache: false,
        global:false,
        dataType:"json",
        contentType: "application/json; charset=utf-8",
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $("#msg").html("<img src='/images/icons/warning.gif' alt='!' />Unable to log in:<br />A server error occured");
        },
        success: function(data){
            if (data.success == "True") {
                $('#msg').html(loadinganim);
                window.location.replace("/members.aspx");
            } else {
                $("#msg").html("<img src='/images/icons/warning.gif' alt='!' style='margin-right:10px;vertical-align:text-bottom;'/>Unable to log in: <br />" + data.errtext);
            }
        }
    });
    }
    
}
function logout(event){
     $.cookies.del("ecm");
     window.location.replace("/");
     return false;
}