function simpleAutoTab(CurrentElementID, NextElementID, FieldLength) {
    //Get a reference to the two elements in the tab sequence.
    var CurrentElement = $('#' + CurrentElementID);
    var NextElement = $('#' + NextElementID);
 
    CurrentElement.keyup(function(e) {
        //Retrieve which key was pressed.
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
 
        //If the user has filled the textbox to the given length and 
        //the user just pressed a number or letter, then move the 
        //cursor to the next element in the tab sequence.    
        if (CurrentElement.val().length >= FieldLength
            && ((KeyID >= 48 && KeyID <= 90) || 
            (KeyID >= 96 && KeyID <= 105)))
            NextElement.focus();
    });
}

$(document).ready(function() {
	if($('.cha_form_lead') || $('.cha_form')) {
	    simpleAutoTab('phone1', 'phone2', 3);
    	simpleAutoTab('phone2', 'phone3', 3);
    	simpleAutoTab('phone3', 'phone4', 4);
	}


	// Member Directory Table row Hover
	
	$("#member-directory tbody tr").click( function(){
		link = $(this).find("td a").attr("href");
		window.location = link;											
	});

	
	// Creates Collapseable Categories
	
	$('.hospital-name, .qotm-archive dt').click(function() {														 
		$(this).toggleClass("selected").next().slideToggle('fast');
	if($(this).hasClass("ie7_class3"))$(this).removeClass("ie7_class3");
	if($(this).hasClass("hospital-name")){
		  if($(this).hasClass("selected")){
			$(this).css("font-weight","bold");
		  } else{
			$(this).css("font-weight","normal");
		  }
									
		}
  	});	
	$('.hospital-name, .qotm-archive dt').click();

	$('.month').prepend("::&nbsp;").append("&nbsp;:");
  	$('h2.instructions').append("<span>(Click a question to view the answer)</span>");
	$('h4.instructions').append("<span>(Click a hospital to view its details.)</span>");
  
  
  	// Form swaps class on input fields whcih changes the input color
	
	//Create an associative array to hold the default hint values
	//The  index is the ID of the element and the value is the hint
	
	var hintArr = new Array()
		hintArr["fname"] = "First Name";
		hintArr["lname"] = "Last Name";
		hintArr["doc-search"] = "Enter a Search Query...";
		hintArr["search-field"] = "Search...";
	
	$("input.initval").each(function(){
		id = $(this).attr("id");
		hint = hintArr[id];
		$(this).attr("value", hint);
												   
	});
	
	$('.initval').focus(function() {
		if($(this).hasClass("initval")){
			$(this).attr("value", "").removeClass("initval");
		}
	});
	
	$('.cha_form input, .cha_form_lead input, #doc-search input, #search-field').blur(function() {
		currentval = $(this).attr("value");		
		if( currentval == undefined || currentval == " "){
			id = $(this).attr("id");
			hint = hintArr[id];
			if( hint != '') {
				$(this).addClass("initval").attr("value", hint);
			}
		}
	});
	$(".cha_form h4, .cha_form_lead h4").hide();

	jQuery.validator.addMethod("defval", function(value, element) {
		if(value == "First Name"){
		 	return false;
		} else if( value == "Last Name"){
		 	return false;
		} else {
			return true;
		}
	}, "Required");

	$(".cha_form").validate({
	   groups: {
			username: "fname lname",
			phone: "phone1 phone2 phone3"
		  },
	   errorPlacement: function(error, element) {
		  if (element.name == "fname" || element.name == "lname" ) 
			   error.insertAfter("lname");
		  else if (element.name == "phone1" || element.name == "phone2" || element.name == "phone3")
		       error.insertAfter("phone4");
		  else
			   error.insertAfter(element);
		},
		rules: { 
            fname: {defval: true},
			lname: {defval: true},
			title: "required",
			org: "required",
			phone1: "required",
			phone2: "required",
			phone3: "required",
			email: {
				required: true,
				email: true
			}
        }, 
		messages: {
		   fname: "Required",
		   lname: "Required",
		   email: "Required",
		   title: "Required",
		   org: "Required",
		   phone1: "Required",
		   phone2: "Required",
		   phone3: "Required"
		},
		errorElement: "h4"
		
	});

$(".cha_form_lead").validate({
	   onClick: true,
	   groups: {
			username: "fname lname",
			phone: "phone1 phone2 phone3"
		  },
	   errorPlacement: function(error, element) {
		  if (element.name == "fname" || element.name == "lname" )
			   error.insertAfter("lname");
		  else if (element.name == "phone1" || element.name == "phone2" || element.name == "phone3")
		       error.insertAfter("phone4");
		  else if (element.name == "chamember")
		  	   error.insertAfter("chamemberradiolabel2");
		  else
			   error.insertAfter(element);
		},
		rules: { 
            fname: {defval: true},
			lname: {defval: true},
			title: "required",
			org: "required",
			phone1: "required",
			phone2: "required",
			phone3: "required",
			chamember: "required",
			email: {
       			required: true,
		        email: true
		     }
        }, 
		messages: {
		   fname: "Required",
		   lname: "Required",
     	   email: "Required",
		   title: "Required",
		   org: "Required",
		   phone1: "Required",
		   phone2: "Required",
		   phone3: "Required",
		   chamember: "Required"
		},
		errorElement: "h4"
		
	});	
	//Print an article
	$("a.print-icon").click(function(){
		window.print();
		return false;
	});
	
	// Add new window icon, opens link in new window.
	$("a[rel='external']").append("<img src='/images/external.png' />");
	$("a[rel='external']").click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

  
 });//EOF


