﻿


/***********************************************************************************/
/******************************** PAGE MODULE EDITOR *******************************/
/***********************************************************************************/

pme_currentAdminDiv = null;

function pme_editClick(linkDiv, adminDiv) {

	pme_hideCurrent();
	

	pme_currentAdminDiv = adminDiv;

	adminDiv.style.display = 'block';

	var adminHeight = parseInt(adminDiv.offsetHeight);
	var adminWidth = parseInt(adminDiv.offsetWidth);
	var linkWidth = parseInt(linkDiv.offsetWidth);
	

	adminDiv.style.left = (oe_findPosX(linkDiv) - adminWidth + linkWidth) + 'px';
	adminDiv.style.top = oe_getYPositionToPlace(oe_findPosY(linkDiv), adminHeight) + 'px';


}

function pme_hideCurrent() {
	if (pme_currentAdminDiv == null) {
		return;
	}
	pme_currentAdminDiv.style.display = 'none';
	pme_currentAdminDiv.style.top = '-1000px';
	pme_currentAdminDiv.style.left = '-1000px';
}


function openObjectRearranger(querystring) {

	var height = td_getWindowHeight() - 20;	
	
	oe_popup('/Site/Admin/Popups/ObjectRearranger.aspx?' + querystring, 'ObjectRearranger', 640, height);
	
}

/***********************************************************************************/
/************************************ PAGE STATS ***********************************/
/***********************************************************************************/

var ps_link = null;
var ps_statsDiv = null;

function loadPageStats(pageId, statsLink, statsDiv) {
	ps_link = statsLink;
	ps_statsDiv = statsDiv;
	statsLink.innerHTML = "Loading stats...";
	ps_loadXMLDoc('/Site/Admin/Stats/GetPageStatsAsXml.aspx?pageId=' + pageId);
}

var ps_req = null;


function ps_loadXMLDoc(url) {
	ps_req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			ps_req = new XMLHttpRequest();
		} catch(e) {
			ps_req = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			ps_req = new ActiveXObject('Msxml2.XMLHTTP');
		} catch(e) {
			try {
				ps_req = new ActiveXObject('Microsoft.XMLHTTP');
			} catch(e) {
				ps_req = false;
			}
		}
	}
	if(ps_req) {
		ps_req.onreadystatechange = ps_processReqChange;
		ps_req.open('GET', url, true);
		ps_req.send('');
	}
}

function ps_processReqChange() {
	// only if req shows 'loaded'
	if (ps_req.readyState == 4) {
		// only if 'OK'
		if (ps_req.status == 200) {
			// ...processing statements go here...
			
			ps_showPageStats( ps_req );

		} else {
			ps_link.innerHTML = 'There was a problem retrieving the statistics. Click here to try again.<br/>(error message: \"' + ps_req.statusText + "\")";
		}
	}
}

function ps_showPageStats( req ) {

	var errors = req.responseXML.getElementsByTagName('ErrorMessage');
	for (var i = 0; i < errors.length; i++) {
		var name = oe_getElementTextNS('', 'ErrorMessage', req.responseXML, 0);
		ps_link.innerHTML = 'There was a problem retrieving the statistics. Click here to try again.<br/>(error message: \"' + name + "\")";
		return;
	}	


	ps_link.style.display = 'none';
	ps_statsDiv.style.display = 'block';
	
	var result = req.responseXML.getElementsByTagName('Result')[0];
	
	document.getElementById('ps_totalViews').innerHTML = oe_getElementTextNS('', 'PageViews', result, 0);
	document.getElementById('ps_totalViewsAsPercentage').innerHTML = oe_getElementTextNS('', 'PageViewsAsPercentage', result, 0);

	ps_createTable(result, 'PreviousPages', 'ps_previousPagesDiv');
	ps_createTable(result, 'NextPages', 'ps_nextPagesDiv');

}

function ps_createTable(result, xmlTagName, divTagName) {	
	
	var items = result.getElementsByTagName(xmlTagName)[0].getElementsByTagName('Page');
	var tbl = '';
	for (var i = 0; i < items.length; i++) {
		var page = items[i];
		var num = oe_getElementTextNS('', 'Occurances', page, 0);
		var name = oe_getElementTextNS('', 'Name', page, 0);
		var id = oe_getElementTextNS('', 'Id', page, 0);
		tbl += '<tr><td align=\"right\">' + num + '</td><td><a href=\"Default.aspx?pageId=' + id + '\">' + name + '</a></td></tr>\n';
	}
	document.getElementById(divTagName).innerHTML = '<table class=\"DataTable\"><th style=\"text-align:right;\">Number</th><th>Page</th>' + tbl + '</table>';

}





/***********************************************************************************/
/******************************* THUMBNAIL DISPLAYER *******************************/
/***********************************************************************************/



