var errorMsg = new Array("0", "email non valida",
 "Username gi&agrave; utilizzata!Provane un'altra!",
 "la lunghezza dello username &egrave; sbagliata",
 "email esistente!",
 "username esistente",
 "errore generico",
 "errore 7",
 "Dati indirizzo non corretti!Rieffettua la registrazione!",
 "errore email",
 "errore email");
var errLabel = "";
var currentPage = 1;
var error = false;
var htcp4500;

var greetMSG = '<IMG SRC="http://ad.it.doubleclick.net/activity;src=1724828;type=pampe188;cat=finei159;ord=1?" WIDTH=1 HEIGHT=1 BORDER=0><h3>Grazie per la registrazione!</h3><p>Riceverai a breve una email per confermare la tua iscrizione. Cliccka sul link.</p><img height="1" width="1" border="0" src="http://www.googleadservices.com/pagead/conversion/1035934995/?label=9on1CNG3kgEQk7r87QM&guid=ON&script=0"/>';
 
 //bootstrap
 $(document).ready(function(){
 
 	//if ($('#birthdate').val() != null){
	 	$('#birthdate').datepicker({
	 		yearRange: '-90:0',
	 		dateFormat: 'yy-mm-dd'
	 	});
 	//}

 	/*
 	$('a#butRegistrati').click(function(){
 	
 		getAjaxRequest('/includes/box_reg.php','','div#boxRegAjax');
 		//setTimeout(function(){
 		$('div#boxRegistrazione').slideDown('medium');
 		//}, 2000);
 	  	
 	});
 	*/
	
 	//aggiunge il gestore dell'evento onclick sul pulsante "vai a registrarti"
 	$('#go_reg').click(function(event){
 		$('#l_form').fadeOut("fast", function(){
 		$('#reg_form').fadeIn("fast");
 		});
 	});
 	
 	
   //controlla che lo username non esista nel db
   $('input[name=username]').blur(function(event){
   //$('input[name=username]').blur(function(event){
   		target = $('p[id=uName_ctrl]');
   		obj = $('input[name=username]');
		validateInput(obj, 'validateUsername', target);
   	});
 	
 	
 	// controlla che le password siano uguali
	$('input[name=pwdConfirm]').blur(function(event){
   		if ($('input[name=password]').attr('value') != $(this).attr('value')) {
	  			$('p[id=pwd_ctrl]').html("le due password non coincidono").show();
	  				setHideErrorLbl($('input[name=password]'),$('p[id=pwd_ctrl]'));
	  				setHideErrorLbl($('input[name=pwdConfirm]'),$('p[id=pwd_ctrl]'));
	  	}
   });
   
   
   //controlla che l'email non esista nel db
   $('input[name=email]').blur(function(event){
   		obj = $('input[name=email]');
   		target = $('p[id=email_ctrl]');
		//alert(target.length)
   		validateInput(obj, 'validateEmail',target );
   });
   
   //controlla campo firstName != ""
   $('input[name=firstName]').blur(function(event){
   		if ($('input[name=firstName]').attr('value') == "" ) {
	  		$('span[id=firstName_ctrl]').html("campo obbligatorio").show();
	  		setHideErrorLbl($('input[name=firstName]'),$('p[id=firstName_ctrl]'));
	  	}
   });
   
   //controlla campo lastName != ""
   $('input[name=lastName]').blur(function(event){
   		if ($('input[name=lastName]').attr('value') == "" ) {
	  		$('p[id=lastName_ctrl]').html("campo obbligatorio").show();
	  		setHideErrorLbl($('input[name=lastName]'),$('p[id=lastName_ctrl]'));
	  	}
   });
   
   
   	//aggiunge il gestore dell'evento change di  nChildSelector
 	$('#nChildSelector').change(function(event){
 	    
 	    currValue = $(this).val();
 	    nChild = $('input#numChild').val();
 	    ch = true;
 	    if (nChild > 0)
        {
            ch = confirm('Attenzione! Cambiando questo valore, perderai i dati sui tuoi figli!');
        }
 		if (ch) {
 		     
 		     $('div#childField').hide("slide", { direction: "up" },500, function(){
 		     deleteAllChildFields();
 		     generateChildFields(currValue);
 		     $('div#childField').show("slide", { direction: "up" },500)
 		     });
 		}
 		
 	});
	
	//aggiunge il gestore dell'evento change di  nChildSelectorLux
 	$('#nChildSelectorLux').change(function(event){
 	    
 	    currValue = $(this).val();
 	    nChild = $('input#numChild').val();
 	    ch = true;
 	    if (nChild > 0)
        {
            ch = confirm('Attenzione! Cambiando questo valore, perderai i dati sui tuoi figli!');
        }
 		if (ch) {
 		     
 		     $('div#childField').hide("slide", { direction: "up" },500, function(){
 		     deleteAllChildFields();
 		     generateChildFieldsLux(currValue);
 		     $('div#childField').show("slide", { direction: "up" },500)
 		     });
 		}
		// rilancio lo scroll per aumentare/diminuire l'altezza
		window.setTimeout("$('.boxModProfiCNT').jScrollPane({scrollbarWidth:8,scrollbarMargin:10,animateStep:1});",1000);
 		
 	});
 
	

 
 });//end bootstrap
 
 
 	function goToStep(nextPage){
 	   
        var nextDiv = '#step-' + nextPage; 
 		
 		switch(currentPage) {
	 	
	 	case 1: {
	 			//alert(currentPage);
	 			//alert(nextPage);
	 			currDiv = '#step-' + currentPage;
	 			if (validateFirstStep($('p#errLabelS1'))){
	 				$(currDiv).fadeOut(100, function(){
	 				for(var i=1;i<=3;i++){
			          (i==parseInt(nextPage))?UB_changeClass('discStep'+i,'sel'):UB_changeClass('discStep'+i,'');
			        }
	 				$(nextDiv).fadeIn(100);
	 				$
	 				});
	 				currentPage = nextPage;
	 				error = false;
		 		}
		 	} 
	 		break;
	 		
	 		case 2:{
	 			//alert(currentPage);
	 			//alert(nextPage);
	 			currDiv = '#step-' + currentPage;
	 			if (validateFirstStep($('p#errLabelS2'))){
		 			if (validateSndStep($('p#errLabelS2'))){
		 			    for(var i=1;i<=3;i++){
                          (i==parseInt(nextPage))?UB_changeClass('discStep'+i,'sel'):UB_changeClass('discStep'+i,'');
                        }
		 					$(currDiv).fadeOut(100, function(){
		 					$(nextDiv).fadeIn(100);
		 				});
		 				currentPage = nextPage;
		 				error = false;
		 	 		}
	 	 		}	
	 		}
	 		break;
	 		
	 		case 3: {
                        
				currDiv = '#step-' + currentPage;
				
				$(currDiv).fadeOut(100, function(){
				for(var i=1;i<=3;i++){
                   (i==parseInt(nextPage))?UB_changeClass('discStep'+i,'sel'):UB_changeClass('discStep'+i,'');
                }
				$(nextDiv).fadeIn(100);
				});
				currentPage = nextPage;
				error = false;
	 		}
	 		break;
	 		
	 		default:
	 		{
	 			//alert('mi son perso qualcosa' + currentPage);
	 		}
	 		break;
	 	}
	 	
 	
 	}
 	
 //funzioni
 function validateMobileLacivita(valore){
	 /** 
	 regole:
	 	prima cifra
			1/9 => max 13, min 9 cifre
			+ => 13 cifre
	 */
	 if(valore.substr(0,1)=="+" && (valore.length==13 || valore.length==12)){
		 return true;
	 }
	 if(valore.substr(0,1)!="+" && valore.substr(0,1)!="0" && (valore.length==10 || valore.length==9)){
		 return true;
	 } 
	 if(valore.substr(0,2)=="00"&& (valore.length==14 || valore.length==13)){
		 return true;
	 } 
	 
	 return false;
 }
 
 function validatePhone(elId){

	var regexpPhone = new RegExp('^\(\\d\{9,15\}\)$');
	
	var inputPhone = $('input#'+ elId).val();
	
	if (inputPhone == ""){
		return true;
	} else {
		
		var match = regexpPhone.exec(inputPhone);
	
		if (match == null) {
		
			return false;
		   
		} else {
			return true;
		}
	
	}
	
}
 
	 function validateInput(obj, type, target) {
	 		
	 		var data;
			inputData = obj.attr('value');
			jQuery.get( '/doValidation.php' //'sendRequest2.php' //
			, 
			{ action: type, value: inputData }, 
				function (data){
				if (data != "0"){
				target.html(errorMsg[data]);
				target.show();
				setHideErrorLbl(obj, target);
					error = true;
				} else {
					error = false;
				}
			},
			"text" ) 
			
	 }
	 
	 function validateInput2(obj, type) {
			var valid;
			inputData = obj.attr('value');
			jQuery.ajax({
				async: false,
				type: "GET",
				url: '/doValidation.php',
				dataType : "text",
				data : { action: type, value: inputData },
				success: function(jData) {
	 	 			
					if (jData != 0){
							 htcp4500 = false;
							return false
						} else {
							 htcp4500 = true;
							return true;
					}
				}
			});
			return htcp4500;
	 }
	 
	 
	 function setHideErrorLbl(obj, lbl){
	 	obj.click(function(event){
	 	    lbl.hide();
	 	});
	 }
	 
	 function validateFirstStep(label){
	 	
	 	$(label).html('');
	 	error = false;
	 	
	 	if(checkNullField($('input#username'))){
	 			setErrMsg("il campo username &egrave; vuoto<br/>",label);
	 			error =  true;
	 	}
	 	if (checkNullField($('input#password'))){
	 		    setErrMsg("il campo password &egrave; vuoto<br/>",label);
	 			error =  true;
	 		
 		}
 		if (checkNullField($('input#pwdConfirm'))){
 		    setErrMsg("il campo conferma password &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		if (checkNullField($('input#firstName'))){
 			setErrMsg("il campo nome &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		if (checkNullField($('input#lastName'))){
 			setErrMsg("il campo cognome &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		
 		if (error){
 			displayErrorLabel(label);
 			return false;
 		}
 		else
 		{
 			hideErrorLabel(label);
 			return true;		
 		}
 	 } 
	 
	 function validateFirstStepMOD(label){
	 	
	 	$(label).html('');
	 	error = false;
	 	
	 	if(checkNullField($('input#nick'))){
	 			setErrMsg("il campo username &egrave; vuoto<br/>",label);
	 			error =  true;
	 	}
 		if (checkNullField($('input#nome'))){
 			setErrMsg("il campo nome &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		if (checkNullField($('input#cognome'))){
 			setErrMsg("il campo cognome &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		
 		if (error){
 			displayErrorLabel(label);
 			return false;
 		}
 		else
 		{
 			hideErrorLabel(label);
 			return true;		
 		}
 	 } 
	 	
	 	
    function validateSndStep(label){
    	
       	$(label).html('');
 		error = false;
 		
 		if(checkNullField($('input#address'))){
 			setErrMsg("il campo indirizzo &egrave; vuoto<br/>",label);
 			error =  true;
 		}
		
		if(checkNullField($('input#addNum'))){
 			setErrMsg("il campo n.civico &egrave; vuoto, se non hai un numero civico inserisci SNC<br/>",label);
 			error =  true;
 		}
 		
 		var myRegxp = /^([a-zA-Z\'\ ]+)$/;
 		if(checkNullField($('input#loc'))){
 			setErrMsg("il campo localit&agrave; &egrave; vuoto<br/>",label);
 			error =  true;
 		} else if (myRegxp.test($('input#loc').val())== false) {
		
			setErrMsg("il campo localit&agrave; non &egrave; valido<br/>",label);
			error =  true;
		
		}
 		
		if(checkNullField($('input#cap'))){
 			setErrMsg("il campo cap &egrave; vuoto<br/>",label);
 			error =  true;
 		}
		if(!checkNullField($('input#cap')) && isNaN($('input#cap').val())){
 			setErrMsg("il campo cap non &egrave; valido<br/>",label);
 			error =  true;
 		}
		if(!checkNullField($('input#cap')) && $('input#cap').val().length!=5){
 			setErrMsg("il campo cap deve essere di 5 cifre<br/>",label);
 			error =  true;
 		}
		
 		if(checkNullField($('select#prov'))){
 			setErrMsg("il campo provincia &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		
 		if(checkNullField($('input#email'))){
 			setErrMsg("il campo email &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		
 		if(checkNullField($('input#email')) || (checkNullField($('input#email_confirm')))){
 			setErrMsg("il campo email o conferma mail non possono essere vuoti<br/>",label);
 			error =  true;
 		} else {
 		
 			email = $('input#email').val();
 			cEmail = $('input#email_confirm').val();
 			if( jQuery.trim(email) != jQuery.trim(cEmail) ){
 				setErrMsg("le email non sono uguali<br/>",label);
 				error =  true;
 		    } else {
 		    
 		    	email = $('input#email');
 		    	valid = validateInput2(email, 'validateEmail');
 		    	
 		    	if (!valid){
 		    	
 		    		setErrMsg("indirizzo email gi&agrave; utilizzato<br/>",label);
 					error =  true;
					 		    	
 		    	} else {
 		    	
 		    	}
 		    
 		    }
 		}
 		if($('#mobile').val()!="" && !validateMobileLacivita($('#mobile').val())) {
 			setErrMsg("il numero di cellulare non &egrave; valido <br/>",label);
 			error =  true;
 		}
 		
 		if($('#mobile').val()!="" && !validatePhone('phone')) {
 			setErrMsg("il numero di telefono non &egrave; valido <br/>",label);
 			error =  true;
 		}
 		
 		if(error){
 			displayErrorLabel(label);
 			return false;
 		}
 		else{
 			hideErrorLabel(label);
 			return true;		
 	  }
	}
	 	
    function validateSndStepMOD(label){
    	
       	$(label).html('');
 		error = false;
 		
 		if(checkNullField($('input#indirizzo'))){
 			setErrMsg("il campo indirizzo &egrave; vuoto<br/>",label);
 			error =  true;
 		}
		
		if(checkNullField($('input#civico'))){
 			setErrMsg("il campo n.civico &egrave; vuoto, se non hai un numero civico inserisci SNC<br/>",label);
 			error =  true;
 		}
 		
 		var myRegxp = /^([a-zA-Z\'\ ]+)$/;
 		if(checkNullField($('input#localita'))){
 			setErrMsg("il campo localit&agrave; &egrave; vuoto<br/>",label);
 			error =  true;
 		} else if (myRegxp.test($('input#localita').val())== false) {
		
			setErrMsg("il campo localit&agrave; non &egrave; valido<br/>",label);
			error =  true;
		
		}
 		
		if(checkNullField($('input#cap'))){
 			setErrMsg("il campo cap &egrave; vuoto<br/>",label);
 			error =  true;
 		}
		if(!checkNullField($('input#cap')) && isNaN($('input#cap').val())){
 			setErrMsg("il campo cap non &egrave; valido<br/>",label);
 			error =  true;
 		}
		if(!checkNullField($('input#cap')) && $('input#cap').val().length!=5){
 			setErrMsg("il campo cap deve essere di 5 cifre<br/>",label);
 			error =  true;
 		}
		
 		if(checkNullField($('select#provincia'))){
 			setErrMsg("il campo provincia &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		
 		if(checkNullField($('input#email'))){
 			setErrMsg("il campo email &egrave; vuoto<br/>",label);
 			error =  true;
 		}
 		 		
 		if(!validatePhone('telefono')) {
 			setErrMsg("il numero di telefono non &egrave; valido <br/>",label);
 			error =  true;
 		}
		
 		/*if(!validatePhone('mobile')) {
 			setErrMsg("il numero di cellulare non &egrave; valido <br/>",label);
 			error =  true;
 		}*/
 		if($('#cellulare').val()!="" && !validateMobileLacivita($('#cellulare').val())) {
 			setErrMsg("il numero di cellulare non &egrave; valido <br/>",label);
 			error =  true;
 		}
 		
		if(isNaN($("input[name=waitingstatus]:checked").val())){
 			setErrMsg("devi indicare il tuo stato <br/>",label);
 			error =  true;
 		}
 		if(error){
 			displayErrorLabel(label);
 			return false;
 		}
 		else{
 			hideErrorLabel(label);
 			return true;		
 	  }
	}
	 		
 	function validateThirdStep(label){
 	
 		$(label).html('');
 		error = false;
		totalChildField = $('input#numChild').val();
		
		
		/*if($('#waitingstatus1:checked').val()==null && $('#waitingstatus2:checked').val()==null && $('#waitingstatus3:checked').val()==null && $('#waitingstatus4:checked').val()==null){
 			setErrMsg("devi indicare il tuo stato <br/>",label);
 			error =  true;
 		}*/
		if(isNaN($("input[name=waitingstatus]:checked").val())){
 			setErrMsg("devi indicare il tuo stato <br/>",label);
 			error =  true;
 		}
		
		if($("input[name=waitingstatus]:checked").val()>1 && !checkChildEntry(0)){
 			setErrMsg("inserisci i dati di almeno un figlio<br/>",label);
 			error =  true;
 		}
		
 		
 		for (i=0; i < totalChildField; i++){
 			if (!checkChildEntry(i)){
 				j = i + 1;
 				setErrMsg("errore nei dati del figlio " + j + " ! ricontrolla, per favore.<br/>",label);
 				error = true;
 			}
 		}
 		
 		if(error){
 			displayErrorLabel(label);
 			return false;
 		} else
 		{
 			hideErrorLabel(label);
 			return true;		
 		}
 	}
 	
 	function checkChildEntry(i){
 		
 		var errors = 0;
 		
 		if (checkNullField($('#CfirstName_' + i))){errors++;}
 		if (checkNullField($('#ClastName_' + i))){errors++;}
 		if (checkNullField($('#Cbd_' + i))){errors++;}
 		if (checkNullField($('#Csex_' + i))){errors++;}
 		
 		if (errors > 0)
 		{
 		 	return false;	
 			
 		}else {
 			return true;
 		}
 		
 	
 	
 	}
	 	
   function checkNullField(obj)
   {
   		inputData = obj.attr('value');
   		inputData = jQuery.trim(inputData);
   		if (inputData == "")
   		{
   			return true;
   		
   		} else
   		{
   			return false;
   		}
   } 
   
 
    function setErrMsg($msg, label)
	{
	 	$(label).append($msg);
	}
	 
	function displayErrorLabel(label){
	   $(label).show();
	}
	 
	 function hideErrorLabel(label){
	 	$(label).hide();
	 }
	 
	 function validateAndSubmit(){
		//$('#submitSpan').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="/img/submit_loader.gif" border="0"/>');
		$('#submitLink').unbind('click');
		if (validateThirdStep('p#errLabelS3')){
		
		    if (validateSndStep('p#errLabelS3'))
		    {
		       if (validateFirstStep('p#errLabelS3')){
		       if ($('#privacyChk:checked').val() == null)
               {
                   alert("Per registrarti, devi accettare l'informativa sulla privacy");
                   return false;
               }
               if ($('#tecoChk:checked').val() == null)
               {
                   alert("Per registrarti, devi aver letto i termini e le condizioni d'uso");
                   return false;
               }
			   $('#submitSpan').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="/img/submit_loader.gif" border="0"/>');
               //postAjaxRequest(formName, page, params, 'p#errLabelS3')
	 		   	submitAjaxForm('p#errLabelS3');
	 		   }
	 		}
	 	}
	 }
	 
	 function submitAjaxForm(label){
	
		//alert('here I am');
		var username = $('input[name=username]').val();
		var password = $('input[name=password]').val();
		var pwdConfirm = $('input[name=pwdConfirm]').val();
		var firstName = $('input[name=firstName]').val();
		var lastName = $('input[name=lastName]').val();
		var sex = $('select[name=sex]').val();
		var nationality = $('select[name=nationality]').val();
		var birthdate = $('input[name=birthdate]').val();
		var address = $('input[name=address]').val();
		var addNum = $('input[name=addNum]').val();
		var loc = $('input[name=loc]').val();
		var cap = $('input[name=cap]').val();
		var prov = $('select[name=prov]').val();
		var email = $('input[name=email]').val();
		var email_confirm = $('input[name=email_confirm]').val();
		var phone = $('input[name=phone]').val();
		var mobile = $('input[name=mobile]').val();
		var ip = $('input[name=ip]').val();
		var newsletter = $('input[name=newsletter]:checked').val();
		var personalChk = $('input[name=personalChk]:checked').val();
		var privacyChk = $('input[name=privacyChk]:checked').val();
		var dataChk = $('input[name=dataChk]:checked').val();
		var waitingstatus = $('input[name=waitingstatus]:checked').val();
		
		
		var childString ="";
	   	
	   	$('#childField input').each(function()
	   	{
	   		childString += "&" + $(this).attr('name') + "=" + $(this).attr('value');
	   		 
	   	});
	   	
	   	$('#childField select').each(function() {
	   	
	   		childString += "&" + $(this).attr('name') + "=" + $(this).val();
	   	
	   	});
	   	
	    //alert(childString); return false;
	    
	    
	    
	     var errString = "";
		 var err = false;
		 
	      $(label).hide();
			if (firstName == "") {
			errString += "Il campo nome non puņ essere vuoto<br/>";	
			err = true;
	     	}
	  		
	  		if (err){
	  			$(label).html(errString).show();
	  		}	 
	      
	 	var dataString = 'username=' + username + '&password=' + password + '&pwdConfirm=' + pwdConfirm + '&firstName=' + firstName + '&lastName=' + lastName + '&sex=' + sex + '&nationality=' + nationality + '&birthdate=' + birthdate  + '&address=' + address + '&addNum=' + addNum + '&loc=' + loc + '&cap=' + cap + '&prov=' + prov + '&email=' + email + '&email_confirm=' + email_confirm + '&phone=' + phone + '&name='+ name + '&email=' + email + '&phone=' + phone + '&mobile=' + mobile + '&ip=' + ip;
	 	dataString += '&newsletter=' + newsletter +  '&personalChk=' + personalChk + '&privacyChk=' + privacyChk + '&dataChk=' + dataChk + '&waitingstatus=' + waitingstatus;
		dataString += childString;
	
		//alert (dataString); return false; //fin qui funziona.
		  $.ajax({
		    type: "POST",
		    //url: "submitForm2.php",
		    url: "doRegister.php",
		    data: dataString,
		    success: function(data) {		    
		    if (data == '0') {
		    	$(label).hide();
		     	$('#boxRegistrazione').html('<div id="message" class="boxStep stpB"><!-- --></div><div class="boxRegistrazioneFTR"><a href="javascript:void(0);" title="chiudi" class="closeReg" onclick="$(\'div.boxRegistrazione\').slideUp(\'medium\');" onfocus="this.blur();">chiudi</a></div>');
		      	$('#message').html(greetMSG).hide().fadeIn(1500);
		      	return true;
		      } else {
		      	$(label).hide();
		     	$('#boxRegistrazione').html('<div id="message" class="boxStep stpB"><!-- --></div><div class="boxRegistrazioneFTR"><a href="javascript:void(0);" title="chiudi" class="closeReg" onclick="$(\'div.boxRegistrazione\').slideUp(\'medium\');" onfocus="this.blur();">chiudi</a></div>');
		     	msg = "";
		     	if (errorMsg[data] != null)
		     	{
		     	    msg = errorMsg[data]
		     	}
		     	 else
		     	 {
		     	    msg = data;
		     	 }
		     	 $('#message').html('<p class="errorMsg">' + msg + '</p>').hide().fadeIn(1500);
		      	return false;
		      	//$('#errLabel').html(errorMsg[data]);
		      	
		      }
		    }
		  });
	  return false;
	}
	
	function generateChildFields(numField){
	
		for (i = 0; i < numField; i++)
		{
			insChildFields(i);
		}
	
	
	}
	
	function generateChildFieldsLux(numField){
	
		for (i = 0; i < numField; i++)
		{
			insChildFieldsLux(i);
		}
	
	
	}
	
	function deleteAllChildFields(){
		$('div#childField').html('');
		$('input#numChild').attr('value', '0');
	}
	
	function insChildFields(i) {
	       j = i+1;
	
        HTMLtext = '                                <div id="childF_'+ i +'">\r\n';
        HTMLtext += '                                   <table border="0" cellpadding="0" cellspacing="0" class="childData">\r\n';
        HTMLtext += '                                       <thead><tr><th colspan="2">Dati '+ j +'&deg; figlio</th></tr></thead>\r\n';
        HTMLtext += '                                       <tbody>\r\n';
        HTMLtext += '                                           <tr><td><label for="CfirstName_'+ i +'">Nome</label></td><td class="field"><input type="text" name="CfirstName_'+ i +'" id="CfirstName_'+ i +'" value="" class="formFieldX" /></td></tr>\r\n';
        HTMLtext += '                                           <tr><td><label for="ClastName_'+ i +'">Cognome</label></td><td class="field"><input type="text" name="ClastName_'+ i +'" id="ClastName_'+ i +'" value="" class="formFieldX" /></td></tr>\r\n';
        //HTMLtext += '                                         <tr><td><label for="Cage_'+ i +'">Et&agrave; (mesi)</label></td><td class="field"><input type="text" name="Cage_'+ i +'" id="Cage_'+ i +'" value="" size="3" class="formFieldX" /></td></tr>\r\n';
        HTMLtext += '                                           <tr><td><label for="Cbd_'+ i +'">Birthdate</label></td><td class="field"><input name="Cbd_'+ i +'" id="Cbd_'+ i +'" value="" size="10" class="formFieldX" readonly/></td></tr>\r\n';
        HTMLtext += '                                           <tr><td><label for="Csex_'+ i +'">Sesso</label></td><td class="field"><select name="Csex_'+ i +'" id="Csex_'+ i +'" class="formSelL"><option value="" selected="selected">scegli</option><option value="M">M</option><option value="F" >F</option></select></td></tr>\r\n';
        HTMLtext += '                                           <tr><td colspan="2" class="field">[<a href="javascript:void(0);" onclick="delChildDiv('+ i +')" onfocus="this.blur();">x</a>]</td></tr>\r\n';
        HTMLtext += '                                       </tbody>\r\n';
        HTMLtext += '                                   </table>\r\n';
        HTMLtext += '                                   <input type="hidden" name="childId_'+ i +'" id="childId_'+ i +'" value="'+ i +'">\r\n';
        HTMLtext += '                               </div>\r\n';

		$('div#childField').append(HTMLtext);
		$('#Cbd_' + i).datepicker({yearRange: '-10:+0',dateFormat: 'yy-mm-dd'});
		j = i + 1
		HTMLcmdText = "";
		//if (j < 10 ) { HTMLcmdText += '<a onClick="javascript: insChildFields(' + j + ');">aggiungi biNbo</a>';}
		HTMLcmdText += '<a href="#" id="submitLink" onClick="javascript: validateAndSubmit();">completa registrazione </a>';
		$('div#cmdDiv').html(HTMLcmdText);
		numChild = $('input#numChild').attr('value');
		numChild++;
		$('input#numChild').attr('value', numChild);
	}
	
	function insChildFieldsLux(i) {
	       j = i+1;
	
        HTMLtext = '                                <div id="childF_'+ i +'" style="margin-top: 12px;">\r\n';
        HTMLtext += '                                   <table border="0" cellpadding="0" cellspacing="0" class="childData">\r\n';
        HTMLtext += '                                       <thead><tr><th colspan="2" style="color:#5A5A5A;">DATI '+ j +'&deg; FIGLIO <span style="margin-left:278px">[<a href="javascript:void(0);" onclick="ch = confirm(\'Attenzione! Cancellando questo elemento perderai i dati che hai inserito su questo figlio!\'); if(ch){delChildDivLux('+ i +')}" onfocus="this.blur();">x</a>]</span></th></tr></thead>\r\n';
        HTMLtext += '                                       <tbody>\r\n';
        HTMLtext += '                                           <tr><td><label for="CfirstName_'+ i +'">Nome *</label></td><td><label for="ClastName_'+ i +'">Cognome *</label></td></tr><tr><td class="field"><input type="text" name="CfirstName_'+ i +'" id="CfirstName_'+ i +'" value="" class="formFieldL" /></td><td class="field"><input type="text" name="ClastName_'+ i +'" id="ClastName_'+ i +'" value="" class="formFieldL" /></td></tr>\r\n';
        HTMLtext += '                                           <tr><td><label for="Cbd_'+ i +'">Data di nascita (aaaa-mm-dd) *</label></td><td><label for="Csex_'+ i +'">Sesso *</label></td></tr><tr><td class="field"><input name="Cbd_'+ i +'" id="Cbd_'+ i +'" value="" size="10" class="formFieldL" readonly/></td><td class="field"><select name="Csex_'+ i +'" id="Csex_'+ i +'" class="formSelM"><option value="" selected="selected">scegli</option><option value="M">M</option><option value="F" >F</option></select></td></tr>\r\n';
        HTMLtext += '                                           <tr></tr><tr></tr>\r\n';
        HTMLtext += '                                           <tr></tr>\r\n';
        HTMLtext += '                                       </tbody>\r\n';
        HTMLtext += '                                   </table>\r\n';
        HTMLtext += '                                   <input type="hidden" name="childId_'+ i +'" id="childId_'+ i +'" value="'+ i +'">\r\n';
        HTMLtext += '                               </div>\r\n';

		$('div#childField').append(HTMLtext);
		$('#Cbd_' + i).datepicker({yearRange: '-30:+0',dateFormat: 'yy-mm-dd'});
		j = i + 1
		HTMLcmdText = "";
		//if (j < 10 ) { HTMLcmdText += '<a onClick="javascript: insChildFields(' + j + ');">aggiungi biNbo</a>';}
		HTMLcmdText += '<a href="#" id="submitLink" onClick="javascript: validateAndSubmit();">completa registrazione </a>';
		$('div#cmdDiv').html(HTMLcmdText);
		numChild = $('input#numChild').attr('value');
		numChild++;
		$('input#numChild').attr('value', numChild);
	}
	
	function delChildDiv(i) {
	
		$('div#childF_' + i ).remove();
		numChild = $('input#numChild').attr('value');
		numChild--;
		$('input#numChild').attr('value', numChild);
		$('#nChildSelector').removeAttr('disabled');		
	}
	
	function delChildDivLux(i) {
	
		$('div#childF_' + i ).remove();
		numChild = $('input#numChild').attr('value');
		numChild--;
		$('input#numChild').attr('value', numChild);
		$('#nChildSelectorLux').removeAttr('disabled');		
	}
		

