/* 
 * system.js	- JavaScript support routines for EPiServer
 * Copyright (c) 2000-2001 ElektroPost Stockholm AB
*/

var nAccessRead			= 1;
var nAccessCreate		= 3;
var nAccessUpdate		= 7;
var nAccessDelete		= 15;
var nAccessPublish		= 31;
var nAccessAdminister	= 63;

var EPnDistinctRead			= 1;
var EPnDistinctCreate		= 2;
var EPnDistinctUpdate		= 4;
var EPnDistinctDelete		= 8;
var EPnDistinctPublish		= 16;
var EPnDistinctAdminister	= 32;

//--
// function addQueryString
//--
// Parameters:
//	qs			String: The original URL including query string
//	name		String: Name of qs parameter to add
//	val			String: Value of qs parameter to add
//
// Return value:
//	String:		The modified URL + qs
function addQueryString( qs, name, val )
{	
	// Check if name exists
	var nName = qs.indexOf(name);
	var nPos;
	if (nName >= 0)
	{	
		// Exists, replace it
		nPos = qs.indexOf('&', nName + name.length);
		if (nPos >= 0)
		{	
			// There is a parameter following it, just replace the value
			qs = qs.slice(0, nName + name.length) + val + qs.slice(nPos);
		}
		else
		{	
			// Simply insert the value
			qs = qs.slice(0, nName + name.length) + val;
		}
	}
	else
	{
		nName = qs.indexOf('____');
		if (nName >= 0 && (name == '?id=' || name == '@id'))
		{	
			// Special case for ?id / @id
			nPos = qs.indexOf('.', nName);
			if (nPos >= nName)
			{
				qs = qs.slice(0, nName + 4) + val + qs.slice(nPos);
			}
		}
		else
		{	
			// Does not exist, add it to the end
			qs = qs + name + val;
		}
	}
	return qs;
}

//---
// function delQueryString
//---
// Parameters:
//	qs			String: The original URL including query string
//	name		String: Name of qs parameter to delete
//
// Return value:
//	String:		The modified URL + qs
function delQueryString( qs, name )
{	
	// Check if name exists
	var nName = qs.indexOf(name);
	var nPos;
	if (nName >= 0)
	{	
		// Exists, delete it
		nPos = qs.indexOf('&', nName + name.length);
		if (nPos >= 0)
		{
			if (name.slice(0, 1) != '&')
			{	
				// We try to delete the first parameter, must keep separator
				nName ++;
				nPos ++;
			}
			qs = qs.slice(0, nName) + qs.slice(nPos);
		}
		else
		{
			qs = qs.slice(0, nName);
		}
	}
	return qs;
}

function positionItemInContainer( oItem, oContainer )
{
	if (oItem.offsetTop > oContainer.scrollTop + oContainer.style.pixelHeight)
		oItem.scrollIntoView( false );
	else if (oItem.offsetTop < oContainer.scrollTop)
		oItem.scrollIntoView( true );
}

function updateTree(id)
{
	var frame;
	var wnd = this.window;
	
	while (wnd != null)
	{
		frame = wnd.frames['ep_tree'];
		if (frame)
		{
			frame.location.href = addQueryString(frame.location.href, '?id=', id);
		}
		if (wnd == wnd.parent)
			break;
		wnd = wnd.parent;
	}
	
	return false;
}

function updateTreeAndVersion(id,work)
{
	var frame;
	var url;
	var wnd = this.window;
	
	while (wnd != null)
	{
		frame = wnd.frames['ep_tree'];
		if (frame)
		{
			frame.ActivateNode(id);
		}
		frame = wnd.frames['ep_version'];
		if (frame)
		{
			url = addQueryString(frame.location.href, '?id=', id);
			url = addQueryString(url, '&work=', work);
			url = delQueryString(url, '&action=');
			url = delQueryString(url, '&taskid=');
			url = delQueryString(url, '&tab=');
			frame.location.href = url;
		}
		if (wnd == wnd.parent)
			break;
		wnd = wnd.parent;
	}
	return false;
}

var oActive = null;

function changeTreeSize()
{
	if (!oActive)
	{
		oActive = document.all.activeItem;
	}
	
	if (document.all.id_tree)
	{
		id_tree.style.pixelWidth = document.body.offsetWidth;
		id_tree.style.pixelHeight = document.body.offsetHeight;
		if (document.all.id_header)
		{
			id_tree.style.pixelHeight -= id_header.offsetHeight;
		}	
		if (document.all.id_footer)
		{
			id_tree.style.pixelHeight -= id_footer.offsetHeight;
		}
		if (oActive)
		{
			positionItemInContainer( oActive, id_tree );
		}
		id_tree.style.visibility = 'visible';
	}
}

function clearRadio(field)
{
	var i;
	if (field)
	{
		for (i = 0; i < field.length; i ++)
		{
			field[i].checked = false;
		}
	}
	return false;
}

