/*

roll() handles the image rollovers.

*/

function roll(node,status)
{ nodeImg = node.firstChild.firstChild
  srcImage = nodeImg.getAttribute("src")
  
  if (status)
  { navState = srcImage.substring(srcImage.length - 7,srcImage.length - 4)
    if (navState == "off")
    { srcImage = srcImage.substring(0,srcImage.length - 7)
      srcImage = srcImage + "on.gif"
      nodeImg.setAttribute("src",srcImage)
    }
  }
  else
  {  navState = srcImage.substring(srcImage.length - 7,srcImage.length - 4)
    if ((navState != "sel") && (navState != "off"))
    {  srcImage = srcImage.substring(0,srcImage.length - 6)
      srcImage = srcImage + "off.gif"
      nodeImg.setAttribute("src",srcImage)
    }
  }
}




/*

these functions begin and maintain the automatic cycling of the slideshow.

*/

var currentSlide = 1
var previousSlide = 3
var slideFading = false
var opcty = 0

function nextSlide()
{ slideFading = true
  previousSlide = currentSlide

  currentSlide++
  if (currentSlide == 4)
  { currentSlide = 1
  }

  slideUpdate()
}




/*

slideRoll() and slideUpdate() handles the slideshow functionality and image swapping.

*/

function slideRoll(which)
{ if (slideFading)
  { return
  }
  if (which == currentSlide)
  { return
  }

  slideFading = true
    
  previousSlide = currentSlide
  currentSlide = which

  slideUpdate()
}

function slideUpdate()
{ setOpacity("slide" + currentSlide,0)
  document.getElementById("slide" + currentSlide).style.visibility = "visible"
  
  for(i=1;i<=3;i++)
  { if (i == currentSlide)
    { document.getElementById("option" + i + "off").style.visibility = "hidden"
      document.getElementById("option" + i + "on").style.visibility = "visible"
    }
    else
    { document.getElementById("option" + i + "off").style.visibility = "visible"
      document.getElementById("option" + i + "on").style.visibility = "hidden"
    }
  }
    
  opcty = 0
  fadeSlides()
}

function fadeSlides()
{ opcty = opcty + 10
  setOpacity("slide" + currentSlide,opcty)
  setOpacity("slide" + previousSlide,(100 - opcty))
  if (opcty < 100)
  { setTimeout("fadeSlides()",30)
  }
  else
  { document.getElementById("slide" + previousSlide).style.visibility = "hidden"
    slideFading = false
  }
}



function setOpacity(layr,num)
{	obj = document.getElementById(layr).style
	obj.MozOpacity = (num/100)
	obj.opacity = (num/100)
	obj.filter = "alpha(opacity=" + num + ")"
}

function getNumberOfPages(theListCtrlID)
{
	var myNumberOfPages = 0;
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null && myItemList.length > 0)
		{
			var myItem = null;
			myItem = myItemList[myItemList.length-1];
			myNumberOfPages = myItem.id;
		}
	}
	return myNumberOfPages;
}

function getDisplayedPageIndex(theListCtrlID)
{
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var myItem = null;
			for (var i = 0; i < myItemList.length; i++)
			{
				myItem = myItemList[i];
				if ( myItem.style.display == "" )
					return myItem.id;
			}
		}
	}
	return -1;
}

function Paginate(thePageIndex, theListCtrlID, thePaginationCtrlID)
{
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var displayedPageIndex = getDisplayedPageIndex(theListCtrlID);
			if (thePageIndex == 0)
			{
				thePageIndex = displayedPageIndex-1;
			}
			if (thePageIndex-1 == getNumberOfPages(theListCtrlID))
			{
				thePageIndex = displayedPageIndex;
				thePageIndex++;
			}

			var myItem = null;
			for (var i = 0; i < myItemList.length; i++)
			{
				myItem = myItemList[i];
				if (myItem.id == thePageIndex)
				{
					myItem.style.display = "";
				}
				else
				{
					myItem.style.display = "none";
				}
			}
		}
	}
	var myPaginationCtrl = document.getElementById(thePaginationCtrlID);
	if (myPaginationCtrl != null)
	{
		var myPageList = myPaginationCtrl.getElementsByTagName("li");
		if (myPageList != null)
		{
			var myPage = null;
			var myPrevPage = myPageList[0];
			var myNextPage = myPageList[myPageList.length-1];
			for (var j = 0; j < myPageList.length; j++)
			{
				myPage = myPageList[j];
				if ( myPage.id == thePageIndex )
				{
					myPage.className="selected";
//					myPage.setAttribute("classname", "selected");
				}
				else
				{
					myPage.className="";
//					myPage.setAttribute("classname", "");
				}
			}
			if (thePageIndex <= 1)
			{
				myPrevPage.style.display = "none";
			}
			else
			{
				myPrevPage.style.display = "";
			}
			if (thePageIndex >= myPageList.length-2)
			{
				myNextPage.style.display = "none";
			}
			else
			{
				myNextPage.style.display = "";
			}
		}
	}
}

