var XMLHTTP;
var isUe;
var labelState;


function enableTipo()
{
	
	abilitaCampo('Telefono');
	abilitaCampo('Fax');
	abilitaCampo('TipoCli_0');
	abilitaCampo('TipoCli_1');
	abilitaCampo('Nome');
	abilitaCampo('Cognome');
	abilitaCampo('Indirizzo');
	abilitaCampo('Citta');
	abilitaCampo('Provincia');
	abilitaCampo('CAP');
	
	var tPaese = document.getElementById('Paese');
	
	Province(tPaese.value);
	
	if (vLanguage != 'IT')
	{
		jQuery.validator.messages.required = "This field is required";
		jQuery.validator.messages.email = "Please enter a valid email address"
	}
	else
	{
		jQuery.validator.messages.required = "Campo richiesto";
		jQuery.validator.messages.email = "Prego inserire un indirizzo email valido"
	}


	abilitaCampo('email');
	abilitaCampo('password');
	abilitaCampo('password_confirm');
	
	abilitaCampo('Submit');
	
	
	if (document.getElementById('TipoCli_0').checked)
	{
		Privato();
	}
	else
	{
		RagSociale();
	}

	
}

function RagSociale()
{
	
	var tblRef = document.getElementById('tblInd');
	
	eliminaRiga ("riga1");
	eliminaRiga ("riga2");
	eliminaRiga ("riga3");

	var tPaese = document.getElementById('Paese');
	
	var newRow = tblRef.insertRow(2);
	newRow.id ="riga1";
	newRow.bgColor = "#ffffff";
	
	
  	var newCell0 = newRow.insertCell(0);
	newCell0.bgColor = "#ffffff";
	if (vLanguage != 'IT')
	{
		newCell0.innerHTML = '<strong>Companies<strong>';
	}
	else
	{
		newCell0.innerHTML = '<strong>Ragione sociale<strong>';
	}

	
	var newCell1 = newRow.insertCell(1);
	newCell1.bgColor = "#ffffff";
	newCell1.innerHTML = '*';
	var newCell2 = newRow.insertCell(2);
	newCell2.bgColor = "#ffffff";
	
	newCell2.colSpan = "2";
	newCell2.innerHTML = '<input type="text" name="RAG_SOC" id="RAG_SOC" class="required" />';
	


	//var tPaese = document.getElementById('Paese');
	
			ajax_request("ajax-stato-ue.asp?iso=" + tPaese.value,null, function(data){ if(data == 1) {
										var newRow = tblRef.insertRow(3);
										newRow.id ="riga2";
										newRow.bgColor = "#ffffff";
										
  										var newCell0 = newRow.insertCell(0);
										newCell0.bgColor = "#ffffff";
										newCell0.vAlign="top";
										
										if (vLanguage != 'IT')
										{
											newCell0.innerHTML = '<strong>Vat number</strong>';
										}
										else
										{
											newCell0.innerHTML = '<strong>Partita iva</strong>';
										}

  										
  										var newCell1 = newRow.insertCell(1);
										newCell1.bgColor = "#ffffff";
  										newCell1.innerHTML = '&nbsp;';
  										var newCell2 = newRow.insertCell(2);
										newCell2.bgColor = "#ffffff";
										
										if (vLanguage != 'IT')
										{
											if ((tPaese.value == 'IT') || (tPaese.value == 'IT-IS') || (tPaese.value == 'SM'))
											{
  										newCell2.innerHTML = '<input type="text" name="USER_PIVA" id="USER_PIVA" value="" />'
											}
											else
											{
											newCell2.innerHTML ='<input type="text" name="USER_PIVA" id="USER_PIVA" value="" /><table width="100%" border="0" cellspacing="4" cellpadding="0"><tr><td align="left"><div class="box"><div class="pop_dazi"><table width="100%" border="0" cellspacing="4" cellpadding="0"><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align = "left">If the shipment country is in the European Union (but not Italy) and you have a valid VAT number,  please select here.  In this way, you&acute;ll avoid being charged by Italian VAT.  You can check the VAT number by clicking on: <a href="http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome&amp;selectedLanguage=EN" target="_blank">Link</a>. Music Center will verify the validity of your VAT number.  If you enter an invalid VAT number, we will not ship the goods until the VAT has been paid. </td></tr><tr><td>&nbsp;</td></tr><tr><td align = "left"><input type="checkbox" name="PIVA_VALIDA" id="PIVA_VALIDA" value="1" /><strong>My VAT number is valid</strong></td></tr></table></td></tr></table></div></div></td></tr></table>';
											}
										}
										else
										{
											if ((tPaese.value == 'IT') || (tPaese.value == 'IT-IS') || (tPaese.value == 'SM'))
											{
											newCell2.innerHTML = '<input type="text" name="USER_PIVA" id="USER_PIVA" value="" />'
											}
											else
											{
											newCell2.innerHTML = '<input type="text" name="USER_PIVA" id="USER_PIVA" value="" /><table width="100%" border="0" cellspacing="4" cellpadding="0"><tr><td align="left"><div class="box"><div class="pop_dazi"><table width="100%" border="0" cellspacing="4" cellpadding="0"><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align = "left">Se lo stato di spedizione &egrave; diverso dall&acute;Italia ed appartiene all&acute;unione europea e si dispone di una partita iva valida per la comunit&agrave; selezionare la casella. Questo vi permetter&agrave; di effettuare l&acute;ordine senza applicazione dell&acute;IVA. Se non siete sicuri potete effettuare un controllo a questo <a href="http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome&amp;selectedLanguage=EN" target="_blank">Link</a>. Music Center si riserva di verificare la validit&agrave; di tale partita iva. Non verranno spediti ordini effettuati senza IVA con partita iva non valida se non previo il pagamento dell&acute;IVA dovuta. </td></tr><tr><td>&nbsp;</td></tr><tr><td align = "left"><input type="checkbox" name="PIVA_VALIDA" id="PIVA_VALIDA" value="1" /><strong>La mia partita iva č valida</strong></td></tr></table></td></tr></table></div></div></td></tr></table>';
											}
													}
										
  																					}})

	
	if ((tPaese.value == 'IT') || (tPaese.value == 'IT-IS') || (tPaese.value == 'SM'))
	{
		var newRow = tblRef.insertRow(3);
		newRow.id ="riga3";
  		var newCell0 = newRow.insertCell(0);
		newCell0.bgColor = "#ffffff";
		
		

		if (vLanguage != 'IT')
			{
				newCell0.innerHTML = '<strong>Fiscal Code</strong>';
			}
		else
			{
				newCell0.innerHTML = '<strong>Codice fiscale</strong>';
			}
		
		
		var newCell1 = newRow.insertCell(1);
		newCell1.bgColor = "#ffffff";
		newCell1.innerHTML = '*';
		var newCell2 = newRow.insertCell(2);
		newCell2.bgColor = "#ffffff";
		
		newCell2.colSpan = "2";
		newCell2.innerHTML = '<input type="text" name="USER_CF" id="USER_CF" class="required" />';
		
	
	};
}

