function createAjaxObj() {
  var httprequest = false;
  if (window.XMLHttpRequest) { // if Mozilla, Safari etc
    httprequest = new XMLHttpRequest();
    if (httprequest.overrideMimeType) httprequest.overrideMimeType('text/xml');
  }
  else if (window.ActiveXObject) { // if IE
    try {
      httprequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e){
      try{
        httprequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){}
    }
  }
  return httprequest;
}

var ajaxpack = new Object()
ajaxpack.basedomain = "http://"+window.location.hostname
ajaxpack.ajaxobj = createAjaxObj()
ajaxpack.filetype = "txt"
ajaxpack.addrandomnumber = 0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest = function(url, parameters, callbackfunc, filetype) {
  ajaxpack.ajaxobj = createAjaxObj() //recreate ajax object to defeat cache problem in IE
  if(ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
  var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
  if(this.ajaxobj) {
    this.filetype = filetype;
    this.ajaxobj.onreadystatechange = callbackfunc;
    this.ajaxobj.open('GET', url+"?"+parameters, true);
    this.ajaxobj.send(null);
  }
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype) {
  ajaxpack.ajaxobj = createAjaxObj() //recreate ajax object to defeat cache problem in IE
  if(this.ajaxobj) {
    this.filetype = filetype;
    this.ajaxobj.onreadystatechange = callbackfunc;
    this.ajaxobj.open('POST', url, true);
    this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.ajaxobj.setRequestHeader("Content-length", parameters.length);
    this.ajaxobj.setRequestHeader("Connection", "close");
    this.ajaxobj.send(parameters);
  }
}

function processGetPost(){
  var myajax = ajaxpack.ajaxobj;
  var myfiletype = ajaxpack.filetype;
  if(download_loading_count > 41) {}
  else {
    if (myajax.readyState == 4){ //if request of file completed
      if (myajax.status==200 || window.location.href.indexOf("http")==-1){
        if (myfiletype=="txt") {
          document.getElementById('download_details').style.display = 'block';
          hideDownloadsLoading();
          document.getElementById('download_details').innerHTML = myajax.responseText;
        }
        else {
          alert(myajax.responseXML);
        }
      }
    }
  }
}

function createpoststring() {
  if(selected_form == 0) {
    var url_value = document.getElementById("chooser_form").url.value;
    var site_value = document.getElementById("chooser_form").site.value;
    var poststr = "url=" + escape(url_value) + "&site=" + escape(site_value);
  }
  else {
    var url_value = document.getElementById("chooser_form_2").url.value;
    var site_value = document.getElementById("chooser_form_2").site.value;
    var poststr = "url=" + escape(url_value) + "&site=" + escape(site_value);
  }
  document.getElementById("chooser_form_2").url.value = "";
  return poststr;
}

var last_tab = '6';
var last_info = '1';
var download_loading_int = false;
var download_loading_count = 0;
var selected_form = 1;
var history_value = '';

function change_tab(tab, site) {
  if(site == "other") {
    document.getElementById('chooser_set_1').style.display = 'none';
    document.getElementById('chooser_set_2').style.display = 'block';
    selected_form = 1;
    document.chooser_form_2.site.selectedIndex = 0;
  }
  else {
    document.getElementById('chooser_set_2').style.display = 'none';
    document.getElementById('chooser_set_1').style.display = 'block';
    selected_form = 0;
  }

  //document.getElementById('info_'+last_info).style.display = 'none';
  document.getElementById('example_'+last_info).style.display = 'none';

  document.getElementById('tab_'+last_tab).className = 'chooser_tab_inactive';
  document.getElementById('tab_'+tab).className = 'chooser_tab_active';
  document.getElementById('example_'+last_tab).style.display = 'none';
  document.getElementById('example_'+tab).style.display = 'inline';
  //document.getElementById('info_'+last_tab).style.display = 'none';
  //document.getElementById('info_'+tab).style.display = 'inline';
  document.chooser_form.site.value = site;
  last_tab = tab;
  last_info = tab;
}

function change_info(info) {
  info = info + 1;
  document.getElementById('example_'+last_info).style.display = 'none';
  document.getElementById('example_'+info).style.display = 'inline';
  //document.getElementById('info_'+last_info).style.display = 'none';
  //document.getElementById('info_'+info).style.display = 'inline';
  last_info = info;
}

function openDownloads() {
  selected_form_name = ''; if(selected_form == 1){selected_form_name = 'chooser_form_2';} else {selected_form_name = 'chooser_form';}
  url_value = document.forms[selected_form_name].url.value;
  if(url_value == "") 
    {alert('Lütfen Video Adresi Girin');document.forms[selected_form_name].url.focus();}
  else {
    document.getElementById('download_container').style.display = 'block';
    document.getElementById('download_details').style.display = 'inline';
    //document.getElementById('download_loading').style.display = 'inline';
    //document.getElementById('download_loading_c').innerHTML = '';
    //addHistory();
    //if(download_loading_int) {
    //  clearInterval(download_loading_int);
    //}
    //download_loading_count = 0;
    
    //ajaxpack.postAjaxRequest("/index_p/", createpoststring(), processGetPost, "txt");
	document.chooser_form_2.submit();
    document.getElementById("chooser_form_2").url.value = "";
    //download_loading_int = setInterval('if(download_loading_count == 40){hideDownloadsLoading();document.getElementById(\'download_details\').style.display = \'block\';document.getElementById(\'download_details\').innerHTML = \'Error: Timeout. Please try again.\';}download_loading_count++;document.getElementById(\'download_loading_c\').innerHTML += "."', 2000);
  }
}

function hideDownloadsLoading() {
  if(download_loading_int) {
    clearInterval(download_loading_int);
  }
  document.getElementById('download_loading').style.display = 'none';
}

function htmlspecialchars(str){
    return str.replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g, "&gt;").replace(/'/g, "&#039;").replace(/"/g, "&quot;");
}

function addHistory() {
  selected_form_name = ''; if(selected_form == 1){selected_form_name = 'chooser_form_2';} else {selected_form_name = 'chooser_form';}
  url_value = document.forms[selected_form_name].url.value;
  
  if(history_value.match(escape(url_value + '..<br>..')) == null) {
    document.getElementById('download_history').innerHTML = htmlspecialchars(url_value) + '<br>' + document.getElementById('download_history').innerHTML;
    history_value += escape(url_value + '..<br>..');
  }
}

function keepTop20(num) {
  scroll(0,0);
  document.getElementById('download_container').style.display = 'block';
  document.getElementById('download_details').style.display = 'none';
  document.getElementById('download_loading').style.display = 'inline';
  document.getElementById('download_loading_c').innerHTML = '';

  url_value = document.getElementById('top_'+num+'_url').innerHTML;
  if(history_value.match(escape(url_value + '..<br>..')) == null) {
    document.getElementById('download_history').innerHTML = htmlspecialchars(url_value) + '<br>' + document.getElementById('download_history').innerHTML;
    history_value += escape(url_value + '..<br>..');
  }

  if(download_loading_int) {
    clearInterval(download_loading_int);
  }
  download_loading_count = 0;

  var url_value = document.getElementById('top_'+num+'_url').innerHTML;
  var site_value = document.getElementById('top_'+num+'_site').innerHTML;

  createpoststring2 = "url=" + escape(url_value) + "&site=" + escape(site_value);
  
  ajaxpack.postAjaxRequest("/index_p/", createpoststring2, processGetPost, "txt");
  download_loading_int = setInterval('if(download_loading_count == 40){hideDownloadsLoading();document.getElementById(\'download_details\').style.display = \'block\';document.getElementById(\'download_details\').innerHTML = \'Error: Timeout. Please try again.\';}download_loading_count++;document.getElementById(\'download_loading_c\').innerHTML += "."', 2000);
}
