// JavaScript buildWeddingList for normal display
/*
AUTHOR: Dennis Benedetto
DATE: 6/20/09
*/
<!--
js_allItemsArray = new Array();//global variable

Array.prototype.multiSort = function(index){
// Written By: WillyDuitt@hotmail.com | 03-10-2005 \\;
for(var i=0; i<this.length; i++){
var temp = this[i].splice(index,1);
this[i].unshift(temp);
} return this.sort();
}
Array.prototype.findIndexByCol = function(value,cIdx){
var ctr = "";
for (var i=0; i < this.length; i++) {
// use === to check for Matches. ie., identical (===), ;
if (this[i][cIdx] == value) {
//alert(this[i][cIdx]+"===="+value);
return i;
}
}
return ctr;
};
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}
function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

expda = 180          //set a general days to expiration for cookies
function roll_over(img_name, img_src)
{
   document[img_name].src = img_src;
}

//cookie scripts from:  http://www.quirksmode.org/
function SetCookie(name,value,days)
{
   if (days)
   {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   //document.cookie = name+"="+value+expires+"; path=/";  //defaults to domain
   document.cookie = name+"="+value+expires;  //no path defaults to full path
}
testInt = 0;
function db_formatDate (x)
{
//format date from yyyymmdd to mm/dd/yyyy
  var formattedDate;
  x = x.toString().trim();
  if (x.length != 8)
  {
    formattedDate = x;
  }
  else
  {
    formattedDate = x.substring(4,6) + "/" + x.substring(6,8) + "/" + x.substring(0,4);
  }
  return formattedDate;
}

function buildFullCookieArray()
{
  //build array of both template and custom cookie values into js_allValuesArray and js_allDatesArray
  var LISTVALUES;
  var LISTDATES;
  var CUSTOMDATES;
  var CUSTOMVALUES;
  var customValueFlag;
  customValueFlag = 0;
  globalVariableTest  = 200;
  //Get dateValues from cookie
  LISTDATES = unquoteXml(unescape(getCookie("TENK_LISTDATES")));
  //Get listValues from cookie
  LISTVALUES = unquoteXml(unescape(getCookie("TENK_LISTVALUES")));
  //get dateValues from custom item cookie
  CUSTOMDATES = unquoteXml(unescape(getCookie("TENK_CUSTOMLISTDATES")));
  //get listValues from custom item cookie
  CUSTOMVALUES = unquoteXml(unescape(getCookie("TENK_CUSTOMLISTVALUES")));
  //IE decided that the name of the cookie is the value after I deleted all of teh values in it.
  if (CUSTOMVALUES.trim() == "TENK_CUSTOMLISTDATE" || CUSTOMVALUES.trim() == "TENK_CUSTOMLISTDATES")
  {
    CUSTOMVALUES = "";
    CUSTOMDATES = "";
  }
  //List Values
  if (LISTVALUES != "false" && LISTDATES != "false")
  {
    var js_dateArray = new Array();
    var js_valueArray = new Array();
    var js_customValueArray = new Array();
    var js_customDateArray = new Array();

    if (LISTVALUES.substring(LISTVALUES.length-2,LISTVALUES.length) == "%7")
      {
        LISTVALUES = LISTVALUES.substring(0,LISTVALUES.length-2).trim();
      }
    if (LISTDATES.substring(LISTDATES.length-2,LISTDATES.length) == "%7")
      {
        LISTDATES = LISTDATES.substring(0,LISTDATES.length-2).trim();
      }
    if (LISTDATES.substring(LISTDATES.length-1,LISTDATES.length) == "|")
      {
        LISTDATES = LISTDATES.substring(0,LISTDATES.length-1).trim();
      }
    if (LISTVALUES.substring(LISTVALUES.length-1,LISTVALUES.length) == "|")
      {
        LISTVALUES = LISTVALUES.substring(0,LISTVALUES.length-1).trim();
      }

    js_dateArray  = LISTDATES.split("|");
    js_valueArray = LISTVALUES.split("|");

    if (js_valueArray.length == js_dateArray.length)
      {
        if (CUSTOMVALUES != "false" && CUSTOMDATES != "false")
        {
            if (CUSTOMVALUES.substring(0,1) == "|")
              {
                CUSTOMVALUES = CUSTOMVALUES.substring(1,CUSTOMVALUES.length).trim();
              }
            if (CUSTOMDATES.substring(0,1) == "|")
              {
                CUSTOMDATES = CUSTOMDATES.substring(1,CUSTOMDATES.length).trim();
              }
            if (CUSTOMVALUES.substring(CUSTOMVALUES.length-2,CUSTOMVALUES.length) == "%7")
              {
                CUSTOMVALUES = CUSTOMVALUES.substring(0,CUSTOMVALUES.length-2).trim();
              }
            if (CUSTOMDATES.substring(CUSTOMDATES.length-2,CUSTOMDATES.length) == "%7")
              {
                CUSTOMDATES = CUSTOMDATES.substring(0,CUSTOMDATES.length-2).trim();
              }
            if (CUSTOMDATES.substring(CUSTOMDATES.length-1,CUSTOMDATES.length) == "|")
              {
                CUSTOMDATES = CUSTOMDATES.substring(0,CUSTOMDATES.length-1).trim();
              }
            if (CUSTOMVALUES.substring(CUSTOMVALUES.length-1,CUSTOMVALUES.length) == "|")
              {
                CUSTOMVALUES = CUSTOMVALUES.substring(0,CUSTOMVALUES.length-1).trim();
              }
            js_customValueArray = CUSTOMVALUES.split("|");
            js_customDateArray = CUSTOMDATES.split("|");
            //alert('customvalues is ' + CUSTOMVALUES + " and length of array is " + js_customDateArray.length);
            if (js_customDateArray.length == js_customValueArray.length && CUSTOMVALUES != "")
            {
              customValueFlag = 1;
            }
        }
        //STEP ONE: now we put all Date elements into js_allDatesArray, first template then custom
        //use counter for total item count
        var thisCounter = 0;
        //instantiate allItemsArray
        var numItems;
        numItems = js_dateArray.length;
        if (customValueFlag == 1)
        {
          numItems = numItems + js_customDateArray.length;
        }
        //js_allItemsArray = new Array(numItems);
        js_allItemsArray = new Array(3);
        for (var i = 0; i < numItems; i++) {
        	js_allItemsArray[i] = new Array(3);
        	for (var j = 0; j < 3; j++) {
        		js_allItemsArray[i][j] = '';
        	}
        }

        for(var i=0; i<= js_dateArray.length-1; i++)
        {
          if (js_dateArray[i] != "" && js_valueArray[i] != "")
          {
            js_allItemsArray[thisCounter][0] = js_dateArray[i];

            if (js_allItemsArray[thisCounter][0].substring(js_allItemsArray[thisCounter][0].length-2,js_allItemsArray[thisCounter][0].length) == "%7")
              {
                js_allItemsArray[thisCounter][0] = js_allItemsArray[thisCounter][0].substring(0,js_allItemsArray[thisCounter][0].length-2).toString().trim();
              }
            js_allItemsArray[thisCounter][1] = js_valueArray[i];
            if (js_allItemsArray[thisCounter][1].substring(js_allItemsArray[thisCounter][1].length-2,js_allItemsArray[thisCounter][1].length) == "%7")
              {
                js_allItemsArray[thisCounter][1] = js_allItemsArray[thisCounter][1].substring(0,js_allItemsArray[thisCounter][1].length-2).toString().trim();
              }
            js_allItemsArray[thisCounter][2] = thisCounter;//cookie value identifier after we sort
            thisCounter += 1;
          }
        }
        if (customValueFlag ==1)
        {
          //STEP TWO:: Add all custom values to this array
          for(var i=0; i<= js_customDateArray.length-1; i++)
          {
            if (URLDecode(js_customDateArray[i]) !="" && URLDecode(js_customValueArray[i]) != "")
            {
              js_allItemsArray[thisCounter][0] = URLDecode(js_customDateArray[i]);
              js_allItemsArray[thisCounter][1] = URLDecode(js_customValueArray[i]);
              js_allItemsArray[thisCounter][2] = thisCounter;//cookie value identifier after we sort
              //alert("building custom items.. thiscounter = " + thisCounter + " i is " + i + " : " + js_customValueArray[i]);
              thisCounter += 1;
            }
          }
        }
      //STEP THREE: SORT THE ELEMENTS BY DATE!
      js_allItemsArray.multiSort(0);// Sort Numerically by date
      }
    else
      {
      alert("ERRORING HERE");
      document.write("_" + LISTDATES + "_<BR>");
      document.write("_" + LISTVALUES + "_<BR>");
      document.write("one " + js_valueArray[0] + " one " + js_dateArray[0] + "<BR>");
      document.write("last " + js_valueArray[105] + " last " + js_dateArray[105] + "<BR>");
      return "list";
      }

  }
  else
  {
    return "noList";
  }
}

function ListToDoItems()
{
   var isValues;
   isValues = buildFullCookieArray();//this populates js_allItemsArray with date/value of all template and custom cookies
  if (isValues != "noList")
  {
  //Render List
  var holdDate;
  var thisItemValue;
  var oldDate;
  var htmlText = "";
  var nextDate;
  nextDate = "";
  oldDate = "";
  var taskStart;
  var taskEnd;
  var itemCounter;
  itemCounter = 0;
  //var myDate = new Date();
  var firstTable = 1;
  for(var i=0; i< js_allItemsArray.length-1; i++)
  {
    itemCounter += 1;
    if (holdDate != js_allItemsArray[i][0].toString().trim())
    {
        holdDate = js_allItemsArray[i][0].toString().trim();
        if (holdDate.substring(holdDate.length-2,holdDate.length) == "%7")
        {
          holdDate = holdDate.substring(0,holdDate.length-2).toString().trim();
        }
        nextDate = "";
        //figure out what the task completion date should be
        for (var z = i; z < js_allItemsArray.length-1; z++)
        {
          try
          {
            if (z <= js_allItemsArray.length-1)
            {
              if (holdDate != js_allItemsArray[z][0].toString().trim())
                {
                  nextDate = js_allItemsArray[z][0] + '';
                  break;
                }
            }
          }
          catch(err){
          window.alert(" z is " + z +  " js_allItemsArray.length is " + js_allItemsArray.length + " Error description: " + err.description);
          }
        }
        if (nextDate == "")
        {
          nextDate = js_allItemsArray[i][0] + '';
        }
        if (nextDate.toString().substring(nextDate.toString().length-2,nextDate.toString().length) == "%7")
        {
          nextDate = nextDate.substring(0,nextDate.toString().length-2).toString().trim();
        }
        taskStart = db_formatDate(js_allItemsArray[i][0]);
        taskEnd = db_formatDate(nextDate);
        //Display Date
        if (firstTable == 0)
        {
            //close previous table first
            document.write("</TABLE></p></div><div class='gr_roundedcorner_bottom'><div></div></div></div><BR><BR>");
        }
        firstTable = 0;
        document.write("<div class='gr_roundedcorner_box'><div class='gr_roundedcorner_top'><div></div></div><div class='gr_roundedcorner_content'><p><TABLE width='100%' cellspacing=0 style='font-family: Arial Black; font-size: 10pt;'>");
        document.write("<tr><th align='left' colspan='4' valign='middle' height='30' class='dateHeading'>Tasks For " + db_formatDate(taskStart) + " - " + db_formatDate(taskEnd) + "</th></tr>");
        oldDate = holdDate;
        holdDate = js_allItemsArray[i][0];
    }
    try
    {
    if (isNumeric(js_allItemsArray[i][1]))
    {
      thisItemValue = tenkValueArray[js_allItemsArray[i][1]];
    }
    else
    {
      thisItemValue = js_allItemsArray[i][1];
       if (thisItemValue.substring(thisItemValue.length-2,thisItemValue.length) == "%7")
        {
          thisItemValue = thisItemValue.substring(0,thisItemValue.length-2).toString().trim();
          if (isNumeric(js_allItemsArray[i][1]))
          {
            thisItemValue = tenkValueArray[js_allItemsArray[i][1]];
          }
          else
          {
            thisItemValue = js_allItemsArray[i][1];
          }
        }

    }
    }
    catch(err)
    {
      alert("isNumeric failure on i is " + i + " js_allItemsArray[i][1] is "  + js_allItemsArray[i][1]);
    }
      // draw out item
      document.write("<tr><td valign='top' width='5%'>"+itemCounter+"&nbsp;</td>");
      document.write("<td width='14%' align='center' valign='top' class='textFormat1' colspan='2'><a href='javascript:addItem(" + js_allItemsArray[i][0] + ")'><Img border='0' src='/images/btn_addblue.gif' title='Add New' alt='Add Item'></A>&nbsp;<a href='javascript:editItem(&quot;" + escape(quoteXml(js_allItemsArray[i][1])) + "&quot;)'><Img border='0' src='/images/btn_smedit.gif' title='Edit' alt='Edit'></a>&nbsp;<a href='javascript:deleteItem(&quot;" + escape(quoteXml(js_allItemsArray[i][1])) + "&quot;)'><Img border='0' title='Delete' src='/images/btn_smdelete.gif' alt='Delete'></a>&nbsp;<input type='checkbox' value='0' name='chk"+itemCounter+"'></td>");
      document.write("<td class='textFormat1' width='81%'>&nbsp;"+thisItemValue+"<br><BR></td></tr>");
  }
  //close last table
  document.write("</TABLE></p></div><div class='gr_roundedcorner_bottom'><div></div></div></div><BR><BR>");
  }
  else{
    //Display Browser Settings not supporting this feature or Cookies not found
    if (document.frmInputs.weddingDate.value != undefined && document.frmInputs.weddingDate.value != "")
    {
    document.write("<BR><BR><BR><BR><BR>If you have submitted the form with your wedding date and still do not see a list, then your browser's current internet settings do not support this feature. Ensure your browser has cookies enabled. this is how we store your customized list so that when you come back to this site, your information is preserved<BR><BR>");
    }
  }
}
function reviseCookieList(taskType, xDate,itemOrID)
{
  //cookie is either template item or custom item
  if (taskType == 'custom')
  {
    // add item and date to the end of the cookies
    var CUSTOMDATES;
    var CUSTOMVALUES;
    var itemValue;
    itemValue = itemOrID;
    //get dateValues from custom item cookie
    CUSTOMDATES = getCookie("TENK_CUSTOMLISTDATES");
    //get listValues from custom item cookie
    CUSTOMVALUES = getCookie("TENK_CUSTOMLISTVALUES");
    if (CUSTOMDATES != "")
    {  CUSTOMDATES = unescape(unquoteXml(CUSTOMDATES));
      CUSTOMVALUES = unescape(unquoteXml(CUSTOMVALUES));
    }

    //List Values
    if (CUSTOMVALUES != "false" && CUSTOMDATES != "false" && CUSTOMVALUES !="")
    {
        if (CUSTOMVALUES.substring(CUSTOMVALUES.length-2,CUSTOMVALUES.length) == "%7")
          {
            CUSTOMVALUES = CUSTOMVALUES.substring(0,CUSTOMVALUES.length-2).trim();
          }
        if (CUSTOMDATES.substring(CUSTOMDATES.length-2,CUSTOMDATES.length) == "%7")
          {
            CUSTOMDATES = CUSTOMDATES.substring(0,CUSTOMDATES.length-2).trim();
          }
        if (CUSTOMDATES.substring(CUSTOMDATES.length-1,CUSTOMDATES.length) == "|")
          {
            CUSTOMDATES = CUSTOMDATES.substring(0,CUSTOMDATES.length-1).trim();
          }
        if (CUSTOMVALUES.substring(CUSTOMVALUES.length-1,CUSTOMVALUES.length) == "|")
          {
            CUSTOMVALUES = CUSTOMVALUES.substring(0,CUSTOMVALUES.length-1).trim();
          }
        //concatenate new values to lists:
        CUSTOMVALUES = CUSTOMVALUES + "|" + unescape(unquoteXml(itemValue)) + "|";
        CUSTOMDATES = CUSTOMDATES + "|" + xDate + "|";
    }
    else
    {
        CUSTOMVALUES = unescape(unquoteXml(itemValue)) + "|";
        CUSTOMDATES = xDate + "|";
    }
    //create/set cookies
    SetCookie("TENK_CUSTOMLISTVALUES",CUSTOMVALUES, expda);
    SetCookie("TENK_CUSTOMLISTDATES",CUSTOMDATES, expda);
  }
  else //tasktype = template
  {

  }

}
function addItem(thisDate)
{
     var ToDoItemX;
     ToDoItemX = prompt("Enter new item");
     if ((ToDoItemX != null) && (ToDoItemX != "undefined" ))
     {
     
        reviseCookieList('custom',thisDate,ToDoItemX);
       
        window.location = window.location;
     }
}

function editItem(valueOrID)
{
  //get new item value
  var newItemX;
  var editCookieDates;
  var editCookieValues;
  var js_newCookieValueArray = new Array();
  var js_newCookieDateArray = new Array();
   newItemX = prompt("Edit item");
  //if value its custom, if ID its from the cookie template items
  if (valueOrID != null && isNumeric(valueOrID))
  {
    //delete template cookie and add an item to custom - must pass in date too though
    deleteItem(valueOrID, newItemX);
  }
  else
  {
    //Custom value
    //get dateValues from custom item cookie
    editCookieDates = unescape(unquoteXml(getCookie("TENK_CUSTOMLISTDATES")));
    //get listValues from custom item cookie
    editCookieValues = unescape(unquoteXml(getCookie("TENK_CUSTOMLISTVALUES")));
  }
  if (editCookieValues != "false" && editCookieDates != "false")
  {
      if (editCookieValues.substring(editCookieValues.length-2,editCookieValues.length) == "%7")
        {
          editCookieValues = editCookieValues.substring(0,editCookieValues.length-2).trim();
        }
      if (editCookieDates.substring(editCookieDates.length-2,editCookieDates.length) == "%7")
        {
          editCookieDates = editCookieDates.substring(0,editCookieDates.length-2).trim();
        }
      if (editCookieDates.substring(editCookieDates.length-1,editCookieDates.length) == "|")
        {
          editCookieDates = editCookieDates.substring(0,editCookieDates.length-1).trim();
        }
      if (editCookieValues.substring(editCookieValues.length-1,editCookieValues.length) == "|")
        {
          editCookieValues = editCookieValues.substring(0,editCookieValues.length-1).trim();
        }
      js_newCookieValueArray = editCookieValues.split("|");
      js_newCookieDateArray = editCookieDates.split("|");
      var newValueList;
      newValueList = "";
      //loop thru array and find value , when found replace value then setCookie to new value
      for(var i=0; i<= js_newCookieDateArray.length-1; i++)
        {
            //check to see if old value = this array element's value:
            if (js_newCookieValueArray[i] == unescape(unquoteXml(valueOrID)))
            {
              newValueList = newValueList + newItemX + "|";
            }
            else
            {
                //keep it the same
                newValueList = newValueList + js_newCookieValueArray[i] + "|";
            }
        }
        //set cookie
        if (valueOrID != null && isNumeric(valueOrID))
        {
          SetCookie("TENK_LISTVALUES",newValueList, expda);
        }
        else
        {
          SetCookie("TENK_CUSTOMLISTVALUES",newValueList, expda);
        }
        window.location = window.location;
        return;

  }
  else
  {
    alert("error");
  }
}
function deleteItem(valueOrID, newValue)
{
    var editCookieDates;
    var editCookieValues;
    var js_newCookieValueArray = new Array();
    var js_newCookieDateArray = new Array();
    if (newValue == null)
     {
       newValue = "";
     }
    //delete cookie item, if valueOrID isNumeric then from template otherwies from custom
    if (valueOrID != null && isNumeric(valueOrID))
    {
         //get dateValues from template item cookie
        editCookieDates = unquoteXml(unescape(getCookie("TENK_LISTDATES")));
        //get listValues from template item cookie
        editCookieValues = unquoteXml(unescape(getCookie("TENK_LISTVALUES")));
    }

    else //custom
    {
         //get dateValues from custom item cookie
        editCookieDates = unescape(unquoteXml(getCookie("TENK_CUSTOMLISTDATES")));
        //get listValues from custom item cookie
        editCookieValues = unescape(unquoteXml(getCookie("TENK_CUSTOMLISTVALUES")));
    }
    //redefine cookie item lists
    if (editCookieValues != "false" && editCookieDates != "false")
    {
      if (editCookieValues.substring(editCookieValues.length-2,editCookieValues.length) == "%7")
        {
          editCookieValues = editCookieValues.substring(0,editCookieValues.length-2).trim();
        }
      if (editCookieDates.substring(editCookieDates.length-2,editCookieDates.length) == "%7")
        {
          editCookieDates = editCookieDates.substring(0,editCookieDates.length-2).trim();
        }
      if (editCookieDates.substring(editCookieDates.length-1,editCookieDates.length) == "|")
        {
          editCookieDates = editCookieDates.substring(0,editCookieDates.length-1).trim();
        }
      if (editCookieValues.substring(editCookieValues.length-1,editCookieValues.length) == "|")
        {
          editCookieValues = editCookieValues.substring(0,editCookieValues.length-1).trim();
        }
      js_newCookieValueArray = editCookieValues.split("|");
      js_newCookieDateArray = editCookieDates.split("|");
      var newValueList;
      var newDateList;
      newValueList = "";
      newDateList = "";
      //loop thru array and find value , when found replace value then setCookie to new value
      for(var i=0; i<= js_newCookieDateArray.length-1; i++)
        {
         // alert("delete step: custom value is : " + valueOrID);
          if (js_newCookieValueArray[i] != unescape(unquoteXml(valueOrID)) && js_newCookieValueArray[i] != "")
          {
            //alert("matched it to: " + js_newCookieValueArray[i] + " AND " + valueOrID);
            newValueList = newValueList + escape(quoteXml(js_newCookieValueArray[i])) + "|";
            newDateList = newDateList + escape(quoteXml(js_newCookieDateArray[i])) + "|";
          }
          else
          {
            //alert("we matched it and it shoudl exclude");
            //if template item and we are editing - not deleting, then add item to custom cookie
            if (valueOrID != null && newValue != "" && isNumeric(valueOrID))
            {
              reviseCookieList('custom',js_newCookieDateArray[i],newValue);
            }
          }
        }
      if (valueOrID != null && isNumeric(valueOrID))
      {
         SetCookie("TENK_LISTVALUES",newValueList, expda);
         SetCookie("TENK_LISTDATES",newDateList, expda);
      }
     else
     {
         SetCookie("TENK_CUSTOMLISTVALUES",newValueList, expda);
         SetCookie("TENK_CUSTOMLISTDATES",newDateList, expda);
     }
     window.location = window.location;
     return;
    }
}

// -->
