// CMS Alert & Comment Function.
function OpenParentPop(OBH, UDF, VAL, CURRENTOBH){
	window.open('?OBH=' + OBH + '&NW=Y&UDF=' + UDF +'&VAL=' + VAL +'&_OBH=' + CURRENTOBH,'','height=150,width=650');
	return false;
}

function OpenCMSPop(OBH, UDF, VAL, SCR){
	if(SCR){
	window.open('?OBH=' + OBH + '&NW=Y&UDF=' + UDF +'&VAL=' + VAL,'','height=300,width=450,scrollbars=yes');
	}else{
	window.open('?OBH=' + OBH + '&NW=Y&UDF=' + UDF +'&VAL=' + VAL,'','height=150,width=650');
	}
	return false;
}

function toggleCheckBox(obj){
	var v = obj.value;
	if(v=='Y'){
		obj.value = 'N';
	}else{
		obj.value = 'Y';										
	}
}


function doActionAlert(ps_Url, ps_Message, pb_DoComment, ps_Mode)
{
	var pa_Arguments = new Array(5);
	pa_Arguments[0] = ps_Url;
	pa_Arguments[1] = ps_Message;
	pa_Arguments[2] = pb_DoComment;
	pa_Arguments[3] = ps_Mode;
	pa_Arguments[4] = null;
	
	
	//NOTE: Change the height of our dialog box based on the existence of the comments field.
	var ls_WinHeight = '180';
	if(pb_DoComment) ls_WinHeight = '320';
	
	pa_Arguments = showModalDialog('jsfunctions/comment.htm', pa_Arguments, 'scroll:0; help:0; unadorned:1; center:1; status:0; dialogWidth:300px; dialogHeight:' + ls_WinHeight + 'px');
	//NOTE: We are running the code so redirect the user accordingly.
	if(pa_Arguments)
	{
		//NOTE; Create our handler form and add info to it.
		var lo_Form = document.createElement('FORM');
		document.body.appendChild(lo_Form);
		lo_Form.style.display = 'none';
		lo_Form.name = 'cms_comment';
		lo_Form.method = 'post';
		lo_Form.action = 'index.aspx' + pa_Arguments[0];
		
		//NOTE: If we are posting back comments then append them to the form.
		if(pa_Arguments[2])
		{
			var lo_textArea = document.createElement('TEXTAREA');
			lo_Form.appendChild(lo_textArea);
			lo_textArea.name = 'COMMENT';
			lo_textArea.value = pa_Arguments[4];
		}
		lo_Form.submit();
	}
	else
	{
		return;
	}
}
	
// toggle function for related items
function toggleREL(element)
{
	var oElement = document.getElementById(element);
	var oExpDiv = document.getElementById("DIV_EXP_" + element);
	var oColDiv = document.getElementById("DIV_COL_" + element);
	
	if (oElement)
	{
		if(oElement.style.display == 'none')
		{
			oElement.style.display = 'block';
			if ((oExpDiv) && (oColDiv))
			{
				oExpDiv.style.display = 'none';
				oColDiv.style.display = 'block';
			}
		}
		else
		{
			oElement.style.display ='none';
			if ((oExpDiv) && (oColDiv))
			{
				oExpDiv.style.display = 'block';
				oColDiv.style.display = 'none';
			}
		}
	}
}
// toggle for trees
function TreeToggle(sElement, sImgName, type)
{
	var oElement = document.getElementById(sElement);
	var oImage = document.getElementById(sImgName);
	
	if (oElement)
	{
		if (oElement.style.display == 'none') 
		{
			oElement.style.display = 'block';
			if (oImage)	
			{
				oImage.src = 'images/i_minus.gif';
				if (type == 'ver'){
					oImage.alt = 'Hide Version History';
				}
				else
				{
					oImage.alt = 'Close Level';
				}
			}
		} else {
			oElement.style.display = 'none';
			if (oImage)	
			{
				oImage.src = 'images/i_plus.gif';
				if (type == 'ver'){
					oImage.alt = 'View Version History';
				}
				else
				{
					oImage.alt = 'Expand Level';
				}
			}
		}
	}
	
}

