var CMS_FILENAME_INDEX = 'index.php';
var CMS_FILENAME_WINDOW_DIALOG = 'rdcms.modal.dialog.php';
var CMS_FILENAME_MSG_DIALOG = 'rdcms.msg.dialog.php';
var CMS_FILENAME_TPL_MODAL_IMAGE_PREVIEW = 'templates/image.preview.php';
// IE Window Methods
/**
* Close current window
*
* @return void
**/
function closeWindow()
{
	window.close();
}

/**
* Refreshed parent window
*
* @return void
**/
function rd_redirectParentWindow(_location)
{
	if(window.opener)
		window.opener.location = _location;
}

/**
* Refreshed parent window and close current popup
*
* @return void
**/
function closeAndRefresh()
{
	if(window.opener)
	{
		rd_redirectParentWindow(window.opener.location);
		closeWindow();
	}
}

/**
* Window that is current open
**/
var popUpWin=0;
/**
* Array of opened windows
**/
var _rd_window_array = new Array();
/**
* Register popup window
*
* @param object windowObject
**/
function rd_addPopup(windowObject)
{
	if(typeof(windowObject) == "object")
	{
		_rd_window_array.push(windowObject);
	}
}

/**
* Closes all popup windows added by use of rd_AddPopup()
*
**/
function rd_closeAllPopUps()
{
	for(var i=0;i<_rd_window_array.length-1;i++)
	{
		if(_rd_window_array[i])
		{
			_rd_window_array[i].close();
		}
	}
}

/**
* Run popup test (opens pop up  and closes it) if successful return true
*
* @return boolean
**/
function rd_doPopupTest()
{
	var _result = true;
	try
	{
		var _window = open();
		window.focus();
		if(_window)
		{
			_window.close();
			_result = true;
			return _result;
		}
	}
	catch(e)
	{
		_result = false;
	}
}

/**
* Open centered window
*
**/
function popUpCenteredWindow(url, width, height, fullscreen, modal, scrollbars, resizable)
{
	var scrollbars = (scrollbars == null) ? 'no' : 'yes';
	var resizable = (resizable == null) ? 'no' : 'yes';
	var modal = (modal!=null) ? modal : false;
	var fullscreen = (fullscreen!=null) ? fullscreen:0;
	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}
	var str = '';
	str+= 'toolbar=no';
	str+= ',location=no';
	str+= ',directories=no';
	str+= ',status=no';
	str+= ',menubar=no';
	str+= ',scrollbars='+scrollbars;
	str+= ',resizable='+resizable;
	str+= ',copyhistory=yes';
	str+= ',width='+width;
	str+= ',height='+height;
	str+= ',fullscreen='+fullscreen;
	if(modal)
	{
		str+= ',modal=yes';
	}
	if(window.screen)
	{
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width)/2;
		var yc = (ah - height)/2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}
	try
	{
		var popUpWin = window.open(url, 'popUpWin', str);
	}
	catch(e)
	{
		rd_raiseMessage(RS_POP_UP_WINDOW_HAS_BEEN_BLOCKED_PLEASE_TURN_OFF_THE_POPUP_FILTERING_BLOCKING);
	}
	if(!popUpWin)
	{
		rd_raiseMessage(RS_POP_UP_WINDOW_HAS_BEEN_BLOCKED_PLEASE_TURN_OFF_THE_POPUP_FILTERING_BLOCKING);
	}
	rd_addPopup(popUpWin);
	if(popUpWin != false)
	{
		popUpWin.focus();
	}
	return popUpWin;
}

/**
* Open cms modal dialog
*
**/
function rd_modalDialog(title, identifier, getString, arguments, style)
{
	style+=";help:no";
	var _url = CMS_FILENAME_WINDOW_DIALOG+"?title="+title+"&data="+identifier+getString;
	var _arguments  = new Object();
	_arguments.args = new Array();
	_arguments.args.push(arguments);
	_arguments.args.push(window);
	try
	{
		var _window = window.showModalDialog(_url, _arguments, style);
	}
	catch(e)
	{
		rd_raiseMessage(RS_POP_UP_WINDOW_HAS_BEEN_BLOCKED_PLEASE_TURN_OFF_THE_POPUP_FILTERING_BLOCKING);
	}
	rd_addPopup(_window);
	return _window;
}

/**
* Open dialog window
*
**/
function rd_openCustomDialogue(title, identifier, width, height, features, windowArguments, windowInitiateParameters)
{
	if(windowInitiateParameters == null) {windowInitiateParameters = '';}
	if(width<200) { width = 200; }
	if(height<200) { height = 200; }
	var windowStyle = '';
	if(width != null && height != null)
	{
		windowStyle = "dialogHeight= "+height+"px; dialogWidth= "+width+"px;";
	}
	if(features != null)
	{
		windowStyle +=  features;
	}
	if(window.screen)
	{
		var ah = screen.availHeight - 30;    var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;    var yc = (ah - height) / 2;
		windowStyle += ",left=" + xc + ",screenX=" + xc;
		windowStyle += ",top=" + yc + ",screenY=" + yc;
	}
	if(window.showModalDialog)
	{
		var _return = rd_modalDialog(title, identifier, windowInitiateParameters, windowArguments, windowStyle);
		rd_addPopup(_return);
	}
	else
	{
		rd_openCenteredPopUp(title, identifier, width, height, windowInitiateParameters, false, true);
	}
	return _return;
}

