var lastElement = "";

var curTab = "Info";

var curRepeatTab = "Daily";

var curCalObj = "";

var contactUpdateXML = new Array();

var xmlString = new String();

var curDeviceID = 0;

var attendeeXML = "";



var arySelDays = new Array();

arySelDays[44] = false;

arySelDays[1] = false;

arySelDays[2] = false;

arySelDays[3] = false;

arySelDays[4] = false;

arySelDays[5] = false;

arySelDays[6] = false;



var re = /@/;

var formatPhone = /\d?\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/;



var objXML = new Array();



isDragging = false;

dragXOffset = 0;

dragYOffset = 0;

//document.onmousemove = doMove;

//document.onmousedown = setDragOn;

//document.onmouseup = setDragOff;



// Task popup

function doTaskPopup(xmlContent) {

  //alert(xmlContent);

}



// This is the DOM compliant version of the above function

function showPopup() {



  // Setup an array to generate our table

  var aryCol1 = new Array();

  var aryCol2 = new Array();

  aryCol1[0] = "Start time";

  aryCol2[0] = "start";

  aryCol1[1] = "End time";

  aryCol2[1] = "end";

  aryCol1[2] = "Subject";

  aryCol2[2] = "subject";

  aryCol1[3] = "Location";

  aryCol2[3] = "location";



  curDiv = document.getElementById("detailPopup");

  //clearChildren(curDiv);



  if (document.getElementById("container")) {

    //clearChildren(document.getElementById("container"));

  }



  curDiv.style.visibility = "visible";



  //alert("Begin parsing XML.");

  // Get our XML

  var newXML = document.all.vzcal_weekview4.GetVariable("/:detailsXML");

  parseXML(newXML);

  //alert("XML parsed.");

  if (thisAppt.type == "ReachMe") {

    curTab = "ReachMeInfo";

  } else {

    curTab = "Info";

  }



  if (curDiv.children.length == 0) {

    clearChildren(curDiv);



    curDiv.style.backgroundImage = "url('images/popup_back.gif')";



    curDiv.style.top = (document.body.clientHeight / 2) - 115 + parseInt(document.body.scrollTop);

    curDiv.style.left = (document.body.clientWidth / 2) - 165 + parseInt(document.body.scrollLeft);



    // Create our "tabs" div

    tabsDiv = document.createElement("div");

    var tabs_btnInfo = document.createElement("img");

    var tabs_btnReminder = document.createElement("img");

    var tabs_btnRepeat = document.createElement("img");

    var tabs_btnParticipants = document.createElement("img");



    tabs_btnInfo.setAttribute("src", "images/btnInfo_off.gif");

    tabs_btnInfo.setAttribute("id", "btn_Info");

    tabs_btnInfo.attachEvent("onclick", function() { showTab("Info") });

    tabs_btnInfo.attachEvent("onmouseover", function() { tabOver('Info') });

    tabs_btnInfo.attachEvent("onmouseout", function() { tabOut("Info") });



    tabs_btnReminder.setAttribute("src", "images/btnReminder_off.gif");

    tabs_btnReminder.setAttribute("id", "btn_Reminder");

    tabs_btnReminder.attachEvent("onclick", function() { showTab("Reminder") });

    tabs_btnReminder.attachEvent("onmouseover", function() { tabOver("Reminder") });

    tabs_btnReminder.attachEvent("onmouseout", function() { tabOut("Reminder") });



    tabs_btnRepeat.setAttribute("src", "images/btnRepeat_off.gif");

    tabs_btnRepeat.setAttribute("id", "btn_Repeat");

    tabs_btnRepeat.attachEvent("onclick", function() { showTab("Repeat") });

    tabs_btnRepeat.attachEvent("onmouseover", function() { tabOver("Repeat") });

    tabs_btnRepeat.attachEvent("onmouseout", function() { tabOut("Repeat") });



    tabs_btnParticipants.setAttribute("src", "images/btnParticipants_off.gif");

    tabs_btnParticipants.setAttribute("id", "btn_Participants");

    tabs_btnParticipants.attachEvent("onclick", function() { showTab("Participants") });

    tabs_btnParticipants.attachEvent("onmouseover", function() { tabOver("Participants") });

    tabs_btnParticipants.attachEvent("onmouseout", function() { tabOut("Participants") });



    //tabs_btnSettings.setAttribute("src", "images/btn_settings_on.gif");

    //tabs_btnAttendees.setAttribute("src", "images/btn_attendees_off.gif");

    //tabs_btnAttendees.setAttribute("id", "btn_Attendees");

    //tabs_btnAttendees.attachEvent("onclick", function() { showAttendees(); });

    //tabs_btnAttendees.attachEvent("onmouseover", function() { document.getElementById("btn_Attendees").style.cursor='hand'; document.getElementById("btn_Attendees").src='images/btn_Attendees_on.gif'; });

    //tabs_btnAttendees.attachEvent("onmouseout", function() { document.getElementById("btn_Attendees").src='images/btn_Attendees_off.gif'; });



    //tabsDiv.appendChild(tabs_btnSettings);

    //tabsDiv.appendChild(tabs_btnAttendees);



    tabsDiv.appendChild(tabs_btnInfo);

    tabsDiv.appendChild(tabs_btnReminder);

    tabsDiv.appendChild(tabs_btnRepeat);

    tabsDiv.appendChild(tabs_btnParticipants);



    tabsDiv.style.position = "relative";

    tabsDiv.style.left = "8px";

    tabsDiv.style.top = "23px";



    curDiv.appendChild(tabsDiv);



    // Create our container

    containerDiv = document.createElement("div");

    containerDiv.setAttribute("id", "container");

    containerDiv.style.position = "relative";

    containerDiv.style.left = "20px";

    containerDiv.style.top = "35px";

    curDiv.appendChild(containerDiv);



    // Change our curDiv

    curDiv = document.getElementById("container");



    // Create our table

    settingsDiv = document.createElement("div");

    var tbl = document.createElement("table");

    var tblBody = document.createElement("tbody");

    var tblRow = document.createElement("tr");

    var tblCol1 = document.createElement("td");

    var tblCol2 = document.createElement("td");



    // Setup our table

    tblBody.setAttribute("class", "normalFont");

    tbl.setAttribute("border", "0");

    tbl.cellPadding = "5";

    tbl.cellSpacing = "0";

    tblCol1.setAttribute("class", "normalFont");

    tblCol1.style.fontWeight = "bold";



    //alert(tabsDiv.outerHTML);



    tbl.appendChild(tblBody);

    settingsDiv.appendChild(tbl);

    //curDiv.appendChild(settingsDiv);

  }





  tabs_btnInfo = document.getElementById("btn_Info");



    if (thisAppt.type == "NewAppt") {

      tabs_btnInfo.attachEvent("onclick", function() { showTab("Info") });

      tabs_btnInfo.attachEvent("onmouseover", function() { tabOver('Info') });

      tabs_btnInfo.attachEvent("onmouseout", function() { tabOut("Info") });

    } else {

      tabs_btnInfo.attachEvent("onclick", function() { showTab("ReachMeInfo") });

      tabs_btnInfo.attachEvent("onmouseover", function() { tabOver('ReachMeInfo') });

      tabs_btnInfo.attachEvent("onmouseout", function() { tabOut("ReachMeInfo") });

    }



  // The line below causes the rest of the JavaScript to stop loading

  //document.all.flashDragNDrop.TCallLabel("/", "xmlFlush");



  showTab(curTab);



}



