dojo.provide("dojox.date.hebrew.numerals");
dojo.experimental("dojox.date.hebrew.numerals");

//Conversion from "Hindi" numerals to Hebrew numerals and vice versa

(function(){

	var DIG=["א","ב","ג","ד","ה","ו","ז","ח","ט"];
	var	TEN=["י","כ","ל","מ","נ","ס","ע","פ","צ"];
	var	HUN=["ק","ר","ש","ת"];
	var MONTHS =["א'","ב'","ג'","ד'","ה'","ו'","ז'","ח'","ט'","י'","י\"א","י\"ב","י\"ג"];		

	var transformChars = function(str, nogrsh){
		str = str.replace("יה", "טו").replace("יו", "טז");

		if(!nogrsh){
			var len = str.length;
			if(len > 1){
				str = str.substr(0, len - 1) + '"' + str.charAt(len - 1);
			}else{
				str += "\u05F3"; // 05F3:geresh
			}
		}
		return str; // String
	};
	 
	var parseStrToNumber = function(str){
		var num = 0;
		dojo.forEach(str, function(ch){
			var i;
			if((i = HUN.indexOf(ch)) != -1){
				num += 100 * ++i;
			}else if ((i = TEN.indexOf(ch)) != -1){
				num += 10 * ++i;
			}else if ((i = DIG.indexOf(ch)) != -1){
				num += ++i;
			}
		});
		return num; //Number
	};
	 
	var convertNumberToStr = function(num){
		var str  = "", n = 4, j = 9;
  		while(num){ 
			if(num >= n*100){
				str += HUN[n-1];
				num -= n*100;
				continue;
			}else if(n > 1){
				n--;
				continue;
			}else if(num >= j*10){
				str += TEN[j-1];
				num -= j*10;
			}else if (j > 1){
				j--;
				continue;
			}else if(num > 0){
				str += DIG[num-1];
				num = 0;
			}		
		}
		return str; //String	
	};

	dojox.date.hebrew.numerals.getYearHebrewLetters = function(/*Number */ year){
		// summary: This function return year written in Hebrew numbers-letters, 
		//
		// example:
		// |		var date1 = new dojox.date.hebrew.Date();
		// |
		// |		document.writeln(dojox.date.hebrew.numerals.getYearHebrewLetters(date1.getFullYear());	
		
		var y = year % 1000;
		if(!y){ throw new Error("Hebrew year "+year+" is not in range 5001-5999");} // FIXME: wrong test
		return transformChars(convertNumberToStr(y)); // String
	};
	
	dojox.date.hebrew.numerals.parseYearHebrewLetters  = function(/*String hebrew year*/ year){
		// summary: This function return year in format number from  the year written in Hebrew numbers-letters
		//                   
		// example:
		// |		var date = new dojox.date.hebrew.Date();
		// |        	date.setFullYear(dojox.date.hebrew.numerals.parseYearHebrewLetters('תשס"ח'));

		return parseStrToNumber(year) + 5000; // int
	};
	
	dojox.date.hebrew.numerals.getDayHebrewLetters =  function(day, /*boolean?*/ nogrsh){
		// summary: This function return date written in Hebrew numbers-letter,  can be in format א or א' (with geresh)
		//
		// example:
		// |		var date1 = new dojox.date.hebrew.Date();
		// |
		// |		document.writeln(dojox.date.hebrew.numerals.getDayHebrewLetters(date1.getDay());

		return transformChars(convertNumberToStr(day), nogrsh); // String
	};
	
	dojox.date.hebrew.numerals.parseDayHebrewLetters =  function(/*String hebrew*/ day){
		// summary: This function return date in format number from  the date written in Hebrew numbers-letter
		//
		// example:
		// |		var date1 = new dojox.date.hebrew.Date();
		// |
		// |		date1.setDate(dojox.date.hebrew.numerals.parseDayHebrewLetters('א'));
		return parseStrToNumber(day); // int
	};

	dojox.date.hebrew.numerals.getMonthHebrewLetters =  function(/*int*/monthNum){
		// summary: This function return month written in Hebrew numerals
		//
		// example:
		// |		var date1 = new dojox.date.hebrew.Date();
		// |
		// |		document.writeln(dojox.date.hebrew.numerals.getMonthHebrewLetters(date1.getMonth());
		return MONTHS[monthNum]; // String
	};	

	dojox.date.hebrew.numerals.parseMonthHebrewLetters = function(/*String*/monthStr){
		// summary: This function return month in format number from  the month written in Hebrew  word  or numbers-letters
		//                   the return number is index in month name array, to use it for setMont, do correction for leap year
		// example:
		// |		var date = new dojox.date.hebrew.Date();
		// |            var number = dojox.date.hebrew.numerals.parseMonthHebrewLetters("תמוז");
		// |		date.setMonth(number);
			
		//month number from 0 to 12
		var monnum = dojox.date.hebrew.numerals.parseDayHebrewLetters(monthStr) - 1;

		if(monnum == -1 || monnum > 12){
			throw new Error("The month name is incorrect , month = " + monnum); 
		}
		return monnum;
	};
})();

@@ -797,19 +797,20 @@
 if((i = 
-HUN
+dojo
 .indexOf
@@ -802,32 +802,37 @@
  = dojo.indexOf(
+DIG, 
 ch)) != -1)%7B%0A%09%09%09
@@ -842,14 +842,8 @@
 m +=
- 100 *
  ++i
@@ -862,19 +862,20 @@
 f ((i = 
-TEN
+dojo
 .indexOf
@@ -867,32 +867,37 @@
  = dojo.indexOf(
+TEN, 
 ch)) != -1)%7B%0A%09%09%09
@@ -932,19 +932,20 @@
 f ((i = 
-DIG
+dojo
 .indexOf
@@ -945,16 +945,21 @@
 indexOf(
+HUN, 
 ch)) != 
@@ -965,32 +965,38 @@
  -1)%7B%0A%09%09%09%09num +=
+ 100 *
  ++i;%0A%09%09%09%7D%0A%09%09%7D);

use dojo.indexOf instead of Array.indexOf