/**
* Open centered window
*
* @return object newly created window
**/
function rd_openCenteredPopUp(title, identifier, width, height, url_addon, fullscreen, modal, scrollbars, resizable)
{
	var url_addon = (url_addon!=null) ? url_addon:"";
	fullscreen = (fullscreen!=null) ? fullscreen:0;
	resizable = (resizable!=null) ? resizable:'no';
	return popUpCenteredWindow(CMS_FILENAME_WINDOW_DIALOG+"?title="+title+"&data="+identifier+url_addon, width, height, fullscreen, modal, scrollbars, resizable);
}

/**
* Open new window
*
* @return void
**/
function new_window(bool,action)
{
	if(bool)
	{
		window.open(action);
	}
	else if(bool == null)
	{
		window.open(action);
	}
}

/**
* Preform browser redirection
*
* @return void
**/
function window_location(bool, action)
{
	if(bool || bool == null)
	{
		window.location.href = action;
	}
}

/* Generic PopUps */

function rd_raiseMessage(msg)
{
	var button_set = new Array("OK");
	var alert_type = "ALERT";
	var message = msg;
	var _window = rd_openMessageModal(message, button_set, alert_type);
}

function rd_raiseConfirm(msg)
{
	var button_set = new Array("NO","YES");
	var alert_type = "CONFIRM";
	var message = msg;
	return rd_openMessageModal(message, button_set, alert_type);
}

/**
* Open a message modal dialog
* @param string
* @param string
* @param string
**/
function rd_openMessageModal(message, button_set, alert_type)
{
	var _window = false;
	if(message != '')
	{
		var message = message.toString();
		var unencoded_message = message;
		message = replaceCharacters(unencoded_message ,"\n","<br>");
		var style = "help:no; scroll:0; resizable:1; status:no; dialogHeight:300px; dialogWidth:400px; ";
		if(window)
		{
			var _url_link = CMS_FILENAME_MSG_DIALOG+"?button_set="+button_set.join(",")+"&message="+message+"&alerttype="+alert_type;
			var _window = window.showModalDialog(_url_link, null, style);
			if(_window == null)
				_window = false;
		}
	}
	else
	{
		alert(message);
	}
	return _window;
}

function rd_popUpSelectConfirm()
{
	window.close();
}

function rd_popUpSetReturnValue(value)
{
	window.returnValue = value;
}

function rd_eventPopUpClose($assoc_index)
{
	rd_popUpSetReturnValue(false);
	rd_popUpSelectConfirm();
}

function rd_eventPopUpOk()
{
	rd_popUpSetReturnValue(true);
	rd_popUpSelectConfirm();
}

function rd_eventPopUpCancel()
{
	rd_popUpSetReturnValue(false);
	rd_popUpSelectConfirm();
}

function rd_eventPopUpNo()
{
	rd_eventPopUpCancel();
}

function rd_eventPopUpYes()
{
	rd_eventPopUpOk();
}

/**
* Sets the windows status message (bottom left corner)
*
* @param string messag
**/
function rd_setWindowStatus(message)
{
	if(window)
	{
		window.status = message;
	}
}

/**
* Create popup window (DEPRECATED)
* Calendar uses this method
*
**/
function popUpWindow(URLStr, left, top, width, height)
{
	if(popUpWin)
	{
		if(popUpWin.closed == false)
		{
			popUpWin.close();
		}
	}
	try
	{
		popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	}
	catch(e)
	{
		rd_raiseMessage(RS_POP_UP_WINDOW_HAS_BEEN_BLOCKED_PLEASE_TURN_OFF_THE_POPUP_FILTERING_BLOCKING);
	}
	if(!popUpWin)
	{
		rd_raiseMessage(RS_POP_UP_WINDOW_HAS_BEEN_BLOCKED_PLEASE_TURN_OFF_THE_POPUP_FILTERING_BLOCKING);
	}
	rd_addPopup(popUpWin);
	return popUpWin;
}

/**
 * Navigation link window
 *
 * @return void
 **/
function rd_openLinkWindow(identifier)
{
	var properties = rd_openCustomDialogue(RS_CREATE_A_HYPERLINK, 'MODAL_NAVIGATION_LINK', 730, 450);
	if(properties && properties.text != null && properties.text != 'false')
	{
		var targetElement = rd_getElement('target');
		var targetElementCheck = rd_getElement('check_box_target');
		rd_getElement(identifier+'_href').value = '';
		rd_getElement(identifier+'linktype').value = properties.linktype;
		if(properties.target == '_blank')
		{
			targetElement.value = 1;
			targetElementCheck.checked = true;
		}
		else if(properties.target == '_self')
		{
			targetElement.value = 0;
			targetElementCheck.checked = false;
		}
		if(properties.url != '')
		{
			rd_getElement(identifier+'_href').value = properties.url;
			rd_getElement('label_'+identifier).value = properties.text;
			rd_getElement(identifier).value = properties.url;
		}
		else if(properties.value != '')
		{
			rd_getElement('label_'+identifier).value = properties.text;
			rd_getElement(identifier).value = properties.value;
		}
	}
}