﻿/* jalali.js  Gregorian to Jalali and inverse date convertor
 * Copyright (C) 2001  Roozbeh Pournader <roozbeh@sharif.edu>
 * Copyright (C) 2001  Mohammad Toossi <mohammad@bamdad.org>
 * Copyright (C) 2003  Behdad Esfahbod <js@behdad.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can receive a copy of GNU Lesser General Public License at the
 * World Wide Web address <http://www.gnu.org/licenses/lgpl.html>.
 *
 * For licensing issues, contact The FarsiWeb Project Group,
 * Computing Center, Sharif University of Technology,
 * PO Box 11365-8515, Tehran, Iran, or contact us the
 * email address <FWPG@sharif.edu>.
 */

/* Changes:
 * 
 * 2003-Mar-29:
 *      Ported to javascript by Behdad Esfahbod
 *
 * 2001-Sep-21:
 *	Fixed a bug with "30 Esfand" dates, reported by Mahmoud Ghandi
 *
 * 2001-Sep-20:
 *	First LGPL release, with both sides of conversions
 */
 
g_days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
j_days_in_month = new Array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
 
function div(a,b) {
  return Math.floor(a/b);
}

function gregorian_to_jalali(g /* array containing year, month, day*/ )
{
   var gy, gm, gd;
   var jy, jm, jd;
   var g_day_no, j_day_no;
   var j_np;
 
   var i;

   gy = g[0]-1600;
   gm = g[1]-1;
   gd = g[2]-1;

   g_day_no = 365*gy+div((gy+3),4)-div((gy+99),100)+div((gy+399),400);
   for (i=0;i<gm;++i)
      g_day_no += g_days_in_month[i];
   if (gm>1 && ((gy%4==0 && gy%100!=0) || (gy%400==0)))
      /* leap and after Feb */
      ++g_day_no;
   g_day_no += gd;
 
   j_day_no = g_day_no-79;
 
   j_np = div(j_day_no, 12053);
   j_day_no %= 12053;
 
   jy = 979+33*j_np+4*div(j_day_no,1461);
   j_day_no %= 1461;
 
   if (j_day_no >= 366) {
      jy += div((j_day_no-1),365);
      j_day_no = (j_day_no-1)%365;
   }
 
   for (i = 0; i < 11 && j_day_no >= j_days_in_month[i]; ++i) {
      j_day_no -= j_days_in_month[i];
   }
   jm = i+1;
   jd = j_day_no+1;

   return new Array(jy, jm, jd);
}

