// ****************************************** Universal Form Validator INC File **************************************************

// WEB 150 [Assignment 3]
// David Fraga
// February 21, 2006



//accepts a string, strips spaces, capitalizes first letter of each word; [requires stripSpaces()] 
function capFirst(str)
{
//alert("You are inside capFirst()");
	str = stripSpaces(str); //get rid o' extra spaces!
	var myStart = "";
	var myEnd = "";
	var nameStr = ""; //to store name
	capArray = str.split(" ");
	for (x=0; x<capArray.length;x++)
	{
		myStart = capArray[x].substr(0,1); //grab first char
		myEnd = capArray[x].substr(1,capArray[x].length - 1); //grab rest
		myStart	= myStart.toUpperCase();  //capitalize first letter
		myEnd = myEnd.toLowerCase();  //lowercase the rest
		if(nameStr == "")
		{
			nameStr += myStart + myEnd;
		}else{
			nameStr += " " + myStart + myEnd;  //add back ONE space between words!!
		}
	}
	return nameStr;
}
// -------------------------------------------------------------------------------------------------------------------



// strips excess spaces from beginning and end of string; also removes excess space between words inside string
function stripSpaces(str)
{
//alert("You are inside stripSpaces()");
	myMarker = "";
	for(x=0;x<str.length;x++)
	{
		myMarker = str.substr(0,1);
		if(myMarker == " ")
		{
			str = str.substr(1,str.length - 1);// grab rest
		}else{
			break; //exit loop if no more beginning spaces
		}	
	}
	for(x=0;x<str.length;x++)
	{
		myMarker = str.slice(str.length-1,str.length);
		if(myMarker == " ")
		{
			str = str.slice(0,str.length - 1);// grab rest
		}else{
			break; //exit loop if no more beginning spaces
		}
	}
	var blnSpace = false;
	for(x=0;x<str.length;x++)
	{
		myMarker = str.substr(x,1); //one char at a time
		if(myMarker == " ")
		{
			if (blnSpace == true) //2 zeros, remove 1!
			{
			str = str.slice(0,x) + str.slice(x+1,str.length);//concat both ends around space
			x--  //adjust for removal!
			}else{
				blnSpace = true;
			}
		}else{
			blnSpace = false;
		}
			
	}
	return str;
}
// ---------------------------------------------------------------------------------------------------------------------------


// checks required form fields for data; [requires processName()]
function checkReq(thisForm,checkChar){ 

for(x=0; x<thisForm.length; x++)
{
var thisElement = thisForm.elements[x];
var myCheck = 0;
	if(thisElement.name.charAt(0) == checkChar)
	{//first char == checkChar means required element!
		var tempName = "";  //stores removed char version
		if(thisElement.type == "select-one" && thisElement.selectedIndex==0)
		{//single select opt	
			tempName = processName(thisElement.name); //temp name, rem x, add spaces
			alert("Please select one: " + tempName);	
			thisElement.focus();
			return false;
		}else{
			
			switch(thisElement.type)
			{
				case "select-multiple":
					for(y=0;y<thisElement.options.length; y++)
					{
						if(thisElement.options[y].selected)
						{
							myCheck = 1;
						}
						if(myCheck == 0)
						{
							tempName = processName(thisElement.name);
							alert("Please make selections: " + tempName);	
							thisElement.focus();
							return false;
						}
					}//end mult for
					break;
				case "radio":
				case "checkbox":
					var myLength = 1; //start at one
					var myStart = x;  //will change x
					while(thisElement.name == thisForm.elements[x + 1].name)
					{//calc number of radio/checkboxes
						myLength++;
						x++
					}
					for(y=myStart;(y<myStart + myLength);y++)
					{
						if(thisForm.elements[y].checked == true)//add x to offset
						{
							myCheck++;
						}
					}//end for
					if(myCheck == 0)
					{
						tempName = processName(thisElement.name);
						alert("Please check one: " + tempName);	
						thisForm.elements[myStart].focus(); //focus back to first element
						return false;
					}
					break;
				case "text":
				case "textarea":
				case "password":
					if(thisElement.value == "")
					{	
						tempName = processName(thisElement.name);
						alert("Please enter required field: " + tempName);	
						thisElement.focus();
						return false;
					}
					break;
			}//end switch
		}//end select-one check		
	}//end of charAt()
}//end of for
return true; //if passed all checks, return true
}
// -----------------------------------------------------------------------------------------------------------------------------


