/*	-------------------------------------------------------------------	*/
/*	Filename: common.js
	Author: Jonathan S. Knoll
	Revision Date: 2005/12/16
	Time: 14:00 PST
	San Diego Center for Jewish Culture
	Lawrence Family Jewish Community Center, JACOBS FAMILY CAMPUS		*/
/*	-------------------------------------------------------------------	*/

var DC = new DateCalculators();

// -----------------------------------------------
// -----------------------------------------------
// ----- NAVIGATION / IMAGE SWAPPING
// -----
function HighlightNav(area) {
 var navRoot = document.getElementById("navleft");
 if(document.all && navRoot) {
  var navItems, i, node;
  navItems = navRoot.getElementsByTagName("P");
  for(i=0; i<navItems.length; i++) {
   node = navItems[i];
   node.onmouseover=function() { SwapOn(this); }
   node.onmouseout=function() { SwapOff(this); }
  }
 }
 MakeSwappableButtons(area);
} // HighlightNav




function MakeSwappableButtons() {
	var imgArray, i, node, nodeName;
	imgArray = document.images;
	for(i=0; i<imgArray.length; i++) {
		node = imgArray[i];
		nodeName = node.attributes["name"];
		if(nodeName && nodeName.value && node.parentNode.tagName=="A") {
			node.events = [];
			node.onmouseover=function() { SwapOn(this);	}
			node.onmouseout=function() { SwapOff(this); }
		}
	}
} // MakeSwappableButtons

function SwapOn(node) {
	if(node.src) {
		node.src = node.src.replace(".gif", "_H.gif");
		for(var e=0; e<node.events.length; e++)
			node.events[e].className += " over";
	}
	else {
		if(node.img)
			node.img.src = node.img.src.replace(".gif", "_H.gif");
		node.className += " over";
	}
} // SwapOn

function SwapOff(node) {
	if(node.src) {
		node.src = node.src.replace("_H", "");
		for(var e=0; e<node.events.length; e++)
			node.events[e].className = node.events[e].className.replace(" over", "");
	}
	else {
		if(node.img)
			node.img.src = node.img.src.replace("_H", "");
		node.className = node.className.replace(" over", "");
	}
} // SwapOff

function OpenPopUpWindow(url, width, height, scrollbars) {
	if(url) {
		if(scrollbars)
			window.open(url, "PopUpWindow");
		else {
			var custom = ",scrollbars=no";
			if(width)
				custom += (",width="+width);
			if(height)
				custom += (",height="+height);
			window.open(url, "PopUpWindow", "toolbar=no,location=no,directories=no,status=no,menubar=no"+custom+",resizable=no,copyhistory=no,screenX=150,screenY=150,top=0,left=0,margin=0");
		}
	}
	return false;
} // OpenPopUpWindow


// -----------------------------------------------
// -----------------------------------------------
// ----- FORM VALIDATION
// -----
function CheckField(field) {
	var re = new RegExp("[a-z0-9]", "ig");
	if(!re.test(field.value)) {
		alert("Invalid Search String!");
		return false;
	}
	return true;
} // CheckField

function changemonth(theForm) {
	theForm.monthsubmit.value = "x";
	theForm.MV.value = theForm.SearchMonth.value;
	theForm.submit();
} // changemonth



