﻿
var xmlhttp;
var url = 'http://www.visureinrete.it/02_richiesta_servizi/Listacomuni.asp';
var urlbis = 'http://www.visureinrete.it/02_richiesta_servizi/verificacons.asp';
var ar;
var moduloattuale = '<%=modulo_attivo%>';

function okDatiModulo()
{
    try
    {
	    var IdModulo=document.f.modulo.value;
	    var ris;
    	
	    if (IdModulo=='')
	    {
            jAlertWrapper('Completare la Sezione A', 'Sezione A');
		    
		    ris= false;
	    }
	    else
	    {
		    ris = okdati();		
	    }

	    return ris;
    }
    catch(err)
    {
        err.message = 'okDatiModulo() :: ' + err.message;
        throw(err);
    }
}

function okdati()
{
	var CurrFile = location.pathname.split('/');
	var logmsg = '';
    try
    {
	    if (document.f.modulo.value == 'modulo1')
	    {
		    // controllo primo form  
            if (document.f.m1_Denominazione_ob.value == '')
		    {
			    jAlertWrapper('Inserire la denominazione della Società', 'Sezione A');
			    return false;
		    }
		    if (document.f.m1_Comune_ob.value == '')
		    {
			    jAlertWrapper('Inserire il Comune sede della Società', 'Sezione A');
			    return false;
		    }
		    else
		    {
		    	if (!check_letter(document.f.m1_Comune_ob.value))
                {
                    jAlertWrapper('Il campo Comune sede deve contenere solo caratteri alfabetici', 'Sezione A');
	                return false;
                }
		    }
		    if (document.f.m1_Provincia_ob.selectedIndex == 0)
		    {
			    jAlertWrapper('Selezionare la Provincia sede della Società', 'Sezione A');
			    return false;
		    }
		    
		    //if (document.f.m1_CodFiscIVA_ob.value == '')
		    //{
            //    jAlertWrapper('Inserire il Codice Fiscale della Società', 'Sezione A');
			//    return false;
			//}
            //else
            if (document.f.m1_CodFiscIVA_ob.value != '')
		    {
		    	if (document.f.m1_CodFiscIVA_ob.value.length < 11)
		        {
			        jAlertWrapper('Il campo Codice Fiscale deve contenere 11 caratteri numerici', 'Sezione A');
				    return false;
		        }
		        if (!isanum(document.f.m1_CodFiscIVA_ob.value) && document.f.m1_CodFiscIVA_ob.value.length == 11)
		        {
			        jAlertWrapper('Il campo Codice Fiscale deve contenere 11 caratteri numerici', 'Sezione A');
				    return false;
		        }
		        if (isanum(document.f.m1_CodFiscIVA_ob.value) && document.f.m1_CodFiscIVA_ob.value.length == 11)
		        {
		            if (!partitaIVA(document.f.m1_CodFiscIVA_ob.value))
			        {  	
			            jAlertWrapper('Codice Fiscale formalmente errato', 'Sezione A');
				        return false;
			        }
		        }
		    }
		    if (document.f.m1_Provincia_bene.selectedIndex == 0)
		    {
			    jAlertWrapper('Selezionare la Provincia di ricerca del bene', 'Sezione A');
			    return false;
		    }
			if (document.f.m1_Comune_bene.selectedIndex == 0)
		    {
			    jAlertWrapper('Selezionare il Comune di ricerca del bene', 'Sezione A');
			    return false;
		    }
		    if (document.f.m1_Conservatoria.value == '')
		    {
		    	jAlertWrapper('Riselezionare Provincia e Comune di ricerca del bene', 'Sezione A');
			    return false;
		    }
		    
		    logmsg = document.f.m1_Denominazione_ob.value.replace('\'', '\'\'') + ', ' + document.f.m1_Comune_ob.value.replace('\'', '\'\'') + ', ' + document.f.m1_Provincia_ob.value.replace('\'', '\'\'') + ', ' + document.f.m1_CodFiscIVA_ob.value.replace('\'', '\'\'') + ', ' + document.f.m1_Provincia_bene.value.replace('\'', '\'\'') + ', ' + document.f.m1_Comune_bene.value.replace('\'', '\'\'') + ', ' + document.f.m1_Conservatoria.value.replace('\'', '\'\'') + ', ' + document.f.m1_Note.value.replace('\'', '\'\'');
		    
		    document.f.m1_CodFiscIVA_ob.value = document.f.m1_CodFiscIVA_ob.value.toUpperCase();
	    	document.f.m1_Denominazione_ob.value = document.f.m1_Denominazione_ob.value.toUpperCase();
	    }
	}
	catch(err)
    {
        err.message = 'okdati(modulo1) :: ' + err.message;
        throw(err);
        
        return false;
    }
	
	try
    {
	    if (document.f.modulo.value == 'modulo2')
	    {
		    // controllo secondo form	
			if (document.f.m2_Nominativo_ob.value == '')
		    {
			    jAlertWrapper('Inserire Cognome e Nome del Nominativo', 'Sezione A');
			    return false;
		    }
		    if (document.f.m2_Nominativo_ob.value != '' && WordCount(document.f.m2_Nominativo_ob) == 1)
			{
				jAlertWrapper('Inserire Cognome e Nome del Nominativo', 'Sezione A');
			    return false;
			}
		    //if (document.f.m2_Comune_ob.value == '')
		    //{
			//    jAlertWrapper('Inserire il Comune di nascita del Nominativo', 'Sezione A');
			//    return false;
		    //}
		    //else
		    //{
		    //	if (!check_letter(document.f.m2_Comune_ob.value))
            //    {
            //        jAlertWrapper('Il campo Comune di nascita deve contenere solo caratteri alfabetici', 'Sezione A');
	        //        return false;
            //    }
		    //}
		    //if (document.f.m2_Provincia_ob.selectedIndex == 0)
		    //{
			//    jAlertWrapper('Selezionare la Provincia di nascita del Nominativo', 'Sezione A');
			//    return false;
		    //}
		    
		    if (document.f.m2_Comune_ob.value != '')
		    {
			    if (!check_letter(document.f.m2_Comune_ob.value))
	            {
	                jAlertWrapper('Il campo Comune di nascita deve contenere solo caratteri alfabetici', 'Sezione A');
	                return false;
	            }
			}
		    if ( document.f.m2_CodFiscale_ob.value == '' && (document.f.m2_gg_nasc_ob.value=='')&&(document.f.m2_mm_nasc_ob.value=='')&&(document.f.m2_aaaa_nasc_ob.value=='') )
		    {
		    	jAlertWrapper('Inserire i dati nel campo Codice Fiscale e/o nel campo Data di nascita del Nominativo', 'Sezione A');
				return false;
		    }
		    if ( (document.f.m2_gg_nasc_ob.value!='') || (document.f.m2_mm_nasc_ob.value!='') || (document.f.m2_aaaa_nasc_ob.value!='') )
		    {
			  	if (!check_data2(document.f.m2_gg_nasc_ob.value, document.f.m2_mm_nasc_ob.value, document.f.m2_aaaa_nasc_ob.value))
	            {
	                jAlertWrapper('Data di nascita formalmente errata', 'Sezione A');
	                return false;
	            }
		    }
		    if (document.f.m2_CodFiscale_ob.value != '')
		    {
		    	if (document.f.m2_CodFiscale_ob.value.length < 16)
		        {
			        jAlertWrapper('Il campo Codice Fiscale deve contenere 16 caratteri alfanumerici', 'Sezione A');
				    return false;
		        }
		        if (document.f.m2_CodFiscale_ob.value.length == 16)
		        {
		            if (!codiceFISCALE(document.f.m2_CodFiscale_ob.value))
			        {  	
			            jAlertWrapper('Codice Fiscale formalmente errato', 'Sezione A');
				        return false;
			        }
		        }
		    }
		    if (document.f.m2_Provincia_bene.selectedIndex == 0)
		    {
			    jAlertWrapper('Selezionare la Provincia di ricerca del bene', 'Sezione A');
			    return false;
		    }
			if (document.f.m2_Comune_bene.selectedIndex == 0)
		    {
			    jAlertWrapper('Selezionare il Comune di ricerca del bene', 'Sezione A');
			    return false;
		    }
		    if (document.f.m2_Conservatoria.value == '')
		    {
		    	jAlertWrapper('Riselezionare Provincia e Comune di ricerca del bene', 'Sezione A');
			    return false;
		    }

			logmsg = document.f.m2_Nominativo_ob.value.replace('\'', '\'\'') + ', ' + document.f.m2_Comune_ob.value.replace('\'', '\'\'') + ', ' + document.f.m2_Provincia_ob.value.replace('\'', '\'\'') + ', ' + (document.f.m2_gg_nasc_ob.value + document.f.m2_mm_nasc_ob.value + document.f.m2_aaaa_nasc_ob.value) + ', ' + document.f.m2_CodFiscale_ob.value.replace('\'', '\'\'') + ', ' + document.f.m2_Provincia_bene.value.replace('\'', '\'\'') + ', ' + document.f.m2_Comune_bene.value.replace('\'', '\'\'') + ', ' + document.f.m2_Conservatoria.value.replace('\'', '\'\'') + ', ' + document.f.m2_Note.value.replace('\'', '\'\'');

			document.f.m2_CodFiscale_ob.value = document.f.m2_CodFiscale_ob.value.toUpperCase();
	    	document.f.m2_Nominativo_ob.value = document.f.m2_Nominativo_ob.value.toUpperCase();
	    }
	}
	catch(err)
    {
        err.message = 'okdati(modulo2) :: ' + err.message;
        throw(err);
        
        return false;
    }
   
	//Tutto ok, scrivo log
	var paramErr = new Array('javascript', '0', '['+ sessId + '] :: ' + 'okdati (' + logmsg + ')', 'true', CurrFile[CurrFile.length - 1]);		
	Log(paramErr);

	return true;
}


