//This file is used in default.aspx and all other public pages
var parentWnd = self.opener;
var clickObj = null

function process_form(action) {
	var msg = "";

	document.forms[0].action.value=action

	if (msg=="")
		document.forms[0].submit();
	else
		alert(msg);
}
function CallSearch(ArticleID, ModuleID, OpenInNewWindow)
{
	var strEval
	strEval = document.getElementById('Module' + ModuleID + '_strSearchKeyword' + ModuleID).value;
	
	if (isblank(strEval))
	{
		alert ("Please enter your keywords");
		return;
	}

	//Added by Tracey on 24th May 2005. Cater for user friendly URLs
	var baseObj = document.getElementById('BaseDefault');
	var baseRef;
	if (baseObj) {
		baseRef = baseObj.href.substring(0, baseObj.href.lastIndexOf("/") + 1) + 'PerformSearch.aspx';
	} else {
		baseRef = location.href.substring(0, location.href.lastIndexOf("/") + 1) + 'PerformSearch.aspx';
	}
	if (OpenInNewWindow == 1)
		window.open(baseRef + "?ArticleID=" + ArticleID + "&ModuleID=" + ModuleID + "&Keywords=" + escape(encodeMyHtml(strEval)));		
	else
		location.href = baseRef + "?ArticleID=" + ArticleID + "&ModuleID=" + ModuleID + "&Keywords=" + escape(encodeMyHtml(strEval));		
}

function encodeMyHtml(strValue)
{
var  encodedHtml = strValue; 
encodedHtml = stringreplace(encodedHtml, "=", "%3D");
encodedHtml = stringreplace(encodedHtml, "+", "%2B");
encodedHtml = stringreplace(encodedHtml, "?", "%3F");
encodedHtml = stringreplace(encodedHtml, "&", "%26");
encodedHtml = stringreplace(encodedHtml, "@", "%40");
return encodedHtml;
}

function CallNews(ArticleID, OpenInNewWindow)
{
	//Added by Tracey on 24th May 2005. Cater for user friendly URLs
	var baseObj = document.getElementById('BaseDefault');
	var baseRef;
	if (baseObj) {
		baseRef = baseObj.href.substring(0, baseObj.href.lastIndexOf("/") + 1) + 'default.aspx';
	} else {
		baseRef = location.href;
	}

	if (OpenInNewWindow == 1)
		window.open(baseRef + "?ArticleID=" + ArticleID);		
	else
		location.href = baseRef + "?ArticleID=" + ArticleID ;		
}

function CallNewsUserFriendly(strURL, OpenInNewWindow)
{
	//Added by Tracey on 24th May 2005. Cater for user friendly URLs
	var baseObj = document.getElementById('BaseDefault');
	var baseRef;
	if (baseObj) {
		baseRef = baseObj.href.substring(0, baseObj.href.lastIndexOf("/") + 1);
	} else {
		baseRef = location.href;
	}
	if (OpenInNewWindow == 1)
		window.open(baseRef + strURL);		
	else
		location.href = baseRef + strURL;		
}

function CallAdvert(strURL)
{
	window.open(strURL);		
}

function SetAction(action)
{
	document.forms[0].action.value=action
}

function ResubmitFormWithEdit(ArticleID)
{
	location.href = "default.aspx?Mode=BusyEditing&ArticleID=" + ArticleID
}

function PrintForm(sURL)
{
	var oWnd = window;	
	var oDoc = oWnd.document;	
	var strIFrame = "<iframe id='printHiddenFrame' name='printHiddenFrame' width='0' height='0'></iframe>";
	var strHFrame = "<frameset name=test onload='printMe.focus();printMe.print();' rows=\"100%\">" +
				"<frame name=printMe src=\""+sURL+"\">" +
				"</frameset>";

	if( oWnd.printHiddenFrame == null){
	  if (document.body.insertAdjacentHTML) {
	    //IE only
        oDoc.body.insertAdjacentHTML("beforeEnd", strIFrame);
         var framedoc = oWnd.printHiddenFrame.document;
         framedoc.open();
         framedoc.write(strHFrame);
         framedoc.close();
      } else if( typeof( document.body.innerHTML ) != 'undefined' ) {
         document.body.innerHTML += strIFrame;
         var framedoc = oWnd.printHiddenFrame.document;
         framedoc.open();
         framedoc.write(strHFrame);
         framedoc.close();
      } else {
        //use the old code
        var wndPrint = window.open(sURL, 'Print', 'toolbar=no,width=700,height=600,resizable, scrollbars');
        wndPrint.setTimeout('window.print();window.close();',1000);
      }
    } else {
      oWnd.printHiddenFrame.focus();
      oWnd.printHiddenFrame.print();
    }
}

