


function formDateHandler(prefix){
	var giorni_mese = [31,29,31,30,31,30,31,31,30,31,30,31];

	var giorno = document.getElementById(prefix+"day").selectedIndex;
	var mese = document.getElementById(prefix+"month").selectedIndex;
	var anno = document.getElementById(prefix+"year").value;
	if(mese==2 && anno!='' && anno%4==0 && giorno>29){
		//	Febbraio Bisesitle
		document.getElementById(prefix+"day").selectedIndex = 0;
	}else if(mese==2 && anno!='' && anno%4!=0 && giorno>28){
		//	Febbraio non Bisesitle
		document.getElementById(prefix+"day").selectedIndex = 0;
	}else if(giorno>giorni_mese[mese-1]){
		document.getElementById(prefix+"day").selectedIndex = 0;
	}
}





/************************************************************************************/





function formUpdateLocation(number, fieldname){
	var selectedIndex = document.getElementById(fieldname+"_select_"+number).selectedIndex;

	if(number<3){
		document.getElementById(fieldname+'_'+number).style.backgroundImage = "url('images/loading.gif')";
		document.getElementById(fieldname+'_'+number).style.backgroundRepeat = 'no-repeat';
		document.getElementById(fieldname+'_'+number).style.backgroundPosition = 'right center';
		document.getElementById(fieldname+'_'+number).style.paddingRight = '20px';
	}

	//	Svuoto tutte le tendine sucessive e le nascondo
	for(var i=(number+1); i<=3; i++){
		document.getElementById(fieldname+"_select_"+i).innerHTML = '';
		document.getElementById(fieldname+"_"+i).style.display = 'none';
	}

	//	Ora tramite ajax devo estrarre gli elementi per la tendina sucessiva
	if(number<3 && selectedIndex!=0){
		var parent_id = document.getElementById(fieldname+"_select_"+number).value;
		//	Controllo con Ajax che sia valido
		var url = absolute_uri+"ajax/ad_insert_get_location.php?parent_id="+parent_id;

		//hack for IE caching
		url = url + "&time=" + new Date().getTime();
		var request = ajaxCreateRequest();
		request.open("GET",url,true);
		request.onreadystatechange = function(){
			if (request.readyState == 4){
				var result = request.responseText;

				if(number<3){
					//	Devo far scomparire il loading
					document.getElementById(fieldname+'_'+number).style.backgroundImage = "none";
					document.getElementById(fieldname+'_'+number).style.paddingRight = '0px';
				}

				if(result!=''){

					//	Aggiungo la prima option
					var option = document.createElement('option');
					option.setAttribute('value', '');
					option.innerHTML = '::: '+label_select_location+' :::';
					document.getElementById(fieldname+"_select_"+(number+1)).appendChild(option);
					//	Parso l'XML
					if (window.ActiveXObject){
						//	code for Internet Explorer
						var doc=new ActiveXObject("Microsoft.XMLDOM");
						doc.async="false";
						doc.loadXML(result);
					}else{
						// code for Mozilla, Firefox, Opera, etc.
						var parser=new DOMParser();
						var doc=parser.parseFromString(result,"text/xml");
					}
					//	Scorro i figli
					var items=doc.getElementsByTagName('child');
					for (i=0;i<items.length;i++){
						var id = items[i].getAttribute('id');
						var label = items[i].childNodes[0].nodeValue;
						var option = document.createElement('option');
						option.setAttribute('value', id);
						option.innerHTML = label;
						document.getElementById(fieldname+"_select_"+(number+1)).appendChild(option);
					}
					//	Rendo visibile lo span
					document.getElementById(fieldname+"_"+(number+1)).style.display = 'inline';
				}
			}
		}
		request.send(null);
	}else{
		if(number<3){
			//	Devo far scomparire il loading
			document.getElementById(fieldname+'_'+number).style.backgroundImage = "none";
			document.getElementById(fieldname+'_'+number).style.paddingRight = '0px';
		}
	}
}





/************************************************************************************/