function caricacomune(cboprov, cbocomu)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=function()
											{ 
												if (xmlhttp.readyState == 4)
													if (xmlhttp.status == 200)
														caricacom(xmlhttp, cbocomu);
											}
											
		xmlhttp.open("GET", url + '?P=' + cboprov.value, true);
		xmlhttp.send(null);
	}
	else
	{
		jAlertWrapper('Il tuo browser non supporta l\'oggetto XMLHTTP', 'Attenzione');
	}
}


function caricacom(xmlhttp, cbocomu)
{
	if (xmlhttp.readyState == 4)									// 4 = "loaded"
	{
	  	if (xmlhttp.status == 200)									// 200 = OK
		{
		     var array = xmlhttp.responseText.split('*');
		
			 for (var i = cbocomu.length; i>=0; i--)
			 {
				cbocomu.options[i] = null;
			 }    
			 
			 var newOption = new Option('-->','');
			 cbocomu.options[cbocomu.options.length] = newOption;		
			 
			 for (cont=0; cont<array.length; cont++)
			 { 
		    	var newOption = new Option(array[cont],array[cont]);
		    	cbocomu.options[cbocomu.options.length] = newOption;		
			 }
		}
	  	else
	    {
	    	//alert("Problem retrieving XML data");
	    	jAlertWrapper('Problem retrieving XML data', 'Attention');
	    }
	}
}


function verificacons(cboprov, cbocomu, tr, txtcons, conshidd)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp != null)
	{
		xmlhttp.onreadystatechange=function()
											{ 
												if (xmlhttp.readyState == 4)
													if (xmlhttp.status == 200) 
														vericons(xmlhttp, tr, txtcons, conshidd);
											}      

		xmlhttp.open("GET", urlbis + '?P=' + cboprov.value + '&C=' + cbocomu.value, true);
		xmlhttp.send(null);
	}
	else
	{
		jAlertWrapper('Il tuo browser non supporta l\'oggetto XMLHTTP', 'Attenzione');
	}
}


function letturanote(esito)
{
	if (esito=='ok')
	{
		if (document.f.modulo.value  =='modulo1')
		{			
		}
		
		if (document.f.modulo.value =='modulo2')
		{
		}

		Tipo_Pag()
	}
	
	if (esito=='ko')
	{
		if (moduloattuale=='modulo1')
		{
			document.f.controllo2.value = '';
		}
		
		if (moduloattuale=='modulo2')
		{
			document.f.controllo2.value = '';
		}
	}
}