//
// menu functions
//
function preload() {
	var barDnImage = new Image;
	barDnImage.src='bar-dn.gif';
	var itemDnImage = new Image;
	itemDnImage.src='item-dn.gif';
}
function barUp(name) {
	var elem = document.getElementById(name);
	elem.style.background="url('bar-up.gif')";
}
function barDn(name) {
	var elem = document.getElementById(name);
	elem.style.background="url('bar-dn.gif')";
}
function itemUp(name) {
	var elem = document.getElementById(name);
	elem.style.background="url('item-up.gif')";
}
function itemDn(name) {
	var elem = document.getElementById(name);
	elem.style.background="url('item-dn.gif')";
}
function menubar(name) {
	this.name=name;
	this.add=addMenu;
	this.get=getMenu;
	this.slide=slideMenu;
	this.list = new Array();
}
function addMenu(name,w,h,t) {
	this.list[this.list.length]=new Menu(this.name,name,w,h,t);
}
function slideMenu(name) {
	for (var i=0; i<this.list.length; i++) {
		if (this.list[i].name==name) {
			this.list[i].direction = 'dn';
			this.list[i].slide();
		} else {
			this.list[i].reset();
		}
	}
}
function getMenu(name) {
	for (var i=0; i<this.list.length; i++) {
		if (this.list[i].name==name) {
			return this.list[i];
		}
	}
}
function Menu(bar,name,w,h,t) {
	this.bar=bar;
	this.name = name;
	this.w = w;
	this.h = h;
	this.t = t;
	this.clip = this.h;
	this.slide=slide;
	this.reset=reset;
	this.direction='dn';
	this.timer = null;
	this.elem = document.getElementById(name);
	this.elem.style.top=(this.t-this.clip)+'px';
	this.elem.style.clip='rect('+this.clip+','+this.w+','+this.h+',0)';
	this.elem.style.visibility='visible';
}
function reset() {
	if (this.timer != null)	{
		clearTimeout(this.timer);
		this.timer = null;
	}
	this.clip = this.h;
	this.direction='dn';
	this.elem.style.top=(this.t-this.clip)+'px';
	this.elem.style.clip='rect('+this.clip+','+this.w+','+this.h+',0)';
}
function slide() {
	if (this.timer == null)	
		this.timer = setTimeout(this.direction+"('"+this.bar+"','"+this.name+"')",5);
	else {
		clearTimeout(this.timer);
		this.timer = setTimeout(this.direction+"('"+this.bar+"','"+this.name+"')",5);
	}
}
function dn(bar,name) {
	var menu = eval(bar).get(name);
	if (menu.clip > 0) {
		menu.clip -= 10;
		if (menu.clip < 0)menu.clip=0;
		menu.elem.style.top=(menu.t-menu.clip)+'px';
		menu.elem.style.clip='rect('+menu.clip+','+menu.w+','+menu.h+',0)';
		menu.timer = setTimeout(menu.direction+"('"+menu.bar+"','"+menu.name+"')",5);
	} else {
		menu.timer = setTimeout("up('"+menu.bar+"','"+menu.name+"')",2000);
	}
}
function up(bar,name) {
	var menu = eval(bar).get(name);
	menu.direction = 'up';
	if (menu.clip < menu.h) {
		menu.clip += 10;
		if (menu.clip > menu.h)menu.clip=menu.h;
		menu.elem.style.top=(menu.t-menu.clip)+'px';
		menu.elem.style.clip='rect('+menu.clip+','+menu.w+','+menu.h+',0)';
		menu.timer = setTimeout(menu.direction+"('"+menu.bar+"','"+menu.name+"')",5);
	} else {
		menu.direction='dn';
		menu.timer = null;
	}
}

//
// functions for managing windows
//
function popup_image(image, title, width, height) {
	window.open('image_holder.php?src='+image+'&width='+width+'&height='+height+'&title='+title,'','width='+width+',height='+height+',scrollbars=no,menubar=no,location=no,status=no,resizable=no');
}
//
//  CMS functions
//


