
/*   javascript functions for TIMETABLE component   */
  
  var old_background;
  
  /* on mouse over event on available element */
  function mOver(item) {
  	if (!((item.className).indexOf('_clk')>0)) {
  		item.className=item.className + "_o";
	}
	old_background = item.style.backgroundColor;
	item.style.backgroundColor = "";
  }
  
  /* submit bookForm form */
  function submitRes() {
    var prebookFormObj = document.getElementById("reservForm");
    if (prebookFormObj!=null) {
    	prebookFormObj.submit();
    	prebookFormObj.reset();
    }
  }
  
  /* on mouse out event on available element */
  function mOut(item) {
  	if (!((item.className).indexOf('_clk')>0)) {
  		item.className=((item.className).split('_'))[0];
  		item.style.backgroundColor = old_background;
  	}
  }
  
  function editTT(item) {
 	var addPrebookObj = document.getElementById("addPrebook");
  	if ((item.className).indexOf('_clk')>0) {
  		item.className=((item.className).split('_'))[0];
  		addPrebookObj.value = (addPrebookObj.value).replace(item.id + ';', '');
  	} else {
  		addPrebookObj.value += item.id + ';';
  		item.className=((item.className).split('_'))[0] + "_clk";
  	}
  }
  
  function editTTSubmit() {
  	var navigationObj = document.getElementById("navigation");
    if (navigationObj != null ) {
      navigationObj.value = "Sclose";
      submitRes();
    }
  	
  }
  
  /* on mouse click event on available element, set this element as prebooked or cancel prereservation*/
  function mClick(item, price) {
    var addPrebookObj = document.getElementById("addPrebook");
    var resDiv, count;
  	if ((item.className).indexOf('_clk')>0) {
  		//remove prereservation
  		if ( (price>0) && ((item.className).indexOf("avl")>-1)) {
	  		//item.innerHTML="&nbsp";
	  		old_background = getBrighter(price);
	  		item.style.backgroundColor = old_background;
	  	}
		if ((addPrebookObj!=null) && (addPrebookObj.value.indexOf(item.id)>=0)) {
			//prereservation is not in session
			//item.className=((item.className).split('_'))[0];
			addPrebookObj.value = (addPrebookObj.value).replace(item.id + ';', '');
			/*resDiv = document.getElementById('unConfirmedReservationCount');
			if (resDiv!=null) {
				count = Number(resDiv.innerHTML) - 1;
				if (count>=0) {
					resDiv.innerHTML=String(count);
				}
			}*/
			submitRes(); //direct submit
		} else {
			//prereservation is in session
			var removePrebookObj = document.getElementById("removePrebook");
      		if (removePrebookObj!=null) {
      			removePrebookObj.value = removePrebookObj.value + item.id + ';';
      			submitRes();
			}
		}
	} else if (addPrebookObj!=null) {
		//add prereservation
		//item.className=((item.className).split('_'))[0] + "_clk";
		addPrebookObj.value = addPrebookObj.value + item.id + ';';
		/*resDiv = document.getElementById('unConfirmedReservationCount');
		if (resDiv!=null) {
			count = Number(resDiv.innerHTML)+1;
			resDiv.innerHTML=String(count);
		}*/
		/*if ( (price>0) && ((item.className).indexOf("avl")>-1) ) {
	        //show price in td
    	   // item.innerHTML=price+",-";
        	old_background = item.style.backgroundColor;
        	item.style.backgroundColor = "";
		}*/
		submitRes(); //direct submit
	}
  }
  
  function mInfo(item) {
  	var navigationObj = document.getElementById("navigation");
    if (navigationObj != null ) {
      navigationObj.value = "Sdetail:"+item.id;
      submitRes();  	
    }
  }
  
  function reserve() {
    var navigationObj = document.getElementById("navigation");
    if (navigationObj != null ) {
      navigationObj.value = "reserve";
      submitRes();  	
    }
  }
  
  //add new presubstitution
  function sub(item) {
  	var navigationObj = document.getElementById("navigation");
  	if ((substituteDlg()) && (navigationObj != null) ) {
      navigationObj.value = "Ssubstitute:"+item.id;
      submitRes();  	
    }
  }
  
  //remove presubstitution
  function rSub(item) {
  	var navigationObj = document.getElementById("navigation");
  	if (navigationObj != null ) {
      navigationObj.value = "Sremovesubstitute:"+item.id;
      submitRes();  	
    }
  }

  function seasonReserve(item) {
    var navigationObj = document.getElementById("navigation");
    var addPrebookObj = document.getElementById("addPrebook");
    if ((navigationObj != null ) && (addPrebookObj != null )) {
      addPrebookObj.value = 'S' + item.id + ';';
      navigationObj.value = "reservationSeason";
      //'S' have to be set! (tapestry string value binding)
      submitRes();  	
    }
  }
  
  function navigate(day) {
    var navigationObj = document.getElementById("navigation");
    if (navigationObj != null ) {
      navigationObj.value = day;
      submitRes();  	
    } /*else {
    	//window.alert("navigationObj is null");
    	return;
    }*/
  }

  /* COLORING FUNCTIONS */
  function pre_00(d_) {
	if (d_.length==1){ d_="0"+d_};
	return d_
  }
  
  /*get color derived from color with y brightness*/
  function brigh_(color,y) {
    var colo_r = parseInt(color.substr(1,2),16);
    var colo_g = parseInt(color.substr(3,2),16);
    var colo_b = parseInt(color.substr(5,2),16);	
    if(y>128) {
		  y=y-128
		  colo_r=colo_r-y*(colo_r-0)/128
		  colo_g=colo_g-y*(colo_g-0)/128
		  colo_b=colo_b-y*(colo_b-0)/128
	 } else {
  		y=128-y
  		colo_r=colo_r-y*(colo_r-255)/128
  		colo_g=colo_g-y*(colo_g-255)/128
  		colo_b=colo_b-y*(colo_b-255)/128
	 }
	 colo_r=pre_00((Math.floor(colo_r).toString(16)))
	 colo_g=pre_00((Math.floor(colo_g).toString(16)))
	 colo_b=pre_00((Math.floor(colo_b).toString(16)))
	 return "#"+colo_r+colo_g+colo_b
  }

  function getBrighter(price) {
    return brigh_(avl_color, Math.round((32+price*96/maximal_price)));
  }

  function getBrighterBKD(price) {
    return brigh_(bkd_color, Math.round((64+price*64/maximal_price)));
  }
 
  
  /* DISABLE TEXT SELECTION */
  //works only in IE5+, for MOZILLA see css
  function disableSelection() {
  	var element = document.getElementById("timetable");
  	if (element!=null) {
  		element.ondrag = function () { return false; };
  		element.onselectstart = function () { return false; };
  	}
  }
