﻿/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * File:		SMJavaScript.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Purpose:	javascript global function library
 *			
 * Copyright:	Synaptic Mash 2008 All Rights Reserved
 *
 * Classes:
 *		BaloonTip
 *		
 * Requirements:
 *		*auto includes dependencies ifndef*
 *		prototype.js
 *
 * Developer contact:	Jason Hillier <jason.hillier@synapticmash.com>
 * 
 * Notes:
 * ------
 * javascript function lib for widely used client-side functionality
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

 function isdefined( variable)
 {
    return (typeof(window[variable]) == "undefined")?  false: true;
 }
 
 //for use in fixing dates
 function MicrosoftJSONSerializationFix(value) {
	if (value.DateTime)
		value = eval(value.DateTime);
	
	return value;
}
 
 //extend Array object to have this useful functionality
 Object.extend(Array, { MakeHashTable:
	 function MakeHashTable(object_array)
	 {
		for(var i=0; i<object_array.length; i++)
		{
			if (object_array[i].hash != null)
			{
				object_array[object_array[i].hash + ""] = object_array[i].hash;
			}
		}
	 } });
 
 function StyleRecursive(obj, st)
 {
	var obj=Element.Methods.cleanWhitespace(obj);
	
	for (var childIndex=0; childIndex<obj.childNodes.length; childIndex++)
	{
		try
		{
			$(obj.childNodes[childIndex]).setStyle(st);
		} catch (ex)
		{
			//alert(ex);
		}
		StyleRecursive(obj.childNodes[childIndex], st);
	}
 }
 
 function Base64Decode_HtmlSafe(decodeString)
 {
	//SEE: Base64Decode_HtmlSafe in SynapticMash.Framework
	return Base64.decode(decodeString.replace("_equals_", "=").replace('-', '+').replace('_', '/'));
 }
 
 //EnterToTab override---->
 //delegate for key handlers
var KeyHandlerPlugin = Class.create({
	_CallBack: null,
	_Keys: [],
	
	initialize: function(CallBack, Keys)
	{
		this._CallBack = CallBack;
		this._Keys = Keys;
	},
	
	CallBack: function(event)
	{
		return this._CallBack(event, this);
	}
});

 var DISABLE_ENTER_KEY = false;
 var DISABLE_ENTER_AUTOCLICK = false;
 var DEBUG_TAB = false;
 
 /*
cross-browser compatible activeElement property.
---------------------------
*/

var activeElement = null;

function onElementFocused(e)
{
    if (e && e.target)
        activeElement =
        e.target == document ? null : e.target;
} 

if (document.addEventListener)
    document.addEventListener("focus", onElementFocused, true);
//------------------

