﻿function ticketMMS(link) {

var test=link.toString().search("youtube");
if (test==-1)
 {
     window.open(link);
    return;
}

var prefix = "mms";
 playYoutube(link,prefix);


}
function ticketHTTP(link) {

    var test = link.toString().search("youtube");
    if (test == -1) {
        window.open(link);
        return;
    }

    var prefix = "http";
    playYoutube(link,prefix);


} 
function play()
 {
var url=window.location.search.toString().substr(5);
var urldec=unescape(url);
playYoutube(urldec);
}
function playYoutube(link,prefix)
 {
	var id1 = getId(link);
var url="http://www.youtube.com/get_video_info?video_id="+id1;
 // Tell the user that we're invoking the webservice
    //Construct an XMLHTTP Object to handle our HTTP Request
   
    
 var getvideo= XMLHttp(url,false);
  if (prefix=="http")
    {
    window.open(getvideo);
    }
    if (prefix=="mms")
    {
    var getvideo1=ChiamaWS(getvideo);
  window.open(getvideo1);
    }
 
 }
    
//test(client.responseText);
    function ParseResponse(data) 
    {
         var pippo = data.toString().indexOf("&title=");
         if ( pippo!= -1)
         {
         var title=unescape(data.toString().substring(pippo));
         }
         var urldecoded = unescape(data).toString().split("|");
         var urlencoded = data.toString().split("%7C");
     var x=0;
  while (x <urldecoded.length)
         { 
 var test=urldecoded[x].toString()
  if(test.search("itag=5&")!=-1)
             {
 
    // var mmsurl =unescape(urlencoded[x]+title);
      var mmsurl = unescape(urldecoded[x] + title);

     
   return mmsurl ;
             }
  x += 1;
          }
  
    
    }


    function getId(link)
     {
    	try {
    		var id = link.toString().split("=");
    		var end = id[1].toString().search("&");
    		var id1
    		if (end != -1)
    		         {
    			 id1= id[1].substring(0, end);
    	        	}
    		if (end == -1)
    	    	     {
    			 id1 = id[1].substring(0);
    	        	}
    		return id1;
        	}
    	catch (e) {
    		alert(e.message);
        	}

     }
function ChiamaWS(url)
 {
   //  var url1 = url.replace("&", "|");
     var url1 = "service1.svc/Play?url=" + url;
     var ws = true;
var url2= XMLHttp(url1,ws);
 return url2;
 }
 
 function XMLHttp(url,ws)
 {  
  var xmlHttpReq = null;
if (window.XMLHttpRequest) {
  // If IE7, Mozilla, Safari, and so on: Use native object.
  xmlHttpReq = new XMLHttpRequest();
}
else 
{
  if (window.ActiveXObject) {

    try
    {
        xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }   
    catch (err)
    {
        try
        {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (err2)
        {
        }
    }
 }
}
try {
    // Specify HTTP Method as POST so that we can upload large amounts of text
    xmlHttpReq.open('GET', url, false);
   
}
catch (e) {
    alert(e.message);
}
    // Set the content type to text/xml, otherwise the webservice will not
    // consider it a valid XML document
    xmlHttpReq.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
   
    // Set a callback to be invoked when the ReadyState changes on the XMLHTTP object.
    // If the ReadyState value == 4, then the request is completed.
    // See for more info: http://tinyurl.com/858me
  //  xmlHttpReq.onreadystatechange = function() {
       
   // }

   
    xmlHttpReq.send();
    if (xmlHttpReq.readyState == 4) 
    {
     var getvideo;
     if (ws == false) {
         getvideo = ParseResponse(xmlHttpReq.responseText);
     }
     else {
         getvideo = "test1.aspx?file=" + xmlHttpReq.responseXML.text;
      }
       return getvideo;
     
        
    }
 }
 

