var homepageStep = 1;
var detailStep = 1;
$(document).ready(function() {
	$("#content_ren03").hide();
});

function homepageStep1(urlfor)
{
	if (homepageStep != 1)  return false;
	var optionSelectedVal = '';
	var optionElementId = 'aligator';
	//optionSelectedVal = $("#step01 .selectBox .activeSelect").attr('id').replace('spanMacchina_' ,'');
  optionSelectedVal = $('#selectTipiMacchina').val().replace('spanMacchina_' ,'');
	if(optionSelectedVal != undefined){
		$.ajax({
			 type: "GET",
			 url: urlfor,
			 data: "step=1&id=" + optionSelectedVal,
			 success: function(data){
				 if (data != 'false')
				 {
					$('#stepContainer2').html(data);
					$("#stepContainer").attr('class', 'step02 aj_area');
					// set for step1
					//$("#step01 .selectBox .activeSelect").attr('class', 'passiveSelect');
          $('#selectTipiMacchina').attr('disabled', true).css('background', '#999999')
					$("#step01").removeAttr('class');
					$("#step01").attr('class', 'column01');
					// set for step2
					$("#step02").removeAttr('class');
					$("#step02").attr('class', 'column02 active');
					homepageStep = 2;

					$("#step01").attr('disabled','disabled');
					$(".styleSelect_item").html('');
				 }
				 else
				 {
					 alert('Error: Can\'t load data from Sever!');
				 }
			 }
		 });
	}else{
		return false;
	}
}

function homepageStep2(urlfor)
{
	if (homepageStep != 2)  return false;
	var optionSelectedVal = '';
	var optionElementId = 'aligator';
	var radioSel = $('#stepContainer2 input:checked');
	if(radioSel.length > 0){
		var arrayVal = radioSel.attr('value').split('|');
		var plant_range_id = arrayVal[0];
		var plant_id = arrayVal[1];
		$.ajax({
			 type: "GET",
			 url: urlfor,
			 data: 'plant_id='+plant_id+'&plant_range_id='+plant_range_id+'&step=2',
			 success: function(data){
				 if (data != 'false')
				 {
					$('#stepContainer3').html(data);
					$("#stepContainer").attr('class', 'step03 aj_area');
					// set for step2
					$("#step02").removeAttr('class');
					$("#step02").attr('class', 'column02');
					// set for step3
					$("#step03").removeAttr('class');
					$("#step03").attr('class', 'column03 active');
					homepageStep = 3;

					$("#step02").attr('disabled','disabled');
					$("#step02 .form p input").attr('readonly', 'readonly');
				 }
				 else
				 {
					 alert('Error: Can\'t load data from Sever!');
				 }
			 }
		 });
	}else{
		return false;
	}
}

function homepageStep3()
{
	return false;
	if (homepageStep != 3)  return false;
	alert('Done');
}

$("#content_ren03").hide();
function detailStep_ShowDetail(id_activate)
{
	$(".contShow").css('display', 'none');
	$("#" + id_activate).css('display', 'block');
}

function detailStep1(testoErrore)
{
	if (detailStep != 1)  return false;
  //$("#content_ren03").hide();
  if ($("#ren01 input[type=radio]:checked").length > 0){
    var optionSelectedVal = '';
    var optionElementId = 'aligator';
    //optionSelectedVal = $("#step01 .selectBox .activeSelect").text();
    $("#ren01").attr('class', 'item');
    $("#ren02").attr('class', 'item active');
    $("#ren01 input[type=radio]").attr('readonly', 'readonly')
    $("#ren02 input[type=checkbox]").removeAttr('readonly');
    detailStep = 2;
  } else {
    alert($('#testoErrore').html())
  }

}

