<!--

/* Poiston varmistus */
function confirmDelete(msg)
{
	if(msg==undefined){
			msg="Ok?";
	}
	var con=confirm(msg);
	if (con)
        return true;
	else
        return false;
}


/*	setTextField_text()
	-------------------
	kun halutaan kirjoittaa/muuttaa tekstikenttään haluttu teksti
	field = kentän nimi johon arvo asetetaan
	value = asetettava arvo
*/
function setTextField_text(field,value)
{
	field.value=value;
}


/*	setTextField_date()
	-------------------
	kun halutaan kirjoittaa/muuttaa tekstikenttään haluttu päivämäärä
	field = kentän nimi johon arvo asetetaan
	direction = suunta					('' = kun halutaan nykyinen päivä, '-' = menneisyyteen, '+' = tulevaisuuteen)
	days = päiviä eteen/taakse			(0 = nykyinen)
	months = kuukausia eteen/taakse		(0 = nykyinen)
	years = vuosia eteen/taakse			(0 = nykyinen)
	skipWeekend = jos päivä osuu vklopulle -> hyppää yli
*/
function setTextField_date(field,direction,days,months,years,skipWeekend)
{
	var myDate = new Date();
	
	/* ei suuntaa -> nykyinen päivä */
	if(direction==''){
		myDate.setDate(myDate.getDate());
		myDate.setMonth((myDate.getMonth()));
		myDate.setFullYear(myDate.getFullYear());
	}
	
	/* menneisyyteen */
	if(direction=='-'){
		myDate.setDate(myDate.getDate()-days);
		myDate.setMonth((myDate.getMonth())-months);
		myDate.setFullYear(myDate.getFullYear()-years);
		if(skipWeekend==true){
			dayOK=false;
			while(dayOK==false){
				if(myDate.getDay()>0 && myDate.getDay()<6){
					dayOK=true;
				}else{
					myDate.setDate(myDate.getDate()-1);
				}
			}
		}
	}
	
	/* tulevaisuuteen */
	if(direction=='+'){
		myDate.setDate(myDate.getDate()+days);
		myDate.setMonth((myDate.getMonth())+months);
		myDate.setFullYear(myDate.getFullYear()+years);
		if(skipWeekend==true){
			dayOK=false;
			while(dayOK==false){
				if(myDate.getDay()>0 && myDate.getDay()<6){
					dayOK=true;
				}else{
					myDate.setDate(myDate.getDate()+1);
				}
			}
		}
	}
	field.value=(myDate.getDate()) + '.' + (myDate.getMonth()+1) + '.' + (myDate.getFullYear());
}

/*	copyField2Field()
	-------------------
	kun halutaan kopioida kentän sisältö toiseen kenttään
	fieldFrom = kentän nimi josta arvo kopioidaan
	fieldTo = kentän nimi johon arvo kopioidaan
*/
function copyField2Field(fieldFrom, fieldTo){
	fieldTo.value=fieldFrom.value;
}

function setCurDate(elm_day,elm_month,elm_year)
{
        var dCurDate = new Date();
        var dCurMonth = dCurDate.getMonth()+1;
        var dCurDayOfMonth = dCurDate.getDate();
        var dCurYear = dCurDate.getFullYear();
		
		//elm_day.options[dCurDayOfMonth].selected=true;
		elm_day.value=dCurDayOfMonth;
		//elm_month.options[dCurMonth].selected=true;
		elm_month.value=dCurMonth;
		//elm_vuosi.options[dCurYear].selected=true;
		elm_year.value=dCurYear;
}


/*	toggleDisplay()
	-------------------
	elementtien piilotus/näyttö
*/
function toggleDisplay(elem){
	var disp;
	elemObj=document.getElementById(elem);
	if(elemObj.style.display=='none'){
		disp='';
		elemObj.style.display='';
	}else{
		disp='none';
		elemObj.style.display='none';
	}
}



function setLight(r, status) {
	setStyle(rows[r], status);
}

function setStyle(elem, status, defclass) {
	if (elem.className != "selected")
		elem.prevclass = elem.className;

	if (status) {
		elem.className="selected";
	} else {
		elem.className=elem.prevclass;
	}
}


