function CreateRequestObject() {
	var req = false;
  // branch for native XMLHttpRequest object
  if(window.XMLHttpRequest && !(window.ActiveXObject)) {
      try {
          req = new XMLHttpRequest();
      } catch(e) {
          req = false;
      }
  // branch for IE/Windows ActiveX version
  } else if(window.ActiveXObject) {
         try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
          try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
                req = false;
          }
      }
  }
	return req;
}

function EcrireCookie(nom, valeur)
{
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : "/";
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function CreateHostCookie() {
	var HttpCreateHostCookie = CreateRequestObject();
	HttpCreateHostCookie.open('get', '/'+site_sub_dir+'ajax/new_host.php');
	HttpCreateHostCookie.onreadystatechange = function () { 
		if(HttpCreateHostCookie.readyState == 4){
			var response = HttpCreateHostCookie.responseText;
			if (response != ""){
				date=new Date;
				date.setFullYear(date.getFullYear()+1);
				EcrireCookie("TlfHostId", response, date, "/");
			}
		}
	};
	HttpCreateHostCookie.send(null);
}

function LogTlfHostConnexion(host_id) {
	var HttpLogTlfHostConnexion = CreateRequestObject();
	HttpLogTlfHostConnexion.open('get', '/'+site_sub_dir+'ajax/log_host_connexion.php?host_id='+host_id);
	HttpLogTlfHostConnexion.send(null);
}

function LogHostFlmView(flm_id) {
	var HttpLogHostFlmView = CreateRequestObject();
	HttpLogHostFlmView.open('get', '/'+site_sub_dir+'ajax/log_host_flm_view.php?flm_id='+flm_id);
	HttpLogHostFlmView.send(null);
}

function LogHostBaView(flm_id) {
	var HttpLogHostBaView = CreateRequestObject();
	HttpLogHostBaView.open('get', '/'+site_sub_dir+'ajax/log_host_ba_view.php?flm_id='+flm_id);
	HttpLogHostBaView.send(null);
}

function LogSearch(q, flm_id) {
	var HttpLogSearch = CreateRequestObject();
	HttpLogSearch.open('get', '/'+site_sub_dir+'ajax/log_search.php?q='+q+'&flm_id='+flm_id);
	HttpLogSearch.send(null);
}

function LogClickUrl(flm_id, ste_id) {
	var HttpLogClickUrl = CreateRequestObject();
	HttpLogClickUrl.open('get', '/'+site_sub_dir+'ajax/click_url.php?flm_id='+flm_id+'&ste_id='+ste_id);
	HttpLogClickUrl.send(null);
}

function installSearchEngine() {
	date=new Date;
	date.setFullYear(date.getFullYear()+1);
	EcrireCookie('tlf_searchInstall','hide', date, "/");
	if (window.external && ("AddSearchProvider" in window.external)) {
	 // Firefox 2 and IE 7, OpenSearch
	 window.external.AddSearchProvider("http://www.trouvelefilm.com/opensearch.xml");
	}
	document.getElementById('search_pointer').className = "textoff";
}

function hideInstallSearchEngine() {
	date=new Date;
	date.setFullYear(date.getFullYear()+1);
	EcrireCookie('tlf_searchInstall','hide', date, "/");
	document.getElementById('search_pointer').className='textoff';
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

function getDocumentWidth() {
	var wdt = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		wdt = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		wdt = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		wdt = document.body.clientWidth;
	}
	return wdt;
}

var advertAdsense2WidthSize = -1;
if (getDocumentWidth() >= 1200) {
	advertAdsense2WidthSize = 1200;
} else {
	advertAdsense2WidthSize = 830;
}

function onDocumentResizeShowHideAdsense() {
	if (document.getElementById('idAdvertAdsense2') != null) {
		if (getDocumentWidth() < advertAdsense2WidthSize) {
			document.getElementById('idAdvertAdsense2').style.display = 'none';
		} else {
			document.getElementById('idAdvertAdsense2').style.display = 'block';
		}
	}
}
