//<!--

var currItem;

var updateSteps = function(obj){
 var questions = new Array('PublicUserIsAllergic','PublicUserHasAilment','PublicUserAskWeight','PublicUserMonitorsNutrition','PublicUserTakesVitamins','PublicUserTakesMedication')
 var intStep = 1;
for(i=0;i<questions.length;i++){
 oObj = eval('document.personal_info.'+questions[i]);
 for(j=0;j<oObj.length;j++){
  if(oObj[j].checked){
   if(oObj[j].value == 1){
    intStep = intStep + 1;
   }
   else{
    //intStep = intStep - 1;
   }
  }
 }
}
 
 document.getElementById('divStepCount').innerText = intStep
}



var toggleEmergencyContactsView = function(v){
 var mode;
 
 if(v.toUpperCase() == 'EDIT'){
  mode = "edit";
  document.getElementById('divEmergencyContactsView').innerHTML = 'Finished';
  document.getElementById('divEmergencyContactsView').onclick = function(){
   toggleEmergencyContactsView('view');
  }
 }
 else{
  mode = "view";
  document.getElementById('divEmergencyContactsView').innerHTML = '<img src="/images/iconEdit.gif" width="19" height="16" alt="Edit">Edit';
  document.getElementById('divEmergencyContactsView').onclick = function(){
   toggleEmergencyContactsView('edit');
  }
 }
 
 ColdFusion.navigate('dspEmergencyContacts.cfm?mode='+mode,'divEmergencyContacts');
}




var saveEmergencyContacts = function(){
  ColdFusion.Ajax.submitForm("frmEmergencyContacts","actSaveEmergencyContacts.cfm",returnEmergencyContacts);
}



var returnEmergencyContacts = function(r){
 if(isNumeric(r.trim())){
  ColdFusion.navigate('/profile/dspEmergencyContacts.cfm?saved=true','divEmergencyContacts');
  toggleEmergencyContactsView('VIEW');
 }
 else{
  alert(r.trim());
 }
}



var toggleAlertView = function(v){
 var mode;
 
 if(v.toUpperCase() == 'EDIT'){
  mode = "edit";
  document.getElementById('divAlertView').innerHTML = 'Finished';
  document.getElementById('divAlertView').onclick = function(){
   toggleAlertView('view');
  }
 }
 else{
  mode = "view";
  document.getElementById('divAlertView').innerHTML = 'Edit';
  document.getElementById('divAlertView').onclick = function(){
   toggleAlertView('edit');
  }
 }
 
 ColdFusion.navigate('dspEmailAndAlerts.cfm?mode='+mode,'divEmailAlerts');
}



var toggleSubAccountView = function(v){
 var mode;
 
 if(v.toUpperCase() == 'EDIT'){
  mode = "edit";
  document.getElementById('divSubAccountView').innerHTML = 'Finished';
  document.getElementById('divSubAccountView').onclick = function(){
   toggleSubAccountView('view');
  }
 }
 else{
  mode = "view";
  document.getElementById('divSubAccountView').innerHTML = 'Add a Sub-Account';
  document.getElementById('divSubAccountView').onclick = function(){
   toggleSubAccountView('edit');
  }
 }
 
 ColdFusion.navigate('dspSubAccounts.cfm?mode='+mode,'divSubAccounts');
}



var togglePersonalNotesView = function(v){
 var mode;
  
 if(v.toUpperCase() == 'EDIT'){
  mode = "edit";
  document.getElementById('divPersonalNotesView').innerHTML = 'Cancel';
  document.getElementById('divPersonalNotesView').onclick = function(){
   togglePersonalNotesView('view');
  }
 }
 else{
  mode = "view";
  document.getElementById('divPersonalNotesView').innerHTML = '<img src="/images/iconEdit.gif" width="19" height="16" alt="Edit">Edit';
  document.getElementById('divPersonalNotesView').onclick = function(){
   togglePersonalNotesView('edit');
  }
 }
 
 ColdFusion.navigate('dspPersonalNotes.cfm?mode='+mode,'divPersonalNotes');
}