function detailStep2(urlfor, urlForNextFascia)
{
	if (detailStep != 2)  return false;
	var optionSelectedVal = '';
	var optionElementId = 'aligator';
	
	$("#content_ren03").hide();
  $("#ren02").attr('class', 'item');
  $("#ren02 input[type=checkbox]").attr('readonly', 'readonly')
  $("#ren03").attr('class', 'item active');

  var maxPrice = parseFloat($('#maxPrice').val());
  var fasciaSel = $('input[name=plantRange]:checked');
  var accessoriSel = $('input[name="plantOption[]"]:checked');

  var rispostaFasciaSel = $.ajax({
    type: "POST",
    url:  urlForNextFascia.replace('_next_', '_price_'),
    async : false,
    data: "fascia=" + fasciaSel.attr('id').replace('plantRange_', '')
  });

  var prezzoFasciaSel = parseFloat(rispostaFasciaSel['responseText']);
  //var prezzoFasciaSel = parseFloat(fasciaSel.val());
  var totAccessori = 0;
  var idAccessoriSel = '';
  for (i=0; i<accessoriSel.length; i++){
    idAccessoriSel += $(accessoriSel[i]).attr('id').replace('plantOption', '') + '|';
  }

  var rispostaNextFascia = $.ajax({
    type: "POST",
    url:  urlForNextFascia,
    async : false,
    data: "fascia=" + fasciaSel.attr('id').replace('plantRange_', '')
  });

  var nextFascia = rispostaNextFascia['responseText'];

  var idNextFascia = 0;
  var titoloNextFascia = '';
  var prezzoNextFascia = 0;
  if(nextFascia.length > 0){
    var arrNextFascia = nextFascia.split('|')
    idNextFascia = parseInt(arrNextFascia[0]);
    titoloNextFascia = arrNextFascia[1];
    prezzoNextFascia = parseFloat(arrNextFascia[2]);
  }

  var risposta = $.ajax({
    type: "POST",
    url:  urlfor,
    async : false,
    data: "fascia=" + fasciaSel.attr('id').replace('plantRange_', '') + '&accessori=' + idAccessoriSel
  });

  var totAccessori = parseFloat(risposta['responseText']);
  
  var totAccessoriNextFascia = totAccessori;

  if (idNextFascia > 0){
    var rispostaAccessoriNexFascia = $.ajax({
      type: "POST",
      url:  urlfor,
      async : false,
      data: "fascia=" + idNextFascia + '&accessori=' + idAccessoriSel
    });
    totAccessoriNextFascia = parseFloat(rispostaAccessoriNexFascia['responseText'])
  }

  //*****TOTALI*******//
  var totaleAlGiorno = 0;
  var totaleAlGiornoNextFascia = 0;

  totaleAlGiorno = prezzoFasciaSel + totAccessori;
  totaleAlGiorno = totaleAlGiorno.toFixed(2);

  if (idNextFascia > 0){
    totaleAlGiornoNextFascia = prezzoNextFascia + totAccessoriNextFascia;
    totaleAlGiornoNextFascia = totaleAlGiornoNextFascia.toFixed(2);
  }

  $('#totalePreventivo').html(totaleAlGiorno.toString().replace('.', ',') + ' &euro;');

  if (idNextFascia > 0){
    $('#messNextFascia').html($('#sorgenteMessNextFascia').html().replace('#FASCIA#', '<span style="font-size:20px">' + titoloNextFascia + '</span>').replace('#PREZZO#', '<span style="font-size:20px">' + totaleAlGiornoNextFascia.toString().replace('.', ',') + ' &euro;</span>'));
    $('#messNextFascia').show();
    $('#messPeriodiSuperiori').hide();
  } else {
    $('#messNextFascia').hide();
    $('#messPeriodiSuperiori').show();
  }

  var risparmio = 0;
  if (maxPrice > 0){
    risparmio = maxPrice - prezzoFasciaSel;
  }
  if (risparmio > 0){
    risparmio = risparmio.toFixed(2);
    $('#risparmio').val(risparmio.toString().replace('.', ','));
    $('#divRisparmio').show();
  }


  $("#content_ren03").show();

  // set for step2
  detailStep = 3;

}

function detailStep3()
{
	if (detailStep != 3)  return false;
	alert('Work in progress...\\nVerrà spedita una mail con tutte le informazioni');
}

function backStep1(){
  detailStep = 1;
  $("#ren01 input[type=radio]").removeAttr('readonly');
  $("#ren02 input[type=checkbox]").attr('readonly', 'readonly');
  $("#ren01").attr('class', 'item active');
  $("#ren02").attr('class', 'item');
}

function backStep2(){
  detailStep = 2;

	$("#content_ren03").hide();
  $("#ren01 input[type=radio]").removeAttr('readonly');
  $("#ren02 input[type=checkbox]").removeAttr('readonly');
  
  $("#ren02").attr('class', 'item active');
  $("#ren03").attr('class', 'item');
  $('#divRisparmio').hide()
  $('#erroriForm').hide();
  $('input[name^="richiestaPreventivoForm"]').attr('style', '')
  


  $('#totalePreventivo').html('');
}
