﻿
//if ( document.all && !document.getElementById )
//{
//��� document.getElementById = function(id) { return document.all[id]; }
//}

//
// ****  GENERAL FUNCTIONS  ****
//

function pseudoStandardizeName (n)
{
  if(n == null) return n;
  n = n.replace(/[\[ !""#%'@^&*+,:;<>?\/\]\\]/g,'_');
  return n;
}

function indexOf(a,e)
{
  for(i=0;i<a.length;i++)
    if(a[i]==e)return i;
  return -1;
}

function trim(s)
{
    if ( s==null || s=="" ) return "";
	return s.replace(/(^\s+)|(\s+$)/g, '');
}

//---------------------------------------
// strip skin tags
//---------------------------------------
function stripSkinTags(s)
{
	// apply regular expression
	var matches = new RegExp("(<ski" + "n id=\\d+>)?([\\w\\W]+)", "i").exec(s)

	// return stripped string (assumes terminal </skin> when opening skin tag present)
	return (matches[1] == "" ? s : matches[2].substring(0, matches[2].length - 7));
}	

//
// ****  DETECT BROWSER  ****
//

function _isMac() 
{
	var agt = navigator.userAgent.toLowerCase();
	return agt.indexOf("mac") != -1;
}

function _isIE() 
{
	var agt = navigator.userAgent.toLowerCase();
	return agt.indexOf('msie') != -1;
}

function WindowWidth()
{
    //Non-IE
    if( window.document.body!=null && typeof( window.document.body.clientWidth ) == 'number' )
      return window.document.body.clientWidth; 

    //IE 6+ in 'standards compliant mode'
    else if ( window.document.documentElement && window.document.documentElement.clientWidth ) 
      return window.document.documentElement.clientWidth;

    //IE 4 compatible
    else if ( window.document.body && window.document.body.clientWidth ) 
      return window.document.body.clientWidth;
}

function WindowHeight()
{
    //Non-IE
    if( typeof( window.innerHeight ) == 'number' )
      return window.innerHeight;

    //IE 6+ in 'standards compliant mode'
    else if ( window.document.documentElement && window.document.documentElement.clientHeight ) 
      return window.document.documentElement.clientHeight;

    //IE 4 compatible
    else if ( window.document.body && window.document.body.clientHeight ) 
      return window.document.body.clientHeight;
}

var hintHandler = null;
var guideHandler = null;

function MouseMoveHandler(e)
{
    if ( hintHandler!=null ) hintHandler(e);
    if ( guideHandler!=null ) guideHandler(e);
}
//
// ****  HINTS  ****
//
// hintSwitch is a global JS variable - if it exists and is set to true, 
// then do hint stuff
// hintText is the span which contains the tooltip text
// hint is the span containing the tooltip which is used for positioning

function showHint( me,mynote ) 
{
	if (!hintSwitch) return false; 
	if (mynote.length==0) return false; 
	// moveHint();
	
	hintHandler=null; // document.onmousemove = null;
	
	var hint = document.getElementById("hint");
	
	hint.innerHTML = mynote;
	if ( parseInt(hint.style.width) > 200 ) hint.style.width ="200px";	
	hint.style.display = 'block';
	hint.style.left = "-500px";
	hint.style.top = "-500px";	
	// hintText.style.zIndex = 1;  unable to stack above SELECT box (see html doc)
	me.onmouseout = hideHint;
	
	hintHandler=moveHint; // document.onmousemove = moveHint;
	document.onmousemove=MouseMoveHandler;
}
	  
function moveHint(e) 
{
  var mainContentEl = document.getElementById("main_content");
  if ( mainContentEl==null ) return; 
  var mainContent_left = getElementLeft(mainContentEl);
	var hint = document.getElementById("hint");
	if ( hint==null ) return; 
	
  var x = 0;
	if ( window.document.all )
	{
	  if ( event==null ) return;
	  var eventX = parseInt(event.x);
	  x = mainContent_left + eventX + 10 -20;  
	}
	else
	{
      if ( e==null ) return;
      var eventX = parseInt(e.pageX);
      x = eventX + 10; 
	}      
  // dont run off right edge of screen
  if ( x+300 > parseInt(document.body.offsetWidth) ) 
    x = parseInt(document.body.offsetWidth) - 300;
      
  var y = e ? 10+parseInt(e.pageY) : 10+parseInt(event.clientY)+document.documentElement.scrollTop;
  
	hint.style.top = ""+y+"px";
	hint.style.left = ""+x+"px";
}

function hideHint() 
{
	var hint = window.document.getElementById("hint");

	hint.style.display = 'none';	
	hintHandler=null; // document.onmousemove = null;
}

//
// ****  MOUSE GUIDE  ****
//
// horizontal bar as mouse guide

// show mouse guide
function guideOn()
{
	var mg = document.getElementById("MouseGuide");
	if ( mg==null ) return;

  var x = 0.5 * (parseInt(WindowWidth())-970);
	
	mg.style.display = 'block';
	mg.style.left=""+x+"px";
//  mg.style.top = "0px";

	guideHandler=guideUpdate;
	document.onmousemove = MouseMoveHandler; 
}

// update Y of mouse guide
function guideUpdate(e) 
{
  var mainContentEl = document.getElementById("main_content");
  if ( mainContentEl==null ) return; 
  var mainContent_left = getElementLeft(mainContentEl);
	var mg = document.getElementById("MouseGuide");
	if ( mg==null ) return;
	
  var x = 0.5 * (parseInt(WindowWidth())-970);		  
  var y = e ? 5+parseInt(e.pageY) : 5+parseInt(event.clientY)+document.documentElement.scrollTop;
  
	mg.style.left=""+x+"px";
  mg.style.top = ""+y+"px";
  diag(mg.style.top);
}

// hide mouse guide
function guideOff()
{
	var mg = window.document.getElementById("MouseGuide");
	if( mg==null ) return;
	mg.style.display = 'none';
}

//
// ****  JOBBER  ****
//
function _hud_pos() 
{
	var _hud = window.document.getElementById("_hud");
	if ( _hud==null ) return; 

	var _hudfv = window.document.getElementById("_hudfv");
	if ( _hudfv==null ) return; 

    // window dimensions	
	var iScrW = parseInt(WindowWidth()); 
	if ( !document.all ) iScrW+=23;
	
	var iScrH = WindowHeight();

    // scroll position
	var iScrT = 0; // parseInt(oD.body.scrollTop);
    if (document.documentElement && document.documentElement.scrollTop)
        iScrT = document.documentElement.scrollTop;
    else if (document.body)
        iScrT = document.body.scrollTop; 

	// set positions
	
//	var _hud_x;
//	if (document.all)
//	  _hud_x = parseInt(iScrW - _hud_width - 4+20 - 35);
//	else
//	  _hud_x = parseInt(iScrW - _hud_width - 4+20 - 58);
	  
  var _hud_x = 0;
  if ( document.all )
    _hud_x = 960 + 0.5 * (iScrW-980);			  
  else
    _hud_x = 950 + 0.5 * (iScrW-980);			  
	
//	if ( document.all )
//	{
//	  if ( _hud_x<885 ) _hud_x=885;
//	}
//	else
//	{
//	  if ( _hud_x<905 ) _hud_x=905;
//	}
  var MenuArrows = document.getElementById("MenuArrows");

	_hud.style.left = ""+_hud_x+"px";
  _hudfv.style.left=""+(_hud_x-66)+"px";
  
  if ( MenuArrows!=null ) MenuArrows.style.left="250px";//""+_hud_x+"px";

	var _hud_y = parseInt(iScrH/2 + iScrT);
	_hudfv.style.top = ""+(_hud_y - _hud_height)+"px"; 
	_hud.style.top = ""+_hud_y+"px";

  if ( MenuArrows!=null ) 
  {
    if ( document.all )
      MenuArrows.style.top=""+(_hud_y-200)+"px"; 
    else
      MenuArrows.style.top=""+(_hud_y-160)+"px"; 
  }

	if(_hud.style.visibility == 'hidden')
	{
		_hud.style.visibility = 'visible';
  }
  if ( MenuArrows!=null && MenuArrows.style.visibility=="hidden")
      MenuArrows.style.visibility="visible"; 
      
//      alert( "MenuArrows.style: "+
//        MenuArrows.style.position+" "+
//        MenuArrows.style.display+" "+
//        MenuArrows.style.visibility+" "+
//        MenuArrows.style.zIndex+" "+
//        MenuArrows.style.left+" "+
//        MenuArrows.style.top);
//      alert( "Jobber.style: "+
//        _hud.style.position+" "+
//        _hud.style.display+" "+
//        _hud.style.visibility+" "+
//        _hud.style.zIndex+" "+
//        _hud.style.left+" "+
//        _hud.style.top);
}

function _hud_fullview(sw) 
{
	var _hud = window.document.getElementById("_hud");
	var _hudfv = window.document.getElementById("_hudfv");
	if (sw) 
	{
		_hudfv.style.visibility = 'visible';
		_hud.style.visibility = 'hidden';
	}
	else 
	{
		_hudfv.style.visibility = 'hidden';
		_hud.style.visibility = 'visible';
	}
	return false;
}

function _hud_click(x) 
{
	//03.04.14 now do Prompt as per Thierry's 
	//needPromptModified = false;
	var frm = document.forms[0]; 
	var btn = frm.elements["cmd"+x];
	
	btn.click();
	_hud_fullview(false);
	return false;
}

function hud_click(x) 
{
	//03.04.14 now do Prompt as per Thierry's 
	//needPromptModified = false;
	var frm = document.forms[0]; 
	var btn = frm.elements[x];
	
	btn.click();
	_hud_fullview(false);
	return false;
}


























//
// ****  PROGRESS BAR  ****
//

function registerProgressBar(iCheckTime,skinId)
{
    // set a timer for creating the progress bar (one time only)
    window.setTimeout('progressBarLoadData('+skinId+')', iCheckTime * 1000);
}

function progressBarLoadData(skinId) 
{
    load("../ws/smProgressData.asmx/Status", "s="+siteCode+"&u="+userID+"&k="+skinId,ProgressDataHandler );
}

function ParseProgressData( ResponseXML, tagName, defaultValue )
{
    var MarquiData = ResponseXML.documentElement; 
    if ( MarquiData==null || MarquiData.tagName!="marqui-data" ) return -1;    
    
    var tag = getFirstChildByTagName(MarquiData,tagName);
    if ( tag==null ) return -1;

    var text = getFirstChildByNodeType( tag, 3 );
    if ( text==null ) return defaultValue;
    
    return text.nodeValue;
}
		    
function ShowProgressBar(iVal,iJobId,sWarning,sError)
{
    var tdPBMeter = document.getElementById("tdPBMeter");
    var tdPBMessage = document.getElementById("tdPBMessage");
    if ( tdPBMeter==null ) return;
    if ( tdPBMessage==null ) return;
    
//    sError="";
//    sWarning="";
//    iVal=50;
    if ( sError.length > 0 ) 
    {
    	tdPBMeter.style.visibility = "hidden";    
    	tdPBMessage.style.visibility = "visible";    
        tdPBMessage.style.backgroundColor = "#FFCC00";
        tdPBMessage.style.backgroundImage = "none";
        tdPBMessage.innerHTML = 
            "<div style='text-align:center;valign:middle;font-weight:bold;color:white;margin:3px;'>"+
            "<a alt='Click here for details' href='smxferjobdetail.aspx?i=" + iJobId+"'>"+
            sError+
            "</a></div>";
    }
    else if ( sWarning.length > 0 ) 
    {
    	tdPBMeter.style.visibility = "hidden";    
    	tdPBMessage.style.visibility = "visible";    
        tdPBMessage.style.backgroundColor = "#FFFF99";
        tdPBMessage.style.backgroundImage = "none";
        tdPBMessage.innerHTML = "<div style='text-align:center;valign:middle;font-weight:bold;color:white;margin:3px;'>"+
            "<a alt='Click here for details' href='smxferjobdetail.aspx?i=" + iJobId+"'>"+
            sWarning+
            "</a></div>";
    }
    else if ( iVal <= 0 )
	{
    	tdPBMeter.style.visibility = "hidden";
    	tdPBMessage.style.visibility = "hidden";    
	} 
	else 
	{    
    	tdPBMessage.style.visibility = "hidden";    
    	tdPBMeter.style.visibility = "visible";  
    	tdPBMeter.style.backgroundImage = "url(images/progress.gif)";  
    	tdPBMeter.style.backgroundPosition = ""+(iVal-300)+"px 0px";  
	    tdPBMeter.innerHTML = "<div style='text-align:center;valign:middle;font-weight:bold;color:white;margin:3px;'>"+iVal + "%</div>";
	    tdPBMeter.title = "Publishing in progress - " + iVal + "% completed";
		window.setTimeout('progressBarLoadData()', _pbClientPingFreq * 1000);
	}
}
		    
function ProgressDataHandler( TimedOut, HttpStatus, ResponseXML )
{
    if ( TimedOut )
        ShowProgressBar(0,0,"","timeout");
    else if ( HttpStatus != 200 )
        ShowProgressBar(0,0,"","HTTP status = "+HttpStatus);    
    else
        ShowProgressBar(
            ParseProgressData(ResponseXML,"pct",-1),
            ParseProgressData(ResponseXML,"jobid",-1),
            ParseProgressData(ResponseXML,"warning",""),
            ParseProgressData(ResponseXML,"error","") );    
}











var dialogInterval=null;
var dialogHandler = null;
var dialogWindow = null;

			function GetRadWindow()
			{
				var oWindow = null;
				if (window.radWindow) oWindow = window.radWindow;
				else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
				return oWindow;
			}		

    function GetDialogArguments()  
    {  
      if (window.radWindow)   
        return window.radWindow.Argument;  
      else 
        return null;  
    }  
                
// beware when accessing opener window:  
// showModalDialog does not have an opener, so passing window in as dialogArguments
// window.open has no dialogArguments, so use window.opener
function ShowDialog(url,width,height,handler)
{
	if ( window.document.all )
	{
		var result = showModalDialog( 
			url, 
			window, 
			"dialogWidth:"+width+"px;dialogHeight:"+height+"px;status:no;");
		if ( typeof(handler)!='undefined' && handler!=null ) 
			handler(result);
	}
	else
	{
		dialogHandler=handler;
		setTimeout("dialogWindow.focus();",1000);
		dialogWindow = window.open(
			url,
			"_blank",
			"width="+width+",height="+height+",modal=yes,dialog=yes,"+
			"directories=no,menubar=no,toolbar=no,location=no,scrollbars=yes,"+
			"status:no,resizable=yes");
	}
	return;
}

// beware when accessing opener window:  
// showModalDialog does not have an opener, so passing window in as dialogArguments
// window.open has no dialogArguments, so use window.opener
function ShowEditorDialog(editor,url,width,height,handler)
{
    if ( editor==null) { alert("cant find editor! (an [invisible] instance of the RadEditor must be on the page to use modal dialogs)"); return; }            
    RadEditor1ClientObject = editor;
    editor.ShowDialog(
        url //"smaskfolder.aspx?f="+ escape( rc ) + optMedia
        , null//argument
        , width
        , height
        , handler
        , null
        , "Custom Dialog");
}

function DialogDone(result)
{
	if ( window.document.all )
	{
		window.returnValue = result;
		window.close();
	}
	else
	{
		var topFrame = window.top;
		var opener = topFrame.opener;
		if ( typeof(opener.dialogHandler)!='undefined' && opener.dialogHandler!=null ) 
		{		
			opener.dialogHandler(result);
			opener.dialogHandler=null;
		}
		topFrame.close();

	}
  return;
}

function EditorDialogDone(result)
{
  CloseDlg(result);
}
















function getFormByName(name)
{
  for ( var i=0; i<window.document.forms.length; i++ )
  {
    var f = window.document.forms[i];
  	if ( f.name==name )
	{
		return f;
	}
  }
  return null;
}

function getFormByNameAndWindow(name,win)
{
  for ( var i=0; i<win.document.forms.length; i++ )
  {
    var f = win.document.forms[i];
	if ( f.name==name )
	{
		return f;
	}
  }
  return null;
}

function setSelectValue(obj,value)
{
  if ( obj!=null && typeof(obj.options)!="undefined" && obj.options!=null 
     && typeof(obj.selectedIndex)!="undefined" && obj.selectedIndex!=null )
  {
    for ( var i=0; i<obj.options.length; i++ )
    {

      if ( obj.options[i].value==value )
      {
        obj.selectedIndex=i;
        return;
      }
    }
  }
}

function findTBODY(t)
{
  for ( var i=0; i<t.childNodes.length; i++ )
  {
    if ( t.childNodes[i].nodeName=="TBODY" ) return t.childNodes[i];
  }
  return null;
}

function addTableRow(tbl)
{
  var tbody = findTBODY(tbl);
  if ( tbody==null )
  {
    tbody = document.createElement('tbody')
    tbl.appendChild(tbody);
  }
  var row = document.createElement('tr');
  tbody.appendChild(row);
  return row;
}
      
function addTableCell(row)
{
  var cell = document.createElement('td');
  row.appendChild(cell); 
  return cell;
}
      
// ************************** general utility functions ***************************

function examine(obj)
{      
  var simple = true;
    if ( obj )
    {
        var rpt = "";
        for (var item in obj)
        {
        if ( item )
        {
            try
            {
            if ( obj[item] )
            {
                if ( item=="innerHTML" || item=="outerHTML" )
                {
                    if ( simple )
                        rpt += item+" = [suppressed]\n";
                    else
                        rpt += "<span style='font-weight:bold;'>"+item+" = [suppressed]\n";
                }
                else
                {
                    if ( simple )
                        rpt += item+" = "+obj[item]+"\n";
                    else
                        rpt += "<span style='font-weight:bold;'>"+item+" = "+obj[item]+"\n";
                }
            }
            else
            {
                if ( simple )
                rpt += item+" = null\n";
                else
                rpt += "<span style='font-weight:bold;'>"+item+" = <span style='color:blue;'>null</span>\n";
            }
            }
            catch( e )
            {
            if ( simple )
                rpt += item+" = error\n";
            else
                rpt += "<span style='font-weight:bold;'>"+item+" = <span style='color:red;'>error</span>\n";
            }
        }
        else
        {
            if ( simple )
            rpt += "invalid member\n";
            else
            rpt += "<span style='color:red;'>invalid member</span>\n";
        }
        }
    }
    else
    {
        if ( simple )
        rpt = "null";
        else
        rpt = "<span style='color:red;'>null</span>\n";
    }
    if ( false )
        alert(rpt);
    else
    {
        var w = window.open("","_blank","status=yes,resizable=yes,scrollbars=yes,width=400,height=300");
        w.document.write("<html><body><pre>"+rpt+"</pre></body></html>\n");
        w.document.close();
    }
}

function show(rpt)
{
    var w = window.open("","_blank","status=yes,resizable=yes,scrollbars=yes,width=400,height=300");
    w.document.write("<html><body>"+rpt+"</body></html>\n");
    w.document.close();
}
      
// Return the x coordinate of an element relative to the page.
function getElementLeft(el)
{
  if ( el==null) return 0;
  var x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getElementLeft(el.offsetParent);

  return x;
}

// Return the y coordinate of an element relative to the page.
function getElementTop(el)
{
  if ( el==null) return 0;
  var y = el.offsetTop;
  if (el.offsetParent != null)
    y += getElementTop(el.offsetParent);

  return y;
}

// climbs up out of the DOM and determines if el, or one of its ancestors has the given id
function isDescendantOf( el, id )
{
  while( el != null )
  {
    if ( el.id!=null && el.id==id ) return true;
    el = el.parentNode;
  }
  return false;
}

function SubtractDate(a,b)
{
  return ( Date.UTC(a.getFullYear(),a.getMonth(),a.getDate()) - Date.UTC(b.getFullYear(),b.getMonth(),b.getDate()) ) / 86400000;
}

function removeExtraWhitespace(s)
{
	return s.replace(/\s+/g, ' ');
}

// aproximates the size of the text in element e
function CharacterCount(e,includeHTML)
{
  var s = "";
  if ( includeHTML!=null && includeHTML==true )
    s=e.innerHTML;  
  else if ( document.all )
	  s=removeExtraWhitespace(e.innerText);
  else
	  s=removeExtraWhitespace(e.textContent);
	return s.length;
}

function diag(msg)
{
  var el = document.getElementById("diag");
  if ( el!=null ) el.innerHTML += msg+"<br>\n";  
}

function addLoadEvent(fn)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = fn;
  }
  else
  {
    window.onload = function() { oldonload(); fn(); }
  }
} 

function switchLanguage(formID,elementID)
{
  var frm = document.forms[formID];
  if ( frm==null ) return;
  
  var el = frm.elements[elementID];
  if ( el==null ) return;
  
  window.location.href = el.options[el.selectedIndex].value;
}