/*
	Funktiot joiden avulla voidaan siirtää objekteja select boxeista toisiin
	http://gethelp.devx.com/techtips/js_pro/10min/10min0499/10min0499.asp => Boris Feldman
*/

function makeStringFromSelect(selectCtrl) {
	var i;
	var j = 0;
	var outlist = "";

	for (i = 0; i < selectCtrl.options.length; i++) {
		if (j > 0) {
			outlist = outlist + ", ";
		}
		outlist = outlist + selectCtrl.options[i].value;
		j++;
	}
	return outlist;
}

function addItems(fromCtrl, toCtrl) {
	var i;
	var j;
	var itemexists;
	var nextitem;

	// step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++) {
		if (fromCtrl.options[i].selected) {
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists))) {
				if (toCtrl.options[j].value == fromCtrl.options[i].value) {
					itemexists = true;
					//alert(fromCtrl.options[i].value + " found!");
				}
				j++;
			}
			if (!(itemexists)) {
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
				toCtrl.options[nextitem].value = fromCtrl.options[i].value;
			}
		}
	}
}

function removeItems(fromCtrl) {
	var i = 0;
	var j;
	var k = 0;

	while (i < (fromCtrl.options.length - k)) {
		if (fromCtrl.options[i].selected) {
			// remove the item
			for (j = i; j < (fromCtrl.options.length - 1); j++) {
				fromCtrl.options[j].text = fromCtrl.options[j+1].text;
				fromCtrl.options[j].value = fromCtrl.options[j+1].value;
				fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
			}
			k++;
		} else {
			i++;
		}
	}
	for (i = 0; i < k; i++) {
		fromCtrl.options[fromCtrl.options.length - 1] = null;
	}
}

function deleteBooking( id, parent_id, repeat_id, hasSubBookings ){
	if( repeat_id != undefined && repeat_id.toString().match( /^\d+$/ ) ){
		var cont					= document.createElement( 'div' );
		cont.innerHTML				= '<h5>Varauksen poistaminen</h5>';
		cont.style.border			= '2px outset #808080';
		cont.style.padding			= '5px';
		cont.style.position			= 'absolute';
		cont.style.zIndex			= 999999;
		cont.style.backgroundColor	= 'white';
		cont.style.width			= '310px';
		var killCont				= function(){ document.body.removeChild( cont ); };
		var buttons = [
			[ 'Poista kaikki saman toiston varaukset tästä eteenpäin', function(){ killCont(); window.location = 'admin_bookings.php?cmd=delete&id=' + id + '&modifyRecursive=1'; } ],
			[ 'Poista vain tämä varaus', function(){ killCont(); window.location = 'admin_bookings.php?cmd=delete&id=' + id; } ],
			[ 'Peruuta', killCont ]
		];
		for( var i = 0; i < buttons.length; i++ ){
			var btn				= document.createElement( "input" );
			btn.type			= 'button';
			btn.style.display	= 'block';
			btn.style.width		= '300px';
			btn.style.margin	= '5px';
			btn.style.textAlign	= 'center';
			btn.value			= buttons[ i ][ 0 ];
			btn.onclick			= buttons[ i ][ 1 ];
			cont.appendChild( btn );
		}
		document.body.appendChild( cont );
		cont.style.left		= ( ( getWindowWidth() - cont.offsetWidth ) / 2 ) + "px";
		cont.style.right	= ( ( getWindowWidth() - cont.offsetWidth ) / 2 ) + "px"
		cont.style.top		= "300px";
		scrollTo( 0, 0 );
	}else{
		var confirmText = ( hasSubBookings ) ? 'Haluatko varmasti poistaa varauksen ja kaikki sen alivaraukset?' : 'Haluatko varmasti poistaa varauksen?';
		if( confirm( confirmText ) ) window.location = 'admin_bookings.php?cmd=delete&id=' + id;
	}
};
function getWindowWidth(){
	return ( !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) ) ? (!(document.documentElement.clientWidth) || (document.documentElement.clientWidth === 0)) ?	document.body.clientWidth : document.documentElement.clientWidth : window.innerWidth;
};
function getWindowHeight(){
	return ( !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) ) ? (!(document.documentElement.clientHeight) || (document.documentElement.clientHeight === 0)) ? document.body.clientHeight : document.documentElement.clientHeight: window.innerHeight;
};
//-->