function CheckKey(evt)
{
	if (evt.keyCode == 13)
	{
		if (!document.all && document.getElementById) {
			evt.preventDefault();
			evt.stopPropagation();
		}
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
}

function HandleSearch(evt, ArticleID, ModuleID, OpenInNewWindow)
{
	if (evt.keyCode == 13)
	{
		CallSearch(ArticleID, ModuleID, OpenInNewWindow)
		if (!document.all && document.getElementById) {
			evt.preventDefault();
			evt.stopPropagation();
		}
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
}

function CallLogin(ModuleID, CurrentPage)
{

	var strUID;
	strUID = document.getElementById('Module' + ModuleID + '_strLoginName' + ModuleID).value;

	var strPassword;
	strPassword = document.getElementById('Module' + ModuleID + '_strPassword').value;

	//Added by Tracey on 28th Nov 2005. Cater for remember me cookie
	var strRememberMe;
	var strRememberMeURL = "";
	var strRememberMePassword = "";
	if (document.getElementById('Module' + ModuleID + '_chkRememberMe'))
	{
		strRememberMe = document.getElementById('Module' + ModuleID + '_chkRememberMe').checked;
		if (strRememberMe)
		{	
			strRememberMeURL = '&RemCook=Yes';
		}
	}

	//Added by Tracey on 24th May 2005. Cater for user friendly URLs
	var baseObj = document.getElementById('BaseDefault');
	var baseRef;
	if (baseObj) {
		baseRef = baseObj.href.substring(0, baseObj.href.lastIndexOf("/") + 1) + 'Login.aspx';
	} else {
		baseRef = location.href;
	}
	if (document.getElementById) {
	  document.form2.action = 'login.aspx?PublicPage=Yes' + strRememberMeURL + '&RedirectURL=' + CurrentPage;
	  document.form2.UID.value = strUID;
	  document.form2.PWD.value = strPassword;
	  document.form2.submit();
	}
	return false;
}


function HandleLogin(evt, ModuleID, CurrentPage)
{
	if (evt.keyCode == 13)
	{
		CallLogin(ModuleID, CurrentPage)
		
		if (!document.all && document.getElementById) {
			evt.preventDefault();
			evt.stopPropagation();
		}
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;

	}
}

function ReferFriendOpen(strURL, intArticleID, strPageToOpen)
{
	window.open(strPageToOpen + '?Article=' + intArticleID + '&' + strURL, 'Help', 'toolbar=no,width=580,height=380,resize=no, scrollbar=no');
}


/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!

  This is used when embedding the shopping site inside an iframe.
  Use these function to allow the iframe to resize depending on the size
  of the shopping page so that scroll bars do not show.
*************************************************************************/

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  
if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) {
		docHt = Math.max(sh, oh);
	}
	else if (!sh && oh) {
		docHt = oh;
	}
	else if (sh && !oh) {
		docHt = sh;
	}

}
  return docHt;
}

function getDocWidth(doc) {
  var docWt = 0, sw, ow;

if (doc.body) {
    if (doc.body.scrollWidth) docWt = sw = doc.body.scrollWidth;
    if (doc.body.offsetWidth) docWt = ow = doc.body.offsetWidth;
    if (sw && ow) {
		docWt = Math.max(sw, ow);
	}
    else if (!sw && ow) {
		docWt = ow
    }
    else if (sw && !ow) {
		docWt = sw;
    }
  }
  return docWt;
}

function ResizeParentFrame(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
  
    //iframeEl.style.height = "30px";
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
	var docHt 
    var docWt 
	if (iframeEl.contentDocument != null) {
		docHt = getDocHeight(iframeEl.contentDocument);
		docWt = getDocWidth(iframeEl.contentDocument);
	}
	else if (iframeEl.Document) {
		docHt = getDocHeight(iframeEl.Document);
		docWt = getDocWidth(iframeEl.Document);
	}    
    
    
    
    
    if (docHt) iframeEl.style.height = docHt + "px";
    if (docWt) iframeEl.style.width = docWt + "px";
   //alert("height" + docHt + "px");
  }
}

