/**
* Fonctions ajax et DOM pour la gestion de liste de rpg finis
* @author Medion
*/

//Recup du nom du navigateur avec version (0,1)
//var "globales"
var array_all_select = new Array(); //va contenir les valeurs des select en retour de httprequest

var id; //id html
var support_selected; 
var version_selected; 
var genre_selected; 
var dev_selected; 
var chara_selected;
var compo_selected;
var date_selected;
var aInitArray= new Array();

/*********************** AU LOAD **********************************************/
/**
* Construction de l'array d'init
*/
function buildInitArray() {
	aInitArray[0]=$A($('support_recherche').options).clone() ;
	aInitArray[1]=$A($('version_recherche').options).clone() ;
	aInitArray[2]=$A($('genre_recherche').options).clone() ;
	aInitArray[3]=$A($('dev_recherche').options).clone() ;
	aInitArray[4]=$A($('chara_recherche').options).clone() ;
	aInitArray[5]=$A($('compo_recherche').options).clone() ;
	aInitArray[6]=$A($('date_recherche').options).clone() ;
	aInitArray[7]=$A($('editeur_recherche').options).clone() ;
	aInitArray[8]=$A($('serie_recherche').options).clone() ;
}

window.onload = buildInitArray; //on recup de base les valeurs par défaut

/************* GESTION DU DOM (Avec maniement du DOM via prototype) ************************************/

/******** SCRIPT AJAX POUR LA GESTION DES LISTES DE RPG FINIS ****************/

/**
* Construit la liste du select selon le nav
*@param selectid
*/
function construct_select(selectid ){

	var table_index; //index pour recup l'une des arrays du select
	var table_prefixe; //prefixe pour recup les données
	
	switch(selectid){
		case "support_recherche":
			table_index = 0;
			table_prefixe = "supports";
			break;
		case "version_recherche":
			table_index = 1;
			table_prefixe ="versions";
			break;
		case "genre_recherche":
			table_index = 2;
			table_prefixe ="genres";
			break;
		case "dev_recherche":
			table_index = 3;
			table_prefixe ="developpeurs";
			break;
		case "chara_recherche":
			table_index = 4;
			table_prefixe ="chara";
			break;
		case "compo_recherche":
			table_index = 5;
			table_prefixe ="compositeurs";
			break;
		case "date_recherche":
			table_index = 6;
			table_prefixe ="annee";
			break;
		case "editeur_recherche":
			table_index = 7;
			table_prefixe ="editeurs";
			break;
		case "serie_recherche":
			table_index = 8;
			table_prefixe ="series";
			break;
	}
	
	for (i=$(selectid).options.length-1 ; i>=0 ; i-- ) {
		$(selectid).options[i]=null ;
	}
	
	for(i=0;i<array_all_select[table_index].length;i++){	
		$(selectid).options[i] = new Option(array_all_select[table_index][i][table_prefixe+"_nom"], array_all_select[table_index][i][table_prefixe+"_id"]);
	}
}

/**
* Construit toutes les listes select croisées
*@param liste_id,string reponse du serveur
*/
function construct_all_select(liste_id,stringo){
		
	var formu = "advsearch";
	array_all_select = stringo.evalJSON(true);

	if(liste_id!="support_recherche"){construct_select('support_recherche' );Select_Value_Set(formu,'support_recherche', support_selected);}
	if(liste_id!="version_recherche"){construct_select('version_recherche' );Select_Value_Set(formu,'version_recherche', version_selected);}
	if(liste_id!="genre_recherche"){construct_select('genre_recherche' );Select_Value_Set(formu,'genre_recherche', genre_selected);}
	if(liste_id!="dev_recherche"){construct_select('dev_recherche' );Select_Value_Set(formu,'dev_recherche', dev_selected);}
	if(liste_id!="chara_recherche"){construct_select('chara_recherche' );Select_Value_Set(formu,'chara_recherche', chara_selected);}
	if(liste_id!="compo_recherche"){construct_select('compo_recherche' );Select_Value_Set(formu,'compo_recherche', compo_selected);}
	if(liste_id!="date_recherche"){construct_select('date_recherche' );Select_Value_Set(formu,'date_recherche', date_selected);}
	if(liste_id!="editeur_recherche"){construct_select('editeur_recherche' );Select_Value_Set(formu,'editeur_recherche', editeur_selected);}
	if(liste_id!="serie_recherche"){construct_select('serie_recherche' );Select_Value_Set(formu,'serie_recherche', serie_selected);}
}
			
