/*
 * Author: Charles Lauzon
 * Name:   Functions_Recherche.js
 * Date:   5 Janvier, 2010
 */
 
/********************************************************************
 * 																	*
 *					   RECHERCHE ACCESS DIRECT                      *
 *																	*
 ********************************************************************/
//Variable Declaration
var kc; 
var key;
var strCheck = "0123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; //String de Verification
var currentTime = new Date();

//Acces Direct
//KeyPress es pour voir si on utiliser seulement des chiffres
function txtNoMaison_onKeyPress(e){
	//Prendre le keyCode de la key appuyer	
	if(e.keyCode){ kc = e.keyCode; }
	else if(e.which){ kc = e.which; }
	//Get String Code from the keyCode
	key = String.fromCharCode(kc);
	//Mettre en Majuscule
	key = key.toUpperCase();
	//Check If a Valid Key
	if(kc == 8 || kc == 9 || kc == 46 || kc == 37 || kc == 39){ //Efface ou Tab ou Del ou Gauche ou Droite
		return true;
	}else if(kc == 13){ //Enter
		Initialize();
		//Start Loading
		document.getElementById("rAD").style.display = "none";
		document.getElementById("rADL").style.display = "block";
		//Construct AJAX url
		var url="recherche_process.php?exec_noResiTempo=1&NoMaison="+document.fRecherche.txtNoMaison.value+"&DATE="+currentTime.getSeconds();
		if(req!=null){
			req.open('GET', url, false);
			req.send(null);
			if(req.status == 200){
				if(req.responseText != ""){
					var str = req.responseText;
					var arrStr = str.split("|");
					if(arrStr[0] == "OK"){
						//alert(arrStr[1]);
						document.fRecherche.MaisonId.value = arrStr[1];
						document.fRecherche.action = "maison.php";
						document.fRecherche.submit();
					}else if(arrStr[0] == "ERROR" && arrStr[1] == 1){
						alert("SVP, Entrez un numéro à quatre chiffres!");
						document.fRecherche.txtNoMaison.value = "";
					}else if(arrStr[0] == "ERROR" && arrStr[1] == 2){
						alert("Aucune propriété avec le numéro: "+document.fRecherche.txtNoMaison.value+"!");
						document.fRecherche.txtNoMaison.value = "";
					}
				}
			}else{
				document.getElementById("ville").innerHTML = "There was a problem retrieving data:<br>" + req.statusText;
			}
		}
		//Stop Loading
		document.getElementById("rAD").style.display = "block";
		document.getElementById("rADL").style.display = "none";
		return false;
	}else{ //Check If a Valid Key
		if(strCheck.indexOf(key) == -1){ return false; } //Invalid
		else{ if(document.getElementById("txtNoMaison").value.length < 4){ document.getElementById("txtNoMaison").value += key; return false; } } //Valid
	}
}
//Il a pas de boutton presentement...
//Boutton GO! for Submit
function btnAccessDirect_onClick(){
	Initialize();
	//Start Loading
	document.getElementById("divNoMaison").style.display = "none";
	document.getElementById("loadingNoMaison").style.display = "block";
	//Construct AJAX url
	var url="recherche_process.php?exec_noResiTempo=1&NoMaison="+document.fAccesDirect.txtNoMaison1.value+""+document.fAccesDirect.txtNoMaison2.value+""+document.fAccesDirect.txtNoMaison3.value+""+document.fAccesDirect.txtNoMaison4.value+"&DATE="+d;
	if(req!=null){
		req.onreadystatechange = ResiTempo_Process_Recherche;
		req.open("GET", url, true);
		req.send(null);
	}
}

/********************************************************************
 * 																	*
 *						  RECHERCHE RAPIDE                 	        *
 *																	*
 ********************************************************************/
