//zn = "Овен       Телец      Близнаци   Рак        Лъв        Дева       Везни      Скорпион   Стрелец	   Козирог    Водолей    Риби";  // Zodiac
zn=new Array(12);
/*zn[0]='Овен';
zn[1]='Телец';
zn[2]='Близнаци';
zn[3]='Рак';
zn[4]='Лъв';
zn[5]='Дева';
zn[6]='Везни';
zn[7]='Скорпион';
zn[8]='Стрелец';
zn[9]='Козирог';
zn[10]='Водолей';
zn[11]='Риби';*/

zn[0]='aries';
zn[1]='taurus';
zn[2]='gemini';
zn[3]='cancer';
zn[4]='leo';
zn[5]='virgo';
zn[6]='libra';
zn[7]='scorpio';
zn[8]='sagittarius';
zn[9]='capricorn';
zn[10]='aquarius';
zn[11]='pisces';

d2r = Math.PI/180;	// degrees to radians
r2d = 180/Math.PI;	// radians to degrees
var range = [1,12,1,31,1800,2100,0,23,0,59,0,12,0,59,0,0,0,179,0,59,0,0,0,89,0,59];

function calculate()
{
	if($('more_info')){
		$('more_info').update('');
	}

	with(Math){
		var mon = floor(document.AscForm.Month.value);
		var day = floor(document.AscForm.Day.value);
		var year= floor(document.AscForm.Year.value);
		//var hr= floor(document.AscForm.Hour.value);
		var hr= floor($('hour').value);
		
		hr	+= floor(document.AscForm.Min.value)/60;
		
		
/*
		if($('manual').checked)
		{
			var tz= floor(document.AscForm.ZHour.value);
			tz += floor(document.AscForm.ZMin.value)/60;
		}
		else
		{
			var tz = floor(2);
		}
*/
		var tz = floor(2);
/*
		if($('manual').checked)
		{
			var LonDeg = document.AscForm.LonDeg.value;
			var LonMin = document.AscForm.LonMin.value;
			var LatDeg = document.AscForm.LatDeg.value;
			var LatMin = document.AscForm.LatMin.value;
		}
		else
		{
			var coords = $F('town').split('x');
			var LonDeg = coords[2];
			var LonMin = coords[3];
			var LatDeg = coords[0];
			var LatMin = coords[1];
		}
*/
		var coords = $F('town').split('x');
	
		var LonDeg = coords[2];
		var LonMin = coords[3];
		var LatDeg = coords[0];
		var LatMin = coords[1];

		var ln= floor(LonDeg);
		ln += floor(LonMin)/60;
		var la= floor(LatDeg);
		la += floor(LatMin)/60;
	}
	// checks for checked DST, East, South
	//var dst = document.AscForm.DST;
	var eln = document.AscForm.East;
	var sla = document.AscForm.South;

/*
	if(!$('manual').checked)
	{
		var eln_man = true;
	}
	*/
	var eln_man = true;

	if(eln.checked || eln_man)ln = -ln;
	if(sla.checked)la = -la;

	if(year>=1979 && mon >= 4 && mon <=9){
		if(ln < 0.0)tz++;
		else tz--;
	}

	/*
	if(dst.checked){
		if(ln < 0.0)tz++;
		else tz--;
	}
*/
	jd = mdy2julian(mon,day,year);
	if(ln < 0.0)f = hr - tz;
	else f = hr+tz;

	t = ((jd - 2415020) + f/24 - 0.5)/36525;
	ay = calcAyanamsa(t);
 	ra = (((6.6460656 + 2400.0513 * t + 2.58e-5 * t * t + f) * 15 - ln) % 360) * d2r; // RAMC
	ob = (23.452294 - 0.0130125 * t) * d2r; // Obliquity of Ecliptic
	with(Math){
		// Calculate Midheaven
		mc = atan2(tan(ra),cos(ob));
		if(mc < 0.0)mc += PI;
		if(sin(ra) < 0.0)mc += PI
		mc *= r2d;

		// Calculate Ascendant
		as = atan2(cos(ra),-sin(ra)*cos(ob)-tan(la * d2r)*sin(ob));
		if(as < 0.0)as += PI;
		if(cos(ra) < 0.0)as += PI;
		as *= r2d % 360.0;
	}
	// add Ayanamsa
	as += ay; mc += ay;

	if($('town').value==0){
		$('warning').show();
		$('your_asc').hide();
	}
	else{
		document.location='ascendents/sign/'+lon2dmsz(as) ;
		/*$('more_info').insert(lon2dmsz(as));
		$('warning').hide();
		$('your_asc').show();*/
	}

	//document.AscForm.Asc.value = lon2dmsz(as);
}

//============vlado


