// ***********************************************************************************
// Editors / Controls Js
// ***********************************************************************************

// General
function SetField(pField,pValue)
{
	document.all.item(pField).value = pValue;
}

function __deleteItem(pTarget)
{
	if (window.confirm(msgDelete)) 
	{
		window.location.href = pTarget;
	}
}


// Editor.aspx ***********************************************************************
function sd(pItem,pImagePath)
{
	if (document.getElementById(pItem).style.display == "block")
	{
		document.getElementById(pItem).style.display = "none";
		document.getElementById("IMG_"+pItem).src = pImagePath + "i_cc.gif";
	}
	else
	{
		document.getElementById(pItem).style.display = "block";
		document.getElementById("IMG_"+pItem).src = pImagePath + "i_c.gif";
	}
}

function sdr(pItem,pImagePath,pHiddenField)
{
	if (document.all.item(pItem).style.display == "block")
	{
		document.all.item(pHiddenField).value = "none";
		document.all.item(pItem).style.display = "none";
		document.all.item("IMG_"+pItem).src = pImagePath + "ico_editor_admin_category_closed.gif";
	}
	else
	{
		document.all.item(pHiddenField).value = "block";
		document.all.item(pItem).style.display = "block";
		document.all.item("IMG_"+pItem).src = pImagePath + "ico_editor_admin_category.gif";
	}
}


function sdrx(pItem,pImagePath,pHiddenField,pCat)
{
	if (document.all.item(pItem).style.display == "block")
	{
		document.all.item(pHiddenField).value = pCat + "_0";
		document.all.item(pItem).style.display = "none";
		document.all.item("IMG_"+pItem).src = pImagePath + "ico_editor_admin_category_closed.gif";
	}
	else
	{
		document.all.item(pHiddenField).value = pCat + "_1";
		document.all.item(pItem).style.display = "block";
		document.all.item("IMG_"+pItem).src = pImagePath + "ico_editor_admin_category.gif";
	}
}

catArray = new Array();
function GetLink(pLinkbase,pSpot)
{	
	var ls = "";
	var c = 0;
	for (var i=0; i<catArray.length; i++)
	{
		if (c != 0)
		{
			ls += "x";
		}
		ls += document.all.item(catArray[i]).value;
		c++;
	}
	window.location.href = pLinkbase + "&tree=" + ls + pSpot;
	
}

function DeleteItem(pAsk,pLinkbase,pSpot)
{
	if (window.confirm(pAsk))
	{
		GetLink(pLinkbase,pSpot)
	}
}

function ome(pThis) // onmouseenter
{
	pThis.style.background='#FFFFFF';
}

function oml(pThis) // onmouseleave
{
	pThis.style.background="#EFEFEF";
}

function red(pModule)
{
	window.location.href = "Editor.aspx?select=" + pModule;
}


// Editor.aspx END ***********************************************************************




// Checklist ***********************************************************************
function CLCI(pItem, pStyleSet, pLast)//CheckListClickItem
{

	if (pLast == "True")
	{
		if (document.getElementById("h_"+ pItem).value == "True")
		{
			//document.getElementById(pItem).className = "c_cl_"+ pStyleSet +"_off";
			document.getElementById(pItem).style.background = "#efefef";
			document.getElementById("h_"+ pItem).value = "False";
		}
		else
		{
			//document.getElementById(pItem).className = "c_cl_"+ pStyleSet +"_on";
			document.getElementById(pItem).style.background = "#B5CBEB";
			document.getElementById("h_"+ pItem).value = "True";
		}
	}
	else
	{
		if (document.getElementById("h_"+ pItem).value == "True")
		{
			//document.getElementById(pItem).className = "c_cl_"+ pStyleSet +"_off_bb";
			document.getElementById(pItem).style.background = "#efefef";
			document.getElementById("h_"+ pItem).value = "False";
		}
		else
		{
			//document.getElementById(pItem).className = "c_cl_"+ pStyleSet +"_on_bb";
			document.getElementById(pItem).style.background = "#B5CBEB";
			document.getElementById("h_"+ pItem).value = "True";
		}
	}
}






// Checklist END ***********************************************************************






// PropertyRelationEditor ***********************************************************************

function PRD(pItem, pOpenImage, pClosedImage) // PropertyRelationDetail
{
	if (document.all.item("PropertyDetail"+ pItem).value == "False")
	{
		document.all.item("EditProperty"+ pItem).style.display = "block";
		document.all.item("PropertyDetail"+ pItem).value = "True";
		document.all.item("OpenImage"+ pItem).src = pOpenImage;
	}
	else
	{
		document.all.item("EditProperty"+ pItem).style.display = "none";
		document.all.item("PropertyDetail"+ pItem).value = "False";
		document.all.item("OpenImage"+ pItem).src = pClosedImage;
	}
}