var DisableFocusFirst = false;
 
 var SMKeyHandler = {
	//constants
	TAB_KEYS: [ 9, 13 ],
	TAB_KEY: 9,
	ENTER_KEY: 13,
	FOCUSABLE_ELEMENT_TYPES: [ "INPUT", "A", "SELECT", "TEXTAREA" ],
	CANCEL_ENTERKEY_CLASSES: [ "eip" ],
	IGNORE_ENTERKEY_CLASSES: [ "FXInputDirector" ],
	CUSTOM_BUTTON_CLASSES: [ "SMSimpleLinkButton", "SMImageButton", "SMLinkButton" ],
	ABORT_SCAN_COUNT: 50, //abort DOM scan for new focus target after X elements.
	//
	_Handlers: [],
	CurrentElement: null,
	WasHandledFlag: false,
	_ScannedItemsCount: 0,
	
	Init: function()
	{
		Event.observe(document, "keypress", SMKeyHandler.handleKeyPress.bindAsEventListener());
		Event.observe(window, "dom:loaded", SMKeyHandler.focusFirst.bindAsEventListener());
		
		SMKeyHandler.AddHandler(new KeyHandlerPlugin(SMKeyHandler.enterToTabPlugin, [ SMKeyHandler.ENTER_KEY ]));
	},
	
	AddHandler: function(handler)
	{
		SMKeyHandler._Handlers.push(handler);
	},
	
	//all this looping junk needs optimized
	handleKeyPress: function(e)
	{
		var handlesKey = false;
		var matchingHandlers = [];
		
		//send keystroke to key handlers		
		for (var i=0; i<SMKeyHandler._Handlers.length; i++)
		{
			for ( var n = 0; n<SMKeyHandler._Handlers[i]._Keys.length; n++)
			{
				var key = SMKeyHandler._Handlers[i]._Keys[n];
				if (e.keyCode == key)
				{
					matchingHandlers.push(SMKeyHandler._Handlers[i]);
				}
			}
		}

		if (matchingHandlers.length <= 0)
			return;
		
		SMKeyHandler.CurrentElement = e.target;
		
		SMKeyHandler.WasHandledFlag = false;
		
		for (var i=0; i<matchingHandlers.length; i++)
		{
			if (matchingHandlers[i].CallBack(e))
				SMKeyHandler.WasHandledFlag = true;
		}
	},
	
	ScanForNext: function(startingNode)
	{
		SMKeyHandler._ScannedItemsCount = 0;
		
		var nextNode = SMKeyHandler.MoveNext(startingNode.parentNode, startingNode);
		if (nextNode != null)
		{
			SMKeyHandler.CurrentElement = nextNode;
			return true;
		}
		else
		{
			return false;
		}
	},
	
	//recursively scan remainder of DOM from position
	MoveNext: function(fromNode, startingChild)
	{
		var nextNode;
		
		if (fromNode == null)
			return null;
		
		if (startingChild == null)
		{
			if (SMKeyHandler._ScannedItemsCount > SMKeyHandler.ABORT_SCAN_COUNT)
			{
				SMLayoutManager.Log.Error("Exceeded max element scan count. DOM scan aborted.");
				return null;
			}
			if (SMKeyHandler.CheckIfNext(fromNode))
			{
				SMKeyHandler._ScannedItemsCount++;
				return fromNode;
			}
		}
		
		var hitChild = (startingChild == null);
		//scan children
		for(var i=0;i<fromNode.childNodes.length; i++)
		{
			if (!hitChild && (fromNode.childNodes[i] == startingChild))
			{
				hitChild = true;
				continue;
			}
			else if (!hitChild)
			{
				continue;
			}
			nextNode = SMKeyHandler.MoveNext(fromNode.childNodes[i], null);
			if (nextNode != null)
				return nextNode;
		}
		
		if (startingChild != null)
		{
			//go up
			nextNode = SMKeyHandler.MoveNext(fromNode.parentNode, fromNode);
			if (nextNode != null)
				return nextNode;
		}
		
		return null;
	},
	
	CheckIfNext: function(targetNode)
	{
		if (targetNode.tagName == null ||
			targetNode.tagName == 'undefined' ||
			targetNode.tagName == '!')
			return false;
			
		if (SMKeyHandler.isVisible(targetNode))
		{
			for (var n=0; n< SMKeyHandler.FOCUSABLE_ELEMENT_TYPES.length; n++)
			{
				if (targetNode.tagName == SMKeyHandler.FOCUSABLE_ELEMENT_TYPES[n])
				{
					if (DEBUG_TAB)
						SMLayoutManager.Log.Debug("Next is %o", targetNode);
					return true;
				}
			}
			
			for (var n=0; n < SMKeyHandler.CUSTOM_BUTTON_CLASSES.length; n++)
			{
				if ($(targetNode).hasClassName(SMKeyHandler.CUSTOM_BUTTON_CLASSES[n]))
				{
					if (DEBUG_TAB)
						SMLayoutManager.Log.Debug("Next is %o", targetNode);
					return true;
				}
			}
		}
		return false;
	},
	
	enterToTabPlugin: function(e, plugin)
	{
		if (DISABLE_ENTER_KEY)
		{
			Event.stop(e);
			return;
		}
		
		
		for(var i=0; i<SMKeyHandler.IGNORE_ENTERKEY_CLASSES.length; i++)
		{
			var className = SMKeyHandler.IGNORE_ENTERKEY_CLASSES[i];
			if (e.target.hasClassName(className) || $(e.target).up("." + className) != null)
			{
				return;
			}
		}
		
		for(var i=0; i<SMKeyHandler.CANCEL_ENTERKEY_CLASSES.length; i++)
		{
			var className = SMKeyHandler.CANCEL_ENTERKEY_CLASSES[i];
			
			if (e.target.hasClassName(className) || $(e.target).up("." + className) != null)
			{
				SMLayoutManager.Log.Info("ett cancelled on class '" + className + "'.");
				Event.stop(e);
				e.target.blur();
				return;
			}
		}
		
		if (e.target!=null && e.target.id!="undefined")
		{
			if (e.target.tagName.toUpperCase()=="TEXTAREA") return;
			
			//always stop enter key
			Event.stop(e);
			
			//if it has an explicit target, only use it ---->
			var ett_target = e.target.getAttribute('ett_target');
			
			if (ett_target != null)
			{
				var target = document.getElementById(ett_target);
				if (target == null)
				{
					SMLayoutManager.Log.Error("[enterToTabPlugin] Could not find ett_target '" + ett_target + "' element!");
				}
				else
				{
					return SMKeyHandler.PerformClick(document.getElementById(ett_target));
				}
			}
			//<-----------
			
			var defaultButton = $$(".DefaultFormButton");
			if (defaultButton.length>0)
			{
				SMKeyHandler.PerformClick(defaultButton[0]);
			}
			
			var useNext=false;
			
			if (SMKeyHandler.CurrentElement.tagName == "submit" ||
				SMKeyHandler.CurrentElement.tagName == "A" ||
				SMKeyHandler.CurrentElement.type == "button")
			{
				if (DEBUG_TAB)
					SMLayoutManager.Log.Debug("Submitting using: ", SMKeyHandler.CurrentElement);
				
				SMKeyHandler.simulateClick(SMKeyHandler.CurrentElement); return true;
			}
			
			if (SMKeyHandler.ScanForNext(SMKeyHandler.CurrentElement))
			{
				return SMKeyHandler.PerformClick(SMKeyHandler.CurrentElement);
			}
			
			SMLayoutManager.Log.Debug('not found');
			
			//if we made it here, than no more elements were found... or something...
			if (e.target.type=='submit')
			{
				e.target.click();
			}
			else
			{
				//else just blur the target
				e.target.blur();
			}
		}
		
		return false;
	},
	
	PerformClick: function(element)
	{
		if (DEBUG_TAB)
			SMLayoutManager.Log.Debug("Tabbing into: ", element);
		
		try
		{
			element.focus();
		} catch(ex){}
		if (element.type=="submit" || element.type=="button")
		{
			SMKeyHandler.simulateClick(element); return true;
		}
		
		if ($(element).hasClassName("DefaultFormButton"))
		{
			SMKeyHandler.simulateClick(element);
			return true;
		}
		
		
		//see: CUSTOM_BUTTON_CLASSES
		
		if ($(element).hasClassName("SMSimpleLinkButton"))
		{
			SMKeyHandler.simulateClick(element); return true;
		}
		else if ($(element).hasClassName("SMImageButton"))
		{
			SMKeyHandler.simulateClick(element.down(0)); return true;
		}
		else if ($(element).hasClassName("SMLinkButton"))
		{
			SMKeyHandler.simulateClick(element.down(0)); return true;
		}
		
		return true;
	},
	
	simulateClick: function(element)
	{
		if (DISABLE_ENTER_AUTOCLICK || element.hasClassName("DisableSubmitOnEnter"))
			return;
		
		
		//first, leave active element
		try
		{
			if (document.activeElement!=null)
				activeElement = document.activeElement;
				
			activeElement.blur();
			element.focus();
		}
		catch (ex) {}
		
		SMLayoutManager.Log.Debug("db: " + element.id, element);
		setTimeout(SMKeyHandler.simulateClickDebouce.bindAsEventListener(this, element), 1000);
	},
	
	simulateClickDebouce: function(e, element)
	{
		//then, click target
		SMLayoutManager.Log.Debug("Simulating click: %o", element);
		
		if (isIE)
		{
			element.click();
			return;
		}
		
		if (element.tagName=="A" && element.onclick==null)
		{
			window.location=element.href;
			return;
		}
		
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("click", true, true, window,
		0, 0, 0, 0, 0, false, false, false, false, 0, null);
		var canceled = !element.dispatchEvent(evt);
		/*if(canceled) {
		// A handler called preventDefault
		alert("canceled");
		} else {
		// None of the handlers called preventDefault
		alert("not canceled");
		}*/
	},
	
	focusFirst: function()
	{
		if (DisableFocusFirst)
			return;
			
		var elements = document.getElementsByTagName('*');
		
		for (var i=0; i<elements.length; i++)
		{
			if (elements[i].tagName=="INPUT" && SMKeyHandler.isVisible(elements[i]))
			{
				elements[i].focus();
				return;
			}
		}
	},

	isVisible: function(checkElement)
	{
		//SMLayoutManager.Log.Debug("check viz: %o", checkElement);
		
		element = $(checkElement);
		
		if (element.type && element.type.toUpperCase()=="HIDDEN")
			return false;
		
		try
		{
			if(element.getStyle){
				if (element.getStyle("display").toUpperCase()=="NONE")
					return false;
				if (element.getStyle("visibility").toUpperCase()=="HIDDEN")
					return false;
			}
		} catch (ex)
		{
		}
		
		if (element.parentNode!=null)
			return SMKeyHandler.isVisible(element.parentNode);
		
		return true;
	}
}

