function writeOptions(startNumber,endNumber)
{
	var optionCounter;
	
	for (optionCounter = startNumber; optionCounter <= endNumber; optionCounter++)
	{
		
		document.write("<option value = "+ optionCounter + ">" + optionCounter);
	}
}

//when writing options for 'minutes', include leading zero on number lower than 10
function writeMinOptions(startNumber,endNumber)
{
	var optionCounter;
	
	for (optionCounter = startNumber; optionCounter <= endNumber; optionCounter++)
	{
	
		if (optionCounter < 10)
			{
			document.write("<option value = "+ optionCounter + ">0" + optionCounter);
			}
		else
			{
			document.write("<option value = "+ optionCounter + ">" + optionCounter);
		}	
	}
}

//-----------------------------------------------------------------------------------------------------------------------


function recalcDateDiff()
{
	var myForm = document.form1;
	
	var secondDay = myForm.secondDay.options[myForm.secondDay.selectedIndex].value;
	var secondMonth = myForm.secondMonth.options[myForm.secondMonth.selectedIndex].value;
	var secondYear = myForm.secondYear.options[myForm.secondYear.selectedIndex].value;
	var secondHour = myForm.secondHour.options[myForm.secondHour.selectedIndex].value;
	var secondMinute = myForm.secondMinute.options[myForm.secondMinute.selectedIndex].value;
	var secondSecond = 0;	
	
	var months = new Array("January", "February","March","April","May","June","July","August","September","October", "November", "December");
	var myDays = new Array("Sunday", "Monday","Tuesday","Wednesday","Thursday", "Friday", "Saturday");
	var firstDate = new Date();
	var secondDate = new Date();
	
	secondDate.setDate(secondDay);
	secondDate.setMonth(secondMonth);
	secondDate.setFullYear(secondYear);	
	secondDate.setHours(secondHour);
	secondDate.setMinutes(secondMinute);	
	secondDate.setSeconds(secondSecond);
	
	
	var daysDiff = (secondDate.valueOf() - firstDate.valueOf());
	
	var usrMsgDateOK = "<h4>"+""+"</ h4>"
	var usrMsgDateNOTok = "<h4>"+"* The Date/Time selected above is AFTER the current Date/Time. Information below is NOT updated/accurate!"+"</ h4>";
	
	
	if (!(daysDiff > 0))
	{
		document.getElementById("usrDateLate").innerHTML = usrMsgDateOK; 		
		usrInputIsFine();
	}
	else
	{
		document.getElementById("usrDateLate").innerHTML =usrMsgDateNOTok; 
	}
			
}



