//funkce zakoduje řetězec na hodnoty unicode oddělených tečkou
function String2Unicode_encoding(text) {
   var output_text = ''; 
   for(var i=0; i<text.length; i++) {     
     if (i > 0) { output_text += '.'; }
     output_text += text.charCodeAt(i)       
   }
   return output_text;
}

//------------------------------------------------------------------------------------------------------
// AJAX - NASTAVENI OBSAHU BOXU
// id - hodnota pro predavani nejakeho ID pro potreby boxu
// Parameters - sekvence parametru pro predavani (vetsinou nejak oddelene a nasledne parsovane)
//------------------------------------------------------------------------------------------------------

var httpRequest = false;


function __SetBoxContent(idBox, Url, id, Action, Parameters, SID) {

  var MSXmlVersion = new Array('MSXML2.XMLHttp.6.0','MSXML2.XMLHttp.5.0','MSXML2.XMLHttp.4.0','MSXML2.XMLHttp.3.0','MSXML2.XMLHttp.2.0','Microsoft.XMLHttp'); //různé verze objektu pro IE

	if (window.XMLHttpRequest) {
	
    httpRequest = new XMLHttpRequest();
	
	} else if (window.ActiveXObject) {

     for(i=0;i<MSXmlVersion.length;i++) {   
        try{   
           httpRequest = new ActiveXObject(MSXmlVersion[i]);   
           break;   
        } catch(e) {} 
     }	
	}
     
  if (!httpRequest) {
    alert('Lituji, ale váš prohlížee nedovoluje korektni zobrazit objekt XmlhttpRequest!');
    return false;
  }
	
	httpRequest.open('POST', Url, true);
	httpRequest.onreadystatechange = function () {ProcessRequest(idBox); } ;
	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  httpRequest.send('id='+id+'&Action='+Action+'&Parameters='+Parameters+'&PHPSESSID='+SID);
  
  return true;
}


function ProcessRequest(idBox) {

	if (httpRequest.readyState == 4) {
		
		if(httpRequest.status == 200) {
			
			if(document.getElementById('Spinner') != null)
				document.getElementById('Spinner').style.visibility = 'hidden';

			document.getElementById(idBox).style.background = 'none';
			document.getElementById(idBox).innerHTML = httpRequest.responseText;
		} else {
			alert("Chyba pri nacitani stanky ("+ httpRequest.status +"): "+ httpRequest.statusText + "!");
		}
	}
}


function $(v) { return(document.getElementById(v)); }

function agent(v) {return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }

function xy(e,v) { return(v?(agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:e.pageX)); }

function dragOBJ(d,e) {

    function drag(e) {if(!stop){d.style.top=(tX=xy(e,1)+oY-eY+'px'); d.style.left=(tY=xy(e)+oX-eX+'px');}}

    var oX=parseInt(d.style.left),oY=parseInt(d.style.top),eX=xy(e),eY=xy(e,1),tX,tY,stop;

    document.onmousemove=drag; 
    document.onmouseup=function() { 
        stop=1;
        document.onmousemove=''; 
        document.onmouseup=''; 
        if(d.style.top <= '0')
         d.style.top = '10px'; 
        if(parseInt(d.style.left) + parseInt(d.style.width) <= 10) d.style.left = (parseInt(d.style.width) * -1) + 20 + 'px';
    };
}


function Spinner(Status) {
	
	if(Status == 'On' && document.getElementById('Spinner') != null)
		document.getElementById('Spinner').style.visibility = 'visible';
	
	if(Status == 'Off' && document.getElementById('Spinner') != null)
		document.getElementById('Spinner').style.visibility = 'hidden';
		
}

// Přidá element newElementId do elementu parentElementId
function AddElement(newElementId, parentElementId) {
	
	if(document.getElementById(parentElementId) == null)
		return false;
	
	var ni = document.getElementById(parentElementId);
	var newdiv = document.createElement('div');
	
	newdiv.setAttribute('id', newElementId);
	ni.appendChild(newdiv);
	
	return true;
}