SMKeyHandler.Init();

//<------------
 
 //<summary>
 //static class of baloon and normal tool tip functions
 //</summary>
 var ToolTip = {
	_currentElement: null,
	_divToolTip: null,
	_divLastTarget: null,
	_divToolTipContent: null,
	_ToolTipItems: [],
	_cursorHandle: null,
	_toolText: '',
	_toolType: '',
	//this function is called directly from onmouseover event (it handles mousemove, mouseout binding)
	Set: function(evt, toolText, toolType)
	{
		if (ToolTip._divLastTarget && ToolTip._divLastTarget!=evt.target)
		{
			ToolTip.OnMouseUp(evt); //clear mouseup blocking if another element has tooltip
		}
		if (ToolTip._currentElement || $(evt.target).MouseUpHandler) return; //short-circuit on bubbling
		ToolTip._currentElement=evt.target;
		ToolTip._divLastTarget=null;
		ToolTip._cursorHandle=Event.Methods.pointer(evt);
		ToolTip._toolText=toolText;
		ToolTip._toolType=toolType;
		if (!ToolTip._toolType) ToolTip._toolType='';
		
		if (ToolTip._divToolTip==null)
		{
			if (ToolTip._toolType=="baloon")
			{
				ToolTip._divToolTip=document.createElement("DIV");
				$(ToolTip._divToolTip).addClassName("bubble_tooltip");
				$(document.body).appendChild(ToolTip._divToolTip);
				
				var obj
				obj=document.createElement("DIV");
				$(obj).addClassName("bubble_top");
				ToolTip._divToolTip.appendChild(obj);
				
				obj=document.createElement("DIV");
				$(obj).addClassName("bubble_middle");
				$(ToolTip._divToolTip).appendChild(obj);
				var middle=obj;
				
				obj=document.createElement("SPAN");
				ToolTip._divToolTipContent=obj;
				$(middle).appendChild(obj);
				
				obj=document.createElement("DIV");
				$(obj).addClassName("bubble_bottom");
				$(ToolTip._divToolTip).appendChild(obj);
				
			}
			else
			{
				ToolTip._divToolTip=document.createElement("DIV");
				$(ToolTip._divToolTip).addClassName("ToolTip");
				ToolTip._divToolTipContent=ToolTip._divToolTip;
				$(document.body).appendChild(ToolTip._divToolTip);
				$(ToolTip._divToolTip).setStyle({position: 'absolute', display: 'none'});
			}
		}
		
		if (!$(ToolTip._currentElement).ToolTipHandler)
		{
			Object.extend({ ToolTipHandler: null }, $(ToolTip._currentElement));
			
			$(ToolTip._currentElement).ToolTipHandler=ToolTip.Hide.bindAsEventListener(ToolTip)
			Event.observe(ToolTip._currentElement, "mouseout", $(ToolTip._currentElement).ToolTipHandler);
		}
		
		if (!$(ToolTip._currentElement).MouseMoveHandler)
		{
			Object.extend({ MouseMoveHandler: null }, $(ToolTip._currentElement));
			
			$(ToolTip._currentElement).MouseMoveHandler=ToolTip.OnMouseMove.bindAsEventListener(ToolTip)
			Event.observe(ToolTip._currentElement, "mousemove", $(ToolTip._currentElement).MouseMoveHandler);
		}
		
		if (!$(ToolTip._currentElement).MouseDownHandler)
		{
			Object.extend({ MouseDownHandler: null }, $(ToolTip._currentElement));
			
			$(ToolTip._currentElement).MouseDownHandler=ToolTip.OnMouseDown.bindAsEventListener(ToolTip)
			Event.observe(ToolTip._currentElement, "mousedown", $(ToolTip._currentElement).MouseDownHandler);
		}
		
		setTimeout("ToolTip.Show();", 1200);
	},
	
	Show: function()
	{
		if (!ToolTip._currentElement) return;
		ToolTip._divToolTipContent.innerHTML=ToolTip._toolText;
		if (ToolTip._toolType=='baloon')
		{
			var height=$(ToolTip._divToolTip).getDimensions().height;
			$(ToolTip._divToolTip).setStyle({display: '', top: (ToolTip._cursorHandle.y-height-5) + 'px', left: ToolTip._cursorHandle.x-100 + 'px'});
		}
		else
		{
			$(ToolTip._divToolTip).setStyle({display: '', top: (ToolTip._cursorHandle.y+10) + 'px', left: ToolTip._cursorHandle.x-20 + 'px'});
		}
	},
	
	//OnMouseOut
	Hide: function(e)
	{
		$(ToolTip._divToolTip).setStyle({display: 'none'});
		//
		Event.stopObserving(ToolTip._currentElement, 'mouseout', $(ToolTip._currentElement).ToolTipHandler);
		$(ToolTip._currentElement).ToolTipHandler=null;
		//
		Event.stopObserving(ToolTip._currentElement, 'mousemove', $(ToolTip._currentElement).MouseMoveHandler);
		$(ToolTip._currentElement).MouseMoveHandler=null;
		//
		ToolTip._divLastTarget=ToolTip._currentElement;
		ToolTip._currentElement=null;
		ToolTip._cursorHandle=null;
	},
	
	//OnMouseMove
	OnMouseMove: function(e)
	{
		ToolTip._cursorHandle=Event.Methods.pointer(e);
	},
	//OnMouseDown
	OnMouseDown: function(e)
	{
		Event.stopObserving(ToolTip._currentElement, 'mousedown', $(ToolTip._currentElement).MouseDownHandler);
		$(ToolTip._currentElement).MouseDownHandler=null;
		//
		//block until the mouse comes back up
		if (!$(ToolTip._currentElement).MouseUpHandler)
		{
			Object.extend({ MouseUpHandler: null }, $(ToolTip._currentElement));
			
			$(ToolTip._currentElement).MouseUpHandler=ToolTip.OnMouseUp.bindAsEventListener(ToolTip)
			Event.observe(ToolTip._currentElement, "mouseup", $(ToolTip._currentElement).MouseUpHandler);
		}
		//
		ToolTip.Hide();
	},
	//OnMouseUp
	OnMouseUp: function(e)
	{
		//stop blocking
		Event.stopObserving(e.target, 'mouseup', $(e.target).MouseUpHandler);
		$(e.target).MouseUpHandler=null;
	}
}

