function setKSTheme(theme){
	setCookie('KSTheme',theme);
	location.reload();
}
function addFavoris(id){
	sMoteur = document.getElementById("sitesFav");
	moteur = sMoteur.options[sMoteur.selectedIndex].value;
	nom = 'KSFavoris' + id;
	setCookie(nom,moteur);
	location.reload();
}
function isChecked(cb){
	if(document.getElementById(cb).checked == true){
		return true;
	}
	else{
		return false;
	}
}
function searchWith(moteur,kst){
	var q = document.getElementById('recherche').value;
	var url = 'url?moteur=' + encodeURIComponent(moteur) + '&q=' + encodeURIComponent(q);
	if(kst == 1 && isChecked('saveSearchCB')){
		url += '&enrg=on';
	}
	if(kst == 1 && isChecked('twitterCB')){
		url += '&twitter=on';
	}
	window.location.href = url;
}
function setCookie(sName, sValue) {
	var today = new Date(), expires = new Date();
	expires.setTime(today.getTime() + (365*24*60*60*1000));
	document.cookie = sName + "=" + sValue + ";expires=" + expires.toGMTString();
}


//AJAX
function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

function actualise(selectSource, selectActualise,moinsFav,moinsKST){
	var xhr = getXMLHttpRequest();
	var sType = document.getElementById(selectSource);
	var type = sType.options[sType.selectedIndex].value;
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			loadData(xhr.responseXML,selectActualise);
		}
	};
	xhr.open('GET','moteurs.php?t=' + type + '&moinsFav=' + moinsFav + '&moinsKST=' + moinsKST,true);
	xhr.send(null);
}

function loadData(oData,selectActualise) {
	var nodes   = oData.getElementsByTagName("item");
	var oSelect = document.getElementById(selectActualise);
	var oOption, oInner;
	
	oSelect.innerHTML = "";
	for (var i=0, c=nodes.length; i<c; i++) {
		oOption = document.createElement("option");
		oInner  = document.createTextNode(nodes[i].getAttribute("titre"));
		oOption.value = nodes[i].getAttribute("code");
		
		oOption.appendChild(oInner);
		oSelect.appendChild(oOption);
	}
}
function actualiseStats(type,div){
	var xhr = getXMLHttpRequest();
	var div = document.getElementById(div);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			div.innerHTML = xhr.responseText;
		}
	};
	var date = new Date();
	xhr.open('GET','statsAjax.php?type=' + type + '&d=' + date.getTime(),true);
	xhr.send(null);
}

function filtrerContenu(q,cb){
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			var rep = xhr.responseText;
			if(rep == 'oui'){
				document.getElementById(cb).checked = true;
				//document.getElementById('pervers').style.display = 'none';
			}
			else{
				document.getElementById(cb).checked = false;
				//document.getElementById('pervers').style.display = '';
			}
		}
	};
	xhr.open('GET','kstfiltreAjax.php?q=' + encodeURIComponent(q),true);
	xhr.send(null);
}

function goToMutliSearch(recherche,moteur){
	q = encodeURIComponent(document.getElementById(recherche).value);
	moteurIndex = document.getElementById(moteur);
	m = encodeURIComponent(moteurIndex.options[moteurIndex.selectedIndex].value);
	window.location.href = 'multi-search.php?q=' + q + '&moteur=' + m;
}


/* Multi Search Iframes */
function showUrl(url){
	document.getElementById('frame').src = url;var a = document.getElementById('frame');a && a.contentWindow && a.contentWindow.focus();
}

function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
function multiSearch(div){
	var tab = document.getElementsByTagName("input"); 
	var q = document.getElementById(div).value;
	q = encodeURIComponent(q);
	var tabM = new Array();
	var txt = '';
	var nbr = 0;
	var mot = '';
	for(i = 0; i < tab.length; i ++){
		if(tab[i].type == 'checkbox' && tab[i].className == 'cbMS' && tab[i].checked == true){
			tabM.push(tab[i].id);
		}
	}
	for(i = 0; i < tabM.length; i ++){
		mot += tabM[i] + '|';
		nbr ++;
	}	
	if(nbr < 6){
		window.location.href = 'multi-search-results?&q=' + q + '&moteurs=' + encodeURIComponent(mot);
	}
	else{
		document.getElementById('error_kms').style.display = '';
	}
	return false;
}

/* AMIS */
function actionAmis(id, action, launcher){
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText != ''){
				alert(xhr.responseText);
			}
			window.location.reload();
		}
	};
	xhr.open('GET','amisfonctions.php?action=' + action + '&id=' + id,true);
	xhr.send(null);
}
function openVideo(adresse,lecteur,div){
	var xhr = getXMLHttpRequest();
	var divAff = document.getElementById(div);
	
	if(lecteur == 'kyoutube'){
		var hash = adresse.substring(31, 42);
	}
	if(lecteur == 'kdailymotion'){
		var regex = /http:\/\/www\.dailymotion\.com\/video\/([a-zA-Z0-9])/;
		var hash = adresse.replace(regex, "$1");
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			divAff.innerHTML = xhr.responseText;
		}
		else if(xhr.readyState < 4){
		}
	};

	xhr.open('GET','kresultsOpenMedia.php?player=' + lecteur + '&hash=' + hash,true);
	xhr.send(null);
}

function twitterRecherche(id){
	var xhr = getXMLHttpRequest();
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			alert(xhr.responseText);
		}
	};

	xhr.open('GET','kstfonctions.php?action=twitter&id=' + id,true);
	xhr.send(null);
}