// Odstraní element newElementId z elementu parentElementId
function removeElement(elementId, parentElementId) {
  var d = document.getElementById('TopParentBox');
  var olddiv = document.getElementById(elementId);
  d.removeChild(olddiv);
}


var ActualZIndex = 100;

function setBoxOnTop(idBox) {
	document.getElementById(idBox).style.zIndex = ActualZIndex;
	ActualZIndex++;
}

// Připojí událost k elementu
function attachEvent(source, eventType, handler)
{
	if (source) {
		if (source.addEventListener) {
			source.addEventListener(eventType, handler, false);
			return true;
		}
		else if (source.attachEvent)
			return source.attachEvent('on' + eventType, handler);
	}
	return false;
}

 
// Odpojí událost od elementu
function detachEvent(source, eventType, handler)
{
	if (source) {
		if (source.removeEventListener) {
			source.removeEventListener(eventType, handler, false);
			return true;
		}
		
		else if (source.detachEvent)
			return source.detachEvent('on' + eventType, handler);
	}
	return false;
}

//------------------------------------------------------------------------------------------------------
// AJAX - end
//------------------------------------------------------------------------------------------------------


// Vytvoří DragDrop Box
function CreateDragDropBox(IdBox) {
	
	if(document.getElementById(IdBox) == null) {
		if(AddElement(IdBox, 'TopParentBox') == false)
			return false;
	} else
		return false;
	
	if(document.getElementById(IdBox + 'Slave') == null) {
		if(AddElement(IdBox + 'Slave', IdBox) == false)
			return false;
	} else
		return false;

	attachEvent(document.getElementById(IdBox), 'mousedown', function() {setBoxOnTop(IdBox)});
	
	return true;
}

//------------------------------------------------------------------------------------------------------
// AJAX - kalendář
//------------------------------------------------------------------------------------------------------


// DragDrop Box pro kalendář
function CalendarBox(idTag) {

	if (CreateDragDropBox('CalendarBox') == true) {
		
            var CalendarBox = document.getElementById('CalendarBox');
	
            CalendarBox.className = "DragDropBox";	
            CalendarBox.style.width = '250px';
            CalendarBox.style.height = '290px';
            CalendarBox.style.left = parseInt((document.body.clientWidth - parseInt(CalendarBox.style.width)) / 2) + 'px';
            CalendarBox.style.top = (parseInt(document.body.scrollTop) + parseInt((document.body.clientHeight - parseInt(CalendarBox.style.height)) / 2)) + 'px';	
            CalendarBox.style.visibilty = 'visible';
            CalendarBox.style.backgroundColor = '#C0C0C0';
            CalendarBox.style.backgroundRepeat = 'no-repeat'; 
            CalendarBox.style.backgroundPosition = 'center center'; 					
            CalendarBox.style.backgroundImage = "url('img/Spinner4.gif')";
       		   	
	} else 
		var CalendarBox = document.getElementById('CalendarBox');
		
	CalendarBox.style.zIndex = ActualZIndex;
	ActualZIndex++;
		
	__SetBoxContent('CalendarBoxSlave', 'kalendar-ajax.php?idTagWithDate=' + idTag.name + '&CalendarDefaultDate=' + idTag.value, 0, 'none', 'none', 'none');				
}


function CalendarBoxChange(CalendarDay, CalendarMonth, CalendarYear, idTagWithDate) {

  document.getElementById('CalendarBox').style.backgroundImage = 'none';  
  document.getElementById('CalendarBoxList').innerHTML = "<div align=\"center\" style=\"margin-top:90px;\"><img src=\"img/Spinner4.gif\" width=\"24\" height=\"24\" border=\"0\"></div>";

  __SetBoxContent('CalendarBoxList', 'kalendar-detail-ajax.php?CalendarDay=' + CalendarDay + '&CalendarMonth=' + CalendarMonth + '&CalendarYear=' + CalendarYear + '&idTagWithDate=' + idTagWithDate, 0, 'none', 'none','none');

}

