var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
function myXMLHttpRequest() {
  var xmlhttplocal;
  try {
    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
    xmlhttplocal=false;
  }
 }

if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
 try {
  var xmlhttplocal = new XMLHttpRequest();
 } catch (e) {
  var xmlhttplocal=false;
  alert('couldn\'t create xmlhttp object');
 }
}
return(xmlhttplocal);
}


function sndRequest(vote,id_num,ip_num, path) {
	/*alert(path);*/
	
	//var element = document.getElementById('unit_long'+id_num);
	//new Effect.Fade(element);
    //element.innerHTML = '<div style="height: 20px;"><em>Nahravam ...</em></div>';
	
    xmlhttp.open('get', path+'?j='+vote+'&q='+id_num+'&t='+ip_num);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
	
}

function handleResponse() {
    if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

	        if(response.indexOf('|') != -1) {
	            update = response.split('|');
	            changeText(update[0], update[1]);
	        }
		}
    }
}

function handleResponse2() {
    
    if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

	        if(response.indexOf('|') != -1) {
	            update = response.split('|');
	            //console.log(update); console.log(xmlhttp);
	            changeText2(update[0], update[1]);
	        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    //alert(div2show);
    //alert(text);
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"

    if (DOM) {
        var viewer = document.getElementById(div2show)
        viewer.innerHTML=text
    }
    else if(IE) {
        document.all[div2show].innerHTML=text
    }
}

function changeText2( div2show, text ) {
    // Detect Browser
    //alert(div2show);
    //alert(text);
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    //console.log(div2show);
    //console.log(text);
    if (DOM) {
        var viewer = document.getElementById(div2show)
        viewer.innerHTML=text+viewer.innerHTML
    }
    else if(IE) {
        document.all[div2show].innerHTML=text+document.all[div2show].innerHTML
    }
}

function createInput(id,value) {
	base_path = "http://" + window.location.host;
	www_path = base_path + window.location.pathname.replace(/admin(.*)/i,"");
	//alert(id);
	//document.write("<tr><td>Počet</td><td>Produkt</td><td>Cena</td></tr><tr><td><input type='text' name='products["+ id +"][count]' size='3'></td><td><select name='products["+ id +"][product_id]'>");
	
	xmlhttp.open('get', www_path + 'admin/LimsEshop/create/option/' + id);
	xmlhttp.onreadystatechange = handleResponse2;
	xmlhttp.send(null);
	
	//document.write("</select></td><td><input type='text' name='products["+ id +"][price]' size='5'></td></tr>");
}

function checkAddon(){
	
	var addon = document.getElementById('page_addon').value;
	
	base_path = "http://" + window.location.host;
	www_path = base_path + window.location.pathname.replace(/admin(.*)/i,"");
	//alert (www_path + 'apps/LimsApp/modules/LimsPages/lib/test.php?addon='+addon);
	//window.open(www_path + 'data/tools/test.php?addon='+addon);
	
	var adresa = location.href;
	
	var pole = adresa.substr(1).split("/")
	
	var count = 0;
	
	for(i=0;i<pole.length;i++){
	    count = count+1;
	}
	
	//alert(pole[count-3]);
	//alert(pole[count-1]);
	
	xmlhttp.open('get', www_path + 'admin/LimsPages/create/' + addon);
	
	xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
    
	document.getElementById('page_addon_params').style.visibility = 'visible';
	
}

function pasteAddon(){
	
	var addon = document.getElementById('page_addon').value;
	
	var content = document.getElementById('use_addons').value;

	
	paste_string = '{%MODUL';
	
	if(addon){
		paste_string = paste_string + '_' + addon;
	}
	
	if(document.getElementById('page_addon_parametr').value){
		paste_string = paste_string + '_' + document.getElementById('page_addon_parametr').value;
	}
	
	paste_string = paste_string + '%}';
	
	if(content)	document.getElementById('use_addons').value = content + '\n' + paste_string;
	else	document.getElementById('use_addons').value = paste_string;
}

function generateLims(){
    
    base_path = "http://" + window.location.host;
    www_path = base_path + window.location.pathname.replace(/admin(.*)/i,"");
    
    xmlhttp.open('get', www_path + 'admin/LimsTemplates/generate');
    
    document.getElementById('gen_1').style.display = 'block';
    document.getElementById('gen_2').style.display = 'block';
    document.getElementById('gen_3').style.display = 'block';
    
    document.getElementById('generate_line').style.visibility = 'visible';
    document.getElementById('generate_line').style.visibility = 'visible';
    document.getElementById('generate_line').style.background = '#AA0000';
    document.getElementById('generate_line').innerHTML = '<table><tr><td><img src="' + www_path + 'lims_img/loading.gif" width="20" height="20"></td><td>Generuji</td></tr></table>';
    setTimeout('', 10000);

    //xmlhttp.onreadystatechange = handleResponse2;
    xmlhttp.send(null);
    
    document.getElementById('generate_line').style.background = '';
    document.getElementById('generate_line').innerHTML = 'Vygenerováno';
}

function sendVote(ip, points, cid, itid){
    
    base_path = "http://" + window.location.host;
    base_path = base_path + window.location.pathname;
    //www_path = base_path + window.location.pathname.replace(/admin(.*)/i,"");
    
    
    cookie = readCookie('soutez_item_' + itid);
    if(cookie)  cookie = parseInt(cookie);

    if(cookie < 3 || cookie == ""){
      var voted = cookie + 1;
      xmlhttp.open('get', base_path + '?ip=' + ip + '&points=' + points + '&cid=' + cid + '&itid=' + itid);
      createCookie('soutez_item_' + itid, voted);
      alert("Váš hlas byl přidán. Děkujeme");
      xmlhttp.send(null);
    }
    else{
        alert("Nemůžete hlasovat pro jednu soutěžní položku vícekrát než 3x denně.");
    }
    
    //alert(base_path + '?ip=' + ip + '&points=' + points + '&cid=' + cid + '&itid=' + itid);
    
    
}

/**  KOLACKY :)  **/

function createCookie(name,value) {

    var date = new Date();
    date.setTime(date.getTime()+(24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    
    document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}