function FilterList_ItemCtrl(theListCtrlID, theItemID)
{
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var myItem = null;
			for (var i = 0; i < myItemList.length; i++)
			{
				myItem = myItemList[i];
				if (myItem.id == theItemID || theItemID == '')
				{
					myItem.style.display = "";
				}
				else
				{
					myItem.style.display = "none";
				}
			}
		}
	}
}

function FilterList_SelectCtrl(theListCtrlID, theSelectCtrlID)
{
	var mySelectCtrl = document.getElementById(theSelectCtrlID);
	if (mySelectCtrl != null)
	{
		var mySelectedItem = mySelectCtrl.options[mySelectCtrl.selectedIndex];
		if (mySelectedItem != null)
		{
			FilterList_ItemCtrl(theListCtrlID, mySelectedItem.value);
		}
	}
}

function showCtrl(theID, status)
{
	var myCtrl = document.getElementById(theCtrlID);
	if (myCtrl != null)
	{
		if (status)
		{
			myCtrl.style.display = "block";
		}
		else
		{
			myCtrl.style.display = "none";
		}
	}
}

function showCtrl(theCtrlID)
{
	var myCtrl = document.getElementById(theCtrlID);
	if (myCtrl != null)
	{
		if (myCtrl.style.display == "none")
			myCtrl.style.display = "block";
		else
			myCtrl.style.display = "none";

	}
}

function openEmail(path)
{
	var url = window.location;
	path += url	
	window.location.href = (path);
}
/*

handles the Our Services landing page.

*/

function rollService(obj)
{ yy = document.getElementById("servicesNav").getElementsByTagName("li")
  zz = document.getElementById("servicesNav").getElementsByTagName("li").length
  
  for (i=0;i<zz;i++)
  { if (obj == yy[i])
    { document.getElementById("servicesContent" + i).style.display = "block"
      if (i == 0)
      { yy[i].className = "firstSelected"
      }
      else if (i == (zz - 1))
      { yy[i].className = "lastSelected"
      }
      else
      { yy[i].className = "selected"
      }
    }
    else
    { document.getElementById("servicesContent" + i).style.display = "none"
      if (i == 0)
      { yy[i].className = "first"
      }
      else if (i == (zz - 1))
      { yy[i].className = "last"
      }
      else
      { yy[i].className = ""
      }
    }
  }
}

function showServSearch(status)
{ if (status)
  { document.getElementById("servicesDrop").style.display = "block"
  }
  else
  { document.getElementById("servicesDrop").style.display = "none"
  }
}

// This function determines if strInput is empty:
//        Example usage: if (isEmpty(form.login.value)) { ...
//
function isEmpty(strInput)
{
	if (strInput == "" || strInput == null)
	{
		return true;
	}
	return false;
}


