/*
**	******* Notes by Usman Ghani *********
**	Two modifications have been made in this file.
**	=> First one is to use the names of months in months field instead of month numbers.
**  => Second one is to make the selectedIndex key work for days field also.
*/

/**
 * selectbox-utils for jQuery
 * 
 * Copyright (c) 2007 Yoshiomi KURISU
 * Licensed under the MIT (MIT-LICENSE.txt)  licenses.
 * 
 * @example  $('#year1').numericOptions({from:2007,to:2011});
 * @example  $('#month1').numericOptions({from:1,to:12});
 * @example  $('#date1').numericOptions().datePulldown({year:$('#year1'),month:$('#month1')});
 * 
 */

/************************************
** Start of modification by Usman Ghani
*************************************/
// => Below is an array defined to be used in the numericOptions function
//    to use the names of months instead of month number.
monthsArray={
				1: "Jan", 2: "Feb", 3: "Mar", 4: "Apr", 
				5: "May", 6: "Jun", 7: "Jul", 8: "Aug",
				9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" };
			// */
 
/*monthsArray={
				1: "January", 2: "February", 3: "March", 4: "April", 
				5: "May", 6: "June", 7: "July", 8: "August",
				9: "September", 10: "October", 11: "November", 12: "December" };
			// */
/************************************
** End of modification by Usman Ghani
*************************************/
(function() {  
	$.fn.numericOptions = function(settings){
		settings = jQuery.extend({
			remove:true
			,from:1
			,to:31
			,selectedIndex:0
			,valuePadding:0
			,namePadding:0
		},settings);
		//error check
		if(!(settings.from+'').match(/^\d+$/)||!(settings.to+'').match(/^\d+$/)||!(settings.selectedIndex+'').match(/^\d+$/)||!(settings.valuePadding+'').match(/^\d+$/)||!(settings.namePadding+'').match(/^\d+$/)) return;
		if(settings.from > settings.to) return;
		if(settings.to - settings.from < settings.selectedIndex) return;
		//add options
		if(settings.remove)
			this.children().remove();

		var padfunc = function(v,p)
		{
			if((''+v).length < p)
			{
				for(var i = 0,l = p - (v+'').length;i < l ;i++)
				{
					v = '0' + v;
				}
			}
			return v;
		}

		for(var i=settings.from,j=0;i<=settings.to;i++,j++)
		{
			this.each( function()
			{
				this.options[j] = new Option(padfunc(i,settings.namePadding),padfunc(i,settings.valuePadding));

				/*********************************************************
				** Start of modification code by Usman Ghani
				**********************************************************/
				// => The following code is inserted in order to use the names of the months.
				// => Before this modification, it was showing month numbers in the months field.
				if ( settings.monthNames != null 
					&& settings.monthNames == true
					&& j <= 12 )
				{
					this.options[j].innerHTML = monthsArray[j+1];
					// monthsAray, as used in this code, is defined at the top of this file.
				}
				/************************************
				** End of modification by Usman Ghani
				*************************************/
			});
		}
		this.each(function(){
				if(jQuery.browser.opera){
					this.options[settings.selectedIndex].defaultSelected = true;
				}else{
					this.selectedIndex = settings.selectedIndex;
				}
			});
		return this;
	};

	$.fn.datePulldown = function(settings){
		if(!settings.year || !settings.month) return ;
		var y = settings.year;
		var m = settings.month;
		if(!y.val() || !m.val()) return;
		if(!y.val().match(/^\d{1,4}$/)) return;
		if(!m.val().match(/^[0][1-9]$|^[1][1,2]$|^[0-9]$/)) return;

		var self = this;
		var fnc = function(){
			var tmp = new Date(new Date(y.val(),m.val()).getTime() - 1000);
			var lastDay = tmp.getDate() - 0;
			self.each(function(){
				var ind = (this.selectedIndex<lastDay-1)?this.selectedIndex:lastDay-1;
				this.selectedIndex = ind;
				$(this).numericOptions({to:lastDay,selectedIndex:"test"});
			});
		}

		/*********************************************************
		** Start of modification code by Usman Ghani
		**********************************************************/
		// => The following code is inserted in order to make the "selectedIndex" work
		//    for days field.
		// => Originally this key was not working for day field.
		// => It was working for month and year field but not for the day field.
		// => Before this modification the first day of the month was used to
		//    be selected at the time of initialization of date picker.
		if ( settings.selectedIndex != null )
		{
			$(this)[0].selectedIndex = settings.selectedIndex;
		}
		/************************************
		** End of modification by Usman Ghani
		*************************************/

		y.change(fnc);
		m.change(fnc);
		return this;
	};
})(jQuery);