//var _docHeight = document.height || document.body.offsetHeight;
//var _docWidth = document.width || document.body.offsetWidth;

/**
 * function addLoadEvent(func)
 *
 * Adds a function call to the window onload event.  This function was taken
 * from http://www.htmlgoodies.com/beyond/javascript/article.php/3724571 .
 *
 * INPUTS:
 *    - func = The function to add to the window onload event.
 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/**
 * Shows a hidden div and changes the "show" link to a "hide" link.
 */
function showDiv(divId) {
	var divToShow = document.getElementById(divId);
	if (divToShow != null) {
		divToShow.style.display = 'block';
	}
	var showLink = document.getElementById(divId + 'ShowLink');
	if (showLink != null) {
		showLink.style.display = 'none';
	}
	var hideLink = document.getElementById(divId + 'HideLink');
	if (hideLink != null) {
		hideLink.style.display = 'block';
	}
	var arrow = document.getElementById(divId + 'Arrow');
	if (arrow != null) {
		arrow.src = '/img/expanded_arrow.gif'
	}
}

/**
 * Hides a visible div and changes the "hide" link to a "show" link.
 */
function hideDiv(divId) {
	var divToShow = document.getElementById(divId);
	if (divToShow != null) {
		divToShow.style.display = 'none';
	}
	var hideLink = document.getElementById(divId + 'HideLink');
	if (hideLink != null) {
		hideLink.style.display = 'none';
	}
	var showLink = document.getElementById(divId + 'ShowLink');
	if (showLink != null) {
		showLink.style.display = 'block';
	}
	var arrow = document.getElementById(divId + 'Arrow');
	if (arrow != null) {
		arrow.src = '/img/collapsed_arrow.gif'
	}
}

// From Quackit.com
// Popup window code
function newPopup(url, windowHeight, windowWidth) {
	popupWindow = window.open(
		url,'popUpWindow','height=' + windowHeight + ',width=' + windowWidth + ',left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}

// From http://www.mredkj.com/tutorials/tutorial_mixed2b.html
// Add/remove select box options
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue) {
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex) {
  var selLength = theSel.length;
  if(selLength > 0) {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo) {
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i = selLength - 1; i >= 0; i--) {
    if(theSelFrom.options[i].selected) {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i = selectedCount - 1; i >= 0; i--) {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}

function selectAll(selectElmt) {
	if (selectElmt == null) {
		return;
	}
	for (i = 0; i <= selectElmt.options.length - 1; i++) {
		selectElmt.options[i].selected = 'selected';
	}
}

/****************************************************************************/
/* USED BY VIEWS/USERS/REGISTER4.CTP                                        */
/****************************************************************************/

/**
 * function changeGoals(goalInd)
 *
 * Enables/disables the appropriate form fields depending on which goals
 * checkbox was checked/unchecked.  (This function is used by views/users/
 * register4.ctp.)
 *
 * INPUTS:
 *    - goalInd = The index of the fitness goal that was checked/unchecked
 */
function changeGoals(goalInd) {
	var disable = false;
	var lowInd = 0;
	var highInd = 0;
	
	// Decide whether to enable or disable the fields that correspond with the
	// given fitness goal index
	if (goalInd == 1 || goalInd == 3) { // cardio || flexibility
		checkboxNode1 = this.document.getElementById('fitnessGoal1');
		checkboxNode2 = this.document.getElementById('fitnessGoal3');
		if (!checkboxNode1.checked && !checkboxNode2.checked) {
			disable = true;
		}
		lowInd = 1;
		highInd = 12;
	} else if (goalInd == 2) { // resistance training
		checkboxNode = this.document.getElementById('fitnessGoal2');
		if (!checkboxNode.checked) {
			disable = true;
		}
		lowInd = 13;
		highInd = 31;
	}
	
	// Enable/disable the appropriate fields
	for (i = lowInd; i <= highInd; i++) {
		var measVal = document.getElementById('measVal' + i);
		if (measVal != null) {
			measVal.disabled = disable;
		}
		var measGoal = document.getElementById('measGoal' + i);
		if (measGoal != null) {
			measGoal.disabled = disable;
		}
	}
}

/**
 * function syncFields(changedNode, nodeIdToSync)
 *
 * Synchronizes two fields representing the same value.  (This function is
 * used by views/users/register4.ctp.)
 *
 * INPUTS:
 *    - changedNode = The node whose value was changed
 *    - nodeIdToSync = The ID of the node whose value should be synchronized
 */
function syncFields(changedNode, nodeIdToSync) {
	var nodeToSync = document.getElementById(nodeIdToSync);
	if (nodeToSync == null) {
		return;
	}
	nodeToSync.value = changedNode.value;
}

/****************************************************************************/
/* USED BY VIEWS/USERS/REGISTER2.CTP                                        */
/****************************************************************************/

function secQuestionChange(selectNode) {
	secQuestionOther = document.getElementById('secQuestionOtherRow');
	if (selectNode.value == 1) {
		secQuestionOther.style.display = 'table-row';
	} else {
		secQuestionOther.style.display = 'none';
	}
}

/****************************************************************************/
/* USED BY VIEWS/USERS/SEARCH.CTP                                           */
/****************************************************************************/

function showAdvancedCriteria() {
	criteria = document.getElementById('advancedSearchCriteria');
	criteria.style.display = 'block';
	showLink = document.getElementById('showAdvancedCriteriaLink');
	showLink.style.display = 'none';
	hideLink = document.getElementById('hideAdvancedCriteriaLink');
	hideLink.style.display = 'block';
	hideLink2 = document.getElementById('hideAdvancedCriteriaLink2');
	hideLink2.style.display = 'block';
}

function hideAdvancedCriteria() {
	criteria = document.getElementById('advancedSearchCriteria');
	criteria.style.display = 'none';
	showLink = document.getElementById('showAdvancedCriteriaLink');
	showLink.style.display = 'block';
	hideLink = document.getElementById('hideAdvancedCriteriaLink');
	hideLink.style.display = 'none';
	hideLink2 = document.getElementById('hideAdvancedCriteriaLink2');
	hideLink2.style.display = 'none';
}

function clearFileInputField(tagId) {
	document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML;
} 

function enableImageUploadField() {
	document.getElementById('avatarFile').disabled = false;
	document.getElementById('avatarUpload').style.display = 'inline';
	document.getElementById('avatarDisplay').style.display = 'none';
	document.getElementById('hasNewAvatar').value = 1;
}

function disableImageUploadField() {
	document.getElementById('avatarFile').disabled = false;
	document.getElementById('avatarUpload').style.display = 'none';
	document.getElementById('avatarDisplay').style.display = 'inline';
	document.getElementById('hasNewAvatar').value = 0;
}