function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}

function ExecParentWndScript(sScript, close) {
if (! parentWnd)
  return;

if (parentWnd.closed)
  return;
 parentWnd.execScript (sScript);
 if (close)
  setTimeout ("window.close", 2000);

}

function CallArticle(ArticleID) 
{
	location.href = ("default.aspx?ArticleID=" + ArticleID)
}

function CallURL(strURL) 
{
	location.href = (strURL)
}
function isvalidemail(s) {

		if (s.length > 0) {
			if (s.indexOf("@")!=-1 && s.indexOf(".")!=-1)  {
				//email is valid
				return true;	
			}
			else {
				//email is not valid
				return false;
			}			
		}
		else {
			//if string is empty just return true as we assume 
			//the field is not mandatory.
			return true;
		}
		
}
function is_checked(sele) 
{
			var i;
			var num_of_selection;
			
			if 	(!sele)
				return false
				
			if (sele.length) {
				num_of_selection = sele.length;
				for (i = 0; i < num_of_selection; i++) 
					if  (sele[i].checked) 
						return true;
			}
			else
				return sele.checked
}


function isblank(s) {
		for( var i = 0; i < s.length; i++ ) {
			var c = s.charAt(i);
			if ( (c != ' ') && (c != '\n') && (c != '\t') )
				return false;
		}
		return true;
}
function ShowTopMenuDisclaimer(strHref, strDisclaimerText)
{
	if (confirm(strDisclaimerText))
	{
		location.href = strHref;
	}

}
var newWnd = null;
function menuOpenWnd(sURL, sWnd, sFeature)
{
	if (newWnd != null)
		newWnd.close();
	newWnd = window.open(sURL, sWnd, sFeature);
	newWnd.focus();
	return;
}

function menuChangeImage(imgElmName, imgVarName)
{
	if (!document.images[imgElmName])
	return;
	document.images[imgElmName].src = imgVarName.src;
}

function menuChangeBGImage(elmName, imgVarName)
{

	if (!document.getElementById(elmName))
	return;
	if (!document.getElementById(elmName).style)
	return;

	document.getElementById(elmName).style.backgroundImage = 'url(' + imgVarName.src + ')';
}

//Added by Tracey on 19th May 2005.
function stringreplace(checkMe,toberep,repwith)
{

	var temp = checkMe;
	var i = temp.indexOf(toberep);
	while(i > -1)
	{
		temp = temp.replace(toberep, repwith);
		i = temp.indexOf(toberep, i + repwith.length + 1);
	}
	return temp;
}		
//**************************************************************************************************
// Session Timer Functions
//**************************************************************************************************
//You need to paste the following code in the body tag of the page you have included this file in
//onLoad="formTimer()"
//You also need to ensure that the following pages are in the same directory as the page you are adding the timer function to
//AbandonSession.asp and SessionExtension.asp
var sessionwindow=null;
var Stimer=null;
var oNewDoc;
var sessionState = "";
var TimeInMSec = 0;
var sessionTimeout = 0;
var TimeTillWarning;
var SessionSaveContent=0; // SessionSaveContent=1 currently in article edit mode. Note, this is different to
						  // radEditMode=true which means RAD editor is in edit mode.
						  // SessionSaveContent=0 not in edit mode.

