
var divId = "0";

function showIndicator(id, val)
{
  document.getElementById(id).style.display = val;
}

function postNote2(ip)
{
  showIndicator('result2', 'none');

  var s = document.forms['mf2'].name.value;

  if (s=='')
  {
    alert("Please, type a link");
    return;
  }

  if (s.indexOf('http://')<0)
  {
    alert("Please, type a link");
    return;
  }

  var s1 = document.forms['mf2'].transcoder.checked;

  document.getElementById("result2").style.display="none";
  
  var query = 'n='+encodeURIComponent(s)+"&a="+encodeURIComponent(s1)+"&c="+ip;
  
  var uri = '/mp/savenote2.jsp';

  var beforeAction = function() 
  { document.getElementById("indicator2").style.display="inline"; };

  var afterAction = function() 
  { document.getElementById("indicator2").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction2, noteErrorFunction, beforeAction, afterAction);
}

function postNote3(ip)
{
  showIndicator('result3', 'none');

  var s = document.forms['mf3'].name.value;

  if (s=='') s = 'Shared link';

  var s1 = document.forms['mf3'].url.value;
  if (s1=='')
  {
    alert("Please, type a link");
    return;
  }

  if (s1.indexOf('http://')<0)
  {
    alert("Please, type a link");
    return;
  }

  var s2 = document.forms['mf3'].transcoder.checked;

  document.getElementById("result3").style.display="none";
  
  var query = 'n='+encodeURIComponent(s)+'&n1='+encodeURIComponent(s1)+"&a="+encodeURIComponent(s2)+"&c="+ip;
  
  var uri = '/mp/savenote3.jsp';

  var beforeAction = function() 
  { document.getElementById("indicator3").style.display="inline"; };

  var afterAction = function() 
  { document.getElementById("indicator3").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction3, noteErrorFunction, beforeAction, afterAction);
}

function postNote4(ip)
{
  showIndicator('result4', 'none');

  var s = document.forms['mf4'].name.value;

  if (s=='') 
  {
    alert("Please, provide a phone");
    return;
  }

  var s1 = document.forms['mf4'].txt.value;
  if (s1=='')
  {
    alert("Please, type a note");
    return;
  }

  setCookie('sndsmsmp',s,365);

  if (s1.length<=30)
  {

    var link="http://chart.apis.google.com/chart?cht=qr&chl=smsto:"+s+":"+s1+"&chs=300x300";
    var img = "<img border='0' alt='QR code' src=\""+link+"\"/>";
    var ans = "Use QR code for sending your SMS";
    ans+="<br/>";

    document.getElementById("result4").innerHTML=ans; 
    document.getElementById("result4").style.display="block";    


    document.getElementById("qr4").innerHTML=img; 

  }
  else
  {
    document.getElementById("result4").style.display="none";
    showIndicator('result4', 'inline'); 
  
    var query = 'n='+encodeURIComponent(s)+'&n1='+encodeURIComponent(s1)+"&c="+ip;
  
    var uri = '/mp/savenote4.jsp';

    var beforeAction = function() 
    { document.getElementById("indicator4").style.display="inline"; };

    var afterAction = function() 
    { document.getElementById("indicator4").style.display="none"; };

    noteAjaxEnginePost(uri, query, noteHandlerFunction4, noteErrorFunction, beforeAction, afterAction);
  }
}

function deleteLink(id)
{
 var oldDiv = document.getElementById(id);
 document.getElementById("qr6").removeChild(oldDiv);
}

