
//Utilities
var emwUtilitiesManager = 
{
	addEvent : function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener)
		{ 
			elm.addEventListener(evType, fn, useCapture); 
			return true; 
		}
		else if (elm.attachEvent)
		{ 
			var r = elm.attachEvent('on' + evType, fn); 
			return r; 
		}
		else
		{
		    elm['on' + evType] = fn;
		}
	},
	
	ascendDOM : function(e, target)
	{
	  while (e.nodeName.toLowerCase() != target.toLowerCase() && e.nodeName.toLowerCase() != 'html')
	    e = e.parentNode;
	  
	  return (e.nodeName.toLowerCase() == 'html') ? null : e;
	},
	
	ReloadCurPage : function(pageURL)
	{
	    location.href = pageURL;
	},
	
	OpenWindow : function(url,name,scroll,tool,dir,menu,resize,status,loc,width,height)
    {
        var winObj = null;
	    winObj = window.open(url,name,"scrollbars=" + scroll + ",toolbar=" + tool + ",directories=" + dir + ",menubar=" + menu + ",resizable=" + resize + ",status=" + status + ",location=" + loc + ",width=" + width + ",height=" + height);
	    winObj.focus();
    },
	
	OpenWindowSimple : function(url, windowName, width, height)
	{
	    var winObj = null;
	    winObj = window.open(url,windowName,"toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=" + width + ",height=" + height);
	    winObj.focus();
	},
	
	GetURLArgs : function()
	{
		var args = new Object();
		var query = location.search.substring(1);
		// Get query string
		var pairs = query.split("&");
		// Break at comma
		for(var i = 0; i < pairs.length; i++)
		{
			var pos = pairs[i].indexOf('=');
			  // Look for "name=value"
			if (pos == -1) continue;
			  // If not found, skip
			var argname = pairs[i].substring(0,pos);
			  // Extract the name
			var value = pairs[i].substring(pos+1);
			  // Extract the value
			args[argname] = unescape(value);
			 // Store as a property
			// In JavaScript 1.5, use decodeURIComponent(  ) 
			// instead of escape(  )
		}
		return args;
	},
	
	GetCookie : function(name)
	{
		var cname = name + "=";               
		var dc = document.cookie;             
		if (dc.length > 0)
		{              
			begin = dc.indexOf(cname);       
			if (begin != -1)
			{           
				begin += cname.length;       
				end = dc.indexOf(";", begin);
				if (end == -1)
					end = dc.length;
				return unescape(dc.substring(begin, end));
			} 
		}
		return null;
	},
	
	// Setting a cookie.
	SetCookie : function(name, value, expires, path, domain, secure)
	{
		document.cookie = name + "=" + escape(value) + 
		((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
		((path == null) ? "" : "; path=" + path) +
		((domain == null) ? "" : "; domain=" + domain) +
		((secure == null) ? "" : "; secure");
	},
	
	// Deleting a cookie.
	DelCookie : function(name,path,domain)
	{
		if (emwUtilitiesManager.GetCookie(name))
		{
			document.cookie = name + "=" +
			((path == null) ? "" : "; path=" + path) +
			((domain == null) ? "" : "; domain=" + domain) +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	},
	
	ShowDropLists : function()
    {            
            var elementsToToggle = (document.getElementsByTagName) ? document.getElementsByTagName('select') : document.all.tags('select');
            
            for(var j=0; j<elementsToToggle.length; j++)
            {
                var theElement = elementsToToggle[j];
                if(document.getElementById)
                    theElement.style.visibility = 'visible';
                else
                    theElement.visibility = 'visible';
            }
    },
    
    HideDropLists : function()
    {            
            var elementsToToggle = (document.getElementsByTagName) ? document.getElementsByTagName('select') : document.all.tags('select');
           
            for(var j=0; j<elementsToToggle.length; j++)
            {
                var theElement = elementsToToggle[j];
                if(document.getElementById)
                    theElement.style.visibility = 'hidden';
                else
                    theElement.visibility = 'hide';
            }
    },

    ShowModalPopupViaClient: function(pleaseWaitText)
    {
          var txtPleaseWaitObj = document.getElementById('txtPleaseWait'); 
          txtPleaseWaitObj.innerHTML = pleaseWaitText;

          var elem = document.getElementById('dimmer'); 
          
          if(elem != null)
          { 
                emwUtilitiesManager.addEvent(elem,'mousemove', emwUtilitiesManager.HideDropLists, false);
                elem.style.visibility  = 'visible'; 
                elem.style.width=window.screen.width +'px';
                elem.style.height=window.screen.height +'px';
                elem.style.left=document.body.scrollLeft;
                elem.style.top=document.body.scrollTop; 
          }
      
          var elem2 = document.getElementById('windowcontent'); 
      
          if(elem2 != null && pleaseWaitText != "")
          {
                elem2.style.visibility  = 'visible';
                var xPos = ((window.screen.width-parseInt(elem2.style.width))/2); 
                elem2.style.left=document.body.scrollLeft + xPos+'px';
                var yPos = ((window.screen.height-parseInt(elem2.style.height))/2); 
                elem2.style.top=document.body.scrollTop + yPos+'px'; 
          }  
    },
    
    HideModalPopupViaClient: function()
    {
          var elem = document.getElementById('dimmer');          
          if(elem != null)
          { 
                elem.style.visibility  = 'hidden';                 
          }
      
          var elem2 = document.getElementById('windowcontent');      
          if(elem2 != null)
          {
                elem2.style.visibility  = 'hidden';                
          }
          
          //Show Drop Lists
          emwUtilitiesManager.ShowDropLists();
    },
    
    
    //Toggle Element on and off with indicator
    ToggleElement : function(buttonElId, panelElId, minimizeButtonURL, maximizeButtonURL)
    {		
	    if(document.getElementById(panelElId))
	    {
	        var panel = document.getElementById(panelElId);
	        if(panel.style.display == 'none')
	            panel.style.display = 'block';		
	        else
	            panel.style.display = 'none';		
	    	    
	        if(relatedButtonURL != null && document.getElementById(buttonElId))
	        {
	            var button = document.getElementById(buttonElId);
	            if(panel.style.display == 'none')
	                button.src = minimizeButtonURL;
	            else
	                button.src = minimizeButtonURL;
	        }
	    }
    },
	
	//--- Rollover Image Functions ---//
    PreloadImages : function() {
      if (document.images) {
        var imgFiles = emwUtilitiesManager.PreloadImages.arguments;
        if (document.preloadArray==null) document.preloadArray = new Array();
        var i = document.preloadArray.length;
        with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
          preloadArray[i] = new Image;
          preloadArray[i++].src = imgFiles[j];
      } }
    },

    SwapImgRestore : function() {	
      if (document.spm_swapImgData != null)
      {
        for (var i=0; i<(document.spm_swapImgData.length-1); i+=2)
        {
    	    document.spm_swapImgData[i].src = document.spm_swapImgData[i+1];
        }
      }
    },

    SwapImage : function() {
      var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.spm_swapImgData;
      
      for (i=0; i < (spm_swapImage.arguments.length-2); i+=3) {
        objStr = spm_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
        if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
            (objStr.indexOf('document.all[')   ==0 && document.all   ==null))
          objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
        obj = eval(objStr);
        if (obj != null) {
          swapArray[j++] = obj;
          swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
          obj.src = spm_swapImage.arguments[i+2];
      } }
      document.spm_swapImgData = swapArray; //used for restore
    },
    
    GLOBAL_UPDATE_PANEL_ID : null,
    GLOBAL_IFRAME_DIV_ID : null,
    
    ShowContentThroughLayerAndIFrame : function(iFrameName, contentURL, layerId, updatePanelId, virtualDir, actionType, reloadEventsDelay)
    {
	    if(document.getElementsByTagName('body').length > 0)
	    {
		    bodyEl = document.getElementsByTagName('body')[0];
		    domId = "div" + iFrameName;
		    
		    //Set the Global Update Panel Id for other frames to use
		    emwUtilitiesManager.GLOBAL_IFRAME_DIV_ID = domId;
		    emwUtilitiesManager.GLOBAL_UPDATE_PANEL_ID = updatePanelId;
		    		    
		    //Fade the background
		    emwUtilitiesManager.ShowModalPopupViaClient("");
    		
		    var dom = null;
		    if(document.getElementById(domId))
		    {
		        dom=document.getElementById(domId);
			    dom = null;
		    }
		  
		    dom=document.createElement('div');
		    dom.id=domId;
		    dom.className="FormLayerDraggable";
		    bodyEl.appendChild(dom);
		    
		    ppHolder=document.createElement('table');
		    ppHolder.className = "PopupButtons";
		    ppTbody=document.createElement('tbody');
		    ppTR=document.createElement('tr');
		    //Insert the close button
		    ppMove=document.createElement('td');
		    ppMove.className = "PopupMoveButton";
		    ppMove.innerHTML+='<img class="PopupMoveButton" src="' + virtualDir + 'icon_move.gif" vspace="0" hspace="0" border="0" />';
		    ppTR.appendChild(ppMove);
		        		    
			ppClose=document.createElement('td');
		    ppClose.className = "PopupCloseButton";
		    ppClose.innerHTML+="<img class=\"PopupCloseButton\" src=\"" + virtualDir + "icon_close.gif\" vspace=\"0\" hspace=\"0\" border=\"0\" onclick=\"emwUtilitiesManager.CloseIFrameLayerById('" + domId + "','" + updatePanelId + "','" + actionType + "'," + reloadEventsDelay + ");\" />";
		    ppTR.appendChild(ppClose);
		    ppTbody.appendChild(ppTR);
		    ppHolder.appendChild(ppTbody);
		    dom.appendChild(ppHolder);
		    
		    //Holder Div
		    iframeHolder=document.createElement('div');
		    iframeHolder.id="IFrameHolder_"+iFrameName;
		    dom.appendChild(iframeHolder);
    			
		    //Built the IFrame
		    var iFrame = emwUtilitiesManager.CreateIFrame(iFrameName,iFrameName,contentURL);
		    iframeHolder.appendChild(iFrame);
		    //iFrame.contentWindow.location.reload(true);
			
		    //Show Layer
		    dom.style.display='block';
		    dom.style.visibility = "visible";
		    dom.style.width = "640px";
		    dom.style.height = "480px";
		    var xPos = ((window.screen.width-parseInt(dom.style.width))/2);
		    dom.style.left = document.body.scrollLeft + xPos + 'px';
            var yPos = ((window.screen.height-parseInt(dom.style.height))/2); 
            dom.style.top = document.body.scrollTop + yPos + 'px';
            
            if(typeof(Rico) != 'undefined' && Rico != null)
            {
                //var roundCorners = Rico.Corner.round.bind(Rico.Corner);
                //roundCorners(dom.id);
            }
            			        			
		    //Add Drag Event			
		    emwUtilitiesManager.addEvent(document, 'mousedown', emwUtilitiesManager.DragHandler, false);			    		    
	    }
    },
    
    CloseIFrameLayer : function(el, updatePanelId, actionType, reloadEventsDelay)
    {
        if(el.tagName != "div")
        {
	        el = emwUtilitiesManager.ascendDOM(el,"div");
        }
	    if(el != null)
	    {
	        el.style.display='none';
	        el.style.visibility = "hidden";
	        el.parentNode.removeChild(el);
	        el = null;
	    }
	    
	    //Fade the background
		emwUtilitiesManager.HideModalPopupViaClient();
		if(typeof(actionType) != 'undefined' && actionType != null && (actionType == "addParentContent" || actionType == "editParentContent"))
		{		
		    if(parent != null)
		        parent.location.reload(true);		    
		}
		else if(__doPostBack && document.getElementById(updatePanelId) != null)
		{
		    __doPostBack(updatePanelId,'customPostback');
		    //Hook up Layer Events
		    if(reloadEventsDelay == null)
		        reloadEventsDelay = 2500;
		    var reloadTimer = setTimeout(emwUtilitiesManager.ReloadJSEventsAfterIFrameClose,reloadEventsDelay);
		}
    },
    
    CloseIFrameLayerById : function(iframeId, updatePanelId, actionType, reloadEventsDelay)
    {
        if(document.getElementById(iframeId) != null)
        {
	        el = document.getElementById(iframeId);
	        el.style.display='none';
	        el.style.visibility = "hidden";
	        el.parentNode.removeChild(el);
	        el = null;
        }
	    
	    //Fade the background
		emwUtilitiesManager.HideModalPopupViaClient();
		if(typeof(actionType) != 'undefined' && actionType != null && (actionType == "addParentContent" || actionType == "editParentContent"))
		{		
		    if(parent != null)
		        parent.location.reload(true);		    
		}
		else if(__doPostBack && document.getElementById(updatePanelId) != null)
		{
		    __doPostBack(updatePanelId,'customPostback');
		    
		    //Hook up Events
		    if(reloadEventsDelay == null)
		        reloadEventsDelay = 2500;
		    var reloadTimer = setTimeout(emwUtilitiesManager.ReloadJSEventsAfterIFrameClose,reloadEventsDelay);		        		    
		}
    },
    
    ReloadJSEventsAfterIFrameClose: function()
    {
        //Hook up Layer Events
	    if(typeof(LayerManager) != 'undefined' && LayerManager != null)
	    {
	        LayerManager.Initialise();
	    }
	    //Hook Up Table Events
	    if(typeof(emwTableManager) != 'undefined' && emwTableManager != null)
	    {
	        emwTableManager.addListeners();
	    }
	    //Hook Up Any Drag Drop Interfaces
	    if(typeof(dragDropInterface) != 'undefined' && dragDropInterface != null)
	    {
	        //dragDropInterface();
	    }	    
    },

    CreateIFrame : function(id,name,location)
    {
	    var iframe;
	    if(document.getElementById(id))
	    {
		    iframe=document.getElementById(id);
	    }
	    else
	    {
		    iframe=document.createElement('iframe');
		    iframe.id=id;
		    iframe.name=name;
		    iframe.className="ContentIFrame";
		    //iframe.src=location;
		    //iframe.href=location;
		    iframe.setAttribute("width", 640);
		    iframe.setAttribute("height", 480);
		    //iframe.setAttribute("border", 0);
		    iframe.setAttribute("frameborder", 0);
		    iframe.setAttribute("scrolling", "yes");
		    iframe.setAttribute("src", location);
	    }	
	    return iframe;
    },

    savedTarget : null,
    orgCursor : null,
    dragOK : false,
    dragXoffset : 0,
    dragYoffset : 0,   

    MoveHandler : function(e)
    {
	    var el;
	    if (window.event && window.event.srcElement)
		    el = window.event.srcElement;
	    if (e && e.target)
		    el = e.target;
	    if (!el)
		    return;
    	
	    if (e == null)
	    {
		    e = window.event
	    }
    	
	    if (e.button<=1 && emwUtilitiesManager.dragOK)
	    {
		    emwUtilitiesManager.savedTarget.style.left=e.clientX-emwUtilitiesManager.dragXoffset+'px';
		    emwUtilitiesManager.savedTarget.style.top=e.clientY-emwUtilitiesManager.dragYoffset+'px';
		    return false;
	    }
    },
    
    CleanupDrag : function(e)
    {
	    var el;
	    if (window.event && window.event.srcElement)
		    el = window.event.srcElement;
	    if (e && e.target)
		    el = e.target;
	    if (!el)
		    return;
    		
	    document.onmousemove=null;
	    document.onmouseup=null;
	    emwUtilitiesManager.savedTarget.style.cursor=emwUtilitiesManager.orgCursor;
	    emwUtilitiesManager.dragOK=false;
    },
    
    DragHandler : function(e)
    {
	    var htype='-moz-grabbing';
	    var el;
	    if (window.event && window.event.srcElement)
		    el = window.event.srcElement;
	    if (e && e.target)
		    el = e.target;
	    if (!el)
		    return;
    		
	    if (e == null)
	    {
		    e = window.event;
		    htype='move';
	    }
    	
	    emwUtilitiesManager.orgCursor=el.style.cursor;
	    if(el.tagName != "div")
        {
	        el = emwUtilitiesManager.ascendDOM(el,"div");
        }
	    
	    if(el != null && typeof(el) != 'undefined' && el.className.indexOf("Draggable") != -1)
	    {
	        emwUtilitiesManager.savedTarget=el;
		    el.style.cursor=htype;
		    emwUtilitiesManager.dragOK=true;
		    if(el.style.left != "")
		    {
			    emwUtilitiesManager.dragXoffset=e.clientX-parseInt(el.style.left);		
		    }
		    else
		    {
			    emwUtilitiesManager.dragXoffset=e.clientX;		
		    }
		    if(el.style.top != "")
		    {
			    emwUtilitiesManager.dragYoffset=e.clientY-parseInt(el.style.top);
		    }
		    else
		    {
			    emwUtilitiesManager.dragYoffset=e.clientY;
		    }
		    emwUtilitiesManager.addEvent(document, 'mousemove', emwUtilitiesManager.MoveHandler, false);
		    emwUtilitiesManager.addEvent(document, 'mouseup', emwUtilitiesManager.CleanupDrag, false);	
		    return false;
	    }
    },
    
    ShortenCurrentDomain : function()
    {
        var d=document.domain;
        var ar=document.domain.toLowerCase().split(".");
        if(ar.length>1)
        {
            if((ar[ar.length-2]=="com")||(ar[ar.length-2]=="org")||(ar[ar.length-2]=="net")||(ar[ar.length-2]=="co"))
	            ar=ar.slice(ar.length-3);
            else
	            ar=ar.slice(ar.length-2);
            d = ar.join(".");
        }
        return d;
    }
}