// toggle function for switching form tabs
function toggleTAB(tab)
{
	var tpelement = document.getElementById("TABPANE"+tab);
	var telement = document.getElementById("TAB"+tab);
	if (!tpelement||!telement)
	{
		return;
	}
	{
	if (tpelement.style.display == 'none') {
		HideTabs();
		tpelement.style.display = 'block';
		//telement.style.borderBottomWidth = '0px';
		//telement.style.backgroundColor = '#ffffff';
		telement.className = 'tabon';
	}
	}
}

// function to hide all tabs
function HideTabs(){
	for (var x = 0; x < alltabs.length; x++){
		document.getElementById('TABPANE' + alltabs[x]).style.display = 'none';
		//document.getElementById('TAB' + alltabs[x]).style.borderBottomWidth = '1px';
		//document.getElementById('TAB' + alltabs[x]).style.backgroundColor = '#eeeeee';
		document.getElementById('TAB' + alltabs[x]).className = 'taboff';
	}
}

// function to toggle multilanguage form fields
function toggleML(lang){
	for (var x = 0; x < allML.length; x++){
		for (y = 0; y < languages.length; y++){
			if (languages[y] != lang){
			var element = allML[x] + '_' + languages[y];
			document.getElementById(element).style.display = 'none';
			}
			else
			{
			var element = allML[x] + '_' + languages[y];
			document.getElementById(element).style.display = 'block';
			}
		}
		
	}
	currentlang = lang;
}

function toggleFlag(lang){
	for (var a = 0; a < alltabs.length; a ++){
		for (b = 0; b < languages.length; b++){
			if(languages[b] != lang){
				var element = 'MLFlag_' + alltabs[a] + '_' + languages[b];
				if(document.getElementById(element)){
					var imagename = 'images/' + languagesIMAGES[b];
					document.getElementById(element).src = imagename;
					document.getElementById(element).alt = 'Click to select ' + languagesNAME[b];
				}
			}
			else
			{
				var element = 'MLFlag_' + alltabs[a] + '_' + languages[b];
				if(document.getElementById(element)){
					var imagename = 'images/off_' + languagesIMAGES[b];
					document.getElementById(element).src = imagename;
					document.getElementById(element).alt = languagesNAME[b] + ' is the selected language';
				}
			}
		}
	}
}



// function for changing element background colours
function chgColour(currObj,strHex)
{
	currObj.style.backgroundColor = strHex;
}



function NewPerm(objTBody, objTRow, cntfld)
{
	//Create our global variables...
	var objTCell = objTRow.getElementsByTagName('TD');
	var strOrgFldNme = new String();
	var strNewFldNme = new String();
	var strNewVal = new String();
	
	//Go and loop through our TD elements and get the INPUT elements...
	for (var iCell = 0; iCell<objTCell.length; iCell++)	{
		//Go and loop through our input elements adding the ROWID to generate a unique field...
		var objFld = objTCell[iCell].childNodes;
		for (var iFld = 0; iFld<objFld.length; iFld++)	{
			switch(objTRow.childNodes[iCell].childNodes[iFld].nodeName)
			{
				case 'INPUT' :
					strOrgFldNme = objFld[iFld].getAttribute('name')
					strNewVal = eval(objFld[iFld]).value;
					strNewFldNme = strOrgFldNme.substring(0,(strOrgFldNme.length - 4)) + new String(document.getElementById(cntfld).value);
					
					objTRow.childNodes[iCell].childNodes[iFld].setAttribute('id',strNewFldNme);
					objTRow.childNodes[iCell].childNodes[iFld].setAttribute('name',strNewFldNme);
					objTRow.childNodes[iCell].childNodes[iFld].setAttribute('value',strNewVal);
					break;
				case 'SELECT' :
					strOrgFldNme = objFld[iFld].getAttribute('name')
					strNewFldNme = strOrgFldNme.substring(0,(strOrgFldNme.length - 4)) + new String(document.getElementById(cntfld).value);
					
					objTRow.childNodes[iCell].childNodes[iFld].setAttribute('id',strNewFldNme);
					objTRow.childNodes[iCell].childNodes[iFld].setAttribute('name',strNewFldNme);
					break;
				case '#text' :
		//			//Do Nothing
					break;
			}
		}
	}
	
	objTRow.setAttribute('style','display:block');
	objTRow.style.display = 'block';
	
	objTBody.appendChild(objTRow);
	
	// increment counter field
	(document.getElementById(cntfld).value)++;
	return;
}