function CalendarBoxChangeDay(CalendarDayOld, CalendarDayNew) {

  day1 = 'CalendarDay' + CalendarDayOld.toString();
  day2 = 'CalendarDay' + CalendarDayNew.toString();
  
  document.getElementById(day1).style.backgroundColor = 'silver';
  document.getElementById(day2).style.backgroundColor = '#CFCFCF';
  document.getElementById('CalendarDay').value = CalendarDayNew; 
}

function CalendarBoxOK(idTagWithDate) {

   SetDate = document.getElementById('CalendarDay').value + '.' + document.getElementById('CalendarMonth').value + '.' + document.getElementById('CalendarYear').value;
      
   if (document.getElementById(idTagWithDate) != null) {  
      document.getElementById(idTagWithDate).value = SetDate; 
   }
   
   removeElement('CalendarBox', 'TopParentBox');
}

//------------------------------------------------------------------------------------------------------
// AJAX - aukce
//------------------------------------------------------------------------------------------------------

// DragDrop Box pro aukce
function AuctionBox(iduser, idakce, idtermin, idobjekt, cenatag) {

	if (CreateDragDropBox('AuctionBox') == true) {
		
            var AuctionBox = document.getElementById('AuctionBox');
	
            AuctionBox.className = "DragDropBox";	
            AuctionBox.style.width = '500px';
            AuctionBox.style.height = '283px';
            AuctionBox.style.left = parseInt((document.body.clientWidth - parseInt(AuctionBox.style.width)) / 2) + 'px';
            AuctionBox.style.top = (parseInt(document.body.scrollTop) + parseInt((document.body.clientHeight - parseInt(AuctionBox.style.height)) / 2)) + 'px';	
            AuctionBox.style.visibilty = 'visible';
            AuctionBox.style.backgroundColor = '#C0C0C0';
            AuctionBox.style.backgroundRepeat = 'no-repeat'; 
            AuctionBox.style.backgroundPosition = 'center center'; 					
            AuctionBox.style.backgroundImage = "url('img/Spinner4.gif')";
       		   	
	} else 
		var AuctionBox = document.getElementById('AuctionBox');
		
	AuctionBox.style.zIndex = ActualZIndex;
	ActualZIndex++;
		
	__SetBoxContent('AuctionBoxSlave', 'aukce-ajax.php?iduser=' + iduser+ '&idtermin=' + idtermin + '&idobjekt=' + idobjekt + '&cenatag=' + cenatag, idakce, 'none', 'none', 'none');				
}

function AuctionBoxAdd(iduser, idakce, idtermin, idobjekt, prihoz, cenatag) {

  document.getElementById('AuctionBox').style.backgroundImage = 'none';  
  document.getElementById('AuctionBoxList').innerHTML = "<div align=\"center\" style=\"margin-top:65px;\"><img src=\"img/Spinner4.gif\" width=\"24\" height=\"24\" border=\"0\"></div>";
  __SetBoxContent('AuctionBoxList', 'aukce-detail-ajax.php?iduser=' + iduser+ '&idtermin=' + idtermin + '&idobjekt=' + idobjekt + '&prihoz=' + prihoz + '&cenatag=' + cenatag, idakce, 'none', 'none','none');

  cena = parseInt(document.getElementById(cenatag).innerHTML) + parseInt(prihoz);    
  document.getElementById(cenatag).innerHTML = cena;
}

//------------------------------------------------------------------------------------------------------
// AJAX - rezervace
//------------------------------------------------------------------------------------------------------

