



//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;
}
// --------------------------------------------------------------------------------------------------------------------------




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 (or remove this else/if clause all together) 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 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 = 'bold'
         }

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 (with area code) if you choose to submit one.");
			eObj.value = "";
			eObj.focus();
			return false;
		}
	}
	else if (eObj.value == "")
	{
		return true;
	}
}


//---------------------------------------------------------------------------------------------