function AddQueryParameter(inStr, param)
{
	if (inStr.indexOf("?")<0)
	{
		return inStr + "?" + param;
	}
	else
	{
		return inStr + "&" + param;
	}
}

function getCssStyle(className)
{
	var cssObj=document.styleSheets[1].cssRules;
	if (!cssObj) cssObj=document.styleSheets[1].rules;
	
	var cssIndex;
	
	for (cssIndex=0; cssIndex<cssObj.length; cssIndex++)
	{
		if (cssObj[cssIndex].selectorText=="." + className)
		{
			return cssObj[cssIndex].style;
		}
	}
	
	return null;
}

function trim(val)
{
	return val.replace(/(\s+$)|(^\s+)/g, '');
}

function setCssStyle(className, styleArray)
{
	var sIndex=0;
	
	if (className.substring(0,1)=="." || className.substring(0,1)=="#")
	{
		//
	}
	else
	{
		className="." + className;
	}
	
	for (sIndex=0; sIndex<document.styleSheets.length; sIndex++)
	{
		var cssObj=document.styleSheets[sIndex].cssRules;
		if (!cssObj) cssObj=document.styleSheets[sIndex].rules;
		
		var cssIndex;
		
		for (cssIndex=0; cssIndex<cssObj.length; cssIndex++)
		{
			if (cssObj[cssIndex].selectorText==className)
			{
				try
				{
					cssObj[cssIndex].style=Object.extend(cssObj[cssIndex].style, styleArray);
				}
				catch (ex)
				{
					//alert(ex);
				}
				return true;
			}
		}
	}
	
	return false;
}