var savePersonalNotes = function(){
 ColdFusion.Ajax.submitForm("frmPersonalNotes","actSavePersonalNotes.cfm",returnPersonalNotes);
}

var returnPersonalNotes = function(r){
 if(isNumeric(r.trim())){
  ColdFusion.navigate('/profile/dspPersonalNotes.cfm?saved=true','divPersonalNotes');
  togglePersonalNotesView('VIEW');
 }
 else{
  alert(r.trim());
 }
}


var toggleFoodAvoidanceView = function(v){
 var mode;
 if(v.toUpperCase() == 'EDIT'){
  mode = "edit";
  document.getElementById('divFoodAvoidanceView').innerHTML = 'Cancel';
  document.getElementById('divFoodAvoidanceView').onclick = function(){
   toggleFoodAvoidanceView('view');
  }
  document.location.href = '/profile/avoidance_setup.cfm';
 }
 else{
  mode = "view";
  document.getElementById('divFoodAvoidanceView').innerHTML = '<img src="/images/iconEdit.gif" width="19" height="16" alt="Edit">Edit';
  document.getElementById('divFoodAvoidanceView').onclick = function(){
   toggleFoodAvoidanceView('edit');
  }
  ColdFusion.navigate('/profile/dspAvoidanceList.cfm','divFoodAvoidance');
 }
}


var directlyAddCategory = function(iCat){
 oIngredients = new Ingredients();
 oIngredients.addCategoryToAvoidanceList(iCat);
 document.location.href = '/profile/avoidance_setup.cfm';
}


var addCategoriesToAvoidance = function(t){
 if(!t){
  var t = 'profile';
 }
 var oCategories = document.frmIngredientCategories.chkAvoidanceCategory;
 var lstCategories = "";
 if(oCategories.length){
  for(i=0;i<oCategories.length;i++){
   if(oCategories[i].checked){
    lstCategories = lstCategories + oCategories[i].value + ',';
   }
  }
 }
 
 if(lstCategories.trim() != ''){
  lstCategories = lstCategories + '-1';
  oIngredients = new Ingredients();
  oIngredients.addCategoryToAvoidanceList(lstCategories);
 }
 
 switch(t.toUpperCase()){
  case 'SIGNUP':
   document.location.href = '/profile/avoidance_setup.cfm';
   break;
  default:
   for(i=0;i<oCategories.length;i++){
    if(oCategories[i].checked){
     toggleCategoryCheckBoxByVal(true,oCategories[i].value);
    }
   }
   ColdFusion.navigate('/profile/dspLoading.cfm','divAvoidanceList');
   reloadAvoidance();
 }
}


var addSpecificAvoidance = function(type){
 var bStandard = false;
 
 var oIngs = document.frmSpecificIngredients.chkSpecificIngredient;
 var lstIngs = "";

 if(oIngs.length){
  for(i=0;i<oIngs.length;i++){
   if(oIngs[i].checked){
    lstIngs = lstIngs + oIngs[i].value + ',';
   }
  }
 }
 
 if(lstIngs.trim() == ''){
  if(document.getElementById('chkSpecificIngredient')){
   if(document.getElementById('chkSpecificIngredient').checked){
    lstIngs = document.getElementById('chkSpecificIngredient').value + ',';
   }
  }
 }
  
 if(lstIngs.trim() != ''){
  lstIngs= lstIngs + '-1';
  oIngredients = new Ingredients();
  oIngredients.addToAvoidanceList(lstIngs);
 }

 switch(type){
  case "glossary":
   handler = addViaGlossary();
   break;
  default:
   toggleIngredientCheckBox(false);
   ColdFusion.navigate('/profile/dspListAvoidedItems.cfm?category=0','divAvoidanceList');
 }
}

