var _debug=false;
var _FocusFinished=false;

function FocusFinished()
	{
	_FocusFinished = true;
	}

function PlaceCursor(n) 
	{
	if (_FocusFinished)
		return;

	var undefined; 
	if (undefined == n)	n = 0;
	var frm = document.forms[0];
	if (null == frm) return;

	for ( ; n < frm.elements.length ; n++ )
		{
		var e = frm.elements[n];
		if (null == e || e.disabled || e.type == "hidden" || e.type == "textarea" )
			continue;
		e.focus();
		if ( e.type == "text" )
			e.select();
		return;
		}
	}

function SetSrc(id,src)
	{
	var e = document.getElementById(id);
	if (e && e.src != src)
		e.src = src;
	}

function ToggleVisibility(id, relay)
	{
	var e = document.getElementById(id);
	if (e && e.style)
		{
		if (e.style.visibility == 'hidden')
			e.style.visibility = 'visible';
		else
			e.style.visibility = 'hidden';
		}
	if (arguments.length > 1 && relay)
		AdminFrame_OnResize(true, true);
	return false;
	}

function ToggleDisplay(id, relay)
	{
	var e = document.getElementById(id);
	if (e && e.style)
		{
		if (e.style.display == 'none')
			e.style.display = 'block';
		else
			e.style.display = 'none';
		}
	if (arguments.length > 1 && relay)
		AdminFrame_OnResize(true, true);
	return false;
	}

function ScrollSelection()
	{
	var sel = document.selection;
	if (sel && 'None' != sel.type)
		{
		var tr = sel.createRange();
		if (tr)
			tr.scrollIntoView();
		}
	}


var timerResize=0;
var topFileListDIV=0;
var topMainDIV=0;
var topWizardDIV=0;
var topPropertyDIV=0;
var topDesignDIV=0;
var topPreviewDIV=0;
var fNetscape=false;
var widthFixed=0;
var body=null;
var width=0;
var height=0;
// constants
var minWidth=796;
var minHeight=400;
function ClientTop(name)
	{
	elem = document.getElementById(name);
	for (var top=0 ; elem && elem!=elem.offsetParent ; elem=elem.offsetParent)
		top += elem.offsetTop;
	return Math.max(0,top);
	}
function SetHeight(name, y)
	{
	var elem = document.getElementById(name);
	if (null != elem && null != elem.style)
		{
		y += fNetscape?23:18;
		var px = Math.max(height-y,100) + "px";
		if (px != elem.style.height)
			{
			elem.style.height = px;
			return name + ".height:" + px + " ";
			}
		}
	return "";
	}
function SetWidth(name, x, b)
	{
	function atoi(a) {return !a?0:parseInt(a);}
	var elem = document.getElementById(name);
	if (elem && elem.style)
		{
		if (b)
			{
			var parent = elem.offsetParent;
			x += atoi(parent.style.paddingLeft) + atoi(parent.style.paddingRight);
			}
		if (fNetscape)
			{
			x += atoi(elem.style.paddingLeft) + atoi(elem.style.paddingRight);
			}
		var px = Math.max(width-x,100) + "px";
		if (px != elem.style.width)
			{
			//alert("Setting " + name + " to " + px);
			elem.style.width = px;
			return name + ".width:" + px + " ";
			}
		}
	return "";
	}
function AdminFrame_OnResize(now, reset)
	{
	var cookie="";
	if (_debug) window.status = "";

	if (arguments.length > 1 && reset)
		{
		body = null;
		widthFixed = 0;
		}

	if (!body)
		{
		body=document.getElementsByTagName('body').item(0);
 		topFileListDIV = ClientTop('FileListDIV');
		topDesignDIV   = ClientTop('DesignDIV');
		topPreviewDIV  = ClientTop('PreviewDIV');
		topMainDIV     = ClientTop('MainDIV');
		topWizardDIV   = ClientTop('WizardDIV');
		topPropertyDIV = topFileListDIV ? topFileListDIV : ClientTop('PropertyDIV');
		fNetscape = (window.navigator.appName == "Netscape");
		var r = document.getElementById('DesignerTR');
		if (r) for (var i=0,l=r.cells.length; i<l ;i++)
			{
			var c=r.cells[i];
			//alert("Cell id="+c.id+"; width="+c.offsetWidth);
			if (c.id != "MainTD")
				widthFixed += c.offsetWidth;
			}
		}
	if (now == true)
		{
		height = body.clientHeight;
		if (!height) height = window.innerHeight;
		height = height ? Math.max(height,minHeight) : 600;

		width = body.clientWidth;
		if (!width && document.getElementById('NavigationTABLE')) width = document.getElementById('NavigationTABLE').offsetWidth;
		if (!width)	width = window.innerWidth;
		width = width ? Math.max(width,minWidth) : 800;
		
		cookie +=	SetHeight('FileListDIV', topFileListDIV);
					SetHeight('PreviewDIV',	 topPreviewDIV);
					SetHeight('PreviewFRAME', topPreviewDIV);
					SetHeight('DesignDIV',	 topDesignDIV);
					SetHeight('PropertyDIV', topPropertyDIV);
					SetHeight('TipsDIV',   topPropertyDIV);
		cookie +=	SetHeight('MainDIV',     topMainDIV);
		cookie +=	SetHeight('WizardDIV',   topWizardDIV+70, true);
					SetHeight('DisplayFRAME', topPropertyDIV + 18);


// rossb:  i don't think these width's are necessary.
//		SetWidth('PreviewDIV', widthFixed, true);
//		SetWidth('WizardDIV',	 widthFixed+40, true);
//		SetWidth('MainDIV',		 widthFixed, true);
//		SetWidth('DisplayFRAME', widthFixed);
//		SetWidth('StatusFRAME', widthFixed);
//
//		if (!topWizardDIV && !topMainDIV)
//			{
//			SetWidth('DesignDIV',	widthFixed);
//			SetWidth('PreviewDIV', widthFixed);
//			}


		ScrollSelection();
		}
	if (now == false)
		{
		window.clearTimeout(timerResize);
		timerResize = window.setTimeout("AdminFrame_OnResize(true)", 100);
		}
	if (cookie.length)
		{
		// avoid browser limitation, don't want to kick out important cookies
		if (document.cookie.split("; ").length < 20)
			document.cookie = "ws:designer=" + cookie + "; path=/";
		}
	}


