var CSAjax = "/CSAjax.aspx";

//addthis_toolbox

$(document).ready(function()
{
    if ($("#postdetailpage").length > 0)
    {
        $(".addthis_toolbox").show();
    }
    else
    {
        $(".addthis_toolbox").hide();
    }    
});

function login()
{
    if (!checktext($("#txtLUsername")))
    {
        alert("You must enter a Username");
        return;
    }
    else if (!checktext($("#txtLPassword")))
    {
        alert("You must enter a Password");
        return;
    }
    
    var username = $("#txtLUsername").val();
    var password = $("#txtLPassword").val();
    var remember = $("#chkLRemember").is(":checked");

    
    var data = ("command=login");
    if (username.length > 0) data += ("&username=" + username);
    if (password.length > 0) data += ("&password=" + password);
    data += ("&remember=" + remember);
        
    $.ajax({
        type: "POST",
        url: CSAjax,
        data: data,
        error: function(msg, err){
            alert("There was an error logging in. Please try again later.");
        },
        success: function(status){
            if (status > 0)
            {
                $(".createnewarticle").show();
                $(".signuplink").hide();
                $(".loginlink").hide();
                $(".logoutlink").show();                
                $(".usernamelink").show();
                $(".usernamelink").html(username.toUpperCase());
                //$(".nonusersection").hide();
                //$(".usersection").show();

                tb_remove();

		location.reload();
            }
            else if (status == -2)
            {
                alert("You entered some funky characters. That's not allowed. Please try again without typing something so crazy.");
            }
            else
            {
                alert("Invalid login. Please try again.");
            }
        }
    });
}

function logout()
{
    var data = ("command=logout");
        
    $.ajax({
        type: "POST",
        url: CSAjax,
        data: data,
        error: function(msg, err){
            alert("There was an error logging out. Please contact the system administrator.");
        },
        success: function(status){
            if (status == "success")
            {
                $(".createnewarticle").hide();
                $(".signuplink").show();
                $(".loginlink").show();
                $(".logoutlink").hide();
                $(".usernamelink").hide();
                
                location.replace("Default.aspx");
            }
            else
            {
                alert("There was an error logging out. Please contact the system administrator.");
            }
        }
    });
}

function dialogshow(newID, hei, wid, removeold)
{
    var url = "#TB_inline?height=" + hei + "&width=" + wid + "&inlineId=" + newID + "&modal=true";
    if (removeold) $(".TB_modal").hide();
    tb_show("", url); 
}

function checkemail(textbox)
{
    var isvalid = true;
    var email = $(textbox).val();
    var gotat = email.indexOf("@") >= 0 ? true : false;
    var gotdec = email.indexOf(".") >= 0 ? true : false;
    
    isvalid = email.length > 6 && gotat && gotdec;
    return isvalid;
}

function checktext(textbox)
{
    return (jQuery.trim($(textbox).val()).length > 0);
}

function signup()
{    
    if (!checktext($("#txtUsername")))
    {
        alert("You must enter a Username");
        return;
    }
    else if (!checktext($("#txtPassword1")))
    {
        alert("You must enter a Password");
        return;
    }
    else if ($("#txtPassword1").val() != $("#txtPassword2").val())
    {
        alert("Passwords do not match");
        return;
    }
    else if (!checktext($("#txtFirstname")[0]) || !checktext($("#txtLastname")[0]))
    {
        alert("You must enter your first and last name");
        return;
    }
    else if (!checkemail($("#txtEmail")))
    {
        alert("You must enter a valid email address");
        return;
    }
    
    var username = $("#txtUsername").val();
    var password = $("#txtPassword1").val();
    var firstname = $("#txtFirstname").val();
    var lastname = $("#txtLastname").val();
    var email = $("#txtEmail").val();
    
    var data = ("command=signup");
    if (username.length > 0) data += ("&username=" + username);
    if (password.length > 0) data += ("&password=" + password);
    if (firstname.length > 0) data += ("&firstname=" + firstname);
    if (lastname.length > 0) data += ("&lastname=" + lastname);
    if (email.length > 0) data += ("&email=" + email);
    
    $.ajax({
        type: "POST",
        url: CSAjax,
        data: data,
        error: function(msg, err){
            alert("There was an error creating your user profile. Please try again later.");
        },
        success: function(status){
            if (status == -1)
            {
                alert("There is already a user with that Username. Please try again.");
            }
            else if (status == -2)
            {
                alert("You entered some funky characters. That's not allowed. Please try again without typing something so crazy.");
            }
            else if (status == -3)
            {
                alert("You entered the wrong image validation text. Please try again.");
            }
            else if (status == 0)
            {
                alert("There was an error creating your user profile. Please try again later.");
            }
            else if (status > 0)
            {
                var welcomemsg = "Welcome " + firstname + " " + lastname + "."
                    + " Your Screen Name is " + username + "."
                    + " Would you like to remain on this page or visit your User Profile?";
                                
                $("#signupstep1").hide();
                $(".createnewarticle").show();
                $(".usernamelink").show();
                $(".usernamelink").html(username.toUpperCase());
                $(".signuplink").hide();
                $(".loginlink").hide();
                $(".logoutlink").show();                
                $("#signupstep2").show();
                $("#signupwelcome").html(welcomemsg);
            }
        }
    });
}

