
function openPass()
{
 document.getElementById("pwdarea").style.display="block";
 document.forms['mf'].user.focus();
}

function closePass()
{
 document.forms['mf'].user.value="";
 document.forms['mf'].p1.value="";
 document.forms['mf'].p2.value="";

 document.getElementById("pwdarea").style.display="none";
}

function addUrl()
{
  var s = document.forms['mf'].video.value;

  if (s=='')
  {
    alert("Please, provide a Youtube URL for video");
    return;
  }

  if (s.indexOf("youtube.com")<0)
  {
    alert("It is not a YouTube URL");
    return;
  }

  if (s.indexOf("watch?v=")<0)
  {
    alert("It is not a YouTube video URL");
    return;
  }

  hideResult();
  
  var query = "u="+encodeURIComponent(s);

  var uri = '/mvideo/youtube.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 addVideo()
{
  var s = document.forms['mf'].mvideo.value;

  if (s=='')
  {
    alert("Could not get a mobile video for this site");
    return;
  }

  if (s.indexOf("m.youtube.com")<0)
  {
    alert("It is not a YouTube URL");
    return;
  }

  var s1 = document.forms['mf'].vtitle.value;  

  if (s1=='')
  {
    alert("Please, provide a title for this video");
    return;
  }

  hideResult();
  
  document.getElementById("indicator1").style.display="inline"; 

  appendOptionLast(s1, s);

  document.getElementById("indicator1").style.display="none"; 

  clearRes();

  if (document.forms['mf'].films.length>0) 
    document.getElementById("block2").style.display="inline"; 
}

function createSite()
{
  var s = document.forms['mf'].title.value;

  if (s=='')
  {
    alert("Please, provide a title for your mashup");
    return;
  }

  if (document.forms['mf'].films.length==0) 
  {
    alert("There are no video files");
    return;
  }

  hideResult();
  
  var query = "t="+encodeURIComponent(s);

  var elSel = document.forms['mf'].films;
  var i;
  for (i = 0; i<elSel.length; i++) 
  {
    var opt = elSel.options[i];
    query+="&n"+i+"="+encodeURIComponent(opt.text);
    query+="&v"+i+"="+encodeURIComponent(opt.value);
  }

  var uri = '/mvideo/savenote.jsp';

  var beforeAction = function() 
  { document.getElementById("indicator1").style.display="inline"; };

  var afterAction = function() 
  { document.getElementById("indicator1").style.display="none"; };

  noteAjaxEnginePost(uri, query, noteHandlerFunction1, noteErrorFunction1, beforeAction, afterAction);
}


function noteErrorFunction(){  alert("Could not get data ...");}

function noteErrorFunction1(){  alert("Could not save data ...");}

function noteHandlerFunction(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var s = eval("("+s+")");

    if (s.title=='')
    {
      alert("Sorry, there are no mobile data yet for this video");
      return;
    }

    document.forms['mf'].vtitle.value = s.title;
    document.getElementById("block1").style.display="inline"; 
    document.forms['mf'].vtitle.focus();
       
    document.forms['mf'].mvideo.value=s.url; 
  }
}

function noteHandlerFunction1(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    var s = eval("("+s+")");

    if (s.url=='')
    {
      alert("Sorry, could not create a site");
      return;
    }

    clearRes();
    clearOptions();
    document.getElementById("block2").style.display = "none";

    document.forms['mf'].title.value = '';
    document.forms['mf'].title.focus();

    var short = s.short; 
    var ans="<a href=\""+s.url+"\" target=\"_blank\">"+s.url+"</a><br/>";
    ans+="&nbsp;&nbsp;<strong>Share it:</strong> ";
    ans+="<a href='javascript:void(0)' onClick=\"sendMail('"+short+"');\">email</a>";
    ans+="&nbsp;&nbsp;"
    ans+="<a href='javascript:void(0)' onClick=\"sendTwit('"+short+"');\">twitter</a>";
    ans+="&nbsp;&nbsp;"
    ans+="<a href='javascript:void(0)' onClick=\"sendFace('"+short+"');\">facebook</a>";
    ans+="<br/><br/>";

    var img = "<img border='0' alt='QR code' src=\"http://linkstore.ru/servlet/qr?"+s.url+"\"/>";
      
    document.getElementById("result").innerHTML = ans;
    document.getElementById("result").style.display = "block";

    document.getElementById("qr").innerHTML=img; 
  }
}


function moreLinks()
{
  var s = document.getElementById("more").style.display;

  if (s=="block") 
  {
    document.getElementById("more").style.display = "none";
    document.getElementById("more0").innerHTML="...";
  }
  else
  {
    document.getElementById("more").style.display = "block";
    document.getElementById("more0").innerHTML=":";
  }
}

function filmInfo(id)
{
 var json='{"title":"","url":"'+'http://linkstore.ru/mvideo/data/'+id+'.htm","tiny":"'+'http://linkstore.ru/data/'+id+'.htm" }';
 noteHandlerFunction1(json, "");
}

function clearRes()
{
  document.forms['mf'].video.value='';
  document.forms['mf'].vtitle.value = '';
  document.getElementById("block1").style.display="none";  
  document.getElementById("result").style.display=''; 
}

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 site'; 
     var bodyText='You can see this site 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 site for your: '+sUrl;

     var message='http://twitter.com/home?status='+escape(bodyText);

     location.href=message; }

function sendFace(sUrl)
{
 var u=sUrl; t='Mobile site';
 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 delVideo()
{
 if (document.forms['mf'].films.selectedIndex>=0)
 {
  if (confirm("Delete selected video?"))
  {
    removeOptionSelected();
    if (document.forms['mf'].films.length==0) 
      document.getElementById("block2").style.display="none"; 
  }
  else
    document.forms['mf'].films.selectedIndex=-1;
 }
}

function appendOptionLast(text,value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  elOptNew.value = value;
  var elSel = document.forms['mf'].films;

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptionSelected()
{
  var elSel = document.forms['mf'].films;
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function clearOptions()
{
  var elSel = document.forms['mf'].films;
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
       elSel.remove(i); 
  }
}

function hideResult()
{
 document.getElementById("result").style.display="none";
 document.getElementById("qr").innerHTML="";
}




