/////////////////////////////////////////////////////////////////////
// Page Name	: script.js
//
// Description	: Collection of all commonly used JavaScript functions.
//
// Author	: Tony Goode
//
// Creation Date: 19-Apr-2001
//
// Revision History
// Who	When			What
/* ------------------------------------------------------------------
   JLM	24/4/2001	added function AddItem(oselect,String type)
							and AddOrgUnit,AddDocument
   RoyB	20/7/2005	added function AddOrgRole
   RoyB	19/08/2005	added function Addpeopletype
   SA		24/08/2005	added function AddURL
   RoyB	9/12/2005	added function AddEmailSignature
   SA	5/4/2006	added try/catch in DisplayValues (firebox/IE issue)
   SA	5/4/2006	RemoveItem function - see in function note (firebox/IE issue)
   DanB 12/09/2006  Added Container Resize Issue in FF
   SA	21/11/2006	Cross browser changes to resize function.
// ------------------------------------------------------------------
*/
/*  Notes:
 	
*/
//////////////////////////////////////////////////////////////////////

function ListGetAt(inwhat,which,sep) {
	var n = 0,wstr = 0,i = 0,s = 0,f = 0;
	for (i=1 ; i < which ; i++) {
		n = inwhat.indexOf(sep,n)
		if (n < 0) return ''
		n++
	}
	if (n >= 0) {
		var s=n;
		var f = inwhat.indexOf(sep,n)
		if (f < 0) f = inwhat.length
		wstr = inwhat.substring(n,f)
	}
	return wstr
}

function StringTrim(ts) {
	if (ts) {
		ts=""+ts
		for (;ts.charAt(ts.length-1)==' ';) 
			ts=ts.substring(0,ts.length-1);
		for (;ts.charAt(0)==' ';)
			ts=ts.substring(1,ts.length);
	}
	return ts;
}

function TextFormat(t) {
	return t;
}

function RemoveItem(oselect) {
	if (oselect.selectedIndex == -1) {
		alert('Please select at least one item from the list.');
		return false;
	}
	for (i=0; i<oselect.options.length; i++) {
	    if (oselect.options[i].selected == true) {
		    oselect.remove(i); // changed from oselect.options.remove(i). Would not work in firefox
		    i--;
	    }
	}
}

function oldAddItem(oselect,type) {
	var Args = "";
	g_return_value = showModalDialog(
			    g_webserver+"/"+g_root+"/popup/"+type+".cfm", 
			    Args, 
    "dialogWidth:250px;dialogHeight:380px;dialogLeft:100px;dialogTop:100px;");
	if (g_return_value) {
		var SepCount = 0;
		for (i=0; i<g_return_value.length; i++) {
			if (g_return_value.charAt(i) == "|")
				SepCount++;
		}
		for (i=1; i<=SepCount; i++) {
			SelEmp = ListGetAt(g_return_value,i,",");
			var oOption = document.createElement("OPTION");
			oOption.value=ListGetAt(SelEmp,1,"|");
			oOption.text=ListGetAt(SelEmp,2,"|");
			var OptExists = false;
			for (j=0; j<oselect.length; j++) {
				if (oselect.options[j].value == oOption.value)
					OptExists = true;
			}
			if (!OptExists)
				oselect.add(oOption);
		}
	}
}