function addSite()
{
  var s = document.forms['mf6'].name.value;
  var s1 = document.forms['mf6'].url.value;

  if (s!='' && s1!='')
   if (s1.indexOf('http://')>=0)
   {
    var s2="<a style='color:red;font-weight:bold' href='javascript:void(0)' onClick=\"deleteLink('divId"+divId+"')\" title='delete link'>X</a> "+"<span style='color:blue'>"+s+"</span>";
    var newdiv = document.createElement('div');
    //var divIdName = 'my'+num+'Div';
    newdiv.setAttribute('id',"divId"+divId); divId++;
    newdiv.setAttribute('lnk',s1); 
    newdiv.setAttribute('title',s); 
    newdiv.setAttribute('trans',''+document.forms['mf6'].transcoder.checked);
     
    newdiv.innerHTML = s2;
    document.getElementById("qr6").appendChild(newdiv);
   }
}

function postNote5(ip)
{
  showIndicator('result5', 'none');

  var s = document.forms['mf5'].name.value;

  if (s=='') 
  {
    alert("Please, provide a text");
    return;
  }


  document.getElementById("result5").style.display="none";
  showIndicator('result5', 'inline'); 
  
  var query = 'n='+encodeURIComponent(s)+"&c="+ip;
  
  var uri = '/mp/savenote5.jsp';

  var beforeAction = function() 
    { document.getElementById("indicator5").style.display="inline"; };

  var afterAction = function() 
    { document.getElementById("indicator5").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction5, noteErrorFunction, beforeAction, afterAction);
}

function postNote6(ip)
{
 var children = document.getElementById('qr6').getElementsByTagName('div');
 if (children==null) return;
 if (children.length==0) return;

 var s="";

 for (var i=0; i<children.length; i++)
 {
   var o = children[i];
   var lnk = o.getAttribute('lnk');
   var title = o.getAttribute('title');
   var trans = o.getAttribute('trans');

   if ("true"==trans)
   {
     if (lnk.indexOf(".xml")>0 || lnk.indexOf("rss")>0 || lnk.indexOf("feedburner")>0)
       lnk="http://www.google.com/reader/m/view/feed/"+escape(lnk);
     else
       lnk="http://www.google.com/gwt/n?_gwt_noimg=1&u="+escape(lnk);
   }
   
   s+="<a href='"+lnk+"'>"+title+"</a><br/>";
   
 }

 //document.getElementById("result6").style.display="none";
 //showIndicator('result6', 'inline'); 
  
  var query = 'n='+encodeURIComponent(s)+"&c="+ip;
  
  var uri = '/mp/savenote6.jsp';

  var beforeAction = function() 
    { document.getElementById("indicator6").style.display="inline"; };

  var afterAction = function() 
    { document.getElementById("indicator6").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction6, noteErrorFunction, beforeAction, afterAction); 
}


function postNote(ip)
{
  showIndicator('result', 'none');

  var s = document.forms['mf'].name.value;

  if (s=='')
  {
    alert("Please, type a name");
    return;
  }

  var s1 = document.forms['mf'].phone.value;

  if (s1=='')
  {
    alert("Please, type a phone number");
    return;
  }

  document.getElementById("result").style.display="none";
  
  //var query = 'n='+encodeURIComponent(s)+"&a="+encodeURIComponent(s1)+"&c="+ip;
  //var uri = '/mp/savenote.jsp';

  var query = 'name='+encodeURIComponent(s)+"&phone="+encodeURIComponent(s1)+"&c="+ip;
  
  var uri = '/mshare/api.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 page ...");}

function noteHandlerFunction(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    //var res = eval("("+s+")");
    //s = res.tiny; 

    var ans="&nbsp;&nbsp;<strong>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?"+s+"\"/>";

    document.getElementById("result").innerHTML=ans; 
    document.getElementById("result").style.display="block";    

    document.getElementById("qr").innerHTML=img; 
  }
}


function noteHandlerFunction2(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var res = eval("("+s+")");
    s = res.tiny; 

    var ans="&nbsp;&nbsp;<strong>Mobile page:</strong> "+s+"<br/>";
    
    ans+="<br/><br/>";

    var img = "<img border='0' alt='QR code' src=\"http://linkstore.ru/servlet/qr?"+res.site+"\"/>";

    document.getElementById("result2").innerHTML=ans; 
    document.getElementById("result2").style.display="block";    

    document.getElementById("qr2").innerHTML=img; 
  }
}

