// Document Ready page init functionvar debug = false;var DFLT_RESTAURANT = 'Any';function initAppMenus() {	// Check if the request came in from a particular job posting	if(!prefillMenus())	{		// Otherwise disable sub-menus and load restaurants:		$("#locationSelect").attr("disabled","disabled");		$("#positionSelect").attr("disabled","disabled");		// Load the restaurant menu		appendOptions(getRestaurantNames(), '#restaurantSelect', true);	}	// restaurantSelect	$("#restaurantSelect").change(function()	{		var val = this.options[this.selectedIndex].value;		if(val != null && val != '')		{			// update sub-menu			appendOptions(getLocationNames(val), '#locationSelect', true);						// Enable the  location menu			$("#locationSelect").removeAttr("disabled");			// Disable the position menu			$("#positionSelect").attr("disabled","disabled");			$("#Restaurant").val(val);		}		else		{			// Disable the sub-menus			clearOptions("#locationSelect");			clearOptions("#positionSelect");			$("#positionPhone").val('');			$("#positionAddress").val('');			$("#locationSelect").attr("disabled","disabled");			$("#positionSelect").attr("disabled","disabled");			$("#Restaurant").val('');		}		$("#Location").val('');		$("#JobTitle").val('');	});		// locationSelect	$("#locationSelect").change(function()	{		var val = this.options[this.selectedIndex].value;		if(val != null && val != '')		{			log("Selected Location: '" + val + "'");						fillContactInfo($('#restaurantSelect option:selected').val(), val);						// Enable the sub-menu			$("#positionSelect").removeAttr("disabled");			appendOptions(getPositionNames($('#restaurantSelect option:selected').val(), val), '#positionSelect', true);			$("#positionSelect").children('option:eq(0)').attr('selected', 'selected');			$("#Location").val(val);		}		else		{			$("#positionPhone").val('');			$("#positionAddress").val('');			$("#positionSelect").children('option:eq(0)').attr('selected', 'selected');			$("#positionSelect").attr("disabled","disabled");			$("#Location").val('');		}		$("#JobTitle").val('');	});		// positionSelect	$("#positionSelect").change(function()	{		var val = this.options[this.selectedIndex].value;		if(val != null && val != '')		{			log("Selected Position: '" + val + "'");						$("#JobTitle").val(val);		}		else		{			$("#JobTitle").val('');		}	});}// Check to see if we have an existing restaurant, location or position values// Return true if values were found and the menus were filled, false otherwisefunction prefillMenus(){	var prefilled = false;	var restVal = (null == $('#Restaurant').val() || '' == $('#Restaurant').val()) ? DFLT_RESTAURANT : $('#Restaurant').val();	var locVal  = (null == $('#Location').val() || '' == $('#Location').val()) ? DFLT_RESTAURANT : $('#Location').val();	var posVal = (null == $('#JobTitle').val() || '' == $('#JobTitle').val()) ? DFLT_RESTAURANT : $('#JobTitle').val();	//alert("Values= " + restVal + ", " + locVal + ", " + posVal);		if(null != restVal && '' != restVal)	{		var rest = getRestaurant(restVal);		if(null != rest)		{			prefilled = true;			// Populate and select the restaurant menu			appendOptions(getRestaurantNames(), '#restaurantSelect', true);			$("#restaurantSelect").selectOptions(rest.name, true);						// Enable and fill the child menus			appendOptions(getLocationNames(rest.name), '#locationSelect', true);			$("#locationSelect").removeAttr("disabled");			var loc = getLocation(rest.name, locVal);			if(null != loc)			{				$("#locationSelect").selectOptions(loc.name, true);				fillContactInfo(rest.name, rest.locations[i].name);								appendOptions(getPositionNames(rest.name, loc.name), '#positionSelect', true);				$("#positionSelect").removeAttr("disabled");			}						if(null != posVal && '' != posVal && null != loc) 			{				for(i in loc.positions)				{					if(posVal == loc.positions[i].name)					{						$("#positionSelect").selectOptions(loc.positions[i].name, true);					}				}			} // if(null != posVal && '' != posVal) 					} // if(null != rest)	} // if(null != restVal && '' != restVal)		return prefilled;}// Append an option to a select menufunction appendOptions(values, menuId, reset){	if(reset)		clearOptions(menuId);			for(i in values) {		//log("Appending '" + values[i] + "' to menu '" + menuId + "'");		$('<option value="' + values[i] + '">' + values[i] + '</option>').appendTo($(menuId));	}}// Clear all but the first optionfunction clearOptions(menuId){	log("Before clear menu size is " + $(menuId).children('option').length);	log("Children: " + $(menuId).children('option').length);	$(menuId).children('option:gt(0)').remove();	log("After clear menu size is " + $(menuId).children('option').length);}function getRestaurants(){	return restData.restaurants;}function getRestaurantNames(){	var rslt = [];	for(i in restData.restaurants)		rslt.push(restData.restaurants[i].name);		return rslt.sort();}function getRestaurant(name){	var rest = null;	for(i in restData.restaurants) {		if(name == restData.restaurants[i].name)			rest = restData.restaurants[i]	}	log("Restaurant lookup: " + (null == rest ? "Restaurant lookup failed!" : "Found it!"));	return rest;}function getLocation(resaurantName, locationName){	var loc = null;	var rest = getRestaurant(resaurantName);	for(i in rest.locations) {		if(locationName == rest.locations[i].name)			loc = rest.locations[i]	}	log("Location lookup: " + (null == loc ? "Location lookup failed!" : "Found it!"));	return loc;}function getLocationNames(restaurantName){	//log("Getting location names for '" + restaurantName + "'");	var rslt = [];	var rest = getRestaurant(restaurantName)	if(null != rest)	{		log(rest.name + " has " + rest.locations.length + " Locations.");		for(i in rest.locations) {			rslt.push(rest.locations[i].name);		}	}	//log("Locations: " + rslt);	return rslt.sort();}function getPositionNames(resaurantName, locationName){	//log("Getting location names for '" + restaurantName + "'");	var rslt = [];	var loc = getLocation(resaurantName, locationName)	if(null != loc)	{		//log(loc.name + " has " + loc.positions.length + " Positions.");		for(i in loc.positions)			rslt.push(loc.positions[i].name);	}		//log("Positions: " + rslt);	return rslt.sort();}function fillContactInfo(restaurantName, locationName){	log("Filling contact fields for '" + restaurantName + "' and '" + locationName + "'");	var rest = getRestaurant(restaurantName)	if(null != rest)	{		var locs = rest.locations		for(i in locs)		{			if(locationName == locs[i].name)			{				$("#positionPhone").val(locs[i].phone);				$("#positionAddress").val(locs[i].email);				log("Contact Phone is " + $("#positionPhone").val() + "\nContact Email is " + $("#positionAddress").val());			}		}	}}function log(msg) {	if(debug)		alert(msg);}
