//set all the options for sortable
var originalOptions =
{
    update: function()
    {
        var form = $('.sortable').sortable('serialize');

        var url = base_url + "admin/houses/update_dimensions_positions/";

        $.post(url, form, function(data)
        {

        }, "text");//text is the response from the server



    },
    handle : ".handle"

};
/*& This is for sorting */
var houseOptions =
{
    update: function()
    {
        var form = $('.sortable').sortable('serialize');

        var url = base_url + "admin/houses/updateElevationPositions/";


        $.post(url, form, function(data)
        {

        }, "text");//text is the response from the server



    },
    handle : ".handle"

};

  /*& This is for sorting */
var housePositionOptions =
{
    update: function()
    {
        var form = $('.sortable').sortable('serialize');

        var url = base_url + "admin/house_styles/updateHouseStylePosition/";

       // alert(form);
        //return false;

        $.post(url, form, function(data)
        {

        }, "text");//text is the response from the server
    },
    handle : ".handle"

};

//set options for TinyMCE
var tinyOptions = { mode : "textareas", editor_selector : "mceSimple" };

//activley look out for images so we can display with lightbox
$("#form a.image").live("click", function()
    {
        $(this).lightBox();
        //this clicks the link after setting the handler.
        //maybe buggy and will require a function that overwrites
        //itself afterwards.
        $(this).click();
        return false;
    });



//loads data into the form div
function showFormData(url, sortableOptions)
{
    var arr = new Array();
    $.post(url, arr, function(data)
    {

        $("#form").html(data).find(".sortable").sortable(sortableOptions);

    }, "text");//text is the response from the server
}

function validForm()
{

    var result = true;
    $(".required").each(function( index )
    {
        //get the value
        var temp = $(this).val();


        //remove any spaces for validation
        var fieldValue = trim(temp);

        //remove any previous errors
        if($(this).hasClass("failed"))
        {
            $(this).removeClass("failed");
        }

        //make sure required fields are not blank
        if( fieldValue == "" )
        {
         $(this).addClass(" failed");
         result = false;
        }

        //validate if it should be numeric only
        if($(this).hasClass("numeric"))
        {
            if( isNaN(fieldValue) )
            {
                $(this).addClass(" failed");
                result = false;
            }
        }
    });
    return result;
}


function resetFields(form)
{
    //loop through form resetting the input fields but not buttons etc
    $(':input', form).not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');

    $(':input', form).removeAttr("checked").removeAttr("selected");

}

function updateData(url)
{
      $("#display").fadeOut(500);

        var series = new Array();
        var str = "";
        var arr = new Array();
        setTimeout(function()
        {
            $.post(url, arr, function(data)
            {

                $("#display").html(data).find(".sortable").sortable(housePositionOptions);
                $("#display").fadeIn(900);

            }, "text");//text is the response from the server

        }, 500);    //wait half a second before the ajax call so we can see the fade out

        return false;
}
function getClass()
{
    var type = document.getElementById("classname").innerHTML;
    return type;
}

function feedback(str)
{
    //display feedback
    //reset any post request result
    $(".result").html("");
    $(".ajaxresult").css("visibilty", "visible");
    $(".ajaxresult").html("<p>" + str + "</p>");


}

function ajaxError(xhr, status)
{
    var message = "";
    var title = "";
    if(xhr.status == 404)
    {
        title = "404 Page Not Found";
        message = "You should report this to the website administrator";
    }
    else if( xhr.status == 500)
    {
        title = "Internal Server Error";
        message = "You should report this to the website administrator";
    }
    else
    {
        title = "Unhandled Exception";
        message = "You should report this to the website administrator";
    }
    $("#dialogresult").html(message);

    $("#dialogresult").dialog({
        modal: true,
        draggable: false,
        show: "drop",
        height: 300,
        width: 450,
        minHeight: 300,
        minWidth: 450,
        title: title,
        buttons:
        {
            "Ok": function()
            {
                $(this).dialog("close");
                $("#dialogresult").html("");
                feedback("<p>Your last request could not completed");
            }
        }//end buttons
    });//end dialog

}


function addLoadListener(fn)
{
    if (typeof window.addEventListener != 'undefined')
    {
        window.addEventListener('load', fn, false);
    }
    else if (typeof document.addEventListener != 'undefined')
    {
        document.addEventListener('load', fn, false);
    }
    else if (typeof window.attachEvent != 'undefined')
    {
        window.attachEvent('onload', fn);
    }
    else
    {
        var oldfn = window.onload;
        if (typeof window.onload != 'function')
        {
            window.onload = fn;
        }
        else
        {
            window.onload = function()
        {
        oldfn();
        fn();
        };
        }
    }
}

function getXhr()
{
    var xhr;
	try
	{
		xhr = new XMLHttpRequest();
	}
    catch (error)
	{
		try
		{
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
        catch (error)
		{
			xhr = null;
		}
	}

    return xhr;
}
function trim(stringToTrim)
{
    return stringToTrim.replace(/\s+/g, "");
}
