// JAVASCRIPT INCLUDE FILE

function regExEmail(eObj)
{//Uses regular expression for email check
var rePattern = /^[a-zA-Z0-9\-]+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3})$/;
 if(rePattern.test(eObj.value))
 {
 	return true;
 }else{
    alert("Please enter a valid email address");
	eObj.value = "";
	eObj.focus();
	return false;
 }
}

function regExZip(eObj) //OK for inc file
{ //uses regular expression to check zip and postal codes
var rePattern = /^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/;
  if(rePattern.test(eObj.value))
  {
	return true;
  }else{
	alert("Please enter a valid Zip or Postal Code");
	eObj.value="";
	eObj.focus();
	return false;
  }
}


/*
function regExPassword(eObj)
{ //uses regular expression to check for illegal chars in password
var rePattern = /^([a-zA-Z0-9]{4,9})$/;
  if(rePattern.test(eObj.value))
  {

	return true;

  }else{
	alert("Please use ONLY Alpha Numeric Passwords, 4-9 Characters");
	eObj.value="";
	eObj.focus();
	return false;
  }
}
*/

function processForm(thisForm)
{
	thisForm.myText.value = capFirst(thisForm.myText.value);  //capitalize & rem spaces
	return false;  //change to true to test submitted version
}

function capFirst(str)
{//accepts a string, strips spaces, capitalizes first letter of each word
	str = stripSpaces(str); //get rid of 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;
}

function stripSpaces(str)
{
	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;
}

function checkReq(thisForm,checkChar){ //this was used inside form, can be passed to function
for(x=1; 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, submit form
}

function processName(str)
{//Removes checkChar, adds space before capitalized words in field name
	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, finished!
	}while(myPos > -1);
	newStr += " " + prevChar + str; //add remaining bit to end
	return newStr;
}