function deleteRow(objTRow,iRowNum,arrDELs)	{
	//We don't delete the row we just hide it from the user so the auto increment still works...
	objTRow.style.display = 'none';
	//We then need to flag the row as deleted so when we submit to the server we don't add it to the DB...
	for (var i=0; i < arrDELs.length; i++)
	{
		document.getElementById('_QUES_DELETE_' + arrDELs[i] + '_' + iRowNum).value = 'Y';
	}
	return;
}

function RemoveImage(sId){
	var lo_Element = document.getElementById('_IMG' + sId);
	lo_Element.src = '/images/i_SELECT_IMAGE.gif';
	lo_Element.className = 'noimage';
	document.getElementById(sId).value = "";
	document.getElementById('remimage').style.display='none';
}


function UpdateCMSImageSelection(filename, alt) {

var ls_Library = '/resources/library/';

window.opener.SetUrl(ls_Library + filename, alt) ;
window.close();

}


function UpdateDataField(sFilename, sTitle, sId, bWysiwyg, sMode)
{

	if (window.opener && !window.opener.closed)
	{
		//NOTE: If we are working with the wysiwyg deal with the request differently.
		if(bWysiwyg)
		
		{
		
			var ls_Library = 'resources/library/';
			switch(sMode)
			{
				//NOTE: Static image request we update the form fields with our data and set our image source.
				case 'static':
					var lo_Element = window.opener.document.getElementById(sId);
					if(lo_Element) lo_Element.value = sFilename;
					lo_Element = window.opener.document.getElementById('_IMG' + sId);
					
					if(lo_Element)
					{
						window.opener.document.getElementById('remimage').style.display='block';
						lo_Element.src = ls_Library + sFilename;
						lo_Element.className = 'image';
					}
					break;
				//NOTE: Dynamic image we update the contant editable area with html image code crated by the modal dialog.
				case 'dynamic':
					window.opener.document.getElementById('src').value = ls_Library + sFilename;
					window.opener.document.getElementById('alt').value = sTitle;
			}		
		}
		//NOTE: Otherwise return the Image Name to the CMS image field control.
		else
		{
		
			var udf1 = window.opener.document.getElementById(sId);
			if (udf1)
			{
				udf1.value = sFilename;
			}
			
			var udf2 = window.opener.document.getElementById("_CMSIMG" + sId);
			if (udf2)
			{
				udf2.value = sFilename;
			}
			
			var udf3 = window.opener.document.getElementById("lbl_" + sId);
			if (udf3)
			{
				udf3.innerText = Trim(sTitle);
			}
		}
	}
   	window.close();
}

function CmsImagePP(iOBH, sId, bWysiwyg, sMode)
{
	var sUrl;
	if(bWysiwyg)
	{
		sUrl = 'index.aspx?OBH=' + iOBH + '&UDF=' + sId + '&WYS=Y&MDE=' + sMode + '&NW=Y'
	}
	else
	{
		sUrl = 'index.aspx?OBH=' + iOBH + '&UDF=' + sId + '&NW=Y'
	}
	window.open(sUrl,"WIN_CMS" + sId,"toolbar=no,location=no,status=yes,scrollbars=yes,height=600,width=660,left=10,top=10");
}

function ClearImagePP(sId)
{
	var udf1 = document.getElementById(sId);

	if (udf1)
	{
		udf1.value = "";
	}
	var udf2 = document.getElementById("_CMSIMG" + sId);

	if (udf2)
	{
		udf2.value = "";
	}
	
	var oClearCmsButton = document.getElementById("_CLRCMSBUT" + sId);
	if (oClearCmsButton)
	{
		oClearCmsButton.focus();
	}
}

function doActiveOption(po_Select)
{
	var ls_Value = po_Select.options[po_Select.selectedIndex].value;
	if(!ls_Value)
	{
		return;
	}
	else
	{
		eval(ls_Value);
	}
	po_Select.selectedIndex = 0;
}

function ConfirmMSG(strURL, strMessage)
	{
		var doit
		if (!strMessage)
			{
				doit = window.confirm('Are you sure you want to delete this item?');
			}			
		else
			{
				doit = window.confirm(strMessage);
			}		
		if (doit)
			{
				window.location.href = strURL;
			}
		else
			{
				document.focus();
			}
	}	



