var	isIE = document.all;
var	isDom = document.getElementById;
var	isNs4 = document.layers;

function getRef(obj){
	return(typeof obj == "string") ? document.getElementById(obj) : obj;
}

function clearText(txtField) { 
	txtField.value = "" 
}

function setClassName(obj, className){
    getRef(obj).className = className;
}

function toggleVisibility(obj){
	var obj = getRef(obj);

	if( obj )
	{
		if( obj.className.indexOf('css_visible') > -1 )
			setClassName(obj, obj.className.replace('css_visible', 'css_invisible'));
		else if( obj.className.indexOf('css_invisible') > -1 )
			setClassName(obj, obj.className.replace('css_invisible', 'css_visible'));
	}
}

function SetCheckBoxes(boxID, action)
{
	var i, formElements = document.forms[0].elements;
	for(i = 0; i < formElements.length; i++)
		if( (formElements[i].type == 'checkbox') && (formElements[i].id.indexOf(boxID) > -1) )
		{
			if( action == 0 || action == 1 )
				formElements[i].checked = action;
			else if( action == 2 )
				formElements[i].checked = !formElements[i].checked;
				
			CheckRelatedPermissions(formElements[i]);
		}
}

function RelatedPermissions(	pView, 
															pAdd, 
															pNeedsAddApproval, 
															pEdit, 
															pEditOwnItems, 
															pDelete, 
															pDeleteOwnItems, 
															pSave, 
															pModerate, 
															pAdministrate)
{
	//Object to contain related permissions
	this.chkView							= pView;
	this.chkAdd								 = pAdd;
	this.chkNeedsAddApproval	= pNeedsAddApproval;
	this.chkEdit							= pEdit;
	this.chkEditOwnItems			= pEditOwnItems;
	this.chkDelete						  = pDelete;
	this.chkDeleteOwnItems		  = pDeleteOwnItems;
	this.chkSave							= pSave;
	this.chkModerate					= pModerate;
	this.chkAdministrate			= pAdministrate;
}

var relatedPermissionMapping = new Object();

//Define mappings here
//key format [serversideid_checkedvalue]
//parameters can have 3 values
//true = corresponding checkbox wil be checked
//false = corresponding checkbox wil be unchecked
//null = corresponding checkbox wil be left alone ;)

