/*****************************************************************************
 *                                                                           *
 *      Aplikační rámec pro vícejazyčné webové aplikace TORNADO         *
 *                                                                           *
 *                            Javascript, AJAX                               *
 *                                                                           *
 *  file name:  app_glob.js                                                  *
 *  encoding:   UTF-8                                                        *
 *  created:    2007-11-01                                                   *
 * ------------------------------------------------------------------------- *
 * written by Karel KUČERA                                                   *
 * 2007                                                                      *
 *                                                                           *
 *****************************************************************************/    

/* All next comments are written in czech */
                                                               
/*****************************************************************************/
function nothing(){
}

/**************************/
function is_MSIE() {
	if (navigator.userAgent.indexOf("MSIE")!= -1) 
	return(1); else return(0); 
}

/**************************/
function jump(where) {
  document.location.href=where;
}

/**************************/
/* Zjistí abs. souřadnice objektu na HTML stránce
   pType=0 top, pType=1 left */
function findPosition(obj,pType) {
	cur = 0;
	if(obj.offsetParent) {		
		while(obj.offsetParent) {
			cur+=pType?obj.offsetLeft:obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	return cur;
}

/**************************/

function setPosition(id,X,Y) {
	document.getElementById(id).style.top = Y+"px"; 
	document.getElementById(id).style.left = X+"px";
}        

/**************************/

function hide_window(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.top = "-2000px";
		document.getElementById(id).style.left = "-2000px";
	}
}

/**************************/
/*
id - identifikator okna indexu 
line_id - identifikator radky, ke ktere index patri
*/
function show_index(id, line_id) {
	var ln = document.getElementById(line_id); 
	var newY = findPosition(ln,0) + ln.offsetHeight + 3; 	
	var newX = findPosition(ln,1);  
	document.getElementById(id).style.top = newY+"px"; 
	document.getElementById(id).style.left = newX+"px";
}

/**************************/
function loadImg(imgName) {
  var img=new Image();
  img.src=imgName;
  return img;
}

/**************************/
function displayError(where,msg) {
	document.getElementById(where).innerHTML = msg;
}

/**************************/
function displayText(where,msg) {
	document.getElementById(where).innerHTML = msg;
} 

/*********************************/
/*                               */
/*           A J A X             */
/*                               */
/*********************************/

var index_element_id;

var current_index_field_num;
var current_index_edit_id;
var current_index_edit_value;
var current_index_language;
/* KK 8.2.2010 - nastaveni indexu podle polohy kurzoru na radce */
var current_index_edit_caret_pos;

var xmlHttpEnabled = false; // Promnn uruje, zda je mono pout xmlHttpRequestObject

// Inicializace XML HTTP Request objektu 
var xmlHttp = null;  //createXmlHttpRequestObject(); !!! pro IE7 a pravdepodobne jeste za urcitych jinych okolnosti musi vytvoreni objektu probehnout pri kazdem volani call_index !!!
 
/**************************/
function createXmlHttpRequestObject() {
  var xmlHttp1;
  try {                 
    xmlHttp1 = new XMLHttpRequest();
  }
  catch(E) {            
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp1; i++) {
      try { 
        xmlHttp1 = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }    
  }
  if (!xmlHttp1) xmlHttpEnabled = false;
  else {                                             
  	xmlHttpEnabled = true;
    return xmlHttp1;
  }
}  

/**************************/
/* 
		call_index vola funkce indexu, ktery je typicky umisten v elementu <div>. Parametry funkce jsou:
		sessId		-	identifikator Tornado session - v aplikaci zpravidla promenna $link
		script		- cele URL skriptu, ktery pracuje s indexem (muze to byt i hlavni skript aplikace nebo samostatny skript)
		request		- pozadavek na index
				index_open
				index_prev
				index_next
				index_home
				index_end
				index_word
				index_entry
		edit_id		- identifikator (id) editacni radky, ke ktere index patri
		edit_value- obsah editacni radky
		field_num	- cislo pole (od 0) pro index
		index_id	- identifikator objektu (<div>), do ktereho se vypise obsah indexu
		lang			- jazykova verze		
KK 8.2.2010 - nastaveni indexu podle kurzoru v editacni radce
		edit_caret_pos -  pozice kurzoru v editacni radce
		
*/
//function call_index(sessId, script, request, edit_id, edit_value, field_num, index_id, lang) {  
function call_index(sessId, script, request, edit_id, edit_value, field_num, index_id, lang, edit_caret_pos) {  
	index_element_id = index_id;           
                              
	if (field_num >= 0)	{                            
		current_index_field_num = field_num;
		current_index_edit_id = edit_id;
		current_index_edit_value = edit_value;
    current_index_language = lang; 
/* KK 8.2.2010 - nastaveni indexu podle posice kurzoru v editacni radce, pro zpetnou kompatibilitu, neni-li definovana posice, nastavuje se -1, potom funguje jako doposud */
    if (edit_caret_pos==undefined)
      current_index_edit_caret_pos = -1; 
    else
    	current_index_edit_caret_pos = edit_caret_pos;  
	}
                                          
	xmlHttp = createXmlHttpRequestObject();
	
	if (xmlHttpEnabled) {                  
		try {   
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {      
       	xmlHttp.onreadystatechange = write_index;                  // Priradi funkci pro obsluhu odezvy      				
// KK 8.2.2010  params = "sessionId=" + sessId + "&request=" + request + "&idx_field=" + current_index_field_num + "&edit_id=" + current_index_edit_id + "&value=" + current_index_edit_value + "&language="+current_index_language; 
        params = "sessionId=" + sessId + "&request=" + request + "&idx_field=" + current_index_field_num + "&edit_id=" + current_index_edit_id + "&value=" + current_index_edit_value + "&language="+current_index_language + "&caret="+current_index_edit_caret_pos; 
        xmlHttp.open("POST", script, true);    												
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(params);
      } else {   
// KK 8.2.2010 setTimeout("call_index('" + sessId + "', '" + script + "', '" + request + "', '" + current_index_edit_id + "', '" + current_index_edit_value + "', '" + current_index_field_num + "', '" + index_id + "', '" + current_index_language + "');", 1000); 
   	  	setTimeout("call_index('" + sessId + "', '" + script + "', '" + request + "', '" + current_index_edit_id + "', '" + current_index_edit_value + "', '" + current_index_field_num + "', '" + index_id + "', '" + current_index_language + "', '" + current_index_edit_caret_pos + "');", 1000); 
   	  }
    }
    catch (e) {
      displayError(index_id, e.toString());
    }
	} else displayError(index_id,'<p>Pro tento typ prohlizece je index bohuzel nedostupny</p>');		
}

/**************************/
function write_index() {       
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {  
		  displayText(index_element_id,xmlHttp.responseText);
		}
	}
}
  
