var menuunrotmot;
var menuselected;
var photoindex;
var alertUrl;

function navObject(nme,url) {
	this.nme = nme;
	this.url = url;
}
		
function menuro(num) {
	clearTimeout(window.menuunrotmot);
	for (i=1; i<section.length; i++) {
		if(num==i) {
			//cb_getElementStyle("navbar"+i).backgroundColor = eval('color['+i+']');
			cb_getElementStyle("navbar"+i).backgroundColor = "black";
			cb_getElementStyle("subnavbar"+i).backgroundColor = "black";
			cb_getElementStyle("navbar"+i).color = "white";
			cb_getElementStyle("subnavbar"+i).visibility = "visible";
		} else {
			cb_getElementStyle("navbar"+i).backgroundColor = "white";
			cb_getElementStyle("navbar"+i).color = eval('color['+i+']');
			cb_getElementStyle("subnavbar"+i).visibility = "hidden";
		}
	}
	if (num==0 && menuselected>0)
		cb_getElementStyle("subnavbar"+menuselected).visibility = "visible";
}
	
function WriteSideMenu(strEOPath)
{
	document.write('<div id="mainsidenav"><UL class="mainsidenav">');
	
	for (i=1; i<section.length; i++) {
	    document.write('<UL class="mainsidenav"><li><a class="link'+i+'" onClick=checkUserAlert(); href="'+strEOPath+eval('section['+i+'].url')+'">'+eval('section['+i+'].nme')+'</a>');
		for (j=1; j<subsection[i].length; j++) {
			document.write('<li><a class="sublink'+i+'" onClick=checkUserAlert(); href="'+strEOPath+eval('subsection['+i+']['+j+'].url')+'">'+eval('subsection['+i+']['+j+'].nme')+'</a>');
		}
	    document.write('</LI></UL>');
	}
	document.write('</div>');
}

function menuunro(num) {
	window.menuunrotmot = setTimeout("menuro(0)",700);
	//cb_getElementStyle("navbar"+num).backgroundColor = "white";
	//cb_getElementStyle("navbar"+num).color = eval('color['+num+']');
	//cb_getElementStyle("subnavbar"+num).visibility = "hidden";
}
	
function WriteTier1Menu(strEOPath)
{
	//write main menu bar table
	for (i=1; i<section.length; i++) {
		document.write('<td height="40" align="center">');
	    document.write('<a href="'+strEOPath+eval('section['+i+'].url')+'" style="text-decoration: none;"><div id="navbar'+i+'" onmouseover="menuro('+i+');" onmouseout="menuunro('+i+');"><span class="navbartext">'+eval('section['+i+'].nme')+'</span></div></a></td>');
		if (i<section.length-1)
			document.write('<td height="40" width="2"></td><td bgcolor="black" width="1"></td><td width="2"></td>');
	}
}
function WriteTier2Menu()
{
	//write main menu bar table
	for (i=1; i<section.length; i++) {
		document.write('<td height="10">');
	    document.write('<div id="navbar0" onmouseover="menuro('+i+');" onmouseout="menuunro('+i+');"></div></td>');
		document.write('<td width="5" colspan="3"></td>');
	}
}
 
function WriteTier1SubMenuLayers(strEOPath, selected)
{
	var menuindex;
	//write submenu bar layers
	for (i=1; i<section.length; i++) {
		txt = ('<div id="subnavbar'+i+'" onmouseover="menuro('+i+');" onmouseout="menuunro('+i+');">');
		txt += ('<table cellpadding="0" cellspacing="0" border="0" class="subnavbartext">');
			for (j=1; j<subsection[i].length; j++) {
				nme = eval('subsection['+i+']['+j+'].nme');
				url = eval('subsection['+i+']['+j+'].url');
				txt += ('<td><a href="'+strEOPath+url+'" class="subnavbartext">'+nme+'</a></td>');
				if(j<(subsection[i].length-1)) {
					txt += ('<td>&bull;</td>');
				}
		}
		txt += ('</table>');
		txt += ('</div>');
		document.write(txt);
	}
	menuindex = GetMenuIndex(selected);
	//alert('selected='+selected+', menuindex='+menuindex);

	if (menuindex!=0)
	{
		menuselected = menuindex;
		cb_getElementStyle("subnavbar"+menuselected).visibility = "visible";
	}
}
 