// DragDrop Box pro rezervace
function ReservationBox(iduser, idtermin, idbyt, info, idobjekt, stavtag, resbutton) {

	if (CreateDragDropBox('ReservationBox') == true) {
		
            var ReservationBox = document.getElementById('ReservationBox');
	
            ReservationBox.className = "DragDropBox";	
            ReservationBox.style.width = '500px';
            ReservationBox.style.height = '220px';
            ReservationBox.style.left = parseInt((document.body.clientWidth - parseInt(ReservationBox.style.width)) / 2) + 'px';
            ReservationBox.style.top = (parseInt(document.body.scrollTop) + parseInt((document.body.clientHeight - parseInt(ReservationBox.style.height)) / 2)) + 'px';	
            ReservationBox.style.visibilty = 'visible';
            ReservationBox.style.backgroundColor = '#C0C0C0';
            ReservationBox.style.backgroundRepeat = 'no-repeat'; 
            ReservationBox.style.backgroundPosition = 'center center'; 					
            ReservationBox.style.backgroundImage = "url('img/Spinner4.gif')";
       		   	
	} else 
		var ReservationBox = document.getElementById('ReservationBox');
		
	ReservationBox.style.zIndex = ActualZIndex;
	ActualZIndex++;
		
	__SetBoxContent('ReservationBoxSlave', 'rezervace-ajax.php?iduser=' + iduser + '&idtermin=' + idtermin + '&info=' + info + '&idobjekt=' + idobjekt + '&stavtag=' + stavtag + '&resbutton=' + resbutton, idbyt, 'none', 'none', 'none');				
}

//přidání rezervace
function ReservationBoxAdd(iduser, idtermin, idbyt, info, idobjekt, stavtag, komentar, rezervace, resbutton) {

  document.getElementById('ReservationBox').style.backgroundImage = 'none';  
  document.getElementById('ReservationBoxList').innerHTML = "<div align=\"center\" style=\"margin-top:75px;\"><img src=\"img/Spinner4.gif\" width=\"24\" height=\"24\" border=\"0\"></div>";
  __SetBoxContent('ReservationBoxList', 'rezervace-detail-ajax.php?iduser=' + iduser+ '&idtermin=' + idtermin + '&idobjekt=' + idobjekt + '&info=' + info + '&stavtag=' + stavtag + '&rezervace=' + rezervace + '&resbutton=' + resbutton, idbyt, 'none', String2Unicode_encoding(komentar),'none');

  if (document.getElementById(stavtag) != null) { document.getElementById(stavtag).innerHTML = 'Zamluvený'; }  
  if (document.getElementById(resbutton) != null) { document.getElementById(resbutton).value = 'Zamluvený'; }  
}

//odebrání rezervace
function ReservationBoxRemove(iduser, idtermin, idbyt, info, idobjekt, stavtag, komentar, rezervace, resbutton) {

  document.getElementById('ReservationBox').style.backgroundImage = 'none';  
  document.getElementById('ReservationBoxList').innerHTML = "<div align=\"center\" style=\"margin-top:75px;\"><img src=\"img/Spinner4.gif\" width=\"24\" height=\"24\" border=\"0\"></div>";
  __SetBoxContent('ReservationBoxList', 'rezervace-detail-ajax.php?iduser=' + iduser+ '&idtermin=' + idtermin + '&idobjekt=' + idobjekt + '&info=' + info + '&stavtag=' + stavtag + '&rezervace=' + rezervace + '&resbutton=' + resbutton, idbyt, 'none', String2Unicode_encoding(komentar),'none');

  if (document.getElementById(stavtag) != null) { document.getElementById(stavtag).innerHTML = 'Volný'; }
  if (document.getElementById(resbutton) != null) { document.getElementById(resbutton).value = 'Volný'; }
}

//uložení poznámky
function ReservationBoxCommentSave(iduser, idtermin, idbyt, info, idobjekt, stavtag, komentar, rezervace, resbutton) {
  document.getElementById('ReservationBox').style.backgroundImage = 'none';  
  document.getElementById('ReservationBoxList').innerHTML = "<div align=\"center\" style=\"margin-top:75px;\"><img src=\"img/Spinner4.gif\" width=\"24\" height=\"24\" border=\"0\"></div>";
  __SetBoxContent('ReservationBoxList', 'rezervace-detail-ajax.php?iduser=' + iduser+ '&idtermin=' + idtermin + '&idobjekt=' + idobjekt + '&info=' + info + '&stavtag=' + stavtag + '&rezervace=' + rezervace + '&resbutton=' + resbutton, idbyt, 'none', String2Unicode_encoding(komentar),'none');
}

