
function api()
{
  var o = document.getElementById("api");
  if (o.style.display=='none')
   o.style.display='block';
  else
   o.style.display='none';
}

function setAttachment(title, file)
{
  document.forms['mf'].attachment.value=file+":"+title;
  showIndicator("indicator1", "none");
  showIndicator("adiv", "none");
  document.getElementById("ata").innerHTML = "<strong>Attachment:</strong>&nbsp;<em><a href='/pn/data/"+file+"'>"+title+"</a></em><br/><br/>";
}

function uploadFile()
{
  var t = document.forms['mf1'].title.value;

  if (t=='')
  {
    alert("Please, provide a title");
    return;
  }

  var f = document.forms['mf1'].fname.value;

  if (f=='')
  {
    alert("Please, provide a file");
    return;
  }

  showIndicator("indicator1", "inline");
  
  document.forms['mf1'].submit();
}

function showIndicator(id, val)
{
  document.getElementById(id).style.display = val;
}

function attachFile()
{
  showIndicator('result', 'none');
  document.getElementById("adiv").style.display="block";
  document.forms['mf1'].title.focus();
}

function closeAttachFile()
{
  document.getElementById("adiv").style.display="none";
}

 function postNote(ip)
 {
  var s = document.forms['mf'].note.value;

  if (s=='')
  {
    alert("Please, type a note");
    return;
  }

  var u  = document.forms['mf'].user.value;
  var p1 = document.forms['mf'].p1.value;
  var p2 = document.forms['mf'].p2.value;


  if (u=='')
  {
    alert("Please, provide a user name");
    return;
  }

  if (p1=='' || p2=='')
  {
       alert("Please, provide a password");
       return;
  }

  if (p1!=p2)
  {
       alert("Passwords do not match");
       return;
  }
    
  document.getElementById("result").style.display="none";

  var s1 = document.forms['mf'].attachment.value;
  
  var query = 'n='+encodeURIComponent(s)+"&c="+ip;
  if (u!='')
  {
     query+="&u="+encodeURIComponent(u);
     query+="&p="+encodeURIComponent(p1);
  }   

  if (s1!='')
       query+="&a="+s1;

  var uri = '/pn/savenote.jsp';

  var beforeAction = function() 
  { document.getElementById("indicator").style.display="inline"; };

  var afterAction = function() 
  { document.getElementById("indicator").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction, noteErrorFunction, beforeAction, afterAction);
 }

 function noteErrorFunction(){  alert("Could not save note ...");}

 function noteHandlerFunction(txt, xmlDoc)
 { if (txt)
   { var s = trimResponse(txt);
     var ans="&nbsp;&nbsp;<strong>Your link:</strong> "+s+"<br/>";
     ans+="&nbsp;&nbsp;<strong>Share it:</strong> ";
     ans+="<a href='javascript:void(0)' onClick=\"sendMail('"+s+"');\">email</a>";
     ans+="&nbsp;&nbsp;"
     ans+="<a href='javascript:void(0)' onClick=\"sendTwit('"+s+"');\">twitter</a>";
     ans+="&nbsp;&nbsp;"
     ans+="<a href='javascript:void(0)' onClick=\"sendFace('"+s+"');\">facebook</a>";
     ans+="<br/><br/>";
     document.getElementById("result").innerHTML=ans; 
     document.getElementById("result").style.display="block"; 
   }
 }

 function noteAjaxEnginePost(uri, query, handlerFunction, errorFunction, beforeAction, afterAction)
 {
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    if (handlerFunction==null)  handlerFunction = function() {};
    if (errorFunction==null)    errorFunction = function () {};
    if (beforeAction == null) beforeAction = function() {};
    if (afterAction == null) afterAction = function() {};  
    if (query==null) query="";
    var r = (window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
    if (r) { r.onreadystatechange = function()
      {	if (r.readyState == 4)
        {afterAction();
         if (r.status == 200)
  	 { xmlDoc = r.responseXML; txt = r.responseText;
           handlerFunction(txt, xmlDoc);}
         else          errorFunction();
        } }

     beforeAction(); r.open("POST", uri, true);
     r.setRequestHeader("Content-Type", contentType);
     r.setRequestHeader("Content-length", query.length);
     r.setRequestHeader("Connection", "close");
     r.send(query); return true;
  } else { afterAction(); errorFunction(); return false; }}

 function trimResponse(s0)
 { if (s0==null) return s0; s=s0;

  var s1 = ' ';
  while (s.length>0 && s1==' ')
  {
   s1 = s.substring(s.length-1); 
   if (s1==' ' || s1=='\r' || s1=='\n' || s1=='\t') { s=s.substring(0,s.length-1); s1=' '; }      
  }

  while (s.length>0)
  { s1=s.substring(0,1);
    if (s1==' ' || s1=='\r' || s1=='\n' || s1=='\t') s=s.substring(1);
    else  return s; }

  return '';}

 function sendMail(sUrl)
 {    var subjectLine='Take a look at this note'; 
     var bodyText='You can see this note at: '+sUrl;

     var message='mailto:?subject='+escape(subjectLine)+'&body='+escape(bodyText);
     var messageIE='mailto:?subject='+(subjectLine)+'&body='+escape(bodyText);     

     if(document.all) location.href=messageIE; 
     else  location.href=message; }

 function sendTwit(sUrl)
 {    var bodyText='a note for your: '+sUrl;

     var message='http://twitter.com/home?status='+escape(bodyText);

     location.href=message; }

 function sendFace(sUrl)
 {
  var u=sUrl; t='Protected note';
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
 }