function cancelsignup()
{
    $(".TB_modal .textbox").val("");
    tb_remove();
}

function commentclicked(ownerid, ownertype, subjectid, uid, newID, hei, wid, removeold)
{
    var replysubject = $("#" + subjectid).html();    
    
    $("#hidOwnerID").val(ownerid);
    $("#hidOwnerType").val(ownertype);
    $("#hidUID").val(uid);
    
    if (replysubject == "")
    {
        $("#commentmessage").html("");
    }
    else
    {
        if (ownertype == 1)
        {
            $("#commentmessage").html("You are posting a comment to '" 
                + $("#postSubject").html() 
                + "'");
        }
        else
        {
            $("#commentmessage").html("Replying to: " + replysubject);
        }
    }   
    
    dialogshow(newID, hei, wid, removeold);
}

function postcomment()
{
    var subject = $("#txtCSubject").val();
    var message = $("#txtCMessage").val();
    var uid = $("#hidUID").val();
    var ownerid = $("#hidOwnerID").val();
    var ownertype = $("#hidOwnerType").val();
    
    var data = ("command=postcomment");
    data += ("&subject=" + subject);
    data += ("&message=" + message);
    data += ("&uid=" + uid);
    data += ("&ownerid=" + ownerid);
    data += ("&ownertype=" + ownertype);
    
    $.ajax({
        type: "POST",
        url: CSAjax,
        data: data,
        error: function(msg, err){
            alert("There was an error posting your comment. Please try again later.");
        },
        success: function(status){
            
            if (status == "success")
            {
                location.reload();
            }
            else
            {
                alert("There was an error posting your comment. Please try again later.");
                tb_remove();
            }
        }
    });
}

function userProfileSaveVerify()
{
    var p1 =  $("[id*='txtPassword1']").val();
    var p2 = $("[id*='txtPassword2']").val();        
    
    if (p1 != "" && p1 != p2) alert("Passwords do not match");
    else $("[id*='btnSave']").click();
}

function activateUserPost(linkid, pid, headid)
{
    var active = $("#hidIsActive" + pid).val();
    
    var data = ("command=activatepost");
    data += ("&active=" + active);
    data += ("&pid=" + pid);
    
    var actstr = "activating";
    if (active == "false")
    {
        actstr = "deactivating";
    }
    
    $.ajax({
        type: "POST",
        url: CSAjax,
        data: data,
        error: function(msg, err){
            alert("There was an error " + actstr + " this article. Please contact the system administrator.");
        },
        success: function(status){
            if (status == "success")
            {                
                if (active == "true")
                {
                    $("#" + headid).removeClass("headerinactive");
                    $("#" + linkid).html("HIDE THIS ARTICLE");
                    $("#hidIsActive" + pid).val("false");
                    //alert("This article is now visible on The Simple Contrarian");
                }
                else
                {
                    $("#" + headid).addClass("headerinactive");
                    $("#" + linkid).html("SHOW THIS ARTICLE");
                    $("#hidIsActive" + pid).val("true");
                    //alert("This article will not be displayed on The Simple Contrarian");
                }
            }
            else
            {
                alert("There was an error " + actstr + " this article. Please contact the system administrator.");
            }
        }
    });
    
    
}