$(document).ready(loadDialogForm);
$(document).ready(loadAJAXClick);

function loadDialogForm() {
	var frameId = 'frame_ID';	//set an id for a hidden frame
	var formID = 'form_ID';		//set an id for the form
	var jFrame = null;				//clear the frame

	$('a.adfrm').click(function() {
		var url = this.href;
		var dialogX = $('<div style="display:hidden"></div>').appendTo('body');
		var a = $(this);
		// load remote content
		dialogX.load(
			url,
			{},
			function (responseText, textStatus, XMLHttpRequest) {
				//var fileupload = $(this).find('[type="file"]').attr('name'); //check if there is a file to upload
				//checkup('txt: ' + responseText + '<br />' + 'status: ' + textStatus + '<br />' + 'xml: ' + XMLHttpRequest + '<br />' + fileupload);
				var dialogForm = $(this).find('form');
				var stdform = dialogForm.attr('class')=='stdform'? true: false;
				dialogForm.attr('id',formID);
				dialogX.dialog({
					//autoOpen: false,
					title: a.attr('title')? a.attr('title'): 'Dialog Form',
					//height: 500,
					width: 800,
					modal: true,
					buttons: {
						"Update Field": function() {
							if (stdform){
								//alert('Standard Form');
								$('#'+formID).submit();
								//location.reload(false);
							} else {
								//alert('Ajax form');
								submitAJAXForm(dialogForm);
							}
							$(this).dialog( "close" );
						},
						Cancel: function() {
							$(this).dialog( "close" );
						}
					},
					close: function() {
						$(this).remove();
					}
				}); //end of dialog
				dialogX.find(':submit').hide();  //hide the submit button if it is on the form
			} //end of function
		); //end of dialogX load
		return false; //prevent the browser from following the link
	}); //end of a click

} //end of function

/*
 * The form needs has to have a div container inside the form with an id of ff101
 */
function submitAJAXForm(myform){
	var myurl = myform.attr('action');
	$('#ff101').upload(myurl, {'action':'submitted'}, callback, 'json');
	return false;
};

/*
 * My original AJAX click updater
 */
function loadAJAXClick(){
	//ajax link
	$('a.adlnk').click(function(){
		var myurl = this.href;
		$.ajax({
			url: myurl,
			type: 'GET',
			data: {other: 123},
			dataType: 'json',
			success: callback
		});
		return false;
	});
}

/* Frame creation for image upload handling - used to direct to form load output
 * Increase the width and height if you need to debug image uploads
 */
function createUploadIframe(id){
	return $('<iframe width="0" height="0" name="' + id + '" id="' + id + '"></iframe>')
		.css({position: 'absolute',top: '-999px',left: '0px',border:'0px solid #f00'})
		.appendTo('body');
}

function checkup(jqparam){
	$("#checker").append('<br />' + jqparam);
}

/*
 * Callback function used by all the loaders
 */
function callback(data){
	var outcome;
	var fd_out=6000;

	if(undefined===data.outcome){
		outcome = 'Error - Outcome not set';
	} else {
		if (data.outcome=='success'){
			outcome = 'Success - Site updated';
			//location.reload(true);
		} else if(data.outcome=='success (reload)') {
			outcome = 'Success - Site updated';
			location.reload(true);
		} else {
			outcome = 'Error - Site not updated';
		}
	}
	for (x in data.scrn){
		if (data.scrn[x]['act']=='html'){
			$('#'+data.scrn[x]['fld']).html(data.scrn[x]['val']);
		} else if(data.scrn[x]['act']=='src') {
			$('#'+data.scrn[x]['fld']).attr("src",data.scrn[x]['val']);
		} else if(data.scrn[x]['act']=='href') {
			$('#'+data.scrn[x]['fld']).attr("href",data.scrn[x]['val']);
		} else if(data.scrn[x]['act']=='hide') {
			$('#'+data.scrn[x]['fld']).hide();
		} else if(data.scrn[x]['act']=='show') {
			$('#'+data.scrn[x]['fld']).show();
		} else if(data.scrn[x]['act']=='highlight') {
			var fd_background = data.scrn[x]['val']==''? "#dddddd": data.scrn[x]['val'];
			$('#'+data.scrn[x]['fld']).css("background-color",fd_background);
		} else if(data.scrn[x]['act']=='refresh') {
			fd_out = data.scrn[x]['val']>0? data.scrn[x]['val']: fd_out;
			outcome = 'Success - Please refresh the page';
			//setTimeout (location.reload(true),25000);
			//location.reload(true);
		} else {
			fd_out = data.scrn[x]['val']>0? data.scrn[x]['val']: fd_out;
			outcome = data.scrn[x]['act'];
		}
		//alert(outcome);
	}
	//if (status=='success'){
		$('#status101').html(outcome).fadeIn(200).fadeOut(fd_out);
	//}
}

/*
 * Error callback function used by the ajax loaders
 */
function errback(xhr, reason, ex){
	var outcome = 'Error: ' + reason;
	$('#status101').html(outcome);
}