function noteHandlerFunction3(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var res = eval("("+s+")");
    s = res.tiny; 

    var ans="&nbsp;&nbsp;<strong>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/>";
    
    ans+="<br/><br/>";

    var img = "<img border='0' alt='QR code' src=\"http://linkstore.ru/servlet/qr?"+res.site+"\"/>";

    document.getElementById("result3").innerHTML=ans; 
    document.getElementById("result3").style.display="block";    

    document.getElementById("qr3").innerHTML=img; 
  }
}

function noteHandlerFunction4(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var res = eval("("+s+")");
    s = res.url; 

    var img = "<img border='0' alt='QR code' src=\""+s+"\"/>";
    var ans = "Use QR code for sending your SMS";
    ans+="<br/><br/>";

    document.getElementById("result4").innerHTML=ans; 
    document.getElementById("result4").style.display="block";    

    document.getElementById("qr4").innerHTML=img; 
  }
}

function noteHandlerFunction5(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var res = eval("("+s+")");
    s = res.tiny; 

    var ans="&nbsp;&nbsp;<strong>Your draft SMS 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/>";
    
    ans+="<br/><br/>";

    var img = "<img border='0' alt='QR code' src=\"http://linkstore.ru/servlet/qr?"+res.site+"\"/>";

    document.getElementById("result5").innerHTML=ans; 
    document.getElementById("result5").style.display="block";    

    document.getElementById("qr5").innerHTML=img; 
  }
}

function noteHandlerFunction6(txt, xmlDoc)
{
 if (txt)
 {
  var s = trimResponse(txt);
  var res = eval("("+s+")");
  
  document.getElementById("dialog").innerHTML="<center><img alt='QR code' border='0' src='"+res.url+"'/></center>";

  $("#dialog").dialog({
    title: "QR code",
    width: 300,
    height: 320,
    resizable: true,
    draggable: true,
    position: "center"
  });


 }
}

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='Here is my phone'; 
     var bodyText='See my phone 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='my phone: '+sUrl;

     var message='http://twitter.com/home?status='+escape(bodyText);

     location.href=message; }

function sendFace(sUrl)
{
 var u=sUrl; t='My phone';
 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 block(s)
{
  var o = document.getElementById(s);
  if (o.style.display=='block') o.style.display=='none';
  else
  {
    for (var i=1; i<=5; i++)
    {
       var sO="action"+i;
       if (sO==s) 
       {
         document.getElementById(sO).style.display='block';
         document.getElementById(sO+"_m").style.display='block';
         document.getElementById(sO+"_a").style.display='none';

         var form="mf";
         if (i>1) form+=i;
         if (i==4)
         {
          var ini = getCookie('sndsmsmp');
          if (ini!='')
           document.forms[form].name.value = ini; 
         }
         document.forms[form].name.focus(); 
         
       }         
       else
       {
         document.getElementById(sO).style.display='none'; 
         document.getElementById(sO+"_m").style.display='none';
         document.getElementById(sO+"_a").style.display='inline';
       }
    }
     
  }
}

 function getExpirydate( nodays)
      {
        var UTCstring;
        Today = new Date();
        nomilli=Date.parse(Today);
        Today.setTime(nomilli+nodays*24*60*60*1000);
        UTCstring = Today.toUTCString();
        return UTCstring;
      }

      function getCookie(cookiename) 
      {
      var cookiestring=""+document.cookie;
       var index1=cookiestring.indexOf(cookiename);
       if (index1==-1 || cookiename=="") return ""; 
       var index2=cookiestring.indexOf(';',index1);
       if (index2==-1) index2=cookiestring.length; 
       return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
      }

     function setCookie(name,value,duration)
     {
      cookiestring=name+"="+escape(value)+";EXPIRES="+getExpirydate(duration);
      document.cookie=cookiestring;
     }