//Removes checkChar, adds space before capitalized words in field name
function processName(str)
{
//alert("You are inside processName()");
	var newStr = "";
	var leftStr = "";
	var myChar = "";
	var myPos = "";
	var prevChar = "";
	var myCounter = 0;
	var rePattern = /\B[A-Z]\B/; //any capital letter \B is not on a word boundary
	str = str.substring(1) //remove first char
	do
	{
		myChar = str.match(rePattern);//find first RegEx pattern match
		if(myChar == null && myCounter == 0){return str;} //no match, abort!
		myPos = str.indexOf(myChar); //find first char of pattern
		leftStr = str.substring(0,myPos); //grab left str
		str = str.substring(myPos + 1) //right side of str
		if(myChar != null)
		{
			if(newStr == "") //start of new string
			{
				newStr = leftStr; //add left side of string
				prevChar = myChar;  //store old char for next round
			}else{
				newStr += " " + prevChar + leftStr; //prev. stored char, + left side
				prevChar = myChar;
			}
		}
		myCounter++;
		if(myChar == null){myPos = -1;} //no more chars to search for, finis!
	}while(myPos > -1);
	newStr += " " + prevChar + str; //add remaining bit to end
	return newStr;
}
// --------------------------------------------------------------------------------------------------------------------------



// validates email only if one was entered
function optEmail(eObj)  
{//Uses regular expression for email check
//alert("You are inside optEmail()");
	if (eObj.value != "")
	{	
		var rePattern = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if (rePattern.test(eObj.value))
		{
			return true;
		}
		else
		{
    		alert("Please enter a valid email address");
			eObj.value = "";
			eObj.focus();
			return false;
		}
	}
	/*else if (eObj.value == "")
	{
		return false; // change to true if email is optional which was the original purpose of this function
	}*/
}
// --------------------------------------------------------------------------------------------------------------------------


//Uses regular expression for zip code check
function regExZip(eObj)
{
//alert("You are inside regExZip()");
var rePattern = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 if(rePattern.test(eObj.value))
 {
 	return true;
 }else{
    alert("Please enter a valid Zip Code");
	eObj.value = "";
	eObj.focus();
	return false;
 }
}
// --------------------------------------------------------------------------------------------------------------------------


//Uses regular expression for soc sec num check
function regExSoc(eObj)
{
//alert("You are inside regExSoc()");
var rePattern = /^(\d{9}|\d{3}-\d{2}-\d{4})$/;
 if(rePattern.test(eObj.value))
 {
 	//return true;  //will return for submittal
	alert("Valid Soc: " + eObj.value); //testing only
	return false; //testing only
 }else{
    alert("Please enter a valid social security number.");
	eObj.value = "";
	eObj.focus();
	return false;
 }
}
// --------------------------------------------------------------------------------------------------------------------------

// Uses regular expression for phone number check in local or INT formats
function regExPhone(eObj)
{
//alert("You are inside regExPhone()");
var rePattern = /^[\(]?(\d{3})[\)]?[ -\.]?(\d{3})[ -\.]?(\d{4})$/;
 if(rePattern.test(eObj.value))
 {
 	return true;
 }else{
    alert("Please enter a valid phone number");
	eObj.value = "";
	eObj.focus();
	return false;
 }
}
//-----------------------------------------------------------------------------------------------------------------------------

// creates check/unchech all toggle checkbox
function toggle(thisForm, myCheck){
	for(var x=0; x<myCheck.length; x++){
		myCheck[x].checked = thisForm.checkAll.checked;
	}	
}
// --------------------------------------------------------------------------------------------------------------------------



//Uses regular expression for code name (password) check
function regExPass(eObj)
{//alert("You are inside regExPass()");
var rePattern = /^[a-zA-Z0-9]+$/;
 if(rePattern.test(eObj.value))
 {
 	return true;
 }else{
    alert("Please enter a valid Code Name");
	eObj.value = "";
	eObj.focus();
	return false;
 }
}
// --------------------------------------------------------------------------------------------------------------------------


// creates character maxlength alert for textarea field 
function tooLong(eObj) 
	{
    if (eObj.value.length > 50)
		{
    	alert('You\'ve reached your limit of 50 characters');
		eObj.value = eObj.value.substr(0,50);
    	eObj.focus();
    	}
    return true;
	}
// --------------------------------------------------------------------------------------------------------------------------

function tooLongSubmit(eObj) 
	{
    if (eObj.value.length > 50)
		{
    	alert('You\'ve reached your limit of 50 characters');
		eObj.value = eObj.value.substr(0,50);
    	eObj.focus();
   	    return false;
		} 
	else
		{
		return true;
		}
	}

function Highlight(Element)
         {
            Element.style.color = '#000000'
            Element.style.backgroundColor = '#f0f8ff'
			Element.style.fontWeight = 'bold'
         }

function Lowlight(Element)
         {
            Element.style.color = '#000000'
            Element.style.backgroundColor = '#ffffff'
			Element.style.fontWeight = 'normal'
         }

function Validate(EmailAddress)
         {		 
            var Location1 = EmailAddress.indexOf('@')
			var Location2 = EmailAddress.indexOf('.')
            if (Location1 == -1 || Location2 == -1)
            {
               alert('You entered an inaccurate email address.  Please try again.');
               document.forms.signup.email.focus();
			   document.forms.signup.email.value="";
			}
			
			
         }
		 
