/*******************
	TOC

Basics
ApplyNowForm Check
Toggle Elements
Form Element Actions
Select Options
Stripes
*******************/


/*****
Basics
*****/

function getPassedObject(theItem)
	{
	if("undefined" != typeof(theItem))
		{
		if("object" == typeof(theItem))
			{
			return theItem;
			}
		else if(document.getElementById(theItem))
			{
			return document.getElementById(theItem);
			}
		}
	return false;
	}


/**** **** *****
ApplyNowForm Check
***** **** ****/

function checkApplyNowForm(form)
{
	var error = "";
	var return_value = true;
	var z;
	var tempzip = "";
	var firstchar = "";
	
	tempzip = form.zip_to_work_in.value;
	firstchar = tempzip.charAt(0);
	
	// IF CANADIAN ZIP BASED ON FIRST CHARACTER - JGIORDANO 05052005 - CINCO DE MAYO!!
	if ((/[A-Z]/.test(firstchar)) || (/[a-z]/.test(firstchar))) {
		// ADDED TO ALLOW 6 DIGIT CANADIAN POSTAL CODES - JGIORDANO 04282006
		tempzip = form.zip_to_work_in.value;
		if (6 == tempzip.length) {		
			form.zip_to_work_in.value = tempzip.substring(0,3) + " " + tempzip.substring(3,6);
		}
	
		// CHECKING THAT THIRD CHARACTER - JGIORDANO 04282006
		tempzip = form.zip_to_work_in.value;
		if ("-" == tempzip.charAt(3)) {
			form.zip_to_work_in.value = tempzip.substring(0,3) + " " + tempzip.substring(4,7);
		}
	}
	
	// CONVERTING TO UPPERCASE FOR CANADIAN - JGIORDANO 04282006
	form.zip_to_work_in.value = form.zip_to_work_in.value.toUpperCase();

	if ((5 != form.zip_to_work_in.value.length) && (7 != form.zip_to_work_in.value.length))
	{
		error = "Please enter a full Zip/Postal Code.";
		return_value = false;
	}
	else if ((5 == form.zip_to_work_in.value.length) && (/hello/.test("hello world")) && !(/\d\d\d\d\d/.test(form.zip_to_work_in.value)))
		{
			error = "Please enter a valid Zip Code.";
			return_value = false;
		}
	else if ((7 == form.zip_to_work_in.value.length) && (/hello/.test("hello world")) && (!/[A-Z]\d[A-Z] \d[A-Z]\d/.test(form.zip_to_work_in.value)))
		{
			error = "Please enter a valid Postal Code.";
			return_value = false;
		}

	if (!return_value)
	{
		if(error)
		{
			alert(error);
		}
	}

	return return_value;
}

/***** ********
Toggle Elements
****** *******/

function displayToggle(theID)
	{
	if(document.getElementById(theID))
		{
		document.getElementById(theID).style.display = (document.getElementById(theID).style.display == "none" ) ? "" : "none";
		return true;
		}
	return false;
	}
function displayOff(theID)
	{
	if(document.getElementById(theID) && ("none" != document.getElementById(theID).style.display))
		{
		if(document.getElementById(theID).style.display = "none")
			{
			return true;
			}
		}
	return false;
	}
function displayOn(theID)
	{
	if(document.getElementById(theID) && ("none" == document.getElementById(theID).style.display))
		{
		if(document.getElementById(theID).style.display = "")
			{
			return true;
			}
		}
	return false;
	}

function showHide(theID, which)
	{
	var e = document.getElementById(theID);
	if(e)
		{
		switch(which)
			{
			case "hide":
				if("visible" == e.style.visibility)
					{
					e.style.visibility = "hidden";
					}
				break;
			
			case "show":
				if("hidden" == e.style.visibility)
					{
					e.style.visibility = "visible";
					}
				break;
			
			default:
				if("visible" == e.style.visibility)
					{
					e.style.visibility = "hidden";
					}
				else if("hidden" == e.style.visibility)
					{
					e.style.visibility = "visible";
					}
				break;
			}
		}
	}



/*** ******* *******
Form Element Actions
**** ******* ******/

function blurField(theField, theDefaultText)
	{
	if("object" != typeof(theField))
		{
		theField = getPassedObject(theField);
		}
	if("object" == typeof(theField))
		{
		if("" == theField.value)
			{
			theField.value = theDefaultText;
			}
		if((theDefaultText == theField.value) && ("textinputGrey" != theField.className))
			{
			theField.className = "textinputGrey";
			}
		}
	}
function focusField(theField, theDefaultText)
	{
	if("object" != typeof(theField))
		{
		theField = getPassedObject(theField);
		}
	if("object" == typeof(theField))
		{
		if(theDefaultText == theField.value)
			{
			theField.value = "";
			}
		if((theDefaultText != theField.value) && ("textinput" != theField.className))
			{
			theField.className = "textinput";
			}
		}
	}