/**
* Méthode qui sera appelée sur le select
*@param liste_id
*/
function updateListes(liste_id){
	
	var data = null;
	
	afficheCentre('loading-compendium-advsearch');
	
	//On prépares le data à send
	switch(liste_id){
		case "support_recherche":
			data="su";
			break;
		case "version_recherche":
			data="ve";
			break;
		case "genre_recherche":
			data="ge";
			break;
		case "dev_recherche":
			data="dv";
			break;
		case "chara_recherche":
			data="ch";
			break;
		case "compo_recherche":
			data="co";
			break;
		case "date_recherche":
			data="dt";
			break;
		case "editeur_recherche":
			data="ed";
			break;
		case "serie_recherche":
			data="se";
			break;
		default:
			data="";
			break;
	}

	support_selected = $F("support_recherche"); //le support choisi
	version_selected = $F("version_recherche"); //le support choisi
	genre_selected = $F("genre_recherche"); //le support choisi
	dev_selected = $F("dev_recherche"); //le support choisi
	chara_selected = $F("chara_recherche"); //le support choisi
	compo_selected = $F("compo_recherche"); //le support choisi
	date_selected = $F("date_recherche"); //le support choisi
	editeur_selected = $F("editeur_recherche"); //le support choisi
	serie_selected = $F("serie_recherche"); //le support choisi

	var params=$H({'su':support_selected,'ve':version_selected,'ge':genre_selected,'dv':dev_selected,'ch':chara_selected,'co':compo_selected,'dt':date_selected,'ed':editeur_selected,'se':serie_selected,'ref':data});
	
	var filename="../scripts_async/setSelectComp.php";
	
	var ajax = new Ajax.Request(filename,
	{method: 'post', parameters: params,
		onSuccess: function (requete){ 
			construct_all_select(liste_id,requete.responseText);
			$('loading-compendium-advsearch').hide();
		} 
	});
}

/**
* Initialise la fenêtre d'ajout
*/
function init(){
	var formu = "advsearch";
	var j=0;
	$('loading-compendium-advsearch').show();
	
	$w('support_recherche version_recherche genre_recherche dev_recherche chara_recherche compo_recherche date_recherche editeur_recherche serie_recherche').each(function(s) {
		emptyList(s);
		for(i=0;i<aInitArray[j].length;i++){
			$(s).options[i] = new Option(aInitArray[j][i].text, aInitArray[j][i].value);
		}
		j++;
		Select_Value_Set(formu,s, -99);
	});

	$('loading-compendium-advsearch').hide();
}

/**
*
*/
function checkSearch(){
	var mess = "";

	if(!$('dti').checked){
		$('dtf').value = 0;
		$('dtt').value = 0;
	
		document.forms['advsearch'].submit();
	}
	else{
		var ok= 1;
		var dexp = /[0-9]{2}\/[0-9]{2}\/[0-9]{4}/;
		
		if(!dexp.test($F('dtf')) || !dexp.test($F('dtt'))){
			ok = 0;
			if(!dexp.test($F('dtf'))) mess+= "Date de début invalide\n";
			if(!dexp.test($F('dtt'))) mess+= "Date de fin invalide\n";
		}
		else{
			var dayf = $F('dtf').substring(0,2);
			var monthf = $F('dtf').substring(3,5);
			var yearf = $F('dtf').substring(6,10);
			var dayt = $F('dtt').substring(0,2);
			var montht = $F('dtt').substring(3,5);
			var yeart = $F('dtt').substring(6,10);		
		
			var dateF = new Date(yearf,monthf-1,dayf);
			var dateT = new Date(yeart,montht-1,dayt);
			
			if($F('dtf')!=(maskDeux(dateF.getDate())+"/"+maskDeux((dateF.getMonth()+1))+"/"+dateF.getFullYear())){
				ok = 0;
				mess+= "Date de début invalide\n";
			}
			if($F('dtt')!=(maskDeux(dateT.getDate())+"/"+maskDeux((dateT.getMonth()+1))+"/"+dateT.getFullYear())){
				ok = 0;
				mess+= "Date de fin invalide\n";
			}
			
			//Checke des dates
			
			if(((yearf>yeart) || ((yearf==yeart)&&(monthf>montht)) || ((yearf==yeart)&&(monthf==montht)&&(dayf>dayt))) && ok==1){
				ok = 0;
				mess+= "Intervalle incorrect, la date de fin de recherche doit être plus grande que celle de début voyons!\n";
			}
		}
		
		if(ok==1){
			$('dtf').value = yearf+maskDeux((dateF.getMonth()+1))+maskDeux(dateF.getDate());
			$('dtt').value = yeart+maskDeux((dateT.getMonth()+1))+maskDeux(dateT.getDate());
			document.forms['advsearch'].submit();
		}
		else{
			alert(mess);
		}	
	}

}

function maskDeux(str){
	if(str.toString().length==1){
		return "0"+str.toString();
	}
	else{
		return str.toString();
	}
}

window.onload = function() {
	Calendar.setup({dateField: 'dtf', triggerElement : 'dtf_ch'});
	Calendar.setup({dateField: 'dtt', triggerElement : 'dtt_ch'});
};