//* Utility functions *//

//Trims spaces
function trim(string)
{return string.replace(/^\s+|\s+$/g,"");}

//Sets the background color of an element on the page
function setBackgroundColor(id, color)
{document.getElementById(id).style.backgroundColor = color;}

//Adds the current page to the user's bookmarks
function setBookmark()
{
	if(document.all)
	{window.external.AddFavorite(self.location.href, document.title);}
	else if (window.sidebar)
	{window.sidebar.addPanel(document.title, self.location.href, "");}
	else if(window.opera && window.print)
	{
		var elem = document.createElement('a');
		elem.setAttribute('href',self.location.href);
		elem.setAttribute('title',document.title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else
	{alert("Press CTRL + D to add a bookmark to: \n" + self.location.href);}
}

//Swaps the InnerHTML value on the given object
function setText(objectId, text)
{
	var obj = document.getElementById(objectId);
	if (obj != null)
	{obj.innerHTML = text;}
}

// Code to show or hide div tags based on their previous state
function showMeOrNot(divID) 
{
	var item = document.getElementById(divID);
	if (item) 
	{item.className=(item.className=='showMeNot')?'showMe':'showMeNot';}
	//resizing();
}

// Checks if an email address is in a valid format
function validEmail(source, arguments)
{
	var regExp=/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
	var email = trim(arguments.Value).toLowerCase();
	if (regExp.test(email))
	{arguments.IsValid = true;}
	else
	{arguments.IsValid = false;}
}

// Tests for release of key in field (CurrentFieldName). Moves focus to next 
// field (NewFocusField) after certain number of key releases (FieldLength).  
// Ignores Tab (keycode 9) and Shift (keyCode 16) keys.  
// The RegularExpression is used in a search to make sure only numbers are put into the field.
function PhoneFocus(FieldLength, CurrentFieldId, NewFocusFieldId, RegularExpression, Key)
{
	if((Key != 9) && (Key != 16))
		{	
		if((document.getElementById(CurrentFieldId).value.length == FieldLength) && (document.getElementById(CurrentFieldId).value.search(RegularExpression) == -1))
			{
				//alert("Your phone number must contain only numbers.");
				if( document.getElementById(CurrentFieldId).getAttribute('autocomplete') )
				{document.getElementById(CurrentFieldId).setAttribute('autocomplete','off');}
				document.getElementById(CurrentFieldId).focus();
				return(false);
			}
		
		if(document.getElementById(CurrentFieldId).value.length == FieldLength)
			{
				if( document.getElementById(NewFocusFieldId).getAttribute('autocomplete') )
				{document.getElementById(NewFocusFieldId).setAttribute('autocomplete','off');}
				document.getElementById(NewFocusFieldId).focus();
			}
		}
}

function IsNumeric(val) 
{
	if (isNaN(parseFloat(val))) 
	{return false;}
	return true
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.69
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='* '+a[i+3]+'\n'; err=false}
  }
  if (s!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}

function validateGoogleSearch(source, arguments)
{
	if (trim(arguments.Value) == getDefaultGoogleSearchText() || trim(arguments.Value) == "")
	{
		arguments.IsValid = false;
		alert("Please enter a search term.");
	}
	else
	{arguments.IsValid = true;}
}

function validateMyCintas(source, arguments)
{
	if (trim(arguments.Value) == getDefaultMyCintasText() || trim(arguments.Value) == "")
	{
		arguments.IsValid = false;
		alert("Please make entry.");
	}
	else
	{arguments.IsValid = true;}
}

function validateSearch(googleSearchBoxId)
{
	arg = document.getElementById(googleSearchBoxId);
	if (trim(arg.value) == getDefaultGoogleSearchText() || trim(arg.value) == "")
	{
		alert("Please enter a search term.");
		return false;
	}
	else
	{return true;}
}

function clearGoogleSearchText(googleSearchBoxId)
{
	searchBox = document.getElementById(googleSearchBoxId);
	if (trim(searchBox.value) == getDefaultGoogleSearchText())
	{searchBox.value = "";}
}

function resetGoogleSearchText(googleSearchBoxId)
{
	searchBox = document.getElementById(googleSearchBoxId);
	if (trim(searchBox.value) == "")
	{searchBox.value = getDefaultGoogleSearchText();}
}

function clearMyCintasText(myCintasBoxId)
{
	myCintasBox = document.getElementById(myCintasBoxId);
	if (trim(myCintasBox.value) == getDefaultMyCintasText())
	{myCintasBox.value = "";}
}

function resetMyCintasText(myCintasBoxId)
{
	myCintasBox = document.getElementById(myCintasBoxId);
	if (trim(myCintasBox.value) == "")
	{myCintasBox.value = getDefaultMyCintasText();}
}


function googleSearchButtonOver(googleSearchButtonId)
{setBackgroundColor(googleSearchButtonId, "#C1D4E3");}

function googleSearchButtonOut(googleSearchButtonId)
{setBackgroundColor(googleSearchButtonId, "#A3ADB8");}

function getDefaultGoogleSearchText()
{
	if (defaultGoogleSearchText == null)
	{return "Search Cintas";}
	else
	{return defaultGoogleSearchText;}
}

function getDefaultMyCintasText()
{
	if (defaultMyCintasText == null)
	{return "MyCintas ID";}
	else
	{return defaultMyCintasText;}
}
function myCintasButtonOver(myCintasButtonId)
{setBackgroundColor(myCintasButtonId, "#C1D4E3");}
function myCintasButtonOut(myCintasButtonId)
{setBackgroundColor(myCintasButtonId, "#A3ADB8");}

function googleSearchButtonOut(myCintasButtonId)
{setBackgroundColor(myCintasButtonId, "#A3ADB8");}


function validateLocationSearch(source, arguments)
{
	if (trim(arguments.Value) == getDefaultLocationSearchText() || trim(arguments.Value) == "")
	{
		arguments.IsValid = false;
		alert("Please enter a location zip.");
	}
	else
	{return true;}
}

function validateLocSearch(locSearchBoxId)
{
	arg = document.getElementById(locSearchBoxId);
	if (trim(arg.value) == getDefaultLocationSearchText() || trim(arg.value) == "")
	{
		alert("Please enter a location zip.");
		return false;
	}
	else
	{
		cmCreatePageElementTag(arg.value,'Location Finder');
		return true;
	}
}

function clearLocationSearchText(locationSearchBoxId)
{
	searchBox = document.getElementById(locationSearchBoxId);
	if (trim(searchBox.value) == getDefaultLocationSearchText())
	{searchBox.value = "";}
}

function resetLocationSearchText(locationSearchBoxId)
{
	searchBox = document.getElementById(locationSearchBoxId);
	if (trim(searchBox.value) == "")
	{searchBox.value = getDefaultLocationSearchText();}
}
 
function locationSearchButtonOver(locationSearchButtonId)
{setBackgroundColor(locationSearchButtonId, "#C1D4E3");}

function locationSearchButtonOut(locationSearchButtonId)
{setBackgroundColor(locationSearchButtonId, "#003698");}

function getDefaultLocationSearchText()
{
	if (defaultLocationSearchText == null)
	{return "Find Location by ZIP";}
	else
	{return defaultLocationSearchText;}
}

//* SlideShow Functions *//

//global slideshow object
var slideShow = {	"clock" : null, "fade" : true, "count" : 1, "selectedSlideId" : 0, "selectedSlideNavId" : 0,
					"transitioning" : false, "hoveringSlideNavId" : 0, "stoppedHovering" : false }

//wrapper function for the swapfade call.
function transitionSlide(newSlideId, newSlideNavId)
{
	var selectedSlide = document.getElementById(slideShow.selectedSlideId);
	var newSlide = document.getElementById(newSlideId);
	
	if ((selectedSlide != null) && (newSlide != null) && (slideShow.selectedSlideId != newSlideId))
	{
		if (!slideShow.transitioning)
		{
			slideShow.transitioning = true;
			document.getElementById(slideShow.selectedSlideNavId).className = "";
			document.getElementById(newSlideNavId).className = "SelectedNavItem";
			
			try
			{
				document.getElementById(slideShow.selectedSlideId + "_navlink").className = "";
				document.getElementById(newSlideId + "_navlink").className = "SelectedNavLink";
			}
			catch (e){}
			
			slideShow.selectedSlideNavId = newSlideNavId;
			swapfade(newSlideId);
		}
	}
}

//wrapper function for the swapfade call.
function transitionSlideNoFade(newSlideId, newSlideNavId)
{
	var selectedSlide = document.getElementById(slideShow.selectedSlideId);
	var newSlide = document.getElementById(newSlideId);
	
	if ((selectedSlide != null) && (newSlide != null) && (slideShow.selectedSlideId != newSlideId))
	{
		if (!slideShow.transitioning)
		{
			slideShow.transitioning = true;
			document.getElementById(slideShow.selectedSlideNavId).className = "";
			document.getElementById(newSlideNavId).className = "SelectedNavItem";
			
			try
			{
				document.getElementById(slideShow.selectedSlideId + "_navlink").className = "";
				document.getElementById(newSlideId + "_navlink").className = "SelectedNavLink";
			}
			catch (e){}
			
			slideShow.selectedSlideNavId = newSlideNavId;
			swap(newSlideId);
		}
	}
}

function delayHomeSlideTransition(slideNavId, newSlideId)
{
	if ((slideNavId == slideShow.hoveringSlideNavId) && (slideShow.stoppedHovering == false))
	{
		if(slideShow.transitioning)
		{
			setInterval("delayHomeSlideTransition('" + slideNavId + "', '" + newSlideId + "')", 500);
			delayHomeSlideTransition(slideNavId, newSlideId)
		}
		else
		{
			slideShow.hoveringSlideNavId = 0;
			transitionSlide(newSlideId, slideNavId);
		}
	}
}

function noDelayHomeSlideTransition(slideNavId, newSlideId)
{
	if ((slideNavId == slideShow.hoveringSlideNavId) && (slideShow.stoppedHovering == false))
	{
		if(slideShow.transitioning)
		{
			//setInterval("delayHomeSlideTransition('" + slideNavId + "', '" + newSlideId + "')", 500);
			delayHomeSlideTransition(slideNavId, newSlideId)
		}
		else
		{
			slideShow.hoveringSlideNavId = 0;
			transitionSlideNoFade(newSlideId, slideNavId);
		}
	}
}

function slideNavOver(slideNavId)
{
	if (slideNavId != slideShow.selectedSlideNavId)
	{
		document.getElementById(slideNavId).className = "SelectedNavItem";
		
		if (arguments[1] != null)
		{
			document.getElementById(slideNavId).className = "SelectedNavItem";
			document.getElementById(slideNavId).style.cursor = "default";
			slideShow.stoppedHovering = false;
			slideShow.hoveringSlideNavId = slideNavId;
			window.setTimeout("delayHomeSlideTransition('" + slideNavId + "', '" + arguments[1] + "')", 250);
		}
		else
		{document.getElementById(slideNavId).style.cursor = "pointer";}
	}
	else
	{document.getElementById(slideNavId).style.cursor = "default";}
}

function slideNavOverNoFade(slideNavId)
{
	if (slideNavId != slideShow.selectedSlideNavId)
	{
		document.getElementById(slideNavId).className = "SelectedNavItem";
		
		if (arguments[1] != null)
		{
			document.getElementById(slideNavId).className = "SelectedNavItem";
			document.getElementById(slideNavId).style.cursor = "default";
			slideShow.stoppedHovering = false;
			slideShow.hoveringSlideNavId = slideNavId;
			//window.setTimeout("delayHomeSlideTransition('" + slideNavId + "', '" + arguments[1] + "')", 250);
			noDelayHomeSlideTransition(slideNavId, arguments[1]);
		}
		else
		{document.getElementById(slideNavId).style.cursor = "pointer";}
	}
	else
	{document.getElementById(slideNavId).style.cursor = "default";}
}

function slideNavOut(slideNavId)
{
	slideShow.stoppedHovering = true;
	if (slideNavId != slideShow.selectedSlideNavId)
	{document.getElementById(slideNavId).className = "";}
}

//swap setup function
function swap()
{
	slideShow.newSlide = document.getElementById(arguments[0]);
	slideShow.selectedSlide = document.getElementById(slideShow.selectedSlideId);
	slideShow.selectedSlideId = arguments[0];
	//swap the div layers
	slideShow.newSlide.className = "SelectedSlide";
	slideShow.transitioning = false;
	slideShow.selectedSlide.className = "DeselectSlide";
};


//swapfade setup function
function swapfade()
{
slideShow.clock = null;

	//if the timer is not already going
	if(slideShow.clock == null)
	{
		slideShow.newSlide = document.getElementById(arguments[0]);
		slideShow.selectedSlide = document.getElementById(slideShow.selectedSlideId);
		
		//store the supported form of opacity
		if(typeof slideShow.newSlide.style.opacity != "undefined")
		{slideShow.type = "w3c";}
		else if(typeof slideShow.newSlide.style.MozOpacity != "undefined")
		{slideShow.type = "moz";}
		else if(typeof slideShow.newSlide.style.KhtmlOpacity != "undefined")
		{slideShow.type = "khtml";}
		else if(typeof slideShow.newSlide.style.filter != "undefined")
		{slideShow.type = "ie";}
		else
		{slideShow.type = "none";}
		

		//if any kind of opacity is supported
		if(slideShow.type != "none")
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			slideShow.length = parseInt(1, 10) * 250;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			slideShow.resolution = parseInt(1, 10) * 10;
			
			//start the timer
			slideShow.clock = setInterval("slideShow.swapfade()", slideShow.length/slideShow.resolution);
			slideShow.selectedSlideId = arguments[0];
		}
		
		//otherwise if opacity is not supported
		else
		{
			slideShow.selectedSlideId = arguments[0];
			
			//swap the div layers
			slideShow.newSlide.className = "SelectedSlide";
			slideShow.selectedSlide.className = "";
			slideShow.transitioning = false;
	}
			//slideShow.selectedSlide.className = "DeselectSlide";
	 } 
};

