/* 	****************************************************
	**** 	International Fixed Calendar Scripting	****
	****************************************************
	**** 	Created By: Txec Ereufighleu            ****
	****	Email: Txec@TalossanProgress.Org        ****
	****	via the Union for Talossan Progress     ****
	****	Date: [IFC] April 21, 2006              ****
	****	Date: [Gr] April 27, 2006               ****
	****	Copy Right: All Rights Reserved         ****
	****	Direct all correspondence about this    ****
	****        script to the above email address   ****
	**************************************************** */

	function TypeOf(CheckObject)
	// Returns the type of the CheckObject as a string
	{
		switch (typeof(CheckObject))
		{
			case "Undefined" : return "Undefined";
			case "Number" 	 : return "Number";
			case "String"  	 : return "String";
			case "Object" : 
					switch (CheckObject.constructor)
					{
						case Array 	: return "Array";
						case Date 	: return "Date";
						default		: return "Other Object";
					} // end switch switch (CheckObject.constructor)
			default : return ("Other Primitive");
		} 	// end switch (typeof(CheckObject)
	}	// end TypeOf	

 	function Watch (Comment, Stuff)
	{ // General varialbe Watch function
		document.writeln("<strong>" + Comment + "</strong>" + Stuff + "<br />");
	} 	// end Watch

	function IntToMonth (Months, IFCDate)
	{
		switch (Months)
		{
			case 1:  return  "January";
			case 2:  return  "February";
			case 3:  return  "March";
			case 4:  return  "April";
			case 5:	 return  "May";
			case 6:  return  "June";
			case 7:  if (IFCDate) return  "Sol"
						else return  "July";
			case 8:	 if (IFCDate) return  "July"
						else return  "August";
			case 9:	 if (IFCDate) return  "August"
						else return  "September";
			case 10: if (IFCDate) return  "September"
						else return  "October";
			case 11: if (IFCDate) return  "October"
						else return  "November";
			case 12: if (IFCDate) return  "November"
						else return  "December";
			case 13: if (IFCDate) return  "December"
						else return  "July";
			default: return null;
		} // end switch (weeks)
	} // end getMonthOfYear

/* 	*************************************
	* 				All Pages 			*
	*************************************	*/
	
	function PrintTodaysIFCDate()
	{
		var TestDate = new Date(2000, 8, 9 )
		var Today = new Date();
		var IFCDate = new clsIFCDate(Today);			
		var insNode = document.getElementById("IFC-Date");
		
		insNode.innerHTML = '[<abbr title="International Fixed Calender">IFC</abbr>] ' +  IFCDate.getDate();
		
		insNode = document.getElementById("Greg-Date");
		insNode.innerHTML = Today.toLocaleString().replace(/.{11}$/,"");
	} // end PrintTodaysDate


/* 	*********************************
	* Gregorian to IFC Conversion 	*
	*********************************	*/

	function isValidGregInput()
	{
		var grDay 	= parseInt (document.getElementById ("GregDay").value );
		var grMonth = parseInt (document.getElementById ("GregMonth").value);
		var grYear 	= parseInt (document.getElementById ("GregYear").value );

		var MonthStr = "The day entered is not in the month of "
		var MaxYear = 65500;
		var LeapYear = false;
			if (grYear % 4 == 0) LeapYear = true;
			
		var errStr = "";

		if ( (grYear < 1970) || (grYear > MaxYear) ) errStr += "Year is either > 1970 or <" + MaxYear + "\n" ;
		if ( isNaN(grYear) ) errStr += " Please enter a year from 1970 to 65500\n";
		
		switch (grMonth)
		{
			case 2: 
				if ( ( (!LeapYear) && (grDay >28) ) || ( (LeapYear) && (grDay >29) ) ) errStr+= MonthStr + IntToMonth(grMonth, false);
				break;
			case 4, 6, 9, 11 :
				if (grDay > 30) errStr += MonthStr + IntToMonth(grMonth, false) +"\n";
				break;
			case 1, 3, 4, 7, 8, 10, 12: 
				//nothing neads to be done the post-switch code checks for this situation.
				break;			
		} // end main switch 

		if ( (grDay < 1) || (grDay > 31) ) errStr += "Day of month is either <0 or > 31\n";
		if ( isNaN(grMonth) ) errStr += "Please Select a Month\n";
		if ( isNaN(grDay) ) errStr  += "Please Select a Day";
			

		return errStr;
	} // end function ValidateInput
	
	function ProcessGregConversion ()
	{
		if ( isValidGregInput() != "" ) alert( isValidGregInput() )
		else 
		{
			var intDay 	 = parseInt (document.getElementById("GregDay").value );
			var intMonth = parseInt (document.getElementById("GregMonth").value);
			var intYear  = parseInt (document.getElementById("GregYear").value );				

			dtIFC = new clsIFCDate( new Date( intYear, intMonth-1, intDay) );

			var insNode = document.getElementById("Conv-IFCDate");
			insNode.innerHTML = dtIFC.getDate();								
		} // end else
	} // end function ProcesssConversion
							
	
/* 	*********************************
	* IFC to Gregorian Conversion 	*
	*******************(*************	*/
	function isValidIFCInput()
	{
		var IFCYear 	= parseInt (document.getElementById ("IFCYear").value );
		var IFCMonth 	= parseInt (document.getElementById ("IFCMonth").value);
		var IFCDay	 	= parseInt (document.getElementById ("IFCDay").value);

		var MonthStr = "The day entered is not in the month of "
		var MaxYear = 65500;
		var LeapYear = false;
			if (IFCYear % 4 == 0) LeapYear = true;
			
		var errStr = "";

		if ( (IFCYear < 1970) || (IFCYear > MaxYear) ) errStr += "Year is either > 1970 or <" + MaxYear + "\n" ;
		if ( isNaN(IFCYear) ) errStr += " Please enter a year from 1970 to 65500\n";
		
		if ( (IFCDay < 1) ) errStr += "Day of month is either <0\n";

		switch (IFCMonth)
		{
			case 6:
				if ( (!LeapYear) && (IFCDay >29) ) errStr+= MonthStr + IntToMonth(IFCMonth);
				break;
			case 13:
				if ( (IFCDay >29) ) errStr+= MonthStr + IntToMonth(IFCMonth);
				break;
			default: 
				if ( (IFCDay > 28) ) errStr+= MonthStr + IntToMonth(IFCMonth);

		} // end main switch 

		if ( isNaN(IFCMonth) ) errStr += "Please Select a Month\n";
		if ( isNaN(IFCDay) ) errStr  += "Please Select a Day";			

		return errStr;
	} // end function ValidateInput


	function ProcessIFCConversion ()
	{	// int Year: number
		var intYear  = parseInt (document.getElementById("IFCYear").value );
		// int Year: number 0 - 12
		var intMonth = parseInt (document.getElementById("IFCMonth").value) -1;
		// int Year: number 0 - 28
		var intDay 	 = parseInt (document.getElementById("IFCDay").value) -1;

		var dtGreg = null;
		
		var Message = isValidIFCInput();
		if ( Message == "") 
		{
			dtGreg = new clsIFCDate(intYear, intMonth, intDay).getGregDate();
			var insNode = document.getElementById("Conv-GregDate");			
			insNode.innerHTML = dtGreg.toLocaleString().replace(/.{11}$/,"");								
		}
		else alert (Message);
		
	} // end function ProcesssConversion