// Tab mouseover

function tabOver(whichTab) {

  document.getElementById("btn_" + whichTab).style.cursor='hand';

  document.getElementById("btn_" + whichTab).src='images/btn' + whichTab + '_on.gif';

}



// Tab mouseout

function tabOut(whichTab) {

  if (curTab != whichTab) {

    document.getElementById("btn_" + whichTab).src='images/btn' + whichTab + '_off.gif';

  }

}

// Show tab

function showTab(whichTab) {



  //alert("Showing " + whichTab);



  holderDiv = document.getElementById("container");



  if (curTab != null && holderDiv.children.length > 0) {



    // Remove the content from the old tab

    switch(curTab) {

      case "Info":

        document.getElementById("btn_Info").src = "images/btnInfo_off.gif";

        document.body.appendChild(holderDiv.firstChild);

        break;

      case "ReachMeInfo":

        document.getElementById("btn_Info").src = "images/btnInfo_off.gif";

        document.body.appendChild(holderDiv.firstChild);

        break;

      case "Reminder":

        document.getElementById("btn_Reminder").src = "images/btnReminder_off.gif";

        document.body.appendChild(holderDiv.firstChild);

        break;

      case "Repeat":

        document.getElementById("repeatDailyHTML").style.visibility = "hidden";

        document.getElementById("btn_Repeat").src = "images/btnRepeat_off.gif";

        document.body.appendChild(holderDiv.firstChild);

        break;

      case "Participants":

        document.getElementById("btn_Participants").src = "images/btnParticipants_off.gif";

        document.body.appendChild(holderDiv.firstChild);

        break;

    }



    document.body.lastChild.style.visibility = "hidden";

    document.body.lastChild.style.pixelTop = 0;



    //if (curTab == "Repeat") {

    //  document.body.appendChild(holderDiv.lastChild);

    //  document.body.lastChild.style.visibility = "hidden";

    //  document.body.lastChild.style.pixelTop = 0;

    //}



  }



  // Show the new tab

  switch (whichTab) {

    case "Info":

      document.getElementById("btn_Info").src = "images/btnInfo_on.gif";

      clearChildren(holderDiv);

      document.getElementById("infoHTML").style.visibility = "visible";

      holderDiv.appendChild(document.getElementById("infoHTML"));

      curTab = "Info";

      break;

    case "ReachMeInfo":

      document.getElementById("btn_Info").src = "images/btnInfo_on.gif";

      clearChildren(holderDiv);

      document.getElementById("reachme_infoHTML").style.visibility = "visible";

      holderDiv.appendChild(document.getElementById("reachme_infoHTML"));

      curTab = "ReachMeInfo";

      break;

    case "Reminder":

      document.getElementById("btn_Reminder").src = "images/btnReminder_on.gif";

      clearChildren(holderDiv);

      document.getElementById("reminderHTML").style.visibility = "visible";

      holderDiv.appendChild(document.getElementById("reminderHTML"));

      curTab = "Reminder";

      break;

    case "Repeat":

      document.getElementById("repeatDailyHTML").style.visibility = "visible";

      document.getElementById("btn_Repeat").src = "images/btnRepeat_on.gif";

      clearChildren(holderDiv);

      document.getElementById("repeatHTML").style.visibility = "visible";

      holderDiv.appendChild(document.getElementById("repeatHTML"));

      if (document.getElementById("repeatHTML").children.length == 1) {

        document.getElementById("repeatDailyHTML").style.visibility = "visible";

        document.getElementById("repeatDailyHTML").style.pixelTop += 60;

        document.getElementById("repeatHTML").appendChild(document.getElementById("repeatDailyHTML"));

      }

      curTab = "Repeat";

      break;

    case "Participants":

      document.all.flashDragNDrop.TCallLabel("/", "clearList");

      document.all.flashDragNDrop.setVariable("historyXML", xmlString);

      setTimeout('document.all.flashDragNDrop.TCallLabel("/", "setHistory")', 500);

      document.getElementById("btn_Participants").src = "images/btnParticipants_on.gif";

      clearChildren(holderDiv);

      document.getElementById("participantHTML").style.visibility = "visible";

      holderDiv.appendChild(document.getElementById("participantHTML"));

      curTab = "Participants";

      break;



  }



  showSelect();



}