function checkPhoneForward(theField, theLimit, nextField)
	{
	msgText = "";
	if(theLimit == theField.value.length)
		{
		if (theLimit == 3 && !(/\d\d\d/.test(theField.value))) 
			{
			msgText = "Please enter a correct";
			if (theField.name == "phone_area_1")
				{
				msgText += " home area code";
				}
			if (theField.name == "phone_area_2")
				{
				msgText += " cell area code";
				}
			if (theField.name == "phone_area_3")
				{
				msgText += " work/other area code";
				}
			if (theField.name == "phone_exchange_1")
				{
				msgText += " home exchange";
				}
			if (theField.name == "phone_exchange_2")
				{
				msgText += " cell exchange";
				}
			if (theField.name == "phone_exchange_3")
				{
				msgText += " work/other exchange";
				}
			}
		else if (theLimit == 4 && !(/\d\d\d\d/.test(theField.value))) 
			{
			msgText = "Please enter a correct";
			if (theField.name == "phone_number_1")
				{
				msgText += " home phone number";
				}
			if (theField.name == "phone_number_2")
				{
				msgText += " cell phone number";
				}
			if (theField.name == "phone_number_3")
				{
				msgText += " work/other phone number";
				}
			}
		if (msgText != "") 
			{
			alert(msgText);
			theField.value = "";
			theField.focus();
			}
		else 
			{
			nextField.focus();
			}
		}
	}


function blurZip()
	{
	if(document.getElementById("applyNowForm") && ("" == document.getElementById("applyNowForm").value))
		{
		document.getElementById("applyNowForm").maxLength = 13;
		document.getElementById("applyNowForm").value = "Your Zip Code";
		document.getElementById("applyNowForm").className = "textinputGrey";
		}
	}
function focusZip()
	{
	if(document.getElementById("applyNowForm") && ("Your Zip Code" == document.getElementById("applyNowForm").value))
		{
		document.getElementById("applyNowForm").value = "";
		document.getElementById("applyNowForm").maxLength = 7;
		document.getElementById("applyNowForm").className = "textinput";
		}
	}
function pageBlurZip()
	{
	if(document.getElementById("applyNowForm") && (("Your Zip Code" == document.getElementById("applyNowForm").value) || ("" == document.getElementById("applyNowForm").value) || !document.getElementById("applyNowForm").value))
		{
		document.getElementById("applyNowForm").value = "";
		}
	blurZip();
	}



/***** *******
Select Options
****** ******/

function setSelectOptions(theSelect, theTexts, theValues, defaultValue, zeroText)
	{
	if(("object" == typeof(theValues)) && theValues.length)
		{
		theSelect.length = theValues.length + 1;
		theSelect.selectedIndex = 0;
		
		var i, x;
		for(i = 0; i < theValues.length; i++)
			{
			x = i + 1;
			if("object" == typeof(theValues[i]))
				{
				theSelect.options[x].value = theValues[i][0];
				theSelect.options[x].text = theTexts[i][1];
				if(theValues[i][0] == defaultValue)
					{
					theSelect.options[x].selected = true;
					}
				}
			else
				{
				theSelect.options[x].value = theValues[i];
				theSelect.options[x].text  = theTexts[i];
				if(theValues[i] == defaultValue)
					{
					theSelect.options[x].selected = true;
					}
				}
			}
		}
	else
		{
		theSelect.length = 1;
		theSelect.selectedIndex = 0;
		}
	
	theSelect.options[0].text = (zeroText) ? zeroText : "Select One";
	}

function updateSelect(primarySelect, secondarySelect, secondaryDefaultValue, secondaryValues, nullText)
	{
	var x;
	var primaryValue = primarySelect.options[primarySelect.selectedIndex].value;
	var zeroText;
	
	var secondaryValuesExist = (("undefined" == typeof(secondaryValues)) || ("object" != typeof(secondaryValues)) || !secondaryValues.length || ("undefined" == typeof(secondaryValues[primaryValue])) || ("object" != typeof(secondaryValues[primaryValue])) || !secondaryValues[primaryValue].length) ? false : true;
	
	if(0 == primarySelect.selectedIndex)
		{
		zeroText = nullText;
		}
	else if(!secondaryValuesExist)
		{
		zeroText = "None Available";
		}
	
	setSelectOptions(secondarySelect, ((secondaryValuesExist) ? secondaryValues[primaryValue] : false), secondaryDefaultValue, zeroText);
	}



/******
Stripes
******/

/* this function is needed to work around
   a bug in IE related to element attributes */
function hasClass(obj)
	{
	var result = false;
	if(obj.getAttributeNode("class") != null)
		{
		result = obj.getAttributeNode("class").value;
		}
	return result;
	}

function stripe(id)
	{
	var i, j, tbodies, trs, tds;
	var even = false;
	
	/* if arguments are provided to specify the colours
		of the even & odd rows, then use the them;
		otherwise use the following defaults: */
	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eef";
	
	if(table = document.getElementById(id))
		{
		/* by definition, tables can have more than one tbody
			element, so we'll have to get the list of child <tbody>s */
		tbodies = table.getElementsByTagName("tbody");
		for(var h = 0; h < tbodies.length; h++)
			{
			trs = tbodies[h].getElementsByTagName("tr");
			for(i = 0; i < trs.length; i++)
				{
				/* avoid rows with class attribute or backgroundColor style */
				if(!hasClass(trs[i]) && !trs[i].style.backgroundColor)
					{
					tds = trs[i].getElementsByTagName("td");
					for(j = 0; j < tds.length; j++)
						{
						/* avoid cells with class attribute or backgroundColor style */
						if (!hasClass(tds[j]) && !tds[j].style.backgroundColor)
							{
							tds[j].style.backgroundColor = (even) ? evenColor : oddColor;
							}
						}
					}
				even = (!even);
				}
			}
		}
	}