function usrInputIsFine()
{
	var myForm = document.form1;
	
	var secondDay = myForm.secondDay.options[myForm.secondDay.selectedIndex].value;
	var secondMonth = myForm.secondMonth.options[myForm.secondMonth.selectedIndex].value;
	var secondYear = myForm.secondYear.options[myForm.secondYear.selectedIndex].value;
	var secondHour = myForm.secondHour.options[myForm.secondHour.selectedIndex].value;
	var secondMinute = myForm.secondMinute.options[myForm.secondMinute.selectedIndex].value;
	var secondSecond = 0;	
	
	var months = new Array("January", "February","March","April","May","June","July","August","September","October", "November", "December");
	var myDays = new Array("Sunday", "Monday","Tuesday","Wednesday","Thursday", "Friday", "Saturday");
	var firstDate = new Date();
	var secondDate = new Date();
	
	secondDate.setFullYear(secondYear);	
	secondDate.setMonth(secondMonth);
	secondDate.setDate(secondDay);
	secondDate.setHours(secondHour);
	secondDate.setMinutes(secondMinute);	
	secondDate.setSeconds(secondSecond);
	
//	alert("day number selected: " + secondDay + "  Date Reads: " + secondDate)
	
	
	var daysDiff = (secondDate.valueOf() - firstDate.valueOf());
	
	var myDatesDiff = (firstDate.valueOf() - secondDate.valueOf());
	
	daysDiff = Math.floor(Math.abs((((daysDiff / 1000) / 60) / 60) / 24));
	

// ----------------------------------------------------- Clean Time Fun ----------------------------------------------
	var stValentines = new Date()
	stValentines.setFullYear(myForm.secondYear.options[myForm.secondYear.selectedIndex].value)
	stValentines.setMonth(1)
	stValentines.setDate(14);
	var stValentinesDiff = (stValentines .valueOf() - secondDate.valueOf()) ;
	stValentinesDiff = (Math.abs(((( stValentinesDiff / 1000) / 60) / 60) / 24));
	
	var halloWeen = new Date()
	halloWeen.setFullYear(myForm.secondYear.options[myForm.secondYear.selectedIndex].value)
	halloWeen.setMonth(9)
	halloWeen.setDate(31);
	var halloWeenDiff = (halloWeen.valueOf() - secondDate.valueOf()) ;
	halloWeenDiff = (Math.abs(((( halloWeenDiff / 1000) / 60) / 60) / 24));

	

	var xmasNext = new Date()
	xmasNext.setFullYear(myForm.secondYear.options[myForm.secondYear.selectedIndex].value)
	xmasNext.setMonth(11)
	xmasNext.setDate(25);
	var xmasDiff = (xmasNext.valueOf() - secondDate.valueOf()) ;
	xmasDiff = (Math.abs(((( xmasDiff / 1000) / 60) / 60) / 24));
	
	
	

	var stPats = new Date()
	stPats.setFullYear(myForm.secondYear.options[myForm.secondYear.selectedIndex].value)
	stPats.setMonth(2)
	stPats.setDate(17);
	var stPatsDiff = (stPats.valueOf() - secondDate.valueOf()) ;
	stPatsDiff  = (Math.abs(((( stPatsDiff  / 1000) / 60) / 60) / 24));
	

	var txtMillClean = (secondDate.valueOf() - firstDate.valueOf());
	var txtDaysClean = Math.floor(Math.abs(((( txtMillClean / 1000) / 60) / 60) / 24));
	


// building the statement

	var yrFull=""
	var mthFull=""
	var wkFull=""
	var dyFull=""
	
	
	// YEAR part
	var txtYearsClean = txtDaysClean / 365;	
	var txtFullYearsClean = parseInt(txtYearsClean); 	
	if (parseInt(txtYearsClean)>=2)
		{
			yrFull = yrFull + txtFullYearsClean + " years";
		}
	if(parseInt(txtYearsClean)==1)	
		{
			yrFull = yrFull + txtFullYearsClean + " year";
		}
	var txtDaysUsedByYears = txtFullYearsClean * 365;
	var txtDaysLeftAfterYears = txtDaysClean - txtDaysUsedByYears; 
	
	//MONTH part
	var txtMonthsClean = txtDaysLeftAfterYears / (365/12);
	var txtFullMonthsClean = parseInt(txtMonthsClean)
	if (parseInt(txtMonthsClean)>=2)
		{
			mthFull = mthFull + txtFullMonthsClean + " months";
		}
	if (parseInt(txtMonthsClean)==1)	
		{
			mthFull = mthFull + txtFullMonthsClean + " month";
		}
	var txtDaysUsedByMonths = parseInt(txtFullMonthsClean * (365/12));
	var txtDaysLeftAfterMonths = txtDaysLeftAfterYears - txtDaysUsedByMonths;
	
	//WEEK part
	 var txtWeeksClean = txtDaysLeftAfterMonths / 7;
	 var txtFullWeeksClean = parseInt(txtWeeksClean);
	 if (parseInt(txtWeeksClean)>=2)
	 	{
	 		wkFull = wkFull +  txtFullWeeksClean + " weeks";
	 	}
	 if (parseInt(txtWeeksClean)==1)
		{
	 		wkFull = wkFull +  txtFullWeeksClean + " week";
	 	}
	 var txtDaysUsedByWeeks = parseInt(txtFullWeeksClean) * 7 ;
	 var txtDaysLeftAfterWeeks = txtDaysLeftAfterMonths - txtDaysUsedByWeeks ;
	 
	 if (txtDaysLeftAfterWeeks >=2)
	 	{
	 		dyFull = dyFull + txtDaysLeftAfterWeeks + " days";
	 	}
	 if (txtDaysLeftAfterWeeks == 1)
	 	{
	 		dyFull = dyFull +  	txtDaysLeftAfterWeeks + " day";
	 	}	
	  		
	
	var txtFullParts = ""
	var txtFullState = "You can now say you are free of a bet " 
	

	
	if (!(yrFull ==""))
		{
		txtFullParts = txtFullParts + yrFull;
		}
		

	
	if(!(mthFull ==""))
		{
			if(!(txtFullParts ==""))
				{
				txtFullParts = txtFullParts + " and " +  mthFull;

				} 	
			else
				{
				txtFullParts = txtFullParts + mthFull
				}
		}
			

	
	if (!(wkFull ==""))
		{
			if(!(txtFullParts ==""))
				{
				txtFullParts  = txtFullParts + " and " + wkFull;
				}
			else
				{
				txtFullParts = txtFullParts + wkFull;
				}
		}
		
	
	if (!(dyFull==""))
		{
			if (!(txtFullParts==""))
				{
				txtFullParts = txtFullParts + " and " + dyFull;
				}
			else
				{
				txtFullParts = txtFullParts + dyFull;
				}
		}						
				 

	txtFullState = txtFullState + txtFullParts;
	
	txtFullState =txtFullState + "."

	var divDiff = (secondDate.valueOf() - firstDate.valueOf());
	
	var myDivText = "<p>" +"<strong>"+ commaSplit(txtDaysClean) + " days." +"</strong>"  + "</ p>"	
	myDivText = myDivText + "<p>" + txtFullState + "</p>"
	myDivText = myDivText + "<p>" + "It is now " +  commaSplit((Math.abs(divDiff) /1000).toFixed(0)) + " seconds since your last bet. " 
	myDivText = myDivText + "This (of course) means it is " + commaSplit(((Math.abs(divDiff) /1000) / 60).toFixed(2))+ " minutes since your last bet. "
	myDivText = myDivText + "Obviously, it is " + commaSplit(    (((Math.abs(divDiff) /1000) / 60) / 60).toFixed(2)     ) + " hours since you last bet" 
	myDivText = myDivText + ", but then again - you already knew this, didn't you?" + "</ p>"
	myDivText = myDivText + "<p>" + "You stopped gambling on a " + myDays[(secondDate.getDay())] +". " + "</ p>"
	
	myDivText = myDivText + "<p>" + "St. Valentine's Day of that year was " + stValentinesDiff.toFixed(0) + " days away." + "</ p>"
	myDivText = myDivText + "<p>" + "St. Patrick's Day of that year was " + stPatsDiff.toFixed(0) + " days away." + "</ p>"
	myDivText = myDivText + "<p>" + "Halloween of that year was " + halloWeenDiff.toFixed(0) + " days away." + "</ p>"
	myDivText = myDivText + "<p>" + "Christmas of that year was " + xmasDiff.toFixed(0) + " days away." + "</ p>"
	

	myDivText = myDivText + "<p>" + "</ p>"	
	myDivText = myDivText + "<p>" +"A month for our calculations is 365 days divided by 12 which is actually " + 365 / 12 + " days - which is a little over the top! (Ah well...) " + "</ p>"

	document.getElementById("cleanDivStuff").innerHTML = myDivText;
	

}

