var m_thumbnailId;
var m_imageSrc;
var m_showPopup = false;


function PageWidth() {
	return window.innerWidth != null? window.innerWidth : 
		document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : 
		document.body != null ? document.body.clientWidth : 
		null;
} 
function PageHeight() {
	return  window.innerHeight != null? window.innerHeight : 
		document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : 
		document.body != null? document.body.clientHeight : 
		null;
}
function PageLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :
		document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : 
		document.body.scrollLeft ? document.body.scrollLeft : 
		0;
} 
function PageTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : 
		document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : 
		document.body.scrollTop ? document.body.scrollTop : 
		0;
} 

function GetLeft(element) {
	if (element.offsetParent) return element.offsetLeft + GetLeft(element.offsetParent);
	else return element.offsetLeft;
}
function GetTop(element) {
	if (element.offsetParent) return element.offsetTop + GetTop(element.offsetParent);
	else return element.offsetTop;
}
function setOpacity(element, value) {
	element.style.opacity = value/100;
	element.style.filter = 'alpha(opacity=' + value + ');';
}
function ShowPicturePopup(imageSrc, thumbnailId) {
	m_showPopup = true;
	m_thumbnailId = thumbnailId;
	m_imageSrc = imageSrc;
	
	// Preload the image before showing the popup
	var picture = document.getElementById('popupPictureSrc');
	picture.onload = ImageLoaded;
	picture.src = imageSrc;
}
function HidePicturePopup(thumbnailId) {
	m_showPopup = false;
	var thumbnail = document.getElementById(thumbnailId);
	setOpacity(thumbnail, 100);
	document.getElementById('popupPicture').style.display = 'none';
	thumbnail.style.zIndex = "0";
}
function ImageLoaded() {
	if (!m_showPopup) return;
	var thumbnail = document.getElementById(m_thumbnailId);
	setOpacity(thumbnail, 0);
	thumbnail.style.zIndex = "2";

	// Show the popup
	var popup = document.getElementById('popupPicture');
	var picture = document.getElementById('popupPictureSrc');
	popup.style.display = 'block';

	var left = GetLeft(thumbnail) - picture.clientWidth/2;
	if (left + picture.clientWidth > PageLeft()+PageWidth()-20) left = PageLeft()+PageWidth()-20 - picture.clientWidth;
	if (left < PageLeft()) left = PageLeft();
	popup.style.left = left + "px";

	var top = GetTop(thumbnail) - picture.clientHeight/2
	if (top + picture.clientHeight > PageTop()+PageHeight()-20) top = PageTop()+PageHeight()-20 - picture.clientHeight;
	if (top < PageTop()) top = PageTop();
	popup.style.top = top + "px";
}
document.write("<div id='popupPicture'><img id='popupPictureSrc' src='' alt=''></div>");

function OpenChatWindow(user_id) {
	var win = window.open("http://www.theyfall.com/chatwindow.aspx?user_id=" + user_id, user_id, "width=488,height=400,location=no,menubar=no,status=no,toolbar=no,resizable=yes");
	win.focus();
}

function SetDisplay(i_clientId, i_display) {
	var element = document.getElementById(i_clientId);
	var value = i_display;
	if (value == true) value = "block";
	if (value == false) value = "none";
	if (element) element.style.display = value;
}

function ToggleDisplay(i_clientId) {
	var element = document.getElementById(i_clientId);
	if (element.style.display == "none") {
		element.style.display = "block";
	} else {
		element.style.display = "none";
	}
}

function ShowPopup(i_type) {
	window.open("popup.aspx?type=" + i_type, i_type, "width=500,height=600,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}


function VerifyDate(ctlYear, ctlMonth, ctlDay){
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

	err = 0;
	/* year is wrong if year = 0000 */
	year = document.getElementById(ctlYear).selectedIndex;
	if (year == 0) {
		return false;
	}
	year += 1919;
	
	/* Validation of month*/
	month = document.getElementById(ctlMonth).selectedIndex;
	if ((month < 1) || (month > 12)) {
		return false;
	}
	/* Validation of day*/
	day = document.getElementById(ctlDay).selectedIndex;
	if (day < 1 || day > 31) {
		return false;
	}
	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
	  leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) {
		return false;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) {
		return false;
	}
	/* Validation of other months */
	if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))) {
		return false;
	}

	return true;
}