var g_return_value;
var field;
var cat;
var hidden;
var g_type;
function AddItem(lfield,lcat,lhidden, lwidth, lheight, sbars, urloptions) {
// The following global variables are set from locally passed values, as they
// will be used in the DisplayValues return function.
    field = lfield;
    cat = lcat;
    hidden = lhidden;
    g_type = field.type;

    if (lwidth)
	width = lwidth
    else
	width = "250"

    if (lheight)
	height = lheight
    else
	height = "380"
    if (sbars)
	stext=",scrollbars=yes"
    else
	stext=""
    if (urloptions)
	utext="&urloptions="+urloptions
    else
	utext=""
	
    if (!g_return_value || g_return_value.closed)
	g_return_value=window.open(g_webserver+"/"+g_root+"/popup/"+cat+".cfm?type="+g_type+utext, cat,"HEIGHT="+height+"px,WIDTH="+width+"px,left=100,top=100"+stext);
	else
	g_return_value.focus();
}
function AddData(lfield, lcat, lwidth, lheight, sbars, urloptions) {
// The following global variables are set from locally passed values, as they
// will be used in the DisplayValues return function.
    field = lfield;
    value = field.value;
    cat = lcat;

    if (lwidth)
	width = lwidth
    else
	width = "650"

    if (lheight)
	height = lheight
    else
	height = "550"
    if (sbars)
	stext=",scrollbars=yes"
    else
	stext=""
    if (urloptions)
	utext="&urloptions="+urloptions
    else
	utext=""
	
    if (!g_return_value || g_return_value.closed)
	g_return_value=window.open(g_webserver+"/"+g_root+"/popup/"+cat+".cfm?Data="+value+utext, cat,"HEIGHT="+height+"px,WIDTH="+width+"px,left=100,top=100"+stext);
	else
	g_return_value.focus();
}
function DisplayData(ReturnValue)
{
  if (ReturnValue) {
    field.value = ReturnValue;
  }
  if (div != null){
    div.innerHTML=ReturnValue;
  }
}
function DisplayValues(ReturnValue)
{

    var l_type = g_type;
	
    if (ReturnValue) {
		if (l_type == 'text') {		
			 id = ListGetAt(ReturnValue,1,"|");
			 text = ListGetAt(ReturnValue,2,"|");
			 field.value = text;
			 hidden.value = id;
		}
	else {
	    var SepCount = 0;
	    for (i=0; i<ReturnValue.length; i++) {
			if (ReturnValue.charAt(i) == "|")
				 SepCount++;
			 }
	    for (i=1; i<=SepCount; i++) {
		SelEmp = ListGetAt(ReturnValue,i,",");
		var oOption = document.createElement("OPTION");
		oOption.value=ListGetAt(SelEmp,1,"|");
		oOption.text=ListGetAt(SelEmp,2,"|");
		var OptExists = false;
		for (j=0; j<field.length; j++) {			
		    if (field.options[j].value == oOption.value)
			OptExists = true;
		}
		if (!OptExists){
			try {
		    	field.add(oOption,null); //DOM2 method (firefox)
				}
			catch (e) {
				field.add(oOption); //DOM1 method (IE)
				}
			}
	    }
	}
    }
}


function AddMenu(field,hidden) {
	AddItem (field,"menus",hidden, 340, 320);
}

function AddContent(field,hidden) {
	AddItem (field,"content",hidden, 330, 450);
}

function AddForm(field,hidden) {
	AddItem (field,"form",hidden, 300, 300);
}

function AddContentFile(field) {
	AddItem (field,"contentfile",hidden, 600, 240);
}

function AddContentCategory(field) {
	AddItem (field,"contentcategory",hidden, 550, 240);
}

function AddContentCategoryAll(field) {
	AddItem (field,"contentcategoryall",hidden, 550, 240);
}

function AddOrgUnit(field,hidden,optionlist) {
	AddItem (field,"orgunit",hidden, 310, 400, true,optionlist);
}

function AddOrgUnitAll(field,hidden,optionlist) {
	AddItem (field,"orgunitall",hidden, 310, 400, true,optionlist);
}

function AddPeopleType(field,hidden) {
	AddItem (field,"peopletype",hidden);
}

function AddEmployee(field,hidden) {
	AddItem (field,"employee",hidden);
}

function AddEmployeeAll(field,hidden) {
	AddItem (field,"employeeall",hidden,340,450);
}

function AddForumCategory(field,hidden) {
	AddItem (field,"ForumCategory",hidden, 240, 240);
}

function AddSite(field,hidden) {
	AddItem (field,"site",hidden);
}
function PickLocation(field,hidden) {
	AddItem (field,"location",hidden,500,400,true);
}

function AddOrgRole(field,hidden,optionlist) {
	AddItem (field, "orgrole", hidden, 310, 400, true, optionlist);
}

function AddURL(field,hidden,optionlist) {
	AddItem (field, "url", hidden, 350, 275, true, optionlist);
}

function AddEmailSignature(field,ldiv) {
	div = ldiv;
	AddData (field, "data", 650, 550, true);
}

