function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function prepCivPage() {
    //|| location.href.indexOf('guests.aspx') != -1
    if (location.href.indexOf('photographs.aspx') != -1) toggleSlideshow();
	MM_preloadImages('images/mission.gif','images/history.gif','images/selection.gif','images/fellows.gif','images/resources.gif','images/photos.gif','images/video.gif','images/home.gif','images/board_staff.gif','images/contact_us.gif');
}

function newWindow() {
	window.open('photographs.htm','newwindow','status,height=365,width=550');
}

function CloseWindow() {
	self.close();
}
			
function toggleSlideshow() {
	var nextImageID = MM_findObj('NextImageID').value;
	var slideshowRunning = getCookie('SlideshowRunning');
	if (slideshowRunning == null) {
		if (ssFirstRun == false) {
			alert("Cookies required to stop/start slideshow. Please enable cookies in your browser.");
		}
		slideshowRunning = "True";
	}

	if (slideshowRunning == "True") {
		if (ssFirstRun == true) {
			startSlideshow(nextImageID, (1000 * seconds));
		} else {
			stopSlideshow();
		}
	} else {
		if (ssFirstRun == true) {
			stopSlideshow();
		} else {
			startSlideshow(nextImageID);
		}
	}
}

function startSlideshow(nextImageID, pause) {
	var ssToggle = MM_findObj('SSToggle');
	var t = pause || 0;
	theTimer = setTimeout("nextPage(" + nextImageID + ")", t);
	ssToggle.src = "images/nav/stop_slideshow.gif";
	setCookie("SlideshowRunning", "True");
	ssFirstRun = false;
}

function stopSlideshow() {
	var ssToggle = MM_findObj('SSToggle');
	clearTimeout(theTimer);
	ssToggle.src = "images/nav/start_slideshow.gif";
	setCookie("SlideshowRunning", "False");
	ssFirstRun = false;
}

function nextPage(nextImageID) {
	// if we don't have nextImageID, we must not have main image, in which case we better stop SS
	if (nextImageID != null) location.href = location.pathname + "?p=" + nextImageID;
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


var popUpCalendar; 

function OpenCalendar(idname) {
	popUpCalendar = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value, 
		'popupcal', 
		'width=165,height=208,left=200,top=250');
}

function SetDate(formName, id, newDate) {
	eval('var theform = document.' + formName + ';');
	popUpCalendar.close();
	theform.elements[id].value = newDate;				
}

function storeCaret (field) {
	var theField = MM_findObj('Axezz1_Form_' + field);
  if (theField.createTextRange) 
    theField.caretPos = document.selection.createRange().duplicate();
}

function InsertFellowTag(field, selectField) {
	var theSelectField = MM_findObj(selectField);
	var theField = MM_findObj('Axezz1_Form_' + field);
	var text = '<fellow id="' + theSelectField.options[theSelectField.selectedIndex].value + '">';
	if (theField.createTextRange && theField.caretPos) {
		var caretPos = theField.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else {
		theField.value += text;
	}
}

function InsertMedia(field, mediaText) {
	var theField = MM_findObj('Axezz1_Form_' + field);
	if (theField.createTextRange && theField.caretPos) {
		var caretPos = theField.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? mediaText + ' ' : mediaText;
	} else {
		theField.value += mediaText;
	}
	return false;
}

function prepFellowInsert() {
	var fellow_id = MM_findObj('Axezz1_Form_fellow_id');
	var fellowSelect = MM_findObj('fellowSelect');
	if (fellow_id && fellowSelect) {
		for (var i=0; i<fellow_id.options.length; i++) {
			if (fellow_id.options[i].text != "") {
				var newOption = document.createElement("option");
				newOption.value = fellow_id.options[i].value;
				newOption.text = fellow_id.options[i].text;
				fellowSelect.options[fellowSelect.options.length] = newOption;
				if (newOption.value == fellow_id.options[fellow_id.selectedIndex].value) newOption.selected = true;
			}
		}
		
		document.NewsForm.Axezz1_Form_description.onclick = function() {
			storeCaret('description');
		}
		document.NewsForm.Axezz1_Form_description.onselect = function() {
			storeCaret('description');
		}
		document.NewsForm.Axezz1_Form_description.onkeyup = function() {
			storeCaret('description');
		}
	}
}

var popUpFileUpload;

function OpenFileUpload(idname, idnameRO, existingFile, fileType) {
	popUpFileUpload = window.open('file_upload.aspx?id=' + idname + '&idRO=' + idnameRO + '&existingFile=' + existingFile + '&fileType=' + fileType, 
		'popupfileupload', 
		'width=550,height=550,left=50,top=50,resizable,scrollbars');
}

function SetFilename(id, idRO, filenameDisplay, filenameSave) {
	var theElement = MM_findObj(id);
	var theElementRO = MM_findObj(idRO);
	//popUpFileUpload.close();
	if (theElement != null) {
		theElement.value = filenameSave;
		theElementRO.value = filenameDisplay;
	}
}

function DeleteFilename(idname, idnameRO) {
	var theElement = MM_findObj(idname);
	var theElementRO = MM_findObj(idnameRO);
	theElement.value = '';
	theElementRO.value = '';
}

function ToggleNewsItem(news_id) {
	var newsDescription = MM_findObj('newsDescription_' + news_id);
	var newsDetails = MM_findObj('newsDetails_' + news_id);
	ToggleDivVisibility(newsDescription);
	ToggleDivVisibility(newsDetails);
}

function ToggleDivVisibility(div) {
	div.style.display = (div.style.display == "none") ? "" : "none";
}
