//------------------------------------------------------------------------------------------------------
// 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';
	    
      if(document.getElementById('vykon_kod') != null) {
         document.getElementById('vykon_kod').focus();
      }
      
			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;
}

// DragDrop Box pro kalendář
function CalendarBox(idTag) {

  offset = getScrollXY(); //souřadnice scrolling stránky

	if (CreateDragDropBox('CalendarBox') == true) {
		
      var CalendarBox = document.getElementById('CalendarBox');	
      CalendarBox.className = "DragDropBox";	
      CalendarBox.style.width = '250px';
      CalendarBox.style.height = '310px';
      CalendarBox.style.left = parseInt((document.body.clientWidth - parseInt(CalendarBox.style.width)) / 2) + 'px';
      CalendarBox.style.top = parseInt(offset[1] + parseInt((GetHeight() - 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', 'calendar-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', 'calendar-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');
}

// DragDrop Box pro kalendář
function AjaxBox(ww, hh, path, random) {

  offset = getScrollXY(); //souřadnice scrolling stránky
  
  
  random = Math.ceil(Math.random()*1000000);
    
	box_name = 'AjaxBox' + random;
  
  if (CreateDragDropBox(box_name) == true) {
		
      var AjaxBox = document.getElementById(box_name);
	
      AjaxBox.className = "DragDropBox";	
      AjaxBox.style.width = ww + 'px';
      AjaxBox.style.height = hh + 'px';
      AjaxBox.style.left = parseInt((document.body.clientWidth - parseInt(AjaxBox.style.width)) / 2) + 'px'; 
      AjaxBox.style.top = parseInt(offset[1] + parseInt((GetHeight() - parseInt(AjaxBox.style.height)) / 2)) + 'px';      
      AjaxBox.style.visibilty = 'visible';
      AjaxBox.style.backgroundColor = '#ECECFF';
      AjaxBox.style.backgroundRepeat = 'no-repeat'; 
      AjaxBox.style.backgroundPosition = 'center center'; 					
      AjaxBox.style.backgroundImage = "url('images/spinner.gif')";       		   	
	} else 
		var AjaxBox = document.getElementById(box_name);
		
	AjaxBox.style.zIndex = ActualZIndex;
	ActualZIndex++;
	  	
	__SetBoxContent(box_name +'Slave', 'box-ajax.php?ww=' + ww + '&hh=' + hh + '&path=' + path, random, 'none', 'none', 'none');				
}


