var JQUERY_UI     = '/dc_includes/jquery-ui-1.7/';
var JQUERY_UI_URL = JQUERY_UI+'';
var JQUERY_UI_EXT = '.min.js';

function dcLoadJQueryUI(plugins, callback){

	if ( $.isArray(plugins) ){
	
		// Loop over each value in the array.
		$.each(plugins,
			function( index, plugin ){

				//alert(index+':'+plugin+':'+dcJQueryUIPath(plugin));
		
				plugins[index] = dcJQueryUIPath(plugin);
			}
		);		
	}else{
	
		plugins = dcJQueryUIPath(plugins);		
	}
	
	// console.log(plugins);
	
	$.requireScript(plugins,callback);
}

function dcJQueryUIPath(plugin){

	if ( plugin == 'ui' ){
	
		plugin = JQUERY_UI+'jquery-ui'+JQUERY_UI_EXT;
	}else{
	
		plugin = JQUERY_UI+plugin+JQUERY_UI_EXT;
	}
	
	return plugin;
}

// Place the user's cursor in a particular form field
//
// AK24122008
function dcFirstFormField(field_name) { 

	var field = document.getElementsByName(field_name)[0]; 
	field.focus(); 
}

// Open popup window in centre of page
//
// AK24122008
function dcPopup(windowUrl, return_field,windowHeight, windowWidth, windowName) {

    if ( windowHeight == null ){

    	windowHeight = 300; 
    }

    if ( windowWidth == null ){

    	windowWidth = 600; 
    }

    if ( windowName == null ){

    	windowName = 'popup';
    }

    var ifield = document.getElementById(return_field);

    var centerWidth = (window.screen.width - windowWidth) / 2; var centerHeight = (window.screen.height - windowHeight) / 2;

    newWindow = window.open(windowUrl, windowName, 'resizable=0,width=' + windowWidth + ',height=' + windowHeight + ',left=' + centerWidth + ',top=' + centerHeight);

    newWindow.focus();

    newWindow.ifield = ifield; window.ifield = ifield;

    return newWindow.name;
}

// Launch a JqueryUI dialog popup
function dcDialog(windowUrl, windowTitle, windowHeight, windowWidth) {

/*
jquery.ui.core.js
 *	jquery.ui.widget.js
 *  jquery.ui.button.js
 *	jquery.ui.draggable.js
 *	jquery.ui.mouse.js
 *	jquery.ui.position.js
 *	jquery.ui.resizable.js
 *	*/

	$.requireScript(['/dc_includes/jquery-ui/jquery.ui.core.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.widget.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.button.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.draggable.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.mouse.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.position.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.resizable.min.js',
			 '/dc_includes/jquery-ui/jquery.ui.dialog.min.js'],function(){
			 

		if(typeof $().dialog != 'function'){

			alert('Please install jquery_ui::dialog before calling dcDialog');
			return false;
		}

		if ( windowHeight == null ){

			windowHeight = 560; 
		}

		if ( windowWidth == null ){

			windowWidth = 640; 
		}

		if ( windowTitle == null ){

			windowTitle = 'Dialog';
		}

		var dialog = $("<div id='dialog'>");

		dialog.dialog({resizable: true,
		     modal: false,
		     width: windowWidth,
		     height: windowHeight,
		     title: windowTitle,
		     hide: 'slow',
		     zIndex: 999999999,
		     bgiframe: true,
		     close: function(){ dialog.hide('slow'); dialog.dialog('destroy'); dialog.remove(); }
		    });

		dialog.html("<div align='center'><blink>Loading...</blink></div>");

		dialog.load(windowUrl);

		return dialog;
	});
}

// Turn on wait cursor and disable buttons
function dcWaitOn(){

	// Turn on Waiting Cursor
	$('body').css('cursor','wait');

	// Disable button
	$('.submit_button').attr("disabled", "disabled");   
}

// Turn off wait cursor and enable buttons
function dcWaitOff(){

	// Turn off Waiting Cursor
	setTimeout(function() {
		$('body').css('cursor', 'default');
	}, 0); 

	// Enable button
	$('.submit_button').removeAttr("disabled");	   
}     