function calculate_new(month,day,year,hour,minute,city)
{

	mymon = $(month).options[$(month).selectedIndex].value;
	myday = $(day).options[$(day).selectedIndex].value;
	myyear = $(year).options[$(year).selectedIndex].value;
	myhr = $(hour).options[$(hour).selectedIndex].value;
	mymin = $(minute).options[$(minute).selectedIndex].value;
	mytown = $(city).options[$(city).selectedIndex].value;




	with(Math){
		var mon = floor(mymon);
		var day = floor(myday);
		var year= floor(myyear);
		var hr= floor(myhr);
		//alert(mon+"@"+day + "@" + year + "@" + hr + "@" + mymin);
		hr	+= floor(mymin)/60;
/*
		if($('manual').checked)
		{
			var tz= floor(document.AscForm.ZHour.value);
			tz += floor(document.AscForm.ZMin.value)/60;
		}
		else
		{
			var tz = floor(2);
		}
*/
		var tz = floor(2);
/*
		if($('manual').checked)
		{
			var LonDeg = document.AscForm.LonDeg.value;
			var LonMin = document.AscForm.LonMin.value;
			var LatDeg = document.AscForm.LatDeg.value;
			var LatMin = document.AscForm.LatMin.value;
		}
		else
		{
			var coords = $F('town').split('x');
			var LonDeg = coords[2];
			var LonMin = coords[3];
			var LatDeg = coords[0];
			var LatMin = coords[1];
		}
*/
		var coords = mytown.split('x');//$F('town');
		//alert(coords);
		var LonDeg = coords[2];
		var LonMin = coords[3];
		var LatDeg = coords[0];
		var LatMin = coords[1];

		var ln= floor(LonDeg);
		ln += floor(LonMin)/60;
		var la= floor(LatDeg);
		la += floor(LatMin)/60;
	}
	// checks for checked DST, East, South
	//var dst = document.AscForm.DST;
	var eln = 'East'
	var sla = 'South';

/*
	if(!$('manual').checked)
	{
		var eln_man = true;
	}
	*/
	var eln_man = true;

	if(eln.checked || eln_man)ln = -ln;
	if(sla.checked)la = -la;

	if(year>=1979 && mon >= 4 && mon <=9){
		if(ln < 0.0)tz++;
		else tz--;
	}

	/*
	if(dst.checked){
		if(ln < 0.0)tz++;
		else tz--;
	}
*/
	jd = mdy2julian(mon,day,year);
	if(ln < 0.0)f = hr - tz;
	else f = hr+tz;

	t = ((jd - 2415020) + f/24 - 0.5)/36525;
	ay = calcAyanamsa(t);
 	ra = (((6.6460656 + 2400.0513 * t + 2.58e-5 * t * t + f) * 15 - ln) % 360) * d2r; // RAMC
	ob = (23.452294 - 0.0130125 * t) * d2r; // Obliquity of Ecliptic
	with(Math){
		// Calculate Midheaven
		mc = atan2(tan(ra),cos(ob));
		if(mc < 0.0)mc += PI;
		if(sin(ra) < 0.0)mc += PI
		mc *= r2d;

		// Calculate Ascendant
		as = atan2(cos(ra),-sin(ra)*cos(ob)-tan(la * d2r)*sin(ob));
		if(as < 0.0)as += PI;
		if(cos(ra) < 0.0)as += PI;
		as *= r2d % 360.0;
	}
	// add Ayanamsa
	as += ay; mc += ay;

	
		document.location='ascendents/sign/'+lon2dmsz(as) ;
		

	//document.AscForm.Asc.value = lon2dmsz(as);
}



//====== end vlado
	// calculate bhavas as per
	// Deepak Kapoor's "Astronomy and Mathematical Astrology" - 1997 Ranjan Pub.

function calcAyanamsa(t)
{
	ln = ((933060-6962911*t+7.5*t*t)/3600.0) % 360.0;  /* Mean lunar node */
	Off = (259205536.0*t+2013816.0)/3600.0;             /* Mean Sun        */
	Off = 17.23*Math.sin(d2r * ln)+1.27*Math.sin(d2r * Off)-(5025.64+1.11*t)*t;
	Off = (Off- 80861.27)/3600.0;  // 84038.27 = Fagan-Bradley 80861.27 = Lahiri
	return Off;
}

// calculate Julian Day from Month, Day and Year
function mdy2julian(m,d,y)
{
	with(Math){
		im = 12 * (y + 4800) + m - 3;
		j = (2 * (im - floor(im/12) * 12) + 7 + 365 * im)/12;
		j = floor(j) + d + floor(im/48) - 32083;
		if(j > 2299171)j += floor(im/4800) - floor(im/1200) + 38;
		return j;
	}
}

// build string with degrees, minutes, seconds and zodiac sign from longitude
function lon2dmsz(x)
{
	with(Math){
		var d,m,s;
		x = abs(x);
		d = floor(x);
		m = (x - d);
		s = m * 60;
		m = floor(s);
		s = s - m;
		z = floor(d/30);
		d %= 30;
		//str = zn.substr(z*11,11);
		str = zn[z];
	}
	return str;
}

// write degrees minutes
function writedms(x) {
	with(Math){
		var d,m,s;
		if(x < 0.0)sgn = -1;
		else sgn = 1;
		x = abs(x);
		d = floor(x);
		m = (x - d);
		s = m * 60;
		m = floor(s);
		s = s - m;
		str =  (d*sgn) + "° " + m + "' " + floor(s * 60) + "\"";
	}
	return str;
}

// keep within 360 degrees
function fix360(v)
{
	if(v < 0.0)v += 360;
	if(v > 360)v -= 360;
	return v;
}

Event.observe(window, 'load', function(){
	if($('manual'))
	{
		$('manual').observe('click', function(el){
			$('manual_coords').toggle();
			if($('town').disabled)
			{
				$('town').enable();
			}
			else
			{
				$('town').disable();
			}
		});
	}
});