function makeItHot(f){
	// "f" is a multiple select box
	for (i=0; i<f.length; i++){
		if (f.options[i].selected){
			if (f.options[i].value.charAt(0) != "*"){
				var old=f.options[i].value
				f.options[i].value="*"+old;
				f.options[i].style.color="red";
			}
		}
	}
	for (i=0; i<f.length; i++){
		f.options[i].selected=false;
	}
}

function makeItNot(f){
	// "f" is a multiple select box
	for (i=0; i<f.length; i++){
		if (f.options[i].selected){
			if (f.options[i].value.charAt(0) == "*"){
				var old=f.options[i].value
				var newVal=old.substring(1,old.length)
				f.options[i].value=newVal;
				f.options[i].style.color="black";
			}
		}
	}
	for (i=0; i<f.length; i++){
		f.options[i].selected=false;
	}
}

function flipHotness (f, i){
	// f is the select box
	// i is selectBox.selectedIndex
	var val = f.options[i].value.charAt(0);

	if (val == "*"){
		// Make it not
		makeItNot(f);
	}else{
		// Make it hot
		makeItHot(f);
	}
}

function initHotItems(f){
	// "f" is a multiple select box
	for (i=0; i<f.length; i++){
		if (f.options[i].value.charAt(0) == "*"){
			f.options[i].style.color="red";
		}
	}
	for (i=0; i<f.length; i++){
		f.options[i].selected=false;
	}
}

function g_trim(ts) {
	if (ts) {
		ts=""+ts
		for (;ts.charAt(ts.length-1)==' ';) 
			ts=ts.substring(0,ts.length-1);
		for (;ts.charAt(0)==' ';)
			ts=ts.substring(1,ts.length);
	}
	return ts;
}