var td_div = null;
var td_imgTbl = null;
var td_imgTblCell = null;
var td_imgDiv = null;
var td_img = null;
var td_captionDiv = null;
var td_nameDiv = null;


function td_showLargeImage(imageUrl, alt, caption) {
	if (td_div == null) {
		td_div = document.createElement('div');
		td_div.id = 'td_div';	
		td_div.style.position = 'absolute';
		td_div.style.left = '-1000px';
		td_div.style.top = '-1000px';
		td_div.onclick = td_hideLargeImage;		
		document.body.appendChild(td_div);

		var tableHolder = document.createElement('div');
		tableHolder.innerHTML = "<table id=\"td_imgTbl\" style=\"position:absolute;left:0px;top:0px;\"><tr><td valign=\"center\" align=\"center\" id=\"td_imgTblCell\"></td></tr></table>";
		document.body.appendChild(tableHolder);

		td_imgTbl = document.getElementById('td_imgTbl');
		td_imgTblCell = document.getElementById('td_imgTblCell');



		td_nameDiv = document.createElement('div');
		td_nameDiv.id = 'td_nameDiv';
		td_imgTblCell.appendChild(td_nameDiv);
		td_nameDiv.innerHTML = '<a href=\"javascript:td_hideLargeImage();\" style=\"color:Red;display:block;float:right;\"><img src=\"/Site/Images/close.gif\" style=\"border:0px;\" alt=\"Close\" title=\"\" /></a><div id=\"td_nameLabel\"></div>';


		
		td_imgDiv = document.createElement('div');
		td_imgDiv.style.overflow = 'auto';
		td_imgDiv.id = 'td_imgDiv';
		td_imgTblCell.appendChild(td_imgDiv);

	}

	if (td_img != null) {
		td_imgTblCell.removeChild(td_captionDiv);
		td_imgDiv.removeChild(td_img);
	}

	//td_img = new Image(); dont use image constructer in safari
	td_img = document.createElement('img');
	td_img.id = 'td_img';


	


	var winWidth = td_getWindowWidth();
	var winHeight = td_getWindowHeight();

	var divWidth = (winWidth - 50) + 'px';
	td_imgDiv.style.height = (winHeight - 100) + 'px';
	td_imgDiv.style.width = divWidth;



	var td_nameLabel = document.getElementById('td_nameLabel');
	td_nameLabel.innerHTML = alt;
	//td_nameLabel.style.width = divWidth;

	td_imgDiv.appendChild(td_img);

	
	td_captionDiv = document.createElement('div');
	td_captionDiv.id = 'td_captionDiv';
	td_captionDiv.style.width = divWidth;
	td_imgTblCell.appendChild(td_captionDiv);

	td_img.onload = td_imageLoaded;
	td_img.src = imageUrl;
	td_img.alt = alt;
	td_captionDiv.innerHTML = caption;
	
	td_setTDPositions();

	td_imgTbl.onclick = td_hideLargeImage;
	td_img.onclick = td_onImageClick;


	
	window.onscroll = td_windowScroll;
	window.onresize = td_windowResize;
	
}

function td_onImageClick() {
	return false;
}

function td_getWindowWidth() {
	var x;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
	}
	return parseInt(x);
}

function td_getWindowHeight() {
	var y;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		y = document.body.clientHeight;
	}
	return parseInt(y);
}

function td_imageLoaded() {
	
	var cellWidth = parseInt(td_imgDiv.style.width);
	var cellHeight = parseInt(td_imgDiv.style.height);
	
	var winWidth = td_getWindowWidth();
	var winHeight = td_getWindowHeight();

	var aspectRatio = td_img.width / td_img.height;

	if (td_img.height > cellHeight) {
		td_img.style.height = cellHeight + 'px';
		td_img.style.width = parseInt(cellHeight * aspectRatio) + 'px';
	} else {
		td_imgDiv.style.height = parseInt(td_img.height) + 'px';
	}

	if (td_img.width > cellWidth) {
		td_img.style.width = cellWidth + 'px';
		td_img.style.height = parseInt(cellWidth / aspectRatio) + 'px';
	} else if (td_img.height > winHeight - 100) {	
		td_imgDiv.style.width = (parseInt(td_img.width) + 19) + 'px';
	} else {
		td_imgDiv.style.width = parseInt(td_img.width) + 'px';
	}


	var finalWidth = Math.max(100, parseInt(td_imgDiv.style.width)) + 'px';
	td_captionDiv.style.width = finalWidth;
	td_nameDiv.style.width = finalWidth;
	
		
}

function td_getScrollY() {
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	return y;

}

