//Counts all the required fields on a page

function getRequired() {
	var lis = document.getElementsByTagName("li");
 	requiredCount = 0;
 	var required = new Array();	
	for(num = 0; num < lis.length; num++)
	{
		if(lis[num].className == 'required' || lis[num].className == 'kindagood' || lis[num].className == 'welldone')
		{
			var children = lis[num].getElementsByTagName('input')
			if(children.length > 0)
			{
				if(lis[num].className == 'welldone')
				{
					required[children[0].name] = true;				
				}
				else
				{
					required[children[0].name] = false;
				}
				required.push(children[0].name);
			}
			var children = lis[num].getElementsByTagName('textarea')
			if(children.length > 0)
			{		
				if(lis[num].className == 'welldone')
				{
					required[children[0].name] = true;
				}
				else
				{
					required[children[0].name] = false;
				}
				required.push(children[0].name);
			}
			requiredCount++;
		}
	}
	window.requiredFields = required;
	check_valid_fields();
}

//Checks if all required fields are validated.

function check_valid_fields()
{
	var required = window.requiredFields;
	if(required.length > 0)
	{
		var count = 0;
		for(i = 0; i < required.length; i++)
		{
			if(!required[required[i]])
			{
				count++;
			}
		}
		window.requiredFields = required;
				
		if(count == 0)
		{
			show_submit();
		}
		else
		{
			hide_submit();		
		}
	}
}

// This function grabs the content 
// from a textarea that has been 
// converted from TinyMCE and sends it 
// to see if it has something in it.

function formValidation(ed) {
	ed.onKeyUp.add(function(ed, e) {
			var content = tinyMCE.get(ed.editorId).getContent();
			var textarea = document.getElementById(ed.editorId);
			checkTaIsEmpty(content, textarea);
		});
}


// This function checks if the field
// has something in it.
// If so, it attaches class="welldone" to the 
// containing fieldset.

function checkIsEmpty(whatYouTyped) {
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 0) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;
	}
	else 
	{
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;
	}
		check_valid_fields();	
	return requiredFields[whatYouTyped.name];
}

// This function checks if the textarea
// from TinyMCE has something in it.
// If so, it attaches class="welldone" to the 
// containing fieldset.

function checkTaIsEmpty(whatYouTyped, textarea) {
	var li = textarea.parentNode;
	if (whatYouTyped.length > 0) {
		li.className = "welldone";
		requiredFields[textarea.name] = true;
	}
	else
	{
		li.className = "kindagood";
		requiredFields[textarea.name] = false;
	}
	check_valid_fields();	
}


// This function checks if the radio 
// is checked.
// If so, it attaches class="welldone" to the 
// containing fieldset.

function checkIsChecked(whatYouTyped) {
	var li = whatYouTyped.parentNode;
	
	if(whatYouTyped.checked)
	{
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;
	}
	else
	{
		li.className = "kindagood";	
		requiredFields[whatYouTyped.name] = false;
	}
	check_valid_fields();	
}

// This function checks if the field
// is at least 6 characters long.
// If so, it attaches class="welldone" to the 
// containing fieldset.

function checkForLength(whatYouTyped) {
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 5) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;		
	}
	else {
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;		
	}
	check_valid_fields();		
}

// If the password is at least 4 characters long, the containing 
// fieldset is assigned class="kindagood".
// If it's at least 8 characters long, the containing
// fieldset is assigned class="welldone", to give the user
// the indication that they've selected a harder-to-crack
// password.

function checkPassword(whatYouTyped) {
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 0 && txt.length < 8) {
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;		
	} else if (txt.length > 7) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;		
	} else {
		li.className = "required";
		requiredFields[whatYouTyped.name] = false;		
	}
	check_valid_fields();		
}

//Compares the confirm password field with the original password field.

function comparePassword(whatYouTyped,id) {

//	var pw = document.getElementById(id);
	var pw = $('#'+id).attr('value');
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 0 && txt.length < 8) {
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;		
	} else if (txt.length > 7 && txt == pw) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;		
	} else {
		li.className = "required";
		requiredFields[whatYouTyped.name] = false;
	}

	check_valid_fields();	
	return requiredFields[whatYouTyped.name];
}

// This function checks the email address to be sure
// it follows a certain pattern:
// blah@blah.blah
// If so, it assigns class="welldone" to the containing
// fieldset.

function checkEmail(whatYouTyped) {
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;		
	} else {
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;		
	}
	check_valid_fields();	
}

// This function checks the security code
// to make sure it matches
// If so, it assigns class="welldone" to the containing
// fieldset.

function checkSecCode(whatYouTyped, secCode) {
	var li = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.toUpperCase() == secCode) {
		li.className = "welldone";
		requiredFields[whatYouTyped.name] = true;		
	} else {
		li.className = "kindagood";
		requiredFields[whatYouTyped.name] = false;		
	}
	check_valid_fields();	

}

// this part is for the form field hints to display
// only on the condition that the text input has focus.
// otherwise, it stays hidden.

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints()
{
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++)
	{
		inputs[i].onfocus = function () {
			$(this).parents('li:first').find('span.hint').css('visibility', 'visible');
		}

		inputs[i].onblur = function () {
			$(this).parents('li:first').find('span.hint').css('visibility', 'hidden');
		}
	}
}