function deleteRow( id )
{
	id.parentElement.deleteRow(id.rowIndex);
	return false;
}

function moveOption( oSelectFrom, oSelectTo )
{
	var oOption = document.createElement("OPTION");
	var nIndex = oSelectFrom.selectedIndex;
	
	if (nIndex >= 0)
	{
		oOption.text = oSelectFrom.options(nIndex).text;
		oOption.value = oSelectFrom.options(nIndex).value;
		oSelectTo.add(oOption);
		oSelectFrom.remove(nIndex);
	}
}

function enableAllBelow(field, nAccess)
{
	var i;
	if (field)
	{
		for (i = 0; i < field.length; i ++)
		{
			field[i].checked = (field[i].value <= nAccess);
		}
	}
	return false;
}

var nActiveTab = null;

function clickedTab( nTab )
{
	var oField;
	var oTab;
	
	document.body.scrollTop = 0;
	
	// Check for exit conditions (clicking active tab, tab disabled)
	if (nTab == nActiveTab)
		return false;
	oTab = document.all['id_tab' + nTab];
	if (oTab)
	{
		if (oTab.className == 'EPEdit-tabDisabled')
			return false;
	}
	disableCurrentTab();
	nActiveTab = nTab;
	if (window.clickedTabHook != null)
	{
		clickedTabHook( nTab );
	}
	enableCurrentTab();
}

function disableCurrentTab()
{
	var oField;
	
	if (nActiveTab != null)
	{
		oField = document.all['id_field' + nActiveTab];
		if (oField != null)
		{
			oField.style.display = 'none';
		}
		if (document.all['id_tab' + nActiveTab])
		{
			document.all['id_tab' + nActiveTab].className = 'EPEdit-tabInactive';
			document.all['id_tabright' + nActiveTab].className = 'EPEdit-tabRightInactive';
			document.all['id_tableft' + nActiveTab].className = 'EPEdit-tabLeftInactive';
		}
	}
}

function enableCurrentTab()
{
	var oField;
	var oTab;

	oField = document.all['id_field' + nActiveTab];
	if (oField != null)
	{
		oField.style.display = '';
	}
	oTab = document.all['id_tab' + nActiveTab];
	if (oTab != null)
	{
		oTab.className = 'EPEdit-tabActive';
		document.all['id_tabright' + nActiveTab].className = 'EPEdit-tabRightActive';
		document.all['id_tableft' + nActiveTab].className = 'EPEdit-tabLeftActive';
	}
}

var aFieldValidate = new Array();

function fieldValidateDate( oField )
{
	var oDate = /^\s*\d{4}[\-/]\d{1,2}[\-/]\d{1,2}(\s*$|\s+\d{1,2}(:\d{1,2}){1,2}\s*$)/
	return oDate.test(oField.value);
}

function fieldValidateInteger( oField )
{
	var oInteger = /^\s*-?\d{1,20}\s*$/
	return oInteger.test(oField.value);
}

function fieldValidateFloat( oField )
{
	var oFloat = /^\s*\-?\d{1,20}(\s*$|[\.,]\d{1,20}\s*$)/
	return oFloat.test(oField.value);
}

function formFieldValidate( sField, sError, pFunction, fRequired )
{
	var a = new Array(4);
	a[0] = sField;
	a[1] = sError;
	a[2] = pFunction;
	a[3] = fRequired;
	aFieldValidate[aFieldValidate.length] = a;
}

function formFieldRequired(sField, sError)
{
	formFieldValidate( sField, sError, null, true );
}

function formValidate( obj )
{
	var fOk = true;
	var sAlert = '';
	var i;
	var oField;
	var oParent;
	var fValid;
	var sWhiteSpace = new RegExp('^\s+$');
	
	for (i = 0; i < aFieldValidate.length; i ++)
	{	
		oField = obj[aFieldValidate[i][0]];
		fValid = !aFieldValidate[i][3];
		if ( (oField.value.length > 0) && !sWhiteSpace.test(oField.value) )
		{
			fValid = aFieldValidate[i][2] == null;
			if (!fValid)
				fValid = aFieldValidate[i][2]( oField );
		}
		if (!fValid)
		{
			if (fOk)
			{
				oParent = oField;
				while (oParent != null)
				{
					if (oParent.style.display == 'none')
					{
						if (window.formValidateHook)
						{
							if (formValidateHook( oParent ))
								break;
						}
						else
							oParent.style.display = '';
					}
					oParent = oParent.parentElement;
				}
				oField.focus();
			}
			fOk = false;
			sAlert = sAlert + aFieldValidate[i][1] + '\n';
		}
	}
	if (!fOk)
	{
		alert(sAlert);
	}
	return fOk;
}

