/* SKRYPTY DO HOROSKOPU */

var citiesReady = false;
function cityOptionAjax(){
	if(!citiesReady)
		{
			AjaxDisplay("ajaxcities",'action/city_show.php');
			citiesReady = true;
		}
}

function iniContent(state){
	/* 	włacza się jako ONLOAD przy wyświetlaniu horoskopów
		state = 0 gdy wychodzi na ekran wpisywanie
		state = 1 gdy horoskop
	*/
	if (state==1){
		show(3);
	} else {
		show(1);
	}
	cityOptionAjax();
}


function getNow()
	{
		var dnow = new Date();
		//dnow.getTimezoneOffset() 	//Returns the time difference between GMT and local time, in minutes
		var form = document.getElementById("edit_horodata");
		
		form.elements['date_year'].value = dnow.getFullYear(); 	//Returns the year (four digits)
		form.elements['date_month'].value = dnow.getMonth()+1; 	//Returns the month (from 0-11)
		form.elements['date_day'].value = dnow.getDate(); 	//Returns the day of the month (from 1-31)
		form.elements['time_hour'].value = dnow.getHours(); 	//Returns the hour (from 0-23)
		form.elements['time_min'].value = dnow.getMinutes(); 	//Returns the minutes (from 0-59)
		form.elements['time_sec'].value = dnow.getSeconds(); 	//Returns the seconds (from 0-59)
		/* form.elements['zone_hour'].value = 2;
		form.elements['zone_min'].value = 0;
		form.elements['zone_sec'].value = 1; */
		form.elements['label'].value = 'Teraz '+dnow.toDateString();
		var cnn = new String(form.elements['citynow_name'].value);
		if (cnn.length>0){
			form.elements['city_name'].value = form.elements['citynow_name'].value;
			form.elements['latitude'].value = form.elements['citynow_lat'].value;
			form.elements['longitude'].value = form.elements['citynow_long'].value;
		} else {
			form.elements['city_name'].value = "Warszawa";
			form.elements['latitude'].value = 52.226;
			form.elements['longitude'].value = 21.022;
		}
		timeZoneWarsaw();
		show(1);
	}
	
function InputStringReady(){
	var form = document.getElementById("edit_horodata");
	form.elements['inputString'].value = ajaxInput();
}

function ParseEmptyAsZero(astring){
		var result=0;
		while(astring.charAt(0)=='0' || astring.charAt(0)==' '){
			// pętla obcina początkowe zera i spacje
			astring=astring.slice(1);
		}
		if (astring!="") result=parseInt(astring);
		return result;
	} 

function ajaxInput()
	{
		// zbiera i formuje liczby z pól tekstowych
		var form = document.getElementById("edit_horodata");
		
		var ajaxInputString = '?horoid='+form.elements['horoid'].value;
		var label = form.elements['label'].value;
		ajaxInputString += '&label='+label;
		
		var year = ParseEmptyAsZero(form.elements['date_year'].value);
		var month = ParseEmptyAsZero(form.elements['date_month'].value);
		var day = ParseEmptyAsZero(form.elements['date_day'].value);
		var hour = ParseEmptyAsZero(form.elements['time_hour'].value);
		var min = ParseEmptyAsZero(form.elements['time_min'].value);
		var sec = ParseEmptyAsZero(form.elements['time_sec'].value);
		var zhour = ParseEmptyAsZero(form.elements['zone_hour'].value);
		var zmin = ParseEmptyAsZero(form.elements['zone_min'].value);
		var zsec = ParseEmptyAsZero(form.elements['zone_sec'].value);
		var timestr = year+';'+month+';'+day+';'+hour+';'+min+';'+sec+';'+zhour+';'+zmin+';'+zsec;
		
		ajaxInputString += '&timestr='+timestr;
		form.elements['timestr'].value = timestr;
		//form.elements['label'].value = timestr;
		
		//document.getElementById("edit_horodata").elements['city_name'].value = timeof;
		//form.elements['city_name'].value = timeof;
		
		var lat = parseFloat(form.elements['latitude'].value);
		form.elements['latitude'].value = lat;
		var lon = parseFloat(form.elements['longitude'].value);
		form.elements['longitude'].value = lon;
		
		ajaxInputString += '&city_name='+form.elements['city_name'].value;
		ajaxInputString += '&latitude='+lat;
		ajaxInputString += '&longitude='+lon;
						
		// form.elements['calendar'].value = '1';
		ajaxInputString += '&calendar='+form.elements['calendar'].value;
		ajaxInputString += '&public='+form.elements['public'].value;
		ajaxInputString += '&category='+form.elements['category'].value;
		ajaxInputString += '&note='+form.elements['note'].value;
		
		return ajaxInputString;
	}
		
function thover(tabid)
	{
		var tab = document.getElementById(tabid);
		if (!(tab.className=="tab-open")){
			tab.className = "tab-hover";
		}
	}
function thout(tabid)
	{
		var tab = document.getElementById(tabid);
		if (!(tab.className=="tab-open")){
			tab.className="tab-ready";
		}
	}