function PRC(pItem) // PropertyRelationCount
{
	var count = 0;
	var lastCount = 0;
	var currentCount = 0;
	var begin = "<span class=c_pre_Editor_cb>";
	var end = "</span>";
	
	currentCount = document.all.item("PropertyRelationLastCounter").value;
	count = document.all.item("PropertyRelationCounterField").value;
	if (currentCount > 0)
	{
		count = currentCount;
		document.all.item("PropertyRelationLastCounter").value = 0;
	}

	document.all.item("PropertyRelationCounterField").value = count;

	count++;
	document.all.item("PropertyRelationCounterField").value = count;
	document.all.item("SortOrderCounter"+ pItem).innerHTML = begin + count + end;
	document.all.item("3_"+ pItem).value = count;
	
}

function PLC(pCount) // PropertySetLastCount
{
	var lastCount = 0;
	lastCount = document.all.item("PropertyRelationLastCounter").value;
	if (pCount > lastCount)
	{
		document.all.item("PropertyRelationLastCounter").value = pCount;
	}
}


function PSF(pItem) // PropertyRelationCountSetFirst
{
	var count = 1;
	var begin = "<span class=c_pre_Editor_cb>";
	var end = "</span>";

	document.all.item("PropertyRelationLastCounter").value = count;
	document.all.item("PropertyRelationCounterField").value = count;
	document.all.item("SortOrderCounter"+ pItem).innerHTML = begin + count + end;
	document.all.item("3_"+ pItem).value = count;
}

function RCS(pItem,pCount) // PropertyRelationCountSetSpecific
{
	var count = 0;
	if (pCount > 0)
	{
		count = pCount;
	}
	var begin = "<span class=c_pre_Editor_cb>";
	var end = "</span>";

	document.all.item("PropertyRelationCounterField").value = count;
	document.all.item("SortOrderCounter"+ pItem).innerHTML = begin + count + end;
	document.all.item("3_"+ pItem).value = count;
}




function CNR(pID) //CreateNewPropertyRelation
{
	document.getElementById("EditPropertyRelation").value = pID;
	document.getElementById("PropertyRelationAction").value = "add";
	document.forms[0].submit();
}

function RPR(pID) // RemovePropertyRelation
{
	if (window.confirm('Do you really want to remove this relation?'))
	{
		document.getElementById("EditPropertyRelation").value = pID;
		document.getElementById("PropertyRelationAction").value = "remove";
		document.forms[0].submit();
	}
}



// PropertyRelationEditor END ***********************************************************************




// TemplateEditor ***************************************************************************


var restrictoutput = false;

function ip(iproperty,ixmlproperty) // Insert Property
{
	if (restrictoutput != true)
	{
		var insertProperty = "";
		var name = document.all.item("TE_select_Name").value;
		var titlename = document.all.item("TE_title_Name").value;
		var bodyname = document.all.item("TE_body_Name").value;
		var temptypename = document.all.item("TE_temp_Type").value;
		var temptype = document.all.item(temptypename).value;
		var target = document.forms[0].elements(document.all.item(name).value)
		var sel = document.selection;		
		
		insertProperty = iproperty;
		
		switch (temptype)
		{
			case "html":
				break;
			case "activecontent":
				break;
			case "xslt":
				if (document.all.item(name).value == bodyname)
				{
					insertProperty = ixmlproperty;
				}
				break;
			case "renderclass":
				break;
		}


		if (sel!=null)			
		{				
			var rng = sel.createRange();
			var oRng1 = document.body.createTextRange();
			oRng1.moveToElementText(target);
			if (rng!=null) 
			{	
				if (oRng1.inRange(rng))
				{
					 rng.text = insertProperty;
				}
				else
				{
					//alert("not in range[" + rng.text +"]");
					oRng1.text = oRng1.text + insertProperty;
				}
				
			}
			else
			{
				//alert('range null');
				oRng1.text = oRng1.text + insertProperty;
			}
		}
	}
}



function ipt(pStartTag,pEndTag) // Insert TAG
{
	var rangeTop = 0;
	var rangeLeft = 0;
	var rangeRight = 0;
	var rangeBottom = 0;
	if (restrictoutput != true)
	{
		var name = document.all.item("TE_select_Name").value;
		var target = document.forms[0].elements(document.all.item(name).value)
		var sel = document.selection;		
		if (sel!=null)			
		{	
			var rng = sel.createRange();
			var oRng1 = document.body.createTextRange();
			oRng1.moveToElementText(target);

			if (rng!=null) 
			{	
				/*
				rangeTop = rng.boundingTop;
				rangeLeft = rng.boundingLeft;
				rangeRight = (rng.boundingLeft + rng.boundingWidth);
				rangeBottom = (rng.boundingTop + rng.boundingHeight);
				*/

				if (oRng1.inRange(rng))
				{
					 rng.text = pStartTag + rng.text + pEndTag;
				}
				else
				{
					//alert("not in range, [" + rangeTop + ", "+ rangeLeft +", "+ rangeRight +", "+ rangeBottom +"]");
					oRng1.text = oRng1.text + pStartTag + " " + pEndTag;
				}
				
			}
			else
			{
				//alert('range null');
				oRng1.text = oRng1.text + pStartTag + " " + pEndTag;
			}
		}
	}
}

