//$(document).ready(function() {  This is the same as the next line below
$(function() {
	activateActiveX();
	$.id = function(id){  return document.getElementById(id);	 }

			$("#primary-nav li").hover(
				function(){ $("ul", this).fadeIn("fast"); }, 
				function() { } 
			);
	  	if (document.all) {
				$("#primary-nav li").hoverClass ("sfHover");
			}

/*	$("#primary-nav li").hover(
		function(){ $("ul", this).fadeIn("fast"); }, 
		function() { } 
	);
	if (document.all) {
			$("#primary-nav li").hoverClass ("sfHover");
	}*/


// Finds a form on the page and instantiates a Calendar Popup
// if it is present
	$(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 popuu
			cal = new CalendarPopup( "cp" );
			cal.addDisabledDates( null, formatDate( yesterday, "yyyy-MM-dd" ) );
			
			// create the <div> for calendar popup
			var d = document.createElement( "div" );
			d.id = "cp";
			document.body.appendChild( d );
		}
	});

	// For Input Elements that need a calendar, but no calendar Icon
	// Simply put a class on each input and put that class name in the
	// function below
	$(".makeitpopup").click(function() {
		var o = this;
		cal.select( o, this.id, "MM/dd/yyyy" );
		return false;
	});



// For Input Elements w/a calendar icon
// Put the class name of the span where the Icon will leave
// preferrably behind the input
	$(this).find(".form-cal").each(function() {
		// finds each form-cal instance and 
		// replaces <span> tags with calendar image
		
		// creates the calendar icon with a link and attributes
		var calLink = document.createElement( "a" );
		calLink.href = "#";
		calLink.id = "form-cal-" + $(this).siblings ("input").id();
		calLink.name = "form-cal-" + $(this).siblings ("input").id();
		calLink.title = "Click to select a date";

		// creates the icon and adds it
		var calImg = document.createElement( "img" );
		calImg.src = "images/icon-cal.gif";
		calLink.appendChild( calImg );
		
		// adds the icon and removes the span
		this.parentNode.appendChild( calLink );
		this.parentNode.removeChild( this );

		// when the icon is clicked show the popup
		$(calLink).click(function(){
			var o = this.previousSibling;
			while ( o.nodeType != 1 ) {
				o = o.previousSibling;
			}
			cal.select( o, this.id, "MM/dd/yyyy" );
			return false;
		});
	});





// Validates the Forms and lets the user know which
// form fields they have problems with
	$(".validate").submit(function() {
		var counter = 0;
		var first_error = "";
		
		// find all elements w/field class name
		$(this).find(".field").each(function() { 
			// find all elements w/required class name
			$(this).find(".required").each(function() { 
				// if the element is not a label do it
				$.removeWarning($(this));
				if ( ($(this).is("input")) || ($(this).is("select")) ) {
					// This part is for input, select, and textareas

					var valid = true;
					var o = $(this).val();
	
					if ($(this).id() == "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 (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).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 it is checked do this
							if(testdis.attr("checked") == true){
								valid = true;
							}
						});
						if(!valid) { 
							$.addWarning($(this));
							counter++;
							if ( first_error == "" ) {
								first_error = $(this).id();
							}		
						}
					});					
				}
			});
		});
		if ( counter > 0 ) {
			alert( "One or more required fields were not correctly supplied. Please double-check the noted fields." );
			$.id(first_error).focus();
			return false;
		}		
	});


// Removes the Warning Icon
	jQuery.removeWarning = function(io) {
		// find img elements, if it's present, remove it
		var testwar = $(io).parent(".field").find("img").id();
		if (testwar != null){
			// this = first_name
			$(io).parent(".field").find("img").remove();
		}
	};
	
// Adds the Warning Icon
	jQuery.addWarning = function(inputObject) {
		var img = "<img />";
		// adds the source attribute
		img = $(img).attr("src","images/icon-warning.gif");
		// adds the validation error class
		img = $(img).attr("class","validation-error");	
		// Add the image after the input field
		//if inputObject is part of checkbox group put it after the first list item
		if( $(inputObject).is("ul") ) {
			$(inputObject).find("li:first").append(img);
		}
		// if not throw it at the end
		else {
			$(inputObject).after(img);
		}
		// if the focus hasn't been set, set focus to first problem area
		/*if ( first_error == "" ) {
			first_error = $(inputObject).id();
		}*/		
	};


// 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\" style=\"clear:both;\">");

		// 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).ancestors("div.package-wrapper").find("div.package-long").animate({
			height: 'show',
			opacity: 'show'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("Hide Details");
	}, function() {
		$(this).ancestors("div.package-wrapper").find("div.package-long").animate({
			height: 'hide',
			opacity: 'hide'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("View Details");
	});
});

/* Source : http://sixtwothree.org/blog/archives/2006/05/20/activateactivex-11/ */
function activateActiveX() {
	if ( !document.getElementsByTagName || !document.body.outerHTML || !document.compatMode ) return false;
	var elems = new Array( "object", "applet" );
	for ( i = 0, j = elems.length; i < j; i++ ) {
		var objects = document.getElementsByTagName(elems[i]);
		for ( k = 0, l = objects.length; k < l; k++ ) {
			var params = "";
			for ( m = 0, n = objects[k].childNodes.length; m < n; m++ ) {
				params += objects[k].childNodes[m].outerHTML;
			}
			objects[k].outerHTML = objects[k].outerHTML.replace( "</" + elems[i].toUpperCase() + ">", params + "</" + elems[i].toUpperCase() + ">" );
		}
	}
}

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};	 
