// Javascript functions used in the main pages

// to Search Film and Festivals

function GoSearchFest() {
	theForm = this.document.FestSearch;

	//var detail = MiseEnMaj(theForm.DetailNom.value);
	var detail = ConvertCaseNonSensible(theForm.DetailNom.value) ;

	theForm.SQL.value = "select * from fest where fest_nom like '%" + detail +"%' OR fest_ville like '%" + detail +"%' order by fest_nom" ;
	theForm.NOSQL.value = "SELECT fest.fest_num,fest.fest_nom,fest_ville,fest.fest_loc_ville FROM fest WHERE fest.fest_private=1" ;
	SubmitForm(theForm) ;
}

function TestForm2() {
	theForm = this.document.FF;


	var nPaysID =  theForm.fest_pays.options[theForm.fest_pays.selectedIndex].value;
	var nDateID =  theForm.choixdate.options[theForm.choixdate.selectedIndex].value;

	if ( nPaysID == "X" ) {
		if ( nDateID == "0" ) {
			theForm.SQL.value = "select fest_num,fest_nom,fest_ville from fest order by fest_nom" ;
		}
		else {
			theForm.SQL.value = "select fest_num,fest_nom,fest_ville from fest where fest_ddm=" + nDateID +" order by fest_nom" ;
		}
	}
	else {
		if ( nDateID == "0" ) {
			theForm.SQL.value = "select fest_num,fest_nom,fest_ville from fest where fest_pays=" + nPaysID +" order by fest_nom" ;
		}
		else {
			theForm.SQL.value = "select fest_num,fest_nom,fest_ville from fest where fest_ddm=" + nDateID +" AND fest_pays=" + nPaysID +" order by fest_nom" ;
		}
	}
	SubmitForm(theForm) ;
}

function GoSearchFilm() {
	var theForm = this.document.FilmSearch;

	//var nPaysID =  theForm.fest_pays.options[theForm.fest_pays.selectedIndex].value;

	var vfilm =  theForm.FilmName.value;
	//var vproduction =  theForm.ProductionName.value;
	//var vdirector =  theForm.DirectorName.value;

	//vproduction = vproduction.toUpperCase() ;
	//vfilm = vfilm.toUpperCase() ;

	//vdirector=ConvertCaseNonSensible(vdirector) ;
	//vproduction=ConvertCaseNonSensible(vproduction) ;
	vfilm=ConvertCaseNonSensible(vfilm) ;

	//alert(vdirector) ;
	
	//var detail = MiseEnMaj(theForm.DetailNom.value);
   if (theForm.FilmName.value != "" ) {
		theForm.SQL.value = "select film.film_num,film.film_titleus,film.film_director,customer.cus_comp from film,customer where film.film_titleus like '%" + vfilm +"%' and customer.cus_num=film.film_num_cust order by film.film_titleus"; 
		}
	
	//alert('test : ' + theForm.SQL.value);
	SubmitForm(theForm) ; 
	
}

function SubmitForm(form)
{
	form.action='/servlet/JSCRun' ;
	//alert("submit !") ;
	form.submit() ;
}
function ConvertCaseNonSensible(toConvert) 
{

	//var nSizeRadio = myForm.CaseSensible.length;
	var resultat = toConvert.value ;	
	//myForm.key.value = myForm.keyPage.value;
	
	{//begin
		var strConvertOrig = String(toConvert);
		var strConvert = strConvertOrig;
		var strConverted = String(""), strTmp=String("");
		var strLastOne = String("");
		var strKeyToExclureForOR = String("%");
		var strSub = String ("");
		var endspace = 0 ;
		strConvert = strConvert .toUpperCase();
		
		
		var i, nSize = strConvert.length, nPosB = 0;
		var nbsep=0 ;
		var nbexp=0 ;
		
		for (i = 0; i < nSize;i++)
		{
			strTmp = strConvert.charAt(i);
			strTmpLower = strTmp.toLowerCase();

			//alert("strTmp : "+strTmp) ; 
			if (strLastOne == ' ' && strTmp == ' ')
			{
				endspace = 1 ; //nothing, we skip the empty
			}
			else
			{
				if (strTmp == ' ')
				{
				
					if(i==nSize-1)
						endspace = 1 ;
						
					//alert("i="+i+"nbexp="+nbexp+" strConverted : "+strConverted+"("+eval(nPosB+nbexp)+","+eval((Number(i-nbexp)*4)+nbexp)+")") ;
				strSub = strConverted.substring(eval(nPosB+nbexp),eval((Number(i-nbexp)*4)+nbexp));
					//alert("strSub : "+strSub) ;
					
					//alert(nPosB+"-"+eval((Number(i)*4))) ;
					
					strKeyToExclureForOR += strSub + '%';
					//alert("strKeyToExclureForOR : "+strKeyToExclureForOR) ;			
					
					
					strConverted = strConverted + strConvert.charAt(i);
					
					nPosB = eval((Number(i-nbexp)*4)) ;
					nbexp++ ;
				}
				else
				{
				strConverted = strConverted +'['+strConvert.charAt(i)+strTmpLower+']';					
				}
			}
			strLastOne = strTmp;
			
		}//for
		
		//if(endspace==1)
			//alert("finit par un ou des espaces") ;
		//alert(nPosB) ;
		//alert("strConverted + taille substring : "+strConverted+"("+eval(nPosB-1)+"-"+eval((Number(i-1)*4))+")") ;
		//strSub = strConverted.substring(eval(nPosB-4), eval((Number(i-1)*4)));
		
		if(endspace == 0)
		{
		strSub = strConverted.substring(eval(nPosB+nbexp-1),eval((Number(i-nbexp)*4)+nbexp));
		
		//alert("strSub : "+strSub) ;
		strKeyToExclureForOR += strSub + '%';
		}
		//alert("strKeyToExclureForOR : "+strKeyToExclureForOR) ;
		
		}//end
	//alert("strSub"+strSub) ;
	resultat = strKeyToExclureForOR ;
	
	return resultat ;
	
}