function td_setTDPositions() {

	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}


	var scrollY = parseInt(y);
	var scrollX = parseInt(x);
	
	td_div.style.left = scrollX + 'px';
	td_div.style.top = scrollY + 'px';

	td_imgTbl.style.left = scrollX + 'px';
	td_imgTbl.style.top = scrollY + 'px';
	

	var winWidth = td_getWindowWidth();
	var winHeight = td_getWindowHeight();

	td_div.style.width = winWidth + 'px';
	td_div.style.height = winHeight + 'px';


	td_imgTbl.style.width = winWidth + 'px';
	td_imgTbl.style.height = winHeight + 'px';

}

function td_windowScroll() {
	td_setTDPositions();	
}

function td_windowResize() {
	td_setTDPositions();
	td_imageLoaded();
}

function td_hideLargeImage() {
	window.onscroll = null;
	window.onresize = null;
	td_div.style.left = '-5000px';
	td_div.style.top = '-5000px';
	td_imgTbl.style.left = '-5000px';
	td_imgTbl.style.top = '-5000px';
	
}



var sap_lastUrl = null;
var sap_lastName = null;
var sap_lastNotes = null;

function sap_showLargeImage() {
	if (sap_lastUrl == null) {
		return;
	}
	td_showLargeImage(sap_lastUrl, sap_lastName, sap_lastNotes);
}

function SetActivePicture(pictureID, fullSizeImageFilename, imageFilename, imageName, imageNotes)
{
	var image = document.getElementById(pictureID);
	var imageCaptionDiv = document.getElementById(pictureID+'_caption');
	
	image.src = imageFilename;
	image.alt = imageName;
	
	sap_lastUrl = fullSizeImageFilename;
	sap_lastName = imageName;
	sap_lastNotes = imageNotes;
	image.onclick = sap_showLargeImage;
	image.style.cursor = 'pointer';
	
	imageCaptionDiv.innerHTML = imageNotes;
	
}




function PictureLoad(emptyPicture, activeImageId, hiddenId )
{
	var hiddenfield = document.getElementById(hiddenId);
	//alert(hiddenfield.value);
	changePictures(emptyPicture,parseInt(hiddenfield.value), activeImageId);		
}

function ImageClick(emptyPicture, hiddenId, imageIndex, activeImageId)
{
		var hiddenfield = document.getElementById(hiddenId);
		var currentIndex;
		var halfValue = Math.round(controlArray.length/2) ;
		//alert(halfValue);
		
		//alert(imageIndex);
		if (imageIndex < halfValue)
		{
			currentIndex = (parseInt(hiddenfield.value) - (halfValue  - (imageIndex )))  ;	
		}
		else
		{
			currentIndex = (parseInt(hiddenfield.value) - (halfValue - (imageIndex + 1  )))  ;
		//	alert(currentIndex);
		}
		
		
		
		
		if ((currentIndex >= 0) && ( currentIndex <= mainPictureArray.length))
		{
		
		hiddenfield.value = currentIndex;
		changePictures(emptyPicture,currentIndex, activeImageId);		
		}
}

function PreviousClick(emptyPicture, hiddenId, activeImageId)
{
		var hiddenfield = document.getElementById(hiddenId);
		if (parseInt(hiddenfield.value) != 0)
		{
		var currentIndex;
		
		currentIndex = parseInt(hiddenfield.value) - 1;
		
		if ((currentIndex >= 0) && ( currentIndex <= mainPictureArray.length))
		{
		
		hiddenfield.value = currentIndex;
		changePictures(emptyPicture,currentIndex, activeImageId);		
		}
}
}

function NextClick(emptyPicture, hiddenId, activeImageId)
{
		
		var hiddenfield = document.getElementById(hiddenId);
		if (parseInt(hiddenfield.value) != (mainPictureArray.length -1))
		{
		var currentIndex;
		
		currentIndex = parseInt(hiddenfield.value) + 1;
		
		if ((currentIndex >= 0) && ( currentIndex <= mainPictureArray.length))
		{
		
		hiddenfield.value = currentIndex;
		changePictures(emptyPicture,currentIndex, activeImageId);		
		}
}
}