var addViaGlossary = function(){
 ColdFusion.navigate('dspIngredientDefinition.cfm?ingredient='+document.getElementById('chkSpecificIngredient').value,'divIngredientDefinition');
}

var toggleCategoryCheckBox = function(s){
 var l = document.frmIngredientCategories.chkAvoidanceCategory;
 if(l.length){
  for(var i=0;i<l.length;i++){
   if(l[i].checked){
    l[i].className = s ? '' : 'hidden';
   }
  }
 }
}

var toggleCategoryCheckBoxByVal = function(s,c){
 var b = document.frmIngredientCategories.chkAvoidanceCategory;
 document.getElementById('div'+c).className = s ? '' : 'hidden';
 if(b.length){
  for(var i=0;i<b.length;i++){
   if(b[i].value == c){
	b[i].className = s ? 'hidden' : '';
	b[i].checked = false;
   }
  }
 }
}

var toggleIngredientCheckBox = function(s){
 var l = document.frmSpecificIngredients.chkSpecificIngredient;
 if(l.length){
  for(var i=0;i<l.length;i++){
   if(l[i].checked){
    l[i].parentNode.className = s ? '' : 'hidden';
   }
  }
 }
}

var toggleIngredientCheckBoxByIngredient = function(s,g){
 var l = document.getElementById('frmSpecificIngredients');
 if(l){
  l = document.frmSpecificIngredients.chkSpecificIngredient;
  if(l.length){
   for(var i=0;i<l.length;i++){
    if(l[i].value == g){
     l[i].parentNode.className = s ? '' : 'hidden';
    }
   }
  }
 }
}

var toggleIngredientCheckBoxByCategory = function(s,c,h){
 if(document.frmSpecificIngredients){
  var d = document.frmSpecificIngredients.getElementsByTagName("div");
  if(d.length){
   //if(h)toggleCategoryCheckBoxByVal(s,c);
   for(var i=0;i<d.length;i++){
    if(d[i].id.indexOf('_'+c) != -1){
     d[i].className = s ? '' : 'hidden';
    }
   }
  }
 }
}


var reloadAvoidance = function(r){
 if(document.getElementById('divAvoidanceList')){
  ColdFusion.navigate('/profile/dspListAvoidedItems.cfm','divAvoidanceList');
 }
 else{
  document.location.href = '/profile/avoidance_setup.cfm';
 }
}

var removeAvoidance = function(intUser,intID,intCat,type,passCat){
 if(!type){
  type = "regular";
 }
 if(!passCat){
  var passCat = 0;
 }
 oIngredients = new Ingredients();
 oIngredients.deleteAvoidance(intUser,intID,intCat);
 switch(type){
  case "glossary":
   addViaGlossary();
   break;
  case "specific":
   ColdFusion.navigate('/profile/dspListAvoidedItems.cfm?category='+passCat,'divAvoidanceList');
   break; 
  default:
    ColdFusion.navigate('/profile/dspListAvoidedItems.cfm','divAvoidanceList');
 }
}

var resetCount = function(){
 ColdFusion.navigate('/profile/dspAvoidanceCount.cfm','divAvoidanceCount');
}


var addToFavorites = function(i,r,t){
 if(!t){
  t = "ITEM";
 }

 if(i <= 0 && r > 0){
  oRecipe = new Recipes();
  oRecipe.addFavoriteRecipe(r);
 }
 
 if(i > 0 && r <= 0){
  oProfile = new Profile();
  oProfile.addFavoriteItem(i);
 }
 
 switch(t.toUpperCase()){
  case "SEARCH":
   ColdFusion.navigate('/members/dspFavoritesButton.cfm?item='+i+'&recipe='+r+'&pageType=Search','divFavButton'+i+'_'+r);
   break;
  case "SEARCHRESULTS":
   alert("The item has been added to your favorites list.");
   break;
  default:
   delayedFavoriteReload(i,r);
 }
}