function stripHTML(inputHTML)
{
 	if(inputHTML)
 	{
 		//strip HTML
 		var strInputCode = inputHTML;
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
 		
 		//strip whitespace
 		strTagStrippedText = strTagStrippedText.replace("&nbsp", "");
 		strTagStrippedText = strTagStrippedText.replace('\n', '');
 		strTagStrippedText = strTagStrippedText.replace('\r', '');
 	}
 	
 	return strTagStrippedText;
}

function keyCodeToNumber(code)
{
	if (code >= 48 && code <= 57)
	{
		return code - 48;
	}
	else
	{
		return -1;
	}
}

//<summary>
//popup window class (static)
//</summary>
var SMPopupWindow={
	_Current: [],
	_PopupOptions: [],
	_zTop: 9999,
	
	Create: function(PopupName)
	{
		var options=Object.extend({
			PopupURL: '',
			ShowPopup: true,
			FullScreen: false,
			PopupHandle: null,
			Width: 500,
			Height: 400
			}, arguments[1] || { });
		
		SMPopupWindow._PopupOptions.push({Name: PopupName, Options: options});
		
		SMPopupWindow.Render(PopupName);
		
		if (SMPopupWindow.Options(PopupName).ShowPopup)
			SMPopupWindow.Show(PopupName);
	},
	PopupInit: function(PopupName)
	{
		SMPopupWindow._Current = window.parent.SMPopupWindow._Current;
	},
	Options: function(PopupName)
	{
		for (var index=0; index<SMPopupWindow._PopupOptions.length; index++)
		{
			if (SMPopupWindow._PopupOptions[index].Name==PopupName)
			{
				return SMPopupWindow._PopupOptions[index].Options;
			}
		}
		
		alert("[SMPopupWindow] ERROR: PopupName not found!");
		return null;
	},
	Render: function(PopupName)
	{
		var url = SMPopupWindow.Options(PopupName).PopupURL;
		var FullScreen = SMPopupWindow.Options(PopupName).FullScreen;
		
		var PopupHandle=document.getElementById(PopupName + "_DIV");
		var PopupHandle_IFRAME=null;
		var PopupHandle_HEADER=null;
		
		if (PopupHandle)
		{
			$(PopupHandle).remove();
		}
		
		//ifndef("dragdrop");
		
		PopupHandle=document.createElement("DIV");
		PopupHandle.id=PopupName + "_DIV";
		document.body.appendChild(PopupHandle);
		
		this._zTop++;
		$(PopupHandle).setStyle({zIndex: this._zTop});
		
		PopupHandle_IFRAME=document.createElement('IFRAME');
		PopupHandle_IFRAME.id=PopupName + "_IFRAME";
		PopupHandle_IFRAME.scrolling='no';
		
		if (!FullScreen)
		{
			PopupHandle_HEADER=document.createElement("DIV");
			PopupHandle_HEADER.innerHTML="&nbsp;";
			PopupHandle.appendChild(PopupHandle_HEADER);
			$(PopupHandle_HEADER).setStyle({height: '25px'});
			
			new Draggable(PopupName + "_DIV", {MouseOutContainer: PopupHandle });
		}
		
		PopupHandle.appendChild(PopupHandle_IFRAME);

		Object.extend(PopupHandle, {IFRAME: PopupHandle_IFRAME, HEADER: PopupHandle_HEADER, ReplaceElement:null});

		PopupHandle_IFRAME=$(PopupHandle).IFRAME;
		PopupHandle_HEADER=$(PopupHandle).HEADER;
		
		//something is wrong with css add so we will do it manually for now --
		//$(SMLayoutManager._LocalizationPopupDIV).addClassName="Localization_Popup_DragHandle";
		if (!FullScreen)
		{
			$(PopupHandle).setStyle({backgroundColor: '#5E6E7D', cursor:'move', borderTop: 'solid 1px #000000'});
		}
		//--
		
		if (!FullScreen)
		{
			$(PopupHandle).setStyle({position:'absolute', left:'100px', top:'60px', visibility: 'hidden'});
			$(PopupHandle_IFRAME).setStyle({display:'block', border:'solid 2px #5E6E7D', borderTop:'none', backgroundColor:'#FFFFFF', zIndex:'9999',height:SMPopupWindow.Options(PopupName).Height + 'px',width:SMPopupWindow.Options(PopupName).Width + 'px'});
		}
		else
		{
			$(PopupHandle).setStyle({position:'absolute', left:'0px', top:'0px', width:'100%', height:'100%', visibility: 'hidden'});
			$(PopupHandle_IFRAME).setStyle({display:'block', border:'0px', zIndex:'9999',height:'100%',width:'100%'});
			PopupHandle_IFRAME.allowTransparency=true;
		}
		PopupHandle_IFRAME.frameborder=0;
		PopupHandle_IFRAME.scrolling='no';
		PopupHandle_IFRAME.src=url;
		
		SMPopupWindow.Options(PopupName).PopupHandle = PopupHandle;
	},
	Show: function(PopupName)
	{
		SMPopupWindow._Current={Name: PopupName, Options: SMPopupWindow.Options(PopupName)};
		
		var PopupHandle = SMPopupWindow.Options(PopupName).PopupHandle;
		$(PopupHandle).setStyle({visibility:''});
	},
	Close: function(PopupName)
	{
		if (!PopupName)
		{
			window.parent.SMPopupWindow.Close(SMPopupWindow._Current.Name);
		}
		else
		{
			var PopupHandle = SMPopupWindow.Options(PopupName).PopupHandle;
			$(PopupHandle).setStyle({visibility:'hidden'});
			SMPopupWindow._Current=null;
		}
	},
	ShowOverlay: function(opacity, color)
	{
		if (color==null) color='#000000';
		
		//fade out background
		if (!this._Overlay)
		{
			this._Overlay=document.createElement("DIV");
			document.body.appendChild(this._Overlay);
		}
		
		var width=document.body.scrollWidth;
		var height=document.body.clientHeight+document.body.scrollHeight;
		
		if (!isIE)
		{
		    width=window.innerWidth;
		    height=window.innerHeight;
		}
		
		$(this._Overlay).setStyle({position:'absolute', backgroundColor:color,zIndex:1});
		this._Overlay.style.width=width + "px";
		this._Overlay.style.height=height + "px";
		this._Overlay.style.top=0;
		this._Overlay.style.left=0;
		this._Overlay.style.opacity=0;
		this._Overlay.style.filter="alpha(opacity=0)"; 
		new Effect.Appear(this._Overlay, { duration: 0.2, from: 0.0, to: opacity });
		this._Overlay.style.display='';
	}
};


