/* JavaScript Document
scripts/buildtimeList.js
author: dennis benedetto
10kdreamwedding.com
6/7/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).toString();
this[i].unshift(temp.toString());
} return this.sort();
}
function prompt(id, x, timeValue) {
// get field to be validated
var pf = document.getElementById( 'pForm' );
pf.style.display = 'block';
document.pForm.agenda.value = x;
document.pForm.id.value = id;
document.pForm.item.value = unquoteXml(id);
//convert hour to standard time format from military time, then place in form fields
var splitThisTime;
splitThisTime = db_formatTime(timeValue,1);//returns delimited list with time elements
var tempTimeSplit = new Array(3);
tempTimeSplit = splitThisTime.split("|");
var tempHour;
var tempMin;
var tempM;
tempHour = tempTimeSplit[0];
tempMin = tempTimeSplit[1];
tempM = tempTimeSplit[2];
//assign values to form
for(i=0;i<document.pForm.hour.length;i++)
{
  if(document.pForm.hour[i].value == tempHour)
    {document.pForm.hour[i].selected = 1;
    break;
    }
}
document.pForm.minutes.value = tempMin;
if (tempM == "AM")
  document.pForm.M[0].selected = 1;
else
  document.pForm.M[1].selected = 1;
  
document.pForm.item.focus();
}

function getPdata( arg, id )
{
  var myForm = document.getElementById( 'pForm' );
  if ( 'CANCEL' == arg )
  {
    myForm.style.display = 'none'; // hide form
    return; // exit immediately
  }

  else
  {
    var x = arg;
    var newItemValue = escape(quoteXml(document.pForm.item.value));
    var time;
    time = db_convertTime(document.pForm.hour.value,document.pForm.minutes.value,document.pForm.M.value);
    myForm.style.display = 'none'; // hide form
    newItemValue = newItemValue + "|" + time;
    if (x == "edit")
    {
      editItem(id, newItemValue);
    }
    else
    {
      editItem(id, newItemValue,"1");//last parameter is to add it only (dont remove first)
    }
    return;
  }
}
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+"; path=/";  //no path defaults to full path
}

function db_formatTime(x, returnDelimited)
{//get standard time from military
  var formattedTime;
  var delimitedTime;
  if (returnDelimited == undefined)
    {
      var returnDelimited = 0;
    }
  var x_array = new Array(2);
  x_array = x.toString().split(":");
  //some mishap - losing last 0 when pulling down from cookie
  if (x_array[1].length == 1)
    {x_array[1] = x_array[1] + '0';}
    if (x_array[0] < 12)
    {
      if (x_array[0] > 1)
        {
          formattedTime = x_array[0] + ":" + x_array[1] + " AM";
          delimitedTime = x_array[0] + "|" + x_array[1] + "|" + "AM";
        }
      else
        {
          formattedTime = "12:" + x_array[1] + "AM";
          delimitedTime = "12:" + "|" + x_array[1] + "|" + "AM";
        }
    }
    else if (x_array[0] == "00")
    {
      formattedTime = "12:" + x_array[1]+ " AM";
      delimitedTime = "12:" + "|" + x_array[1] + "|" + "AM";
    }
    else if (x_array[0] == "12")
      {
        formattedTime = "12:" + "" + x_array[1].toString() + " PM";
        delimitedTime = "12:" + "|" + x_array[1].toString() + "|" + "PM";
      }
    else
    {
      formattedTime = parseInt(x_array[0]) - 12 + ":" + x_array[1] + " PM";
      delimitedTime = Right('0' + '' +(parseInt(x_array[0]) - 12).toString(),2) + "|" + x_array[1] + "|" + "PM";

    }
  if (returnDelimited == 0)
    {return formattedTime;}
  else
    {return delimitedTime;}
}
function db_convertTime(x,y,z)
{//return military time
  var formattedTime;

  if (y > 60)
  {
    y = "00";
  }
  if (y.length == 1)
  {
    y= "0" + y;
  }
  if (x < 13)
  {
    if (z =="PM")
      {
        if (x != "12")
        {
          formattedTime = parseInt(x) + 12 + ":" + y;
        }
        else
        {
          formattedTime = x + ":" + y;
        }

      }
      else
      {
        //handle AM
        if (x == "12")
          {
            formattedTime = "00:" + y;
          }
        else
        {
          formattedTime = x + ":" + y;
        }
      }
  }
  else
  {
    //already military
    formattedTime = x + ":" + y;
  }
  return formattedTime;
}

js_allItemsArray = new Array(3);
function buildFullCookieArray()
{
  //build array of both template and custom cookie values into js_allValuesArray and js_allDatesArray
  var LISTVALUES;
  var LISTTIMES;
  //Get timeValues from cookie
  LISTTIMES = unquoteXml(unescape(getCookie("TENK_TIMELIST")));
  //Get listValues from cookie
  LISTVALUES = unquoteXml(unescape(getCookie("TENK_DAYLIST")));
  //List Values
  if (LISTVALUES != "false" && LISTTIMES != "false")
  {
    var js_timeArray = new Array();
    var js_valueArray = new Array();

    if (LISTVALUES.substring(LISTVALUES.length-2,LISTVALUES.length) == "%7")
      {
        LISTVALUES = LISTVALUES.substring(0,LISTVALUES.length-2).trim();
      }
    if (LISTTIMES.substring(LISTTIMES.length-2,LISTTIMES.length) == "%7")
      {
        LISTTIMES = LISTTIMES.substring(0,LISTTIMES.length-2).trim();
      }
    if (LISTTIMES.substring(LISTTIMES.length-1,LISTTIMES.length) == "|")
      {
        LISTTIMES = LISTTIMES.substring(0,LISTTIMES.length-1).trim();
      }
    if (LISTVALUES.substring(LISTVALUES.length-1,LISTVALUES.length) == "|")
      {
        LISTVALUES = LISTVALUES.substring(0,LISTVALUES.length-1).trim();
      }
    js_timeArray  = LISTTIMES.split("|");
    js_valueArray = LISTVALUES.split("|");

    if (js_valueArray.length == js_timeArray.length)
      {
        //STEP ONE: now we put all time elements into js_allItemsArray, first template then custom
        //use counter for total item count
        var thisCounter = 0;
        //instantiate allItemsArray
        var numItems;
        numItems = js_timeArray.length;
        
        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_timeArray.length-1; i++)
        {
          if (js_timeArray[i] != "" && js_valueArray[i] != "")
          {
            js_allItemsArray[thisCounter][0] = js_timeArray[i];
            if(i==52)
            
            if (js_allItemsArray[thisCounter][0].substring(js_allItemsArray[thisCounter][0].length-2,js_allItemsArray[thisCounter][0].length) == "%2")
              {
                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) == "%2")
              {
                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;
          }
        }
        
      }
    else
      {
      alert("ERRORING HERE");
      document.write("_" + LISTTIMES + "_<BR>");
      document.write("_" + LISTVALUES + "_<BR>");
      document.write("one " + js_valueArray[0] + " one " + js_timeArray[0] + "<BR>");
      document.write("last " + js_valueArray[53] + " last " + js_timeArray[53] + "<BR>");
      return "list";
      }

  }
  else
  {
    return "noList";
  }
}
function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}
function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}
function removeColon(x)
{
  var Tarray = new Array(2)
  Tarray = x.split(":");
  if (Tarray[1].length != 2)
    Tarray[1] = Tarray[1] + '' + '0';//last element gets 0 truncated :(
  return '' + Tarray[0].toString() + '' + Tarray[1].toString() + '';
}
function addColon(x)
{
  var Ttime;
  Ttime = Left(x,2) + ":" + Right(x,2);
  return Ttime;
}
function ListToDoItems()
{
  var ieTest = "";
  var isValues;
  isValues = buildFullCookieArray();//this populates js_allItemsArray with date/value of all template and custom cookies

  if (isValues != "noList")
  {
  //Render List
  var holdTime;
  var thisItemValue;
  //items displayed in 2 hour increments
  var startBlock;
  var endBlock;
  startBlock = "00:00";
  endBlock = Right('0' + (parseInt(Left(startBlock,2)) + parseInt(2)),2) + ":00";
  var newBlock;
  var openTable;
  openTable = 0;
  newBlock = 0;
  itemTime = "";
  var itemCounter = 0;
  var firstTable = 1;

  for(i=0;i< js_allItemsArray.length-1;i++)
  {
    itemTime = js_allItemsArray[i][0].toString().trim();
    itemCounter += 1;
    if (itemTime.substring(itemTime.length-2,itemTime.length) == "%7")
      {
        itemTime = itemTime.substring(0,itemTime.length-2).toString().trim();
      }
      var test;
      var timeArray = new Array(2);
    while ((itemTime < startBlock || itemTime >= endBlock) && endBlock <="24:00")
      {
          startBlock = endBlock.trim();
          timeArray = startBlock.split(":");
          test = (2 * 1) + (timeArray[0] * 1);
          endBlock = Right('0' + '' + test.toString(),2) + ":00";
          newBlock = 1;
      }

    if (newBlock == 1)
      {
        if (openTable == 1)
          {
            //close table
             document.write("</TABLE></p></div><div class='gr_roundedcorner_bottom'><div></div></div></div><BR><BR>");
             openTable = 0;
          }
        //open a table
        document.write("<div class='gr_roundedcorner_box'><div class='gr_roundedcorner_top'><div></div></div><div class='gr_roundedcorner_content'><p class='one'><TABLE width='100%' cellspacing='0' style='font-family: Arial Black; font-size: 10pt;'>");
        
        document.write("<tr><td align='center' colspan='4' valign='middle' class='dateHeading'>" + db_formatTime(startBlock) + " - " + db_formatTime(endBlock) + "</td></tr>");
        
        openTable = 1;
        newBlock = 0;
      }
    try
    {

      thisItemValue = js_allItemsArray[i][1];
       if (thisItemValue.substring(thisItemValue.length-2,thisItemValue.length) == "%7")
        {
          thisItemValue = thisItemValue.substring(0,thisItemValue.length-2).toString().trim();
          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='10%' class='itemFormat'>"+db_formatTime(itemTime)+"&nbsp;</td>");
      ieTest = ieTest + "<tr><td valign='top' width='10%' class='itemFormat'>"+db_formatTime(itemTime)+"&nbsp;</td>";
      if (js_allItemsArray[i][0].length == 4)
      {
        var tempTimeSplit = new Array(2);
        tempTimeSplit = js_allItemsArray[i][0].split(":");
        if (tempTimeSplit[1].length == 1)
          {
            js_allItemsArray[i][0] = tempTimeSplit[0] + ":" + tempTimeSplit[1] + "0";
          }
      }
      document.write("<td width='14%' align='center' valign='top' class='itemFormat' colspan='2'><a href='javascript:prompt(&quot;" + escape(quoteXml(js_allItemsArray[i][1])) + "&quot;,&quot;add&quot;,&quot;" + escape(quoteXml(js_allItemsArray[i][0])) + "&quot;)'><Img border='0' src='/images/btn_addblue.gif' title='Add New' alt='Add Item'></A>&nbsp;<a href='javascript:prompt(&quot;" + escape(quoteXml(js_allItemsArray[i][1])) + "&quot;,&quot;edit&quot;,&quot;" + escape(quoteXml(js_allItemsArray[i][0])) + "&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;,&quot;delete&quot;)'><Img border='0' title='Delete' src='/images/btn_smdelete.gif' alt='Delete'></a>&nbsp;<input type='checkbox' value='0' name='chk"+itemCounter+"'></td>");
      //ieTest = ieTest + "<td width='14%' align='center' valign='top' class='itemFormat' colspan='2'><a href='javascript:prompt(&quot;" + js_allItemsArray[i][1] + "&quot;,&quot;add&quot;)'><Img border='0' src='/images/btn_addBlue.gif' title='Add New' alt='Add Item'></A>&nbsp;<a href='javascript:prompt(&quot;" + js_allItemsArray[i][1] + "&quot;,&quot;edit&quot;)'><Img border='0' src='/images/btn_smEdit.gif' title='Edit' alt='Edit'></a>&nbsp;<a href='javascript:deleteItem(&quot;" + 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='itemFormat' width='76%'>&nbsp;"+unquoteXml(thisItemValue)+"<br><BR></td></tr>");
      //ieTest = ieTest + "<td class='itemFormat' width='76%'>&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
  {//browser settings need correction
    if (document.frmSubmit.reset.value != undefined && document.frmSubmit.reset.value != "")
    {
    document.write("<BR><BR><BR><BR><BR>If you are viewing this message, and 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 if you come back to this site, your information is preserved<BR><BR>");
    }
  }
  
}

function editItem(id, valueOrID,addOnly)
{
  if (addOnly == undefined)
    {addOnly = "0";}
  if (addOnly == "0")
    {//delete then add
      deleteItem(id,'norefresh');
    }
  //get new item value
  var editCookieDates;
  var editCookieValues;
  var js_newCookieValueArray = new Array();
  var js_newCookieDateArray = new Array();
  var newItemValue = valueOrID;
  var splitNew = new Array(2);
  splitNew = newItemValue.split("|");
  var oldItemValue = id;
  editCookieDates = URLDecode(getCookie("TENK_TIMELIST"));
  //get listValues from custom item cookie
  editCookieValues = URLDecode(getCookie("TENK_DAYLIST"));

  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 = "";
      var endOfList = "0";
      var itemIsHome = "0";
      //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] != "")
            {
              if ((i + 1) > js_newCookieDateArray.length - 1)
                {
                endOfList = 1;
                }

              if (itemIsHome == "0" && (splitNew[1] <= js_newCookieDateArray[i] || endOfList =="1"))
              {
                  //insert here!
                newValueList = newValueList + "|" + quoteXml(splitNew[0]);
                newDateList = newDateList + "|" + escape(splitNew[1]);
                itemIsHome = "1";
              }
              if (js_newCookieDateArray[i].length != 5)
                js_newCookieDateArray[i] = js_newCookieDateArray[i] + '0';
              //keep it the same
              newValueList = newValueList + "|" + escape(quoteXml(js_newCookieValueArray[i]));
              newDateList = newDateList + "|" + escape(quoteXml(js_newCookieDateArray[i]));
            }
        }
        //document.write("<BR>Test this out:<BR>" + newValueList);
        //return;
        //set cookie
        SetCookie("TENK_TIMELIST",newDateList, expda);
        SetCookie("TENK_DAYLIST",newValueList, expda);
        window.location = window.location;
        return;

  }
  else
  {
    alert("error");
  }
}
function deleteItem(valueOrID, refreshFlag)
{
    var editCookieDates;
    var editCookieValues;
    var js_newCookieValueArray = new Array();
    var js_newCookieDateArray = new Array();
   if (refreshFlag == null || refreshFlag == undefined)
    {var refreshFlag = "refresh";
    }
     //get dateValues from custom item cookie
    editCookieDates = getCookie("TENK_TIMELIST");
    //get listValues from custom item cookie
    editCookieValues = getCookie("TENK_DAYLIST");

    //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++)
        {
          if (js_newCookieDateArray[i].length != 5)
            js_newCookieDateArray[i] = js_newCookieDateArray[i] + "0";
            
          if (quoteXml(js_newCookieValueArray[i]) != quoteXml(valueOrID) && js_newCookieValueArray[i] != "")
          {
            newValueList = newValueList + escape(quoteXml(js_newCookieValueArray[i])) + "|";
            newDateList = newDateList + escape(quoteXml(js_newCookieDateArray[i])) + "|";
          }
        }
     //SetCookie("TENK_DAYLIST","","NOW");
     //SetCookie("TENK_TIMELIST","","NOW");
     SetCookie("TENK_DAYLIST",newValueList, expda);
     SetCookie("TENK_TIMELIST",newDateList, expda);
     if (refreshFlag == "delete")
     {
        window.location = window.location;
     }
     return;
    }

}
// -->

