function showUser(userID)
{
	document.getElementById('userInfo').src = "/meta/showuser.php?ID=" + userID;
	document.getElementById('userInfo').focus();
}

function showSQLAttack(logID)
{
	document.all.userInfo.src = "/meta/showsqlattack.php?ID=" + logID;
	document.all.userInfo.focus();
}

function showTransactionFailure(ID)
{
	document.all.userInfo.src = "/meta/showtransactionfailure.php?ID=" + ID;
	document.all.userInfo.focus();
}

function showIPBan(ID)
{
	document.all.userInfo.src = "/meta/showipban.php?ID=" + ID;
	document.all.userInfo.focus();
}

function changeReviewStatus()
{
	document.subForm.submit();
}

function changeUserID()
{
	document.subFormImpersonate.submit();
}

function setViewSelectStatus(selectedItem)
{
	document.subForm.selectStatus.value = selectedItem;
}

function submitUserInfo(source)
{
	var proceed = true;
	var errorMessage = "";
	if(source != 'pp')
	{
		if(document.subFormUserInfo.resetpassword.checked)
		{
			if(document.subFormUserInfo.NewPassword.value != document.subFormUserInfo.NewPassword2.value)
			{
				proceed = false;
				errorMessage = errorMessage + "You did not retype the password correctly. Please check it and resubmit.\n";
			}
			if(document.subFormUserInfo.NewPassword.value.length < 6)
			{
				proceed = false;
				errorMessage = errorMessage + "The new password must be at least 6 characters\n";
			}
			if(source == "Profile")
			{
				if(document.subFormUserInfo.Password.value == ''){
					proceed = false;
					errorMessage = errorMessage + "You must enter your old password (the one which you logged in with)\n";
				}
			}
		}
	}
	if(proceed)
	{
		document.subFormUserInfo.submitForm.value = 'toProcess';
		document.subFormUserInfo.submit();
	}else
	{
		alert(errorMessage);
	}
}