//swapfade timer function
slideShow.swapfade = function()
{
	//increase or reduce the counter on an exponential scale
	slideShow.count = (slideShow.fade) ? slideShow.count * 0.9 : (slideShow.count * (1/0.9)); 
	
	//choose which slide to fade
	slideShow.fadingSlide = (slideShow.fade) ? slideShow.selectedSlide : slideShow.newSlide;
	
	//if the counter has reached the bottom
	if(slideShow.count < (1 / slideShow.resolution))
	{
		//clear the timer
		clearInterval(slideShow.clock);
		slideShow.clock = null;

		//do the div layer swap
		slideShow.selectedSlide.className = "";
		slideShow.newSlide.className = "SelectedSlide";

		//reverse the fade direction flag
		slideShow.fade = false;
		
		//restart the timer
		slideShow.clock = setInterval("slideShow.swapfade()", slideShow.length/slideShow.resolution);
	}
	
	//if the counter has reached the top
	if(slideShow.count > (1 - (1 / slideShow.resolution)))
	{
		//clear the timer
		clearInterval(slideShow.clock);
		slideShow.clock = null;

		//reset the fade direction flag
		slideShow.fade = true;
		
		//reset the counter
		slideShow.count = 1;
		
		slideShow.transitioning = false;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(slideShow.type)
	{
		case "ie" :
			slideShow.newSlide.style.filter = "alpha(opacity=" + Math.ceil(slideShow.count * 100) + ")";
			slideShow.selectedSlide.style.filter = "alpha(opacity=" + Math.ceil(slideShow.count * 100) + ")";
			break;
			
		case "khtml" :
			slideShow.newSlide.style.KhtmlOpacity = slideShow.count;
			slideShow.selectedSlide.style.KhtmlOpacity = slideShow.count;
			break;
			
		case "moz" : 
			//restrict max opacity to prevent a visual popping effect in firefox
			slideShow.newSlide.style.MozOpacity = (slideShow.count == 1 ? 0.9999999 : slideShow.count);
			slideShow.selectedSlide.style.MozOpacity = (slideShow.count == 1 ? 0.9999999 : slideShow.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			slideShow.newSlide.style.opacity = (slideShow.count == 1 ? 0.9999999 : slideShow.count);
			slideShow.selectedSlide.style.opacity = (slideShow.count == 1 ? 0.9999999 : slideShow.count);
	}
};

var homepagescript;
var homeSelectedSlide;
var homeNavSlideArray;

function executeHomepageNavScriptOver()
{
	for (i=1;i<=8;i++)
	{
		var tempSlide = "slide_" + i;
	
		if (tempSlide == homeSelectedSlide)
		{slideNavOverNoFade(homeNavSlideArray[i-1], homeSelectedSlide);}
		/*else
		{
			document.getElementById(tempSlide).className = "DeselectSlide";
		}*/
	}
}

function homepageNavScriptOver(selectedSlide, navSlideArray)
{
	homeSelectedSlide = selectedSlide;
	homeNavSlideArray = navSlideArray;
	homepagescript = setTimeout("executeHomepageNavScriptOver()", 200);
}

function homepageNavScriptOut(selectedSlide, navSlideArray)
{
	clearTimeout(homepagescript);
	
	for (i=1;i<=8;i++)
	{
		var tempSlide = "slide_" + i;
	
		if (tempSlide == selectedSlide)
		{slideNavOut(navSlideArray[i-1]);}
		
	}
}

/* main menu */
var menuTimer;
var currItem = '0';
var closeFn;

function menu(item, tab)
{
	if (currItem == tab && menuTimer != null)
	{
		clearTimeout(menuTimer);
	}
	else
	{
		if (menuTimer != null)
			{
				clearTimeout(menuTimer);
				setTimeout(closeFn, 0);
			}
	}
	
	currItem = tab;
	
	var menuTabArea = document.getElementById("menuTabArea" + tab);
	var menuTab = document.getElementById("menuTab" + tab);
	
	menuTabArea.style.zIndex = '100';
	menuTab.style.zIndex = '102';
	
	menuTabArea.style.borderBottomWidth = "1px";
	menuTabArea.style.borderColor = "#BDBDBD #BDBDBD #f6f1e9 #BDBDBD";
	menuTab.style.backgroundColor="#F6F1E9";
	menuTab.style.borderColor = "#F6F1E9 #F6F1E9 #F6F1E9 #F6F1E9";
	document.getElementById("menu" + tab).style.display = "block";
}

function closeMenu(item, tab)
{
	closeFn = 'setCloseMenu(' + tab + ')';
	menuTimer = setTimeout(closeFn, 30); 
	
}
function setCloseMenu(tab)
{
	var menuTabArea = document.getElementById("menuTabArea" + tab);
	var menuTab = document.getElementById("menuTab" + tab);
	
	document.getElementById('menu' + tab).style.display = 'none';
	menuTabArea.style.borderColor = "Transparent";
	menuTabArea.style.borderBottomWidth = "0px";
	menuTab.style.backgroundColor="Transparent";
	menuTab.style.borderColor = "Transparent";
	menuTabArea.style.zIndex = "100";
	menuTab.style.zIndex = "100";
	//document.getElementById("menu1").className = "tab";
	//document.getElementById("menu2").className = "tab";
}
/* main menu */