//checked mappings
relatedPermissionMapping["chkView_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkAdd_true"]= new RelatedPermissions(null, null, false, null, null, null, null, true, null, null);
relatedPermissionMapping["chkNeedsAddApproval_true"] = new RelatedPermissions(null, false, null, null, null, null,null, false, null, null);
relatedPermissionMapping["chkEdit_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, true);
relatedPermissionMapping["chkEditOwnItems_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkDelete_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, true, null);
relatedPermissionMapping["chkDeleteOwnItems_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkSave_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkModerate_true"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkAdministrate_true"] = new RelatedPermissions(true, true, true, true, true, true, true, true, true, null);

//unchecked mappings
relatedPermissionMapping["chkView_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkAdd_false"]= new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkNeedsAddApproval_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkEdit_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkEditOwnItems_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkDelete_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkDeleteOwnItems_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkSave_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkModerate_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);
relatedPermissionMapping["chkAdministrate_false"] = new RelatedPermissions(null, null, null, null, null, null, null, null, null, null);

function CheckRelatedPermissionsSpan(clickedSpan)
{
	//user firstchild because framework renders a span tag around
	//the checkbox with the onclick event
	//CheckRelatedPermissions(clickedSpan.firstChild)
	CheckRelatedPermissions(clickedSpan)
}

function CheckRelatedPermissions(clickedCheckBox)
{
	var checkBoxID				= clickedCheckBox.id;
	var checkBox					= document.getElementById(checkBoxID);
	var checkBoxState			= checkBox.checked;
	
	var clickedPermission = checkBoxID.substring(checkBoxID.lastIndexOf("_")+1, checkBoxID.length);
	var clickedRow				= checkBoxID.substring(0, checkBoxID.lastIndexOf("_")+1);
	
	var formElements			= new Array(clickedRow+"chkView",
																		clickedRow+"chkAdd",
																		clickedRow+"chkNeedsAddApproval",
																		clickedRow+"chkEdit",
																		clickedRow+"chkEditOwnItems",
																		clickedRow+"chkDelete",
																		clickedRow+"chkDeleteOwnItems",
																		clickedRow+"chkSave",
																		clickedRow+"chkModerate",
																		clickedRow+"chkAdministrate"
																		);
	
	var relatedPermissionMappingKey = clickedPermission + "_" + String(checkBoxState);

	for(i = 0; i < formElements.length; i++)
	{
			var permissionType = formElements[i].substring(formElements[i].lastIndexOf("_")+1, formElements[i].length);
			var currenCheckBox = document.getElementById(formElements[i]);
			
			if( currenCheckBox != null )
				if( relatedPermissionMapping[relatedPermissionMappingKey][permissionType] != null )
					currenCheckBox.checked = relatedPermissionMapping[relatedPermissionMappingKey][permissionType];
	}
}

function ShowAdminHelp()
{
	window.open('content/en-US/helpadmin/help_admin.htm','','width=460,height=600, menubar=no,resizable=no, scrollbars=yes,titlebar=no,toolbar=no');
}

function storeCursor(el)
{
	if( document.all && el.createTextRange) el.cursorPos = document.selection.createRange().duplicate();
}

function putStr(el, text)
{
	target = document.getElementById(el);
	if( target )
	{
		if (document.all && target.cursorPos)
		{
			target.cursorPos.text = text;
		}
		else if( typeof(target.selectionStart) != 'undefined' )
		{
			var sStart = target.selectionStart;
			var sEnd = target.selectionEnd;
			target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length);
			target.selectionStart = (sStart == sEnd)? sStart + text.length:sStart;
			target.selectionEnd = sStart + text.length;
		}
		else
		{
			target.value += text;
		}
		target.focus();
		storeCursor(target);
	}
}


/*  Popup Image functions  */
function detectexist(obj)
{
	return(typeof obj != "undefined")
}

function popupImage(imgPath, popupWidth, popupHeight, popupTitle, textDescription, popupBackGround, popupForeGround)
{
	function getpos()
	{
		leftpos = (detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popupWidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popupWidth/2 : 0;
		toppos = (detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popupHeight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popupHeight/2 : 0;
		if (window.opera)
		{
			leftpos -= screenLeft;
			toppos -= screenTop;
		}
	}

	getpos();

	var winattributes='width='+popupWidth+',height='+popupHeight+',resizable=no,left='+leftpos+',top='+toppos
	var bodyattribute=(popupBackGround.indexOf(".")!=-1)? 'background="'+popupBackGround+'"' : 'bgcolor="'+popupBackGround+'"'
	bodyattribute += " text='" + popupForeGround + "'"
	if (typeof jkpopwin=="undefined" || jkpopwin.closed)
		jkpopwin=window.open("","",winattributes)
	else
	{
		//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
		//jkpopwin.moveTo(leftpos, toppos)
		jkpopwin.resizeTo(popupWidth, popupHeight+30)
	}

	jkpopwin.document.open()
	jkpopwin.document.write('<html><title>'+popupTitle+'</title><body '+bodyattribute+' style="text-align: center; font-family: Verdana, Arial, Helvetica; font-size: 10px;"><img src="'+imgPath+'" style="margin-bottom: 0.5em"><br><span style="word-break:break-all;">'+textDescription+'</span></body></html>')
	jkpopwin.document.close()
	jkpopwin.focus()
}
/*  End Popup Image functions  */

function DisableClickableItems(DoValidatorOnSubmit)
{	
	var objButtonElems;
		
	// Disable all button onclick events while submitting
	objButtonElems= document.getElementsByTagName('input');

	for(i=0;i<objButtonElems.length;i++)
	{
		if(objButtonElems[i].type.toLowerCase() == 'submit' || objButtonElems[i].type.toLowerCase() == 'image')
		{
			objButtonElems[i].originalOnclick = objButtonElems[i].onclick;
			objButtonElems[i].originalCssClass = objButtonElems[i].className;
			objButtonElems[i].onclick = new Function('return false');
			objButtonElems[i].className = 'disabled';
		}
	}

	var objLinkElems;
	
	// Disable all link onclick events while submitting
	objLinkElems= document.getElementsByTagName('a');
	for(i=0;i<objLinkElems.length;i++)
	{
		objLinkElems[i].originalOnclick = objLinkElems[i].onclick;
		objLinkElems[i].onclick = new Function('return false');
	}
	
	//perform dotnet validate function which we override
	if( this["ValidatorOnSubmit"] && DoValidatorOnSubmit )
		ValidatorOnSubmit();
	
	if( this["Page_IsValid"] != undefined )
	{
		// Page_IsValid is a global variable from WebUIValidation.js
		if( !Page_IsValid )
		{
			//If page doesn't validate restore original onclick eventhandlers and styles
			for(i=0;i<objButtonElems.length;i++)
				if(objButtonElems[i].type.toLowerCase() == 'submit' || objButtonElems[i].type.toLowerCase() == 'image')
				{
					objButtonElems[i].onclick = objButtonElems[i].originalOnclick;
					objButtonElems[i].className= objButtonElems[i].originalCssClass;
				}
			
			for(i=0;i<objLinkElems.length;i++)
			{
				objLinkElems[i].onclick = objLinkElems[i].originalOnclick;		
			}
		}
	}
	else
	{
		//If Page_IsValid is not defined restore original onclick eventhandlers and styles
		for(i=0;i<objButtonElems.length;i++)
			if(objButtonElems[i].type.toLowerCase() == 'submit' || objButtonElems[i].type.toLowerCase() == 'image')
			{
				objButtonElems[i].onclick = objButtonElems[i].originalOnclick;
				objButtonElems[i].className= objButtonElems[i].originalCssClass;
			}
			
		for(i=0;i<objLinkElems.length;i++)
		{
			objLinkElems[i].onclick = objLinkElems[i].originalOnclick;		
		}	
	}
}

// News category mouseover
var mouseOver_TimeOut = 0;

function set_innerHTML(element, content)
{
	document.getElementById(element).innerHTML = content;
	clearTimeout(mouseOver_TimeOut);
}

function reset_innerHTML(element, content) {
	mouseOver_TimeOut = setTimeout("document.getElementById('" + element + "').innerHTML = '" + content + "';", 2000);
}

function resizeContentArea(editor)
{
	if (editor.InitialHeight == -1)
	{
		editor.InitialHeight = editor.Document.body.clientHeight;
	}
	
	var targetHeight = editor.Document.body.scrollHeight;
  
	if (targetHeight > editor.InitialHeight)
	{
		var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);
		theIFrame.style.height = parseInt(targetHeight) + "px";
  }
}

function attachResizeEvents(editor)
{
	editor.InitialHeight = -1;
	
	editor.GetContentArea().style.overflow = "hidden";
  var resizeFnRef = function anon(){resizeContentArea(editor)};
  editor.AttachEventHandler(RADEVENT_SEL_CHANGED, resizeFnRef)
  editor.AttachEventHandler("keydown", resizeFnRef)
}
