function fillFormHelper(myelement, eleid, elevalue){
    //console.log(typeof(eleid) + "  " + typeof(elevalue));
    //console.log(eleid + "  " + elevalue);
    if (typeof(eleid) == 'undefined' || typeof(elevalue) == 'undefined') return;

    if (typeof(elevalue) == 'object' || typeof(elevalue) == 'array') {
        var curvar = elevalue;
        for (x in curvar)
        {
            var newid = eleid + "[" + x + "]";
            fillFormHelper(myelement,newid,curvar[x]);
        }
    } else {
        eleid=eleid.replace(/\[/g,"\\[");
        eleid=eleid.replace(/\]/g,"\\]");
        if (myelement.find('td.'+eleid + " input").length > 0) fillFormHelper2(myelement.find('td.'+eleid + " input"),elevalue);
        if (myelement.find('#'+eleid).length > 0) fillFormHelper2(myelement.find('#'+eleid),elevalue);
        if (myelement.find('.'+eleid).length > 0) fillFormHelper2(myelement.find('.'+eleid),elevalue);
    }

}

function fillFormHelper2(cureletop,elevalue){
    cureletop.each(function(){
        curele = $(this);
        if (curele.attr('type')=='checkbox') {
            if (elevalue=='1') curele.prop('checked',true);
            else curele.prop('checked',false);
        } else if(curele.is('img')) {
            curele.loadImage(elevalue);
        } else if(curele.is('input') || curele.is('textarea') || curele.is('select')) {
            curele.attr('value', elevalue);
        } else if(curele.hasClass('is_currency')){
            curele.html(formatCurrency(elevalue));
        } else if(curele.hasClass('first_caps')){
            curele.html(elevalue.charAt(0).toUpperCase() + elevalue.slice(1));
        } else if(curele.is('div') || curele.hasClass('fillhtml')){
            curele.html(elevalue);
        } 
    })
}

jQuery.fn.fillForm = function(mydata){
    return this.each(function(){
        var myelement = $(this);
        $.each(mydata, function(index, val) {
            fillFormHelper(myelement,index, val);
        })

    });
}

$(document).ready(function(event){
    if (typeof(formFillHelperData) != 'undefined') {
        $(document).fillForm(formFillHelperData);		
    }
})

	
jQuery.fn.saveStatus = function(success,message){
    return this.each(function(){
        var savebutton = $(this).find(".savebutton");
        savebutton.attr('disabled',false);
        if (typeof(message) != 'undefined') {
            savebutton.bt(message, {
                trigger: 'none', 
                positions: 'top', 
                fill: 'rgb(255,255,255)', 
                width: 100
            });
            savebutton.btOn();
            console.log(message);
        }
        if (!success) return;
		
        
        $(this).find('input').each(function(){
            $(this).data('savedstatus', $(this).attr('value'));
        });
        $(this).find('textarea').each(function(){
            $(this).data('savedstatus', $(this).attr('value'));
        });	    	
        $(this).find('img').each(function(){
            if ($(this).data('actualimage')){
                $(this).data('savedstatus', $(this).data('actualimage'));
                $(this).data('actualimage','');
            } else {
                $(this).data('savedstatus', $(this).attr('src'));
            }
        });
        $(this).find(".buttonframe").hide();
        $(this).addClass("editablefs");
        $(this).removeClass("editablefs-active");
    });
}

jQuery.fn.restoreStatus = function(){
    return this.each(function(){
        $(this).find('input').each(function(){
            $(this).attr('value',$(this).data('savedstatus'));
        });
        $(this).find('textarea').each(function(){
            $(this).attr('value',$(this).data('savedstatus'));
        });	    	
        $(this).find('img').each(function(){
            $(this).attr('src',$(this).data('savedstatus'));
        });	    
    });
}

jQuery.fn.editableFS = function(saveFunction){
    return this.each(function(){
        var curframe = $(this);

        //bind functions for the save and cancel buttons
        curframe.bind('editableFSSaveFunction', saveFunction);
        curframe.bind('editableFSCancelFunction', function() {
            $(this).find(".buttonframe").hide();
            $(this).addClass("editablefs");
            $(this).removeClass("editablefs-active");
            $(this).restoreStatus();			
        });

        //set the class
        curframe.addClass("editablefs");

        //set the button frame
        var buttonframe = $('<fieldset class="buttonframe" />');
        buttonframe.append('<input class="savebutton" type="button" value="Save Changes" />');
        buttonframe.append('<input class="cancelbutton" type="button" value="Cancel" />');
        buttonframe.find(".cancelbutton").click(function(event) {
            $(this).closest('.editablefs-active').trigger('editableFSCancelFunction');
            event.stopPropagation();
        });
        buttonframe.find(".savebutton").click(function(event) {
            $(this).attr('disabled','disabled');
            $(this).closest('.editablefs-active').trigger('editableFSSaveFunction');
            event.stopPropagation();
        });

        //click to activate
        curframe.click(function() {
            $(this).find(".buttonframe").show();
            $(this).removeClass("editablefs");
            $(this).addClass("editablefs-active");
        });

        curframe.append(buttonframe);
        buttonframe.hide();				
    })
}

jQuery.fn.getStatus = function(){
    var dataarray = {};

    $(this).find('select').each(function() {
        dataarray[$(this).attr('class')] = $(this).attr('value'); 
    })

    $(this).find('input').each(function() {
        dataarray[$(this).attr('class')] = $(this).attr('value'); 
    })
		
    $(this).find('textarea').each(function() {
        dataarray[$(this).attr('class')] = $(this).attr('value'); 
    })
    
    $(this).find('img').each(function() {
        dataarray[$(this).attr('class')] = $(this).attr('src'); 
    })

    return dataarray;
}

jQuery.fn.getStatusById = function(){
    var dataarray = {};

    $(this).find('select').each(function() {
        dataarray[$(this).attr('id')] = $(this).attr('value'); 
    })

    $(this).find('input').each(function() {
        dataarray[$(this).attr('id')] = $(this).attr('value'); 
    })
		
    $(this).find('textarea').each(function() {
        dataarray[$(this).attr('id')] = $(this).attr('value'); 
    })    
		
    $(this).find('img').each(function() {
        dataarray[$(this).attr('id')] = $(this).attr('src'); 
    })

    return dataarray;
}

jQuery.fn.getStatusFromTable = function() {
    var dataarray = {};
    $(this).find("td").each(function(){
        var colname = $(this).attr('class');
        var colvalue;

        colvalue = $(this).find("input").attr('value');
        if (colvalue==undefined) colvalue = $(this).find("select").attr('value');
				
        dataarray[colname] = colvalue
    })
    return dataarray;
}
	

/*
	(function($) {
	    $.extend({
	        getGo: function(url, params) {
	            document.location = url + '?' + $.param(params);
	        },
	        postGo: function(url, params) {
	            var $form = $("<form>")
	                .attr("method", "post")
	                .attr("action", url);
	            $.each(params, function(name, value) {
	                $("<input type='hidden'>")
	                    .attr("name", name)
	                    .attr("value", value)
	                    .appendTo($form);
	            });
	            $form.appendTo("body");
	            $form.submit();
	        }
	    });
	})(jQuery);
*/
