var errorBackgroundColor = '#F9D5D5';
var standardBackgroundColor = '#FFFFFF';
var okBackgroundColor = '#DFFFE8';





/************************************************************************************/



var friends_email_num = 0;

function friends_add_email(){
	//	Incremento il numero di indirizzi email inseriti
	friends_email_num++;

	//	Inizializzo l'oggetto lista
	var ul = document.getElementById('friends_email_ul');

	//	Creo il nuovo elemento da appendere

	var li = document.createElement("li");
	li.id = 'friends_email_li_'+friends_email_num;

	var input = document.createElement("input");
	input.type = 'text';
	input.size = 35;
	input.name = 'recipients_list[]';

	var a = document.createElement("a");
	a.href = 'javascript:friends_delete_email('+friends_email_num+');';

	var a_text = document.createTextNode('x');

	li.appendChild(input);
	a.appendChild(a_text);
	li.appendChild(a);
	ul.appendChild(li);

	//	Aggiorno l'altezza del retino
	var pageHeight = getPageHeight();
	document.getElementById('page_shadow').style.height = pageHeight+'px';
}





//--------------------------------------------------------------------------//





function friends_delete_email(li_num){
	var ul = document.getElementById('friends_email_ul');
	//	Se c'è un solo elemento lo svuoto e basta
	if(ul.getElementsByTagName('li').length==1){
		document.getElementById('friends_email_li_'+li_num).getElementsByTagName('input')[0].value = '';
	}else{
		ul.removeChild(document.getElementById('friends_email_li_'+li_num));
	}
}





/************************************************************************************/





function validateEmail(email){
	//	Ripulisco l'email da eventuali spazi
	email = clearStringWhiteSpace(email).toLowerCase();
	//	Ne verifico la correttezza
//	var check = new RegExp("^[a-z0-9][_\.a-z0-9-]+@([a-z0-9][0-9a-z-]+\.)+([a-z]{2,4})$");
	var check = new RegExp("^[\&a-zA-Z0-9_-]+([\.]?[\&a-zA-Z0-9_-])+[a-zA-Z0-9]?@([a-zA-Z0-9][0-9a-zA-Z-]*[\.]{1})+([a-zA-Z]{2,6})$");

	if(email.length==0){
		return false;
	}else if(email.match(check)){
		return true;
	}else{
		return false;
	}
}





/************************************************************************************/





function clearStringWhiteSpace(stringa){
	str = stringa.replace(/^\s*/,"").replace(/\s*$/,"").replace(/\s+/g,"");
	return str;
}





/************************************************************************************/





function ajaxCreateRequest(){
	try{
		var request = new XMLHttpRequest();
	}catch (trymicrosoft){
		try{
			var request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (othermicrosoft){
			try{
				var request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (failed){
				var request = null;
			}
		}
	}
	if (request == null){
		alert("Error creating request object!");
	}else{
		return request;
	}
}





/************************************************************************************/





function searchKeyword(marketplace_url, deep, homepage){
	if(marketplace_url!=''){
		document.getElementById("searchForm").action = marketplace_url;
	}else if(deep==0){
		//	Ritorno in homepage
		document.getElementById("searchForm").action = homepage;
		document.getElementById("searchForm").submit();
	}else{
		//	Torno al marketplace padre
		marketplace_url = document.getElementById("search_marketplace_"+(deep-1)).value;
		document.getElementById("searchForm").action = marketplace_url;
	}

	document.getElementById("searchForm").submit();
}





//---------------------------------------------------------------------//





function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}





//---------------------------------------------------------------------//





function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}





//---------------------------------------------------------------------//





function getPageHeight(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientWidth;
  }

  return myHeight;

/*
		var yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = document.body.offsetHeight;
		}

		var windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}


		return pageHeight;
*/
}





//---------------------------------------------------------------------//





function getPageWidth(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }

  return myWidth;

	/*
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );



		var xScroll;

		if (window.innerWidth && window.scrollMaxX) {
			xScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
		}

		var windowWidth;
		if (self.innerWidth) {	// all except Explorer
			windowWidth = self.innerWidth;
		} else if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
		}

		// for small pages with total height less then height of the viewport
		if(xScroll < windowWidth){
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}


		return pageWidth;
	*/
}





//---------------------------------------------------------------------//




function getPageScrollTop(){
	return document.body.parentNode.scrollTop;
}




//------------------------------------------------------------------------//





function ad_form_photo_handler(){
	//	Verifico se esiste il box delle immagini
	if(document.getElementById('ad_form_image_box')){
		//	Se è selezionata una richiesta non faccio inserire le foto
		var richiesta = document.adForm.typology[0].checked;
		if(richiesta==true){
			//	Sto inserendo un'offerta
			//	Mostro il box delle immagini
			document.getElementById('ad_form_image_box').style.display = 'block';
		}else{
			//	Sto inserendo una richiesta
			//	Nascondo il box delle immagini
			document.getElementById('ad_form_image_box').style.display = 'none';
			//	Svuoto i campi
			var inputs = document.getElementById('ad_form_image_box').getElementsByTagName('input');
			for(var i=0; i<inputs.length; i++){
				inputs[i].value = '';
			}
		}
	}
}