function formTimer(TimeoutInMin, bSaveContent, strTimeoutURL){

	SessionSaveContent=bSaveContent;

	if (!Stimer) {
		//alert('timer on')
		window.clearTimeout(Stimer)
	}
	//alert('state==' + sessionState);
	if (sessionState =="") {
		//session has just begun, so set the state to 'Current'
		sessionState = "Current";
		sessionTimeout = TimeoutInMin;
		if (sessionTimeout>4) {
			//display popup window 4 minutes before session expires
			TimeTillWarning=sessionTimeout-4;
		}
		else {
			TimeTillWarning = sessionTimeout
		}
		
		TimeInMSec = 1000*60*TimeTillWarning
		Stimer= window.setTimeout("formTimer(" + sessionTimeout + "," + SessionSaveContent + ", '" + strTimeoutURL + "')", TimeInMSec) 
	}
	else if (sessionState=="Current") {
		
		//session is about to expire, so set the state to 'AboutToExpire'
		sessionState = "AboutToExpire"
		
		//display the popup window if not currently displayed
		if (!sessionwindow||sessionwindow.closed) {
			sessionwindow = window.open('','','toolbar=no,width=330,height=210,titlebar=no');
			if (sessionwindow != null) {

				if (!sessionwindow.opener) {
					sessionwindow.opener = window
				}

				oNewDoc = sessionwindow.document.open();
				//write out html now!!!
				formcontent = '<html>'
				
				formcontent += '<body background="' + strTimeoutURL + '/images/extend_bg_elcom.jpg">'
				formcontent += '<table><tr><td colspan=2><img src="' + strTimeoutURL + '/images/trans.gif" width=100 height=110></td></tr><tr><td><img src="' + strTimeoutURL + '/images/trans.gif" width=100 height=10></td><td valign=middle align=right>'
				formcontent += '<tr><td colspan=2><font color=white face=verdana size=1><b>There has been no activity for an extended period. Click here to extend the session or it will expire in 2 minutes.</b></font></td></tr><tr><td><img src="' + strTimeoutURL + '/images/trans.gif" width=100 height=10></td><td valign=middle align=right>'
				formcontent += '<a href="SessionExtension.aspx"><img src="' + strTimeoutURL + '/images/click_extend.gif" border=0 align=right></a>&nbsp;&nbsp;&nbsp;&nbsp;'
				formcontent += '</td></tr></table>'
				formcontent += '</body>'
				formcontent += '</html>'

				oNewDoc.write(formcontent);
				oNewDoc.close();
			}
		}
		else {
			//popup window already displayed, so just show it
			sessionwindow.focus();
		}	

		//give the user 2 more minutes before killing the session if they have not extended the session.
		TimeInMSec = 1000*60*2
		Stimer= window.setTimeout("formTimer(" + sessionTimeout + "," + SessionSaveContent + ", '" + strTimeoutURL + "')", TimeInMSec) 
	}
	else if (sessionState=="AboutToExpire") {
		//session has expired
		
		sessionState = ""

		if (!sessionwindow||sessionwindow.closed) {
			//popup window is closed, do nothing
		}
		else {
			//popup window still open, so close it
			sessionwindow.close();
		}
		//Save the article content if required (function will check if RAD editor
		//is in edit mode. If so, the content will be saved).
		if (SessionSaveContent==1) {
			saveNoCheckIn();
		}
		//kill the session 
		window.location.href = "SessionAbandon.aspx"
	}
}

function resetTimer(strTimeoutURL)
{
	//user has extended the session so reset the timer
	window.clearTimeout(Stimer)
	sessionState = "Current"
	TimeInMSec = 1000*60*TimeTillWarning
 	Stimer= window.setTimeout("formTimer(" + sessionTimeout + "," + SessionSaveContent + ", '" + strTimeoutURL + "')",TimeInMSec) 

}

function BodyOnLoadJS() {
	goSetHeightinCMDefault();
}

function goSetHeightinCMDefault() {
	if (parent == window) {
		return;
	}
	else {
		parent.setIframeHeightinCMDefault('AnotherWebsiteFrame');
		
	}
}
function setIframeHeightinCMDefault(iframeName) {
		//var iframeWin = window.frames[iframeName];
		//var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
		var iframeEl;// = document.getElementById(iframeName);
		var doc;



		if (document.getElementById(iframeName) != null) {
			iframeEl = document.getElementById(iframeName);
		}
		else if (parent.document.getElementById(iframeName) != null) {
			iframeEl = parent.document.getElementById(iframeName);
		}
		else {
		   var pWnd = parent.parent;
		   iframeEl = pWnd.document.getElementById(iframeName)
		   while (pWnd != top && iframeEl != null){
				iframeEl = pWnd.document.getElementById(iframeName)
				pWnd  = pWnd.parent;
			}
		}

	if (iframeEl) {
		if (iframeEl.contentDocument != null) {
			doc = iframeEl.contentDocument;
		}
		else if (iframeEl.Document) {
			doc = iframeEl.Document;
		}
		
		//if ( iframeEl && iframeWin ) {
		if (iframeEl) {

			var docHt = getDocHeight(doc);
			var docWt = getDocWidth(doc);

			if (docHt) iframeEl.height = docHt + "px";
			if (docWt) iframeEl.width = docWt + "px";

			}
	}
}


