// JavaScript Document

function formChange() {
	$(document).ready(
		function(){
			$('div[id^="search_"]').hide()
			selectedValue=$('select[name="categorie"]').attr("value");
			//selectedValue=fromUTF8(selectedValue);
			switch(selectedValue) {
				case "Etalon":
					$('div[id="search_Stallion"]').show();
					break;
				case "Pouliniere":
					$('div[id="search_Pouliniere"]').show();
					break;
				case "A vendre":
					$('div[id="search_ToSell"]').show();
					break;
				case "Site":
					$('div[id="search_Websites"]').show();
					break;
				case "Petite annonce":
					$('div[id="search_Adds"]').show();
					break;
			}
		}
	);
}

function searchValidate(formData, jqForm, options){
	strSQL="005";
	if($('select[name="categorie"]').length>0) {
		selectedValue=$('select[name="categorie"]').attr("value");
	} else {
		selectedValue=$('input[name="categorie"]').attr("value");
	}
	//selectedValue=noAccent(selectedValue);
	switch(selectedValue) {
		case "Etalon":
			strSQL+=";horses;add_type;like;"+selectedValue;
			if ($('select[name="e_discipline"]').attr("value")) {strSQL+=";discipline;like;"+$('select[name="e_discipline"]').attr("value");}
			if ($('select[name="e_race"]').attr("value")) {strSQL+=";race;=;"+$('select[name="e_race"]').attr("value");}
			if ($('select[name="e_age"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="e_age"]').attr("value");}
			if ($('select[name="e_color"]').attr("value")) {strSQL+=";color;=;"+$('select[name="e_color"]').attr("value");}
			if ($('select[name="e_father"]').attr("value")) {strSQL+=";father;=;"+$('select[name="e_father"]').attr("value");}
			if ($('select[name="e_mother_s_father"]').attr("value")) {strSQL+=";mother_s_father;=;"+$('select[name="e_mother_s_father"]').attr("value");}
			break;
		case "Pouliniere":
			strSQL+=";horses;add_type;like;Poulini";
			if ($('select[name="p_discipline"]').attr("value")) {strSQL+=";discipline;like;"+$('select[name="p_discipline"]').attr("value");}
			if ($('select[name="p_age"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="p_age"]').attr("value");}
			if ($('select[name="p_color"]').attr("value")) {strSQL+=";color;=;"+$('select[name="p_color"]').attr("value");}
			if ($('select[name="e_discipline"]').attr("value")) {strSQL+=";discipline;like;"+$('select[name="e_discipline"]').attr("value");}
			if ($('select[name="e_race"]').attr("value")) {strSQL+=";race;=;"+$('select[name="e_race"]').attr("value");}
			if ($('select[name="e_age"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="e_age"]').attr("value");}
			if ($('select[name="e_color"]').attr("value")) {strSQL+=";color;=;"+$('select[name="e_color"]').attr("value");}
			if ($('select[name="e_father"]').attr("value")) {strSQL+=";father;=;"+$('select[name="e_father"]').attr("value");}
			if ($('select[name="e_mother_s_father"]').attr("value")) {strSQL+=";mother_s_father;=;"+$('select[name="e_mother_s_father"]').attr("value");}
			break;
		case "A vendre":
			strSQL+=";horses;name;like;"+""+";^add_type;like;"+selectedValue+";!add_type;like;Poulini;?to_sell;=;1;!add_type;like;Etalon;)to_sell;=;1";
			if ($('select[name="av_discipline"]').attr("value")) {strSQL+=";discipline;like;"+$('select[name="av_discipline"]').attr("value");}
			if ($('select[name="av_age"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="av_age"]').attr("value");}
			if ($('select[name="av_sexe"]').attr("value")) {strSQL+=";sex;=;"+$('select[name="av_sexe"]').attr("value");}
			if ($('select[name="e_discipline"]').attr("value")) {strSQL+=";discipline;like;"+$('select[name="e_discipline"]').attr("value");}
			if ($('select[name="e_race"]').attr("value")) {strSQL+=";race;=;"+$('select[name="e_race"]').attr("value");}
			if ($('select[name="e_age"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="e_age"]').attr("value");}
			if ($('select[name="e_father"]').attr("value")) {strSQL+=";father;=;"+$('select[name="e_father"]').attr("value");}
			if ($('select[name="e_mother_s_father"]').attr("value")) {strSQL+=";mother_s_father;=;"+$('select[name="e_mother_s_father"]').attr("value");}
			if ($('select[name="e_sexe"]').attr("value")) {strSQL+=";sex;=;"+$('select[name="e_sexe"]').attr("value");}
			
			break;
		case "Site":
			strSQL+=";horses;add_type;like;"+$('select[name="categorie"]').attr("value");
			if ($('select[name="w_rubrique"]').attr("value")) {strSQL+=";website_type;=;"+$('select[name="w_rubrique"]').attr("value");}
			 if($('select[name="w_pays"]').attr("value")) {strSQL+=";website_country;=;"+$('select[name="w_pays"]').attr("value");}
			 if($('select[name="w_langue"]').attr("value")) {strSQL+=";website_language;like;"+$('select[name="w_langue"]').attr("value");}
			break;
		case "Petite annonce":
			strSQL+=";horses;add_type;like;"+$('select[name="categorie"]').attr("value");
			if ($('select[name="a_rubrique"]').attr("value")) {strSQL+=";type;=;"+$('select[name="a_rubrique"]').attr("value");}
			if ($('select[name="a_periode"]').attr("value")) {strSQL+=";publication_date;daysBefore;"+$('select[name="a_periode"]').attr("value");}
			if ($('select[name="a_pays"]').attr("value")) {strSQL+=";add_country;=;"+$('select[name="a_pays"]').attr("value");}
			break;
		case "detaillee":
			strSQL+=";horses";
			if ($('input[name="name"]').attr("value")) {strSQL+=";name;like;"+$('input[name="name"]').attr("value");}
			if ($('select[name="sex"]').attr("value")) {strSQL+=";sex;like;"+$('select[name="sex"]').attr("value");}
			if ($('select[name="discipline"]').attr("value")) {strSQL+=";^discipline;=;"+$('select[name="discipline"]').attr("value");}
			if ($('select[name="discipline"]').attr("value")) {strSQL+=";,discipline_2;=;"+$('select[name="discipline"]').attr("value");}
			if ($('select[name="discipline"]').attr("value")) {strSQL+=";$discipline_3;=;"+$('select[name="discipline"]').attr("value");}
			if ($('select[name="race"]').attr("value")) {strSQL+=";race;=;"+$('select[name="race"]').attr("value");}
			if ($('select[name="birth_date"]').attr("value")) {strSQL+=";birth_date;between;"+$('select[name="birth_date"]').attr("value");}
			if ($('select[name="father"]').attr("value")) {strSQL+=";father;=;"+$('select[name="father"]').attr("value");}
			if ($('select[name="mother"]').attr("value")) {strSQL+=";mother;=;"+$('select[name="mother"]').attr("value");}
			if ($('select[name="mother_s_father"]').attr("value")) {strSQL+=";mother_s_father;=;"+$('select[name="mother_s_father"]').attr("value");}
			if ($('select[name="color"]').attr("value")) {strSQL+=";color;=;"+$('select[name="color"]').attr("value");}
			if ($('select[name="height"]').attr("value")) {strSQL+=";height;between;"+$('select[name="height"]').attr("value");}
			if ($('select[name="sell_price_from"]').attr("value")) {strSQL+=";sell_price_from;between;"+$('select[name="sell_price_from"]').attr("value");}
			if ($('input[name="movie"]').attr("checked")) {strSQL+=";movie;notNull;1";}
			if ($('input[name="avendre"]').attr("checked")) {strSQL+=";^add_type;like;A vendre;!add_type;like;Poulini;?to_sell;=;1;!add_type;like;Etalon;)to_sell;=;1";}
			if ($('select[name="publication_date"]').attr("value")) {strSQL+=";publication_date;daysBefore;"+$('select[name="publication_date"]').attr("value");}
			break;
	}
	strSQL=utf8.encode(strSQL);
	$.ajax({
		type: "POST",
		url: "/phpInc/ajax_search_preview.php",
		dataType: "html",
		data: "_="+strSQL,
		async: false,
		success: function(returnValue) {
			valueNb=parseInt(returnValue);
		}});
	
	if(valueNb==0) {
		alert(simpleText("noAnswer"));
		return false;
	} else {
		//switch(fromUTF8(selectedValue)) {
		lang=getSessionValue("session_language");
		switch(selectedValue) {
			case "Etalon":
				if(lang=="fr") {window.location="/vente-chevaux/cherche-chevaux/etalon";}
				else if (lang=="en") {window.location="/horses-for-sale/horses-wanted/the-stallion";}
				else if (lang=="de") {window.location="/pferd-kaufen/pferde-hengst/deckhengst";}
				return false;
				break;
			case "Pouliniere":
				if(lang=="fr") {window.location="/vente-chevaux/cherche-chevaux/chevaux-poulain";}
				else if (lang=="en") {window.location="/horses-for-sale/horses-wanted/horse-foals";}
				else if (lang=="de") {window.location="/pferd-kaufen/fohlen-aufzucht/fohlen-verkaufen";}
				return false;
				break;
			case "A vendre":
				if(lang=="fr") {window.location="/vente-chevaux/cherche-chevaux/annonce-de-chevaux";}
				else if (lang=="en") {window.location="/horses-for-sale/horses-wanted/sports-horse-jumping";}
				else if (lang=="de") {window.location="/pferd-kaufen/pferde-kauf/pferderasse";}
				return false;
				break;
			case "Site":
				if(lang=="fr") {window.location="/cheval/annonces-cheval/photo-de-cheval.html";}
				else if (lang=="en") {window.location="/horses/horses-for-sale/horses-pictures.html";}
				else if (lang=="de") {window.location="/pferde/pferde-anzeigen/pferdemarkt.html";}
				return false;
				break;
			case "Petite annonce":
				if(lang=="fr") {window.location="/cheval/annonces-cheval/annonces-chevaux.html";}
				else if (lang=="en") {window.location="/horses/horses-for-sale/equestrian-advertising.html";}
				else if (lang=="de") {window.location="/pferde/pferde-anzeigen/pferde-kleinanzeigen.html";}
				return false;
				break;
			case "detaillee":
				if(lang=="fr") {window.location="/vente-chevaux/cherche-chevaux/acheter-un-cheval";}
				else if (lang=="en") {window.location="/horses-for-sale/horses-wanted/sports-horses";}
				else if (lang=="de") {window.location="/pferd-kaufen/pferd-sport/pferderasse";}
				return false;
				break;
		}
	}
}