function GetMenuIndex(menu)
{
	var index=0;
	for (i=1; i<section.length; i++) {
		if (eval('section['+i+'].nme')==menu)
			index=i;
	}
	//alert("index="+index);
	return index;
}

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
    var result = false;
    if (obj.getAttributeNode("class") != null) {
        result = obj.getAttributeNode("class").value;
    }
    return result;
}   

function stripe(id) {

// the flag we'll use to keep track of 
// whether the current row is odd or even
var even = false;

// if arguments are provided to specify the colours
// of the even & odd rows, then use the them;
// otherwise use the following defaults:
var evenColor = arguments[1] ? arguments[1] : "#fff";
var oddColor = arguments[2] ? arguments[2] : "#eee";

// obtain a reference to the desired table
// if no such table exists, abort
var table = document.getElementById(id);
if (! table) { return; }

// by definition, tables can have more than one tbody
// element, so we'll have to get the list of child
// &lt;tbody&gt;s 
var tbodies = table.getElementsByTagName("tbody");

// and iterate through them...
for (var h = 0; h < tbodies.length; h++) {

    // find all the &lt;tr&gt; elements... 
    var trs = tbodies[h].getElementsByTagName("tr");
    
    // ... and iterate through them
    for (var i = 0; i < trs.length; i++) {

    // avoid rows that have a class attribute
    // or backgroundColor style
    if ((! hasClass(trs[i]) || hasClass(trs[i]) == id) &&
        ! trs[i].style.backgroundColor) {
 		
        // get all the cells in this row...
        var tds = trs[i].getElementsByTagName("td");
    
        // and iterate through them...
        for (var j = 0; j < tds.length; j++) {
    
        var mytd = tds[j];

        // avoid cells that have a class attribute
        // or backgroundColor style
        if ((! hasClass(mytd) || hasClass(mytd) == id) &&
            ! mytd.style.backgroundColor) {
    
            mytd.style.backgroundColor =
            even ? evenColor : oddColor;
        
        }
        }
    }
    // flip from odd to even, or vice-versa
    even =  ! even;
    }
}
}

function stripeli(id) {

// the flag we'll use to keep track of 
// whether the current row is odd or even
var even = false;

// if arguments are provided to specify the colours
// of the even & odd rows, then use the them;
// otherwise use the following defaults:
var evenColor = arguments[1] ? arguments[1] : "#fff";
var oddColor = arguments[2] ? arguments[2] : "#eee";

// obtain a reference to the desired list
// if no such list exists, abort
var list = document.getElementById(id);
if (! list) { return; }

    // find all the &lt;li&gt; elements... 
    var lis = list.getElementsByTagName("li");
    
    // ... and iterate through them
    for (var i = 0; i < lis.length; i++) {

    // avoid rows that have a class attribute
    // or backgroundColor style
    if (! hasClass(lis[i]) &&
        ! lis[i].style.backgroundColor) {
 		
        var myli = lis[i];

        // avoid lis that have a class attribute
        // or backgroundColor style
        if (! hasClass(myli) &&
            ! myli.style.backgroundColor) {
    
            myli.style.backgroundColor =
            even ? evenColor : oddColor;
        
        }
        }
    // flip from odd to even, or vice-versa
    even =  ! even;
    }
}
if ( navigator.appName == "Netscape" && document.layers == null )
{
	var isNav6 = true;
}
else
{
	var isNav6 = false;
}

function drawer(wri, lyrnme)
{
	
	if ( document.layers != null )
	{
		var lyr = eval("document.layers." + lyrnme + ".document");
		lyr.open();
		lyr.write(wri);
		lyr.close(); 
	}
	else
	{		
		if ( isNav6 )
		{
			cb_getElement(lyrnme).innerHTML = wri;
		}
		else
		{
			eval('document.all.' + lyrnme + '.style.visibility = "hidden"');
			obj = eval('document.all.' + lyrnme + '.innerHTML = wri');
			eval('document.all.' + lyrnme + '.style.visibility = "visible"');
		}
	}
}
function AddToFavorites(sTitle, sResource, sType)
{
	// alert('sTitle='+sTitle+', sResource='+sResource+',sType='+sType);
	// document.forms[0].addtofavorites.value = sType + '|' + sTitle + '|' + sResource;
	document.forms[0].addtomyfavorites.value = sType + '#' + sResource + '#' + sTitle;
	document.forms[0].submit();
}