function CheckDate(fd_datefield,date_format,no_change)
{
	if (!date_format) date_format="DD-MMM-YYYY"
	date_format=date_format.toUpperCase()
	var datefield = g_trim(fd_datefield.value)
	if (g_trim(datefield)=='')
	{
		return true;
	}
	//convert slashes to dashes
	while (datefield.indexOf("/") != -1) 
	{  // The end of the next line is not commented out. Only the editor thinks so!
		var tmp = datefield.substring(0,datefield.indexOf("/"))+"-"+datefield.substring(datefield.indexOf("/")+1,datefield.length)
		datefield = tmp
	}
	// convert spaces to dashes
	while (datefield.indexOf(" ") != -1) 
	{  // The end of the next line is not commented out. Only the editor thinks so!
		var tmp = datefield.substring(0,datefield.indexOf(" "))+"-"+datefield.substring(datefield.indexOf(" ")+1,datefield.length)
		datefield = tmp
	}
	// remove commas
	while (datefield.indexOf(",") != -1) 
	{  // The end of the next line is not commented out. Only the editor thinks so!
		var tmp = datefield.substring(0,datefield.indexOf(","))+datefield.substring(datefield.indexOf(",")+1,datefield.length)
		datefield = tmp
	}
	var delim1 = datefield.indexOf("-");   		// find the first dash
	var delim2 = datefield.lastIndexOf("-");	// find the last dash
	if (delim1 != -1 && delim1 == delim2) {		// if both can't be found then give up
		alert ("Date format not valid.");
		fd_datefield.focus();
		fd_datefield.select();
		return false;
	}
	if (delim1 != -1) {	 // There are delimiters
		var dd=parseInt(datefield.substring(0,delim1),10);	// extract the day part
		var mmstring=datefield.substring(delim1+1,delim2);	// extract the month part
		if (isNaN(parseInt(mmstring,10)))
		{ //if the month isn't a number, check for a month name.
			var mm=g_getmnthnum(mmstring);	// convert the monthname to a number
			if (isNaN(parseInt(mm,10))) { 	// if it couldn't be converted, then exit.
				alert("Date format not valid. Month name not found");
				fd_datefield.focus();
				fd_datefield.select();
				return false;
			}
		} else 
		{ 
			var mm=parseInt(mmstring,10);  // the month is a number, so keep it
		} 
		var yyyy=parseInt(datefield.substring(delim2+1,datefield.length),10); // get the year part
	}
	else { // there are no delimitors. Assume a date format of DDMMYY[YY] or MMDDYY[YY]
		var dd=parseInt(datefield.substring(0,2),10);	// get the day part
		var mm=parseInt(datefield.substring(2,4),10);	// get the month part
		var yyyy=parseInt(datefield.substring(4,datefield.length),10);	// get the year part
	}
	yyyy=parseInt(g_trim((yyyy+"    ").substring(0,4)),10);
	if (isNaN(dd) || isNaN(mm) || isNaN(yyyy)) {	// All the fields need to be numeric
		alert('Date format not valid - Non numeric entries',null,"WARN");
		fd_datefield.focus();
		fd_datefield.select();
		return false;
		}
	if (mm<1 || mm >12) {	// month as to be 1 to 12 
		alert("Date format not valid. Invalid month");
		fd_datefield.focus();
		fd_datefield.select();
		return false;
	}
	if (yyyy < 100)		// If the year had only 2 digits
		if (yyyy > 30) yyyy+=1900; else yyyy+=2000;		// add to it. Pivot year is 1930
	if (yyyy < 1753) {	// SQL doesn't like less than 1753
		alert("Date not valid. Invalid Year");
		fd_datefield.focus();
		fd_datefield.select();
		return false;
	}

	if (!g_chkdaysmnth(dd,mm,yyyy)) {	// Check the number of days is valid for this month.
		alert("Date format not valid. Invalid days");
		fd_datefield.focus();
		fd_datefield.select();
		return false;
	}
	if (date_format=="MMM-DD-YYYY")
	{
		fd_datefield.value=""+g_getshtmnth(mm)+" "+dd+", "+yyyy;	// Format the return date as MMM-DD-YYYY
	} else
	{
		fd_datefield.value=""+dd+"-"+g_getshtmnth(mm)+"-"+yyyy;	// Format the return date as DD-MMM-YYYY
	}
	return true;
}

	// Returns the month number of the supplied month name. eg g_getmnthnum(January) returns 1
	function g_getmnthnum(monthname)
	{
		var sm=monthname.substring(0,3).toLowerCase()
		for (x=1;x<=12;x++)	{
				if (sm==g_getshtmnth(x).toLowerCase())
				return x;
			}
		return false;
	}

	function g_getshtmnth(monthno)
	{
		var m=new Array(12);
		m[1]="Jan";		m[2]="Feb";		m[3]="Mar";		m[4]="Apr";
		m[5]="May";		m[6]="Jun";		m[7]="Jul";		m[8]="Aug";
		m[9]="Sep";		m[10]="Oct";	m[11]="Nov";	m[12]="Dec";
		if (monthno>=1 && monthno <=12)	return m[monthno];
		else return false;
	}
		
	// Checks if the number of days is valid for the supplied month and year.	
	function g_chkdaysmnth(days,month,year)
	{
		if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
			if (days>=1 && days <=31) return true;
			else return false; 
		if (month==4 || month==6 || month==9 || month==11)
			 if (days>=1 && days <=30) return true;
			 else return false; 
		if (month==2) 
			if (days>=1 && days <=28) return true;
			else
				if (days==29) 
				 	if (parseInt(year/4,10)==(year/4)) return true;
					else return false;
				else return false;
	}
	
function CheckForHtml(o){
	if(o.value.search(/<\S/g)>=0) {
		alert("Please remove any HTML tags from the Free Text area.\nIf using the < or > symbols, please put a space character on either side.");
		return false;
	}
	else
		return true;
}

var l_contacts_window;
function contacts_list(oid,showsub)
{
	
	if (!l_contacts_window || l_contacts_window.closed)
		l_contacts_window=window.open(g_webserver+"/"+g_root+"/popup/contactslist.cfm?orgunitid="+oid+"&showsub="+showsub, "ContactsList","HEIGHT=500px,WIDTH=700px,left=50,top=50,scrollbars=yes,menubar=yes");		
	else
		l_contacts_window.focus();
}

