var useContactCompose = false;

/* ---- Toolbar Button Actions ---- */
// Enables the correct toolbar buttons when checkboxes are checked and unchecked
function initButtons(total){
  var toolbar = document.getElementById('toolbar');
  if (toolbar == null) return false;
  var icons = toolbar.getElementsByTagName('li');
  var tempPage = document.body.className.split(" ");
  var currentPage;

  
  // If total is greater than 2, make it 2
  total = total > 1 ? 2 : total;
  
  var good = false;
  for (var i=1; i>=0; i--) {
    if (!tempPage[i]) continue;
    currentPage = tempPage[i];
    if(currentPage == "msglist" && isMailServerPrimary != 1) {
        currentPage = "msglist1";
    }
    if (buttonArray[currentPage]
      && buttonArray[currentPage] != null
      && icons.length == buttonArray[currentPage][total].length) {
        good = true;
        break;
    }
  }
  if (!good) return false;


  if (icons.length == buttonArray[currentPage][total].length) {
    var anchor, span;
    for (var i=0; i<icons.length; i++) {
      anchor = icons[i].getElementsByTagName('a').item(0);
      span = icons[i].getElementsByTagName('span').item(0);
      if (buttonArray[currentPage][total][i]) {
        if (anchor) anchor.style.display = 'block';
        if (span) span.style.display = 'none';
      } else {
        if (anchor) anchor.style.display = 'none';
        if (span) span.style.display = 'block';
      }
    }
  }
}

// This lets us know which buttons to enable/disable in each screen
var buttonArray = {
  msglist:[
    [ true, true, true, false, false, false, false, false ],
    [ true, true, true, true, true, true, true, true ], 
    [ true, true, true, false, false, false, true, true ]
  ],
  msglist1:[
    [ true, true, true, false, false, false, false ],
    [ true, true, true, true, true, true, true ], 
    [ true, true, true, false, false, false, true ]
  ],
  Draft:[
    [ true, true, true, false, false, false, false, false ],
    [ true, true, true, false, false, false, false, true ],
    [ true, true, true, false, false, false, false, true ]
  ],
  addrbook:[
    [ true, true, true, true, true, false, true, true, false ],
    [ true, true, true, true, true, true, true, true, true ],
    [ true, true, true, true, true, false, true, true, true ]
  ],
  SentMail:[
    [ true, true, true, false, false, false, false ],
    [ true, true, true, true, true, true, true ], 
    [ true, true, true, false, false, false, true ]
  ],
  junk:[
    [ true, true, true, false, false, false, false, false, true ],
    [ true, true, true, true, true, true, true, true, true ], 
    [ true, true, true, false, false, false, true, true, true ]
  ]
};

function genericButtonAction(popupTemplate, formAction, submitFunction, height, width) {
	var formobj = document.getElementById("Webmail");
	if (popupTemplate != null) {
		doPopup(formobj, popupTemplate, height, width);
	}
	else {
		noPopup(formobj);
	}
	if (formAction != null) {
		formobj.action += formAction;
	}
	imageSubmit(formobj, submitFunction);
}



/* ---- Global Buttons ---- */

function getemailButton() {
}

var composetempl = "";
function doCompose()	{
	composeButton();
	return false;
}

function composeButton() {

	if (document.execCommand && is_ie5_5up && !is_mac && useHTML != "n") {
		composetempl = "&templ=compose2.html";
	}
	var composeHeight = 720;
	var composeWidth = 696;
	if (useContactCompose){
		var button = document.getElementById("tool-compose");
                var a = button.getElementsByTagName("a")[0];
		contactCompose(a, composeHeight, composeWidth);
	}
	else {
		pop(curl+'&js=yes&popup=yes'+composetempl, composeWidth, composeHeight, "_blank");
	}
}

function videomailButton() {
	if (isMailServerPrimary) {
		if (is_win && is_ie6up) {
			if (useContactCompose && abContactSelected())      {
				return videoMailSelect(document.addr_list);
			}
			else
				vmLauncherCheck();
		} 
		else {
			alert("Comcast Video Mail can be used on Windows computers running Windows 98SE, ME, 2000, XP Professional or XP Home operating systems. Integration with Webmail is only supported in Internet Explorer 6.0 and later.");
		}
	}
}



/* ---- Message List Buttons ---- */

function replyButton() {
	genericButtonAction("reply", "&js=yes", "e_reply", "695", "700");
}

function replyallButton() {
	genericButtonAction("reply", "&js=yes", "e_replyall", "695", "700");
}

function forwardButton() {
	genericButtonAction("forward", "&js=yes", "e_forward", "695", "700");
}

function reportspamButton() {
	genericButtonAction(null, null, "e_doublecheck_spam");
}

function notspamButton() {
	genericButtonAction(null, null, "e_notspam_marked");
}

function emptyButton(obj) {
	return confirmEmpty(obj);
}

function deleteButton() {
	genericButtonAction(null, null, "e_del_marked");
}

// is this the same as emptyButton?
function trashButton() {
	return confirmEmpty(this);
}



/* ---- Readmail Buttons ---- */

function printButton(url) {
	if (typeof print != "undefined") {
                print();
	}
	else {
		pop(url, 520, 570);
	}
}



/* ------ The old VM functions - cleaned up a bit ------ */
function vmLauncherCheck() {
	popVMwindow("/wmv/vm_checkLauncher.html");
	return;
}

function popVMwindow(VMurl)	{

  var winHeight;
  var winWidth = 702;
  if (screen.height <= 666) {
    winHeight = eval(parseInt(screen.height * 0.75));
  } else {
    winHeight = 562;
  }
  var winLeft = eval((screen.width - 727)/2);
  var winTop = eval((screen.height - winHeight)/4);

    checkWin = window.open(
        VMurl,
        "videomail_launcher_check", 
        "scrollbars=yes,resizable=yes,menubar=yes,toolbar=no,status=no,location=no,address=no,width="+winWidth+",height=" + winHeight + ",top=" + winTop + ",left=" + winLeft);

    checkWin.focus();
    checkWin.opener = self;
    
    return checkWin;
}



