function popup(url, width, height) {
	var params = "status=1, width=" + width + ", height=" + height;
	var newpopup = window.open(url, "popupwindow", params);
	if (!newpopup) {
		alert("Your browser has blocked this popup");
	} else {
		newpopup.focus();
	}
}

function gotoDropdownlistPage(elId) {
	if (document.getElementById(elId) == null) {
		return;
	}
	dd =  document.getElementById(elId);
	var url = dd.options[dd.selectedIndex].value;
	if (url == "") {
		return;
	}
	var gotoPage = false;
	if (self.location.href.indexOf("/admin/temp") > -1) {
		if (confirm("Following this link will take you away from the page being edited and your changes may be lost. Are you sure you wish to proceed?")) {
			window.location.href = url;
			return;
		}
	}  else {	
		window.location.href = url;
	}
}


function isValidDate(day,month,year){
	var dteDate = new Date(year, month, day);
	return ((day == dteDate.getDate()) && (month == dteDate.getMonth()) && (year == dteDate.getFullYear()));
}

function validateDate(source, arguments) {
	parts = arguments.Value.split("/");
	month = eval(parts[1] * 1) - 1;
	year = 19 + parts[2];
	arguments.IsValid = (parts.length == 3) ? isValidDate(parts[0], month, year) : false;
}

var dom = (typeof document.getElementById != "undefined");
var ie = (typeof document.all != "undefined");

function setOpacity(el, opacity) {
	var opacityPercentage = Math.floor(100 * opacity);
	opacity = opacityPercentage / 100;
	if (el.filters && el.filters[0]) {
		if (typeof el.filters[0].opacity == "number") { // IE6+
			el.filters[0].opacity = opacityPercentage;
		} else { // IE5.5-
			el.style.filter = "alpha(opacity=" + opacityPercentage +")";
		}
	} else if (el.style.MozOpacity) {
		el.style.MozOpacity = opacity;
	} else if (el.style.KhtmlOpacity) {
		el.style.KhtmlOpacity = opacity;
	} else if (el.style.opacity && !el.filters) {
		el.style.opacity = opacity;
	}
}

function ImageTransition(imageUrls, id, width, height) {
	this.imageUrls = imageUrls;
	this.id = id;
	this.width = width;
	this.height = height;
	this.images = [];
	this.currentImageIndex = 0;
	this.firstSlideIsCurrent = true;
	this.transitionDuration = 1000;
	this.frameDelay = 50;
	this.transitionDelay = 3000;
	
	// Preload images
	for (var i = 0; i < imageUrls.length; i++) {
		this.images[i] = new Image();
		this.images[i].src = imageUrls[i];
	}
}

ImageTransition.prototype = {
	getSlideId: function(slideNum) {
		return "slide_" + this.id + "_" + slideNum;
	},
	
	getSlide: function(slideNum) {
		return document.getElementById(this.getSlideId(slideNum));
	},

	create: function() {
		var trans = this;
		
		function getSlideHtml(slideNum) {
			return '<div id="' + trans.getSlideId(slideNum) + '" style="width:' + 
				trans.width + 'px;height:' + trans.height + 'px;filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);opacity:0;-moz-opacity:0;-khtml-opacity:0" class="slide"></div>';
		}

		document.write('<div id="' + this.id + '" style="width:' + this.width + 'px;height:' +
			this.height + 'px" class="transition">' + getSlideHtml(0) + getSlideHtml(1) + '</div>');
	},
	
	setSlideImage: function(slide, imageIndex) {
		slide.innerHTML = '<img src="' + this.images[imageIndex].src + '" width="' + this.width + '" height="' + this.height + '" />';
	},
	
	start: function() {
		var currentSlide = this.getCurrentImageSlide();
		this.setSlideImage(currentSlide, this.currentImageIndex);
		setOpacity(currentSlide, 1);
		var trans = this;
		setTimeout(function() { trans.doTransition(); }, this.transitionDelay);
	},
	
	getCurrentImageSlide: function() {
		var slideNum = this.firstSlideIsCurrent ? 0 : 1;
		return this.getSlide(slideNum);
	},

	getNextImageSlide: function() {
		var slideNum = this.firstSlideIsCurrent ? 1 : 0;
		return this.getSlide(slideNum);
	},
	
	doTransition: function() {
		var trans = this;

		var nextImageIndex = (this.currentImageIndex == this.images.length - 1) ? 0 : this.currentImageIndex + 1;
		var currentImageSlide = this.getCurrentImageSlide();
		var nextImageSlide = this.getNextImageSlide();
		var transitionStartTime = new Date();
		
		currentImageSlide.style.zIndex = 1;
		nextImageSlide.style.zIndex = 2;

		// Set image for the next slide
		this.setSlideImage(nextImageSlide, nextImageIndex);
		
		function nextTransition() {
			trans.doTransition();
		}
		
		function nextFrame() {
			var now = new Date();
			var transitionProgress = Math.min(1, (now - transitionStartTime) / trans.transitionDuration);
			var nextSlideOpacity = transitionProgress;

			setOpacity(nextImageSlide, nextSlideOpacity);
			
			if (transitionProgress == 1) {
				// The transition is over. Set the variables and the timer till the next transition
				setOpacity(currentImageSlide, 0);
				trans.currentImageIndex = nextImageIndex;
				trans.firstSlideIsCurrent = !trans.firstSlideIsCurrent;
				setTimeout(nextTransition, trans.transitionDelay);
			} else {
				setTimeout(nextFrame, trans.frameDelay);
			}
		}
		
		nextFrame();
	}
};