function LoadNode(nId,sData)
{
	if (sData.length > 0)
	{
		//Load data, using outerhtml so we dont get duplicate "containers".
		document.all['DATA' + nId].outerHTML=sData; 
		
		//Display
		document.all['DATA' + nId].style.display='';
	}
	else
	{
		document.all['DATA' + nId].innerHTML=''; 
		document.all['DATA' + nId].style.display='none';
		document.all['IMG' + nId].src=imgNone.src;
		document.all['IMG' + nId].width=imgOpen.width;
	}
}

function ToggleNode(nId,sTemplate)
{
	if(document.all['DATA' + nId].style.display=='none')
	{
		// Call loader and pre-open image (so users directly see a response).
		var oLoc = window.frames['TreeLoader'].location;
		var sUrl = addQueryString(oLoc.href, '?id=', nId);
		sUrl = addQueryString(sUrl, '&template=', sTemplate);
		sUrl = addQueryString(sUrl, '&t=', nActiveTab);
		document.all['IMG' + nId].src = imgOpen.src;
		oLoc.href = sUrl;
	}
	else
	{
		//Close node.
		document.all['IMG' + nId].src=imgClosed.src;
		document.all['DATA' + nId].style.display='none';
		document.all['DATA' + nId].innerHTML='';
	}
}

function DeleteNode(nId)
{
	var o;
	var sId;
	var n;
	
	// First check that we have something to delete
	o = document.all['DATA' + nId];
	if (o != null)
	{
		if (nId == nActivePage)
		{
			// Set activePage to parent of this item
			sId = o.parentElement.id;
			n = new Number(sId.slice(4));
			editOnClick( n );
		}
		o.outerHTML='';
		document.all['PAGE' + nId].outerHTML='';
	}
}

function RefreshNode(nId)
{
	var o;
	var oImg;
	
	o = document.all('DATA' + nId);
	if (o)
	{
		oImg = document.all('IMG' + nId);
		if (o.style.display!='none')
		{
			o.style.display = 'none';
			o.innerHTML = '';
			
			if (oImg.onclick)
				oImg.click();
		}
		else
		{
			oImg.src=imgClosed.src;
		}
	}
}

function ActivateNode(nId)
{
	var o;
	var oLoc;
	var sUrl;
	
	if (nActiveTab == 2)
	{
		oLoc = window.frames['TreeLoader'].location;
		sUrl = addQueryString(oLoc.href, '?id=', nId);
		oLoc.href = addQueryString(sUrl, '&t=', nActiveTab);
	}
	else
	{
		o = document.all['DATA' + nId];
		if (o)
		{
			editOnClick(nId);
		}
		else
		{
			oLoc = this.window.location;
			sUrl = addQueryString(oLoc.href, '?id=', nId);
			oLoc.href = delQueryString(sUrl, '&click=');
		}
	}
}

function SimulateClickNode(nId)
{
	var o;
	var oParent;
	var sUrl;
	
	if (nActiveTab != 1)
	{
		clickedTab(1);
	}
	o = document.all['A' + nId];
	if (o)
	{
		oParent = o;
		while (oParent != null)
		{
			if (oParent.style.display == 'none')
			{
				oParent.style.display = '';
			}
			oParent = oParent.parentElement;
		}

		o.click();
	}
	else
	{
		sUrl = addQueryString(this.window.location.href, '?id=', nId);
		this.window.location.href=addQueryString(sUrl, '&click=', 1);
	}
}

function RefreshTree(nChangedId, nDeletedId)
{
	var frame;
	var url;
	var wnd = this.window;
	
	while (wnd != null)
	{
		frame = wnd.frames['ep_tree'];
		if (frame)
		{
			if (nDeletedId != null)
				frame.DeleteNode(nDeletedId);
			if (nChangedId != null)
				frame.RefreshNode(nChangedId);
			frame.editOnClick(null);
			break;
		}
		if (wnd == wnd.parent)
			break;
		wnd = wnd.parent;
	}
}

function changeEditEncoding(oSelect,sMessage)
{
	var sLanguage = oSelect.value;
	
	for(var i=0;i<oSelect.length;i++)
		if(oSelect[i].value==oSelect.defaultValue)
			break;
	
	if(oSelect[i].langcode==oSelect[oSelect.selectedIndex].langcode)
			return;
		
	if(sLanguage=='')
		sLanguage = 'none';
	
	if(confirm(sMessage))
		window.location.href = addQueryString(window.location.href,'&language=',sLanguage);
}

function syncTree()
{
	if(window.parent)
	{
		var frame = window.parent.frames['ep_main'];
		if (frame)
		{
			var verFrame = frame.frames['ep_version'];
			if(verFrame)
			{
				var id = verFrame.nPageID;
				if(id && nActivePage!=id)
				{
					ActivateNode(id);
					editOnClick(null);
				}
			}
		}
	}

}