
//########################################################################################
// HelpMeCook Recipe Definitions
//########################################################################################

// hmcRecipe class
function hmcRecipe() {
  this.key = null;
  this.title = '';
  this.size = '';
  this.pageTitle = '';
  this.sourceKey = null;
  this.status = null;
  this.imagePath = 'NONE';
  this.categoryList = new Array();
  this.ingredientList = new Array();
  this.instructionList = new Array();
  this.noteList = new Array();
  this.recipeLinkList = new Array();
}

// hmcRecipeCategory class
function hmcRecipeCategory() {
  this.key = null;
  this.name = null;
}

// hmcRecipeSource class
function hmcRecipeSource() {
  this.key = null;
  this.title = null;
  this.file = null;
}

// hmcRecipeIngredient class
function hmcRecipeIngredient() {
  this.key = null;
  this.sequence = null;
  this.size = null;
  this.detail = null;
}

// hmcRecipeInstruction class
function hmcRecipeInstruction() {
  this.key = null;
  this.sequence = null;
  this.detail = null;
}

// hmcRecipeNote class
function hmcRecipeNote() {
  this.key = null;
  this.sequence = null;
  this.subCat = null;
  this.detail = null;
}

// hmcRecipeLink class
function hmcRecipeLink(argKey,argTitle) {
  this.recipeKey = argKey;
  this.recipeTitle = argTitle;
}

//########################################################################################
//########################################################################################
// HelpMeCook Recipe Functions
//########################################################################################
//########################################################################################


