function init_xform (xformid, xtarget) 
{
	if (xformid == null)
		xformid = 'xform';
		
	if (xtarget == null)
		xtarget = 'log';
		
	disable_buttons ("#"+xformid, false);
	
	var options = 
	{ 
		url: $('#'+xformid).attr ('action')+'&no_header=1&no_footer=1&is_submit=1'
		, beforeSubmit: function () {
			disable_buttons ("#"+xformid, true);	
		}
		, success: function(data) {
			$('#'+xtarget).html (data);
			if ($('#'+xtarget+' .formErrors').length == 0)
				$('#container').hide ();
		}
		, complete: function () {
			disable_buttons ("#"+xformid, false);
		}
	};
	$("#"+xformid).ajaxForm (options);
	
	$("#"+xformid+" input").keypress (function(e) 
	{
		if(e.keyCode==13){
			return false;
        }
	});
	
	$("#"+xformid+" input[name='apply_button']").click (function () 
	{
		$("#"+xformid+" input[name='submit_apply']").val (1);
	});
	
	$("#"+xformid+" input[name='submit_button']").click (function () 
	{
		$("#"+xformid+" input[name='submit_apply']").val ('');
	});
}

function disable_buttons (form_id, disable) 
{
	$(form_id + " input:[type='button'], "+form_id + " input:[type='submit']").each (function () 
	{
		if (!$(this).hasClass ('exclude')) 
		{
			if (disable) 
			{
				$(this).addClass ('button_grey');
				$(this).attr ('disabled', 'disabled');
			}
			else 
			{
				$(this).removeClass ('button_grey');
				$(this).removeAttr ('disabled');
			}
		}
	});
}

function init_xbrowse (xformid, xtarget) 
{
	if (xformid == null)
		xformid = 'xsearch_form';
		
	if (xtarget == null)
		xtarget = 'content_id';
		
	var options = { 
		target: '#'+xtarget
		, url: $('#'+xformid).attr ('action')+'&no_header=1&no_footer=1'
		, success: function(data) {}
	};
	$("#"+xformid).ajaxForm (options);
	
	$("#"+xformid+" input").keypress (function(e) 
	{
		if(e.keyCode==13){
			$("#"+xformid).submit ();
        }
	});
	
	$("#"+xformid+" input[name='clear_search']").click (function(e) 
	{
		$("#"+xformid+" input[name='search_criteria']").val ('');
		$("#"+xformid).submit ();
	});
}

function xdelete (obj) {
	if (confirm ("Are you sure you want to delete this entry?")) {
		$.post($(obj).attr ('href'), function(data) {
			$(obj).parentsUntil ('tr').parent().fadeOut ('slow');
		});
	}
	return false;
}

function xupload (obj, xformid) {
	if (xformid == null)
		xformid = 'xform';
		
	var idx = get_element_index (obj);
	
	$('#'+xformid).ajaxSubmit ({
		url:'upload.proc.php'
		, iframe: true
		, data: ({fileframe: 1})
		, beforeSubmit: function () {
			$('#upload_status'+(idx == null ? '' : '_'+idx)).html ('<img src="images/upload_loader.gif">');
		}
		, success: function () {
		}
		, complete: function () {}
	});
	
}

function redirect (url, open_window) 
{
	if (open_window == true)
		window.open(url, '_blank');
	else
		window.location.href = url;
}

function get_element_index (obj, offset) {
	var id = $(obj).attr ('id');
	var token = id.split ('_');
	if (offset == null)
		offset = 1;
	var idx = token[token.length-offset];
	return isNaN (idx) ? null : idx;
}	