/***********************************************************************************/
// MENU ADD PAGE STUFF
/***********************************************************************************/

// global pop up calendar current date
var gsAddPageParams = '';


// global Mouse Position
var giMouseX = 0;
var giMouseY = 0;


if (window.event)
{
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = captureMousePosition;
}
else
{
	document.onmousemove = captureMousePosition;
}

/***********************************************************************************/
function fShowAddPage( psMenuParams )
/***********************************************************************************/
{
	gsAddPageParams = psMenuParams;

	var oAddDiv = document.getElementById("PAGEADD");

	if (oAddDiv)
	{
		//alert(giMouseX + ',' + giMouseY);
/*
		if (document.layers) {
			// Netscape
			alert('Netscape');
			oAddDiv.left = giMouseX - 10;
			oAddDiv.top = giMouseY - 10;
		} else if (document.all) {
			// IE
			alert('IE');
			oAddDiv.style.left = giMouseX - 10;
			oAddDiv.style.top = giMouseY - 10;
		} else if (document.getElementById) {
			// Netscape 6 behaves the same as Netscape 4 in this regard
			alert('Netscape 6');
			oAddDiv.left = giMouseX - 10;
			oAddDiv.top = giMouseY - 10;
		}
*/
		oAddDiv.style.left = giMouseX - 10;
		oAddDiv.style.top = giMouseY - 10;

		oAddDiv.style.visibility='visible';
		//oAddDiv.style.display='block';
	}

}

/***********************************************************************************/
function fHideAddPage( )
// *******************************************************************************
{
//	alert('fHideAddPage()');
//	gsAddPageParams = '';

	var oAddDiv = document.getElementById("PAGEADD");

	// 3px margin to be on the safe side
	var iXmin = 3 + oAddDiv.offsetLeft
	var iXmax = -3 + parseInt(oAddDiv.style.left) + ((oAddDiv.offsetWidth)?(oAddDiv.offsetWidth):(oAddDiv.clientWidth))
	var iYmin = 3 + oAddDiv.offsetTop
	var iYmax = -3 + parseInt(oAddDiv.style.top) + ((oAddDiv.offsetHeight)?(oAddDiv.offsetHeight):(oAddDiv.clientHeight))
/*
alert('X: ' + giMouseX + '<=' + iXmin);
alert('X: ' + giMouseX + '>=' + iXmax);

alert('Y: ' + giMouseY + '<=' + iYmin);
alert('Y: ' + giMouseY + '>=' + iYmax);
*/

	if (oAddDiv)
	{
		if	(	(giMouseX <= iXmin)
			||	(giMouseX >= iXmax) 
			||	(giMouseY <= iYmin) 
			||	(giMouseY >= iYmax) 
			)
		{
			oAddDiv.style.visibility='hidden';
		}
	}

}

/***********************************************************************************/
function fMenuAdd( psAddParams )
// *******************************************************************************
{
	//alert(psAddParams + '&amp;' + gsAddPageParams);
	document.location = '/?' + psAddParams + '&amp;' + gsAddPageParams;
}

/***********************************************************************************/
function captureMousePosition(e) 
/***********************************************************************************/
{
/*
	if (e.clientX)	// OPERA
	{
		giMouseX = e.clientX + document.body.scrollLeft;
		giMouseY = e.clientY + document.body.scrollTop;
	}
	else 
*/	
	if (e)	
	{
        giMouseX = e.pageX;
        giMouseY = e.pageY;
	}
	else 
	{
        giMouseX = window.event.x+document.body.scrollLeft;
        giMouseY = window.event.y+document.body.scrollTop;
	}	
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);

		if(TRIM_VALUE==""){
			return "";
		}
		else{
			return TRIM_VALUE;
		}
}


function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";

	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){}
			else{
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
		}
		return strTemp;
}


function LTrim(VALUE){
	var w_space = String.fromCharCode(32);


		if(v_length < 1){
			return"";
		}
		var v_length = VALUE.length;
		var strTemp = "";
		var iTemp = 0;


			while(iTemp < v_length){
				if(VALUE.charAt(iTemp) == w_space){}


					else{
						strTemp = VALUE.substring(iTemp,v_length);
						break;
					}
					iTemp = iTemp + 1;
				}
				return strTemp;
		}
		

/***********************************************************************************/
/* END OF FILE */
/***********************************************************************************/

