﻿// UTILITY

// Get style object for element if possible
function getStyleObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId).style;
  } else {
    return false;
  }
}

// Change display style of division
function changeDivDisplay(the_div,the_change)
{
  var the_style = getStyleObject(the_div);
  if (the_style != false)
  {
    the_style.display = the_change;
  }
}

function getRadioGroupValue(element)
{
for (var i = 0; i < element.length; i++)
   {
   if (element[i].checked)
      {
      	return element[i].value;
      }
   }
   
   return '';
}

// Returns true if the element has the specified value present. Works with checkboxes, selections, etc
function selectionHasValue(searchElement, searchValue)
{
	var elementType = searchElement.type;
	
	if ( elementType == undefined )
	{
		// Is searchElement an array of elements?
		elementType = searchElement[0].type;		
	}

	if ( elementType == 'checkbox' )
	{
		for ( var i = 0; i < searchElement.length; i++ )
		{
			if ( searchElement[i].checked )
			{
				if ( searchElement[i].value == searchValue ) { return true };
			}
		}
	}
	else if ( elementType == 'select-one' )
	{
		for ( var i = 0; i < searchElement.length; i++ )
		{
		
			if ( searchElement.options[i].selected )
			{
				if ( searchElement.options[i].value == searchValue ) { return true };
			}
		}
	}
	else if ( elementType == 'radio' )
	{
		if ( getRadioGroupValue(searchElement) == searchValue ) { return true };
	}
	else
	{
		alert(elementType);
	}
	
	
	return false ;
}

// VALIDATION

/* -- Form Related JavaScript Library */

/* Exmple of  use these function:

-- This is usally in the form itself
function validateForm (form)
{
	requiredText = new Array ("firstName");
	requiredTextName = new Array ("First Name");
	
	return checkRequiredText (form, requiredText);
	
	-- include addtional functions here.
	return checkRequiredText (form, requireText, requireTextNames) 
}

-- Form handler
<form method="get" onsubmit="validateForm(this);">

*/

// Use this as a hash to track those elements validated on a per element basis
// that have formatting problems
validate = new Object () ;

// Takes a form and an aray of form element names; verifies that each has a value
function checkRequiredText ( form, requiredField, requiredFieldTitles )
{

	for ( var i = 0; i < requiredField.length; i++ )
	{
		var elementName = requiredField[i];
		var elementValue;
		var elementType = form[elementName].type;
		
		if ( elementType == undefined )
		{
			elementValue = getRadioGroupValue(form[elementName]);
		}
		else if ( elementType == 'select-one' )
		{
			elementValue = form[elementName].options[form[elementName].selectedIndex].value;
		}
		else
		{
			elementValue = form[elementName].value;
		}
		
		if ( elementValue == '' || elementValue == null )
		{
			alert("Please enter a value for " + requiredFieldTitles[i] + ".");
			if ( elementType != undefined ) { form[elementName].focus() } ;
			return false;
		}
	}
	
	return true;
}

// Takes an element and makes sure it is a valid ZIP (or ZIP+)
function checkZIPCode ( element )
{
	var pattern = /^\d{5}-?(\d{4})?$/;
	
	if ( !pattern.test(element.value) )
	{
		alert("Please specify a valid ZIP code");
		element.focus();
		element.select();
		return false;
	}
	
	return true;
}

// Takes an element and makes sure it is a valid integer
function checkInteger ( element )
{
	var pattern = /^\d+$/ ;

	if ( !pattern.test(element.value) )
	{
		alert("Please specify a valid number");
		element.focus();
		element.select();
		return false;
	}
	
	return true;
}

// Takes an element and length and verifies the element value has correct length
function checkLength ( element, minLength, maxLength )
{

	if ( maxLength == minLength && element.value.length != minLength)
	{
		alert("FIeld requires at least " + minLength + " characters.");
		element.focus();
		element.select();
		return false;
	} 


	if ( ! (element.value.length >= minLength) )
	{
		alert("FIeld requires at least " + minLength + " characters.");
		element.focus();
		element.select();
		return false;
	} 

	if ( ! (element.value.length <= maxLength) )
	{
		alert("FIeld requires no more than " + maxLength + " characters.");
		element.focus();
		element.select();
		return false;
	} 
	
	return true;
}

// Takes an element validates as real email address
function checkEmailAddress ( element )
{
	var pattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
	
	if ( !pattern.test(element.value) )
	{
		alert("Please specify a valid email address.");
		element.focus();
		element.select();
		return false;
	}
	
	return true;
}

function checkDateCCYYMMDD ( element )
{
	var pattern = /^[12]\d{3}\/[01]\d\/[0123]\d$/ ;
	
	if ( element.value && !pattern.test(element.value) )
	{
		alert("Please specifiy a valid date in CCYY/MM/DD format.");
		element.focus();
		element.select();
		return false;
	}
	
	return true;
}

// USER INTERACE ENHANCEMENTS

// Takes element, max length, and "next field" element
// Us on onkeyup="forwardOnLength(element, 3, document.application.phonePrefix);"
function forwardOnLength ( element, maxLength, next )
{
	if ( element.value.length == 3 )
	{
		next.focus();
	}

}

