//Copyright John M Mizzi (MizziSoft) 2008


function mailCount()
{
	//var frameName = 'mailCount';
	var frameName = "FRAME~" + "MAIL" + "~PROCESS";
	createFrame(frameName, "", "update", "", "Mail");
	var process = "mail/mail.php";
	//var sendData = ('action=process&process=' + process + '&frame=' + frameName + '&command=' + 'mailcount' + '&msgUser=' + dry_su_id + '&bulkData=' + returnValue);
	var sendData = ('action=process&process=' + process + '&frame=' + frameName + '&command=' + 'mailcount');
	//alert(sendData);
	mailSendAction(sendData);
}


function getMail()
{
	var dry_su_id = document.body.getAttribute("diaryuser").toUpperCase();

	var frameName = "FRAME~" + "MAIL" + "~PROCESS";
	createFrame(frameName, "", "update", "", "Mail");
	var process = "mail/mail.php";
	var sendData = ('action=process&process=' + process + '&frame=' + frameName + '&command=' + 'getmail' + '&dry_su_id=' + dry_su_id);
	//alert(sendData);
	mailSendAction(sendData);
}



function sendMail(input_to, input_subject, input_body)
{
	var dry_su_id = document.body.getAttribute("diaryuser").toUpperCase();

	var frameName = "FRAME~" + "MAIL" + "~PROCESS";
	createFrame(frameName, "", "update", "", "Mail");
	var process = "mail/mail.php";
	var sendData = ('action=process&process=' + process + '&frame=' + frameName + '&command=' + 'sendmail' + '&dry_su_id=' + dry_su_id +
	                '&to=' + escape(input_to) + '&subject=' + escape(input_subject) + '&body=' + escape(input_body));
	//alert(sendData);
	mailSendAction(sendData);
}


function sendSimple(input_username, input_email, input_to, input_subject, input_body)
{
	//var frameName = "sendSimple";
	var frameName = "FRAME~" + "MAIL" + "~PROCESS";
	var process = "mail/mail.php";
	var sendData = ('action=process&process=' + process + '&frame=' + frameName + '&command=' + 'sendsimple' +
	                '&username=' + escape(input_username) + '&email=' + escape(input_email) +
	                '&to=' + escape(input_to) + '&subject=' + escape(input_subject) + '&body=' + escape(input_body));
	//alert(sendData);
	mailSendAction(sendData);
}


//Ajax Functions

function mailSendAction(input_action)
{
   var url = mainurl + "/mail/mail.php";
   var pars = input_action;


   var myAjax = new Ajax.Request(
         url, 
         {
            method:      "post", 
            parameters:  pars, 
            onComplete:  mailActionResponse,
            onFailure:   mailActionFailure
         });

   //alert("Set Url:" + url + ", " + pars);

}

function mailActionFailure(input_originalRequest)
{
   alert("Server Call Failed. Please try again.");
}

      
function mailActionResponse(input_originalRequest)
{
   //put returned XML in the textarea
   //$("result").value = input_originalRequest.responseText;

   var result = trim(input_originalRequest.responseText);

   //alert(result);

   if (result.indexOf("~[DB") == 0)
   {
      var dataTree = buildTree(result);

      var table = findKeyDO(dataTree, "TABLE").value;
      var tag = findKeyDO(dataTree, "TAG").value;
      var errorObj = findKeyDO(dataTree, "ERROR");
      if (errorObj != null)
      {
         if (errorObj.value != "")
            alert("Error: " + errorObj.value);
      }
   }
   else if (result.indexOf("~[MAIL") == 0)
   {
      var dataTree = buildTree(result);

      //var emails = findKeyDO(dataTree, "EMAILS").value;
      //alert("Emails:" + emails.length);

	   var frameName = "FRAME~" + "MAIL" + "~PROCESS";
      if ($(frameName) != null)
      {
			var frame = $(frameName);
			frame.innerHTML = frame.innerHTML  + "<br><br><br><br>" + viewDO2(dataTree);
         Element.hide("LOADING~" + frameName);
			frameExpand(frameName);
      }
      //else alert(viewDO2(dataTree));

      mailSent();

      /*
      alert(viewDO2(dataTree));
      var count = findKeyDO(dataTree, "count_mails").value;
      alert("Count:" + count);
      */
   }
   else if (result != "")
   {
      var resultArray = result.split("|");

      if (resultArray[0] == "PROCESS")
      {
			var frameName = resultArray[1];
			var answer = trim(resultArray[2]);
			if (frameName == "mailCount")
			{
			   alert(answer);
			}
         else if ($(frameName) != null)
         {
			   chooseDate();
			   var frame = $(frameName);
			   frame.innerHTML = frame.innerHTML  + "<br><br><br><br>" + resultArray[2]; 
            Element.hide("LOADING~" + frameName);
			   frameExpand(frameName);
         }
         else if (answer != "" && answer != "OK")
            alert(answer);
      }
      else alert(result);
   }
}




