jQuery(document).ready(function(){
	
		// Replace HRs with Divs in IE
		// This is necessary becasue IE doesn't let you style the HR element, and we use it extensively on the site.
	  if (jQuery.browser.msie) {
			jQuery("hr").each(function() {
				var hrClass = jQuery(this).attr("class")
				jQuery(this).wrap("<div class='hr " + hrClass +"'></div>");
			});
		}
		
		// Wrap Callout with an inner div
		// This is necessary to create the graphics on the top and bottom of the callout
		jQuery(".callout-center").each(function() {
			jQuery(this).wrapInner("<div class='inner'></div>");
		});
		
		// Add "last" to the last column in the content
		// This removed the right margin on the third or second column
		jQuery(".column-third + .column-third + .column-third").addClass("column-last");
		jQuery(".column-half + .column-half").addClass("column-last");

		// Activate the drop-down nav in the header
		jQuery("#nav-primary > li").each(function(){
		  var curWidth = Math.ceil(jQuery(this).width() + 1);
			$(this).width(curWidth);
			jQuery(this).find("ul").css("width",curWidth);

		});
		jQuery(".nav-primary-sub").parent().addClass("menu");
		jQuery("li.menu").hover(
      function () {
        jQuery(this).children("ul").show();
				jQuery(this).addClass("hover");
      }, 
      function () {
        jQuery(this).children("ul").hide();
				jQuery(this).removeClass("hover");
      }
    );

		jQuery("#login_form, #card_number_search").each(function(){
			var parentForm = jQuery(this);
			$(this).find("input[type='submit']").click(function(){
				if(validateForm(parentForm) == false){
					return false;
				};
			});
		});
	
});

function validateForm(parentForm){
	var fieldNumber = jQuery(parentForm).find("input[type='text'], input[type='password']").each(function(){
		validateField($(this));
	});
	if (window.isInvalid == "invalid"){
	  window.isInvalid = "undetermined";
	  return false;
	};
}

function validateField(field){
	if ($(field).is("#card_number")){
		var shortString =	$(field).val().replace(/[^0-9]/g,'');
		$(field).val(shortString);
		if (shortString.length != 16){
			fieldError(field);
		} else {
			$(field).removeClass("error");
		};
	} else if ($(field).val() == '') {
		fieldError(field);
	  window.isInvalid = "invalid";
	} else {
		$(field).removeClass("errorField");
	}
	
}

function fieldError(field){
	$(field).addClass("errorField");
	$(field).parents("form").addClass("errorForm");
  window.isInvalid = "invalid";
}





