// Javascript for OTR
IsMSIE  = navigator.appVersion.indexOf('MSIE') != -1;
IsMSIE7 = navigator.appVersion.indexOf('MSIE 7.0') != -1;

// Opens JS popup window with image, allows for document.title
function openImageWindow(ImgFile, CaptionText, WindowTitle, WindowOptionsOverride) {
	tmpName = Math.random();
	winName = String(tmpName).replace(/\./, '');
	imgWin = window.open('', winName, (WindowOptionsOverride != null)?WindowOptionsOverride:'width=540,height=475,scrollbars');

	with (imgWin.document) {
		open();
		write("<html><head><title>" + WindowTitle + "</title></head><link href=\"otr.css\" rel=\"stylesheet\"><body><img src=\"" + ImgFile + "\"><p><font face=\"Arial\" size=2><b>" + WindowTitle + "</b></p><p>" + CaptionText + "</p><p align=\"center\"><font size=1><a href=\"javascript: window.close()\">Close Window</a></font></font></body></html>");
		close();
	}
}

// Anti-spiders an email address
function generateAntiSpiderEmail(Username, DomainName, TLD, OutputText, NoLink) {
	EmailAddress = Username + "&#64;" + DomainName + "&#46;" + TLD;
	OutputText   = ((OutputText)?OutputText:EmailAddress);
	
	document.write((NoLink)?OutputText:"<a href=\"ma" + "il" + "to:" + EmailAddress + "\">" + OutputText + "</a>");
}

// Stuff for forms
function onlyDate(Obj) {
	Obj.value = Obj.value.replace(/[^0-9\/]/, '');
}

// Function to create image overlay
function enlargeImg(ImgSrc, ImgLabel, ImgText) {
	
	IsYoutubeVideo = ImgSrc.substr(0,9) == 'VIDEOSRC:';

	with (document) {
		NewContainerDiv	= createElement('div');
		NewDisableDiv	= createElement('div');
		NewImg		= createElement('img');
		NewXImg		= createElement('img');
		ImgTextElmnt	= createTextNode(ImgText);
		NewTextContainerDiv = createElement('div');
	}
	
	with (NewImg) {
		src		= ImgSrc;
		style.margin	= '0px 0px 20px 0px';
		style.cursor	= 'pointer';
		style.display	= 'block';
	
		TmpLabel = ImgLabel + ' [click to close]';		
		setAttribute('alt', TmpLabel);
		setAttribute('title', TmpLabel);
		setAttribute('id', 'MainImg');
	}
	
	with (NewXImg) {
		src = '/images/x.gif';
		
		style.position	= 'absolute';
		style.margin	= '-35px 0px 0px ' + ((IsMSIE7 && IsYoutubeVideo)?'5':NewImg.width+5) + 'px';
		style.cursor	= 'pointer';
		
		setAttribute('width', '30');
		setAttribute('height', '35');
		setAttribute('alt', 'close');
		setAttribute('title', 'close');
		setAttribute('id', 'XImg');
	}
	
	setEvent(NewImg, 'onClick', 'closeMainImg()');
	setEvent(NewXImg, 'onClick', 'closeMainImg()');
	
	if (NewImg.width == 0 && !IsYoutubeVideo) {
		setTimeout('enlargeImg("' + ImgSrc + '", "' + ImgLabel + '", "' + ImgText + '")', 500);
		return;
	}
	else if (IsYoutubeVideo)
		NewImg.width = 425;
	
	NewTextContainerDiv.style.width = NewImg.width;

	setLeft = (Math.round(document.body.clientWidth / 2)*1)/1 - ((NewImg.width + 10) / 2);
	setLeft = ((String(setLeft).indexOf('.') == -1)?String(setLeft):String(setLeft).substr(0, String(setLeft).lastIndexOf('.'))) + 'px';
	
	setTop = String(document.body.scrollTop + 35) + 'px';
	
	with (NewContainerDiv) {
		setAttribute('id', 'MainImgContainer');
		
		style.position	= 'absolute';
		style.top	= setTop;
		style.left	= setLeft;
		style.zIndex	= '2';
		style.background= '#FFFFFF';
		style.fontFamily= 'Arial';
		style.fontSize	= '12px';
		style.padding   = '20px'; 
	}
	
	with (NewDisableDiv.style) {
		top        = '0px';
		left       = '0px';
		width      = '100%';
		position   = 'absolute';
		height     = String(document.body.scrollHeight) + 'px';
		background = '#204052';
		zIndex     = '1';
		
		if (IsMSIE) {
			//toggleSelectMenuVisibility(true, 'hidden');
			filter = 'alpha(opacity=75)';
		}
		else {
			setProperty('-moz-opacity', '.75', ''); 
			setProperty('opacity', '.75', '');			
		}			
	}
	
	NewDisableDiv.setAttribute('id', 'DisableDiv');
	
	with (document.body) {
		appendChild(NewDisableDiv);
		appendChild(NewContainerDiv);
	}
	
	if (IsYoutubeVideo) {
		NewContainerDiv.innerHTML
		= '<object width="425" height="344" style="margin-bottom: 20px"><param name="movie" value="' + ImgSrc.substr(9) + '"></param>'
		+ '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
		+ '<embed src="' + ImgSrc.substr(9) + '" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" '
		+ 'allowfullscreen="true" style="margin-bottom: 20px"></embed></object>';
		
		NewContainerDiv.appendChild(NewXImg);
	}
	else {
		NewContainerDiv.appendChild(NewXImg);
		NewContainerDiv.appendChild(NewImg);
	}
	
	NewContainerDiv.appendChild(NewTextContainerDiv);
	NewTextContainerDiv.appendChild(ImgTextElmnt);
}

// Function to close overlay
function closeMainImg() {
// 	with (NewContainerDiv) {
// 		removeChild(document.getElementById('MainImg'));
// 		removeChild(document.getElementById('XImg'));
// 	}
	
	with (document.body) {
		removeChild(document.getElementById('DisableDiv'));
		removeChild(document.getElementById('MainImgContainer'));
	}
}

// ASSIGNS A JAVASCRIPT EVENT TO A SPECIFIED OBJECT ACCORDING TO THE BROWSER
function setEvent(Obj, Event, Action) {
	
	// For IE, use attachEvent
	if (IsMSIE) {
		eval("Obj.attachEvent('" + Event.toLowerCase() + "', function() { " + Action + " });");
	}
	
	// For proper browsers, simply use setAttribute
	else
		Obj.setAttribute(Event, Action);
}

// Menu function
function menu(ContainerID, Over) {
	document.getElementById(ContainerID).style.display = (Over)?'block':'none';
}