function Privato()
{
	var tblRef = document.getElementById('tblInd');
	
	
	
	eliminaRiga ("riga1");
	eliminaRiga ("riga2");
	eliminaRiga ("riga3");
	
	var tPaese = document.getElementById('Paese');

	var newRow = tblRef.insertRow(2);
	newRow.id ="riga1";
  	var newCell0 = newRow.insertCell(0);
	newCell0.bgColor = "#ffffff";
	
	if (vLanguage != 'IT')
	{
		newCell0.innerHTML = '<strong>Name</strong>';
	}
	else
	{
		newCell0.innerHTML = '<strong>Nome</strong>';
	}

	
	var newCell1 = newRow.insertCell(1);
	newCell1.bgColor = "#ffffff";
	newCell1.innerHTML = '*';
	var newCell2 = newRow.insertCell(2);
	newCell2.bgColor = "#ffffff";
	newCell2.colSpan = "2";
	newCell2.innerHTML = '<input type="text" name="Nome" id="Nome" class="required" />';
	
	var newRow = tblRef.insertRow(3);
	newRow.id ="riga2";
  	var newCell0 = newRow.insertCell(0);
	newCell0.bgColor = "#ffffff";
	
	if (vLanguage != 'IT')
	{
		newCell0.innerHTML = '<strong>Surname</strong>';
	}
	else
	{
		newCell0.innerHTML = '<strong>Cognome</strong>';
	}

	
	var newCell1 = newRow.insertCell(1);
	newCell1.bgColor = "#ffffff";
	newCell1.innerHTML = '*';
	var newCell2 = newRow.insertCell(2);
	newCell2.bgColor = "#ffffff";
	newCell2.colSpan = "2";
	newCell2.innerHTML = '<input type="text" name="Cognome" id="Cognome" class="required" />';

	
	
	if ((tPaese.value == 'IT') || (tPaese.value == 'IT-IS') || (tPaese.value == 'SM'))
	{
		var newRow = tblRef.insertRow(4);
		newRow.id ="riga3";
  		var newCell0 = newRow.insertCell(0);
		newCell0.bgColor = "#ffffff";
		
		if (vLanguage != 'IT')
			{
				newCell0.innerHTML = '<strong>Fiscal code</strong>';
			}
		else
			{
				newCell0.innerHTML = '<strong>Codice fiscale</strong>';
			}

		var newCell1 = newRow.insertCell(1);
		newCell1.bgColor = "#ffffff";
		newCell1.innerHTML = '*';
		var newCell2 = newRow.insertCell(2);
		newCell2.bgColor = "#ffffff";
		newCell2.colSpan = "2";
		newCell2.innerHTML = '<input type="text" name="USER_CF" id="USER_CF" class="required" />';
	
	}


}

