//Mecca Latitude=21+25/60+24/3600 longitude=-39+49/60+24/3600
//Updated 16.1.2004
//Last Updated 21.12.2003
//Based on p3obackupnetversion80-040116.htm
height=0;
L=-(39+49/60+24/3600);
sPhi=21+25/60+24/3600;


function MakeDayPad (Calendar) {
top.Day.document.write('<html>\n<body bgcolor="#000000" text="#FFCC00" link="#FFCC00" vlink="#FFCC00">\n');
top.Day.document.write('<FORM NAME="dmy" method="post">\n');
top.Day.document.write('<br><br>\n');
top.Day.document.write('<div align="center">\n');
top.Day.document.write('  <table border="5" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0">\n');
top.Day.document.write('   <tr>\n');
top.Day.document.write('    <td align="CENTER" colspan="8">\n');
top.Day.document.write('    <center>\n');
top.Day.document.write('    <b><font size="4" color="#000000">Day</font></b>\n');
top.Day.document.write('    </center>\n');
top.Day.document.write('    </td>\n');
top.Day.document.write('   </tr>\n');
top.Day.document.write('   <tr>\n');
top.Day.document.write('    <td align="CENTER" colspan="8">\n');
top.Day.document.write('    <center>\n');
top.Day.document.write('    <input type="text" name="day" size="20" maxlength="18">\n');
top.Day.document.write('    </center>\n');
top.Day.document.write('   </tr>\n');
var rows=1;var number;
while (rows<5){
top.Day.document.write('   <tr>\n');
var cols=1;
while (cols<9){
number=(rows-1)*8+cols;if (number<10) {number='  '+number};
top.Day.document.write('    <td align="CENTER"><font size="3" face="Book Antiqua">\n');
top.Day.document.write('    <center>\n');
if ((rows-1)*8+cols<32) {top.Day.document.write('    <input type="button" value=" '+number+' " onclick="this.form.day.value= \''+((rows-1)*8+cols)+'\'">\n')}
        else {top.Day.document.write('    <input type="button" value="   C " onclick="this.form.day.value=\'\'">\n')};
top.Day.document.write('    </center>\n');
top.Day.document.write('    </font></td>\n');
cols +=1;
};
top.Day.document.write('   </tr>\n');
rows +=1;
};
top.Day.document.write('    </center>\n');
top.Day.document.write('    </font></td>\n');
top.Day.document.write('   </tr>\n');
top.Day.document.write('  </table>\n');
top.Day.document.write(' </div>\n');
top.Day.document.write('</form>\n</body>\n</html>');
top.Day.document.close ();
}

function MHebrewPad () {

top.Month.document.write('<html>\n');
top.Month.document.write('<body bgcolor="#000000" text="#FFCC00" link="#FFCC00" vlink="#FFCC00">\n');
top.Month.document.write('<form name="dmy" method="post">\n');
top.Month.document.write('<br>\n');
top.Month.document.write('   <center>\n');
top.Month.document.write('   <table border="5" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0">\n');
top.Month.document.write('    <tr>\n');
top.Month.document.write('     <td align="CENTER" colspan="3"><b><font size="4" color="#000000">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></b></font>&nbsp;<input type="text" name="month" size="8" maxlength="2"> </td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m7" value="  7 Tishri  "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Tishri\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m8" value="8 Kheshvan"\n');
top.Month.document.write('    onclick="this.form.month.value= \'Kheshvan\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m9" value="  9 Kislev "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Kislev\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m10" value=" 10 Tevet "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Tevet\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m11" value="11 Shevat"\n');
top.Month.document.write('    onclick="this.form.month.value= \'Shevat\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m12" value="12 Adar (I)"\n');
top.Month.document.write('    onclick="this.form.month.value= \'Adar (I)\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m13" value="13 Adar II"\n');
top.Month.document.write('    onclick="this.form.month.value= \'Adar II\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m1" value="  1 Nisan  "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Nisan\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m2" value="    2 Iyar   "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Iyar\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m3" value="  3 Sivan  "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Sivan\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m4" value="4 Tammuz"\n');
top.Month.document.write('    onclick="this.form.month.value= \'Tammuz\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="button" name="m5" value="    5 Av    "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Av\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER">&nbsp;</td>\n');
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <input type="button" name="m6" value="    6 Elul    "\n');
top.Month.document.write('    onclick="this.form.month.value= \'Elul\'"></font></td>\n');
top.Month.document.write('    <td align="CENTER">&nbsp;</td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   </table>\n');
top.Month.document.write('   <br>\n');
top.Month.document.write('   <font size="2" face="Book Antiqua">   <input type="button" value="    FINISHED    " onclick="Y=top.Year.document.dmy.year.value*1;M=this.form.month.value;D=top.Day.document.dmy.day.value*1;W=top.Year.document.dmy.bora.value;top.Program.kill (Y,M,D,W);"></center></font>\n');
top.Month.document.write('</form></center>\n');
top.Month.document.write('</body>\n');
top.Month.document.write('</html>\n');
top.Month.document.close ();
top.Year.document.dmy.bora.value="AM";
}