function jalali_to_gregorian(j /* array containing year, month, day*/ )
{
   var gy, gm, gd;
   var jy, jm, jd;
   var g_day_no, j_day_no;
   var leap;

   var i;

   jy = j[0]-979;
   jm = j[1]-1;
   jd = j[2]-1;

   j_day_no = 365*jy + div(jy,33)*8 + div((jy%33+3),4);
   for (i=0; i < jm; ++i)
      j_day_no += j_days_in_month[i];

   j_day_no += jd;

   g_day_no = j_day_no+79;

   gy = 1600 + 400*div(g_day_no,146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
   g_day_no = g_day_no % 146097;

   leap = 1;
   if (g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
   {
      g_day_no--;
      gy += 100*div(g_day_no,36524); /* 36524 = 365*100 + 100/4 - 100/100 */
      g_day_no = g_day_no % 36524;
      
      if (g_day_no >= 365)
         g_day_no++;
      else
         leap = 0;
   }

   gy += 4*div(g_day_no,1461); /* 1461 = 365*4 + 4/4 */
   g_day_no %= 1461;

   if (g_day_no >= 366) {
      leap = 0;

      g_day_no--;
      gy += div(g_day_no, 365);
      g_day_no = g_day_no % 365;
   }

   for (i = 0; g_day_no >= g_days_in_month[i] + (i == 1 && leap); i++)
      g_day_no -= g_days_in_month[i] + (i == 1 && leap);
   gm = i+1;
   gd = g_day_no+1;

   return new Array(gy, gm, gd);
}

function makeArray() {
  for (i = 0; i<makeArray.arguments.length; i++)
    this[i + 1] = makeArray.arguments[i];
}

function EasternNumeric(x)
{
	var str = "";
	var strArray = x.split("");
	for( i=0; i<strArray.length; i++ )
	{
		strArray[i] = strArray[i].replace("1","۱");
		strArray[i] = strArray[i].replace("2","۲");
		strArray[i] = strArray[i].replace("3","۳");
		strArray[i] = strArray[i].replace("4","۴");
		strArray[i] = strArray[i].replace("5","۵");
		strArray[i] = strArray[i].replace("6","۶");
		strArray[i] = strArray[i].replace("7","۷");
		strArray[i] = strArray[i].replace("8","۸");
		strArray[i] = strArray[i].replace("9","۹");
		strArray[i] = strArray[i].replace("0","۰");
		str = str + strArray[i];
	}
	return str;
}

function PersianTime(x)
{
	str = str.replace("Days","روز");
	str = str.replace("Hours","ساعت");
	str = str.replace("Mins","دقیقه");
	str = str.replace("Secs","ثانيه");
	return str;
}

Today = new Date();
dt = gregorian_to_jalali(new Array(Today.getFullYear(),Today.getMonth()+1,Today.getDate()));
var PersianMonth = new makeArray('فروردين','ارديبهشت','خرداد','تير','مرداد','شهريور','مهر','آبان','آذر','دی','بهمن','اسفند');
var monthArray = new Array("January","Febuary","March","April","May","June","July","August","September","October","November","December");



//http://www.bloke.com/
// permission to use and modify as long as you leave
//these 4 comment
// lines in tact and unmodified.
// http://www.bloke.com/javascript/Countup/
speed=1000;
len=40;
tid = 0;
num=0;
clockA = new Array();
timeA = new Array();
formatA = new Array();
dd = new Date();
var d,x;

function doDate(x)
{
  for (i=0;i<num;i++) {
    dt = new Date();
  
    if (timeA[i] != 0) {
      v1 = Math.round(( dt - timeA[i] )/1000) ;
      if (formatA[i] ==4 ) {
        sec = v1%60;
	v1 = Math.floor( v1/60);
	min = v1 %60 ;
	v1 = Math.floor(v1 / 60);
	hour = v1 %24 ;
	day = Math.floor(v1 / 24);
        clockA[i].date.value = day+(day==1?"day ":" Days   ")+hour+(hour==1?"hour ":" Hours   ")+min+(min==1?"min ":" Mins   ")+sec+(sec==1?"sec":" Secs ")
	if( (location.href).indexOf("/farsi") >= 0 )
	   {
		//clockA[i].date.value = PersianTime(clockA[i].date.value);
	   }
        }
      else
        clockA[i].date.value = "Invalid Format spec";
      }
    else
      clockA[i].date.value = "Countup from when?";
    }

  tid=window.setTimeout("doDate()",speed);
}

function start(d,x,format) {
  clockA[num] = x
  if (d == "now")
    timeA[num] = new Date();
  else
    timeA[num] = new Date(d);
  formatA[num] = format;
//window.alert(timeA[num]+":"+d);
  if (num == 0)  
    tid=window.setTimeout("doDate()",speed);
  num++;
}

function CountupLong(t,format,len)
{
  document.write('<FORM name=form'+num+'><input name=date dir=ltr size=')
  document.write(len)
  document.write(' value="Countup: Requires Javascript"></FORM><div style=\"display:none\">')
  start(t,document.forms["form"+num],format);
}


function Countup(t)
{
  CountupLong(t,2,20);
}


var myENDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
var myFADays = ["یکشنبه","دوشنبه","سه شنبه","چهارشنبه","پنج شنبه","جمعه","شنبه","یکشنبه"]



if( (location.href).indexOf("/farsi") >= 0 )
{

// Organization Header in Persian

document.write( "<link href=\"application.css\" media=\"screen\" rel=\"Stylesheet\" title=\"main\" type=\"text/css\" />" );
document.write( "<div id=\"site_wrapper\" style=\"background-color:#eeeeee\">" );
document.write( "<div id=\"banner\">" );
document.write( "<table width=100%><tr><td width=100%>" );
document.write( "	<div id=\"button_container\">" );
document.write( "		<table width=100%><tr><td>" );
document.write( "<table align=\"left\" width=\"150\"><tr><td><div dir=\"rtl\" style=\"font-size:15px;font-weight:bold;filter:glow(color=#FFFFFF,strength=3);\">زمان دربند بودن دانشجويان دلير ايران</div>" );
document.write( "<table align=\"right\" dir=\"rtl\">" );
document.write( CountupLong('July 08, 1999 00:00:00',4,37) + "</div>" );
document.write( "<b>" + myFADays[today.getDay()] + "، " + EasternNumeric(dt[2].toString())+" "+PersianMonth[dt[1]]+" "+ EasternNumeric(dt[0].toString()) + "</b>" );
document.write( "<br></table></td></tr></table>" );
document.write( "		<div dir=\"rtl\" id=\"header_text\" style=\"font-size:15px;font-weight:bold;filter:glow(color=#FFFFFF,strength=3);\">" );
document.write( "ما از مبارزات دانشجويان داخل کشور برای احقاق دموکراسی و ايجاد حکومت غير مذهبی و مردم سالار پشتيبانی مینماییم. و حملات سردمداران رژيم جمهوری اسلامی را برای سرکوب ندای آزادی خواهانه محکوم ميکنيم." );
document.write( "		</td></tr></table>" );
document.write( "	</div>" );
document.write( "		</div>" );
document.write( "</td><td>" );
document.write( "		<a href=\"/\" title=\"Home Page\"><img src=\"/images/nav/logo-per.gif\" alt=\"همبستگی جهانی دانشحویان ایرانی\" width=\"165\" height=\"109\" /></a>" );
document.write( "</td></tr></table></div>" );


}
else if( (location.href).indexOf("/english") >= 0 )
{

// This imports the Organization Header

document.write( "<link href=\"application.css\" media=\"screen\" rel=\"Stylesheet\" title=\"main\" type=\"text/css\" />" );
document.write( "<div id=\"site_wrapper\" style=\"background-color:#eeeeee\">" );
document.write( "<div id=\"banner\">" );
document.write( "<table><tr><td>" );
document.write( "		<a href=\"/\" title=\"Home Page\"><img src=\"/images/nav/logo-eng.gif\" alt=\"International Alliance of Iranian Students\" width=\"165\" height=\"109\" /></a>" );
document.write( "<br><b><font size=2>" + myENDays[today.getDay()] + "<br>" + Today.getDate() + " " + monthArray[Today.getMonth()] + " " + Today.getFullYear() + "</b>");
document.write( "</td><td>" );
document.write( "	<div id=\"button_container\">" );
document.write( "		<div id=\"search_box\">" );
document.write( "<div style=\"font-size:12px;font-weight:bold;filter:glow(color=#FFFFFF,strength=3);\">Time since our beloved students have been captive</div>" );
document.write( "<center><table>" );
document.write( CountupLong('July 08, 1999 00:00:00',4,37) + "</div>" );
document.write( "</table></center>" );
document.write( "		</div>" );
document.write( "		<div id=\"header_text\" style=\"font-size:12px;font-weight:bold;filter:glow(color=#FFFFFF,strength=3);\">" );
document.write( "The International Alliance of Iranian Students is an organization composed of intellectuals, students and political activists all with the common goal of establishing a Secular and Democratic government based on popular vote." );
document.write( "We stand against the reactionary ruling Islamic regime in their use of violence and brutality to silence the voice of freedom and democracy." );
document.write( "		</div>" );
document.write( "	</div>" );
document.write( "</td></tr></table></div>" );

}