var oLastTab = null;
function ToggleTabs(oTab, strSrc)
	{
	var undefined;
	var div = document.getElementById('PropertyDIV');
	var Tdiv = document.getElementById('TipsDIV');
	var frame = document.getElementById('TipsFRAME');

	var oOldTab = (oLastTab) ? oLastTab : document.getElementById("propertyTAB");
	var oNewTab = (oTab) ? oTab : document.getElementById("propertyTAB");
	
	if (oNewTab.className != "tabstripItemSelected")
		{
		if (oOldTab != undefined)
			oOldTab.className = "tabstripItem";
		if (oNewTab != undefined)
			oNewTab.className = "tabstripItemSelected";
		oLastTab = oNewTab;
		}
	if (strSrc && div && Tdiv && frame)
		{
		div.style.display = 'none';
		Tdiv.style.display = 'block';
		if (frame.src != strSrc)
			frame.src = strSrc;
		}
	else if (div && Tdiv)
		{
		Tdiv.style.display = 'none';
		div.style.display = 'block';
		}
	}

function ToggleTips(bShowTips, strSrc)
	{
	var undefined;
	var div = document.getElementById('PropertyDIV');
	var Tdiv = document.getElementById('TipsDIV');
	var frame = document.getElementById('TipsFRAME');
	if (div == undefined || Tdiv == undefined)
		return false;
	var sel = document.getElementById('propertyTAB');
	var tip = document.getElementById('tipsTAB');
	if (bShowTips && tip)
		{
		if (sel != undefined)
			sel.className = "tabstripItem";
		if (tip != undefined)
			tip.className = "tabstripItemSelected";
		div.style.display = 'none';
		Tdiv.style.display = 'block';
		if (strSrc && frame.src != strSrc)
			frame.src = strSrc;
		return true;
		}
	else if (sel != undefined && tip)
		{
		sel.className = "tabstripItemSelected";
		if (tip != undefined)
			tip.className = "tabstripItem";
		Tdiv.style.display = 'none';
		div.style.display = 'block';
		return true;
		}
	return false;
	}


                 
var selectedA = null;
function ShowImg(a,f)
	{
	var img = document.getElementById('PreviewIMG');
	if (null == img)
		return true;
	img.src = f;
	if (null != selectedA)
		selectedA.className = "ws-File";
	if (null != a)
		{
		a.className = "ws-FileS";
		selectedA = a;
		}
	return false;
	}


var nTimeout = 0;
function CheckFocus(focus)
	{
	if (!focus)
		{
		nTimeout = window.setTimeout("window.close()", 300);
		}
	else if (nTimeout)
		{
		window.clearTimeout(nTimeout);
		nTimeout = 0;
		}
	}



var bExplicitSubmit = false;
var bDirty = false;
function SetDirty(b)
	{
	if (arguments.length < 1)
		bDirty = true;
	else
		{
		bDirty = b;
		if (!b)
			{
			var inpDirty = document.getElementById('_dirty');
			if (null != inpDirty)
				inpDirty.value = '';
			}
		}
	var spnSave = document.getElementById("spnsave");
	if (spnSave)
		{
		var bSet = ("(Changed)" == spnSave.innerHTML);
		if (bDirty && !bSet)
			spnSave.innerHTML = "(Changed)";
		else if (!bDirty && bSet)
			spnSave.innerHTML = "";
		}
	}