//-----------------------------------------------------------------//


function homepage_box_handler(){
	if(getPageWidth()>800){
		if( typeof( window.innerWidth ) == 'number' ) {
			document.getElementById('homepage_boxes').style.display = 'table-cell';
		}else{
			document.getElementById('homepage_boxes').style.display = 'block';
		}
	}else{
		document.getElementById('homepage_boxes').style.display = 'none';
	}
}





//-------------------------------------------------------------//



var campo_attivo = null;

function colora_campo(id_campo){
	if(campo_attivo!=null && campo_attivo!=id_campo){
		decolora_campo();
	}
	document.getElementById(id_campo).className = 'form_field_active';
	campo_attivo = id_campo;
}

function decolora_campo(){
	document.getElementById(campo_attivo).className = 'form_field_normal';
}




//--------------------------------------------------------------//





function tag_cloud_button_handler(enabled){
	if(enabled==false){
		document.getElementById("tag_cloud_button").className = 'disabled';
		document.getElementById("tag_cloud_button").getElementsByTagName('a')[0].href = '#';
	}
}





//----------------------------------------------------------------------------//





function add_language_name_to_translation_box(options){
	if(options.selectedIndex!=0){
		//	devo aggiornare il nome della lingua
		var language_name = options[options.selectedIndex].innerHTML;
		document.getElementById("ad_translation_title_language_name").innerHTML = ' ('+language_name+')';
		document.getElementById("ad_translation_title_language_name").style.visibility = 'visible';
		document.getElementById("ad_translation_description_language_name").innerHTML = ' ('+language_name+')';
		document.getElementById("ad_translation_description_language_name").style.visibility = 'visible';
		if(document.getElementById("ad_translation_tag_language_name")){
			document.getElementById("ad_translation_tag_language_name").innerHTML = ' ('+language_name+')';
			document.getElementById("ad_translation_tag_language_name").style.visibility = 'visible';
		}
	}else{
		//	devo nascondere le etichette con i nomi delle lingue
		document.getElementById("ad_translation_title_language_name").innerHTML = '';
		document.getElementById("ad_translation_title_language_name").style.visibility = 'visible';
		document.getElementById("ad_translation_description_language_name").innerHTML = '';
		document.getElementById("ad_translation_description_language_name").style.visibility = 'visible';
		if(document.getElementById("ad_translation_tag_language_name")){
			document.getElementById("ad_translation_tag_language_name").innerHTML = '';
			document.getElementById("ad_translation_tag_language_name").style.visibility = 'visible';
		}
	}
}





//----------------------------------------------------------------------------//





function user_profile_show_additional_box(){
	if(document.getElementById("user_profile_friend_box")){
		if(getPageWidth()>=800){
			document.getElementById('user_profile_friend_box').style.display = 'block';
			document.getElementById('user_profile_friend_box').style.paddingLeft = '10px';
		}else{
			document.getElementById('user_profile_friend_box').style.display = 'none';
			document.getElementById('user_profile_friend_box').style.paddingLeft = '0px';
		}
	}
}





//----------------------------------------------------------------------------//





function newsletter_signup_box_show(){
	document.getElementById("newsletter_signup_box").style.display = 'block';
	document.newsletter_signup_form.email.focus();
}


function newsletter_signup_box_hide(){
	document.getElementById("newsletter_signup_box").style.display = 'none';
}

function newsletter_signup_box_submit(){
	// Controllo che sia stata inserita l'email
	if(document.newsletter_signup_form.email.value.length==0){
		//	Email non inserita
		document.newsletter_signup_form.email.style.backgroundColor = errorBackgroundColor;
		return false;
	}else{
		//	Email inserita
		return true;
	}
}

function newsletter_signup_box_focus(value){
	document.newsletter_signup_form.email.style.backgroundColor=standardBackgroundColor;
	if(document.newsletter_signup_form.email.value==value){
		document.newsletter_signup_form.email.value = '';
	}
}





//----------------------------------------------------------------------------//





function ad_vote(ad_id, ad_vote_type){
	//	Voto in AJAX
	var url = absolute_uri+"ajax/ad_vote.php?ad_id="+ad_id+"&ad_vote_type="+ad_vote_type;

	//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;
			//	Faccio apparire il messaggio del voto registrato
			document.getElementById("ad_main_box_cleaning_system").style.display = 'none';
			document.getElementById("sidebox_red").style.display = 'none';
			document.getElementById("ad_vote_complete_message").style.display = 'block';
		}
	}
	request.send(null);
}





//----------------------------------------------------------------------------//





function load_url_criptato(url_criptato){
	var url_decriptato = url_criptato.replace(/#-#/g, '/');
	document.location = url_decriptato;
}
