/*	TEST EXISTENCE PROMENNE
if ( typeof( window[ 'foo' ] ) != "undefined" ) {
   document.write("foo is defined")
   } 
*/

function cia_upd(url, id_inp){
	try
	{
		var input = id(id_inp);
//		if(input.value) //UDELAT TEST NA CISLO ---------------------------------------------------------
//		alert(document.getElementById(id_inp).value);

		url = url + '?IDENTIF=' + input.id + '&VALUE=' + input.value;
	//	if(domReadyDetection == false) {return false;}; //if dom is not ready reloaded page 
		input.value = '';
		if (!send_xmlhttprequest(cia_upd_resp, 'GET', url)) {return false;}
		return true;
	}
	catch(exception) {}
}

function cia_upd_resp(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
        // aktualizace odpov�d� na z�klad� aktu�ln�ho stavu
		var XData = xmlhttp.responseXML.getElementsByTagName("cia_upd")[0];
		var id_inp = XData.getAttribute('id_item');
		if(typeof(XData) == "undefined" ) {
			alert('chyba, opakujte zapis hodnoty');
			return;
		};
		var input = id(id_inp);
		input.value = XData.firstChild.nodeValue;
		input.style.border = "2px solid green"; 
	}
}

/*
function cia_ax_read_iprg_com(url, com, par_id)
{
	try
	{
		url = url + '?cia_com_select=' + com;
	//	if(domReadyDetection == false) {return false;}; //if dom is not ready reloaded page 
		var select = id(par_id);
		empty(select);

		if (!send_xmlhttprequest(cia_ax_read_iprg_com_resp, 'GET', url)) {return false;}
		return true;
	}
	catch(exception) {}
}

function cia_ax_read_iprg_com_resp(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
		if(!xmlhttp.responseXML.getElementsByTagName("iprg")[0] == null) {
			alert('none'); return;
		};

		var length = xmlhttp.responseXML.getElementsByTagName("iprg").length;
		var select = id('cia_iprg_id');

		for(var i=0; i<length; i++)
		{
			var iprg = xmlhttp.responseXML.getElementsByTagName("iprg")[i].firstChild;
			var option = document.createElement('option');
			select.appendChild(option);
			var textNode = document.createTextNode(iprg.nodeValue);
			option.appendChild(textNode);
		}
	}

}
*/

function unique_requestid() {
            var timestamp = Number(new Date()).toString();
            var random = Math.random() * (Math.random() * 100000 * Math.random() );
            var unique = new String();
            unique = timestamp + random;
            return unique;
}

$(function() {
        $('.edit-data').click(function() {
            $("#dialog2").remove();
            $("#remove-dialog").html('<div id="dialog2"></div>');
            $("#dialog2").load('/povinne-ruceni/index.phtml?dialog=states&state_contract_id='+$(this).attr("id"));
            $("#dialog2").dialog({
                  bgiframe: true,
                  height: 400,
                  width: 500,
                  cache: false,
                  closeOnEscape: false,
                  title:$(this).attr("title"),
                  modal: true,
                    overlay: {
                            backgroundColor: '#000',
                            opacity: 0.5
                    },
                    buttons:
                            {
                            'Uložit': function() {
                            $.ajax({
                                  type: 'POST',
                                  url: '/povinne-ruceni/index.phtml?dialog=states&action=save',
                                  cache: false,
                                  data: $("#state_form").serialize(),
                                  context: this,
                                  success: function(data) { 
                                  	if (data.result){
                                  		$("#"+data.result).html($("#state_form :selected").text());
                                  		$(this).dialog('close'); 
                                  		$(this).remove();
                                  	} else 
                                  		alert('chyba pri ukladani'); 
                                  },
                                  dataType: 'json'
                                });
                            },
                            Cancel: function() {
                                    $(this).dialog('close');
                                    $(this).remove();
                            }
                    }
              });
        })
})

$(function() {
        $('.datepicker').each(function() {
        	$(this).datepicker({
				dayNamesMin: ['Po', 'Út', 'St', 'Čt', 'Pá', 'So', 'Ne'], dayNames:false,
				monthNames:["Leden","Únor","Březen","Duben","Květen","Červen","Červenec","Srpen","Září","Říjen","Listopad","Prosinec"],
				nextText:'Další',prevText:'Předchozí',currentText:'Dnes',dateFormat: 'yy-mm-dd'
			});
        
        })
})