function showRepeatTab(whichType) {



  mainTab = document.getElementById("repeatHTML");



  document.body.appendChild(mainTab.lastChild);

  document.body.lastChild.style.visibility = "hidden";

  document.body.lastChild.style.pixelTop = -100;



  switch (whichType) {

    case "Daily":

      mainTab.appendChild(document.getElementById("repeatDailyHTML"));

      document.getElementById("repeatDailyHTML").style.visibility = "visible";

      document.getElementById("repeatDailyHTML").style.pixelTop = 60;

      curRepeatTab = "Daily";

      break;

    case "Weekly":

      mainTab.appendChild(document.getElementById("repeatWeeklyHTML"));

      document.getElementById("repeatWeeklyHTML").style.visibility = "visible";

      document.getElementById("repeatWeeklyHTML").style.pixelTop = 60;

      curRepeatTab = "Weekly";

      break;

    case "Monthly":

      mainTab.appendChild(document.getElementById("repeatMonthlyHTML"));

      document.getElementById("repeatMonthlyHTML").style.visibility = "visible";

      document.getElementById("repeatMonthlyHTML").style.pixelTop = 60;

      curRepeatTab = "Monthly";

      break;

    case "Yearly":

      mainTab.appendChild(document.getElementById("repeatYearlyHTML"));

      document.getElementById("repeatYearlyHTML").style.visibility = "visible";

      document.getElementById("repeatYearlyHTML").style.pixelTop = 60;

      curRepeatTab = "Yearly";

      break;

  }

}