// -----------------------------------------------
// -----------------------------------------------
// ----- COOKIES
// -----
function GetCookie(cookieName) {
	var searchValue, returnValue;
	searchValue = cookieName + "=";
	returnValue = "";
	if(document.cookie.length > 0) {
		var offset = document.cookie.indexOf(searchValue);
		if(offset != -1) {
			offset += searchValue.length;
			var end = document.cookie.indexOf(";", offset);
			if(end == -1)
				end = document.cookie.length;
			returnValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return returnValue;
} // GetCookie


// -----------------------------------------------
// -----------------------------------------------
// ----- DATE CALCULATORS
// -----
function DateCalculators() {

	// Basic Date Setters
	var today = new Date();
	this.currentDate = today.getDate();
	this.currentMonth = today.getMonth();
	this.currentYear = FormatYear(today.getYear());
	this.gregorianMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.gregorianDate = this.gregorianMonths[this.currentMonth] + " " + this.currentDate + ", " + this.currentYear;

	// Hebrew Date Setters
	this.HALAKIM_PER_DAY=25920;
	this.HALAKIM_PER_LUNAR_CYCLE=((29 * this.HALAKIM_PER_DAY) + 13753);
	this.HALAKIM_PER_METONIC_CYCLE=(this.HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7));
	this.metonicCycle=0, this.metonicYear=0, this.moladDay=0, this.moladHalakim=0;
	this.hebrewDay=0, this.hebrewMonth=0, this.hebrewYear=0;
	this.hebrewMonths = new Array("Tishri","Heshvan","Kislev","Tevet","Shevat","Adar I","Adar II","Nisan","Iyyar","Sivan","Tammuz","Av","Elul");
	this.hebrewMonthsPerYear = new Array(12, 12, 13, 12, 12, 13, 12, 13, 12, 12, 13, 12, 12, 13, 12, 12, 13, 12, 13);

	function FormatYear(y) {
		if(y < 1000)
			y += 1900;
		return y;
	} // FormatYear

	this.HebrewDate = function() {
		var hebrewDate;
		this.SdnToHebrew();
		if(this.hebrewDay != 0 && this.hebrewMonth != 0 && this.hebrewYear != 0) {
			hebrewDate = (this.hebrewDay + " " + this.hebrewMonths[this.hebrewMonth-1] + " " + this.hebrewYear);
			if(hebrewDate)
				return(hebrewDate);
			else
				return("");
		}
	} // this.HebrewDate

	this.BothDates = function() {
		document.cookie = "jccdate=" + (this.gregorianDate + " : " + this.HebrewDate());
		return(GetCookie("jccdate"));
	} // this.BothDates

	this.SdnToHebrew = function() {
		this.FindTishriMolad = function(inputDay) {
			this.metonicCycle = Math.floor((inputDay + 310) / 6940);
			this.MoladOfMetonicCycle();
			while(this.moladDay < inputDay - 6940 + 310) {
				this.metonicCycle++;
				this.moladHalakim += this.HALAKIM_PER_METONIC_CYCLE;
				this.moladDay += Math.floor(this.moladHalakim / this.HALAKIM_PER_DAY);
				this.moladHalakim = this.moladHalakim % this.HALAKIM_PER_DAY;
			}
			for(this.metonicYear = 0; this.metonicYear < 18; this.metonicYear++) {
				if(this.moladDay > inputDay - 74)
					break;
				this.moladHalakim += this.HALAKIM_PER_LUNAR_CYCLE * this.hebrewMonthsPerYear[this.metonicYear];
				this.moladDay += Math.floor(this.moladHalakim / this.HALAKIM_PER_DAY);
				this.moladHalakim = this.moladHalakim % this.HALAKIM_PER_DAY;
			}
		} // this.FindTishriMolad

		this.MoladOfMetonicCycle = function() {
			var r1, r2, d1, d2;
			r1 = 31524; // NEW_MOON_OF_CREATION
			r1 += this.metonicCycle * (this.HALAKIM_PER_METONIC_CYCLE&0xFFFF);
			r2 = r1 >> 16;
			r2 += this.metonicCycle * ((this.HALAKIM_PER_METONIC_CYCLE >> 16)&0xFFFF);
			d2 = Math.floor(r2 / this.HALAKIM_PER_DAY);
			r2 -= d2 * this.HALAKIM_PER_DAY;
			r1 = (r2 << 16) | (r1&0xFFFF);
			d1 = Math.floor(r1 / this.HALAKIM_PER_DAY);
			r1 -= d1 * this.HALAKIM_PER_DAY;
			this.moladDay = (d2 << 16) | d1;
			this.moladHalakim = r1;
		} // this.MoladOfMetonicCycle

		function Tishri1(metonicYear, moladDay, moladHalakim) {
			// var SUN=0, MON=1, TUES=2, WED=3, THUR=4, FRI=5, SAT=6;
			var HALAKIM_PER_HOUR = 1080;
			var NOON = (18 * HALAKIM_PER_HOUR), AM3_11_20 = ((9 * HALAKIM_PER_HOUR) + 204), AM9_32_43 = ((15 * HALAKIM_PER_HOUR) + 589);
			var tishri1 = moladDay;
			var dow = tishri1 % 7;
			var leapYear = metonicYear == 2 || metonicYear == 5 || metonicYear == 7 || metonicYear == 10 || metonicYear == 13 || metonicYear == 16 || metonicYear == 18;
			var lastWasLeapYear = metonicYear == 3 || metonicYear == 6 || metonicYear == 8 || metonicYear == 11 || metonicYear == 14 || metonicYear == 17 || metonicYear == 0;
			if((moladHalakim >= NOON) || ((!leapYear) && dow == 2 && moladHalakim >= AM3_11_20) || (lastWasLeapYear && dow == 1 && moladHalakim >= AM9_32_43)) {
				tishri1++;
				dow++;
				if(dow == 7)
					dow = 0;
			}
			if(dow == 4 || dow == 6 || dow == 0)
				tishri1++;
			return tishri1;
		} // Tishri1

		var GREG_SDN_OFFSET=32045, DAYS_PER_5_MONTHS=153, DAYS_PER_4_YEARS=1461, DAYS_PER_400_YEARS=146097;
		var year=this.currentYear+4800, month=this.currentMonth+1;
		var tishri1=0, tishri1After=0, yearLength=0;
		var inputDay;

		if(month > 2)
			month -= 3;
		else {
			month += 9;
			year--;
		}

		inputDay = Math.floor((Math.floor(year / 100) * DAYS_PER_400_YEARS) / 4);
		inputDay += Math.floor(((year % 100) * DAYS_PER_4_YEARS) / 4);
		inputDay += Math.floor((month * DAYS_PER_5_MONTHS + 2) / 5);
		inputDay += this.currentDate - GREG_SDN_OFFSET;
		inputDay -= 347997; // HEB_SDN_OFFSET

		this.FindTishriMolad(inputDay);
		tishri1 = Tishri1(this.metonicYear, this.moladDay, this.moladHalakim);

		if(inputDay >= tishri1) {
			this.hebrewYear = this.metonicCycle * 19 + this.metonicYear + 1;
			if(inputDay < tishri1 + 59) {
				if(inputDay < tishri1 + 30) {
					this.hebrewMonth = 1;
					this.hebrewDay = inputDay - tishri1 + 1;
				}
				else {
					this.hebrewMonth = 2;
					this.hebrewDay = inputDay - tishri1 - 29;
				}
				return;
			}
			this.moladHalakim += this.HALAKIM_PER_LUNAR_CYCLE * this.hebrewMonthsPerYear[this.metonicYear];
			this.moladDay += Math.floor(this.moladHalakim / this.HALAKIM_PER_DAY);
			this.moladHalakim = this.moladHalakim % this.HALAKIM_PER_DAY;
			tishri1After = Tishri1((this.metonicYear + 1) % 19, this.moladDay, this.moladHalakim);
		}
		else {
			this.hebrewYear = this.metonicCycle * 19 + this.metonicYear;
			if(inputDay >= tishri1 - 177) {
				if(inputDay > tishri1 - 30) {
					this.hebrewMonth = 13;
					this.hebrewDay = inputDay - tishri1 + 30;
				}
				else if(inputDay > tishri1 - 60) {
					this.hebrewMonth = 12;
					this.hebrewDay = inputDay - tishri1 + 60;
				}
				else if(inputDay > tishri1 - 89) {
					this.hebrewMonth = 11;
					this.hebrewDay = inputDay - tishri1 + 89;
				}
				else if(inputDay > tishri1 - 119) {
					this.hebrewMonth = 10;
					this.hebrewDay = inputDay - tishri1 + 119;
				}
				else if(inputDay > tishri1 - 148) {
					this.hebrewMonth = 9;
					this.hebrewDay = inputDay - tishri1 + 148;
				}
				else {
					this.hebrewMonth = 8;
					this.hebrewDay = inputDay - tishri1 + 178;
				}
				return;
			}
			else {
				if(this.hebrewMonthsPerYear[(this.hebrewYear - 1) % 19] == 13) {
					this.hebrewMonth = 7;
					this.hebrewDay = inputDay - tishri1 + 207;
					if(this.hebrewDay > 0)
						return;
					this.hebrewMonth--;
					this.hebrewDay += 30;
					if(this.hebrewDay > 0)
						return;
					this.hebrewMonth--;
					this.hebrewDay += 30;
				}
				else {
					this.hebrewMonth = 6;
					this.hebrewDay = inputDay - tishri1 + 207;
					if(this.hebrewDay > 0)
						return;
					this.hebrewMonth--;
					this.hebrewDay += 30;
				}
				if(this.hebrewDay > 0)
					return;
				this.hebrewMonth--;
				this.hebrewDay += 29;
				if(this.hebrewDay > 0)
					return;
				tishri1After = tishri1;
				this.FindTishriMolad(this.moladDay - 365);
				tishri1 = Tishri1(this.metonicYear, this.moladDay, this.moladHalakim);
			}
		}
		yearLength = tishri1After - tishri1;
		this.moladDay = inputDay - tishri1 - 29;
		if(yearLength == 355 || yearLength == 385) {
			if(this.moladDay <= 30) {
				this.hebrewMonth = 2;
				this.hebrewDay = this.moladDay;
				return;
			}
			this.moladDay -= 30;
		}
		else {
			if(this.moladDay <= 29) {
				this.hebrewMonth = 2;
				this.hebrewDay = this.moladDay;
				return;
			}
			this.moladDay -= 29;
		}
		this.hebrewMonth = 3;
		this.hebrewDay = this.moladDay;
	} // this.SdnToHebrew
	return this;
} // DateCalculators