function hmcRecipeParse(argRecipeElement) {
  var x;
  var recipe = new hmcRecipe();
  recipe.key = parseInt(gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:key'));
  recipe.title = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:title');
  recipe.pageTitle = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:pageTitle');
  recipe.imagePath = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:imagePath');
  recipe.size = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:size');
  recipe.status = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:status');
  recipe.source = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:source');
  recipe.sourceKey = gladstoneXMLGetChildElementValue(argRecipeElement,'hmc:sourceKey');

  // get any linked recipes
  var rcpLinks = gladstoneXMLGetChildElementsByName(argRecipeElement,'hmc:recipeLink');
  for( x=0; x < rcpLinks.length; x++ ) {
    var lnk = rcpLinks[x];
    var lnkKey= gladstoneXMLGetChildElementValue(lnk,'hmc:key');
    var lnkTitle = gladstoneXMLGetChildElementValue(lnk,'hmc:title');
    recipe.recipeLinkList.push( new hmcRecipeLink(lnkKey,lnkTitle));
  }

  // categories
  var catlist = gladstoneXMLGetChildElementsByName(argRecipeElement,'hmc:category');
  for( x=0; x < catlist.length; x++ ) {
    var c = catlist[x];
    var category = new hmcRecipeCategory();
    category.key = gladstoneXMLGetChildElementValue(c,'hmc:key');
    category.name = gladstoneXMLGetChildElementValue(c,'hmc:name');
    recipe.categoryList.push(category);
  }
  
  // ingredients
  var inglist = gladstoneXMLGetChildElementsByName(argRecipeElement,'hmc:ingredient');
  for( x=0; x < inglist.length; x++ ) {
    var g = inglist[x];
    var ingredient = new hmcRecipeIngredient();
    ingredient.size = gladstoneXMLGetChildElementValue(g,'hmc:amount');
    ingredient.detail = gladstoneXMLGetChildElementValue(g,'hmc:detail');
    ingredient.sequence = parseInt(gladstoneXMLGetChildElementValue(g,'hmc:sequence'));
    recipe.ingredientList.push(ingredient);
  }
  if ( recipe.ingredientList.length > 1 ) {
    recipe.ingredientList.sort(hmcRecipeCompareSeqItems);
  }

  // instructions
  var inslist = gladstoneXMLGetChildElementsByName(argRecipeElement,'hmc:instruction');
  for( x=0; x < inslist.length; x++ ) {
    var ins = inslist[x];
    var instruction = new hmcRecipeInstruction();
    instruction.detail = gladstoneXMLGetChildElementValue(ins,'hmc:detail');
    instruction.sequence = parseInt(gladstoneXMLGetChildElementValue(ins,'hmc:sequence'));
    recipe.instructionList.push(instruction);
  }
  if ( recipe.instructionList.length > 1 ) {
    recipe.instructionList.sort(hmcRecipeCompareSeqItems);
  }

  // notes
  var notelist = gladstoneXMLGetChildElementsByName(argRecipeElement,'hmc:note');
  for( x=0; x < notelist.length; x++ ) {
    var n = notelist[x];
    var note = new hmcRecipeNote();
    note.detail = gladstoneXMLGetChildElementValue(n,'hmc:detail');
    note.sequence = parseInt(gladstoneXMLGetChildElementValue(n,'hmc:sequence'));
    note.subCat = gladstoneXMLGetChildElementValue(n,'hmc:subCat');
    recipe.noteList.push(note);
  }
  if ( recipe.noteList.length > 1 ) {
    recipe.noteList.sort(hmcRecipeCompareSeqItems);
  }
  return recipe;
}


function hmcRecipeCompareSeqItems(itemA,itemB) {
  if ( itemA.sequence < itemB.sequence ) {
    return -1;
  } else {
    return 1;
  }
}

function hmcRecipeBuildDetailDisplay(argRecipe) {
  var x;
  // ingredients
  var buf = '';
  buf += '<DIV CLASS=\"RecipeIngredientBlock\">';
  buf += '<DIV CLASS=\"IngredientListTitle\">Ingredients</DIV>';
  buf += '<DIV CLASS=\"IngredientList\">';
  
  if ( argRecipe.imagePath  && (argRecipe.imagePath != 'NONE') && (argRecipe.imagePath.charAt(0) == '/') ) {
    buf += '<TABLE CELLSPACING=\"0px\" CELLPADDING=0 BORDER=0 width="100%">';
    buf += '<TR><TD valign="top">';
  }

  buf += '<TABLE CELLSPACING=\"4px\" CELLPADDING=0 BORDER=0>';
  var subcnt = 0;
  var detail;
  var convDetail;
  var seq;
  for( x=0; x < argRecipe.ingredientList.length; x++ ) {
    var g = argRecipe.ingredientList[x];
    var amt = '';
    if ( g.size ) {
      amt = g.size;
    }
    convDetail = hmcRecipeCheckForLinks(g.detail,argRecipe.recipeLinkList);
    if ( amt == 'SUBHEADER' ) {
      subcnt++;
      buf += '<TR><TD ALIGN=CENTER>*</TD><TD CLASS="s7">'  + convDetail + '</TD></TR>';
    } else {
      buf += '<TR><TD ALIGN=CENTER CLASS=\"IngredientDot\">*</TD><TD CLASS=\"Ingredient\">' + amt + ' ' + convDetail + '</TD></TR>';
    }
  }
  buf += '</TABLE>';

  if ( argRecipe.imagePath  && (argRecipe.imagePath != 'NONE') && (argRecipe.imagePath.charAt(0) == '/') ) {
    buf += '</TD><TD WIDTH="200px" VALIGN="TOP"><IMG BORDER=0 SRC="/hmcui/public/' + argRecipe.imagePath + '"></TD></TR></TABLE>';
  }
  buf += '</DIV></DIV>';

  buf += '<DIV><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="10px"></DIV>';
  buf += '<DIV CLASS=\"RecipeInstructionBlock\">';
  buf += '<DIV CLASS=\"InstructionListTitle\">Instructions</DIV>';
  buf += '<DIV CLASS=\"InstructionList\">';
  buf += '<TABLE CELLSPACING=\"8px\" CELLPADDING=0 BORDER=0>';

  var gift_flag = 0;
  for( x=0; x < argRecipe.instructionList.length; x++ ) {
      var ins = argRecipe.instructionList[x];
      convDetail = hmcRecipeCheckForLinks(ins.detail,argRecipe.recipeLinkList);
      if ( convDetail == 'GIFT' ) {
        buf += '<TR><TD COLSPAN=2 WIDTH=5>&nbsp;</TD></TR>';
        buf += '<TR><TD WIDTH=5>&nbsp;</TD><TD ALIGN=LEFT ><TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0>';
        buf += '<TR><TD WIDTH=40 VALIGN=TOP ALIGN=CENTER><IMG SRC="/cwkpublic/public/images/giftbox.gif"  WIDTH=30 HEIGHT=40 BORDER=0></TD>';
        buf += '<TD ALIGN=LEFT CLASS=s2><SPAN CLASS=s36>Makes A Great Gift:</SPAN><BR>';
        gift_flag = 1;
      } else if ( gift_flag == 0 ) {
         buf += '<TR><TD ALIGN=CENTER CLASS=\"IngredientDot\">' + ins.sequence + '</TD><TD CLASS=\"Ingredient\">' + convDetail + '</TD></TR>'
      } else {
         buf += '<TR><TD ALIGN=CENTER CLASS=\"IngredientDot\">&nbsp</TD><TD CLASS=\"Ingredient\">' + convDetail + '</TD></TR>'
      }
    }
    if ( gift_flag == 1 ) {
        buf += '</TD></TR></TABLE></TD></TR>';
    }
    buf += '</TABLE></DIV></DIV>';

  // notes
  var fflag = 0;
  var curr_subcat = "";
  var actflag = 0;
    if ( argRecipe.size != 'NONE' ) {
        buf += '<SPAN CLASS="RecipeSize">Makes ' + argRecipe.size + '</SPAN>';
    }
    if ( (argRecipe.noteList.length > 0) ) {
      buf += '<DIV><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="10px"></DIV>';
      buf += '<DIV CLASS=\"RecipeNoteBlock\">';
      buf += '<DIV CLASS=\"RecipeNoteListTitle\">Notes</DIV>';
      buf += '<DIV CLASS=\"RecipeNoteList\">';
//      buf += '<TABLE CELLSPACING=\"8px\" CELLPADDING=0 BORDER=0>';
      for( x=0; x < argRecipe.noteList.length; x++ ) {
        var n = argRecipe.noteList[x];
        convDetail = hmcRecipeCheckForLinks(n.detail,argRecipe.recipeLinkList);
        var subcat = n.subCat;
        if ( (subcat) && (subcat != curr_subcat) ) {
            if ( actflag == 1 ) {
                buf += '</UL>';
                actflag = 0;
            }
            curr_subcat = subcat;
            if ( curr_subcat != 'NONE' ) {
                buf += '<B>' + curr_subcat + '</B>';
            }
            buf += '<UL>';
            actflag = 1;
        }            
        buf += '<LI CLASS=\"Ingredient\">' + convDetail;
      }
     if ( actflag == 1 ) {
          buf += '</UL><P>';
          actflag = 0;
     }
     buf += '</DIV>';
     buf += '</DIV>';
    }

// source
  if ( (argRecipe.source) && (argRecipe.source != 'helpmecook') ) {
   buf += '<DIV><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="10px"></DIV>';
    buf += '<DIV CLASS=\"RecipeSourceBlock\">';
     buf += '<DIV CLASS=\"RecipeSourceTitle\">Source</DIV>';
     buf += '<DIV CLASS=\"RecipeSourceDetail\">';
     buf +=  argRecipe.source;
     buf += '</DIV></DIV>';
  }
  
  return buf;    
}

function hmcRecipeCheckForLinks(argText,argLinkList) {
  var offset = 0;
  var x,y,z;
  var outputBuffer = '';
  while( (x = argText.indexOf("@@",offset)) != -1 ) {
    y = argText.indexOf("@@",x+2);
    var rkey = argText.substring(x+2,y);
    var rtitle = null;
    for( z=0; z < argLinkList.length; z++ ) {
      if ( argLinkList[z].recipeKey == rkey ) {
        rtitle = argLinkList[z].recipeTitle;
        break;
      }
    }
    // move the text up to the link to the output buffer
    outputBuffer += argText.substring(offset,x);
    if ( rtitle == null ) {
      outputBuffer += argText.substring(x,y+2);  // no match found so leave raw link
    } else {
      outputBuffer += '<A HREF="javascript:hmcClickedRecipe(' + rkey + ');">' + rtitle + '</A>';
    }
    offset = y + 2;
  }
  outputBuffer += argText.substring(offset);
  return outputBuffer;
}


function hmcRecipeCopy(argRecipe) {
  var recipe = new hmcRecipe();
  recipe.key = argRecipe.key;
  recipe.title = argRecipe.title;
  recipe.size = argRecipe.size;
  recipe.pageTitle = argRecipe.pageTitle;
  recipe.sourceKey = argRecipe.sourceKey;
  recipe.status = argRecipe.status;
  recipe.imagePath = argRecipe.imagePath;
  var x;
  for( x=0; x < argRecipe.categoryList.length; x++ ) {
    recipe.categoryList.push( hmcRecipeCategoryCopy(argRecipe.categoryList[x]) );
  }

  for( x=0; x < argRecipe.ingredientList.length; x++ ) {
    recipe.ingredientList.push( hmcRecipeIngredientCopy(argRecipe.ingredientList[x]) );
  }

  for( x=0; x < argRecipe.instructionList.length; x++ ) {
    recipe.instructionList.push( hmcRecipeInstructionCopy(argRecipe.instructionList[x]) );
  }
  
  for( x=0; x < argRecipe.noteList.length; x++ ) {
    recipe.noteList.push( hmcRecipeNoteCopy(argRecipe.noteList[x]) );
  }
  return recipe;  
}


function hmcRecipeCategoryCopy(argCategory) {
  var cat = new hmcRecipeCategory();
  cat.name = argCategory.name;
  cat.key = argCategory.key;
  return cat;
}

function hmcRecipeIngredientCopy(argIngredient) {
  var ing = new hmcRecipeIngredient();
  ing.key = argIngredient.key;
  ing.sequence = argIngredient.sequence;
  ing.size = argIngredient.size;
  ing.detail = argIngredient.detail;
  return ing;
}

function hmcRecipeInstructionCopy(argInstruction) {
  var ins = new hmcRecipeInstruction();
  ins.key = argInstruction.key;
  ins.sequence = argInstruction.sequence;
  ins.detail = argInstruction.detail;
  return ins;
}


function hmcRecipeNoteCopy(argNote) {
  var note = new hmcRecipeNote();
  note.key = argNote.key;
  note.sequence = argNote.sequence;
  note.detail = argNote.detail;
  note.subCat = argNote.subCat;
  return note;
}


//########################################################################################
// Printing Functions
//########################################################################################
var hmcRecipePrintWindow;
var hmcRecipePrintBuffer;

function hmcRecipePrint(argRecipe) {

  var title = argRecipe.pageTitle;

  var buffer = '';
  buffer += '<HTML><HEAD><link rel=stylesheet type=\"text/css\" HREF=\"/hmcui/public/hmcstyle.css\" Title=\"Style\">';
  buffer += '<script  type=\"text/javascript\">\n';
  buffer += 'function hmcPrintPage() {\n';
  buffer += 'var pd = document.getElementById(\'hmcPrintLink\');\n';
  buffer += 'pd.style.display = \'none\';\n';
  buffer += 'window.print();\n';
  buffer += 'pd.style.display = \'block\';\n';
  buffer += '}</script></head>';
  buffer += '<body style="margin:10px; padding:0px; backgroundColor:#FFFFFF;">';
  buffer += '<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">';
  buffer += '<tr><td><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="5px"></td></tr>';
  buffer += '<tr><td align=right><a id=\"hmcPrintLink\" href=\"javascript:hmcPrintPage();\">Print Page</a></td></tr>';
  buffer += '<tr><td><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="5px"></td></tr>';
  buffer += '<tr><td align=left><IMG BORDER=0 SRC="/hmcui/public/images/hmc_logo.jpg"></td></tr>';
  buffer += '<tr><td><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="5px"></td></tr>';
  buffer += '<tr><td align=\"center\" class=\"PrintPageTitle\">' + title + '</td></tr>';
  buffer += '<tr><td><IMG BORDER=0 SRC="/cwkpublic/public/images/transpace.gif" WIDTH="10px" HEIGHT="5px"></td></tr>';

  buffer += '<tr><td>' + hmcRecipeBuildDetailDisplay(argRecipe) + '</td></tr>';
  buffer += '</table></BODY></HTML>';

  
  // open a new window
  hmcRecipePrintBuffer = buffer;
  hmcRecipePrintWindow = window.open('','hmcReportPrintableView','height=600,width=700,scrollbars,resizable,titlebar');
  setTimeout('hmcRecipePrintWindowOpen();', 50);  // have to wait to make sure window opened
}


//*****************************************************************************
// hmcRecipePrintWindowOpen
// Output the printable version in the new window
//*****************************************************************************
function hmcRecipePrintWindowOpen() {
  hmcRecipePrintWindow.document.write(hmcRecipePrintBuffer);
  hmcRecipePrintWindow.document.close();  
  hmcRecipePrintWindow.focus();
}