function checkPopup() {

  curDiv = document.getElementById("detailPopup");

  //realX = event.x - parseInt(curDiv.style.left);

  //realY = event.y - parseInt(curDiv.style.top);

  // :: FIX :: IR 25748

  realX = event.x - (parseInt(curDiv.style.left) - document.body.scrollLeft);

  realY = event.y - (parseInt(curDiv.style.top) - document.body.scrollTop);

  if ((realX >= 313 && realX <= 325) && (realY >= 7 && realY <= 20)) {

    curDiv.style.visibility = "hidden";

    //clearChildren(curDiv);

    /* for (i = 0; i < curDiv.children[1].children.length; i++) {

      curDiv.children[1].children[i].style.pixelTop = 0;

      curDiv.children[1].children[i].style.visibility = "hidden";

    } */

    hideChildren(document.getElementById("container"));

    if (curTab == "Repeat") {

      mainTab = document.getElementById("repeatHTML");



      document.body.appendChild(mainTab.lastChild);

      document.body.lastChild.style.visibility = "hidden";

      document.body.lastChild.style.pixelTop = -100;

    }

    showSelect();

    buildXML();

  } else if ((realY <= 25) && (realY >= 0)) {

    //isDragging = !isDragging;

    //alert(isDragging);

    dragXOffset = realX;

    dragYOffset = realY;

  }



}



function swapDrag() {

  isDragging = !isDragging;

}



function doPopup() {

  showPopup();

}



function showCoords() {

  curDiv = document.getElementById("detailPopup");

  realX = event.x - parseInt(curDiv.style.pixelLeft);

  realY = event.y - parseInt(curDiv.style.pixelTop);

  curDiv = document.getElementById("mouseCoords");

  curDiv.innerHTML = "X: " + realX + ", Y: " + realY;

}



function doMove() {

  detailDiv = document.getElementById("detailPopup");

  realX = event.x - parseInt(detailDiv.style.left);

  realY = event.y - parseInt(detailDiv.style.top);

  if (isDragging) {

    //alert("realX: " + realX + ", realY: " + realY);

    lastElement = (event.srcElement.id != "") ? event.srcElement.id : lastElement;

  }

  curDiv = document.getElementById("mouseCoords");

  curDiv.innerHTML = "X: " + event.x + ", Y: " + event.y + "<br>" + (event.y - dragYOffset - 2 + detailDiv.style.pixelHeight) + "<br>" + document.body.clientHeight + "<br>" + event.srcElement.id + "<br>" + lastElement;

  dragBottom = detailDiv.style.pixelTop + detailDiv.style.pixelHeight;

  if (isDragging && ((dragBottom <= document.body.clientHeight))) {

    curDiv = document.getElementById("detailPopup");

    if ((event.srcElement.id == "detailPopup" || lastElement == "detailPopup")) {

      curDiv.style.top = event.y - dragYOffset;

      curDiv.style.left = event.x - dragXOffset;

    } else {

      //alert(event.srcElement.id);

      //curDiv.style.top = event.y - dragYOffset;

      //curDiv.style.left = event.x - dragXOffset;

    }

  }



  return false;

}