/*********************************/
/*                               */
/*           I N D E X           */
/*                               */
/*********************************/

function index_row_click(row_id,edit_id) { 

	var row = document.getElementById(row_id).innerHTML; 
	
	while(row.indexOf("&lt;")>=0)	row = row.replace("&lt;","<");
	while(row.indexOf("&gt;")>=0)	row = row.replace("&gt;",">");
      
	var edit = document.getElementById(edit_id);  
// Prevezmi puvodni obsah	editacni radky
	var tx = edit.value;
// Zjisti, zda text konci mezerou
	if (tx.charAt(tx.length-1) == " ") {
// Pokud ano, pridej obsah radky indexu za puvodni obsah editacni radky
		edit.value += row;
	} else {
// pokud je to heslovy index, stejnym zpusobem musime najit apostrof
		if (tx.charAt(tx.length-1) == "'") {
// najdi predchozi apostrof	
			var p = tx.lastIndexOf("'",tx.length-2);
			if (p	< 0) {
// pokud predchazejici apostrof neni -> najdi posledni mezeru a od ni to prepis
				var p = tx.lastIndexOf(" ",tx.length-2);
				if (p < 0) p = 0; else $p++;
			}					
		 	tx = tx.substr(0,p);
		 	edit.value = tx + row;
		} else {
// v tomto pripade neni posledni znak ani mezera ani apostrof			
// musime tedy najit posici posledni mezery zprava			
			var p = tx.lastIndexOf(" ");
			if (p < 0) {
// zadna mezera neni, -> nahradime obsah editacni radky radkou indexu
				edit.value = row;
			} else {
// Vyjmeme text za mezerou a ke zbytku pridame radku indexu
				tx = tx.substr(0,p+1);
				edit.value = tx + row;
			}
		}
	}
	edit.focus();
}

function change_index(i) {
//	alert('index = '+i);
}