/*####################################################################################################
## Software Name        : YouTube Downloader Script													##
## Website              : http://www.youtubedownloaderscript.com									##
##																									##
## This software is not an open source project and is subject to the End User License Agreement		##
## available online at http://www.youtubedownloaderscript.com/eula.php and in this package.			##
##																									##
## By using this software, you acknowledge having read this Agreement and agree to be bound thereby.##
## All the copyright notices and comments must remain intact, removing them is illegal.				##
##																									##
## In the event that any of the terms are violated, you will be exceeding the rights provided,		##
## thereby making null and void any permission granted, and making the use of this software an		##
## infringement of international or local applicable copyright laws which could lead you to Court.	##
##																									##
## Copyright (c) 2011 youtubedownloaderscript.com - All rights reserved.							##
####################################################################################################*/

var xhrConvert = null;
var xhrResult = null;
var xhrUnlink = null;
if (window.XMLHttpRequest) {
	xhrConvert = new XMLHttpRequest();
	xhrResult = new XMLHttpRequest();
	xhrUnlink = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	try {
		xhrConvert = new ActiveXObject("Msxml2.XMLHTTP");
		xhrResult = new ActiveXObject("Msxml2.XMLHTTP");
		xhrUnlink = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xhrConvert = new ActiveXObject("Microsoft.XMLHTTP");
			xhrResult = new ActiveXObject("Microsoft.XMLHTTP");
			xhrUnlink = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e1) {
			xhrConvert = null;
			xhrResult = null;
			xhrUnlink = null;
		}
	}
} else {
	alert("XMLHttpRequest not supported!")
}

function unlink(tmpfile) {
	xhrUnlink.open("GET","unlink.php?rand="+ Math.floor(Math.random()*99999) +"&id="+ tmpfile,true);
	xhrUnlink.send(null);
}

function getResult(tmpID) {
	xhrResult.open("GET","result.php?rand="+ Math.floor(Math.random()*99999) +"&tmpID="+ tmpID,true);
	xhrResult.onreadystatechange = function() {
		if (xhrResult.readyState == 4) {
			var result = xhrResult.responseText;
			if (result == "unknown") {
				setTimeout("getResult('"+ tmpID +"');", 3000);
			} else {
				document.getElementById('process').innerHTML = result;
				unlink(tmpID);
			}
		}
	};
	xhrResult.send(null);
}

function convert(processID) {
	xhrConvert.open("GET","functions.php?convert=1&rand="+ Math.floor(Math.random()*99999) +"&processID="+ processID,true);
	xhrConvert.send(null);
	
	if (document.getElementById('SPBL')) {
		document.getElementById('SPBL').style.visibility = 'visible';
	}
	var tmpID = document.getElementById('tmpID').innerHTML;
	setTimeout("getResult('"+ tmpID +"');", 3000);
}

function downloadClickDetected() {
	//This function is called when the download link is clicked.
	//You can put what you want here, i.e: open a pop-up, call an other function
	//alert('Javascript function fired!');
}

function progressClickDetected() {
	//This function is called when the "Show Progress Bar" link is clicked.
	//You can put what you want here, i.e: open a pop-up, call an other function
	//alert('Javascript function fired!');
}

function pop(url,width,height) {
	win1 = window.open(url,'win1','directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width='+ width +',height='+height+'');
	win1.window.focus();
}

function displayForm() {
	if (document.getElementById('form')) {
		document.getElementById('download').style.display = "none";
		document.getElementById('form').style.display = "block";
	}
	if (document.getElementById('linksBox')) {
		document.getElementById('linksBox').style.display = "none";
	}
}

function displayLinksBox() {
	if (document.getElementById('linksBox')) {
		document.getElementById('linksBox').style.display = "block";
	}
}

function displayProgressBox(url) {
	if (document.getElementById('SPBL')) {
		document.getElementById('SPBL').style.marginTop = "-3px";
		document.getElementById('SPBL').innerHTML = '<iframe src="'+ url +"&rand="+ Math.floor(Math.random()*99999) +'" style="width:360px;height:100px;border:none;"></iframe>';
	}
}
