
var _iPageId=0;
var _iLanguageId=1;
var _sUserIdEnc='';
var _sFolderName='';
var _skinID = 0;

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 mouseHintHandler = null;
var mouseGuideHandler = null;
    
function showHint(me, message)
{
  if (!hintSwitch)
    return false;
    
  if (message.length == 0)
    return false;
    
  var hints = $get('divHints');
  
  hints.innerHTML = message;
  
  if (parseInt(hints.style.width) > 200)
    hints.style.width = '200px';
  
  hints.style.display = 'block';

  me.onmouseout = mouseHideHint;

  mouseHintHandler = mouseMoveHint;
  document.onmousemove = mouseMoveHandler;
}

function mouseHideHint()
{
  var hints = $get('divHints');
  
  hints.style.display = 'none';
  
  mouseHintHandler = null;
  document.onmousemove = null;
}
    
function mouseMoveHint(e)
{
  var Hints = $get('divHints');
  
  var x = e ? parseInt(e.pageX - 15) : parseInt(event.clientX - 15) + document.documentElement.scrollLeft;
  var y = e ? parseInt(e.pageY) : parseInt(event.clientY) + document.documentElement.scrollTop;
  
  if (x + 300 > parseInt(document.body.offsetWidth)) 
    x = parseInt(document.body.offsetWidth) - 350;
  
  Hints.style.left = (1 + x).toString() + 'px';
  Hints.style.top = (1 + y).toString() + 'px';
}

function mouseMoveHandler(e)
{
  if (mouseGuideHandler != null) 
    mouseGuideHandler(e);
    
  if (mouseHintHandler != null) 
    mouseHintHandler(e);
}

function mouseGuideOn()
{
  var mouseGuide = $get('divMouseGuide');
    
  mouseGuide.style.display = 'block';
  
  mouseGuideHandler = mouseGuideUpdate;
  document.onmousemove = mouseMoveHandler;      
}

function mouseGuideOff()
{
  var mouseGuide = $get('divMouseGuide');
  
  mouseGuideHander = null;
  document.onmousemove = null;
  mouseGuide.style.display = 'none';
}

function mouseGuideUpdate(e)
{
  var mouseGuide = $get('divMouseGuide');
  
  var y = e ? parseInt(e.pageY) : parseInt(event.clientY) + document.documentElement.scrollTop;
  mouseGuide.style.top = (y + 1).toString() + 'px';      
}


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 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;
}

function SubtractDate(a,b)
{
  return ( Date.UTC(a.getFullYear(),a.getMonth(),a.getDate()) - Date.UTC(b.getFullYear(),b.getMonth(),b.getDate()) ) / 86400000;
}

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;
}

/* publishing progress */

function InitializePublishingProgress(SkinID)
{
  window.setTimeout('GetPublishingProgress()', 1000);
  
  _skinID = SkinID // make global
}

function GetPublishingProgress()
{
  wxWeb.ws.PublishingProgress.CurrentStatus(siteCode, userID, _skinID, onGetPublishingProgressComplete, onGetPublishingProgressError);
}

function onGetPublishingProgressComplete(PublishingProgressResults)
{       
  RenderPublishingProgress(PublishingProgressResults.Percent, PublishingProgressResults.JobID, PublishingProgressResults.WarningMessage, PublishingProgressResults.ErrorMessage);      
}

function onGetPublishingProgressError(PublishingProgressResults)
{

  var PublishingProgressMessage = $get('divPublishingProgressMessage');
  
  PublishingProgressMessage.style.display = 'block';
  
  divPublishingProgressMessage = '<div class="publishingmeter_message">' + PublishingProgressResults.message() + '</div>';
}

function RenderPublishingProgress(Percent, JobID, WarningMessage, ErrorMessage)
{
  var PublishingProgressBar = $get('divPublishingProgressBar');
  var PublishingProgressMessage = $get('divPublishingProgressMessage');

  if (ErrorMessage.length > 0) 
  {
    PublishingProgressMessage.style.display = 'block';
             
    PublishingProgressMessage.innerHTML = '<div class="publishingmeter_message publishingmeter_message_error">' +
        '<a alt="<skin ID=3737>Click here for details</skin>" href="smxferjobdetail.aspx?i=' + JobID + '">' + ErrorMessage + '</a></div>';        
  }
  else if (WarningMessage.length > 0) 
  { 
    PublishingProgressMessage.style.display = 'block';
  
    PublishingProgressMessage.innerHTML = "<div class='publishingmeter_message publishingmeter_message_warning'>"+
        '<a alt="<skin ID=3737>Click here for details</skin>" href="smxferjobdetail.aspx?i=' + JobID + '">' + WarningMessage + '</a></div>';        
  }
  else if (Percent <= 0) 
  { 
    PublishingProgressMessage.innerHTML = 'Message: No publishing jobs';
    PublishingProgressMessage.style.display = 'none';
    PublishingProgressBar.style.display = 'none';          
  } 
  else 
  { 
    PublishingProgressBar.style.display = 'block';        
    PublishingProgressBar.style.backgroundPosition = "" + (Percent - 121) + "px 0px";  
    PublishingProgressBar.innerHTML = '<div class="publishingbar_message">' + Percent + '%</div>';
    PublishingProgressBar.title = '<skin ID=3738>Publishing in progress</skin> - ' + Percent + '% <skin ID=3739>completed</skin>';
    
    window.setTimeout('GetPublishingProgress()', _pbClientPingFreq * 1000);
  }      
}
