<!--

function encode64(inp)
{
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
	"abcdefghijklmnopqrstuvwxyz" + //all lowercase
	"0123456789+/=";
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter
	
	do { //Set up the loop here
	chr1 = inp.charCodeAt(i++); //Grab the first byte
	chr2 = inp.charCodeAt(i++); //Grab the second byte
	chr3 = inp.charCodeAt(i++); //Grab the third byte
	
	//Here is the actual base64 encode part.
	//There really is only one way to do it.
	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;
	
	if (isNaN(chr2)) {
	enc3 = enc4 = 64;
	} else if (isNaN(chr3)) {
	enc4 = 64;
	}
	
	//Lets spit out the 4 encoded bytes
	out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
	keyStr.charAt(enc4);
	
	// OK, now clean out the variables used.
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";
	
	} while (i < inp.length); //And finish off the loop
	
	//Now return the encoded values.
	return out;
}


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(random_key) {
	var video_url = document.getElementById('q').value;
	var re=/videoid=(.*[^>])/g;
	var matches;
	var videoid;
	
	if (matches = re.exec(video_url)) 
		videoid=matches[1];
	else
		videoid='';
	
	if(video_url.length>20){

		// disable both text and button field
		document.getElementById("q").disabled = true;
		document.getElementById("b").disabled = true;
		
		document.getElementById('ads_left').style.visibility='hidden';
		document.getElementById('ads_left').style.display='none';
		document.getElementById('ads_right').style.visibility='hidden';
		document.getElementById('ads_right').style.display='none';
				
		document.getElementById('ads_left_ajax').style.visibility='visible';
		document.getElementById('ads_left_ajax').style.display='inline';
		document.getElementById('ads_right_ajax').style.visibility='visible';
		document.getElementById('ads_right_ajax').style.display='inline';	
		
		// display loading things
		loading();
		
		// okay, send request to starfleet
		http.open('get', 'robot.php?key='+random_key+'&url='+encode64(video_url));
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	else{
		// yeah, at least please tell me what to download :P
		alert('Please enter video URL you wish to download.');
	}
}
function handleResponse() {
    if(http.readyState == 4){
       	var response = http.responseText;
        
		document.getElementById("result").innerHTML = response;
		
		// undisabled all disabled items
		document.getElementById("q").disabled = false;
		document.getElementById("b").disabled = false;
		document.getElementById("q").value = '';
        return false;
        //}
    }
}

function loading(){
	document.getElementById("result").innerHTML = "<p><img src='img/o.gif' alt='loading..' /></p>";
	return false;
}


-->