var removeFromFavorites = function(i,t){
  if(!t){
   t = "ITEM";
  }
  
  var bRemove = confirm("Are you sure you want to remove this item from your favorites list?");
  
  if(bRemove){
   oProfile = new Profile();
   oProfile.removeFavoriteItem(i);
   switch(t.toUpperCase()){
    case "SEARCH":
     ColdFusion.navigate('/members/dspFavoritesButton.cfm?item='+i+'&recipe=0'+'&pageType=Search','divFavButton'+i);
     break;
    case "PROFILE":
     ColdFusion.navigate('/profile/dspFavoriteFoods.cfm','divFavoriteFoods');
     break;
    default:
     delayedFavoriteReload(i,0);
   }
  }
}

var refreshFavoritesButton = function(i,r){
  setTimeout('delayedFavoriteReload('+i+','+r+')',1000);
}

var delayedFavoriteReload = function(i,r){
 ColdFusion.navigate('/members/dspFavoritesButton.cfm?item='+i+'&recipe='+r,'divFavoritesButton');
}


var insertProfilePublicSettings = function(){
 var oCheck = document.frmProfileSettings.chkShowPublic;
 var oProfile = new Profile();
 var strMyList = '';
 if(oCheck){
  for(i=0;i<oCheck.length;i++){
   if(!oCheck[i].checked){
    strMyList = strMyList + oCheck[i].value + ',';
   }
  }
 }
 strMyList = strMyList + 'x';
 oProfile.updateHiddenSettings(strMyList);
 document.location.href = '/profile/index.cfm?msg=1';
 
}


var removeRecipeFromFavorite = function(r,t){
 if(!t){
  var t = "profile";
 }
 
 var bRemove = confirm("Are you sure you want to remove this recipe from your favorites list?");
 
 if(bRemove){
  var oRecipe = new Recipes();
  oRecipe.removeFavoriteRecipe(r);
 
  switch(t.toUpperCase()){
   case "ITEM":
    delayedFavoriteReload(0,r);
	break;
   default:
    ColdFusion.navigate('/profile/dspFavoriteRecipes.cfm','divFavoriteRecipes');
  }
 }
}

var reportProfile = function(p){
 var bConfirm = confirm("Are you sure you would like to report this profile as offensive? An email will be sent to the FoodFacts staff containing your name and email address, as well as a link to the offensive profile.");
 
 if(bConfirm){
  oProfile = new Profile();
  oProfile.reportProfile(p);
  alert("Thank you, the offensive profile has been reported.");
 }
}


var launchLoadingMessage = function(){
 ColdFusion.Window.create('wndLoading','Loading...','loadingMessage.html',{modal:true,closable:false,draggable:false,resizable:false,center:true,initshow:false});
 ColdFusion.Window.show('wndLoading');
}


var hideLoadingMessage = function(){
 ColdFusion.Window.hide('wndLoading');
}

var photoUploadStatus = function(t){
 if(t.trim() == "Photo uploaded successfully!"){
  ColdFusion.Window.destroy('wndBrowseImages',true); 
  ColdFusion.navigate('/profile/dspProfileImage.cfm','divProfileImage');
 }
 else{
  alert(t);
 }
}


var launchOptOut = function(){
  oProfile = new Profile();
  if(oProfile.getNaturalFlavoringsOptOut() == 0){
    openAJAXModal('wndOptOut','/display/dspFlavorOptOut.cfm',600,450);
  }
}


var setOptOut = function(){
  oProfile = new Profile();  
  var bOptOut = true;
   
  for(i=0;i<document.frmOptOut.chkOptOut.length;i++){
   if(document.frmOptOut.chkOptOut[i].checked){
    if(document.frmOptOut.chkOptOut[i].value == "false"){
	  bOptOut = false;
	}
	else{
	  bOptOut = true;
	}
   }
  }
  
  oProfile.setNaturalFlavoringsOptOut(bOptOut);
  destroyWindow('wndOptOut');
}

//-->