
var MAX_URL_LENGTH = 20;

function moreQR()
{
  document.getElementById("moremashups").style.display='none';

  var o = document.getElementById("moreqr");

  if (o.style.display=='none')
   o.style.display='block';
  else
   o.style.display='none';
}

function moreMashups()
{
  document.getElementById("moreqr").style.display='none';

  var o = document.getElementById("moremashups");

  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 = "&nbsp;<em>"+file+"</em><br/><br/>";
}

function showIndicator(id, val)
{
  document.getElementById(id).style.display = val;
}

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 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)
{
  showIndicator('result', 'none');

  var s = document.forms['mf'].note.value;

  if (s=='')
  {
    alert("Please, type a note");
    return;
  }

  s=trimResponse(s);

  if (s=='')
  {
    alert("Please, type a note");
    return;
  }

  var trans = document.forms['mf'].trans.checked;
  var once = document.forms['mf'].once.checked;

  s = proceedURL(trans,s);

  re = new RegExp("([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})","g");
  s = s.replace(re, "<a href='mailto:$1'>$1</a>");

  var s1 = document.forms['mf'].attachment.value;

  document.getElementById("result").style.display="none";
  
  var query = 'n='+encodeURIComponent(s)+"&c="+ip;
  if (s1!='')
       query+="&a="+s1;

  if (once)
     query+="&o=1"; 

  var uri = '/qr/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 res = eval("("+s+")");
    s = res.tiny; 

    var ans="&nbsp;&nbsp;<strong>Your mobile page:</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/>";

    var img = "<img border='0' alt='QR code' src=\"http://linkstore.ru/servlet/qr?"+res.site+"\"/>";

    document.getElementById("result").innerHTML=ans; 
    document.getElementById("result").style.display="block";    

    document.getElementById("qr").innerHTML=img; 
  }
}

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 you: '+sUrl;

     var message='http://twitter.com/home?status='+escape(bodyText);

     location.href=message; }

function sendFace(sUrl)
{
 var u=sUrl; t='Mobile page';
 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;
}

function isImage(s)
{
  if (s.match(".gif"+"$")==".gif") return true;
  if (s.match(".jpg"+"$")==".jpg") return true;
  if (s.match(".png"+"$")==".png") return true;
  if (s.match(".jpe"+"$")==".jpe") return true;

  return false;
}

function proceedURL(trans, text)
{
  var ans="";
  var s = text;

  var i = s.indexOf("http://");
  if (i<0) i = s.indexOf("https://");
  var j = 0;

  while (i>=0 && s.length>10)
  {
    if (i>0) 
    {
      ans+=s.substring(0,i);
      s = s.substring(i);
    }

    j = getDelimiter(s);
    if (j<10) // not found
    {
      ans+=s.substring(0,1);
      s = s.substring(1);
    }
    else
    {
       ans += getURL(trans, s.substring(0,j));
       s = s.substring(j);   
    }           

    i = s.indexOf("http://");
    if (i<0) i = s.indexOf("https://");
  }

  ans+=s;

  return ans;
}

function getURL(trans, url)
{
     
  if (isImage(url))
    return "<br/><img src='"+url+"' alt='' border='0'/><br/>"; 
  else
   if (trans)
     return "<a href='http://www.google.com/gwt/n?u="+url+"'>"+getUrlName(url)+"</a>";
   else
     return "<a href='"+url+"'>"+getUrlName(url)+"</a>";
}

function getDelimiter(s)
{
   var c = '';

   var ans = 0;

   while (ans<s.length)
   {
     var c = s.substring(ans, ans+1);
     if (c==' ' || c=='\t' || c=='\r' || c=='\t') return ans;
     ans++;     
    }

   return ans;

}

function getUrlName(_s)
{
  var s = _s;
  if (s.indexOf("http://")==0) s=s.substring(7);
  else
    if (s.indexOf("https://")==0) s=s.substring(8);
  
  if (s.length > MAX_URL_LENGTH) 
    return s.substring(0,MAX_URL_LENGTH)+"...";
  else 
    return s;
 }