function setDragOn() {

  curDiv = document.getElementById("detailPopup");

  realX = event.x - parseInt(curDiv.style.left);

  realY = event.y - parseInt(curDiv.style.top);

  dragXOffset = realX;

  dragYOffset = realY;

  isDragging = true;

  lastElement = null;

}



function setDragOff() {

  curDiv = document.getElementById("detailPopup");

  realX = event.x - parseInt(curDiv.style.left);

  realY = event.y - parseInt(curDiv.style.top);

  dragXOffset = realX;

  dragYOffset = realY;

  isDragging = false;

  lastElement = null;

}



// Check to see whether the specified object was clicked

function isClicked(divName) {

  var clickX = event.pageX;

  var clickY = event.pageY;

  var clickSrc = event.srcElement;

  while (clickSrc.parentElement != null) {

    if (clickSrc.id == divName) {

      return true;

    }

    clickSrc = clickSrc.parentElement;

  }

  return false;

}



// Clear all child nodes from an object

function clearChildren(obj) {



  //alert("Number of children: " + obj.childNodes.length);



  var removeDiv = obj.firstChild;



  while (removeDiv != null) {

    tempDiv = removeDiv.nextSibling;

    obj.removeChild(removeDiv);

    removeDiv = tempDiv;

  }



}



function hideChildren(obj) {



  var hideDiv = obj.firstChild;



  while (hideDiv != null) {

    tempDiv = hideDiv.nextSibling;

    hideDiv.style.visibility = "hidden";

    hideDiv = tempDiv;

  }



}



function setPopupRepeat(obj) {



  strRepeat = obj.options[obj.selectedIndex].value;

  repeatDiv = document.getElementById("repeatHTML");



  showRepeatTab(strRepeat);



}



var objCal_popup = null;
var curUserDate = new String();
var curFieldName = new String();


function setupPopupCal(form, objCMonth) {   
  curFieldName = form;
  curApDate = eval("document.frmMain." + form);
  if(!objCMonth){
    if(curApDate.value == ""){

      var curDate = new Date();

      curYear = curDate.getFullYear();
    
      curMonth = curDate.getMonth() + 1;
      
      if(curMonth.toString().length == 1){
        curMonth = "0" + curMonth;
      }

      curDay = curDate.getDate();
      
      if(curDay.toString().length == 1){
        curDay = "0" + curDay;
      }
  
    }else{
  
      var curDate = new Date(curApDate.value);
  
      curYear = curDate.getFullYear();
  
      curMonth = curDate.getMonth() + 1;

      if(curMonth.toString().length == 1){
         curMonth = "0" + curMonth;
      }
  
      curDay = curDate.getDate();
      
      if(curDay.toString().length == 1){
        curDay = "0" + curDay;
      }
  
      //alert(curDay);
    }

    curUserDate = curYear.toString() + curMonth.toString() + curDay.toString();

   }else{
      tmpDate = curUserDate.substr(0,4) + "/" + curUserDate.substr(4,2) + "/" + curUserDate.substr(6,2);
      var curDate = new Date(tmpDate);

      curYear = curDate.getFullYear();

      curMonth = curDate.getMonth() + 1;

      if(curMonth.toString().length == 1){
         curMonth = "0" + curMonth;
      }

      curDay = curDate.getDate();

      if(curDay.toString().length == 1){
        curDay = "0" + curDay;
      }

   }

  //curDay = (curDay.substr(0,1) == "0") ? curDay.substr(1,1) : curDay;

  //curMonth = (curMonth.substr(0,1) == "0") ? curMonth.substr(1,1) : curMonth;

  //alert("YEAR: " + parseInt(curYear) + ", MONTH: " + parseInt(curMonth, 10) + ", DAY: " + parseInt(curDay));

  //curUserDate = curYear.toString() + curMonth.toString() + curDay.toString();

  objCal_popup = new clsWeekView_popup("objCal_popup", "setCalDate", parseInt(curYear), parseInt(curMonth, 10), parseInt(curDay));

  //objCal = new clsWeekView("objCal", "setFlashDate", 2002, 11, 8);

  //objCal.dateDiv = "divDateHeader";

  //objCal.setHeader();

  objCal_popup.backgroundImage = "images/bg_iframetop.gif";

  objCal_popup.setBackground();

}



