//First script for res form
var cal = new CalendarPopup();

cal.showYearNavigation();
cal.setTodayText('');

cal.setReturnFunction("setD");
function setD(y,m,d) {
	document.checkaressrch.year.value=y;
	document.checkaressrch.month.selectedIndex=--m;
	for (var i=0; i<document.checkaressrch.day.options.length; i++) {
		if (document.checkaressrch.day.options[i].value==d) {
			document.checkaressrch.day.selectedIndex=i;
			}
		}
	}

function getDateString(y_obj,m_obj,d_obj) {
	var y = y_obj.options[y_obj.selectedIndex].value;
	var m = m_obj.options[m_obj.selectedIndex].value;
	var d = d_obj.options[d_obj.selectedIndex].value;
	if (y=="" || m=="") { return null; }
	if (d=="") { d=1; }
	return str= y+'-'+m+'-'+d;
	}


//Second script for res form
var advancedDay = 2;

function isLeapYear(yrVal)
{
	var leapYear=false;
	var year = parseInt(yrVal, 10);

	if (year%4 == 0)
		{
		leapYear=true;
		}
	return leapYear;
}

function getDaysInMonth(monthVal, YrVal)
{
	var maxDays=31
	if (monthVal==1)
	{
		if (isLeapYear(YrVal))
			{
			maxDays=29;
			}
		else
			{
			maxDays=28;
			}
	}
	if (monthVal==3 || monthVal==5 || monthVal==8 || monthVal==10)
		{
		maxDays=30;
		}
	return maxDays;
}


function init() {
	var dt = new Date();

	var yr = dt.getFullYear();
	var curYr = dt.getFullYear();
	var mo = dt.getMonth();
	mo++;
	var da = dt.getDate();

	var daysInCurrent = getDaysInMonth(da, yr);

	if (da + advancedDay > daysInCurrent)
	{	da = ((da+advancedDay) % daysInCurrent);
		if (mo == 12)
		{	mo = 1;
			yr++;	}
		else
		{	mo++;	}	}
	else
	{	da = da + advancedDay;	}
	da--;
	mo--;
	yr-=curYr;
	document.checkaressrch.month.selectedIndex = mo;
	document.checkaressrch.day.selectedIndex = da;
	document.checkaressrch.year.selectedIndex = yr;
	return;  }	
	
ActiviateChildAgeCollection();

function ActiviateChildAgeCollection()
{
  (function ($)
  {
	$(function ()
	{
	  var iMaxChildAge = 17;
	  var sChildrenAges = "";
	  var sChildNumberAgeLabel = "Child {!Number} Age";
	  var oChildrenAges = sChildrenAges.split(",");

	  $("#Form2 select#Select7").change(function ()
	  {
		CreateChildrenAgeDropdowns();
	  });
	  
	  $("#Form2").submit(function()
	  {
		 var bValid = true;
		 $("#Form2 .childrenAgeShell select").each(function ()
		  {
			if ($(this).val() == "")
			{
			  bValid = false;
			}
		  });
		 if (bValid == false)
		 {
			alert("The ages for children are required and they must be "+iMaxChildAge+" years and under.");
			return false;
		 }
		 return true;
	  });

	  //LOAD DROPDOWNS ONLOAD IF PRESELECTED VALUES ARE AVAILABLE (USEFUL FOR POSTBACK WITH ERRORS)
	  CreateChildrenAgeDropdowns(true);

	  function CreateChildrenAgeDropdowns(bPreloadValues)
	  {
		//get/create children age shell
		var $childrenAgesShell = $("#Form2 #childrenAgesShell");
		if ($childrenAgesShell.length == 0)
		{
		  $childrenAgesShell = $("<tr><td colspan='2' align='left'></td></tr>");
		  $("td", $childrenAgesShell).append("<div id='childrenAgesShell' class='childrenAgesShell'></div>")

		  //add new table row after children dropdown row
		  $("#Form2 select#Select7").parent().parent().after($childrenAgesShell);
		  $childrenAgesShell = $("#childrenAgesShell", $childrenAgesShell);
		}
		

		//build age dropdown options for each child
		var iChildren = parseInt($("select#Select7").val(), 10);
		if (iChildren == 0)
		{
		  $("#Form2 .childrenAgeShell").remove();
		}
		else
		{
		  var sChildrenAgeOptions = "";
		  for (var i = 1; i <= iChildren; i++)
		  {
			if ($("#Form2 #childrenAge" + i + "Shell").length == 0)
			{
			  var sChildrenAgeDropdown = "";
			  sChildrenAgeDropdown += "<div id='childrenAge" + i + "Shell' class='childrenAgeShell'>";
			  sChildrenAgeDropdown += "<label id='childrenAge" + i + "Label' class='label childrenAgeLabel' for='childrenAge" + i + "Field'>" + sChildNumberAgeLabel.replace("{!Number}", i) + ":</label>";
			  if (sChildrenAgeOptions == "")
			  {
				sChildrenAgeOptions += "<option value=''></option>"
				for (var a = 0; a <= iMaxChildAge; a++)
				{
				  sChildrenAgeOptions += "<option value= '" + a + "' >" + a + "</option>";
				}
			  }
			  sChildrenAgeDropdown += "<select id='childrenAge" + i + "Field' name='childrenAge" + i + "Field' class='childrenAgeField'>" + sChildrenAgeOptions + "</select>";
			  sChildrenAgeDropdown += "</div>";
			  $childrenAgesShell.append(sChildrenAgeDropdown);
			}
		  }
		  //remove any dropdowns left if they were greater than the newly selected child count
		  $("#Form2 .childrenAgeShell:gt(" + (iChildren - 1) + ")").remove();

		  if (bPreloadValues == true && sChildrenAges != "")
		  {
			var $childrenAges = $(".childrenAgeField", $childrenAgesShell);
			for (var i = 0; i < oChildrenAges.length; i++)
			{
			  $childrenAges.eq(i).val(oChildrenAges[i]);
			}
		  }
		}
	  }
	});
  })(jQuery);
}


