/** odesl�n� XMLHttp po�adavku
@param function obsluha funkce zaji��uj�c� obsluhu p�i zm�n� stavu po�adavku, dostane parametr s XMLHttp objektem
@param string method GET|POST|...
@param string url URL po�adavku
@param string [content] t�lo zpr�vy
@param array [headers] pole p�edan�ch hlavi�ek ve tvaru { 'hlavi�ka': 'obsah' }
@return bool true v p��pad� �sp�chu, false jinak
*/

function send_xmlhttprequest(obsluha, method, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}


function ci_ax_read_town(url, psc, par_id){
	try
	{
		var cinm_zip_inp = id('ci_zip_id').value;
		url = url + '?cinm_zip=' + cinm_zip_inp;
//		alert(url);
	//	if(domReadyDetection == false) {return false;}; //if dom is not ready reloaded page 
		var select = id(par_id);
		empty(select);

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

function ci_ax_read_town_resp(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
        // aktualizace odpov�d� na z�klad� aktu�ln�ho stavu
		var length = xmlhttp.responseXML.getElementsByTagName("town").length;
		var select = id('ci_city_id');

		for(var i=0; i<length; i++)
		{
			var town = xmlhttp.responseXML.getElementsByTagName("town")[i];

			var option = document.createElement('option');
			option.setAttribute("value",town.getAttribute('town_id'));

			select.appendChild(option);
			var textNode = document.createTextNode(town.firstChild.nodeValue);
			option.appendChild(textNode);
		}
	}
}

function ts_birth_busin(a, min, max) {
	var busin = id('cinm_business');
	if(busin.checked) {
		return true;
	}
	else {
		return ts_range(a, min, max);
	}
}

function ts_busin_check() {
	id('cinm_birthdate1').style.border = "1px solid #909090";;
	id('cinm_birthdate2').style.border = "1px solid #909090";;
	id('cinm_birthdate3').style.border = "1px solid #909090";;
	return;
}

function ts_range(a, min, max) {
	a.value=a.value.replace(' ', ''); 
	var aa = a.value.replace(',','.');

	re = new RegExp("^[0-9]+[.,]{0,1}[0-9]*$");
	
	if(re.test(aa) && (aa > min) && (aa <= max)) {
		a.style.border = "1px solid #909090";
		return true;
	}
	a.style.border = "1px solid red";
	a.value = '';
	return false;
}

function popupWindow(url) {
	var imgHeight=600;
	var imgWidth=800;
	
	var height = screen.height;
	var width = screen.width;
	var leftpos = width / 2 - imgWidth / 2;
	var toppos = height / 2 - imgHeight / 2; 
  	
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no, width='+imgWidth+',height='+imgHeight+',screenX=+width+,screenY='+height+',top='+toppos+',left='+leftpos);
}


/*
 * Reminder
 */


var tabIds = ['tab_remind', 'tab_mail'];

function cir_nextObject(obj) {
	var n = obj;
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}

function cir_previousObject(obj) {
	var p = obj;
	do p = p.previousSibling;
	while (p && p.nodeType != 1);
	return p;
}

function cir_addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function cir_activateTab(clickedTab, saved) {
	//for (var tabId in tabIds) {
	  for (var tabId =0;tabId < tabIds.length; tabId++) {
		if (tabIds[tabId] == clickedTab.id) {
			if (saved) {
				document.getElementById(tabIds[tabId] + '_saved').className = '';
			}
			else {
				document.getElementById('cir_submit').value = clickedTab.id;
				document.getElementById(tabIds[tabId] + '_content').className = '';
				document.getElementById(tabIds[tabId] + '_saved').className = 'hidden'; 
			}
			clickedTab.className = 'tab-body';
			cir_previousObject(clickedTab).className = 'tab-left-border';
			cir_nextObject(clickedTab).className = 'tab-right-border';
			if(!ci_in_array(ci_sys_id_web,ci_gr_aktualneCentrum)){
				//u grafiky pro centrum chci, aby byla delici lista *_gap_tr vzdy schovana
				document.getElementById(tabIds[tabId] + '_gap_tr').className = '';
			}
		}
		else {
			document.getElementById(tabIds[tabId] + '_content').className = 'hidden';
			document.getElementById(tabIds[tabId] + '_saved').className = 'hidden';
			document.getElementById(tabIds[tabId]).className = 'button-body';
			cir_previousObject(document.getElementById(tabIds[tabId])).className = 'button-left-border';
			cir_nextObject(document.getElementById(tabIds[tabId])).className = 'button-right-border';
			document.getElementById(tabIds[tabId] + '_gap_tr').className = 'hidden';
		}
	}
	//ovladani pro financni noviny
	//zmena rozku "tab_content_top_right" 
	if(ci_in_array(ci_sys_id_web,ci_gr_financniNoviny)){  
		var tab_content_top_right = document.getElementById('tab_content_top_right');
		//element "tab_content_top_right" existuje zatim jen u financnich novin, proto se podminka provede, jen kdyz tento element existuje, tudis s designem FN
		if(tab_content_top_right){
			if(clickedTab.id == tabIds[0])
				tab_content_top_right.className = 'tab_content_top_right';
			else
				tab_content_top_right.className = 'tab_content_top_right_active';	
		}
  
	} 
	//zmena obrazku
	//u aktualne centrum logiku zmenila externi firma, ktera delala sablonu
	if(!ci_in_array(ci_sys_id_web,ci_gr_aktualneCentrum) && ci_sys_id_web!=37){
		//pokud se jedna o prvni prvek definovany  v poli tabIDs, tak nastav obrazky ..., pokud ne, nastav je naopak
		if(clickedTab.id == tabIds[0]){//tab_remind
			document.getElementById(tabIds[0]+"_img").src = "/images/ci/" + ci_web_path + "ICO_4p.png";	
			document.getElementById(tabIds[1]+"_img").src = "/images/ci/" + ci_web_path + "ICO_5.png";
		}else{ //tab_mail
			document.getElementById(tabIds[0]+"_img").src = "/images/ci/" + ci_web_path + "ICO_4.png";
			document.getElementById(tabIds[1]+"_img").src = "/images/ci/" + ci_web_path + "ICO_5p.png";
		}
	}
}

function cir_init() {
	//for (var tabId in tabIds) {
	for (var tabId =0;tabId < tabIds.length; tabId++) {
		tabTmp = document.getElementById(tabIds[tabId]);
		if (tabTmp){
			tabTmp.onclick = function() {
				cir_activateTab(this);
			}
		}
	}
}

//cir_addLoadEvent(cir_init);
YAHOO.util.Event.onDOMReady(
	function() {
		cir_init();
	}
);
function ci_in_array (needle, haystack) {
	var key = '';
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		} 
	return false;
}