	function getDateFromStringDate( date )
	{
		var d = new Date( );
		var parts = date.split(' ');

		var dparts = parts[0].split('-');

		// this sets the date part
		d.setFullYear(dparts[0], dparts[1]-1, 0);	

		// time to handle time now!

		if(parts.length > 1)
		{
			// we have time!
			var tparts = parts[1].split(':');

			// make an exception for PM values
			if(tparts.length > 2)
			{
				if(tparts[2] == 'PM')
				{
					var n = new Number(tparts[0]);

					if( n != 12 )
					{
						n += 12;
					}
					tparts[0] = n;
				}
			}

			d.setHours(tparts[0]);
			d.setMinutes(tparts[1]);
			d.setSeconds(0);
			//d.setSeconds(tparts[2]);
		}

		return d;
	}

	/*
	function getDateFromStringDate( date )
	{
		var d = new Date( );
		var parts = date.split(' ');

		var dparts = parts[0].split('-');

		// this sets the date part
		d.setFullYear(dparts[0], dparts[1]-1, dparts[2]);	

		// time to handle time now!

		if(parts.length > 1)
		{
			// we have time!
			var tparts = parts[1].split(':');

			// make an exception for PM values
			if(tparts.length > 3)
			{
				if(tparts[3] == 'PM')
				{
					var n = new Number(tparts[0]);
					n += 12;
					tparts[0] = n;
				}
			}

			d.setHours(tparts[0]);
			d.setMinutes(tparts[1]);
			d.setSeconds(tparts[2]);
		}

		return d;
	}
	*/

	function fillStates( )
	{
		//alert('foot');
		//alert(regions);
		//return;
		var frm = getForm('frmMain');
		var state = frm.state;
		var fin = new Array( );

		resetStates( frm );

		/*
		// have we selected a time zone?
		for(var i = 0; i < frm.timezone.options.length; i++)
		{
			if(frm.timezone.options[i].value == '0')
			{
				if(frm.timezone.options[i].selected)
				{
					// no timezone selected
					return;
				}
			}
		}
		*/

		//state.options.length = 0;

		for(var r in regions)
		{
			for(var i in regions[r])
			{
				//state.options[state.options.length] = new Option(regions[r][i], regions[r][i]);	
				fin.push(regions[r][i]);
			}
		}

		fin.sort();

		//state.options[0] = new Option("--select your state--", '0');
		state.options[0] = new Option(state.options[0].text, state.options[0].value);
		for(var i in fin)
		{
			state.options[state.options.length] = new Option(fin[i], fin[i]);
		}
	}

	function getRegion()
	{
		var frm = getForm('frmMain');
		var state = frm.state;

		// find the region for the state
		for(var r in regions)
		{
			for(var i = 0; i < regions[r].length; i++)
			{
				if(regions[r][i] == state.value)
				{
					return r;
					break;
				}
			}
		}

		return null;
	}

	function fillMonths()
	{
		var frm = getForm('frmMain');
		var state = frm.state;
		var month = frm.month;
		// find the region for the state
		var curRegion = getRegion( );

		if(! curRegion )
		{
			//alert('Unable to find associated region!');
			return;
		}

		// mark this as our region
		frm.rid.value = curRegion;

		if(! month )
		{
			return;
		}

		// Reset the form
		resetMonths(frm);


		// have we selected a time zone?
		for(var i = 0; i < frm.timezone.options.length; i++)
		{
			if(frm.timezone.options[i].value == '0')
			{
				if(frm.timezone.options[i].selected)
				{
					// no timezone selected
					// alert('No Timezone Selected');
					return;
				}
			}

			// alert(frm.timezone.options[i].selected);
		}

		month.options.length = 0; // reset items

		//for(var i = 0; i < days.length; i++)
		month.options[0] = new Option('--select month--', '0');
		for(var m in alldays[curRegion])
		{
			if(! m )
			{
				resetMonths(frm);
				month.options[0] = new Option("No Timeslots Left", "0");
				return;
			}
			month.options[month.options.length] = new Option(m, m);
		}
	}

	function fillDays( )
	{
		var frm = getForm('frmMain');
		var region = getRegion();

		if(! frm.month )
		{
			return;
		}

		var mon = frm.month.value;
		var day = frm.day;

		resetDays( frm );

		// find the region for the state
		var curRegion = getRegion( );

		if(! curRegion )
		{
			//alert('Unable to find associated region!');
			return;
		}

		if(mon == 0)
		{
			return;
		}

		day.options.length = 0; // reset items

		//for(var i = 0; i < days.length; i++)
		day.options[0] = new Option('--select day--', '0');
		for(var m in alldays[curRegion][mon])
		{
			day.options[day.options.length] = new Option(m, m);
		}
	}

	// fill in the dynamic time values
	// based on timezone
	function fillTimes( )
	{
		var frm = getForm('frmMain');
		if(! frm.month )
		{
			return;
		}
		var mon = frm.month.value;
		var day = frm.day.value;
		var time = frm.time;

		resetTimes( frm );

		// find the region for the state
		var curRegion = getRegion( );

		if(! curRegion )
		{
			//alert('Unable to find associated region!');
			return;
		}

		if(day == 0)
		{
			return;
		}

		// NEW: for timezone offsets (phase 2)
		// get the offset values we need
		var key = curRegion + '_' + frm.timezone.value;
		//alert(key);
		//return;

		var offsetValue = tzmap[key];
		
		//alert(offsetValue);

		time.options.length = 0; // reset items

		time.options[0] = new Option('--select time--', '0');

		for(var m in alldays[curRegion][mon][day])
		{
			var val = alldays[curRegion][mon][day][m];

			// apply the offset
			//alert(val);
			var d = getDateFromStringDate( '2005-12-26 ' + val[1].replace(' ', ':') );

			// this is where the apply happens
			d.setHours(d.getHours() + offsetValue);

			var h = d.getHours( );
			var ampm = 'AM';


			if(h == 0)
			{
				h = 12;
				ampm = 'AM'
			}
			else
			{
				if(h == 12)
				{
					ampm = 'PM';
				}
				else
				{
					if(h > 12)
					{
						h -= 12;
						ampm = 'PM';
					}
				}
			}
			
			//time.options[time.options.length] = new Option(val[1], val[0]);
			time.options[time.options.length] = new Option(h + ':00:00 ' + ampm, val[0]);
			//time.options[time.options.length] = new Option(d.getHours(), val[0]);
			//time.options[time.options.length] = new Option(val[1], val[0]);
		}

		/*
		for(var m in alldays[curRegion][mon][day])
		{
			var val = alldays[curRegion][mon][day][m];

			// apply the offset
			var d = getDateFromStringDate( '2005-12-26 ' + val[1].replace(' ', ':') );

			// this is where the apply happens
			d.setHours(d.getHours() + offsetValue);

			var h = d.getHours( );
			var ampm = 'AM';

			if(h == 0)
			{
				h = 12;
				ampm = 'PM'
			}
			else
			{
				if(h > 12)
				{
					h -= 12;
					ampm = 'PM';
				}
			}

			
			//time.options[time.options.length] = new Option(val[1], val[0]);
			time.options[time.options.length] = new Option(h + ':00:00 ' + ampm, val[0]);
		}
		*/
	}

	function resetAll( )
	{
		var frm = getForm('frmMain');
		resetMonths( frm );

		frm.timezone.options[0].selected = true;
	}

	function resetStates( frm )
	{
		var state = frm.state;
		state.options[0] = new Option(state.options[0].text, state.options[0].value);
		state.options.length = 1;
		//state.options[0] = new Option("---select timezone first---", "0");
		return resetMonths( frm );
	}

	function resetMonths( frm )
	{
		var month = frm.month;
		if(! month )
		{	
			return;
		}
		month.options.length = 0;
		month.options[0] = new Option("---select timezone first---", "0");
		return resetDays( frm );
	}

	function resetDays( frm )
	{
		var day = frm.day;
		if(! day )
		{
			return;
		}
		day.options.length = 0;
		day.options[0] = new Option("---select month first---", "0");
		return resetTimes( frm );
	}

	function resetTimes( frm )
	{
		var time = frm.time;
		if(! time )
		{
			return;
		}
		time.options.length = 0;
		time.options[0] = new Option("---select state/time first---", "0");
		return false;
	}

	function getForm(name)
	{
		var f = document.getElementById(name);

		if( ! f )
		{
			f = eval('document.' + name);
		}

		return f;
	}