function Search()
{
	document.forms[0].submit();
}

function SearchLocal()
{
	document.forms[0].submit();
}

function hideShowElement(element)
{ 	
	if (element.style.display=="none") 
	{
		element.style.display=""; 
	}
	else 
	{
		element.style.display="none"; 
	}						
	return;
}

function getMonthName(month) {
	var monthnames = new Array("","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
	
	return monthnames[month];
}

function showCalendarPicker(id, month, year, monthselected, dayselected, yearselected)
{
	var dateNow = new Date();
	var thismonth = dateNow.getMonth()+1;
	var today = dateNow.getDate();

	var firstDayOfWeek = new Date(year, month-1, 1).getDay()+1;
	var daysInMonth = 32 - new Date(year, month-1, 32).getDate();
	var dayOfMonth;
	
	//alert("month="+month+", year="+year+", monthselected="+monthselected+", yearselected="+yearselected+", firstDayOfWeek="+firstDayOfWeek+", today="+today+", daysInMonth="+daysInMonth);
	
	txt =  "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='1' CELLPADDING='0' class='text' ID='Table7'>";
    dayOfMonth = 1;
 	for (week=1; week<7; week++) {
        txt = txt + "<TR valign='top'>";
 		for (dayOfWeek=1; dayOfWeek<8; dayOfWeek++) {
            if (week == 1 && dayOfWeek < firstDayOfWeek){
               txt = txt + "<TD class='other' height='15'>&nbsp;</TD>";
            }
            else if (dayOfMonth <= daysInMonth) {
				if (dayOfMonth==dayselected && month==monthselected && year==yearselected) 
					cls = 'bl';
				else if (dayOfMonth==today && month==thismonth) 
					cls = 'dkbl';
				else 
					cls = 'wht';
					
				txt = txt + "<TD width='14.28%' ALIGN='CENTER' class='" + cls +"' height='15'>&nbsp;";
				txt = txt + "<A HREF='#' class='blue' onClick=setDate('" + month + "','" + dayOfMonth + "','" + year + "');>" + dayOfMonth + "</A></TD>";
				dayOfMonth = dayOfMonth + 1;
            }
            else {
				txt = txt + "<TD class='other' height='15'>&nbsp;</TD>";
            }
        }
        txt = txt + "</TR>";
    }
    txt = txt + "</Table>";
				//alert("txt="+txt);
	drawer(txt, id);
} 	

function getTextBoxCharsLeft(field, countFieldId, maxlimit) {
	charsleft = maxlimit-field.value.length;
	drawer("<EM>"+charsleft+"</EM>", countFieldId);
	if (charsleft<0) 
		alert('Please limit your entry to '+maxlimit);
}

function getTextBoxCharsLeftKey(field, countFieldId, maxlimit, e) {
	charsleft = maxlimit-field.value.length;
	drawer("<EM>"+charsleft+"</EM>", countFieldId);
	
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
		
	if (charsleft<0 && keynum != 8 && keynum != 46) 
		alert('Please limit your entry to '+maxlimit);
}


function deletePhoto(id) {
	if (confirmDelete('Photo'))
	{
		document.forms[0].DeleteImageId.value = id;
		document.forms[0].submit();
	}
}

function showPhotoList() {
    //photoView = 1;
    if (window.photo.length>0)
    {
		txt = "<Table cellSpacing='0' cellPadding='4' width='98%' border='0'>";
		nAlt = 0;
		for (i=0; i<window.photo.length; i++) {
			if (nAlt==0)
			{
				txt += "<tr class='eogroupsalternatingstyle' align='Left'>";
				nAlt = 1;
			}
			else
			{
				txt += "<tr class='eogroupsitemstyle' align='Left'>";
				nAlt = 0;
			}
			txt += "<td width='10'><a href='javascript:showPhoto(" + i + ");'><img class='eogroupimage' src='" + photo[i].url + "' width='90' border='0' alt='Click for large image of " + photo[i].name + "'></a></td>";
			txt += "<td align='left' valign='top'><span>" + photo[i].descr + "</span></td>";
			if (photo[i].admin==1)
				txt += "<td align='right' valign='top'><span><a href='javascript:deletePhoto(" + photo[i].id + ");'>Delete</a></span></td>";
			else
				txt += "<td></td>"
			txt += "</tr>";
		}
		txt += "</Table>";
	}
	else {
		txt = "";
	}
	drawer(txt,"photos");
	drawer("", "prevnext");
}

function showPhotoTable() {
    //photoView = 2;
	numCols = 5;
	nCol = 1;
	numRows = Math.ceil(window.photo.length/numCols);
	//alert('Rows='+ numRows);

	txt = "<Table width='98%' cellPadding='5'>";
	for (i=0; i<numRows; i++) {
		txt += "<tr>";
		for (j=0; j<numCols; j++) {
			nIndex = i+(j*numRows);
			if (nIndex<window.photo.length) {
				txt += "<td valign='top'><a href='javascript:showPhoto(" + nIndex + ");'><img class='eogroupimage' src='" + photo[nIndex].url + "' width='90' border='0' alt='Click for large image of " + photo[nIndex].name + "'></a></td>";
			}
			else
				txt += "<td></td>";
		}
		txt += "</tr>";
	}
	txt += "</Table>";
	drawer(txt,"photos");
	drawer("", "prevnext");
}

function showPhoto(index) {
	txt = "<Table width='90%' cellPadding='2'>";
	txt += "<tr><td><img class='eogroupimage' src='" + photo[index].url + "' width='580px' alt='" + photo[index].name + "'></td></tr>";
	drawer(txt,"photos");
	
	nextIndex = index+1;
	if (nextIndex>window.photo.length-1)
		nextIndex = 0;
	prevIndex = index-1;
	if (prevIndex<0)
		prevIndex = window.photo.length-1;
		
	txt = "<a href='javascript:showPhoto(" + prevIndex + ");'>Prev</a>&nbsp;|&nbsp;<a href='javascript:showPhoto(" + nextIndex + ");'>Next</a>";
	drawer(txt, "prevnext");
}

// Multi file upload functions
//Init "flash" object
function InitFlashObj()
{
	if(document.embeds && document.embeds.length>=1)
		Flash = document.getElementById("EmbedFlashFilesUpload");
	else
		Flash = document.getElementById("FlashFilesUpload");
}

/*
	MultiPowUpload_onMovieLoad. Invoked when the movie loaded and if browsers supports ExternalInterface i.e 
	programming interface that enables straightforward communication between Flash Movie and JavaScript.
*/
function MultiPowUpload_onMovieLoad()
{
	InitFlashObj(); //sometimes Flash not initialized while window.onload event
	Flash.width = 1;
	Flash.height = 1;
	
	var JSUpload = document.getElementById("JSUpload");
	JSUpload.style.visibility = "visible";
	JSUpload.style.width = 350;
	JSUpload.style.height = 300;	
}

/*
	flash.browseFiles(typelist) displays a file-browsing dialog box in which the user can select a local file to upload. 
	The dialog box is native to the user's operating system.
	typelist - An array of file types used to filter the files displayed in the dialog box.
*/
function browsefiles()
{
	var allTypes = new Array();
	var imageTypes = new Object();
	imageTypes.description = "All files";
	imageTypes.extension = "*.*";
	allTypes.push(imageTypes);	
	imageTypes = new Object();
	imageTypes.description = "Images (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;)";
	imageTypes.extension = "*.jpg; *.jpeg; *.jpe; *.gif; *.png;";
	allTypes.push(imageTypes);	
	Flash.browseFiles(allTypes);
}

//Clear list
function clearListBox(lstBox)
{		
	while (lstBox.length > 0)
	{
		lstBox.remove(0);
	}
}

//Update HTML progress bar state
function updateProgressBar(percentDone)
{
	var rowProgress = document.getElementById("rowProgress");
	if(percentDone>=1)
		rowProgress.width = percentDone + "%";
	else
		rowProgress.width = "1";
}

/*
	flash.uploadAll(url) Starts the upload of a files in list to a remote server.
	url - The URL of the server script configured to handle upload through HTTP POST calls.
*/
function upload()
{
	updateProgressBar(0);
	uploadedfilescount = 0;
	document.getElementById("lable").innerHTML = "";
	Flash.uploadAll("/Resources/uploadfiles.aspx");
}

//MultiPowUpload_onSelect. Invoked when the user selects a file to upload or download from the file-browsing dialog box.
function MultiPowUpload_onSelect()
{
	var list = Flash.fileList();
	var fileslist = document.getElementById("fileslist");
	var i = 0;
	clearListBox(fileslist);
	for(i=0; i<list.length; i++)
	{
		fileslist.options[fileslist.options.length] = new Option(list[i].name + 
		" (" + list[i].size + " bytes)", i, false, false );
	}
}

//MultiPowUpload_onProgress. Invoked periodically during the file upload or download operation
function MultiPowUpload_onProgress(type, index, fileBytesLoaded, fileBytesLength, totalBytesLoaded, totalBytesLength) 
{
var PercentDone = new Number((totalBytesLoaded/totalBytesLength)*100).toFixed(1);
document.getElementById("lable").innerHTML = PercentDone + "% uploaded" + " (" + uploadedfilescount + " files)";
updateProgressBar(PercentDone);
}

//MultiPowUpload_onError. Invoked when an input/output error occurs or when an upload/download fails because of an HTTP error
function MultiPowUpload_onError(type, index, error) 
{
window.alert(error);
}

//MultiPowUpload_onComplete. Invoked when the upload or download of single file operation has successfully completed
function MultiPowUpload_onComplete(type, index)
{
	uploadedfilescount++;
}

//MultiPowUpload_onCompleteAbsolute. Invoked when the upload or download of all files operation has successfully completed
function MultiPowUpload_onCompleteAbsolute(type, totalBytesLoaded)
{
document.getElementById("lable").innerHTML = "Upload complete! Total bytes " + totalBytesLoaded + " (" + uploadedfilescount + " files)";
	var fileslist = document.getElementById("fileslist");
	clearListBox(fileslist);
}

function writeDiv(text,id){
	alert(text+':'+id);
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
}

function chapterPage(page, cid) {
	window.location=page + "?cid=" + cid;
}

function cb_getElement(elemID)
{
	if ( document.layers != null )
	{
		return eval('document.layers["' + elemID + '"]');
	}
	else
	{
		if ( document.all != null )
		{
			return eval('document.all.' + elemID + '');
		}
		else
		{
			return document.getElementById(elemID);
		}
	}
}

function cb_getElementStyle(elemID)
{
	if ( document.layers != null )
	{
		return eval('document.layers["' + elemID + '"]');
	}
	else
	{
		if ( document.all != null )
		{
			return eval('document.all.' + elemID + '.style');
		}
		else
		{
			return document.getElementById(elemID).style;
		}
	}
}

function cb_getImage(elemID, imgID)
{
	if ( document.layers != null )
	{
		return eval('document.layers["' + elemID + '"].document.images["' + imgID + '"]');
	}
	else
	{
		return eval('document.images["' + imgID + '"]');
	}
}

function contentItem(url) {
	if (document.layers) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	var popW = 550, popH = 400;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	args = "width=550,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
	args = args + ", left=" + leftPos + ",top=" + topPos ;
	
	if (typeof(popupWin) != "object"){
		popupWin = window.open (url,"UpdateContent", args)
	} else {
		if (!popupWin.closed){ 
			popupWin.location.href = url;
		} else {
			popupWin = window.open(url, 'UpdateContent', args);
		}
	}
	popupWin.focus();
}

function EmailBody(url) {
	if (document.layers) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	var popW = 550, popH = 400;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	args = "width=550,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
	args = args + ", left=" + leftPos + ",top=" + topPos ;
	
	if (typeof(popupWin) != "object"){
		popupWin = window.open (url,"UpdateContent", args)
	} else {
		if (!popupWin.closed){ 
			popupWin.location.href = url;
		} else {
			popupWin = window.open(url, 'UpdateContent', args);
		}
	}
	popupWin.focus();
}

function openPopUp(popW, popH, url, name) {
	if (document.layers) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	args = "width="+popW+",height="+popH+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes";
	args = args + ", left=" + leftPos + ",top=" + topPos ;
	
	if (typeof(popupWin) != "object"){
		popupWin = window.open (url, name, args)
	} else {
		if (!popupWin.closed){ 
			popupWin.location.href = url;
		} else {
			popupWin = window.open(url, name, args);
		}
	}
	popupWin.focus();
}

function checkUserAlert()
{
	if (alertUrl != null)
		openPopUp(500, 400, alertUrl, 'ProfileUpdate');
}

function setUserAlert(url)
{
	alertUrl = url;
}