function formAdCheckForm(){
	var procede = true;
	var focus = false;
	//	Tipo
	if(document.adForm.typology){
		if(document.adForm.typology[0].checked==false && document.adForm.typology[1].checked==false){
			procede = false;
			document.getElementById("typology_box").className = 'red_text';

			if(focus==false){
				document.adForm.typology[0].focus();
				focus = true;
			}

		}else{
			document.getElementById("typology_box").className = 'black_text';
		}
	}

	//	Language
	if(document.adForm.language_id.selectedIndex==0){
		procede = false;
		document.adForm.language_id.style.backgroundColor = errorBackgroundColor;

		if(focus==false){
			document.adForm.language_id.focus();
			focus = true;
		}

	}else{
		document.adForm.language_id.style.backgroundColor = standardBackgroundColor;
	}

	//	Title
	if(clearStringWhiteSpace(document.adForm.title.value).length==0){
		procede = false;
		document.adForm.title.style.backgroundColor = errorBackgroundColor;

		if(focus==false){
			document.adForm.title.focus();
			focus = true;
		}

	}else{
		document.adForm.title.style.backgroundColor = standardBackgroundColor;
	}

	//	Description
	if(clearStringWhiteSpace(document.adForm.description.value).length==0){
		procede = false;
		document.adForm.description.style.backgroundColor = errorBackgroundColor;

		if(focus==false){
			document.adForm.description.focus();
			focus = true;
		}

	}else{
		document.adForm.description.style.backgroundColor = standardBackgroundColor;
	}


	//	Location
	if(document.adForm.typology && document.adForm.typology[0].checked==true){
		//	Se è stata selezionata offerta come tipologia obbligo a compilare tutti i campi della location
		for(var i=0; i<=3; i++){
			if(document.getElementById("location_"+i) && document.getElementById("location_select_"+i).length>1){
				if(document.getElementById("location_select_"+i).selectedIndex==0){
					//	Non è stato selezionato l'elmento della location
					document.getElementById("location_select_"+i).style.backgroundColor = errorBackgroundColor;
					procede = false;

					if(focus==false){
						document.getElementById("location_select_"+i).focus();
						focus = true;
					}

				}else{
					document.getElementById("location_select_"+i).style.backgroundColor = standardBackgroundColor;
				}
			}
		}
	}


	//	Rent_Sala
	if(document.adForm.rent_sale){
		if(document.adForm.rent_sale[0].checked==false && document.adForm.rent_sale[1].checked==false){
			procede = false;
			document.getElementById("rent_sale").className = 'red_text';

			if(focus==false){
				document.adForm.rent_sale[0].focus();
				focus = true;
			}

		}else{
			document.getElementById("rent_sale").className = 'black_text';
		}
	}


	if(procede==false){
		window.scroll(0, window.scrollY-30) ;
		return false;
	}else{
		return true;
	}
}





/******************************************************************************/





function formQuickAdCheckForm(){
	var procede = true;
	var focus = false;
	//	Tipo
	if(document.adForm.typology){
		if(document.adForm.typology[0].checked==false && document.adForm.typology[1].checked==false){
			procede = false;
			document.getElementById("campo_1").className = 'field_wrong';
			if(focus==false){
				document.adForm.typology[0].focus();
				focus = true;
			}

		}else{
			document.getElementById("campo_1").className = 'field';
		}
	}


	//	marketplace_id
	document.getElementById("campo_2").className = 'field';
	var tendine = document.getElementById("campo_2").getElementsByTagName("select");
	for(var i=0; i<tendine.length; i++){
		if(tendine[i].length>1 && tendine[i].selectedIndex==0){
			//	Ho trovato una tendina non selezionata
			procede = false;
			document.getElementById("campo_2").className = 'field_wrong';
			if(focus==false){
				tendine[i].focus();
				focus = true;
			}
		}
	}



	//	Location
	document.getElementById("campo_3").className = 'field';
	if(document.adForm.typology && document.adForm.typology[0].checked==true){
		//	Se è stata selezionata offerta come tipologia obbligo a compilare tutti i campi della location
		for(var i=0; i<=3; i++){
			if(document.getElementById("location_"+i) && document.getElementById("location_select_"+i).length>0){
				if(document.getElementById("location_select_"+i).selectedIndex==0){
					//	Non è stato selezionato l'elmento della location
					document.getElementById("campo_3").className = 'field_wrong';
					//document.getElementById("location_select_"+i).style.backgroundColor = errorBackgroundColor;
					procede = false;

					if(focus==false){
						document.getElementById("location_select_"+i).focus();
						focus = true;
					}

				}else{
					//document.getElementById("location_select_"+i).style.backgroundColor = standardBackgroundColor;
				}
			}
		}
	}


	//	Language
	if(document.adForm.language_id.selectedIndex==0){
		procede = false;
		document.getElementById("campo_4").className = 'field_wrong';

		if(focus==false){
			document.adForm.language_id.focus();
			focus = true;
		}

	}else{
		document.getElementById("campo_4").className = 'field';
	}

	//	Title
	if(clearStringWhiteSpace(document.adForm.title.value).length==0){
		procede = false;
		document.getElementById("campo_5").className = 'field_wrong';

		if(focus==false){
			document.adForm.title.focus();
			focus = true;
		}
	}else{
		document.getElementById("campo_5").className = 'field';
	}

	//	Description
	if(clearStringWhiteSpace(document.adForm.description.value).length==0){
		procede = false;
		document.getElementById("campo_6").className = 'field_wrong';

		if(focus==false){
			document.adForm.description.focus();
			focus = true;
		}

	}else{
		document.getElementById("campo_6").className = 'field';
	}

	//	Email
	if(clearStringWhiteSpace(document.adForm.email.value).length==0){
		procede = false;
		document.getElementById("campo_7").className = 'field_wrong';

		if(focus==false){
			document.adForm.email.focus();
			focus = true;
		}
	}else{
		document.getElementById("campo_7").className = 'field';
	}

	/*
	//	Nickname
	if(clearStringWhiteSpace(document.adForm.nickname.value).length==0){
		procede = false;
		document.getElementById("campo_8").className = 'field_wrong';

		if(focus==false){
			document.adForm.nickname.focus();
			focus = true;
		}
	}else{
		document.getElementById("campo_8").className = 'field';
	}
	*/


	if(procede==false){
		return false;
	}else{
		return true;
	}
}