function ResizeContainerHeightsFF () {
	var obj;
	obj = document.getElementById('MainCenter');
	obj.style.height = 'auto';
	var x = obj.offsetHeight;
	var obj_a;
	obj_a = document.getElementById('left');
	obj_a.style.height = 'auto';
	var x_a = obj_a.offsetHeight;
	var obj_b;
	obj_b = document.getElementById('center');
	obj_b.style.height = 'auto';
	var x_b = obj_b.offsetHeight;
	
	if (x > 600) {
	obj.style.height = x + "px";
	
	obj2 = document.getElementById('left');
	obj2.style.height = x + "px";
	obj2 = document.getElementById('center');
	obj2.style.height = x + "px";
	
	obj5 = document.getElementById('container');
	var x1 = obj5.style.marginTop;
	
	obj2 = document.getElementById('footer');
	obj2.style.top = (x + 145) + "px";
	}
	else if (x_a < 600) {
	obj.style.height = x + "px";
	
	obj2 = document.getElementById('left');
	obj2.style.height = 600 + "px";
	obj2 = document.getElementById('center');
	obj2.style.height = 600 + "px";
	
	obj2 = document.getElementById('footer');
	obj2.style.top = (600 + 150) + "px";
	
	}
	else if (x_a > 600) {
	
	var obj_a;
	obj_a = document.getElementById('left');
	obj_a.style.height = 'auto';
	var x_a = obj_a.offsetHeight;
	
	obj.style.height = x_a + "px";
	
	obj2 = document.getElementById('left');
	obj2.style.height = x_a + "px";
	obj2 = document.getElementById('center');
	obj2.style.height = x_a + "px";
	
	obj2 = document.getElementById('footer');
	obj2.style.top = (x_a + 150) + "px";
	
	}
	else {
	obj.style.height = x + "px";
	
	obj2 = document.getElementById('left');
	obj2.style.height = x+200 + "px";
	obj2 = document.getElementById('center');
	obj2.style.height = x+200 + "px";
	
	obj2 = document.getElementById('footer');
	obj2.style.top = (x + 345) + "px";
	
	}
}

function RemoveLeftColumnPadding () {
	var obj;
	obj = document.getElementById('leftcolumn');
	obj.style.paddingLeft = 0 + "px";
	obj.style.paddingRight = 0 + "px";
}

function RemoveLeftColumnPaddingHP () {
	var obj;
	obj = document.getElementById('left');
	obj.style.paddingLeft = 0 + "px";
	obj.style.paddingRight = 0 + "px";
}

function ResizeLeftColumnWidthFFMenu3 (s) {
	var obj;
	obj = document.getElementById('leftcolumn');
	obj.style.width = s;
	var x = obj.offsetWidth;
	obj.style.width = x + "px";
}

function ResizeLeftColumnWidthHPMenu3 (s) {
	var obj;
	obj = document.getElementById('left');
	obj.style.width = s;
	var x = obj.offsetWidth;
	obj.style.width = x-18 + "px";
}

function ResizeCenterWidth (IsSafari) {
	// browser detect
	var browserAgent = navigator.userAgent;
	if(browserAgent.indexOf('MSIE') != -1) { var browser = 'MSIE'; }
	else if (browserAgent.indexOf('Firefox') != -1) { var browser = 'Firefox'; }
	else if (browserAgent.indexOf('Safari') != -1) { var browser = 'Safari'; }
	else { var browser = 'MSIE'; }
	// resize columns depending on browser
	var objCenter = document.getElementById("center");
	var objLeft = document.getElementById("left");
	var objRight = document.getElementById("right");	
	
	switch(browser) {
		case 'MSIE':
			//var obj = document.getElementById('center');
			objCenter.style.width = 'auto';
			var x = objCenter.offsetWidth;
			objCenter.style.width = x-(242) + "px";		
			break;
		case 'Firefox':
			var objBodyWidth = document.body.clientWidth;
			var centerWidth = objBodyWidth - objLeft.clientWidth - objRight.clientWidth - 50 + 'px'
			objCenter.style.width = centerWidth;		
			break;
		case 'Safari':
			break;
		}
}

function EmbedDateTime (serverName,lineHeight,TwentyFourHourTime,DispTime) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab##version=7,0,19,0" width="170" height="'+lineHeight+'">');
	document.write('<param name="movie" value="'+serverName+'/modules/include/datetime.swf?showDispTime='+DispTime+'&show24HourFormat='+TwentyFourHourTime+'" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<embed src="'+serverName+'/modules/include/datetime.swf?showDispTime='+DispTime+'&show24HourFormat='+TwentyFourHourTime+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" width="170" height="'+lineHeight+'"></embed>');
	document.write('</object>');
	}