﻿/// <reference name="~/static/scripts/Main.js"/>
///<reference path="~/static/scripts/Jquery.IntelliSense.js" />

function HDate (year,month,day)
{	this.year = year*1; this.month=month*1;this.day=day*1;
	this.toString = function() {return this.day+"/"+this.month+"/"+this.year}
	this.getFullYear =function() {return year*1}; this.getMonth =function() {return month-1}; this.getDate =function() {return day*1};  }

function IDate (day,date,hdate,hour,minute,holiday) {this.day=day*1;this.date=date; this.hour=hour*1;this.minute=minute*1; this.hdate=hdate; if(!holiday) holiday=",0"; this.holiday=holiday.split(",");if (day==6)this.holiday[1]=1; this.dates = [hdate,new HDate(date.getFullYear(),date.getMonth()+1,date.getDate())];}
function nIDate(a,b,c,d,e,f,g,h,i,j) {var t= new IDate (a,new Date(b,c,d),new HDate(e,f,g),h,i,j); if(!window.dDate){IDate.prototype.firstInit(t)};return t};
IDate.prototype = {yesterday:{},
monthYear: function(cal){return Dates.GetMonthName(this,cal,false) + " " + Dates.GetYear(this,cal,false); },
greDate: function() {return Dates.DateToString(this,1,1)},
numGreDate: function(notyear) {return  this.dates[1].day+"/"+this.dates[1].month+(!notyear?"/"+(this.date.getYear()+"").substring(1):"");},
shortGreDate: function() {return this.dates[1].day+" "+ Dates.GMonths2[this.dates[1].month-1]},
short2GreDate: function() {return this.dates[1].day+" ב"+ Dates.GMonths[this.dates[1].month-1]},
shortHebDate: function (){return Dates.HDateToMinString(this)},
hebDate: function (){return Dates.HDateToString(this)},
time: function() {return View.AddZero(this.hour*1)+":"+View.AddZero(this.minute);},
miniDate: function(notime,noyear) {return this.miniDateByCal(0,notime,noyear);},
miniDateByCal: function(cal,notime,noyear) {var sDay="";
	if (Math.abs(this.date.getTime()-new Date().getTime())<86400000*120||this.dates[cal].year==dDate.dates[cal].year ||noyear) sDay = (cal?this.numGreDate(1): this.shortHebDate()); else sDay=( cal? this.numGreDate(): this.hebDate());
if (this.intDate()==dDate.intDate()) sDay ="היום"; if (this.intDate()==this.yesterday.intDate()) sDay= "אתמול"; if(this.intDate()==this.tomorrow.intDate()) sDay = "מחר";
return sDay +(!notime? " "+this.time():""); },
intDate: function() {return (this.dates[1].year+""+View.AddZero(this.dates[1].month)+""+View.AddZero(this.dates[1].day))*1}}
Date.prototype.intDate=  function() {return (this.getFullYear()+""+View.AddZero(this.getMonth()+1)+""+View.AddZero(this.getDate()))*1}
IDate.prototype.firstInit = function(dc) {
var d = new Date(); d.setDate(dc.date.getDate()-1);
IDate.prototype.yesterday =new Date(d.getFullYear(), d.getMonth(),d.getDate());
d = new Date(); d.setDate(dc.date.getDate()+1)
IDate.prototype.tomorrow = new Date(d.getFullYear(), d.getMonth(),d.getDate());
}