// This function will attempt to validate an e-mail address, if it is given
// It assumes that the email field on the form is called "email".
// It will take 2 parameters: the email address object and true/false if
//  the field is required.
//        Example usage:  if (!isValidEmailAddress(form.email, true)) { ...
//
function isValidEmailAddress(email, blnRequired)
{
   var allValid = true;
   var strInput = email.value;
	
   // Check if the email address exists and if it is required:
	if (isEmpty(strInput))
	{
	     if (!blnRequired) { return true; }
	     else {
	      alert("Please enter your e-mail address.");
	      email.focus();
	      return false;
	     } // else
	    } //if
	
	   var strLen = strInput.length;
	
	   // CHECK IF EMAIL ADDRESS HAS A '@' CHARACTER:
	     if (strInput.indexOf("@") == -1) {
	      alert("The e-mail address you entered was not valid because it does not have a '@'.  Please re-enter your \"e-mail address\".");
	      email.focus();
	      email.select();
	      return false;
	     } //if
	
	   // CHECK IF EMAIL ADDRESS HAS A '.' CHARACTER:
	     	if (strInput.indexOf(".") == -1) {
	       		alert("The e-mail address you entered was not valid because it does not have a '.'.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	          return false;
	     	} //if
	
	   // CHECK IF EMAIL ADDRESS STARTS WITH A '.':
	     	if (strInput.indexOf('.') == 0) {
	       		alert("The e-mail address you entered was not valid because it starts with '.'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   // CHECK IF EMAIL ADDRESS STARTS WITH A '@':
	     	if (strInput.indexOf('@') == 0) {
	       		alert("The e-mail address you entered was not valid because it starts with '@'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   // Check for any invalid characters in the address:
	     	for (var i = 0; i < strLen; i++) {
	         	ch = strInput.charAt(i)
	         	if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
	                 	|| (ch == "@") || (ch == ".") || (ch == "_")
	               	  	|| (ch == "-") || (ch >= "0" && ch <= "9")
				|| (ch == ",")) { ; }
	         	else { allValid = false; }
	     	} //for
	
	     	if (!allValid) {
	       		alert("The e-mail address you entered was not valid because it contains an illegal character.  Valid characters are letters, numbers, dashes or underscores.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   //It has a . adjacent to the @:
	         var atpos=strInput.indexOf('@')
	     	if (strInput.charAt(atpos-1)=='.'){
	       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} // if
	
	         var copy=strInput.substring(atpos+1,strInput.length);
	   //It contains more than one @:
	  	/*if (copy.indexOf('@')!=-1){
	       		alert("The e-mail address you entered was not valid because it contains more than one '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}*/
	
	   // It has a . adjacent to the @:
	  	if (copy.indexOf(".")<1){
	       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}
	
	   // It ends with a . or an @:
	  	if (copy.lastIndexOf(".")+1==copy.length){
	       		alert("The e-mail address you entered was not valid because it ends with '.' or an '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}
	
	   // Invalid top level domain length:
	  	copy = strInput.substring(strInput.lastIndexOf('.')+1,strLen);
	  	var len = copy.length;
	   	if (len == 2 || len == 3 || len == 4) { ; }
	   	else {
	       	 alert("The e-mail address you entered was not valid since the top-level domain (after the last .) should only be 2 or 3 characters in length.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	       	 return false;
	 	} //else
	
	   // It contains two adjacent .:
	  	copy = strInput;
	  	while (copy.indexOf('.') != -1) {
	         copy = copy.substring(copy.indexOf('.') + 1, copy.length);
	         if (copy.indexOf('.') == 0) {
	       	  alert("The e-mail address you entered was not valid because it contains two adjacent periods.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	       	  return false;
	     	 } //if
	  	} //while
	
	     	return true;
	
	  } //isValidEmailAddress

/*	  
	 function ValidateForm(form) 
	 {
	
	   	// The friend's name is a required field:
	   if (isEmpty(form.toname.value)) {
	   		alert("Please enter your Friend's name.");
	   		form.toname.focus();
	   		return false;
	   }
	   	// The friend's email address is required and must be valid:
	   if (!isValidEmailAddress(form.toemail, true)) {
	       return false;
	   }
	   	// The sender's name is a required field:
	   if (isEmpty(form.fromname.value)) {
	   		alert("Please enter your name.");
	   		form.fromname.focus();
	   		return false;
	   }
	    // The sender's email address is required and must be valid:
	   if (!isValidEmailAddress(form.fromemail, true)) {
	       return false;
	   }
	}
*/
	function ValidateForm(theForm)
	{
		var isValid = new Boolean(true);
		if (theForm != null)
		{
			var myFormElement = null;
			var i=0;
			while ( i < theForm.elements.length && isValid )
			{
				myFormElement = theForm.elements[i];
				if (myFormElement != null)
				{
					if ( !isEmpty(myFormElement.id) && (myFormElement.id.toUpperCase().indexOf("_REQUIRED") >= 0) )
					{
						if ( (myFormElement.type == "text") && isEmpty( myFormElement.value) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "textarea") && isEmpty( myFormElement.value) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "select-one") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "select-multiple") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "radio") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
					}
					else
					{
					}
					
					if (!isValid)
					{
							alert(myFormElement.name + " is required");
							myFormElement.focus();
					}
				}
				i++;
			}
		}
		
		return isValid;
	}
