function getXhr(){
	var xhr = null;
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject){ // Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
	}
	return xhr;
}


function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

//Load match comments depuis la liste des paris
function load_match_comments(ctn,cty,trn,id){	
	var xhr = getXhr();
	// On défini ce qu'on va faire quand on aura la réponse
	
	xhr.onreadystatechange = function() {
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if (xhr.readyState == 4 && xhr.status == 200) {
			resp = xhr.responseText;
			// On se sert de innerHTML pour rajouter les options a la liste
			document.getElementById('match_choisi').innerHTML =	resp;		
			var a = document.location.href;
			var pos = strpos(a, '#an', 0);
			var b;
			if (pos > 0) {
				b = a.substring(0,pos);
				b = a + '#an';			
			}
			else {
				b = a + '#an';
			}	
			//alert(b);
			//document.location.href = b;				
		}
	}

	// Ici on va voir comment faire du POST
	url = 'http://www.mybstat.com/_view/bs_bookmaker/avis_parieurs_match_choisi.php';
	//url = 'http://bstat.local/_view/bs_bookmaker/avis_parieurs_match_choisi.php';
	
	xhr.open("POST",url,true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var params = "ctn="+ctn+"&cty="+cty+"&trn="+trn+"&id="+id;
	
	xhr.send(params);	
}



//Load match comments depuis la sélection de match
function load_match_comments2(ctn,cty,trn){	
	
	var sel = document.getElementById('bookmaker_choix_match');
	var id = sel.options[sel.selectedIndex].value;				
	
	$.ajax({
	   type: "POST",
	   url: "http://www.mybstat.com/_view/bs_bookmaker/avis_parieurs_match_choisi.php",
	   data: "ctn="+ctn+"&cty="+cty+"&trn="+trn+"&id="+id,
	   success: function(msg){
		 $('#match_choisi').html(msg);
	   }
	 });
	//alert(ctn+'-'+cty+'-'+trn);
}



function load_match_comments4(page){	
	
	//alert(ctn+'-'+cty+'-'+trn);
	var sel = document.getElementById('bookmaker_choix_match');
	var id = sel.options[sel.selectedIndex].value;				
	
	$.ajax({
	   type: "POST",
	   url: "http://www.mybstat.com/_view/bs_bookmaker/avis_parieurs_match_choisi.php",
	   data: "page="+page,
	   success: function(msg){
		 $('#match_choisi').html(msg);
	   }
	 });						
	
}



function addC(cotematch, pari, optionchoisie, oc) {
	//Cote + Pari + Option choisie
	//oc = 1 décimale, oc=3 américaine, oc=2 fractionnelle

	coupon_ajax("cotematch="+cotematch+"&optionchoisie="+optionchoisie+"&pari="+pari+"&oc="+oc+"&acn=a");
}



function dropC(cotematch) {
	//Cote + Pari + Option choisie
	//oc = 1 décimale, oc=3 américaine, oc=2 fractionnelle
	coupon_ajax("cotematch="+cotematch+"&acn=s");
}

function finalize() {
	coupon_ajax("acn=f"); 
	
}

function validate() {
	coupon_ajax("acn=v"); 	
}


function retour() {
	//oc = 1 décimale, oc=3 américaine, oc=2 fractionnelle
	coupon_ajax("acn=r");
}

function calcul_gains() {
	var mise = $('#resume_pari_mises').val();
	coupon_ajax("acn=m&mise="+mise);
}

function coupon_ajax(sParam)
{
	 $.ajax({
	   type: "POST",
	   url: HTTP_PATH+"/_view/bs_bookmaker/coupon_ajax.php",
	   data: sParam,
	   success: function(msg){
		 $('#bs_bm_coupon').html(msg);
	   }
	 });
}



/**************** Lamine *****************************************************/
	//Load match comments depuis la s?lection de match
	function loadAvisParieursMatch(id, page){	
		$.ajax({
		   type: "POST",
		   url: "bs_bookmaker/avis_parieurs.php",
		   data: "bookmaker_choix_match="+id+"&page="+page,
		   success: function(msg){
		   //	alert(msg); return false;
			 $('#avis_parieurs').html(msg);
			  self.location.href = "#avis_parieurs";
		   }
		 });
	}
	
	function saveNewAviParieur(page){	
		var sel = document.getElementById('bookmaker_choix_match');
		var id = sel.options[sel.selectedIndex].value;
		
		var message = $('#message').val();	
		
		$.ajax({
		   type: "POST",
		   url: "bs_bookmaker/avis_parieurs.php",
		   data: "save_avis_parieurs=&bookmaker_choix_match="+id+"&message="+message+"&page="+page,
		   success: function(msg){
		   //	alert(msg); return false;
			 $('#avis_parieurs').html(msg);
		   }
		 });
	}
/**************** Lamine *****************************************************/