function BeforeSubmit()
	{
	if (bDirty == true)
		{
		var inpDirty = document.getElementById('_dirty');
		if (null != inpDirty)
			inpDirty.value = 'true';
		}
	bExplicitSubmit = true;
	return true;
	}
function IsDirty()
	{
	var inpDirty = document.getElementById('_dirty');
	return (true==bDirty) || (null != inpDirty && 'true' == inpDirty.value) || CheckProperties();
	}
function CheckSave(e)
	{
	if (!e)
		e = event;
	if (!IsDirty() || true==bExplicitSubmit)
		return;
	if (CheckProperties())
		e.returnValue = "You will lose any changes made to the property sheet.";
	else
		e.returnValue = "You will lose any changes made to this page.";
	}

var frmProperties = null;
function RememberProperties()
	{
	frmProperties = document.forms[0];
	var elms = frmProperties.elements;
	var i;
	for (i = 0; i < elms.length; i ++)
		{
		var elm = elms[i];
		switch (elm.type)
			{
			case "text":
			case "textarea":
				elm.oldValue = elm.value;
				break;
			case "checkbox":
			case "radio":
				elm.oldValue = elm.checked;
				break;
			case "select-one":
				elm.oldValue = elm.selectedIndex;
				break;
			}
		}
	}

function CheckProperties()
	{
	if (frmProperties == null)
		return false;

	var elms = frmProperties.elements;
	for (i = 0; i < elms.length; i ++)
		{
		var elm = elms[i];
		switch(elm.type)
			{
			case "text":
			case "textarea":
				if (elm.oldValue != elm.value)
					return true;
				break;
			case "checkbox":
			case "radio":
				if (elm.oldValue != elm.checked)
					return true;
				break;
			case "select-one":
				if (elm.oldValue != elm.selectedIndex)
					return true;
				break;
			}
		}
	return false;
	}

function Browse(evt, session, path)
	{
	top.name = "wsDesign";
	if (arguments.length < 3)
		path = location.pathname;
	var brws = 'wsBrowse' + session;
	var f = evt.shiftKey;
	var strUnsafe = "try { if(" + f + ") top.open('" + path + "'); ";
	strUnsafe += "else { window.winBrowse = top.open('" + path + "', '" + brws + "' ); "
	strUnsafe += "window.winBrowse.focus(); } } ";
	strUnsafe += "catch(e) { window.setTimeout('try { window.winBrowse.focus(); } catch(e) {}', 1000); }";
	eval(strUnsafe);
	return false;
	}

function FocusDesigner()
	{
	if(window==top && (location.pathname.charAt(location.pathname.length -1) != "/" || 
					  (location.search.indexOf("_admin=contents") == -1 &&
					   location.search.indexOf("_refresh=") == -1)))
	  top.focus();
	} 

function SetCookie(name, value, path, domain, expires)
  {
  var strCookie = name + "=" + value;
  if(path)
    strCookie += "; path=" + path;
  if(domain)
    strCookie += "; domain=" + domain;
  if(expires)
    strCookie += "; expires=" + expires;
  document.cookie = strCookie;
  //Don't try & fix up the object, just reload if needed
  }
//GetPrefs returns user preferecnces as set by SetPref
//Note: if cookie is changed without using SetPref, 
//GetPrefs may return erroneous data
var m_prefs = null;
function GetPrefs()
  {
  if(m_prefs)
    return m_prefs;
	
  var arPrefs = /prefs=([^;]*);/.exec(document.cookie);
  m_prefs = new Object();
  if(!arPrefs)
    {
    arPrefs = /prefs=([^;]*)$/.exec(document.cookie);
	if(!arPrefs)
      return m_prefs;
	}

  var strPrefs = arPrefs[1];
  var arPrefs = strPrefs.split("&");
  for(var i = 0; i < arPrefs.length; i++)
    {
	var arNameVal = arPrefs[i].split(":");
	m_prefs[arNameVal[0]] = unescape(arNameVal[1]);
	}
  return m_prefs;
  }
  
function SetPref(name, value)
  {
  GetPrefs();
  m_prefs[name] = value;
  var strPrefCookie = "";
  
  for(var strPrefName  in m_prefs)
    {
	if(strPrefCookie != "")
	  strPrefCookie += "&";
	strPrefCookie += strPrefName + ":" + escape(m_prefs[strPrefName]);
	} 
  var strTopLevelDomain = window.location.hostname;
  if(strTopLevelDomain.indexOf(".") != -1)
    strTopLevelDomain = strTopLevelDomain.substring(strTopLevelDomain.indexOf(".") + 1);
  else
    strTopLevelDomain = null; //Cookies don't deal very well with 1 part domains
  var expires = new Date();
  expires.setFullYear(expires.getFullYear() + 1);
  SetCookie("prefs", strPrefCookie, "/", strTopLevelDomain, expires.toGMTString());
  }
 