// Phone validator-- validates phone number if user chooses to enter one
function regExOptPhone(eObj)  
{//Uses regular expression for email check
	if (eObj.value != "")
	{	
		var rePattern = /^[\(]?(\d{3})[\)]?[ -\.]?(\d{3})[ -\.]?(\d{4})$/;
		if (rePattern.test(eObj.value))
		{
			return true;
		}
		else
		{
    		alert("Please enter a valid phone number if you choose to enter one");
			eObj.value = "";
			eObj.focus();
			return false;
		}
	}
	else if (eObj.value == "")
	{
		return true;
	}
}
//-----------------------------------------------------------------------------------
//Breadcumb script by Bill Newman------------------------------------------------
var myQuestion = location.href.lastIndexOf("?"); //grab querystring
if(myQuestion > -1)
{//strip off querystring
	//alert('querystring found');
  myLocation = location.href.substring(0,myQuestion);
}else{//no querystring
//alert('no querystring found');
  myLocation = location.href;
}

var myStart = myLocation.lastIndexOf("/"); //grab last slash
var myEnd = myLocation.length;  //find length
var thisPage = myLocation.substring(myStart+1,myEnd); 

var pageArray = new Array();
var crumbArray = new Array(); 

pageArray[0] = 'guts1.html'; 
pageArray[1] = 'history.html'; 
pageArray[2] = 'arch.html';
pageArray[3] = 'landscape.html';
pageArray[4] = 'gardenhistory.html'; 
pageArray[5] = 'groundsmap.html'; 
pageArray[6] = 'childprograms.html';
pageArray[7] = 'childparties.html';
pageArray[8] = 'historycamp.html';
pageArray[9] = 'rentals.html';
pageArray[10] = 'backintime.html';
pageArray[11] = 'childarch.html';
pageArray[12] = 'childxprogram.html';
pageArray[13] = 'scoutprogram.html';
pageArray[14] = 'readwriteletter.html';
pageArray[15] = 'dayinlife.html';
pageArray[16] = 'outreach.html';
pageArray[17] = 'membership.html';
pageArray[18] = 'volunteer.html';
pageArray[19] = 'calendar.html';
pageArray[20] = 'craftshow.html';
pageArray[21] = 'weddingpics.html';
pageArray[22] = 'tours.html';
pageArray[23] = 'signup.html';
pageArray[24] = 'links.html';
pageArray[25] = 'gardenrestore.html';
pageArray[26] = 'greenhouse.html';
pageArray[27] = 'newsigns.html';
pageArray[28] = 'gardenrestore_sub.html';
pageArray[29] = 'grouptours.html';
pageArray[30] = 'rughooking.html';
pageArray[31] = 'sinkler.html';
pageArray[32] = 'hunt.html';
pageArray[33] = 'rentals.html';

crumbArray[0] = ''; 
crumbArray[1] = '';
crumbArray[2] = '';
crumbArray[3] = '';
crumbArray[4] = 'Landscape >> Garden History'; 
crumbArray[5] = '';
crumbArray[6] = '';
crumbArray[7] = 'Children\'s programs >> Children\'s Birthday Parties';
crumbArray[8] = 'Children\'s programs >> History Camp';
crumbArray[9] = '';
crumbArray[10] = 'Children\'s programs >> A Step Back In Time';
crumbArray[11] = 'Children\'s programs >> Architecture';
crumbArray[12] = 'Children\'s programs >> Extended Collaborative Program';
crumbArray[13] = 'Children\'s programs >> Scout Programs';
crumbArray[14] = 'Children\'s programs >> Read a Letter, Write a Letter Grade 4 - 5';
crumbArray[15] = 'Children\'s programs >> A Day in the Life...';
crumbArray[16] = 'Children\'s programs >> Outreach Programs';
crumbArray[17] = '';
crumbArray[18] = 'HHS Membership >> Volunteer';
crumbArray[19] = '';
crumbArray[20] = '';
crumbArray[21] = 'Rentals >> More Wedding Pics';
crumbArray[22] = '';
crumbArray[23] = '';
crumbArray[24] = '';
crumbArray[25] = '';
crumbArray[26] = 'Garden Restoration >> c. 1920 Greenhouse Restoration';
crumbArray[27] = 'Garden Restoration >> New Signs at The Highlands';
crumbArray[28] = 'Garden Restoration >> Garden Restoration';
crumbArray[29] = '';
crumbArray[30] = '';
crumbArray[31] = 'Home >> Sinkler Garden Restoration';
crumbArray[32] = 'Calendar of Events >> Hunt Breakfast';
crumbArray[33] = 'Children\'s programs >> Chambers >> Rentals';


foundMatch = 0; //if one, write breadcrumb
var myBreadcrumb = '<div align="left"><font color="#cccccc" size="-1"><b>';
for(var x = 0; x < pageArray.length; x++)
{
	if(thisPage==pageArray[x])
	{
		//alert('pageArray match found!');
		myBreadcrumb += crumbArray[x] + '</b></font></div>';
		foundMatch = 1;  //write breadcrumb
		break;
		//alert('End of for-loop here');
	}
} 
if(foundMatch==0){myBreadcrumb = "";} //no match,erase erase
//alert('foundMatch variable = ' + foundMatch);
//alert('URL = ' + location.href);

//---------------------------------------------------------------------------------------------