// ------- Dynamic Popup functions  -------- //

function showPopup(name, headtext, bodytext, wd, ht, ok_callback, save_callback, show_cancel, extra_buttons) {
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id','popup-'+name);
	newdiv.innerHTML = '<div class="hd">'+headtext+'</div><div class="bd">'+bodytext+'</div>';
	document.body.appendChild(newdiv);

	var handleCancel = function() {
		this.cancel();
		this.destroy();
	}
	
	var mybuttons = new Array();
	if (extra_buttons) {
// 		alert(typeof extra_buttons);
// 		alert(extra_buttons.length);
		if (extra_buttons.length) {
			for (i=0; i<extra_buttons.length; i++) {
				mybuttons.push(extra_buttons[i]);
			}
		} else {
			mybuttons.push(extra_buttons);	
		}
	}
	if (ok_callback) {
		mybuttons.push({ text:"OK", handler:ok_callback });
	}
	if (save_callback) {
		mybuttons.push({ text:"Save", handler:save_callback });
	}
	if (show_cancel) {
		mybuttons.push({ text:"Cancel", handler:handleCancel });
	}
	
	var newDialog = new YAHOO.widget.Dialog("popup-"+name, {
		fixedcenter: true,
		visible: false,
		width: wd,
		height: ht,
		modal: true,
		close: false,
		buttons : mybuttons,
		monitorresize: false
	} ); 

	newDialog.render(); 
	newDialog.show(); 
	return newDialog;
}

// ------- "Loading..." dialog  -------- //
var pleaseWait;

function showLoading(message) {
	if (!message) {
		message = "Loading, please wait...";
	}
	pleaseWait = showPopup('pleasewait',message, '<img src="https://cms.richard-group.com/www/images/rel_interstitial_loading.gif" />');
}

function hideLoading() {
	if (pleaseWait) {
		pleaseWait.cancel(); 
		pleaseWait.destroy(); 
	}
}

function changeLoading(headtext, bodytext) {
	document.getElementById('popup-pleasewait').innerHTML = '<div class="hd">'+headtext+'</div><div class="bd">'+bodytext+'</div>';
}