function clickIsTempSelect()
{
	document.forms[0].submit();
}

function HideShowPropCat(hiddVal)
{
	if (document.getElementById("CatProp_PRProperties_" + hiddVal).value == "False")
	{
		document.getElementById("hide_show_PRProperties_" + hiddVal).style.display = "block";
		document.getElementById("CatProp_PRProperties_" + hiddVal).value = "True";
	}
	else
	{
		document.getElementById("hide_show_PRProperties_" + hiddVal).style.display = "none";
		document.getElementById("CatProp_PRProperties_" + hiddVal).value = "False";
	}
}

function toce(pType) // onchange event select template types
{
	var name = document.all.item("TE_select_Name").value;
	var renderername = document.all.item("TE_renderer_Name").value;
	
	switch (pType)
	{
		case  "html":
			document.all.item(renderername).value = 14;
			document.all.item("HTML_tb_" + name).style.display = "block";
			document.all.item("STATIC_tb_" + name).style.display = "block";
			document.all.item("XSLT_tb_" + name).style.display = "none";
			document.all.item("ACL_tb_" + name).style.display = "none";
			document.all.item("FD_tb_"+ name).style.display = "none";
			document.all.item("RCL_tb_" + name).style.display = "none";
			document.all.item("textareadiv_TE_TETemplates_body").style.display = "block";
			break;
		case "activecontent":
			document.all.item(renderername).value = 15;
			document.all.item("HTML_tb_" + name).style.display = "block";
			document.all.item("STATIC_tb_" + name).style.display = "block";
			document.all.item("XSLT_tb_" + name).style.display = "none";
			document.all.item("ACL_tb_" + name).style.display = "block";
			document.all.item("FD_tb_"+ name).style.display = "none";
			document.all.item("RCL_tb_" + name).style.display = "none";
			//document.all.item("textareadiv_TE_TETemplates_body").style.display = "none";
			document.all.item("textareadiv_TE_TETemplates_body").style.display = "block";	
			break;
		case "xslt":
			document.all.item(renderername).value = 23;
			document.all.item("HTML_tb_" + name).style.display = "block";
			document.all.item("STATIC_tb_" + name).style.display = "block";
			document.all.item("XSLT_tb_" + name).style.display = "block";
			document.all.item("ACL_tb_" + name).style.display = "none";
			document.all.item("FD_tb_"+ name).style.display = "none";
			document.all.item("RCL_tb_" + name).style.display = "none";
			document.all.item("textareadiv_TE_TETemplates_body").style.display = "block";
			break;
		case "formdisplay":
			document.all.item(renderername).value = 98;
			document.all.item("HTML_tb_" + name).style.display = "block";
			document.all.item("STATIC_tb_" + name).style.display = "block";
			document.all.item("XSLT_tb_" + name).style.display = "none";
			document.all.item("ACL_tb_" + name).style.display = "none";
			document.all.item("FD_tb_"+ name).style.display = "block";
			document.all.item("RCL_tb_" + name).style.display = "none";
			document.all.item("textareadiv_TE_TETemplates_body").style.display = "block";			
			//document.all.item("textareadiv_TE_TETemplates_body").style.display = "none";
			break;
		default:
			document.all.item("HTML_tb_" + name).style.display = "block";
			document.all.item("STATIC_tb_" + name).style.display = "block";
			document.all.item("XSLT_tb_" + name).style.display = "none";
			document.all.item("ACL_tb_" + name).style.display = "none";
			document.all.item("FD_tb_"+ name).style.display = "none";
			document.all.item("RCL_tb_" + name).style.display = "block";
			//document.all.item("textareadiv_TE_TETemplates_body").style.display = "none";
			document.all.item("textareadiv_TE_TETemplates_body").style.display = "block";	
			break;
	}
}

function aoce(pValue)
{
	var bodyname = document.all.item("TE_body_Name").value;
	document.all.item(bodyname).value = pValue;
}

function roce(pRenderer) // onchange event select renderer classes
{
	var renderername = document.all.item("TE_renderer_Name").value;
	if (pRenderer > 0)
	{
		//alert(pRenderer);
		document.all.item(renderername).value = pRenderer;
	}
	/*
	else
	{
		alert(pRenderer);
	}
	*/
}


// TemplateEditor END ***********************************************************************

window.setTimeout('InitResizeHandlers()',10);