function updateSQLAttack()
{
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

function showJournalEntry(visitID, option)
{
	if(option == 'Food')
	{
		window.location = '/foodjournal.php?ID=' + visitID;
	}
	else
	{
		document.getElementById('userInfo').src = "/meta/showjournalentry.php?ID=" + visitID;
		document.getElementById('userInfo').focus();
	}
}

function newJournalEntry(option)
{
	if(option == 'Food')
	{
		window.location = '/foodjournal.php?ID=New';
	}
	else
	{
		document.getElementById('userInfo').src = "/meta/showjournalentry.php?ID=New";
		document.getElementById('userInfo').focus();
	}
}

function newJournalEntry2(option)
{
	if(option == 'Food')
	{
		window.location = '/foodjournal.php?ID=New';
	}
	else
	{
		document.getElementById('userInfo').src = "/meta/showjournalentry2.php?ID=New";
		document.getElementById('userInfo').focus();
	}
}

function changeJournalEntryType()
{
	var showWeighIn = 'block';
	var showExercise = 'block';
	if(document.subFormUserInfo.VisitType.value == 'WeightLoss')
	{
		showExercise = 'none';
	}
	else if(document.subFormUserInfo.VisitType.value == 'Exercise')
	{
		showWeighIn = 'none';
	}
	
	document.getElementById('trWeight').style.display = showWeighIn;
	document.getElementById('trNutrients').style.display = showWeighIn;
	
	document.getElementById('trExerciseType').style.display = showExercise;
	document.getElementById('trExerciseDuration').style.display = showExercise;
	document.getElementById('trCaloriesExpended').style.display = showExercise;
}

function changeJournalEntryType2()
{
	var showWeighIn = 'visible';
	var showExercise = 'visible';
	if(document.subFormUserInfo.VisitType.value == 'WeightLoss')
	{
		showExercise = 'hidden';
	}
	else if(document.subFormUserInfo.VisitType.value == 'Exercise')
	{
		showWeighIn = 'hidden';
	}
	
	document.getElementById('divWeighIn').style.visiblity = showWeighIn;
	document.getElementById('divExercise').style.visiblity = showExercise;
}

function submitJournalEntry()
{
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

function closeJournalEntry()
{
	var answer = confirm("Close this form? No data or plate entries will be saved.");
	if (answer){
		parent.window.location = '/dailyvisit.php?selectStatus=Food';
	}
}

function changeSetPassword(source)
{
	var showPWFields = 'none';
	if(document.subFormUserInfo.resetpassword.checked == true)
	{
		showPWFields = 'inline';
	}
	
	document.getElementById('trNewPassword').style.display = showPWFields;
	document.getElementById('trNewPassword2').style.display = showPWFields;
	if(source == "Profile")
	{
		document.getElementById('trPassword').style.display = showPWFields;
	}
}

function accountStatusAlert()
{
	var previousStatus = document.subFormUserInfo.originalStatus.value;
	if(previousStatus == 'Pending' & document.subFormUserInfo.AccountStatus.value == 'Active')
	{
		var warning = '<p>Submitting this user information will register them on the forums.</p>';
		document.getElementById('displayAccountStatusWarningSpan').innerHTML = warning;
	}
	else if(previousStatus == 'Active' & document.subFormUserInfo.AccountStatus.value == 'Cancelled')
	{
		var warning = '<p>Submitting this user information will delete the user from the forums, but not their posts.</p>';
		document.getElementById('displayAccountStatusWarningSpan').innerHTML = warning;
	}else
	{
		document.getElementById('displayAccountStatusWarningSpan').innerHTML = '';
	}
}

function editProduct(productID)
{
	document.all.userInfo.src = "/store/productdetails.php?ProductID=" + productID + "&Edit=True";
	document.all.userInfo.focus();		
}

function showHideMealBuilder()
{
	if( document.subFormUserInfo.ShowMealBuilder[0].checked == true)
	{
		document.getElementById('MealBuilderIFrame').style.display = "inline";
	}
	else
	{
		document.getElementById('MealBuilderIFrame').style.display = "none";
	//	document.all.MealBuilderIFrame.style.display = "none";
	}	
}

function submitMealBuilder()
{
	var currentFood = document.subFormUserInfo.Food.options[document.subFormUserInfo.Food.selectedIndex].text;
	for(i=0; i< foodItem.length; i++)
	{
		var temp = foodItem[i][1].trim() + " (" + foodItem[i][2].trim() + ")";
		var temp2 = foodItem[i][1] + " (" + foodItem[i][2] + ")";
		if(temp == currentFood | temp2 == currentFood)
		{
			document.subFormUserInfo.chosenFoodID.value = foodItem[i][5];
		}
	}
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


function submitMealBuilder2()
{
	var currentFood = document.subFormUserInfo.Food.options[document.subFormUserInfo.Food.selectedIndex].text.trim();
	alert('"' + currentFood + '"');
	var found = false;
	for(i=0; i< foodItem.length; i++)
	{
		var temp = foodItem[i][1].trim() + " (" + foodItem[i][2].trim() + ")";
		var temp2 = foodItem[i][1] + " (" + foodItem[i][2] + ")";
		if(i==15)
		{
			alert('"' + currentFood + '" = \r\n"' + temp + '"\r\nUntrimmed\r\n"' + currentFood + '" = \r\n"' + foodItem[i][1] + " (" + foodItem[i][2] + ")");
		}
		if(temp == currentFood | temp2 == currentFood)
		{
			document.subFormUserInfo.chosenFoodID.value = foodItem[i][5];
			found = true;
		}
	}
	alert(found);
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

function deleteMealItem(ID)
{
	document.subFormUserInfo.chosenFoodID.value = ID;
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

function changeCalories()
{
	additionalCalories = parseInt(document.subFormUserInfo.AdditionalCalories.value);
	plateCalories = parseInt(document.subFormUserInfo.PlateCalories.value);
	snackBarCalories = 0;
	if(document.subFormUserInfo.SnackBar[0].checked == true)
	{
		snackBarCalories = parseInt(document.subFormUserInfo.SnackBarCalories.value);
	}
	
	if(additionalCalories.toString() == 'NaN' | additionalCalories == '')
	{
		additionalCalories = 0;
	}
	if(plateCalories.toString() == 'NaN' | plateCalories == '')
	{
		plateCalories = 0;
	}
	document.getElementById('totalCaloriesSpan').innerHTML = (additionalCalories + plateCalories + snackBarCalories);
}

function showHideSnackBarSelect()
{
	var showSnackBar = 'block';
	if(document.subFormUserInfo.SnackBar[1].checked == true)
	{
		showSnackBar = 'none';
		document.subFormUserInfo.SnackBarCalories.value = 0;
	}	
	else
	{
		changeSnackBar();
	}
	document.getElementById('trSnackBar').style.display = showSnackBar;
	document.getElementById('trSnackBarCalories').style.display = showSnackBar;
	changeCalories();
}

function changeSnackBar()
{
	snackBarCalories = 0;
	snackBarID = -1;
	if(document.subFormUserInfo.SnackBar[0].checked == true)
	{
		var currentBar = document.subFormUserInfo.snackBarSelect.options[document.subFormUserInfo.snackBarSelect.selectedIndex].text;	
		for(i=0; i< snackBarItem.length; i++)
		{
			if(currentBar == snackBarItem[i][1])
			{
				snackBarCalories = snackBarItem[i][2];
				snackBarID = snackBarItem[i][0];
				break;
			}
		}
		if(snackBarCalories == -1 | snackBarCalories == '')
		{
			snackBarCalories = 0;
		}
	}
	document.getElementById('snackBarCaloriesSpan').innerHTML = snackBarCalories;
	document.subFormUserInfo.SnackBarCalories.value = snackBarCalories;
	document.subFormUserInfo.SnackBarID.value = snackBarID;
	
	changeCalories();
}

function setSnackBar()
{
	snackBarID = document.subFormUserInfo.SnackBarID.value;
	if(snackBarID != -1)
	{
		for(i=0; i< snackBarItem.length; i++)
		{
			if(snackBarItem[i][0] == snackBarID)
			{
				document.subFormUserInfo.snackBarSelect.selectedIndex = i;
				break;
			}
		}
	}
}

function showhidePW()
{	
	if(document.getElementById('btnShowHidePassword').value == 'Display')
	{
		document.getElementById('btnShowHidePassword').value = 'Hide';
		document.getElementById('showPassword').style.display = 'inline';
		document.getElementById('hidePassword').style.display = 'none';
	}
	else
	{
		document.getElementById('btnShowHidePassword').value = 'Display';
		document.getElementById('showPassword').style.display = 'none';
		document.getElementById('hidePassword').style.display = 'inline';
	}
	
}

function setDesiredUsername()
{
	document.getElementById('LoginName').value = document.getElementById('DesiredUserName').value
}

function editHeadline(headlineID)
{
	document.getElementById('userInfo').src = "/meta/editHeadline.php?ID=" + headlineID;
	document.getElementById('userInfo').focus();
}

function deleteHeadline(headlineID)
{
	var answer = confirm("Are you sure you want to delete this headline?");
	if (answer)
	{
		document.subFormUserInfo.headlineID.value = headlineID;
		document.subFormUserInfo.submitForm.value = "delete";
		document.subFormUserInfo.submit();
	}
}

function submitHeadline()
{
	document.subFormUserInfo.submitForm.value = 'toProcess';
	document.subFormUserInfo.submit();
}

function moreLessDetail(ID)
{
	if(document.getElementById('detailText' + ID).innerText == '+ More Information')
	{
		document.getElementById('desc' + ID).style.display = "inline";
		document.getElementById('detailText' + ID).innerText = '- Hide details';
	}
	else
	{
		document.getElementById('desc' + ID).style.display = "none";
		document.getElementById('detailText' + ID).innerText = '+ More Information';
	}
}

function showHideMovie(ID, Location)
{
	if(document.getElementById('detailText' + ID).innerText == '+ Show Media')
	{
		document.getElementById('desc' + ID).style.display = "inline";
		document.getElementById('MediaPlayer' + ID).src = Location;
		document.getElementById('MediaPlayer' + ID).filename = Location;
		document.getElementById('MediaPlayer' + ID).autostart = true;
		document.getElementById('detailText' + ID).innerText = '- Hide Media';
	}
	else
	{
		document.getElementById('desc' + ID).style.display = "none";
		document.getElementById('MediaPlayer' + ID).src = '';
		document.getElementById('MediaPlayer' + ID).filename = '';
		document.getElementById('MediaPlayer' + ID).autostart = false;
		document.getElementById('detailText' + ID).innerText = '+ Show Media';
	}
}



