$(function()
{
    $('a[rel*=facebox]').facebox()
    
    $(".favourites-link").click( function()
    {
        var id = $( this ).attr( "rel" );
        var cmd = $( this ).attr( "cmd" );
        
        if ( cmd == "add" )
        {
            $.executeCommand({
                command: "saveFavourite",
                extraItems:
                [
                    {name: "itemID", value: id}
                ]
            });
        } else {
            $.executeCommand({
                command: "deleteFavourite",
                extraItems:
                [
                    {name: "itemID", value: id}
                ]
            });
        }
    });
});

function sendEnquiry()
{
    var advertID = $("#adID").val();
    var name = $("#txtName").val();
    var email = $("#txtEmail").val();
    var enquiry = $("#txtEnquiry").val();
    var err = false;
    var errorText = "";
    var br = "";

    if (name == "")
    {
        errorText += "Your Name";
        err = true;
        br = "<br>";
    }
    
    if (email == "")
    {
        errorText += br + "Your Email";
        err = true;
        br = "<br>";
    }
    
    if (enquiry == "")
    {
        errorText += br + "Your Enquiry";
        err = true;
    }
    
    if (err)
    {
        $("#errors").html("<p class='error'>Please fill in the following fields<br>" + errorText + "</p>");
        return false;
    }
    $("#errors").empty();
    
    $("#divForm").hide();
    $("#divWait").show();
    $.executeCommand({
        command: "contactSeller",
        form: "frmContact"
    });       
}

function contactSeller_callback( data )
{
    $("#divWait").hide();
    $("#divThanks").show();
}

function saveFavourite_callback( data )
{
    window.history.go( 0 );
}

function deleteFavourite_callback( data )
{
    window.history.go( 0 );
}