function show(num)
    {
		if(num < 1 || num > 5) num = 1;
		var bodycolor = new Array("#F0F0F0","#F0F0F0","#FFFFFF","#FFFFFF","#FFFFFF")
		var theBody = document.getElementById("theBody");
		var color_inactive = "inherit"; //#FFDAB9
		var color_active = "#FFDEAD";
		
		for(var ii=1;ii<6;ii++){
			
			var tab = document.getElementById("tab"+ii);
			var card = document.getElementById("card"+ii);
			card.style.display = "none"; // zaslepia karty
			if (!(tab.className=="tab-sleep")){
				tab.className = "tab-ready";
				tab.style.borderBottomColor = "#000";
				}
			if (ii==num){
				tab.className = "tab-open";
				tab.style.borderBottomColor = bodycolor[ii-1];
				card.style.display = "block";
				theBody.style.backgroundColor= bodycolor[ii-1];
				}
		}
}

function citySelect(obj){
	var coord = obj.value.split(';');
	var cityInd = obj.selectedIndex;
	if (cityInd>0){
		document.getElementById("latitude").value = coord[0];
		document.getElementById("longitude").value = coord[1];
		document.getElementById("city_name").value = obj.options[cityInd].innerHTML;
		document.getElementById("city_now_id").value = coord[2];
	}
}

// pomocnicza
function pad2zero(astring){
	// przed pojedynczą luczbą wstawia zero
	var retstr;
	var number=ParseEmptyAsZero(astring);
	if (number<10) retstr="0"+number; else retstr=""+number;
	return retstr;
}

function timeZoneWarsaw0(year, month, day, hour, minute){
	// zwraca strefę 1 (zima) lub 2 (lato)
	var result;
	var timenum = parseInt("" + year
				+ pad2zero(month)
				+ pad2zero(day)
				+ pad2zero(hour)
				+ pad2zero(minute));
		/*- wstawka -*/	document.getElementById("zone_test").value = timenum; 
	if (timenum<191508042336 || 202001010000<timenum){
		result=-1; // czyli NIE DOTYCZY
	} else {
		// dopiero tu inicjujemy tablicę
		var timechange = new Array(
			191604302300,
			191610010100,
			191704160200,
			191709170300,
			191804150200,
			191809160300,
			191904150200,
			191909160300,
			194006230200,
			194211020300,
			194303290200,
			194310040300,
			194404030200,
			194410040200,
			194504290000,
			194511010000,
			194604140000,
			194610070300,
			194705040200,
			194710050300,
			194804180200,
			194810030300,
			194904100200,
			194910020300,
			195706020100,
			195709290200,
			195803300100,
			195809280200,
			195905310100,
			195910040200,
			196004030100,
			196010020200,
			196105280100,
			196110010200,
			196205270100,
			196209300200,
			196305260100,
			196309290200,
			196405310100,
			196409270200,
			197704030100,
			197709250200,
			197804020100,
			197810010200,
			197904010100,
			197909300200,
			198004060100,
			198009280200,
			198103290100,
			198109270200,
			198203280100,
			198209260200,
			198303270100,
			198309250200,
			198403250100,
			198409300200,
			198503310100,
			198509290200,
			198603300100,
			198609280200,
			198703290100,
			198709270200,
			198803270200,
			198809250300,
			198903260200,
			198909240300,
			199003250200,
			199009300300,
			199103310200,
			199109290300,
			199203290200,
			199209270300,
			199303280200,
			199309260300,
			199403270200,
			199409250300,
			199503260200,
			199509240300,
			199603310200,
			199610270300,
			199703300200,
			199710260300,
			199803290200,
			199810250300,
			199903280200,
			199910310300,
			200003260200,
			200010290300,
			200103250200,
			200110280300,
			200203310200,
			200210270300,
			200303300200,
			200310260300,
			200403280200,
			200410310300,
			200503270200,
			200510300300,
			200603260200,
			200610290300,
			200703250200,
			200710280300,
			200803300200,
			200810260300,
			200903290200,
			200910250300,
			201003280200,
			201010310300,
			201103270200,
			201110300300,
			201203250200,
			201210280300,
			201303310200,
			201310270300,
			201403300200,
			201410260300,
			201503290200,
			201510250300,
			201603270200,
			201610300300,
			201703260200,
			201710290300,
			201803250200,
			201810280300,
			201903310200,
			201910270300);
		
		var summer = 0;
		for (ii in timechange){
			// nieparzyste: LATO summer=1 --------------- parzyste ZIMA summer=0
			/*- wstawka -*/	document.getElementById("zone_test").value = timechange[ii]; 
			if (timechange[ii] > timenum){
				result = summer+1;
				break;
			} else {
				summer++; if (summer==2) summer = 0;
			}
		}
	}
	return result;
}

function timeZoneWarsaw(){
	// działa bezpośrednio w HTML rysowania kosmogramu
	var zonew = timeZoneWarsaw0(
		document.getElementById("date_year").value,
		document.getElementById("date_month").value,
		document.getElementById("date_day").value,
		document.getElementById("time_hour").value,
		document.getElementById("time_min").value);
	var alertcolor;
	if (zonew==-1) { alertcolor="#FF4040"; } else { alertcolor="#FFFFFF"; }
	document.getElementById("zone_hour").style.backgroundColor=alertcolor;
	document.getElementById("zone_hour").value = zonew;
	document.getElementById("zone_min").value = 0; 
	document.getElementById("zone_sec").value = 0; 
}