function eliminaRiga(idRiga)
{
		var rowRef = document.getElementById(idRiga);
	if (rowRef != null)
	{
		rowRef.parentNode.removeChild(rowRef);
	}


}

function ajax_request(url, data, callback){
	try {
		xhrobj = new XMLHttpRequest();
		} catch (e) {
			try {
				xhrobj=new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					xhrobj=new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			xhrobj.open("GET", url);
			xhrobj.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			
			xhrobj.onreadystatechange = function() {
				if (xhrobj.readyState == 4 && xhrobj.status == 200) {
					callback(xhrobj.responseText);
				}
			};
			xhrobj.send(data);
		}
		
		
function changeDivContent( nameOfDiv, newContent ){
	var div = document.getElementById( nameOfDiv );
	if( div ){div.innerHTML = newContent;}
	}		
		

function abilitaCampo(idCampo)
{
		var Campo = document.getElementById(idCampo);
	if (Campo != null)
	{
		Campo.disabled = false;
	}


}

function disabilitaCampo(idCampo)
{
		var Campo = document.getElementById(idCampo);
	if (Campo != null)
	{
		Campo.disabled = true;
	}


}


function Province(Stringa)
{
    if ((Stringa == "IT") || (Stringa == "IT-IS") || (Stringa == "SM"))
    {
        var url = "ajax_province.asp?rnd="+Math.random(); 
        XMLHTTP = RicavaBrowser(CambioStatoProvince);
        XMLHTTP.open("GET", url, true);
        XMLHTTP.send(null);
		labelState = '<strong>Provincia</strong>';
		
    }
	
	else if (Stringa == "US")
    {
		//alert("US");
        var url = "ajax_province_1.asp?rnd="+Math.random(); 
        XMLHTTP = RicavaBrowser(CambioStatoProvince);
        XMLHTTP.open("GET", url, true);
        XMLHTTP.send(null);
		labelState = '<strong>State</strong>'

    } 

    else
    {
        var url = "ajax_province_2.asp?rnd="+Math.random(); 
        XMLHTTP = RicavaBrowser(CambioStatoNoProvince);
        XMLHTTP.open("GET", url, true);
        XMLHTTP.send(null);
		

    } 
}

function CambioStatoProvince()
{
    if (XMLHTTP.readyState == 4)
    {
        var R = document.getElementById("Province");
		//var R1 = document.getElementById("eCart1_1_ID_Add");
		
        R.innerHTML = XMLHTTP.responseText;
		//R1.value = XMLHTTP.responseText;
		
		var R = document.getElementById("NoProvinceAx");
        R.innerHTML = '*';

		var R = document.getElementById("NoProvince");
        R.innerHTML = labelState;

    }
}



function CambioStatoNoProvince()
{
    if (XMLHTTP.readyState == 4)
    {
        var R = document.getElementById("Province");
        R.innerHTML = XMLHTTP.responseText;
		
		var R = document.getElementById("NoProvince");
        R.innerHTML = '&nbsp;';
		
		var R = document.getElementById("NoProvinceAx");
        R.innerHTML = '&nbsp;';


    }
}


function RicavaBrowser(QualeBrowser)
{
    if (navigator.userAgent.indexOf("MSIE") != (-1))
    {
        var Classe = "Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5") != (-1));
        {
            Classe = "Microsoft.XMLHTTP";
        } 
        try
        {
            OggettoXMLHTTP = new ActiveXObject(Classe);
            OggettoXMLHTTP.onreadystatechange = QualeBrowser;
            return OggettoXMLHTTP;
        }
        catch(e)
        {
            alert("Errore: l'ActiveX non verrā eseguito!");
        }
    }
    else if (navigator.userAgent.indexOf("Mozilla") != (-1))
    {
        OggettoXMLHTTP = new XMLHttpRequest();
        OggettoXMLHTTP.onload = QualeBrowser;
        OggettoXMLHTTP.onerror = QualeBrowser;
        return OggettoXMLHTTP;
    }
	else if (navigator.userAgent.indexOf("Opera") != (-1))
    {
        OggettoXMLHTTP = new XMLHttpRequest();
        OggettoXMLHTTP.onload = QualeBrowser;
        OggettoXMLHTTP.onerror = QualeBrowser;
        return OggettoXMLHTTP;
    }

    else
    {
        alert("Il sito non funziona con altri browser!");
    }
}
