/**
 * Hebrew/Jewish Date Functions
 *
 * The functions in this file are used when converting dates to the Hebrew dates
 *
 * Copyright (C) 2005  KosherJava
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

function getHebrewJewishYear(year) {
	return getHebrewJewishYear(year, false);
}

function getHebrewJewishYear(year, displayThousands) {
	var jAlafim = "&#1488;&#1500;&#1508;&#1497;&#1501;"; //word ALAFIM in Hebrew for display on years evenly divisable by 1000
	var jHundreds = ["", "&#1511;", "&#1512;", "&#1513;", "&#1514;", "&#1514;&#1511;", "&#1514;&#1512;","&#1514;&#1513;", "&#1514;&#1514;", "&#1514;&#1514;&#1511;"];
	var jTens = ["", "&#1497;", "&#1499;", "&#1500;", "&#1502;", "&#1504;", "&#1505;", "&#1506;", "&#1508;", "&#1510;"];
	var jTenEnds = ["", "&#1497;", "&#1498;", "&#1500;", "&#1501;", "&#1503;", "&#1505;", "&#1506;", "&#1507;", "&#1509;"];
	var tavTaz = ["&#1496;&quot;&#1493;", "&#1496;&quot;&#1494;"];
	var jOnes = ["", "&#1488;", "&#1489;", "&#1490;", "&#1491;", "&#1492;", "&#1493;", "&#1494;", "&#1495;", "&#1496;"];

	
	
	year = parseInt(year);
	var singleDigitYear = isSingleDigitJeiwshYear(year);
	
	
	
	var thousands = parseInt(year / 1000); //get # thousands

	var sb = "";	
	//append thousands to String
	if(year % 1000 == 0) { // in year is 5000, 4000 etc
		sb += jOnes[thousands];
		sb += "'";
		sb += " ";
		sb += jAlafim; //add # of thousands plus word thousand (overide alafim boolean)
		return sb;          
	} else if(displayThousands) { // if alafim boolean display thousands
		sb += jOnes[thousands];
		sb += "'"; //append thousands quote
		sb += " ";
	}
	year = parseInt(year % 1000); //remove 1000s
	var hundreds = parseInt(year / 100);   // # of hundreds
	var tens="";
	sb += jHundreds[hundreds];  //add hundreds to String
	year = year % 100;                                         //remove 100s
	if(year == 15) {                                            //special case 15
		sb += tavTaz[0];
	} else if(year == 16) {                                     //special case 16
		sb += tavTaz[1];
	} else {
		tens = parseInt(year / 10);
		if(year % 10 == 0) {  // if evenly divisable by 10
			if(singleDigitYear == false) {
				sb += jTenEnds[tens]; // use end letters so that for example 5750 will end with an end nun
			} else {
				sb += jTens[tens]; // use standard letters so that for example 5050 will end with a regular nun
			}
		} else {
			sb += jTens[tens];
			year = year % 10;
			sb += jOnes[year];
		}
	}

	if(singleDigitYear == true) {
		sb += "'"; //append single quote
	} else { // append double quote before last digit
		if (sb.indexOf("&quot;") < 0){
			var temp = sb.substring(0, sb.lastIndexOf("&"));
			temp += "&quot;";
			temp += sb.substring(sb.lastIndexOf("&"), sb.length);
			sb= temp;
		}
	}
	return sb;
}

function isSingleDigitJeiwshYear(year) {
	var shortYear = year %1000; //discard thousands
	//next check for all possible single Hebrew digit years
	if(shortYear < 11 || (shortYear <100 && shortYear % 10 == 0)  || (shortYear <= 400 && shortYear % 100 == 0) ) {
		return true;
	} else {
		return false;
	}
}