// JavaScript Document

function searchString(txt,check,direction,startPos) {
	// Start from startPos in txt string and search in direction
	// until first instance of a character in searchChars
	// returns location of character
	//   txt = the string to search
	//   check = string containing characters being searched for, e.g. "/_."
	//   direction (optional) = direction of search: 1=forward (default), -1=backwards
	//   startPosition = position in input string to start; defaults to start if forward search, defaults to end if backward search
	//	Note: Javascript 1.2's regular expressions makes this obsolete, but hey, there are still 3.0 browsers

	if (!direction) direction = 1;
	else {
		if (direction < 1) direction = -1;
		if (direction > 1) direction = 1;
	}

	if (!startPos) {
		if (direction == 1) startPos = 0
		else startPos = txt.length - 1;
	}
	
	var i=startPos;
	while ( (i >= 0) && (i <= txt.length) && !found) {
		// move throungh each character in txt starting at startPos
		var chr = txt.charAt(i);
		var found = false;
		// for each character, test it against all the check characters
		for (var j = 0; j < check.length; j++) {
			if (chr == check.charAt(j)) {
				found = true;
				foundPos = i;
				}
			}
		i = i + direction
		}
	if (!found) foundPos=-1
	return foundPos
}

function getSubString(txt,startPos,endPosition) {
	var newTxt="";
	for (i=startPos; i<=endPosition; i++) {
		newTxt += txt.charAt(i);
		}
	return newTxt;
}

function isAmong() {
	// check if param1 (check) is one of the following params (param2,3,4,etc.)
	var check = isAmong.arguments[0];
	var lastArgument = isAmong.arguments.length
	var found = false;
    for (var i=1; (i<=lastArgument && !found); i++) {
      if ( isAmong.arguments[i] == check ){
	  	found = true
    	}
  	}
	return found
}

function updateNav() {
	testString = document.location.href
	// find the section name within the href of this web page
	startOfFolder = searchString(testString,"/",-1)+1;
	endOfSectionName = searchString(testString,"_.",1,startOfFolder+1)-1;
	theSectionName = getSubString(testString,startOfFolder,endOfSectionName);
	if (isAmong(theSectionName,"home","services","productphotos","quality","clients","aboutus","contactus","joblistings")) {
		// if it's amoung the valid possibilities, change the corresponding section image to the active state
		//alert("change: "+theSectionName);
		document.images[theSectionName].src = "images/Buttons/b_"+theSectionName+"_f2.jpg";
		}
}