function showCal(obj, form) {
  curCalObj = eval("document.frmMain." + form);



  var calXPos = getPageOffsetLeft(obj);

  var calYPos = getPageOffsetTop(obj);

  var calDiv = document.getElementById("cal_objCal_popup");



  //hideSelect(calXPos, calYPos);



  calDiv.style.pixelTop = calYPos;

  calDiv.style.pixelLeft = calXPos;

  calDiv.style.visibility = "visible";



}


function prevMonth(){
  //alert(curUserDate);
  curCalObj = eval("document.frmMain." + curFieldName);
  tmpYear = curUserDate.substr(0, 4);
  tmpMonth = curUserDate.substr(4, 2);
  tmpDay = 10;
  tmpMonth = parseInt(tmpMonth, 10);
  if(tmpMonth > 1){
     tmpMonth = parseInt(tmpMonth, 10) -1 ;
     if(tmpMonth.toString().length == 1){
         tmpMonth = "0" + tmpMonth;
     }
  }else if(tmpMonth == 1){
    tmpYear = tmpYear-1;
    tmpMonth = 12;
  }
  curUserDate = tmpYear.toString() + tmpMonth.toString() + tmpDay.toString();
  setupPopupCal(curFieldName, "blah");
}

function nextMonth(){
  //alert(curUserDate);
  curCalObj = eval("document.frmMain." + curFieldName);
  tmpYear = curUserDate.substr(0, 4);
  tmpMonth = curUserDate.substr(4, 2);
  tmpDay = 10;
  tmpMonth = parseInt(tmpMonth, 10);
  if(tmpMonth < 12){
     tmpMonth = parseInt(tmpMonth, 10) +1 ;
     if(tmpMonth.toString().length == 1){
         tmpMonth = "0" + tmpMonth;
     }
  }else if(tmpMonth == 12){
    tmpYear = parseInt(tmpYear)+1;
    tmpMonth = "01";
  }
  curUserDate = tmpYear.toString() + tmpMonth.toString() + tmpDay.toString();
  setupPopupCal(curFieldName, "blah");
}


function setCalDate(str) {


  //showSelect();
  //curCalObj = eval("document.frmMain." + curFieldName);

  var dateRegExp = /([0-9]{4})([0-9]{2})([0-9]{2})/;

  curCalObj.value = str.replace(dateRegExp, "$2/$3/$1");

  document.getElementById("cal_objCal_popup").style.pixelTop = -200;

}



// Hide all select boxes within a certain range of coordinates

function hideSelect(xBound, yBound) {



  showSelect();

  var formElement = document.getElementById("frmMain");

  var formSelects = formElement.getElementsByTagName("select");



  for (i = 0; i < formSelects.length; i++) {

    curEl = formSelects[i];



    curEl_x = getPageOffsetLeft(curEl);

    curEl_y = getPageOffsetTop(curEl);

    curEl_width = curEl.style.pixelWidth;



    //alert("X: " + curEl_x + ", Y: " + curEl_y + ", WIDTH: " + curEl.style.pixelWidth);



    curEl.style.visibility = getParentVisibility(curEl);



    if ((curEl_x >= xBound || (curEl_x + curEl_width) >= (xBound)) && (curEl_y >= yBound || (curEl_y + 20) >= yBound && (curEl_y + 20) <= (yBound + 150))) {

      curEl.style.visibility = "hidden";

    }



  }

}



function showSelect() {

  var formElement = document.getElementById("frmMain");

  var formSelects = document.getElementsByTagName("select");



  for (i = 0; i < formSelects.length; i++) {

    curEl = formSelects[i];

    //alert(curEl.parentElement.tagName);

    //curEl.style.visibility = "visible";

    curEl.style.visibility = getParentVisibility(curEl).toString();

  }



}