//
// functions for checking form input
//
function fDateCheck(aDate) {
	var daysOfMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var daysOfMonthForLeapYear = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	// dates are in format dd.MM.yyyy
	var regExpDate = /\d{1,2}\.\d{1,2}\.\d{4}/;
	if (aDate.search(regExpDate) == -1) return false;

	var dateArray = aDate.split('.');
	var aDay = parseInt(dateArray[0],10);
	var aMonth = parseInt(dateArray[1],10);
	var aYear = parseInt(dateArray[2],10)

	if (aMonth < 1 || aMonth > 12 || aDay < 1) return false;

	if (aYear < 1900 || (fIsLeapYear(aYear) && (aDay > daysOfMonthForLeapYear[aMonth-1])) || (!fIsLeapYear(aYear) && (aDay > daysOfMonth[aMonth-1]))) return false;
	else return true;
}

function fIsLeapYear(year) {
	if (((year % 4 == 0) && year % 100 != 0) || year % 400 == 0) return true;
	else return false;
}

function fTimeCheck(aTime) {
	var regExpTime = /\d{2}\:\d{2}/;

	if (aTime.search(regExpTime) == -1) return false;
	
	var timeArray = aTime.split(':');
	var aHour = parseInt(timeArray[0],10);
	var aMin = parseInt(timeArray[1],10);
	
	if (aHour < 0 || aHour > 24) return false;
	if (aMin < 0 || aMin > 59) return false;
	return true;
}

function fIntervalCheck(aTime) {
	var regExpTime = /\d{2}\:\d{2}\:\d{2}/;

	if (aTime.search(regExpTime) == -1) return false;
	
	var timeArray = aTime.split(':');
	var aHour = parseInt(timeArray[0],10);
	var aMin = parseInt(timeArray[1],10);
	var aSec = parseInt(timeArray[2],10);
	
	if (aHour < 0 || aHour > 24) return false;
	if (aMin < 0 || aMin > 59) return false;
	if (aSec < 0 || aSec > 59) return false;
	return true;
}

function fCompareDates(startDate,endDate){
	// dates are in format dd.MM.yyyy
	if (startDate == "" || endDate == "") return false;

	startDateArray = startDate.split('.');
	endDateArray = endDate.split('.');
	
	hashCode1 = parseInt(startDateArray[2],10)*10000 + parseInt(startDateArray[1],10)*100 + parseInt(startDateArray[0],10);
	hashCode2 = parseInt(endDateArray[2],10)*10000 + parseInt(endDateArray[1],10)*100 + parseInt(endDateArray[0],10);

	if (hashCode2 <= hashCode1) return false;
	else return true;
}

function fCheckInteger(field) {
	if (field == "") return false;
	var regExpInt = /^\-?\d+$/;
	if (field.search(regExpInt) == 0) return true;
	else return false;
}

function fCheckPositiveInteger(field, allowZero) {
	if (field == "") return false;
	var regExpInt = /^\d+$/;
	if (field.search(regExpInt) == 0) {
		if (!allowZero && parseInt(field) == 0) return false;
		else return true;
	}
	else return false;
}

function fCheckFloat(field) {
	if (field == "") return false;
	var regExpFloat = /^\-?\d+\,?\d*$/;
	if (field.search(regExpFloat) == 0) return true;
	else return false;
}

function fCheckPositiveFloat(field) {
	if (field == "") return false;
	var regExpFloat = /^\d+\,?\d*$/;
	if (field.search(regExpFloat) == 0) return true;
	else return false;
}

function fCheckEmail(field) {
	if (field == "") return false;
	var regExpEmail = /^\w+((\-\w+)|(\.\w+))*\@\w+((\.|\-)\w+)*\.([a-zA-Z]{2,4})$/;
	if (field.search(regExpEmail) == 0) return true;
	else return false;
}

function fCheckUsername(field) {
	if (field == "") return false;
	var regExp = /^([a-zA-Z0-9]{8,20})$/;
	if (field.search(regExp) == 0) return true;
	else return false;
}

function fCheckPassword(field) {
	if (field == "") return false;
	var regExp = /^([a-zA-Z0-9]{8,15})$/;
	if (field.search(regExp) == 0) return true;
	else return false;
}

function fCheckEmpty(field) {
	if (field == "") return false;
	else return true;
}

function fCheckText(field) {
	if (field == "") return false;
	var regExp = /^[\S][\S\s]*$/;
	if (field.search(regExp) == 0) return true;
	else return false;
}

function fCheckPhone(field) {
	if (field == "") return false;
	var regExp = /^\+?([0-9]{5,15})$/;
	if (field.search(regExp) == 0) return true;
	else return false;
}