function commaSplit(srcNumber) {
var txtNumber = '' + srcNumber;
if (isNaN(txtNumber) || txtNumber == "") {
alert("Oops!  That does not appear to be a valid number.  Please try again.");
fieldName.select();
fieldName.focus();
}
else {
var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
var arrNumber = txtNumber.split('.');
arrNumber[0] += '.';
do {
arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
} while (rxSplit.test(arrNumber[0]));
if (arrNumber.length > 1) {
return arrNumber.join('');
}
else {
return arrNumber[0].split('.')[0];
      }
   }
}

//-------------------------------------------Clean time Fun -----------------------------------------------------------
function window_onload()
{
	var theForm = document.form1;
	var nowDate = new Date();

	theForm.secondDay.options[nowDate.getDate()-1].selected=true;
	theForm.secondMonth.options[nowDate.getMonth()].selected=true;	
	theForm.secondYear.options[nowDate.getFullYear()- 1970].selected=true;	
	theForm.secondHour.options[nowDate.getHours()].selected=true;
	theForm.secondMinute.options[nowDate.getMinutes()].selected=true;
}	


function myClock()
{
var months = new Array("January", "February","March","April","May","June","July","August","September","October", "November", "December");
var dateNow = new Date();
var yearNow = dateNow.getFullYear();
var monthNow = months[dateNow.getMonth()];
var dayNow = dateNow.getDate();
var hourNow= dateNow.getHours();
var minNow= dateNow.getMinutes();
var secNow= dateNow.getSeconds();
var AorP=" "


if(hourNow>=12)	AorP="P.M."; else AorP="A.M.";

if(hourNow>=13)	hourNow -=12;
if(hourNow==0)hourNow=12;

if(minNow<10) minNow="0"+minNow;
if(secNow<10) secNow="0"+secNow;



var daySuffix;

switch (dayNow)
	{
	case 1:
	case 21:
	case31:
		daySuffix="st";
		break;
	case 2:
	case 22:
		daySuffix="nd";
		break;
	case 3:
	case 23:
		daySuffix="rd";
		break;	
	default:
		daySuffix="th";
		break;
	}
	
document.getElementById("txtNow").value= ( monthNow + " " + dayNow + ", " + yearNow + "  " + hourNow + ":" + minNow + ":" + secNow + " " + AorP );


if (document.title == "Gamblers Anonymous Ireland Fun Calc")
	{
		recalcDateDiff()
	}

}