//CROSS-BROWSER COMPATIBILITY-----

function SetOuterHTML (ElementID, txt)
{
  var someElement = document.getElementById(ElementID);

  if(someElement.outerHTML)
  {
    // The browser supports Microsoft's outerHTML, so use that
    someElement.outerHTML = txt;
  }
  else
  { 
    // The browser doesn't support outerHTML, try an alternative

    // Create a range
    var range = document.createRange();

    // Set the range's starting point to where the HTML needs to be inserted
    // SetStartAfter seems to do the same thing in this case.

    range.setStartBefore(someElement);

    // Create a fragment from the HTML
    var docFrag = range.createContextualFragment(txt);

    // Go to the parent node and replace the element with the new HTML fragment
    // For innerHTML simulation use someElement.appendChild(docFrag);  
    someElement.parentNode.replaceChild(docFrag,someElement);  
  }
}

function gStopEventPropagation(args)
{
	var e = args[0] || window.event;
	
	if (isIE)
      e.cancelBubble = true;
    else
      e.stopPropagation();
}

// browser detection: http://www.quirksmode.org/js/detect.html
var Browser = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "WebKit",
			identity: "WebKit"
		},
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	],
	isIE:function(){
		return Browser.browser == 'Explorer';
	},
	isWebKit:function(){
		return Browser.browser == 'WebKit';
	},
	isIE6:function(){
		return Browser.isIE() && Browser.version < 7;
	}
};
Browser.init();

var isWebKit = Browser.isWebKit();
var isIE=Browser.isIE();
var isIE6=Browser.isIE6();
/*
if (isIE6){
	var clear="/media/js/images/clear.gif" // for PNG fix

	pngfix=function(){var els=document.getElementsByTagName('*');var ip=/\.png/i;var i=els.length;while(i-- >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";el.src=clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(ip)){var path=elb.split('"');var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';var elkids=el.getElementsByTagName('*');if (elkids){var j=elkids.length;if(el.currentStyle.position!="absolute")es.position='static';while (j-- >0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}}
	window.attachEvent('onload',pngfix);
}
*/