var isPaysEmpty = true;
var isProvinceEmpty = true;
var isVilleEmpty = true;
var isChambreEmpty = true;
var isDistanceEmpty = true;
var isDateEmpty = true;
var isFlexibiliteEmpty = true;
var isDureeSejourEmpty = true;
//Recherche Rapide
function btnRechercheRapide_onClick(){
	//Set Le Message Erreurs
	msgErreurs = "SVP, Entrez toutes les informations demandées!\n";
	//Check Si tout a ete entre correctement
	//Pays
	if(document.getElementById("cboPays").selectedIndex != 0){
		isPaysEmpty = false;
		//document.getElementById("lblVille").style.color = "#FFFFFF";
	}else{
		//document.getElementById("lblVille").style.color = "#FF0000";
		msgErreurs += "\nLe Pays n\'a pas été sélectionné!";
	}
	//Province
	if(document.getElementById("cboPays").value == "CA"){
		if(document.getElementById("cboProvince").selectedIndex != 0){
			isProvinceEmpty = false;
			//document.getElementById("lblVille").style.color = "#FFFFFF";
		}else{
			//document.getElementById("lblVille").style.color = "#FF0000";
			msgErreurs += "\nLa Province n\'a pas été sélectionnée!";
		}
	}else if(document.getElementById("cboPays").value == "US"){
		if(document.getElementById("cboEtat").selectedIndex != 0){
			isProvinceEmpty = false;
			//document.getElementById("lblVille").style.color = "#FFFFFF";
		}else{
			//document.getElementById("lblVille").style.color = "#FF0000";
			msgErreurs += "\nL'États n\'a pas été sélectionné!";
		}
	}else if(document.getElementById("cboPays").value == "FR"){
		if(document.getElementById("cboDepartement").selectedIndex != 0){
			isProvinceEmpty = false;
			//document.getElementById("lblVille").style.color = "#FFFFFF";
		}else{
			//document.getElementById("lblVille").style.color = "#FF0000";
			msgErreurs += "\nLe Département n\'a pas été sélectionné!";
		}	
	}else{
		//document.getElementById("lblVille").style.color = "#FF0000";
		msgErreurs += "\nLa Province n\'a pas été sélectionnée!";
	}
	//Ville
	if(document.getElementById("isVilleExist").value == "T"){
		isVilleEmpty = false;
		//document.getElementById("lblVille").style.color = "#FFFFFF";
	}else if(document.getElementById("txtVille").value.length > 0){
		Initialize();
	
		var url = "./systems/ajax_ville_exist.php?P="+document.getElementById("cboPays").value+"&PC=";
		if(document.getElementById("cboPays").value == "CA"){ url += document.getElementById("cboProvince").value; }
		else if(document.getElementById("cboPays").value == "US"){ url += document.getElementById("cboEtat").value; }
		else if(document.getElementById("cboPays").value == "FR"){ url += document.getElementById("cboDepartement").value; }
		url += "&V="+document.getElementById("txtVille").value;
		if(req!=null){
			req.open('GET', url, false);
			req.send(null);
			if(req.status == 200){
				if(req.responseText != "F"){
					isVilleEmpty = false;
					document.getElementById("isVilleExist").value = "T";
					document.getElementById("txtVille").value = req.responseText;
					//document.getElementById("lblVille").style.color = "#FFFFFF";
				}
				else{
					//document.getElementById("lblVille").style.color = "#FF0000";
					msgErreurs += "\nVous devez choisir une VILLE (et l\'écrire correctement)";
				}
			}else{
				document.getElementById("ville").innerHTML = "There was a problem retrieving data:<br>" + req.statusText;
			}
		}	
	}else{
		//document.getElementById("lblVille").style.color = "#FF0000";
		msgErreurs += "\nLa Ville n\'a pas été entrée!";
	}
	//Chambre variable TOUT = 0
	/*if(document.getElementById("cboChambre").selectedIndex != 0){
		isChambreEmpty = false;
		document.getElementById("lblChambre").style.color = "#000";
	}else{
		document.getElementById("lblChambre").style.color = "#f00";
		msgErreurs += "\nLe Nombre de Chambre n\'a pas été sélectionné!";
	}*/
	//Distance
	for(var d = 1; d <= 5; d++){
		obj=document.getElementById("rdtDistance"+d);
		if(obj.checked){
			isDistanceEmpty = false;
		}
	}	
	if(isDistanceEmpty == true){
		//document.getElementById("lblDistance").style.color = "#FF0000";
		msgErreurs += "\nLa Distance n\'a pas été sélectionnée!";
	}

	//Check Si Toute es La Pour Submit
	//if(isPaysEmpty == false && isProvinceEmpty == false && isVilleEmpty == false && isDateEmpty == false && isFlexibiliteEmpty == false && isDureeSejourEmpty == false){
	if(isPaysEmpty == false && isProvinceEmpty == false && isVilleEmpty == false && isDistanceEmpty == false){		
		document.fRecherche.exec_recherche.value = 1;
		document.fRecherche.submit();
		/*document.fAccountInfo.imgLoading1.src = document.fAccountInfo.imgLoading1.src; //Reload AJAX GIF (IE BUG)
		document.fAccountInfo.imgLoading2.src = document.fAccountInfo.imgLoading2.src; //Reload AJAX GIF (IE BUG)*/
	}else{
		alert(msgErreurs);
	}
}

function btnAutoRecherche_onClick(Pays, Province, Ville, Distance){
	//Pays
	cboPays_onChange(Pays);
	//Province
	cboProvince_onChange(Province);
	//Ville
	txtVille_onChange(Ville);
	//Chambre
	//Fait rien default = all
	//Distance
	rdtDistance_onClick(Distance);
	
	//Auto Lancer la Recherche
	btnRechercheRapide_onClick();
}