Dates = {
CalNames : ["עברי","לועזי"],DayChars:["א","ב","ג","ד","ה","ו","ש"], DayNames : ["ראשון","שני","שלישי","רביעי","חמישי","שישי","שבת"],HMonths : ["תשרי","חשוון","כסלו","טבת","שבט","אדר","ניסן","אייר","סיוון","תמוז","אב","אלול"],HMonths2 : ["תשרי","חשוון","כסלו","טבת","שבט","אדר א","אדר ב","ניסן","אייר","סיוון","תמוז","אב","אלול"],GMonths : ["ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר","-"],GMonths2 : ["'ינו","פבו'","מרץ'","אפר'","מאי'","יוני","יולי","אוג'","ספט'","אוק'","נוב'","דצמ'","-"],
DateToString: function(date, useCal, cal) { if (!useCal) cal = CurrentCal; if (!cal) return Dates.HDateToString(date); else return date.dates[1].day + " " + Dates.GMonths2[date.dates[1].month-1] + " " + (date.dates[1].getFullYear() + "").substring(2); },
DateToStringNums: function(date, useCal, cal) { if (!useCal) cal = CurrentCal; if (!cal) return Dates.HDateToString(date); else return date.dates[1].day + "/" + date.dates[1].month + "/" + date.dates[1].getFullYear()},
DateToMinString: function(date, useCal, cal) { if (!useCal) cal = CurrentCal;cal*=1; if (!cal) return Dates.HDateToMinString(date); else return date.dates[1].day + " " + Dates.GMonths2[date.dates[1].month-1]},
GetMonthName: function(date,cal,greShort) {
	date = date.dates;
	if (!cal) {
		months = Dates.isHLeapYear(date[0].year)? Dates.HMonths2 : Dates.HMonths;
		return  months[date[0].month-1] }
	else if (greShort) return Dates.GMonths2[date[1].getMonth()]; else return Dates.GMonths[date[1].getMonth()];
},
HDateToString: function (date){
	return Dates.Gimatria(date.hdate.day,true) + " ב" + this.GetMonthName(date,0,0)+" "+ Dates.Gimatria(date.hdate.year-5000,true);
},
GetYear : function (date,cal,greShort) {if (!cal) return Dates.Gimatria(date.dates[0].year-5000,1); else {if (greShort) return date.dates[1].getFullYear().toString().substring(2); else return date.dates[1].getFullYear();}},
GetDay : function(date,cal,hebGeresh){if(arguments.length==2)hebGeresh=1; if (!cal) return Dates.Gimatria(date.dates[0].day,hebGeresh); else return date.dates[1].getDate();},
HDateToMinString :function (date){	return Dates.GetDay(date,0) + " " + Dates.GetMonthName(date,0);},
Gimatria : function (n,useQ) {
	var p="";
	while(n >= 400) {p = p + "ת";n = n - 400;}

	if (n >= 100) {
		p = p + "קרשת".substring(n / 100-1, n/100);
		n = n % 100;}
	
	if (n >= 10) {
		if (n == 15 || n == 16) n = n - 9;
		if (n >= 10) p = p + "יכלמנסעפצ".substring(n / 10 -1,n/10);
		else p = p + "ט";
		n = n % 10;}
	
	if (n > 0) p = p + "אבגדהוזחט".substring(n-1, n);
	if (useQ)  p = p.length>1? p.substring(0,p.length-1)+"\""+p.substring(p.length-1) : p=p+"'";
	return p;
},DeGimatria : function (s)
{	s = s.replace(/\"/g,"").replace(/\'/g,"");//"
	var n=0;
	for (i=0;i<s.length;i++) {
		c = s.charCodeAt(i);
		if (c > "צ".charCodeAt(0))  n += (c - "צ".charCodeAt(0))*100;
		if (c > "ט".charCodeAt(0) && c < "ק".charCodeAt(0))  n += ("יכלמנסעפצ".indexOf(s.substring(i,i+1))+1)*10;
		if (c < "י".charCodeAt(0)) n += (c - "א".charCodeAt(0)+1);		
		if (c < "א".charCodeAt(0) || c > "ת".charCodeAt(0)) return 0;
	}
	
	return n;
},
isHLeapYear: function (year) {y = year%19;return (y==0||y==3||y==6||y==8||y==11||y==14||y==17);},
Get: function (start,end,cal,func){ExecuteJS("/My/Calendar/DateData.aspx?act=range&saveWeek=1&start="+start+"&end="+end+"&cal="+cal,func);},
GetMonth: function (mdate,cal,func) {
	var date1 = "1/"+mdate.month+"/"+ mdate.year;
	var date2 = "32/"+mdate.month+"/"+mdate.year;
	this.Get(date1,date2,cal,func);
},ValidateDate: function(str,cal)
{	var a;
	if (cal==1)
	{
		a = str.split("/")
		if (a[2] <100) if (a[2]>60) a[2]=a[2]*1+1900; else a[2]=a[2]*1+2000;
		if (a[0]>31) alert("עליך לכתוב יום תקני");
		if (a[1]>12) alert("עליך לכתוב חודש תקני");
		dteDate=new Date(a[2],a[1]-1,a[0]);
		if (!!dteDate && dteDate != "NaN") return true;
		else {alert("על התאריך להיות בפורמט 31/12/2007"); return false;}
	}
	else {
		try {
		a = str.replace("אדר א","אדר_א").replace("אדר ב","אדר_ב").split(" ");
		a[0] = Dates.DeGimatria(a[0]);
		a[2] = Dates.DeGimatria(a[2]);
		if (a[2]<1000) a[2]+=5000;
		
		if (a[1].substring(0,1)=='ב')a[1] = a[1].substring(1)
		if (!Dates.isHLeapYear(a[2])) a[1]= a[1].replace("_א","").replace("_ב","")
		else if(a[1]=="אדר") a[1]= "אדר_ב";
		
		if (Dates.isHLeapYear(a[2])) months = Dates.HMonths2;
			else  months = Dates.HMonths;
		var month = -1;
		for (i=0;i<months.length;i++) if (months[i]==a[1].replace("_"," ")) month=i; 
		
		if (month==-1) {alert("חודש לא נמצא "+a[1]); return false;}
		if (!(a[0]>0 && a[0] < 31 && a[2] >1000 && a[2] < 7000)) {alert("יום - שנה לא חוקיים"+ a[0] +" " + a[2]);  return false;}
		return  true;
		} catch (e) {alert("על התאריך להיות בפורמט א תשרי תשסט"); return false;}
	} 
},ValidateDateTime: function(str,cal)
{	var a,time,datevalid; str = $.trim(str);
	if (cal==1)
	{
		a = str.split(" ");
		if (a.length>1) time=a[1];
		datevalid= this.ValidateDate(a[0],cal);
	}
	else {
		a = str.replace("אדר א","אדר_א").replace("אדר ב","אדר_ב").split(" ");
		if (a.length==4) {time=a[4]; a.length==3;}
		
		datevalid= this.ValidateDate(a.join(" "),cal);
	}
	time=time.split(":");
	if (time[0]*1>23) {alert("שעה לא חוקית"); return false;}
	if (time[1]*1>59) {alert("דקה לא חוקית"); return false;}
	return datevalid;
},
CreateMinTable: function(movecommand,monthid,tblid,tbdid) {
return '<div class=Left><div class=Year onclick='+movecommand+'(Dates.Compare.NextYear)>&#9668;&#9668;</div><div class=Month onclick='+movecommand+'(Dates.Compare.NextMonth)>&#9668;</div></div><div class=Right><div class=Year onclick='+movecommand+'(Dates.Compare.BackYear)>&#9658;&#9658;</div><div class=Month onclick='+movecommand+'(Dates.Compare.BackMonth)>&#9658;</div></div><div id='+monthid+'></div><table id='+tblid+'><thead><tr><th>א</th><th>ב</th><th>ג</th><th>ד</th><th>ה</th><th>ו</th><th>ש</th></tr></thead><tbody id='+tbdid+'></tbody></table>';
},
FillMinTable: function (tbl,aDates,renderFunc,ClickFunc,cal)
{
	$(tbl).empty();
	var CurrentDate = dDate;
	for (var i=0;i<aDates.length;i++){
		var today = dDate.dates[cal], html, currCell, currRow, rowIndex;
		var currday = aDates[i].dates[cal];
		currCell = document.createElement("td");
		
		if (!renderFunc)  html = Dates.GetDay(aDates[i],cal,0)
		else html = renderFunc(aDates[i],currCell);

		rowIndex = tbl.childNodes.length;
		if(rowIndex == 0) {
			currRow = document.createElement("tr");
			tbl.appendChild(currRow); }
		else
		{		if(tbl.childNodes[rowIndex-1].childNodes.length <7)
				currRow = tbl.childNodes[rowIndex-1];
			else
			{	currRow = document.createElement("tr");
				tbl.appendChild(currRow);
			}
		}
		var cc = $(currCell);
		cc.addClass("Day"+aDates[i].holiday[1]+ (aDates[i].dates[cal].month!= aDates[7].dates[cal].month?' Gray':'')).addClass("tdDay"+aDates[i].intDate());
		if (currday.day == today.day && currday.month == today.month && currday.year == today.year) {cc.addClass('Today')}
		currCell.innerHTML = html;
		var f = function(d) {return function () {ClickFunc(d);}}
		currCell.onclick = f(aDates[i])
		currRow.appendChild(currCell);
	}

},Compare: {
NextYear: function (date,cal) {
	var crdate = date.dates[cal]
	var year = crdate.getFullYear()+1;
	var month= crdate.getMonth()+1;

	if (!cal) {
		if(Dates.isHLeapYear(year-1)&&month>6) month--;
		if(Dates.isHLeapYear(year)&&month>5) month++;
	}
	return new HDate(year,month)
},
BackYear : function (date,cal)
{	var crdate = date.dates[cal], year = crdate.getFullYear()-1,month= crdate.getMonth()+1;

	if(!cal) {
		if(Dates.isHLeapYear(year+1)&&month>6) month--;
		if(Dates.isHLeapYear(year)&&month>5) month++;
	}
	return new HDate(year,month)
},
NextMonth: function (date,cal)
{
	var crdate = date.dates[cal], year = crdate.getFullYear(), month= crdate.getMonth()+1;
	
	if(cal) {
		if(month<12) month++;
		else {month=1; year++;}
	}
	else {
		if(Dates.isHLeapYear(year)){
			if(month<13) month++;
			else {month = 1;year++;}
		} else {
			if(month !=12) month++;
			else  {month = 1; year++;} }
	}
	return new HDate(year,month);
},
BackMonth : function (date,cal) {
  var crdate = date.dates[cal],year = crdate.getFullYear(),month= crdate.getMonth()+1;
	if(cal==1) {
		if(month>1) month--;
		else {month=12; year--;}}
	else{
		if(month>1) month--;
		else {year--; month=12+(Dates.isHLeapYear(year)*1) }
	}
	 return new HDate(year,month);
}}
}