function changePictures(emptyPicture, currentIndex, activeImageId)		
{

	var actualActiveImage = document.getElementById(activeImageId);
	var imageCaptionDiv = document.getElementById(activeImageId+'_caption');
	if (currentIndex >= 0)
	{
		actualActiveImage.src = mainPictureArray[currentIndex];
		sap_lastUrl = originalPictureArray[currentIndex]; 
		sap_lastName = nameArray[currentIndex];
		sap_lastNotes = nameArray[currentIndex];
		actualActiveImage.title = notesArray[currentIndex];
		actualActiveImage.onclick = sap_showLargeImage;
		imageCaptionDiv.innerHTML = notesArray[currentIndex];
		var halfValue = Math.round(controlArray.length/2)  ;
		var imageIndex;
		
		for ( i = 0; i < controlArray.length; i++)
		{
		
		var image = document.getElementById(controlArray[i]);
		
			
		if (image != null)
		{
		
		if (i < halfValue)
		{
			imageIndex = currentIndex - (halfValue - i ) ;		
		}
		else
		{
			imageIndex =  currentIndex + (i - halfValue) +1 ;
		}
		
		
		//alert(imageIndex);
		if ((imageIndex >= 0) && (imageIndex <= thumbnailPictureArray.length -1) )
		{
			image.src =  thumbnailPictureArray[imageIndex];
			image.title = nameArray[imageIndex];
			image.alt = "Thumbnail preview image";
		
		}
		else
		{
			image.src = emptyPicture;
		}
		}
		
		}
		
		
		
	}
}


function changeOpac(opacity, obj) { 
    var object = obj.style; 
    object.opacity = (opacity / 100); 
//    object.MozOpacity = (opacity / 100); 
  //  object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 




/***********************************************************************************/
/******************************* FAQ DISPLAYER *************************************/
/***********************************************************************************/





function showElement(elementID)
{
	var answerArea = document.getElementById(elementID);
	answerArea.style.display = "block";
}

function hideElement(elementID)
{
	var answerArea = document.getElementById(elementID);
	answerArea.style.display = "none";

}


/***********************************************************************************/
/******************************* EVENT DISPLAYER *******************************/
/***********************************************************************************/


function showElementInfo(elementInfoContainerId, elementLinkId, elementId, indexArray, valueArray)
{
	
	var elementInfoContainer = document.getElementById(elementInfoContainerId );
	var elementLink = document.getElementById(elementLinkId );
	
	if (elementLink != null)
	{
	
	var indexOfElement;
	
	for (var i = 0; i < indexArray.length; i++)
	{
		if (indexArray[i] == elementId)
		{
		indexOfElement = i;
		break;
		}
	}

	

	if (indexOfElement != null)	
	{
	 elementInfoContainer.innerHTML = valueArray[indexOfElement];
	 elementInfoContainer.style.left =  ( getXPositionToPlace(findPosX(elementLink) + parseInt(elementLink.offsetWidth) + 2, parseInt(elementInfoContainer.offsetWidth))) + "px";
	 elementInfoContainer.style.top =   (( getYPositionToPlace(findPosY(elementLink ) , parseInt(elementInfoContainer.offsetHeight))) )+ "px";

	if (elementInfoContainer != null)
	{
		elementInfoContainer.style.display = "block";
	}
	}
	}
}

function hideElementInfo(elementInfoContainerId)
{
	
	var elementInfoContainer = document.getElementById(elementInfoContainerId );
	
	
	
	if (elementInfoContainer != null)
	{
		elementInfoContainer.style.display = "none";
	}
}

// this looks at a position 'x' and adjusts it if it goes beyond the browser's window
function getXPositionToPlace( x, width ) {
 if (!document.documentElement) {
  return x;
 }
 
 //alert( document.documentElement.scrollLeft + ', ' + document.documentElement.clientWidth + ', ' + x + ', ' + width );
 
 var distanceToRight = document.documentElement.scrollLeft + document.documentElement.clientWidth - x - width;
 if (distanceToRight < 0) {
  x += distanceToRight;
 }
 return x;
}
 
// this looks at a position 'y' and adjusts it if it goes beyond the browser's window
function getYPositionToPlace( y, height ) {
 if (!document.documentElement) {
  return y;
 }
 
 var distanceToBottom = document.documentElement.scrollTop + document.documentElement.clientHeight - y - height;
 if (distanceToBottom < 0) {
  y += distanceToBottom;
 }
 return y;
}
 

function findPosX(obj)
{
 var curleft = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else if (obj.x)
  curleft += obj.x;
 return curleft;
}
 
function findPosY(obj)
{
 var curtop = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else if (obj.y)
  curtop += obj.y;
 return curtop;
}
 
 
 
function climbTo(element, tagname) {

	tagname = tagname.toLowerCase(); 
	var parentElement;
	while((element = element.parentNode) && !parentElement) {
		if (element.parentNode == null) {
			return null;
		}
		if (element.tagName.toLowerCase() == tagname) { 
			parentElement = element; 
		} 
	} 
 
	return parentElement;
 
} 


function returnDayOfWeek(day)
{
switch(day)
{
case 0:
  return 'Monday'
  break    
  case 1:
  return 'Tuesday'
  break 
case 2:
  return 'Wednesday'
  break
case 3:
  return 'Thursday'
  break
case 4:
  return 'Friday'
  break
case 5:
  return 'Saturday'
  break
case 6:
  return 'Sunday'
  break
default:
return 'invalid Day Code';
}
}