function MakeMonthPad (Calendar) {
top.Month.document.write('<html>\n<body bgcolor="#000000" text="#FFCC00" link="#FFCC00" vlink="#FFCC00" onload="Leap=false">\n');
top.Month.document.write('<form name="dmy" method="post">\n');
top.Month.document.write('<br>\n');
top.Month.document.write('   <center>\n');
top.Month.document.write('   <table border="5" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0">\n');
top.Month.document.write('    <tr>\n');
top.Month.document.write('     <td align="CENTER" colspan="3"><b><font size="4" color="#000000">Month</font></b></font></td>\n');
top.Month.document.write('   </tr>\n');
top.Month.document.write('   <tr>\n');
top.Month.document.write('    <td align="CENTER" colspan="3">\n');
top.Month.document.write('    <center>\n');
top.Month.document.write('    <input type="text" name="month" size="4" maxlength="2">\n');
if (Calendar=="Chinese") {top.Month.document.write('    <input type="checkbox" name="status" onclick="if (Leap) {Leap=false} else {Leap=true}"><font size="2" face="Book Antiqua" color="#000000">Leap</font>\n')}
top.Month.document.write('    </center>\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('   </tr>\n');
var rows=1;
while (rows<5){
top.Month.document.write('   <tr>\n');
var cols=1;
while (cols<4){
top.Month.document.write('    <td align="CENTER"><font size="1" face="Book Antiqua">\n');
top.Month.document.write('    <center>\n');
if (Calendar !="Chinese") {top.Month.document.write('    <input type="button" name="m'+((rows-1)*3+cols)+'" value="'+Thec(Calendar,((rows-1)*3+cols))+'"\n');} else
{top.Month.document.write('    <input type="button" name="m'+((rows-1)*3+cols)+'" value="   '+((rows-1)*3+cols)+'"\n');}
top.Month.document.write('    onclick="this.form.month.value= \''+((rows-1)*3+cols)+'\'">\n');
top.Month.document.write('    </center>\n');
top.Month.document.write('    </font></td>\n');
cols +=1
}
top.Month.document.write('   </tr>\n');
rows +=1;
}
top.Month.document.write('   </table>\n')
top.Month.document.write('   <br>\n');
top.Month.document.write('   <font size="2" face="Book Antiqua">');
top.Month.document.write('   <input type="button" value="    FINISHED    " onclick="Y=top.Year.document.dmy.year.value*1;M=this.form.month.value*1;D=top.Day.document.dmy.day.value*1;W=top.Year.document.dmy.bora.value;top.Program.kill (Y,M,D,W,top.Month.Leap);">');
top.Month.document.write('   </center>');
top.Month.document.write('</font>\n');
top.Month.document.write('</form>');
top.Month.document.write('</center>\n');
top.Month.document.write("</body>\n</html>");
top.Month.document.close ();
if (Calendar!="Chinese"){top.Year.document.dmy.bora.value= Calendar ? "AD":"AH";} else {top.Year.document.dmy.bora.value="AC";}
}

function MakeYearPad () {

top.Year.document.write('<html>\n<body bgcolor="#000000" text="#FFCC00" link="#FFCC00" vlink="#FFCC00">\n');
top.Year.document.write('<form name="dmy" method="post">\n<br>\n');
top.Year.document.write('   <center>\n');
top.Year.document.write('   <table border="5" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0">\n');
top.Year.document.write('    <tr>\n');
top.Year.document.write('     <td align="CENTER" colspan="3"><b><font size="3" color="#000000">Year</font></b></td>\n');
top.Year.document.write('    </tr>\n');
top.Year.document.write('    <tr>\n');
top.Year.document.write('     <td align="CENTER" colspan="3"><font size="2" face="Book Antiqua">\n');
top.Year.document.write('     <center>\n')
top.Year.document.write('     <input type="text" name="year" size="5" maxlength="4"><input type="button" name="bora"\n');
top.Year.document.write('     value="AD" onclick="top.Program.Cbora(top.Year.document.dmy.bora)">\n');
top.Year.document.write('     </center>\n');
top.Year.document.write('     </font></td>\n');
top.Year.document.write('    </tr>\n');
var rows=1;
while (rows<4){
top.Year.document.write('   <tr>\n');
var cols=1;
while (cols<4){
top.Year.document.write('    <td align="CENTER"><font size="2" face="Book Antiqua">\n');
top.Year.document.write('    <center>\n');
top.Year.document.write('    <input type="button" name="y'+((rows-1)*3+cols)+'" value="  '+((rows-1)*3+cols)+' "\n');
top.Year.document.write('    onclick="this.form.year.value+= \''+((rows-1)*3+cols)+'\'">\n');
top.Year.document.write('    </center>\n');
top.Year.document.write('    </font></td>\n');
cols +=1
}
top.Year.document.write('   </tr>\n');
rows +=1;
}
top.Year.document.write('    <tr>\n')
top.Year.document.write('     <td align="CENTER"><font size="2" face="Book Antiqua">\n');
top.Year.document.write('     <center>\n')
top.Year.document.write('     <input type="button" value="  0 " onclick="this.form.year.value+=\'0\'">\n')
top.Year.document.write('     </center>\n')
top.Year.document.write('     </font></td>\n')
top.Year.document.write('     <td align="CENTER" colspan="2"><font size="1" face="Book Antiqua">\n');
top.Year.document.write('     <center>\n')
top.Year.document.write('     <input type="button" value=" Clear "\n')
top.Year.document.write('     onclick="this.form.year.value =\'\'; this.form.year.focus ()">\n')
top.Year.document.write('     </center>\n')
top.Year.document.write('     </font></td>\n')
top.Year.document.write('    </tr>\n')
top.Year.document.write('   </table>\n')
top.Year.document.write('   </center>\n')
top.Year.document.write('</form>\n')
top.Year.document.write("</body>\n</html>");
top.Year.document.close ();
}

function MakeChoices () {
top.Choices.document.write ('<html>\n<body bgcolor="#000000" text="#FFCC00" link="#FFCC00" vlink="#FFCC00" onLoad="document.dmy.Input [0].checked=true;top.Program.Calendar=\'Gregorian\'" >\n<FORM NAME="dmy" METHOD="post">\n')
top.Choices.document.write ('<BR><left><FONT SIZE=2>\n<input type="radio" name="Input" onClick="top.Program.MakeMonthPad(true);top.Program.Calendar=\'Gregorian\'" VALUE="Gregorian"  checked>Gregorian/Western Calendar-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.MakeMonthPad(false);top.Program.Calendar=\'Hijri\'">Hijri/Islamic Calendar-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.MakeMonthPad(true);top.Program.Calendar=\'Julian\'">Julian/Old Style Calendar-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.MHebrewPad();top.Program.Calendar=\'Hebrew\'">Hebrew/Jewish Calendar-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.MakeMonthPad(\'Chinese\');top.Program.Calendar=\'Chinese\'">Chinese Calendar-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.Calendar=\'Jd\'">Julian Day (JD)-Others<br>\n')
top.Choices.document.write ('<input type="radio" name="Input"  onClick="top.Program.Calendar=\'mJd\'"> Modified Julian Day (MJD)-Others<br>\n</font></left>\n</FORM>\n')
top.Choices.document.write("</body>\n</html>");
top.Choices.document.close ();
}

function check(Y,M,D,input) {
if (input!='Jd' && input!='mJd') {      
        var dlimit=30
        if (input=='Gregorian' || input=='Julian') {if (M==1 || M==3 || M==5 || M==7 || M==8 || M==10 || M==12) {dlimit=31}; if (M==2) {dlimit=28+gL(Y,input)}};
        if (Y<1) {return '';};
        if (Y>9999) {return '';};
        if (Y-Math.floor(Y) != 0) {return ''};
        if (input !='Hebrew') {
        	if (M<1) {return '';};
        	if (M>12) {return '';};
        	if (M-Math.floor(M) != 0) {return '';};}
        if (D<1) {return '';};
        if (D>dlimit) {return '';}
        ;}
        if (D-Math.floor(D) != 0) {return '';};  
        return 'ok';   
}


function kill (input1,input2,input3,input4,input5) {
var Y=top.Year.document.dmy.year.value*1;
var M=top.Month.document.dmy.month.value;if (Calendar!="Hebrew"){M *=1}
var D=top.Day.document.dmy.day.value*1;
var W=top.Year.document.dmy.bora.value;
if (input4=='HebrewMistake') {D=-1};
var input=Calendar;

        if (check (Y,M,D,input) !='ok') {
        top.Day.document.dmy.day.value="";
        top.Month.document.dmy.month.value="";
        top.Year.document.dmy.year.value="";
        top.Results.document.write("<html>\n<head>\n<title>Results</title>\n</head>\n<body bgcolor='#000000' text='#FFCC00' link='#0000FF' vlink='#0000FF'>\n");
        top.Results.document.write("<BR><center><font size='3' face='Book Antiqua'>\n");
        top.Results.document.write("Mismatch...,\n");
        top.Results.document.write("</font><BR><BR><font size='3' face='Book Antiqua'>Maximum Year entry is 9999<BR>\n");
        top.Results.document.write("<I>NO </I> fractions allowed<BR>\n");
        top.Results.document.write("<I>NO </I> '-' sign (Except JD & MJD)<BR>\n");
        top.Results.document.write("Month & Day combinations must be correct<BR></font>\n");
        top.Results.document.write("</ceneter>\n");
        top.Results.document.write('</body>\n</html>');
        top.Results.document.close ();
        top.Year.document.dmy.year.focus() }
        else {Conversion (Y,M,D,input,W,input5)}
} 


function gL(GYEAR,input) {
var     l=0;    
        if (GYEAR/4 == Math.floor(GYEAR/4)) {l=1;} ;
if (input=='Gregorian') {if (GYEAR/100 == Math.floor(GYEAR/100)) {
                if (GYEAR/400 != Math.floor(GYEAR/400)) {l=0;};} ; }
return l;
}

function Today() {
var Ndate=new Date();FirstLine="Today";
var Y=Ndate.getYear () ;var M=Ndate.getMonth () +1;var D=Ndate.getDate ();var Calendar="Gregorian";var W='AD';
if (Y<1970 && Y<70) {Y +=2000} else {if (Y<1970) {Y +=1900}};
Conversion (Y,M,D,Calendar,W);
FirstLine="Your Entry";
}



function Conversion(Y,M,D,input,W,Leap) {
var JD
if (W=='BH' || W=='BC') {Y =-Y+1;W=''};
if (input=='Gregorian') {JD=JD_from_Gregorian_or_Julian(Y,M,D);var gregorian_date=new Array(Y,M,D)}
if (input=='Hijri') {JD=Htoj(D,M,Y);var hijri_date=new Array(Y,M,D)}
if (input=='Jd') {JD=D-0.5};
if (input=='mJd') {JD=D+2400000.5};
if (input=='Julian') {JD=JD_from_Gregorian_or_Julian(Y,M,D,'Julian');var julian_date=new Array(Y,M,D)}
if (input=='Hebrew') {var jewish_date=new Array(Y,M,D);var Im=M;JD=Joj(Y,M,D);if (JD==Math.floor(JD) || (M !='Tishri' && M !='Tishri' && M !='Kheshvan' && M !='Kislev' && M !='Tevet' && M !='Shevat' && M !='Adar' && M !='Adar I' && M !='Adar (I)' && M !='Adar II' && M !='Nisan' && M !='Iyar' && M !='Sivan' && M !='Tammuz' && M !='Av' && M !='Elul')) {kill(-1,0,.5,'HebrewMistake');return ' '}}
if (input=='Chinese') {var chinese_date=new Array(Math.floor((Y-1)/60)+1,amod(Y,60),M,Leap,D,Y);JD=JD_from_chinese(Math.floor((Y-1)/60)+1,amod(Y,60),M,Leap,D);var corrected_chinese_date=chinese_from_JD(JD);if (chinese_date[3] != corrected_chinese_date[3] || chinese_date[4] != corrected_chinese_date[4]) {chinese_date=corrected_chinese_date;alert("Your date does not exist in my converter")};}
if (input !='Hijri') {var hijri_date=Toh (JD)};
if (input !='Hebrew') {var jewish_date=Toi (JD)}; 
if (input != 'Gregorian') {var gregorian_date=JD_to_Gregorian(JD)};
if (input != 'Julian') {var julian_date=JD_to_julian(JD)};
if (input != 'Chinese') {var chinese_date=chinese_from_JD(JD)};
var MJD=JD-2400000.5;
var hijri_output=hijri_date[2]+" / "+MonthName(hijri_date[1],'Hijri')+" / "+AhBh(hijri_date[0],"Hijri")+" Hijri/Islamic";
var gregorian_output=gregorian_date[2]+" / "+MonthName(gregorian_date[1],'Grgorian')+" / "+AhBh(gregorian_date[0],"Gregorian")+" ميلادي";
var julian_output=julian_date[2]+" / "+MonthName(julian_date[1],'Julian')+" / "+AhBh(julian_date[0],"Julian")+" Julian";
var jewish_output=jewish_date[2]+ " / "+jewish_date[1]+" / "+AhBh(jewish_date[0],"Hebrew")+" Jewish";
//chinese output
var chinese_output=chinese_date[4]+" / ";if (chinese_date[3]){chinese_output +=" Leap "};
chinese_output +=chinese_date[2]+" / "+AhBh(chinese_date[5],"Chinese")+" Chinese<BR>\nYear "+chinese_date[1]+" of Cycle "+chinese_date[0]+" / "+chinese_name_of_year(chinese_date[1])[0]+chinese_name_of_year(chinese_date[1])[1]+" / Year of the "+chinese_name_of_year(chinese_date[1])[2]+" Chinese"
var moon_output=moon_output_numbers(JD);
       
       top.Results.document.write("<html>\n<head>\n<title>Results</title>\n</head>\n<body bgcolor='#000000' text='#FFCC00' link='#FFCC00' vlink='#FFCC00'>\n");
       top.Results.document.write("<table cols=3 width='100%' align='center'>\n");
       top.Results.document.write(" <tr>\n  <td width='33%'>\n  <img SRC='moon"+moon_output[3]+".jpg' height=50 width=50 align=left>\n");
       top.Results.document.write("  </td>\n<td width='33%'>\n   <center><font size='3' face='Book Antiqua'>"+FirstLine+"</font></center>\n  </td>\n  <td width='33%'>\n  </td>\n </tr>\n</table>");
       top.Results.document.write("<center>\n<font size='2' face='Book Antiqua'>"+WeekDay(JD)+"<BR>\n"+gregorian_output+"<BR>\n");
       top.Results.document.write(julian_output+"<BR>\n");
       top.Results.document.write(hijri_output+"<BR>\n");
       top.Results.document.write(jewish_output+"<BR>\n");
       top.Results.document.write(chinese_output+"<BR>\n");
       top.Results.document.write("On the Julian Day (JD) "+JD+"<BR>\n");
       top.Results.document.write("On the Modified Julian Day (MJD) "+MJD+"<BR>\n");
	   top.Results.document.write("Moon Age "+moon_output[1]+" days<BR>\n");
	   top.Results.document.write("Moon Synodic Period Fraction "+moon_output[0]+" % <BR>\n");
	   top.Results.document.write("Moon Illumination Fraction "+moon_output[2]+"<BR>\n");
       top.Results.document.write("</font><br><font size='1' face='Book Antiqua'>(Info Should be correct at 0:0:0 UT)\n");
       top.Results.document.write("</font></ceneter>\n");
       top.Results.document.write('<font size="1" face="Book Antiqua"><br>by: tarek maani</font><br>\n<font size="1" face="Book Antiqua"><a href="mailto:tmaani@bigfoot.com">tmaani@bigfoot.com</a></font>\n');
       top.Results.document.write("</body>\n</html>");
       top.Results.document.close ()}

function moon_output_numbers(JD){
var moon_date=ExtraMoonData(JD);
if (moon_date[0]>JD) {moon_date=ExtraMoonData(JD-29.5)}
var NMJD=moon_date[0];
var NNMJD=moon_date[2];
if (moon_date[5]<JD) {var Status=-1} else {var Status=1}
if (moon_date[5]<(JD+1) && NNMJD>=JD+1) {var NStatus=-1} else {var NStatus=1}
var MSPF=Math.round((JD-NMJD)/(NNMJD-NMJD)*100);
var MA=Math.round((JD-NMJD)*100)/100;
var MIF=Moon(JD+UtE(JD)/86400)[14]*Status;
var NMIF=Moon(JD+1+UtE(JD+1)/86400)[14]*NStatus/2;if (NMIF<0) {NMIF +=1} 
var Illumination=MIF/2;if (MIF<0) {Illumination +=1};
var moon_pic_variables= new Array
(0,0.03125,0.0625,0.125,0.25,0.33,0.42,0.50,0.58,0.67,0.75,0.875,0.9375,0.96875)
var x=0;var moon_pic=0;
while (x<14) {
if (NMIF>=moon_pic_variables[x]) {moon_pic=x}
x +=1}
return new Array (MSPF,MA,MIF,moon_pic)}


//JD to Gregorian
function JD_to_Gregorian(JD){
        var dif=JD-1721059.5;var Gy=0;var Gm=0;var Gd=0;
        var Leap=1;var Count=0;if (dif<=0) {Leap=0;Gy=1};
        var Multiples=Math.floor(Math.abs(dif/365.2425)/400)*400;
        Gy +=Multiples; Count +=Multiples*365.2425;
        while (Math.abs(dif)>=Count+365+Leap) {
                Gy +=1;Count +=365+Leap;
                Leap= Math.floor(Math.floor(Gy/4)/(Gy/4))-Math.floor(Math.floor(Gy/100)/(Gy/100))+Math.floor(Math.floor(Gy/400)/(Gy/400));
        };
        if (dif !=0) {Gy *=(dif/Math.abs(dif))} else {Gy=0};
        Gd=dif-Count+1;
        if (Gy<0) {Gd =(365+Leap)-(Math.abs(dif)-Count)+1};
        if (Gd<(60+Leap)) {Gd +=365;Gy -=1;} else {Gd -=Leap};
        Gm=Math.floor((Gd+63)/30.6001)-1;
        Gd -=(Math.floor((Gm+1)*30.6001)-63);
        if (Gm>12) {Gm -=12; Gy +=1}; 
        return new Array (Gy,Gm,Gd)}

//JD to Julian
function JD_to_julian(JD){
        var dif=JD-1721057.5;
        var Jy=Math.floor(dif/365.25);
        var JyD=Math.ceil(dif-Jy*365.25+1)-1;
        if (JyD<60) {JyD=Math.floor(dif-Jy*365.25+1)+365;Jy -=1};
        var Jm=Math.floor((JyD+63)/30.6001)-1;
        JyD -=(Math.floor((Jm+1)*30.6001)-63);
        if (Jm>12) {Jm -=12; Jy +=1} ;
        return new Array (Jy,Jm,JyD)}

//JD from any Gregorian or Julian Date
function JD_from_Gregorian_or_Julian(Year,Month,Day,Calendar){
if (Month <= 2) { --Year;Month += 12;}
var A = Math.floor(Year/100);
var b = Math.floor(2-A+(A/4));
var Extra=b;if (Calendar=='Julian') {Extra=0};
var JD=Math.floor(365.25 * (Year + 4712))
          +Math.floor(30.6001 * (Month+1))
          + Day + Extra -63.5;
return JD}

//JD to Hijri
function Toh(I) {
        var NMD;
        var Months;
        var JDIE= 1948437.5386277;
        var LC= 29.530594;
		var NMD=ExtraMoonData(I)[1];
		var Dh=I-NMD+1;
		if (Dh<1) {NMD=ExtraMoonData(I-LC)[1];Dh=I-NMD+1};       
        var Months= Math.round((NMD-JDIE)/LC);
        var Yh=Math.floor(Months/12)
        // fix the month to show two digits
        var Mh=Months-Yh*12+1;
        Mh = Mh + "";
        if(Mh.length == 1)Mh = "0" + Mh;
        // fix the day to show two digits
        var Hd=Dh;Hm=Mh; 
        Dh = Dh + "";
        if(Dh.length == 1)Dh = "0" + Dh;
        return new Array (Yh+1,Mh,Dh,NMD)
}

//Jewish to JD
function Joj (Y,M,D) {
JDHE= 347997.25+((5+(204/1080))/24);
LC=29.5+(793/1080/24);
var YoCycle=(Y%19)-1;
var Hcycle=Math.floor(Y/19);
if (YoCycle<0) {YoCycle +=19; Hcycle -=1};
if (Y<0 && Y%19 !=0) {Hcycle +=1};
var NextYear=YoCycle+1;
var ThisLeap=Jleap(YoCycle);
var NextLeap=Jleap(NextYear);
var Lunations=Lunation(YoCycle);
var ThisTishri=Tishri(Hcycle,Lunations,ThisLeap,YoCycle);
var NextTishri=Tishri(Hcycle,Lunations+12+ThisLeap,NextLeap,YoCycle+1);
var YearType=YearStatus(ThisTishri,NextTishri,ThisLeap);
return HJD(ThisTishri,YearType,ThisLeap,M,D);
}

function Jleap (CycleY) {
var LeapStatus=0;
if (CycleY==18 || CycleY==2 || CycleY==5 || CycleY==7 || CycleY==10 || CycleY==13 || CycleY==16) {LeapStatus=1};
return LeapStatus;
}

function Lunation(YoC) {
var counter=0;
var Lun=0;
while (counter<YoC) {
Lun +=12;
if (Jleap(counter)==1) {Lun +=1};
counter +=1;
};
return Lun
}

function YearStatus(Py,Ny,LeapStatus) {
var Status='R';
if (Ny-Py-(LeapStatus*30)==355) {Status='C'};
if (Ny-Py-(LeapStatus*30)==353) {Status='D'};
return Status;
}

function HJD (Tt,Yt,Tl,M,D) {
var Ytc=0;
var Ytd=0;
if (Yt=='D') {Ytd -=1};
if (Yt=='C') {Ytc +=1};
var FinalResult=Tt-1;
if (M=='Tishri') {FinalResult +=D;if (D>30) {FinalResult -=.5};return FinalResult;} else {FinalResult +=30};
if (M=='Kheshvan') {FinalResult +=D;if (D>(29+Ytc)) {FinalResult -=.5};return FinalResult;} else {FinalResult =FinalResult+29+Ytc};
if (M=='Kislev') {FinalResult +=D;if (D>(30+Ytd)) {FinalResult -=.5};return FinalResult;} else {FinalResult =FinalResult+30+Ytd};
if (M=='Tevet') {FinalResult +=D;if (D>29) {FinalResult -=.5};return FinalResult;} else {FinalResult +=29};
if (M=='Shevat') {FinalResult +=D;if (D>30) {FinalResult -=.5};return FinalResult;} else {FinalResult +=30};
if (M=='Adar (I)') {FinalResult +=D;if (D>(29+Tl)) {FinalResult -=.5};if (Tl==1) {M='Adar I'} else {M='Adar'};return FinalResult;} else {FinalResult=FinalResult+29+Tl};
if (M=='Adar II') {FinalResult +=D;if (D>29*Tl) {FinalResult -=.5};return FinalResult;} else {FinalResult =FinalResult+(29*Tl)};
if (M=='Nisan') {FinalResult +=D;if (D>30) {FinalResult -=.5};return FinalResult;} else {FinalResult +=30};
if (M=='Iyar') {FinalResult +=D;if (D>29) {FinalResult -=.5};return FinalResult;} else {FinalResult +=29};
if (M=='Sivan') {FinalResult +=D;if (D>30) {FinalResult -=.5};return FinalResult;} else {FinalResult +=30};
if (M=='Tammuz') {FinalResult +=D;if (D>29) {FinalResult -=.5};return FinalResult;} else {FinalResult +=29};
if (M=='Av') {FinalResult +=D;if (D>30) {FinalResult -=.5};return FinalResult;} else {FinalResult +=30};
if (M=='Elul') {FinalResult +=D;if (D>29) {FinalResult -=.5};return FinalResult;} else {FinalResult +=29};
}

//JD to Hebrew
function Toi(I) {
        var M=0;
        var D=0;
        var C=0;
        var Days=0
        var Months=0;
        /* JDHE=  347997.4662037; hour 5, 204 halakim; the epoch of the Jewish calendar*/
        JDHE= 347997.25+((5+(204/1080))/24);
        /* LC= 29.530594; 
        29 days, 12 hours, 793 halakim with 1080 halakim equalling an hour; the mean lunar cycle*/
        LC=29.5+(793/1080/24);
        Months= (I-JDHE)/LC;
        var Jcycle=Math.floor(Months/235);
        /* Cmonths is the number of the Month in the Cycle; this is needed to determine Years & intercalary months as years 3, 6, 8, 11, 14, 17, and 
        19  of each 19 year cycle has an intercalary month for a total of 235 months */
        var Cmonths=Math.floor(Months)-(Jcycle*235);
        /* Cy is the number of the year in the cycle 0-18
        Hl will tell us if Cy is a leap year or not & lun will give us number of months up to the end of the previuos year*/
        var Cy=0; var Hl=0;var lun=0;
        while (Cmonths>0) {
        if (Cmonths>=25  && (Cy==6 || Cy==17)) {Cmonths -=25;Cy +=2;lun +=25;} else {if (Cy==6 || Cy==17) {if (Cmonths>=12) {Hl=1;Cy +=1;lun +=12}; Cmonths=0;};}
        if (Cmonths>=37) {Cmonths -=37;Cy +=3;lun +=37} else {if (Cmonths>=24) {Hl=1;Cy +=2;lun +=24;} else {Cy +=Math.floor(Cmonths/12);lun=lun+(Math.floor(Cmonths/12)*12);};Cmonths=0}
                }

        Iy=(Jcycle)*19+Cy+1;
        /* new moon of tishri */
        Nmt=Tishri(Jcycle,lun,Hl,Cy);
        /* next tishri */
        var Hl2=0;
        Cy +=1;
        if (Cy==19) {Nmt2=Tishri(Jcycle+1,0,0,0)} else {
        if (Cy==2 || Cy==5 || Cy==7 || Cy==10 || Cy==13 || Cy==16 || Cy==18) {Hl2=1};
        Nmt2=Tishri(Jcycle,lun+12+Hl,Hl2,Cy) }
        complete=Nmt2-Nmt; var Heshvan=0;var Kislev=0;
        if (complete==355 || complete==385) {Heshvan=1};
        if (complete==353 || complete==383) {Kislev=1};
        var doy=I-Nmt+1; Id=0 ; if (doy>30) {doy -=30} else {Im="Tishri";Id=doy};
        	if (doy>(29+Heshvan)) {doy -=(29+Heshvan)} else {if (Id==0){Im="Kheshvan";Id=doy};}
        	if (doy>(30-Kislev)) {doy -=(30-Kislev)} else {if (Id==0){Im="Kislev";Id=doy};}
        	if (doy>29) {doy -=29} else {if (Id==0){Im="Tevet";Id=doy};}
        	if (doy>30) {doy -=30} else {if (Id==0){Im="Shevat";Id=doy};}
        	if (doy>29+Hl) {doy =doy-(29+Hl)} else {if (Id==0) {if (Hl==1) {Im="Adar I";Id=doy} else {Im="Adar";Id=doy}}}
        	if (doy>29 && Hl==1) {doy -=29} else {if (Id==0 && Hl==1){Im="Adar II";Id=doy};}
        	if (doy>30) {doy -=30} else {if (Id==0){Im="Nisan";Id=doy};}
        	if (doy>29) {doy -=29} else {if (Id==0){Im="Iyar";Id=doy};}
        	if (doy>30) {doy -=30} else {if (Id==0){Im="Sivan";Id=doy};}
        	if (doy>29) {doy -=29} else {if (Id==0){Im="Tammuz";Id=doy};}
        	if (doy>30) {doy -=30} else {if (Id==0){Im="Av";Id=doy};}
        	if (doy>29 && Id==0) {doy -=29;Iy +=1;Im="Tishri";Id=doy} else {if (Id==0) {Im="Elul";Id=doy}}
	if (doy==0) {Id=29;Iy -=1};
return new Array(Iy,Im,Id)
}


function Tishri (Jcycle,lun,Hleap,CycleYear) {
        var NMD=0;var p=0
        Molad=(((Jcycle*235)+lun)*LC)+JDHE;
        var dehiyyot=(Math.floor(Molad+.75)%7)+2;if (dehiyyot<1) {dehiyyot +=7};
        if (dehiyyot>6) (dehiyyot -=7);
        var Part =(Molad+.75)-Math.floor(Molad+.75);    
        if ((CycleYear==0 || CycleYear==3 || CycleYear==6 || CycleYear==8 || CycleYear==11 || CycleYear==14 || CycleYear==17 || CycleYear==19) && dehiyyot==2 && Part>=(15+(589/1080))/24) {NMD +=1;p=1}
        if (Part>=(9+(204/1080))/24 && dehiyyot==3 && Hleap==0)  {NMD +=1;p=1}
        if (p==0) {NMD +=Math.ceil(Part-.75);};
        dehiyyot += NMD;
        if (dehiyyot==1 || dehiyyot==4 || dehiyyot==6) {NMD +=1}        
        var Nmt= Math.floor(Molad+.75)-.5+NMD;
        return Nmt;
}

//Hijri to JD
function Htoj(Id,Im,Iy) {

        var JDIE= 1948437.5386277;
        var LC= 29.530594;
        var JulianDay=0;
        var Cycles=0;
        var NewMoon=0;
        var NewMonth=0;
        Cycles=(Iy-1)*12+Im-1;
        var CJD=Cycles*LC+JDIE
        NewMonth= ExtraMoonData(CJD)[1];
        if (Math.abs(NewMonth-CJD)>15) {NewMonth=ExtraMoonData(CJD-(Math.abs(NewMonth-CJD)/(NewMonth-CJD))*LC)[1]};
        JulianDay=Id+NewMonth-1; 
        if (Toh(JulianDay)[1] != Im) {alert ("your date does not exist but it is equivelant to the 1st of "+MonthName(Toh(JulianDay)[1],'Hijri')) };
        return JulianDay;
}

// to remove the 0 & negative sign from final year output
function AhBh(year,input) {
if (year<1) {year=Math.abs(year-1); if (input=='Hijri') {year +=' ق.هـ'} else {year +=' ق.م'}; }
else {
if (input=='Hijri') {year +=' ب.هـ'} 
else {
if (input=='Hebrew') {year +=' AM'} 
else {
if (input=='Chinese') {year +=' AC'} 
else {
year +=' ب.م'}}}}
return year;}


//to give a name to the month number according to calendar
function MonthName(Month,Calendar) {
        var s=false; 
        Month = parseInt(Month);
        var IslamicMonthName=new IslamicMonthNames (false);    
        var ChristianMonthName=new ChristianMonthNames (false);  
        if (Calendar=='Hijri') {s=true}
        return  s ? IslamicMonthName[Month]:ChristianMonthName[Month];
}

function WeekDay (Jd) {
        var Dweek=((Jd+.5)%7)+2;
        var DAY= new Array (7)
        if (Dweek<1)  {Dweek +=7};
        if (Dweek>7)  {Dweek -=7};
        DAY[6]="Friday";
        DAY[7]="Saturday"; 
        DAY[1]="Sunday"; 
        DAY[2]="Monday"; 
        DAY[3]="Tuesday"; 
        DAY[4]="Wednesday"; 
        DAY[5]="Thursday"; 
        return DAY[Dweek]
}

function Thec (w,MonthNumber) {
        var ChristianMonthName= new ChristianMonthNames (true);
        var IslamicMonthName= new IslamicMonthNames (true);       
                var MonthName= w ? ChristianMonthName[MonthNumber]:IslamicMonthName[MonthNumber]
                return MonthNumber+" "+MonthName+" ";

}


function ChristianMonthNames (w){
this[1]= w ? "Jan":"يناير";
this[2]= w ? "Feb":"فبراير";
this[3]= w ? "Mar":"مارس";
this[4]= w ? "Apr":"أبريل";
this[5]= w ? "May":"مايو";
this[6]= w ? "Jun":"يونيو";
this[7]= w ? "Jul":"يوليو";
this[8]= w ? "Aug":"أوغسطس";
this[9]= w ? "Sep":"سبتمبر";
this[10]= w ? "Oct":"أوكتوبر";
this[11]= w ? "Nov":"نوفمبر";
this[12]= w ? "Dec":"ديسمبر";
}

function IslamicMonthNames (w){
this[1]= w ? "Moh":"محرّم";
this[2]= w ? "Saf":"صفر";
this[3]= w ? "Ra1":"ربيع الأول";
this[4]= w ? "Ra2":"ربيع الثاني";
this[5]= w ? "Jo1":"جمادى الأولى";
this[6]= w ? "Jo2":"جمادى الثاني";
this[7]= w ? "Raj":"رجب";
this[8]= w ? "Shb":"شعبان";
this[9]= w ? "Ram":"رمضان";
this[10]= w ? "Shw":"شوّال";
this[11]= w ? "ThQ":"ذو القعدة";
this[12]= w ? "ThH":"ذو الحجة";
}



function Cbora(c) {

if (c.value=="BC") {if (Calendar =="Hebrew") {var b="AM"} else {if (Calendar =="Chinese") {var b="AC"} else {var b="AD"}}} 
if (c.value=="AD") {var b="BC"}
if (c.value=="BH") {var b="AH"}
if (c.value=="AH") {var b="BH"}
if (c.value=="AM") {var b="BC"}
if (c.value=="AC") {var b="BC"}
c.value=b
}

function dtr(d) {
return (d*Math.PI)/180}

function rsin(d) {
return Math.sin(dtr(simple(d)))}

function rcos(d) {
return Math.cos(dtr(simple(d)))}

function rtan(d) {
return Math.tan(dtr(simple(d)))}


function simple (d) {
return d-360*(Math.floor(d/360))}


function Dtodms (d) {
with (Math) {
var id=(abs(d)-floor(abs(d)))*3600;
var d=floor(abs(d))*(d/abs(d));
var m=floor((id/60)%60);
var s=round(1000*(id%60));
if (s==60) {s=0;m +=1};
if (m==60) {m=0;d +=1};
return (			d+"° "+
					m+"' "+
					floor(s/1000)+'"'+
					"."+(s%1000)
		);
		}
}

function Dtohms (d) {
with (Math) {
var h=d/15
var id=(abs(h)-floor(abs(h)))*3600;
var h=floor(abs(h))*(h/abs(h));
var m=floor((id/60)%60);
var s=round(10000*(id%60));
if (s==60) {s=0;m +=1};
if (m==60) {m=0;h +=1};
return (			h+"h "+
					m+"m "+
					s/10000+'s'
					
		);
		}
}

function Dftohms (Df,Type) {
with (Math) {
var d=0
if (Type==0){if (abs(Df-0.5)>0.5) {d=abs(Df)/Df; Df=Df-d};};
var h=Df*24
var id=(abs(h)-floor(abs(h)))*3600;
var h=floor(abs(h))*(h/abs(h));
var m=floor((id/60)%60);
var s=round(1000*(id%60));
if (s==60) {s=0;m +=1};
if (m==60) {m=0;h +=1};
if (h==24) {h=0;d +=1};
var phrase=			h+"h "+
					m+"m "+
					floor(s/1000)+'s'+
					"."+(s%1000);
if (Type==0){
if (d==-1) {phrase +=" on preceading day"};
if (d==1) {phrase +=" on following day"};}
		};
return phrase}


//Calculation of ephemerides for the moon
function Moon(JDE)
{
with (Math)
	{		
	
	var JD=JDE-UtE(JDE)/86400;
	var T1=(JD-2451545)/36525;

	
	//Julian centuries (TDT) since 2000 January 0.5
	var	T = (JDE-2451545)/36525;
	var T5=(T4=(T3=(T2=T*T)*T)*T)*T;

	// Mean longitude of the moon
	var	L1 = 218.3164477+(481267.88123421+(-0.0015786+(1/538841-T/65194000)*T)*T)*T;
	L1 = simple(L1);

	//Mean elongation of the moon
	var	D = 297.8501921+(445267.1114034+(-0.0018819+(1/545868-T/113065000)*T)*T)*T;
	D = simple(D);

	// Mean anomaly of the sun
	var	M = 357.5291092+(35999.0502909+(-0.0001536+T/24490000)*T)*T;
	M = simple(M);

	//Mean anomaly of the moon
	var M1 = 134.9633964+(477198.8675055+(0.0087414+(1/69699-T/14712000)*T)*T)*T;
	M1 = simple(M1);

	//Mean distance of the moon from her ascending node
	var	F = 93.2720950+(483202.0175233+(-0.0036539+(-1/3526000+T/863310000)*T)*T)*T;
	F = simple(F);

	//Corrections
	var	A1 =119.75+131.849*T;
	A1 = simple(A1);
	var	A2 = 53.09+479264.290*T;
	A2 = simple(A2);
	var	A3 = 313.45+481266.484*T;
	A3 = simple(A3);
	var	fE = 1+(-0.002516-0.0000074*T)*T;
	var	fE2 = fE*fE;

	
	//Periodic terms for the moon

	//Longitude and distance
	var E1= new Array (
	0, 0, 1, 0, 6288774, -20905355,
	2, 0, -1, 0, 1274027, -3699111,
	2, 0, 0, 0, 658314, -2955968,
	0, 0, 2, 0, 213618, -569925,
	0, 1, 0, 0, -185116, 48888,
	0, 0, 0, 2, -114332, -3149,
	2, 0, -2, 0, 58793, 246158,
	2, -1, -1, 0, 57066, -152138,
	2, 0, 1, 0, 53322, -170733,
	2, -1, 0, 0, 45758, -204586,

	0, 1, -1, 0, -40923, -129620,
	1, 0, 0, 0, -34720, 108743,
	0, 1, 1, 0, -30383, 104755,
	2, 0, 0, -2, 15327, 10321,
	0, 0, 1, 2, -12528, 0,
	0, 0, 1, -2, 10980, 79661,
	4, 0, -1, 0, 10675, -34782,
	0, 0, 3, 0, 10034, -23210,
	4, 0, -2, 0, 8548, -21636,
	2, 1, -1, 0, -7888, 24208,

	2, 1, 0, 0, -6766, 30824,
	1, 0, -1, 0, -5163, -8379,
	1, 1, 0, 0, 4987, -16675,
	2, -1, 1, 0, 4036, -12831,
	2, 0, 2, 0, 3994, -10445,
	4, 0, 0, 0, 3861, -11650,
	2, 0, -3, 0, 3665, 14403,
	0, 1, -2, 0, -2689, -7003,
	2, 0, -1, 2, -2602, 0,
	2, -1, -2, 0, 2390, 10056,

	1, 0, 1, 0, -2348, 6322,
	2, -2, 0, 0, 2236, -9884,
	0, 1, 2, 0, -2120, 5751,
	0, 2, 0, 0, -2069, 0,
	2, -2, -1, 0, 2048, -4950,
	2, 0, 1, -2, -1773, 4130,
	2, 0, 0, 2, -1595, 0,
	4, -1, -1, 0, 1215, -3958,
	0, 0, 2, 2, -1110, 0,
	3, 0, -1, 0, -892, 3258,

	2, 1, 1, 0, -810, 2616,
	4, -1, -2, 0, 759, -1897,
	0, 2, -1, 0, -713, -2117,
	2, 2, -1, 0, -700, 2354,
	2, 1, -2, 0, 691, 0,
	2, -1, 0, -2, 596, 0,
	4, 0, 1, 0, 549, -1423,
	0, 0, 4, 0, 537, -1117,
	4, -1, 0, 0, 520, -1571,
	1, 0, -2, 0, -487, -1739,

	2, 1, 0, -2, -399, 0,
	0, 0, 2, -2, -381, -4421,
	1, 1, 1, 0, 351, 0,
	3, 0, -2, 0, -340, 0,
	4, 0, -3, 0, 330, 0,
	2, -1, 2, 0, 327, 0,
	0, 2, 1, 0, -323, 1165,
	1, 1, -1, 0, 299, 0,
	2, 0, 3, 0, 294, 0,
	2, 0, -1, -2, 0, 8752)

	//Latitude
	var E2= new Array (
	
	0, 0, 0, 1, 5128122,
	0, 0, 1, 1, 280602,
	0, 0, 1, -1, 277693,
	2, 0, 0, -1, 173237,
	2, 0, -1, 1, 55413,
	2, 0, -1, -1, 46271,
	2, 0, 0, 1, 32573,
	0, 0, 2, 1, 17198,
	2, 0, 1, -1, 9266,
	0, 0, 2, -1, 8822,

	2, -1, 0, -1, 8216,
	2, 0, -2, -1, 4324,
	2, 0, 1, 1, 4200,
	2, 1, 0, -1, -3359,
	2, -1, -1, 1, 2463,
	2, -1, 0, 1, 2211,
	2, -1, -1, -1, 2065,
	0, 1, -1, -1, -1870,
	4, 0, -1, -1, 1828,
	0, 1, 0, 1, -1794,
	
	0, 0, 0, 3, -1749,
	0, 1, -1, 1, -1565,
	1, 0, 0, 1, -1491,
	0, 1, 1, 1, -1475,
	0, 1, 1, -1, -1410,
	0, 1, 0, -1, -1344,
	1, 0, 0, -1, -1335,
	0, 0, 3, 1, 1107,
	4, 0, 0, -1, 1021,
	4, 0, -1, 1, 833,
	
	0, 0, 1, -3, 777,
	4, 0, -2, 1, 671,
	2, 0, 0, -3, 607,
	2, 0, 2, -1, 596,
	2, -1, 1, -1, 491,
	2, 0, -2, 1, -451,
	0, 0, 3, -1, 439,
	2, 0, 2, 1, 422,
	2, 0, -3, -1, 421,
	2, 1, -1, 1, -366,

	2, 1, 0, 1, -351,
	4, 0, 0, 1, 331,
	2, -1, 1, 1, 315,
	2, -2, 0, -1, 302,
	0, 0, 1, 3, -283,
	2, 1, 1, -1, -229,
	1, 1, 0, -1, 223,
	1, 1, 0, 1, 223,
	0, 1, -2, -1, -220,
	2, 1, -1, -1, -220,

	1, 0, 1, 1, -185,
	2, -1, -2, -1, 181,
	0, 1, 2, 1, -177,
	4, 0, -2, -1, 176,
	4, -1, -1, -1, 166,
	1, 0, 1, -1, -164,
	4, 0, 1, -1, 132,
	1, 0, -1, -1, -119,
	4, -1, 0, -1, 115,
	2, -2, 0, 1, 107)
	

	var sumL=0 ; var sumr=0 ; var sumB=0 ; var x=0

	while (x<300)
	{
	var f = 1;
	if(abs(E1[x/5*6+1])==1) f=fE;
	if(abs(E1[x/5*6+1])==2) f=fE2;
	sumL += f*(E1[x/5*6+4]*rsin(E1[x/5*6]*D+E1[x/5*6+1]*M+E1[x/5*6+2]*M1+E1[x/5*6+3]*F));
	sumr += f*(E1[x/5*6+5]*rcos(E1[x/5*6]*D+E1[x/5*6+1]*M+E1[x/5*6+2]*M1+E1[x/5*6+3]*F));
	f = 1;
	if(abs(E2[x+1])==1) f=fE;
	if(abs(E2[x+1])==2) f=fE2;
	sumB += f*(E2[x+4]*rsin(E2[x]*D+E2[x+1]*M+E2[x+2]*M1+E2[x+3]*F));
	x +=5
	}

	//Corrections
	sumL = sumL+3958*rsin(A1)+1962*rsin(L1-F)+318*rsin(A2);
		sumB = sumB-2235*rsin(L1)+382*rsin(A3)+175*rsin(A1-F)+175*rsin(A1+F)+127*rsin(L1-M1)-115*rsin(L1+M1);

	
	//Longitude of the moon
	var Lamda= simple(L1+sumL/1000000);

	//Latitude of the moon
	var Beta= sumB/1000000;

	//Distance earth-moon
	var Delta= 385000.56+sumr/1000;

	//EquatorialHorizontal Parallax of Moon
	var sPi=asin(6378.14/Delta)/(PI/180)

	//Angular Semidiameter of moon
	var s=asin(0.272481*rsin(sPi))/(PI/180);	

	//Apparent Longitiude of the Moon
	var ApparentLamda=simple(Lamda+Nutation(JDE)[0]);

	
	//Apparent Rt ascension of Moon
	var ApparentAlpha=RtAscension(ApparentLamda,Nutation(JDE)[3],Beta);
	var Alpha=RtAscension(Lamda,Nutation(JDE)[2],Beta);

	//Apparent Declinatin
	var ApparentsDelta=Declination(ApparentLamda,Nutation(JDE)[3],Beta);
	var sDelta=Declination(Lamda,Nutation(JDE)[2],Beta);

	var Epsilon=Nutation(JDE)[3]
	var DeltaPsi=Nutation(JDE)[0]


	//Mean GHA Aries
	var GHAAmean = 280.46061837+ 360.98564736629*(JD-2451545)+(0.000387933-T1/38710000)*T1*T1;
	GHAAmean = simple(GHAAmean);

 
	//True GHA Aries
	GHAAtrue = GHAAmean+DeltaPsi*rcos(Epsilon);
	GHAAtrue = simple(GHAAtrue);

	//GHA of the Moon
	GHAmoon = GHAAtrue-L-ApparentAlpha;
	GHAmoon = simple(GHAmoon);

	var p=Earth(L,sPhi,height)[2];
	var psp=Earth(L,sPhi,height)[0];
	var pcp=Earth(L,sPhi,height)[1];

	//to get the value q
	var a=rcos(sDelta)*rsin(GHAmoon);
	var b=rcos(sDelta)*rcos(GHAmoon)-pcp*rsin(sPi);
	var c=rsin(sDelta)*psp*rsin(sPi);
	var q=sqrt(a*a+b*b+c*c);
	
	//Topocentric Angular Semidiameter
	var s1=asin(0.272481*rsin(sPi)/q)/(PI/180);


	//Topocentric Deistance Surface to Center
	var Delta1=q*Delta


	// correction for RtAscention (To Topocentric)
	var DeltaAlpha=atan((-pcp*rsin(sPi)*rsin(GHAmoon))/(rcos(sDelta)-pcp*rsin(sPi)*rcos(GHAmoon)))/(PI/180);
	if ((rcos(sDelta)-pcp*rsin(sPi)*rcos(GHAmoon))<0) {DeltaAlpha +=180};
	
	var Alpha1=Alpha+DeltaAlpha;
	
	//Correction for Declination (To Topocentric)
	sDelta1=atan(((rsin(sDelta)-psp*rsin(sPi))*rcos(DeltaAlpha))/(rcos(sDelta)-pcp*rsin(sPi)*rcos(GHAmoon)))/(PI/180);
	
	//local coordinates A = Azimuth from north , h = altitude from hrizon
	var A=atan(rsin(GHAmoon)/(rcos(GHAmoon)*rsin(sPhi)-rtan(ApparentsDelta)*rcos(sPhi)))/(PI/180)+180;
	A=simple(A);
	if ((rcos(GHAmoon)*rsin(sPhi)-rtan(ApparentsDelta)*rcos(sPhi))<0) {A +=180};
	var h=asin(rsin(sPhi)*rsin(ApparentsDelta)+rcos(sPhi)*rcos(ApparentsDelta)*rcos(GHAmoon))/(PI/180); 

	//correction for altitude 
	var Deltah=asin(p*rsin(sPi)*rcos(h))/(PI/180);
	var Apparenth=h-Deltah;

	
	// Correction for Refraction
	if (Apparenth>=0 && Apparenth<=90) {
	var R=1.02/rtan(Apparenth+(10.3/(Apparenth+5.11)))+0.0019279;
	Apparenth +=R/60};

	//illumination of the moon's disk
		var Alpha0=Solar(JDE)[1];
		var sDelta0=Solar(JDE)[2];
		var R=Solar(JDE)[10]*149597870;
		var Psi=(acos(rsin(sDelta0)*rsin(ApparentsDelta)+rcos(sDelta0)*rcos(ApparentsDelta)*rcos(Alpha0-ApparentAlpha)))/(PI/180);
		var i=atan(R*rsin(Psi)/(Delta-R*rcos(Psi)))/(PI/180);
		if (Delta>0) {var Status=" Waning"} else {var Status=" Waxing"};Status +=" Moon";
		if ((Delta-R*rcos(Psi))<0) {i +=180}
		var k=(1+rcos(i))/2;
		var Status
		//Topocentric & Geocentric Cresent Width
		var Cw1=k*s1*2;var Cw=k*s*2;
		k = round(10000*k)/10000;
	}
return new Array (L1,D,M,M1,F,Lamda,Beta,Delta,ApparentLamda,ApparentAlpha,ApparentsDelta,sPi,Alpha,sDelta,k,Status,Apparenth,A,s1,Delta1,Cw1*60,s,Cw*60);

}


function ExtraMoonData (JD) {
	var JDE=JD+UtE(JD)/86400;
	//Shaukat Criteria
	//Moon Altitude>3.4 Degrees
	//(Altitude/12.7+Crescent Width (arcmin)/1.2)>1 at sunset


	//New Moon Visibility
	var NewMoon=TrueMoonPhase(JDE)[0];
	var NextNewMoon=TrueMoonPhase(JDE+29.530594)[0];
	var PreviousNewMoon=TrueMoonPhase(JDE-29.530594)[0];
	var FirstQurater=TrueMoonPhase(JDE)[1];
	var FullMoon=TrueMoonPhase(JDE)[2];
	var LastQuarter=TrueMoonPhase(JDE)[3];
	NMJD=NewMoon-UtE(NewMoon)/86400;
	NNMJD=NextNewMoon-UtE(NextNewMoon)/86400;
	PNMJD=PreviousNewMoon-UtE(PreviousNewMoon)/86400;
	var Visibility;
	var x=0;var SunSetJDE; var MoonAlt1; var CW;
	var SunSet1; var FSS=0;
	while (Visibility !="Visible") {
	SunSet1=RTS(NMJD+x,L,sPhi,1)[2];SunSet1 += Math.floor(NMJD-0.5)+0.5+x;
	if (SunSet1>NMJD && FSS==0) {FSS=SunSet1};
	SunSetJDE=SunSet1+UtE(SunSet1)/86400;
	MoonAlt1=Moon(SunSetJDE)[16];
	CW=Moon(SunSetJDE)[22];
	if (MoonAlt1>3.4 && (MoonAlt1/12.7+CW/1.2)>1) {Visibility="Visible"} else {Visibility="Not Visible"};
	x +=1;
	}
	var Hijri1=Math.floor(SunSet1-0.5)+1.5;

	//full moon & quarters
	var FQJD=FirstQurater-UtE(FirstQurater)/86400;
	var FMJD=FullMoon-UtE(FullMoon)/86400;
	var LQJD=LastQuarter-UtE(LastQuarter)/86400;
return new Array (NMJD,Hijri1,NNMJD,PNMJD,FQJD,FMJD,LQJD);
}


function Solar (JDE){
var JD=JDE-UtE(JDE)/86400;
var Tau=(JDE-2451545)/365250;
var T=(JDE-2451545)/36525;
var T1=(JD-2451545)/36525;
//A cos(B+C*Tau)

with(Math)
	{
	//Periodic terms for the sun

	//Heliocentric eccliptical Longitude
	var L0=	175347046
			+3341656*cos(4.6692568+6283.0758500*Tau)
			+34894*cos(4.62610+12566.15170*Tau)
			+3497*cos(2.7441+5753.3849*Tau)
			+3418*cos(2.8289+3.5231*Tau)
			+3136*cos(3.6277+77713.7715*Tau)
			+2676*cos(4.4181+7860.4194*Tau)
			+2343*cos(6.1352+3930.2097*Tau)
			+1324*cos(0.7425+11506.7698*Tau)
			+1273*cos(2.0371+529.6910*Tau)
			+1199*cos(1.1096+1577.3435*Tau)
			+990*cos(5.233+5884.927*Tau)
			+902*cos(2.045+26.298*Tau)
			+857*cos(3.508+398.149*Tau)
			+780*cos(1.179+5223.694*Tau)
			+753*cos(2.533+5507.553*Tau)
			+505*cos(4.583+18849.228*Tau)
			+492*cos(4.205+775.523*Tau)
			+357*cos(2.920+0.067*Tau)
			+317*cos(5.849+11790.629*Tau)
			+284*cos(1.899+796.298*Tau)
			+271*cos(0.315+10977.079*Tau)
			+243*cos(0.345+5486.778*Tau)
			+206*cos(4.806+2544.314*Tau)
			+205*cos(1.869+5573.143*Tau)
			+202*cos(2.458+6069.777*Tau)
			+156*cos(0.833+213.299*Tau)
			+132*cos(3.411+2942.463*Tau)
			+126*cos(1.083+20.775*Tau)
			+115*cos(0.645+0.980*Tau)
			+103*cos(0.636+4694.003*Tau)
			+102*cos(0.976+15720.839*Tau)
			+102*cos(4.267+7.114*Tau)
			+99*cos(6.21+2146.17*Tau)
			+98*cos(0.68+155.42*Tau)
			+86*cos(5.98+161000.69*Tau)
			+85*cos(1.30+6275.96*Tau)
			+85*cos(3.67+71430.70*Tau)
			+80*cos(1.81+17260.15*Tau)
			+79*cos(3.04+12036.46*Tau)
			+75*cos(1.76+5088.63*Tau)
			+74*cos(3.50+3154.69*Tau)
			+74*cos(4.68+801.82*Tau)
			+70*cos(0.83+9437.76*Tau)
			+62*cos(3.98+8827.39*Tau)
			+61*cos(1.82+7084.90*Tau)
			+57*cos(2.78+6286.60*Tau)
			+56*cos(4.39+14143.50*Tau)
			+56*cos(3.47+6279.55*Tau)
			+52*cos(0.19+12139.55*Tau)
			+52*cos(1.33+1748.02*Tau)
			+51*cos(0.28+5856.48*Tau)
			+49*cos(0.49+1194.45*Tau)
			+41*cos(5.37+8429.24*Tau)
			+41*cos(2.40+19651.05*Tau)
			+39*cos(6.17+10447.39*Tau)
			+37*cos(6.04+10213.29*Tau)
			+37*cos(2.57+1059.38*Tau)
			+36*cos(1.71+2352.87*Tau)
			+36*cos(1.78+6812.77*Tau)
			+33*cos(0.59+17789.85*Tau)
			+30*cos(0.44+83996.85*Tau)
			+30*cos(2.74+1349.87*Tau)
			+25*cos(3.16+4690.48*Tau);

	var L1=	628331966747
			+206059*cos(2.678235+6283.075850*Tau)
			+4303*cos(2.6351+12566.1517*Tau)
			+425*cos(1.590+3.523*Tau)
			+119*cos(5.796+26.298*Tau)
			+109*cos(2.966+1577.344*Tau)
			+93*cos(2.59+18849.23*Tau)
			+72*cos(1.14+529.69*Tau)
			+68*cos(1.87+398.15*Tau)
			+67*cos(4.41+5507.55*Tau)
			+59*cos(2.89+5223.69*Tau)
			+56*cos(2.17+155.42*Tau)
			+45*cos(0.40+796.30*Tau)
			+36*cos(0.47+775.52*Tau)
			+29*cos(2.65+7.11*Tau)
			+21*cos(5.34+0.98*Tau)
			+19*cos(1.85+5486.78*Tau)
			+19*cos(4.97+213.30*Tau)
			+17*cos(2.99+6275.96*Tau)
			+16*cos(0.03+2544.31*Tau)
			+16*cos(1.43+2146.17*Tau)
			+15*cos(1.21+10977.08*Tau)
			+12*cos(2.83+1748.02*Tau)
			+12*cos(3.26+5088.63*Tau)
			+12*cos(5.27+1194.45*Tau)
			+12*cos(2.08+4694.00*Tau)
			+11*cos(0.77+553.57*Tau)
			+10*cos(1.30+6286.60*Tau)
			+10*cos(4.24+1349.87*Tau)
			+9*cos(2.70+242.73*Tau)
			+9*cos(5.64+951.72*Tau)
			+8*cos(5.30+2352.87*Tau)
			+6*cos(2.65+9437.76*Tau)
			+6*cos(4.67+4690.48*Tau);

	
	var L2=	52919
			+8720*cos(1.0721+6283.0758*Tau)
			+309*cos(0.867+12566.152*Tau)
			+27*cos(0.05+3.52*Tau)
			+16*cos(5.19+26.30*Tau)
			+16*cos(3.68+155.42*Tau)
			+10*cos(0.76+18849.23*Tau)
			+9*cos(2.06+77713.77*Tau)
			+7*cos(0.83+775.52*Tau)
			+5*cos(4.66+1577.34*Tau)
			+4*cos(1.03+7.11*Tau)
			+4*cos(3.44+5573.14*Tau)
			+3*cos(5.14+796.30*Tau)
			+3*cos(6.05+5507.55*Tau)
			+3*cos(1.19+242.73*Tau)
			+3*cos(6.12+529.69*Tau)
			+3*cos(0.31+398.15*Tau)
			+3*cos(2.28+553.57*Tau)
			+2*cos(4.38+5223.69*Tau)
			+2*cos(3.75+0.98*Tau);

	
	var L3=	289*cos(5.844+6283.076*Tau)
			+35
			+17*cos(5.49+12566.15*Tau)
			+3*cos(5.20+155.42*Tau)
			+1*cos(4.72+3.52*Tau)
			+1*cos(5.30+18849.23*Tau)
			+1*cos(5.97+242.73*Tau);

	
	var L4=	114*cos(3.142)
			+8*cos(4.13+6283.08*Tau)
			+1*cos(3.84+12566.15*Tau);

		
	var L5 = 1*cos(3.14);

	//Heliocentric longitude
	var Lh = (L0+(L1+(L2+(L3+(L4+L5*Tau)*Tau)*Tau)*Tau)*Tau)/1e8/(PI/180);
	Lh = simple(Lh);

	//Geocentric longitude
	TrueLs = Lh+180-(9.033/360000);
	TrueLs = simple(TrueLs);


	//Latitude
	var B0=	280*cos(3.199+84334.662*Tau)
			+102*cos(5.422+5507.553*Tau)
			+80*cos(3.88+5223.69*Tau)
			+44*cos(3.70+2352.87*Tau)
			+32*cos(4.00+1577.34*Tau);
	
	var B1=	9*cos(3.90+5507.55*Tau)
			+6*cos(1.73+5223.69*Tau);

	//Heliocentric latitude
	var B = (B0+B1*Tau)/1e8/(PI/180);
	
	//Geocentric latitude
	var Beta = -B;
	Beta = simple(Beta);

	//Corrections
	Ls1 = Lh+180+(-1.397-0.00031*T)*T;
	Ls1 = simple(Ls1);
	DeltaBeta = (3.916/360000)*(rcos(Ls1)-rsin(Ls1));
	TrueBeta=Beta+DeltaBeta

	//Distance earth-sun
	var R0=	100013989
			+1670700*cos(3.0984635+6283.0758500*Tau)
			+13956*cos(3.05525+12566.15170*Tau)
			+3084*cos(5.1985+77713.7715*Tau)
			+1628*cos(1.1739+5753.3849*Tau)
			+1576*cos(2.8469+7860.4194*Tau)
			+925*cos(5.453+11506.770*Tau)
			+542*cos(4.564+3930.210*Tau)
			+472*cos(3.661+5884.927*Tau)
			+346*cos(0.964+5507.553*Tau)
			+329*cos(5.900+5223.694*Tau)
			+307*cos(0.299+5573.143*Tau)
			+243*cos(4.273+11790.629*Tau)
			+212*cos(5.847+1577.344*Tau)
			+186*cos(5.022+10977.079*Tau)
			+175*cos(3.012+18849.228*Tau)
			+110*cos(5.055+5486.778*Tau)
			+98*cos(0.89+6069.78*Tau)
			+86*cos(5.69+15720.84*Tau)
			+86*cos(1.27+161000.69*Tau)
			+65*cos(0.27+17260.15*Tau)
			+63*cos(0.92+529.69*Tau)
			+57*cos(2.01+83996.85*Tau)
			+56*cos(5.24+71430.70*Tau)
			+49*cos(3.25+2544.31*Tau)
			+47*cos(2.58+775.52*Tau)
			+45*cos(5.54+9437.76*Tau)
			+43*cos(6.01+6275.96*Tau)
			+39*cos(5.36+4694.00*Tau)
			+38*cos(2.39+8827.39*Tau)
			+37*cos(0.83+19651.05*Tau)
			+37*cos(4.90+12139.55*Tau)
			+36*cos(1.67+12036.46*Tau)
			+35*cos(1.84+2942.46*Tau)
			+33*cos(0.24+7084.90*Tau)
			+32*cos(0.18+5088.63*Tau)
			+32*cos(1.78+398.15*Tau)
			+28*cos(1.21+6286.60*Tau)
			+28*cos(1.90+6279.55*Tau)
			+26*cos(4.59+10447.39*Tau);

	
	var R1=	103019*cos(1.107490+6283.075850*Tau)
			+1721*cos(1.0644+12566.1517*Tau)
			+702*cos(3.142)
			+32*cos(1.02+18849.23*Tau)
			+31*cos(2.84+5507.55*Tau)
			+25*cos(1.32+5223.69*Tau)
			+18*cos(1.42+1577.34*Tau)
			+10*cos(5.91+10977.08*Tau)
			+9*cos(1.42+6275.96*Tau)
			+9*cos(0.27+5486.78*Tau);

	
	var R2=	4359*cos(5.7846+6283.0758*Tau)
			+124*cos(5.579+12566.152*Tau)
			+12*cos(3.14)
			+9*cos(3.63+77713.77*Tau)
			+6*cos(1.87+5573.14*Tau)
			+3*cos(5.47+18849.23*Tau);

	
	var R3=	145*cos(4.273+6283.076*Tau)
			+7*cos(3.92+12566.15*Tau);
	

	var R4 = 4*cos(2.56+6283.08*Tau);
	
	var R = (R0+(R1+(R2+(R3+R4*Tau)*Tau)*Tau)*Tau)/1e8;

	var Epsilon=Nutation(JDE)[3]
	var DeltaPsi=Nutation(JDE)[0]
	
	
	//Apparent longitude of the sun
	var ApparentLamdaS = TrueLs+DeltaPsi-20.4898/3600/R;
	ApparentLamdaS = simple(ApparentLamdaS);

	//Right ascension of the sun, apparent
	var AlphaS = atan((rsin(ApparentLamdaS)*rcos(Epsilon)-rtan(TrueBeta)*rsin(Epsilon))/rcos(ApparentLamdaS))/(PI/180);
	if (rcos(ApparentLamdaS)<0) AlphaS+=180;
	AlphaS = simple(AlphaS);

	//Declination of the sun, apparent 
	var sDeltaS = asin(rsin(TrueBeta)*rcos(Epsilon)+rcos(TrueBeta)*rsin(Epsilon)*rsin(ApparentLamdaS))/(PI/180);

	//Semidiameter of the sun
	var SDsun = 959.63/R/6000;

	//Horizontal parallax of the sun
	var sPi = asin(rsin(8.794/3600)/R)/(PI/180);

	//Mean longitude of the sun
	var Lsun_mean = 280.4664567+(360007.6982779+(0.03032028+(1/49931+(-1/15299-Tau/1988000)*Tau)*Tau)*Tau)*Tau;
	Lsun_mean = simple(Lsun_mean);
	
	
	//Mean GHA Aries
	var GHAAmean = 280.46061837+ 360.98564736629*(JD-2451545)+ (0.000387933-T1/38710000)*T1*T1;
	GHAAmean = simple(GHAAmean);

 
	//True GHA Aries
	var GHAAtrue = GHAAmean+DeltaPsi*rcos(Epsilon);
	GHAAtrue = simple(GHAAtrue);

	//GHA of the sun
	var GHAsun = GHAAtrue-L-AlphaS;
	GHAsun = simple(GHAsun);

	var p=Earth(L,sPhi,height)[2];
	var psp=Earth(L,sPhi,height)[0];
	var pcp=Earth(L,sPhi,height)[1];

	// correction for RtAscention (To Topocentric)
	var DeltaAlpha=atan((-pcp*rsin(sPi)*rsin(GHAsun))/(rcos(sDeltaS)-pcp*rsin(sPi)*rcos(GHAsun)))/(PI/180);
	if ((rcos(sDeltaS)-pcp*rsin(sPi)*rcos(GHAsun))<0) {DeltaAlpha +=180};
	
	var AlphaS1=AlphaS+DeltaAlpha;
	
	//Correction for Declination (To Topocentric)
	var sDeltaS1=atan(((rsin(sDeltaS)-psp*rsin(sPi))*rcos(DeltaAlpha))/(rcos(sDeltaS)-pcp*rsin(sPi)*rcos(GHAsun)))/(PI/180);
	
	//The lower bit was deleted because the results were not close to actual
 /* //local coordinates A = Azimuth from north , h = altitude from hrizon
	var A=atan(rsin(GHAsun)/(rcos(GHAsun)*rsin(sPhi)-rtan(sDeltaS)*rcos(sPhi)))/(PI/180)+180;
	if ((rcos(GHAsun)*rsin(sPhi)-rtan(sDeltaS)*rcos(sPhi))<0) {A +=180};
	A=simple(A);
	var h=asin(rsin(sPhi)*rsin(sDeltaS)+rcos(sPhi)*rcos(sDeltaS)*rcos(GHAsun))/(PI/180); 
	
	//correction for altitude 
	var Deltah=asin(p*rsin(sPi)*rcos(h))/(PI/180);
	var Apparenth=h-Deltah; */

	
	
	//Test to see conversion from topocentric eccliptic coordinates to horizontal
	var A=atan(rsin(GHAsun)/(rcos(GHAsun)*rsin(sPhi)-rtan(sDeltaS1)*rcos(sPhi)))/(PI/180)+180;
	if ((rcos(GHAsun)*rsin(sPhi)-rtan(sDeltaS1)*rcos(sPhi))<0) {A +=180};
	A=simple(A);
	var h=asin(rsin(sPhi)*rsin(sDeltaS1)+rcos(sPhi)*rcos(sDeltaS1)*rcos(GHAsun))/(PI/180);
	//correction for altitude 
	var Apparenth=h

	// Correction for Refraction
	if (Apparenth>=0 && Apparenth<=90) {
	var Ref=1.02/rtan(Apparenth+(10.03/(Apparenth+5.11)))+0.0019279;
	Apparenth +=Ref/60};
	}
	return new Array (ApparentLamdaS,AlphaS,sDeltaS,A,h,Apparenth,sDeltaS1,AlphaS1,SDsun,sPi,R);
}






function TrueMoonPhase (JDE){
	var SynMonth= 29.530588861;
	var T = (JDE-2451545)/36525;
	var k=Math.round(T*1236.85);
	var x=0;var pt;var Correction;
	var Phase=new Array(4);
	while (x<4){
    pt = 2451550.09766              /* Mean time of phase */
         + SynMonth * k+
         ( 0.00015437 +
         (- 0.000000150
         + 0.00000000073 * T)*T)*T*T;

    var M = 2.5534					/* Sun's mean anomaly */
        	+ 29.10535670 * k+
        	(- 0.0000014
        	- 0.00000011 * T)*T*T;
	M=simple(M);
    var M1 = 201.5643               /* Moon's mean anomaly */
        	+ 385.81693528 * k
        	+ (0.0107582
        	+ (0.00001238
        	- 0.000000058 * T)*T)*T*T;
	M1=simple(M1);

	    var F = 160.7108				/* Moon's argument of latitude */
        	+ 390.67050284 * k+
        	(- 0.0016118+
        	(- 0.00000227
        	+ 0.000000011 * T)*T)*T*T;
	F=simple(F);
	var Omega= 124.7746				/* Longitude of ascending node of lunar orbit */
		- 1.56375588 * k
		+( 0.0020672
		+ 0.00000215 * T)*T*T;
	Omega=simple(Omega);
	var E=1+(-0.002516-0.0000074*T)*T; /*Eccentricity of Earth's orbit around sun */
	var E2=E*E;
			
	//Planetary arguments	
    var A1=simple(299.77 + 0.107408 * k - 0.009173 * T*T);
    var A2=simple(251.88 + 0.016321 * k);
    var A3=simple(251.83 + 26.651886 * k);
    var A4=simple(349.42 + 36.412478 * k);
    var A5=simple(84.66 + 18.206239 * k);
    var A6=simple(141.74 + 53.303771 * k);
    var A7=simple(207.14 + 2.453732 * k);
    var A8=simple(154.84 + 7.306860 * k);
    var A9=simple(34.52 + 27.261239 * k);
    var A10=simple(207.19 + 0.121824 * k);
    var A11=simple(291.34 + 1.844379 * k);
    var A12=simple(161.72 + 24.198154 * k);
    var A13=simple(239.56 + 25.513099 * k);
    var A14=simple(331.55 + 3.592518 * k);
 
    //corrections for New Moon
   if (x==0){Correction=
    			-0.40720	*rsin(M1)
    			+(0.17241*E)*rsin(M)
    			+0.01608	*rsin(2*M1)
    			+0.01039	*rsin(2*F)
 			+(0.00739*E)*rsin(M1-M)
    			-(0.00514*E)*rsin(M1+M)
    			+(0.00208*E2)*rsin(2*M)
    			-0.00111	*rsin(M1-2*F)
    			-0.00057	*rsin(M1+2*F)
    			+(0.00056*E)*rsin(2*M1+M)
    			-0.00042	*rsin(3*M1)
    			+(0.00042*E)*rsin(M+2*F)
    			+(0.00038*E)*rsin(M-2*F)
    			-(0.00024*E)*rsin(2*M1-M)
    			-0.00017	*rsin(Omega)
    			-0.00007	*rsin(M1+2*M)
    			+0.00004	*rsin(2*M1-2*F)
    			+0.00004	*rsin(3*M)
    			+0.00003	*rsin(M1+M-2*F)
    			+0.00003	*rsin(2*M1+2*F)
    			-0.00003	*rsin(M1+M+2*F)
    			+0.00003	*rsin(M1-M+2*F)
    			-0.00002	*rsin(M1-M-2*F)
    			-0.00002	*rsin(3*M1+M)
    			+0.00002	*rsin(4*M1);
    pt += Correction;}
 
//Correction for full Moon
  if (x==2) {Correction=
    			-0.40614	*rsin(M1)
    			+(0.17302*E)*rsin(M)
    			+0.01614	*rsin(2*M1)
    			+0.01043	*rsin(2*F)
 			+(0.00734*E)*rsin(M1-M)
    			-(0.00515*E)*rsin(M1+M)
    			+(0.00209*E2)*rsin(2*M)
    			-0.00111	*rsin(M1-2*F)
    			-0.00057	*rsin(M1+2*F)
    			+(0.00056*E)*rsin(2*M1+M)
    			-0.00042	*rsin(3*M1)
    			+(0.00042*E)*rsin(M+2*F)
    			+(0.00038*E)*rsin(M-2*F)
    			-(0.00024*E)*rsin(2*M1-M)
    			-0.00017	*rsin(Omega)
    			-0.00007	*rsin(M1+2*M)
    			+0.00004	*rsin(2*M1-2*F)
    			+0.00004	*rsin(3*M)
    			+0.00003	*rsin(M1+M-2*F)
    			+0.00003	*rsin(2*M1+2*F)
    			-0.00003	*rsin(M1+M+2*F)
    			+0.00003	*rsin(M1-M+2*F)
    			-0.00002	*rsin(M1-M-2*F)
    			-0.00002	*rsin(3*M1+M)
    			+0.00002	*rsin(4*M1);
    pt += Correction;}			
 

// Correction for First & Last Quarter
  if (x==3 || x==1) {Correction=
    			-0.62801	*rsin(M1)
    			+(0.17172*E)*rsin(M)
    			-(0.01183*E)*rsin(M1+M)
    			+0.00862	*rsin(2*M1)
 			+0.00804    *rsin(2*F)
    			+(0.00454*E)*rsin(M1-M)
    			+(0.00204*E2)*rsin(2*M)
    			-0.00180	*rsin(M1-2*F)
    			-0.00070	*rsin(M1+2*F)
    			-0.00040	*rsin(3*M1)
    			-(0.00034*E)*rsin(2*M1-M)
    			+(0.00032*E)*rsin(M+2*F)
    			+(0.00032*E)*rsin(M-2*F)
    			-(0.00028*E2)*rsin(M1+2*M)
    			+(0.00027*E)*rsin(2*M1+M)
    			-0.00017	*rsin(Omega)
    			-0.00005	*rsin(M1-M-2*F)
    			+0.00004	*rsin(2*M1+2*F)
    			-0.00004	*rsin(M1+M+2*F)
    			+0.00004	*rsin(M1-2*M)
    			+0.00003	*rsin(M1+M-2*F)
    			+0.00003	*rsin(3*M)
    			+0.00002	*rsin(2*M1-2*F)
    			+0.00002	*rsin(M1-M+2*F)
    			-0.00002	*rsin(3*M1+M);
    	pt += Correction;
	var W=	0.00306
			-0.00038*E*rcos(M)
			+0.00026*rcos(M1)
			-0.00002*rcos(M1-M)
			+0.00002*rcos(M1+M)
			+0.0000*rcos(2*F);
	if (x==3){pt -=W} else {pt +=W};}

 	Correction2=
				0.000325*rsin(A1)
				+0.000165*rsin(A2)
				+0.000164*rsin(A3)
				+0.000126*rsin(A4)
				+0.000110*rsin(A5)
				+0.000062*rsin(A6)
				+0.000060*rsin(A7)
				+0.000056*rsin(A8)
				+0.000047*rsin(A9)
				+0.000042*rsin(A10)
				+0.000040*rsin(A11)
				+0.000037*rsin(A12)
				+0.000035*rsin(A13)
				+0.000023*rsin(A14);
	pt += Correction2;
	Phase[x]=pt;
	k += 0.25
	x +=1;}
    	return new Array (Phase[0],Phase[1],Phase[2],Phase[3]);

}


function Siderial (JD,Alpha,sDelta,L,sPhi,Delta,height,moon) {
	
	var JDE=JD+UtE(JD)/86400;
	var Epsilon=Nutation(JDE)[3];
	var DeltaPsi=Nutation(JDE)[0];
	var T =(JD-2451545.0)/36525;
	var T5=(T4=(T3=(T2=T*T)*T)*T)*T;
	var Th=(Math.floor(JD-0.5)+.5-2451545.0)/36525;
	var Th5=(Th4=(Th3=(Th2=Th*Th)*Th)*Th)*Th;
	var sPhi1=Earth(L,sPhi,height)[3]
	
	//Mean Siderial Time at Greenwich at 0h UT
	var cTheta0=100.46061837+(36000.770053608+(0.000387933-Th/38710000)*Th)*Th;
	cTheta0=simple(cTheta0);
	
	//Mean Siderial Time at Greenwich
	/*var Theta0=280.46061837+360.98564736629*(JD-2451545.0)+(0.000387933-T/38710000)*T*T;*/
	var Theta0=cTheta0+1.00273790935*(JD-(Math.floor(JD-0.5)+0.5))*24*15
	Theta0=simple(Theta0);

	// Apparent Siderial Time at Greenwich (Hour angle of True Vernal equinox)
	var ApparentTheta0=Theta0+DeltaPsi*rcos(Epsilon);
	ApparentTheta0=simple(ApparentTheta0);

	//Local Hour angle measured westward from south, apparent alpha should be used if not Theta0 should be used
	var H=ApparentTheta0-L-Alpha;
	
	//local coordinates A = Azimuth from north , h = altitude from hrizon
	var A=Math.atan(rsin(H)/(rcos(H)*rsin(sPhi)-rtan(sDelta)*rcos(sPhi)))/(Math.PI/180)+180;
	if ((rcos(H)*rsin(sPhi)-rtan(sDelta)*rcos(sPhi))<0) {A +=180};
	var h=Math.asin(rsin(sPhi)*rsin(sDelta)+rcos(sPhi)*rcos(sDelta)*rcos(H))/(Math.PI/180); 

	//Geocentric Horizontal Coordinates
	var A1=Math.atan(rsin(H)/(rcos(H)*rsin(sPhi1)-rtan(sDelta)*rcos(sPhi1)))/(Math.PI/180)+180;
	if ((rcos(H)*rsin(sPhi1)-rtan(sDelta)*rcos(sPhi1))<0) {A1 +=180};
	var h1=Math.asin(rsin(sPhi1)*rsin(sDelta)+rcos(sPhi1)*rcos(sDelta)*rcos(H))/(Math.PI/180); 


	//parallax arguments Delta is in AU

	var p=Earth(L,sPhi,height)[2];
	var psp=Earth(L,sPhi,height)[0];
	var pcp=Earth(L,sPhi,height)[1];



	var sPi=Math.asin(rsin(8.794/3600)/Delta)/(Math.PI/180);
	if (moon==1){sPi=Moon(JDE)[11]};
	
	//correction for altitude 
	Deltah=Math.asin(p*rsin(sPi)*rcos(h))/(Math.PI/180);
	Apparenth=h-Deltah;

	
	// Correction for Refraction
	if (Apparenth>=0 && Apparenth<=90) {
	var R=1/(rtan(Apparenth+(7.31/(Apparenth+4.4)))/(Math.PI/180))+0.0013515;
	Apparenth +=R}
	
	
	// correction for RtAscention (To Topocentric)
	DeltaAlpha=Math.atan((-pcp*rsin(sPi)*rsin(H))/(rcos(sDelta)-pcp*rsin(sPi)*rcos(H)))/(Math.PI/180);
	if ((rcos(sDelta)-pcp*rsin(sPi)*rcos(H))<0) {DeltaAlpha +=180};
	
	Alpha1=Alpha+DeltaAlpha;
	
	//Correction for Declination (To Topocentric)
	sDelta1=Math.atan(((rsin(sDelta)-psp*rsin(sPi))*rcos(DeltaAlpha))/(rcos(sDelta)-pcp*rsin(sPi)*rcos(H)))/(Math.PI/180);
	return new Array (cTheta0,Theta0,ApparentTheta0,H,A,h,Apparenth,Alpha1,sDelta1,A1,h1);
	}	
	
	
function Earth(L,sPhi,height) {
// 1976 IAU f=earth's flattening,  a = equatorial radius, p = polar radius (km)
with (Math) {
var a=6378.14;
var f=1/298.257;
var b=a*(1-f);
// height in metres
// pcossPhi1(pcp) & psinsPhi1 (psp) are needed for parallaxes, p being distance to centre of earth in units of equatorial radius
var u=atan((b/a)*rtan(sPhi))/(PI/180);
var p=0.9983271+0.0016764*rcos(2*sPhi)-0.0000035*rcos(4*sPhi)+(height/6378140);
var psp= (b/a)*rsin(u)+(height/6378140)*rsin(sPhi);
var pcp= rcos(u)+(height/6378140)*rcos(sPhi);
//Geocentric Observer Latitude Latitude sPhi1
sPhi1=(-692.73*rsin(2*sPhi)+1.16*rsin(4*sPhi))/3600+sPhi;
}
return new Array(psp,pcp,p,sPhi1)
}



function RTS (JD,L,sPhi,SM) {
with (Math) {
var JDzh=floor(JD-0.5)+0.5;
if (SM==0) {var Alpha1=Moon(JDzh-1)[9];
var Alpha2=Moon(JDzh)[9];
var Alpha3=Moon(JDzh+1)[9];
var sDelta1=Moon(JDzh-1)[10];
var sDelta2=Moon(JDzh)[10];
var sDelta3=Moon(JDzh+1)[10];
//h0 =-34/60 for stars & planets =50/60 for sun & for moon =.7275 * eq hor parallax -34/60
var h0=0.7275*Moon(JDzh)[11]-34/60;
} else {
var Alpha1=Solar(JDzh-1)[1];
var Alpha2=Solar(JDzh)[1];
var Alpha3=Solar(JDzh+1)[1];
var sDelta1=Solar(JDzh-1)[2];
var sDelta2=Solar(JDzh)[2];
var sDelta3=Solar(JDzh+1)[2];
h0=-50/60}
if (abs((rsin(h0)-rsin(sPhi)*rsin(sDelta2))/(rcos(sPhi)*rcos(sDelta2)))<=1) {
var H0=acos((rsin(h0)-rsin(sPhi)*rsin(sDelta2))/(rcos(sPhi)*rcos(sDelta2)))/(PI/180);
if (H0>180) {H0 -=180};
var cTheta0=Siderial(JDzh,Alpha2,sDelta2,L,sPhi)[0];
var m=new Array(3);
var H=new Array(3);
var h=new Array(3);
var h02=new Array(3);
var Theta0= new Array(3);
var Deltam=new Array(3);
var c= new Array(3)
	m[0]=(Alpha2+L-cTheta0)/360;
	m[1]=m[0]-(H0/360);
	m[2]=m[0]+(H0/360);
var aAlpha=Alpha2-Alpha1;
var bAlpha=Alpha3-Alpha2;
var cAlpha=bAlpha-aAlpha;
var aDelta=sDelta2-sDelta1;
var bDelta=sDelta3-sDelta2;
var cDelta=bDelta-aDelta;
var DeltaT=UtE(JDzh);
var x=0;var y=0;var n=0; var Alpha=0;var sDelta=0
while (y<5) {
x=0;
while (x<3) {
c[x]=0
// Next line is to make M always between 0 and 1 And so results are always in the same day
if (abs(m[x]-0.5)>0.5) {c[x]=abs(m[x])/m[x]; m[x]=m[x]-c[x]}
Theta0[x]=cTheta0+360.985647*m[x];
n=m[x]+DeltaT/86400;
Alpha=Alpha2+(n/2)*(aAlpha+bAlpha+n*cAlpha);
sDelta=sDelta2+(n/2)*(aDelta+bDelta+n*cDelta);
H[x]=Theta0[x]-L-Alpha;
h[x]=asin(rsin(sPhi)*rsin(sDelta)+rcos(sPhi)*rcos(sDelta)*rcos(H[x]))/(PI/180);;
h02[x]=h0;
if (SM==0) {h02[x]=0.7275*Moon(JDzh+m[x]+DeltaT/86400)[11]-34/60};
if (x==0){Deltam[0]=(-H[0]/360)} else {Deltam[x]=(h[x]-h02[x])/(360*rcos(sDelta)*rcos(sPhi)*rsin(H[x]))};
m[x] +=(Deltam[x]);
if (abs(m[x]-0.5)>0.5) {c[x]=abs(m[x])/m[x]; m[x]=m[x]-c[x]}
x +=1;}
y +=1;}
return new Array (m[1],m[0],m[2]);}
else {return new Array(-99,-99,-99)};
}
}


function Nutation (JDE) {

	var	T = (JDE-2451545)/36525;
	var T5=(T4=(T3=(T2=T*T)*T)*T)*T;
	var D=simple(297.85036+(445267.111480+(-0.0019142+T/198474)*T)*T);
	var M=simple(357.52772+(35999.050340+(-0.0001603-T/300000)*T)*T);
	var M1=simple(134.96298+(477198.867398+(0.0086972+T/56250)*T)*T);
	var F=simple(93.27191+(483202.017538+(-0.0036825+T/327270)*T)*T);
	var Omega=simple(125.04452+(-1934.136261+(0.0020708+T/450000)*T)*T);
	var DeltaPsi = 		
				(-171996-174.2*T)*rsin(Omega)
				+(-13187-1.6*T)*rsin(-2*D+2*F+2*Omega)
				+(-2274-0.2*T)*rsin(2*F+2*Omega)
				+(2062+0.2*T)*rsin(2*Omega)
				+(1426-3.4*T)*rsin(M)
				+(712+0.1*T)*rsin(M1)
				+(-517+1.2*T)*rsin(-2*D+M+2*F+2*Omega)
				+(-386-0.4*T)*rsin(2*F+Omega)
				-301*rsin(M1+2*F+2*Omega)
				+(217-0.5*T)*rsin(-2*D-M+2*F+2*Omega)
				-158*rsin(-2*D+M1)
				+(129+0.1*T)*rsin(-2*D+2*F+Omega)
				+123*rsin(-M1+2*F+2*Omega)
				+63*rsin(2*D)
				+(63+0.1*T)*rsin(M1+Omega)
				-59*rsin(2*D-M1+2*F+2*Omega)
				+(-58-0.1*T)*rsin(-M1+Omega)
				-51*rsin(M1+2*F+Omega)
				+48*rsin(-2*D+2*M1)
				+46*rsin(-2*M1+2*F+Omega)
				-38*rsin(2*D+2*F+2*Omega)
				-31*rsin(2*M1+2*F+2*Omega)
				+29*rsin(2*M1)
				+29*rsin(-2*D+M1+2*F+2*Omega)
				+26*rsin(2*F)
				-22*rsin(-2*D+2*F)
				+21*rsin(-M1+2*F+Omega)
				+(17-0.1*T)*rsin(2*M)
				+16*rsin(2*D-M1+Omega)
				+(-16+0.1*T)*rsin(-2*D+2*M+2*F+2*Omega)
				-15*rsin(M+Omega)
				-13*rsin(-2*D+M1+Omega)
				-12*rsin(-M+Omega)
				+11*rsin(2*M1-2*F)
				-10*rsin(2*D-M1+2*F+Omega)
				-8*rsin(2*D+M1+2*F+2*Omega)
				+7*rsin(M+2*F+2*Omega)
				-7*rsin(-2*D+M+M1)
				-7*rsin(-M+2*F+2*Omega)
				-7*rsin(2*D+2*F+Omega)
				+6*rsin(2*D+M1)
				+6*rsin(-2*D+2*M1+2*F+2*Omega)
				+6*rsin(-2*D+M1+2*F+Omega)
				-6*rsin(2*D-2*M1+Omega)
				-6*rsin(2*D+Omega)
				+5*rsin(-M+M1)
				-5*rsin(-2*D-M+2*F+Omega)
				-5*rsin(-2*D+Omega)
				-5*rsin(2*M1+2*F+Omega)
				+4*rsin(-2*D+2*M1+Omega)
				+4*rsin(-2*D+M+2*F+Omega)
				+4*rsin(M1-2*F)	
				-4*rsin(-D+M1)
				-4*rsin(-2*D+M)
				-4*rsin(D)
				+3*rsin(M1+2*F)
				-3*rsin(-2*M1+2*F+2*Omega)
				-3*rsin(-D-M+M1)
				-3*rsin(M+M1)
				-3*rsin(-M+M1+2*F+2*Omega)
				-3*rsin(2*D-M-M1+2*F+2*Omega)
				-3*rsin(3*M1+2*F+2*Omega)
				-3*rsin(2*D-M+2*F+2*Omega);
							
				var DeltaEpsilon =
				(92025+8.9*T)*rcos(Omega)
				+(5736-3.1*T)*rcos(-2*D+2*F+2*Omega)
				+(977-0.5*T)*rcos(2*F+2*Omega)
				+(-895+0.5*T)*rcos(2*Omega)
				+(54-0.1*T)*rcos(M)
				-7*rcos(M1)	
				+(224-0.6*T)*rcos(-2*D+M+2*F+2*Omega)
				+200*rcos(2*F+Omega)	
				+(129-0.1*T)*rcos(M1+2*F+2*Omega)
				+(-95+0.3*T)*rcos(-2*D-M+2*F+2*Omega)
				-70*rcos(-2*D+2*F+Omega)
				-53*rcos(-M1+2*F+2*Omega)
				-33*rcos(M1+Omega)
				+26*rcos(2*D-M1+2*F+2*Omega)
				+32*rcos(-M1+Omega)
				+27*rcos(M1+2*F+Omega)	
				-24*rcos(-2*M1+2*F+Omega)
				+16*rcos(2*D+2*F+2*Omega)
				+13*rcos(2*M1+2*F+2*Omega)	
				-12*rcos(-2*D+M1+2*F+2*Omega)
				-10*rcos(-M1+2*F+Omega)
				-8*rcos(2*D-M1+Omega)
				+7*rcos(-2*D+2*M+2*F+2*Omega)
				+9*rcos(M+Omega)
				+7*rcos(-2*D+M1+Omega)
				+6*rcos(-M+Omega)
				+5*rcos(2*D-M1+2*F+Omega)
				+3*rcos(2*D+M1+2*F+2*Omega)
				-3*rcos(M+2*F+2*Omega)
				+3*rcos(-M+2*F+2*Omega)
				+3*rcos(2*D+2*F+Omega)
				-3*rcos(-2*D+2*M1+2*F+2*Omega)
				-3*rcos(-2*D+M1+2*F+Omega)
				+3*rcos(2*D-2*M1+Omega)
				+3*rcos(2*D+Omega)
				+3*rcos(-2*D-M+2*F+Omega)
				+3*rcos(-2*D+Omega)
				+3*rcos(2*M1+2*F+Omega)




// Coeff were given in units of .0001 seconds

	DeltaPsi = DeltaPsi*0.0001;
	DeltaEpsilon = DeltaEpsilon*0.0001;

// Obliquity in ecliptic is Epsilon0

var U=T/100;
var U10=(U9=(U8=(U7=(U6=(U5=(U4=(U3=(U2=U*U)*U)*U)*U)*U)*U)*U)*U)*U;
var Epsilon0=			(84381.448+
						(-4680.93+
						(-1.55
						+(1999.25+
						(-51.38+
						(-249.67+
						(-39.05
						+(7.12
						+(27.87
						+(5.79
						+2.45*U)*U)*U)*U)*U)*U)*U)*U)*U)*U);
						
//var Epsilon0 =84381.448-46.815*T-0.00059*T2+0.001813*T3;						

// True Obliquity of Ecliptic is Epsilon
var Epsilon = Epsilon0+DeltaEpsilon;

return new Array (DeltaPsi/3600,DeltaEpsilon/3600,Epsilon0/3600,Epsilon/3600,D,M,M1,F,Omega);
}


//functions to convert UT to Local Mean Time

function LtoUT (Local,L) {
var UT=Local-L/360;
return UT;}

function UTtoL (UT,L) {
var Local=UT+L/360;
return Local;}

//functions to convert between UT & Standard Zone Time

function UTtoS (UT,L) {
var Standard=UT+(1/24)*L;
return Standard;}

function StoUT (Standard,L) {
var UT=Standard-(1/24)*L;
return UT;}

//functions to convert between Local Mean Time & Standard Zone Time

function LtoS (Local,L) {
var Standard=UTtoS(LtoUT(Local,L),L);
return Stndard;}

function StoL (Standard,L) {
var Local=UTtoL(StoUT(Standard,L),L);
return Local;}


function UtE (JD) {
var T=(JD-2451545)/36525;
var O=(JD-2415021)/36525;
var T2=T*T;
var O2=O*O;
var O3=O2*O;
var O4=O3*O;
var O5=O4*O;
var O6=O5*O;
var O7=O6*O;
var O8=O7*O;
var O9=O8*O;
var O10=O9*O;
var DeltaT=102+102*T+25.3*T2;
if (JD<2488070){
	DeltaT +=(.37*((JD-2488070)/365.25))}
if (JD<2451545){
	DeltaT=-2.44+87.24*O+815.20*O2-2637.80*O3-18756.33*O4+124906.15*O5-303191.19*O6
	+372919.88*O7-232424.66*O8+58353.42*O9;}
if (JD<2415021){
	DeltaT=-2.50+228.95*O+5218.61*O2+56282.84*O3+324011.78*O4+1061660.75*O5+2087298.89*O6
	+2513807.78*O7+1818961.41*O8+727058.63*O9+123563.95*O10;}
if (JD<2378497){
	DeltaT=102+102*T+25.3*T2;}
if (JD<2067310){
	DeltaT=2177+497*T+44.1*T2}
return DeltaT}

function RtAscension (Lamda,Epsilon,Beta) {
	var Alpha=Math.atan((rsin(Lamda)*rcos(Epsilon)-rtan(Beta)*rsin(Epsilon))/rcos(Lamda))/(Math.PI/180);
	if (rcos(Lamda)<0) {Alpha+=180};
	Alpha=simple(Alpha);
	return Alpha;}
	
function Declination (Lamda,Epsilon,Beta) {
	var Delta=Math.asin(rsin(Beta)*rcos(Epsilon)+rcos(Beta)*rsin(Epsilon)*rsin(Lamda))/(Math.PI/180);
	return Delta;}
	
function Azimuth (H,sPhi,sDelta) {
	var A= Math.atan(rsin(H)/(rcos(H)*rsin(sPhi)-rtan(sDelta)*rcos(sPhi)))/(Math.PI/180);
	if ((rcos(H)*rsin(sPhi)-rtan(sDelta)*rcos(sPhi))<0) {A +=180};
	return simple(A+180);}
	
function Altitude (H,sPhi,sDelta) {	
	var h=Math.asin(rsin(sPhi)*rsin(sDelta)+rcos(sPhi)*rcos(sDelta)*rcos(H))/(Math.PI/180);
	return h;}

//Chinese
function mean_tropical_year(){
return 365.242189}

function winter() {
return 270}

function DeltaT(JD){
var T=(JD-2451545)/36525;
var O=(JD-2415021)/36525;
var T2=T*T;
var O2=O*O;
var O3=O2*O;
var O4=O3*O;
var O5=O4*O;
var O6=O5*O;
var O7=O6*O;
var O8=O7*O;
var O9=O8*O;
var O10=O9*O;
var DeltaT=102+102*T+25.3*T2;
if (JD<2488070){
	DeltaT +=(.37*((JD-2488070)/365.25))}
if (JD<2451545){
	DeltaT=-2.44+87.24*O+815.20*O2-2637.80*O3-18756.33*O4+124906.15*O5-303191.19*O6
	+372919.88*O7-232424.66*O8+58353.42*O9;}
if (JD<2415021){
	DeltaT=-2.50+228.95*O+5218.61*O2+56282.84*O3+324011.78*O4+1061660.75*O5+2087298.89*O6
	+2513807.78*O7+1818961.41*O8+727058.63*O9+123563.95*O10;}
if (JD<2378497){
	DeltaT=102+102*T+25.3*T2;}
if (JD<2067310){
	DeltaT=2177+497*T+44.1*T2}
return DeltaT}



function universal_to_dynamic(JD){
return JD+DeltaT(JD)/86400}

function universal_from_dynamic(JDE){
return JDE-DeltaT(JDE)/86400}




//Apparent Solar Longitude at a speccific JDE
function solar_longitude(date){
return Solar(universal_to_dynamic(date))[0]}

//Next Julian Day after JD when sun is at degree Phi
function solar_longitude_after(t,Phi){
var e=1e-6
var rate=mean_tropical_year()/360
var tau=t+rate*((Phi-solar_longitude(t)) %360)
var l=Math.max(t,tau-5)
var u=tau+5
while ((u-l)>=e){
var x=(l+u)/2
if (((solar_longitude(x)-Phi)%360)<180) {u=x} else {l=x}} 
return x}

//Gregorian year from JD
function Gregorian_year_from_JD(JD){
return JD_to_Gregorian(JD12(JD))[0]}

//New moon before date
function new_moon_before(JD){
var new_moon=universal_from_dynamic(TrueMoonPhase(universal_to_dynamic(JD))[0]);
if (new_moon>=JD) {new_moon=universal_from_dynamic(TrueMoonPhase(universal_to_dynamic(JD-mean_synodic_month()))[0])};
return new_moon}

function new_moon_after(JD){
var new_moon=universal_from_dynamic(TrueMoonPhase(universal_to_dynamic(JD))[0]);
if (new_moon<JD) {new_moon=universal_from_dynamic(TrueMoonPhase(universal_to_dynamic(JD+mean_synodic_month()))[0])};
return new_moon}

function JD12(JD){
return (Math.floor(JD-0.5)+0.5)}

//Solar terms

function estimate_prior_solar_longitude(t,Phi){
var rate=mean_tropical_year()/360
var tau=t-rate*(simple(solar_longitude(t)-Phi)%360)
var Delta=((solar_longitude(tau)-Phi+180)%360)-180
return Math.min(t,tau-rate*Delta)}

function current_major_solar_term(date){
var s=solar_longitude (universal_from_standard (date,chinese_location(date)))
return amod((2+Math.floor(s/30)),12)}

function chinese_location(t){
var year=Gregorian_year_from_JD(t)
if (year<1929){return new Array(39.55,-(116+25/60),43.5,1397/180)} else {
return new Array(39.55,-(116+25)/60,43.5,8)}}

function chinese_solar_longitude_on_or_after(date,theta) {
var t=solar_longitude_after(universal_from_standard(date,chinese_location(date)),theta)
return standard_from_universal(t,chinese_location(t))}

function major_solar_term_on_or_after(date){
var l=30*Math.ceiling(solar_longitude(midight_in_china(date))/30) %360
return chinese_solar_longitude_on_or_after(date,l)}

function current_minor_solar_term(date){
var s=solar_longitude(midnight_in_cina(date))
return amod(3+Math.floor((s-15)/30),12)}

function minor_solar_term_on_or_after_date(date){
var l=(30*Math.ceiling((solar_longitude(midight_in_china(date))-15)/30)+15) %360
return chinese_solar_longitude_on_or_after(date,l)}

function midnight_in_china(date){
return universal_from_standard(date,chinese_location(date))}

function chinese_winter_solstice_on_or_before(date) {
var approx=estimate_prior_solar_longitude(midnight_in_china(date+1),winter())
var day=JD12(approx)-1
while (winter() > solar_longitude(midnight_in_china(day+1))) {day +=1}
return day}

//Months
function chinese_new_moon_on_or_after(date){
var t=new_moon_after(midnight_in_china(date))
return JD12(standard_from_universal(t,chinese_location(t)))}

function chinese_new_moon_before(date){
var t=new_moon_before(midnight_in_china(date))
return JD12(standard_from_universal(t,chinese_location(t)))}

function no_major_solar_term_q(date){
return current_major_solar_term(date)==current_major_solar_term(chinese_new_moon_on_or_after(date+1))}

function prior_leap_month_q(m1,m){
return m>=m1 && (no_major_solar_term_q(m) || prior_leap_month_q(m1,chinese_new_moon_before(m)))}

function chinese_new_year_in_sui(date){
var s1=chinese_winter_solstice_on_or_before(date)
var s2=chinese_winter_solstice_on_or_before(s1+370)
var m12=chinese_new_moon_on_or_after(s1+1)
var m13=chinese_new_moon_on_or_after(m12+1)
var next_m11=chinese_new_moon_before(s2+1)
if (Math.round((next_m11-m12)/mean_synodic_month())==12 && (no_major_solar_term_q(m12) || no_major_solar_term_q(m13))) {return chinese_new_moon_on_or_after(m13+1)} else {return m13}}

function chinese_new_year_on_or_before(date){
var new_year=chinese_new_year_in_sui(date)
if (date>=new_year) {return new_year} else {return chinese_new_year_in_sui(date-180)}}

//conversion to and from JD dates

function chinese_epoch(){
return JD_from_Gregorian_or_Julian(-2636,2,15)}

function chinese_from_JD(date){
var s1=chinese_winter_solstice_on_or_before(date)
var s2=chinese_winter_solstice_on_or_before(s1+370)
var m12=chinese_new_moon_on_or_after(s1+1)
var next_m11=chinese_new_moon_before(s2+1)
var m=chinese_new_moon_before(date+1)
var leap_year= Math.round((next_m11-m12)/mean_synodic_month())==12
var month2=0
if (leap_year && prior_leap_month_q(m12,m)) {month2=1}
var month=amod(Math.round((m-m12)/mean_synodic_month())-month2,12)
var leap_month=leap_year && no_major_solar_term_q(m) && (! prior_leap_month_q(m12,chinese_new_moon_before(m)))
var elapsed_years=Math.floor(1.5-month/12+((date-chinese_epoch())/mean_tropical_year()))
var cycle=Math.floor((elapsed_years-1)/60)+1
var year=amod(elapsed_years,60)
var day=date-m+1
var true_year=(cycle-1)*60+year
return new Array (cycle,year,month,leap_month,day,elapsed_years)}

function JD_from_chinese(cycle,year,month,leap,day){
var mid_year=Math.floor(chinese_epoch()+((cycle-1)*60+year-1+0.5)*mean_tropical_year())
var new_year=chinese_new_year_on_or_before(mid_year)
var p=chinese_new_moon_on_or_after (new_year+(month-1)*29)
var d=chinese_from_JD(p)
var prior_new_moon=chinese_new_moon_on_or_after(p+1)
if (month==d[2] && leap==d[3]) {prior_new_moon=p}
return prior_new_moon+day-1}

//seagesimal Cycle of names

function chinese_sexagesimal_name(n){
return new Array (amod(n,10), amod(n,12))}

function chinese_name_difference(stem1branch1,stem2branch2){
var stem_difference=stem2branch2[0]-stem1branch1[0];
var branch_difference=stem2branch2[1]-stem1branch1[1];
return ((stem_difference-1+25*(branchdifference_stem_difference))%60)+1}

function chinese_name_of_year(year){
//Stem Names Celestial,Terrestrial,terrestrial translation
var year_name= new Array(
"Jia","zi","Rat",
"Yi","chou","Ox",
"Bing","yin","Tiger",
"Ding","mao","Hare",
"Wu","chen","Dragon",
"Ji","si","Snake",
"Geng","wu","Horse",
"Xin","wei","Ram",
"Ren","shen","Monkey",
"Gui","you","Fowl",
"Jia","xu","Dog",
"Yi","hai","Pig")
celestial_stem=year_name[(chinese_sexagesimal_name(year)[0]-1)*3]
terrestrial_stem=year_name[(chinese_sexagesimal_name(year)[1]-1)*3+1]
terrestrial_stem_translation=year_name[(chinese_sexagesimal_name(year)[1]-1)*3+2]
return new Array(celestial_stem,terrestrial_stem,terrestrial_stem_translation)}

function chinese_month_name_epoch() {
return 3}

function chinese_name_of_month(year,month){
var elapsed_months=12*(year-1)+month-1
return chinese_sexagesimal_name(elapsed_monrhs+chinese_month_name_epoch())}

function chinese_day_name_epoch(){
return 15}

function chinese_name_of_day(date){
return chinese_sexagesimal_name(date+chinese_day_name_epoch)}

function chinese_day_name_on_or_before(name,date){
return (date-((date+chinese_name_difference(name,chinese_sexagesimal_name(chinese_day_name_epoch())))%60))}

function amod(a,b){
var m=a%b
if (m==0){m=b}
if (m<0) {m +=b}
return m}

function universal_from_standard(ts,locale){
return ts-1/24*locale[3]}

function standard_from_universal(ts,locale){
return ts+1/24*locale[3]}

function mean_synodic_month(){
return 29.530588853}



//Code used to validate, and control the user interface of the 
//GeoHijri Control

		function getDateArray( txtSource )
		{
			var toSplitOn = "-";
			if( txtSource.value.indexOf("/") > 0 )
				toSplitOn = "/";
			var theArray = txtSource.value.split( toSplitOn );
			for(var i = 0; i < 3; i++)
			{
				theArray[i] = parseInt( theArray[i] );
				if (  isNaN( theArray[i] ) ) theArray[i] = -1;
			}
			return theArray;
		}

		function setDateText( txtDateInput, theDiv, dateType )
		{
			txtHijri.value = hijri_date[2]+" / " + MonthName(hijri_date[1],'Hijri') + " / " + AhBh(hijri_date[0],"Hijri");
			txtGregorian.value = gregorian_date[2]+" / "+MonthName(gregorian_date[1],'Grgorian')+" / "+AhBh(gregorian_date[0],"Gregorian")+" Gregorian";
		}

		function Gregorian_To_Hirji( txtGregorian, txtHijri )
		{
			if(txtGregorian.value == "" || txtHijri.value != "")
				return false;
			var JD
			var theDate = getDateArray( txtGregorian );
			if( (theDate[0] != -1) && (theDate[1] != -1) && (theDate[2] != -1) )
			{
				var Y = eval( theDate[2] );
				var M = eval( theDate[1] );
				var D = eval( theDate[0] );
				var gregorian_date = new Array(Y,M,D);
				JD = JD_from_Gregorian_or_Julian(Y,M,D);
				var MJD=JD-2400000.5;
				var hijri_date= Toh( JD );
				txtHijri.value = hijri_date[2]+"/" + hijri_date[1] + "/" + hijri_date[0];
				var hijriValue = hijri_date[2]+" " + MonthName(hijri_date[1],'Hijri') + " " + AhBh(hijri_date[0],"Hijri");
				txtHijri.title = hijriValue;
			}
			
			if( ! isDateComplete( txtGregorian ) )
			{
				txtGregorian.focus( );
				txtGregorian.select( );
				
			}
			
		}
		
		function isDateComplete( theDate )
		{
			if( theDate.value.length != 10 )
				return false;
			else
				return true;
		}

		function Hirji_To_Gregorian( txtHijri, txtGregorian )
		{
			if(txtHijri.value == "" || txtGregorian.value != "")
					return false;
			var JD
			var theDate = getDateArray( txtHijri );
			if( (theDate[0] != -1) && (theDate[1] != -1) && (theDate[2] != -1) )
			{
				var Y = eval( theDate[2] );
				var M = eval( theDate[1] );
				var D = eval( theDate[0] );
				JD = Htoj(D,M,Y); 
				var hijri_date = new Array(Y,M,D)
				var MJD=JD-2400000.5;
				var gregorian_date = JD_to_Gregorian(JD);
				txtGregorian.value = gregorian_date[2]+"/"+gregorian_date[1]+"/"+gregorian_date[0];			
				txtGregorian.title = gregorian_date[2]+" "+MonthName(gregorian_date[1],'Gergorian')+" "+AhBh(gregorian_date[0],"Gregorian")+" ";
			}
		
			
			if( ! isDateComplete( txtHijri ) )			
			{
				txtHijri.focus( );				
				txtHijri.select( );
			}		
		}