function InitResizeHandlers()
{
		document.body.onmousemove=move; 
		document.body.onmouseup=mouseup;
}

var curSizeValue=0;
function move()
{
	if (timer!=0)
	{
		window.clearTimeout(timer);
		timer=0;		
	}	
	if (resizeMode==true)
	{
	
		if (resizeDim=='width' || resizeDim=='both')
		{
			curSizeValue=window.event.clientX-tdStartX;
			if (window.event.clientX-tdStartX>0)
			{
				oelem.width=curSizeValue;
				if (oelem.style)
					oelem.style.width=curSizeValue;
			}
			window.status=curSizeValue;
		}
		if (resizeDim=='height' || resizeDim=='' || resizeDim=='both')
		{
			curSizeValue=window.event.clientY-tdStartY;
			if (window.event.clientY-tdStartY>0)
			{
				oelem.height=curSizeValue;			
				if (oelem.style)
					oelem.style.height=curSizeValue;
			}
			window.status=curSizeValue;
		}
	}
		
	
	return true;
}

var resizeDim='width';
var timer;

function mouseup()
{
	if (timer!=0)
	{
		window.clearTimeout(timer);
		timer=0;		
		window.status='';
	}
	if (resizeMode==true && DragDropEnabled && clientResize!='ClientOnly')
	{
		resizeMode=false;
		document.body.releaseCapture();
		var url=strCallBack + '&s_resizeElement=true&s_PropertyId=' + propertyID + '&s_ElementId=' + resizeElementID + '&s_width=';
		//if (resizeDim=='width')
		//	execASP(url + oelem.width+'#show_'+ resizeElementID);
		//else
			execASP(url + curSizeValue +'#show_'+ resizeElementID);
	}
	if (resizeMode==true && DragDropEnabled && clientResize=='ClientOnly')
	{
		resizeMode=false;
		resizeDim='';
		document.body.releaseCapture();
	}
}

var DragDropEnabled=true;
var tdStartX;
var tdStartY;

var clientResize='';
var oelem;
var resizeMode;
resizeMode=false;

function ResizeElementClient(element, dimension)
{
		resizeMode=true;
		resizeDim=dimension;
		clientResize='ClientOnly';		
		document.body.setCapture();
		window.event.cancelBubble=true;
		oelem=element;	
		tdStartX=window.event.clientX-oelem.scrollWidth;
		tdStartY=window.event.clientY-oelem.scrollHeight;
		return false;
}

function SelectMediaElement(val, valPath,dontSubmit)
{
	if(opener && opener.document && opener.document.forms[0] && opener.document.getElementsByName(window.name)[0])
	{
	
		//opener.onbeforeunload=null;
		if(window.name.indexOf("prop_")==0)
		{
		
			opener.document.getElementsByName(window.name)[0].value=opener.document.getElementsByName(window.name)[0].value + '<img border=0 src="' + valPath +'">';opener.document.forms[0].submit();window.close();
		}
		else
		{
			opener.document.getElementsByName(window.name)[0].value=val;
			if(!dontSubmit)
			{
				opener.document.forms[0].submit();
			}
			window.close();
		}
	}
	else if (window.frameElement)
	{
		parent.document.getElementsByName(window.frameElement.getAttribute('targetField'))[0].value=val;parent.document.forms[0].submit();
	}
}


function Search(Url, SearchText)
{
	window.location.href=Url +'searchText=' + SearchText;
}

function EMail(Url, EMailText)
{
	window.location.href=Url +'emailText=' + EMailText;
}


function ToggleCheck(imgTag, elementID)
{
	imgTag.src=AppRootPath + '/Kontext/WebService/ToggleListItem.aspx?avs=' + CurrentAppViewStateID + '&ID=' + elementID;
}

function markText(textVal, color)
{
  if (document.body.createTextRange)
  {
	var oRange = document.body.createTextRange();
	oRange.collapse();
	var first=true;
	while (oRange.move("word", 1))
	{
		if (first)
		{
			first=false;
			oRange.move("word",-1);
		}
		oRange.expand("word");
		if(oRange.findText(textVal))
		{
			oRange.execCommand('BackColor', "false", color);
		}

	}
  }
}

var timeoutset=false;

function validateFieldLength(control, maxlen, strMsg)
{
	window.status=control.innerText.length + '/' + maxlen;
	if (!timeoutset)
	{
		timeoutset=true;
		window.setTimeout('window.status="";timeoutset=false', 2000);
	}
	
	if (control.innerText.length>maxlen && window.event.keyCode!=8 && window.event.keyCode!=46 && window.event.keyCode!=37 && window.event.keyCode!=39)
	{
		var re='%1';
		strng=new String(strMsg);
		var txt=strng.replace(re,control.innerText.length);		
		window.alert(txt);
	}
	
	
	
}