/************************************************************************************/





function getModelFromBrand(){
	//	Verifico sia stato selezionato un brand
	if(document.getElementById('brand_id').selectedIndex==0){
		//	Nessun brand selezionato
		//	Svuoto la tendina dei modelli
		for(var i=document.getElementById('model_id').length-1; i>=1; i--){
			document.getElementById('model_id').removeChild(document.getElementById('model_id').childNodes[i]);
		}
		//	Non ci sono figli, nascondo la tendina
		document.getElementById('model_id').style.visibility = 'hidden';
	}

	var url = absolute_uri+"ajax/get_model_from_brand.php?brand_id="+document.getElementById('brand_id').value;
		//hack for IE caching
		url = url + "&time=" + new Date().getTime();
		var request = ajaxCreateRequest();
		request.open("GET",url,true);
		request.onreadystatechange = function(){
			if (request.readyState == 4){
				//	Svuoto la tendina dei modelli
				for(var i=document.getElementById('model_id').length-1; i>=1; i--){
					document.getElementById('model_id').removeChild(document.getElementById('model_id').childNodes[i]);
				}


				var result = request.responseText;
				//	Parso l'XML
				if (window.ActiveXObject){
					//	code for Internet Explorer
					var doc=new ActiveXObject("Microsoft.XMLDOM");
					doc.async="false";
					doc.loadXML(result);
				}else{
					// code for Mozilla, Firefox, Opera, etc.
					var parser=new DOMParser();
					var doc=parser.parseFromString(result,"text/xml");
				}
				//	Scorro i figli
				var items=doc.getElementsByTagName('child');
				if(items.length==0){
					//	Non ci sono figli, nascondo la tendina
					document.getElementById('model_id').style.visibility = 'hidden';
				}else{
					//	Appendo i figli
					for (i=0;i<items.length;i++){
						var id = items[i].getAttribute('id');
						var label = items[i].childNodes[0].nodeValue;
						var option = document.createElement('option');
						option.setAttribute('value', id);
						option.innerHTML = label;
						document.getElementById('model_id').appendChild(option);
					}
					document.getElementById('model_id').style.visibility = 'visible';
				}
			}
		}
		request.send(null);
}





/************************************************************************************/





function formIntegerHandler(input){
	var value = input.value;
	value = value.replace(/[\D]/,"");
	input.value = value;
}





/************************************************************************************/





function adChangeCurrencyDecimateSeparator(currency_id){
	for(var i=0; i<currency_decimate_separators.length; i++){
		if(currency_decimate_separators[i][0]==currency_id){
			document.getElementById('currency_decimate_separator').innerHTML = currency_decimate_separators[i][1];
			i = currency_decimate_separators.length;
		}
	}
}