$(function() {
	$(this).find("form").each(function() {
		if ( window.CalendarPopup ) {
			// instantiate CalendarPopup and set some params
			
			// Sets starting date by yesterdays date
			var yesterday = new Date();
			yesterday.setDate( yesterday.getDate() - 1 );
			
			// new instance of Calendar popup
			cal = new CalendarPopup( "cp" );
			cal.addDisabledDates( null, formatDate( yesterday, "MM/dd/yyyy" ) );
			
			// create the <div> for calendar popup
			$("body").append("<div id=\"cp\"></div>");
		}
		//set focus background color for IE
		if (document.all) {
			$(".textfield").focus(function() {
				$(this).css({background: "fdfcfa"});
			});
			$(".textfield").blur(function() {
				$(this).css({background: "ffffff"});
			});
		}
		// hide all "other" fields
		$("ul.checkboxgroup input.other").filter(".textfield").hide();
		$("ul.checkboxgroup input").click(function() {
			// if the input has a class of other, show it's "other" field
			if ($(this).is(".other")){
				$(this).siblings(".other").show().addClass("required");
				// if it's a checkbox, remove the "other" field when it is unchecked
				if (!this.checked) {
					$.removeWarning($(this));
					$(this).siblings(".other").filter(".textfield").hide().removeClass("required");
				}
			}
			// if it's a radio button group, hide "other" fields if not needed
			else if ($(this).is(".radio")) {
				$.removeWarning($(this));
				$(this).parent().parent().find(".other").filter(".textfield").hide().removeClass("required");
			}
		});
		// hide all "select-other" divs
		$("div.select-other").hide();
		// if "Other" is selected, then show it's "other" field
		$("select.select-other").change( function () {
			if($(this).val() == "--") {
				$(this).parent().next().show();
				$(this).parent().next().find("label").addClass("required");
				$(this).parent().next().find("input").addClass("required");
			} else {
				$(this).parent().next().hide().find("input").val("").removeClass("required");
			}
		});
	});


// Input Elements that need a calendar popup, but no calendar Icon, need a class of "makeitpopup"
	$(".makeitpopup").click(function() {
		cal.select( this, this.id, "MM/dd/yyyy" );
		return false;
	});


// Finds all elements with a class of "form-cal" and replaces them with a calendar icon, link, and onclick handler
	$(this).find(".form-cal").each(function(i) {
		var iconpath = "images/icon-cal.gif";
		if($(this).is('.rez-console'))
			iconpath = "/images/console-cal.gif";
		var icon = '<a href="#" class="icon-cal" id="form-cal-' + i + '" name="form-cal-' + i + '" title="Click to select a date"><img src="'+iconpath+'" /></a>';
		$(icon).appendTo($(this).parent()).click(function(){
			//hide all dropdowns for IE6 and lower
			if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","hidden"); }

			var o = this.previousSibling;
			while (o.nodeType != 1) { o = o.previousSibling; }
			
			// check to see if it is an arrival/departure pair
			if($(o).is(".date-end")) {
				var arrive_string = $(o).parent().prev(".field").children(".date-begin").attr("value");
				var arrive_label = $(o).parent().prev(".field").children("label").html().replace(":","");
				//check to see if begin date has been set already and adjust disabled/current dates
				if (arrive_string != "mm/dd/yyyy" && arrive_string) {
					var arrive_array = arrive_string.split('/');
					var arrive_date = new Date(arrive_array[0]+'/'+arrive_array[1]+'/'+arrive_array[2]);
					var arrive_date = formatDate(arrive_date, "MM/dd/yyyy" );
					cal.clearDisabledDates();
					cal.addDisabledDates( null, arrive_date );
					cal.select(o,$(this).attr("id"),"MM/dd/yyyy", arrive_date);
					return false;
				}
				// begin date not set first, so pull label text and alert
				else {
					alert("Please select \"" + arrive_label + "\" first");
					if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","visible"); }
					return false;
				}
			}
			// it's just a plain calendar popup
			else {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() - 1 );
				cal.addDisabledDates( null, formatDate( yesterday, "MM/dd/yyyy" ) );
				cal.select( o,$(this).attr("id"), "MM/dd/yyyy", yesterday );
				return false;
			}
		});
		$(this).remove();
		
	});


// Validates the form, adding warning icons where needed and alerting user upon completion
	$(".validate").submit(function() {
		var counter = 0;
		var first_error = "";
		
		// find all elements with class name of "required" within all divs with a class of field
		$(this).find("div.field .required").each(function() { 
			// remove any previous warnings and validate based on element type
			$.removeWarning($(this));
			if ( ($(this).is("input")) || ($(this).is("select")) || ($(this).is("textarea")) ) {
				var valid = true;
				var o = $(this).val().replace(/^\s+|\s+$/g, '');

				if ($(this).is(".email")) {
					// if it's an email address make sure the email is valid using both regular expressions
					var regExp = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
					var regExp2 = /(\s+)|(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
					if ( ( o.search( regExp ) ) == -1 || o.search( regExp2 ) != -1 ) {
						valid = false;
					}
				} 
				else if ($(this).is(".select-other") && $(this).val() == "--" && $(this).parent().next().find("input").val().replace( /(^\s+)|(\s+$)/g, '' ).length < 1 ) {
					$.addWarning($(this).parent().next().find("input"));
				} 
				else if ( ($(this).next().is(".icon-cal")) && (o.replace( /(^\s+)|(\s+$)/g, '' ).length < 1 || o == "mm/dd/yyyy") ) {
					// if it's a date field
					valid = false;
				} 
				else if (o.replace( /(^\s+)|(\s+$)/g, '' ).length < 1) {
					// if not an email address take out funky characters and see if its still blank
					valid = false;
				}
				// if the data is not valid show warning icon
				if (!valid) {
					$.addWarning($(this));
					counter++;
					if ( first_error == "" ) {
						first_error = $(this).attr("id");
					}		
				}
				
			}
			else if ( $(this).is("ul") ) {
				// This is for checkboxes
				$(this).each(function() { 
					// find all elements w/required class name
					var valid = false;
					$(this).find("li").each(function() {
						// checkboxes in the list
						var testdis = $(this).children("input");
						//if not an "other" pair
						if ( testdis.length == 1 ) {
							if ( $(testdis).attr("checked") == true ) {
								valid = true;
							}
						}
						//it is an "other"
						else {
							if ( $(testdis[0]).attr("checked") && $(testdis[1]).val().replace( /(^\s+)|(\s+$)/g, '' ).length > 0 ) {
								valid = true;
							}
						}
					});
					if(!valid) { 
						$.addWarning($(this));
						counter++;
						if ( first_error == "" ) {
							first_error = $(this).attr("id");
						}
					}
				});			
			}
		});
		if ( counter > 0 ) {
			alert( "A valid " + $('#'+first_error).parent().children('label').html().replace(":","").replace("*","") + " is required." );
			$('#'+first_error).focus();
			return false;
		}		
	});

// Removes the Warning Icon
	jQuery.removeWarning = function(el) {
		// find img elements, if it's present, remove it
		$(el).parents(".field").find("img.validation-error").remove();
	};
	
// Adds the Warning Icon
	jQuery.addWarning = function(el) {
		var img = "<img />";
		// adds the source and class attributes
		img = $(img).attr("src", "/images/icon-warning.gif").addClass("validation-error");	
		// Add the image after the input field
		// if el is part of checkbox group put it after the first list item
		if( $(el).is("ul") ) {
			$(el).find("li:first").append(img);
		}
		// if not throw it at the end
		else {
			$(el).parent().append(img);
		}
	};





// For Each Package Wrapper add A Toggle Function if Neccessary
	$("div.package-wrapper").each(function() {
	// Div for the Toggler to be Held
	$(this).append("<div class=\"package-toggle\">");

		// If this is a package that is going to be expanded
		// add a control for it
		if( $(this).is(".package-expandable") ) {
			// Since this is expandable hide the details
			$(this).find("div.package-long").hide();
			// Add the Control
			$(this).find(".package-toggle").append("<a class=\"view_details\" href=\"#\">View Details</a>");
		}
	});


// Once the Package Toggler Control is clicked
// Toggle the details and chance the text
	$("div.package-toggle a").toggle(function() {
		$(this).parents("div.package-wrapper").find("div.package-long").animate({
			height: 'show'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("Hide Details");
	}, function() {
		$(this).parents("div.package-wrapper").find("div.package-long").animate({
			height: 'hide'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("View Details");
	});



// For Each Package Wrapper add A Toggle Function if Neccessary
	$("div.wrapper-employee-package").each(function() {
	// Div for the Toggler to be Held
	$(this).append("<div class=\"employee-package-toggle\">");

		// If this is a package that is going to be expanded
		// add a control for it
		if( $(this).is(".package-emp-expandable") ) {
			// Since this is expandable hide the details
			$(this).find("div.employee-package-long").hide();
			// Add the Control
			$(this).find(".employee-package-toggle").append("<a class=\"view_details\" href=\"#\"><img src='images/view-more.gif' alt='' border=''></a>");
		}
	});


// Once the Package Toggler Control is clicked
// Toggle the details and chance the text
	$("div.employee-package-toggle a").toggle(function() {
		$(this).parents("div.wrapper-employee-package").find("div.employee-package-long").animate({
			height: 'show'
		}, 'slow');
		// Changes the Text of the Control
		
		$(this).html("<img src='images/view-less.gif' alt='' border='' class='view-less'>");
		$(this).parent().css('position','static');
	}, function() {
		$(this).parent().css('position','relative');
		$(this).parents("div.wrapper-employee-package").find("div.employee-package-long").animate({
			height: 'hide'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("<a class=\"hide_details\" href=\"#\"><img src='images/view-more.gif'  alt='' border=''></a>");
		
	});
});

function flashPutHref() {
	
}
