var tipo = 0;
var antiguo = new Array(null,null,null);
var antReg = new Array(null,null,null);

var aux_op;
function gOrden(opcion,ini,fin)
{	///solamente entra cuando es una opcion valida
	if (opcion == "-1") { opcion = idioma; }
	aux_op = opcion;
	
	var opValue = opcion.split("/");
	var opCountry = opValue[0];
	var opAddress = parseInt(opValue[1],10);
	var opRegion = parseInt(opValue[2],10);
	
	for (var ind=ini;ind<=fin;ind++)
	{	/*este proceso se tiene q repetir tantas veces como tengamos q gestioarlo*/
		var codCP = "addr_04"+ind;
		//document.getElementById(codCP).value = "";
		
		var lLayer; 
		switch(opAddress)
		{	case 0:	lLayer = "lCNP"+ind; break;
			case 1: lLayer = "lNCP"+ind; break;
		}
		
		if (antiguo[ind] != null)	/*existe un objeto*/
		{	document.getElementById(antiguo[ind]).style.display = "none"; }
		antiguo[ind] = lLayer;
				
		var oLayer;
		if (antReg[ind] != null)
		{	document.getElementById(antReg[ind]).style.display = "none"; }
		
		switch(opRegion)
		{	case 0: //oLayer = (opCountry=="ES")?"lPro":"lEst";
					oLayer = "lPro"+ind; tipo = 0;
					if (opCountry=="US") { oLayer = "lEst"+ind; tipo = 1; }
					break;
			case 1: oLayer = "lReg"+ind;  tipo = 2;
					break;
		}
		antReg[ind] = oLayer;
		document.getElementById(antiguo[ind]).style.display = "block";
		document.getElementById(antReg[ind]).style.display = "block";
	}	
}

/* funciones controlar el codigo postal */
function searchCP(cP,ind)
{	var codPostal = null;
	/*codigo provincia*/
	var codProv = "addr_06"+ind+tipo;
	var itemsProv = document.getElementById(codProv);
	for (var ind=0;ind<itemsProv.length;ind++)
	{	if (cP == itemsProv[ind].value)
		{	codPostal = ind; }
	}
	return codPostal;
}
function ValidarCP(ind)
{	/*codigo postal*/
	var codCP = "addr_04"+ind;
	var itemCP = document.getElementById(codCP).value;
	/*codigo provincia*/
	var codProv = "addr_06"+ind+tipo;
	var itemsProv = document.getElementById(codProv);	
	var op_aux = aux_op.split("/");
	var opCountry = op_aux[0];
	if (itemCP.length == 2)
	{	//buscar si exise algun codigo q sea igual
		itemCP = parseInt(itemCP,10)//para CP q empiezan por 0		
		if (tipo == 0)
		{		var codPostal = searchCP(itemCP,ind);
				if (codPostal != null)
				{	itemsProv.selectedIndex = codPostal; }
				else
				{	///solamente deberia de aplicarse para el caso de ESPAŅA
					if (opCountry == "ES") { itemsProv.selectedIndex = 0; }
				}
		}
	}
	if (itemCP.length < 2)
	{	///solamente deberia de aplicarse para el caso de ESPAŅA
		if (opCountry == "ES") { itemsProv.selectedIndex = 0; }
	}
}

/* ****************************************************************** */
/* funciones usasdas para validar direccion usuario de la tarjeta*/
var tipo2 = 0;
var antiguo2 = new Array(null,null,null);
var antReg2 = new Array(null,null,null);

var aux_op2;
function gOrden2(opcion,ini,fin)
{	//solamente entra cuando es una opcion valida
	if (opcion == "-1") { opcion = idioma; }
	aux_op2 = opcion;
	
	var opValue = opcion.split("/");
	var opCountry = opValue[0];
	var opAddress = parseInt(opValue[1],10);
	var opRegion = parseInt(opValue[2],10);

	for (var ind=ini;ind<=fin;ind++)
	{	/*este proceso se tiene q repetir tantas veces como tengamos q gestioarlo*/
	    var codCP = "t_addr_04"+ind;
		//document.getElementById(codCP).value = "";

		var lLayer;
		switch(opAddress)
		{	case 0:	lLayer = "ltCN"+(ind); break;
			case 1: lLayer = "ltNC"+(ind); break;
		}

    	if (antiguo2[ind] != null)	/*existe un objeto*/
		{	document.getElementById(antiguo2[ind]).style.display = "none"; }
		antiguo2[ind] = lLayer;

		var oLayer;
		if (antReg2[ind] != null)
		{	document.getElementById(antReg2[ind]).style.display = "none"; }

		switch(opRegion)
		{	case 0: //oLayer = (opCountry=="ES")?"lPro":"lEst";
					oLayer = "ltPro"+ind; tipo2 = 0;
					if (opCountry=="US") { oLayer = "ltEst"+ind; tipo2 = 1; }
					break;
			case 1: oLayer = "ltReg"+ind;  tipo2 = 2;
					break;
		}

		antReg2[ind] = oLayer;
		document.getElementById(antiguo2[ind]).style.display = "block";
		document.getElementById(antReg2[ind]).style.display = "block";
	}	
}
function searchCP2(cP,ind)
{	var codPostal = null;
	/*codigo provincia*/
	var codProv = "addr_06"+ind+tipo2;
	var itemsProv = document.getElementById(codProv);
	for (var ind=0;ind<itemsProv.length;ind++)
	{	if (cP == itemsProv[ind].value)
		{	codPostal = ind; }
	}
	return codPostal;
}
function ValidarCP2(ind)
{	/*codigo postal*/
	var codCP = "t_addr_04"+ind;
	var itemCP = document.getElementById(codCP).value;
	/*codigo provincia*/
	var codProv = "t_addr_06"+ind+tipo2;
	var itemsProv = document.getElementById(codProv);
	var op_aux2 = aux_op2.split("/");
	var opCountry2 = op_aux2[0];
	if (itemCP.length == 2)
	{	//buscar si exise algun codigo q sea igual
		itemCP = parseInt(itemCP,10)//para CP q empiezan por 0		
		if (tipo2 == 0)
		{	var codPostal = searchCP2(itemCP,ind);
			if (codPostal != null)
			{	itemsProv.selectedIndex = codPostal; }
			else
			{	///solamente deberia de aplicarse para el caso de ESPAŅA
				if (opCountry2 == "ES") { itemsProv.selectedIndex = 0; }
			}
		}
	}
	if (itemCP.length < 2)
	{	if (opCountry2 == "ES") { itemsProv.selectedIndex = 0; } }	
}
//comprobacion del campo CODIGO POSTAL
function ValidaRECP(formula,campo){ 
	//es correcto //comprobar q es numerico
    var noValido = 0;
    if (!validarRE(numericRegexp,campo)) { noValido = 1; }
    return noValido;
}
function ValidaRECPOthers(formula,campo)
{ 	//comprobacion
    var noValido = 0;
    if (!validarRE(alphaNumericSpecialCharRegexp,campo)) { noValido = 1; }
    return noValido;
}
function ValidaCodPos(formula,country,campo)
{	var longitud = campo.value.length;
	var noValido = 0;
	///Cuando el pais seleccionado es ESPAŅA
	if (country == "ES")
	{	switch (longitud)
		{	case 0: noValido = 2; break;
			case 5: noValido = ValidaRECP(formula,campo); break;
			default:noValido = 1; break;
	  }
	 }
	 else
	 {	///Cuando el pais seleccionado es distinto
	 	if (longitud == 0) noValido = 0;	//El campo CODIGO POSTAL, en otros paises no es obligatorio
	 	else
		{	if(longitud <= 10) noValido = ValidaRECPOthers(formula,campo);
			else noValido = 1;
		}
	}
	return noValido;
}
/*	Solamente es palicable para la plantillas IBDIRE */
function ValidaCodPosObliga(formula,country,campo)
{	var longitud = campo.value.length;
	var noValido = 0;
	///Cuando el pais seleccionado es ESPAŅA
	if (country == "ES")
	{	switch (longitud)
		{	case 0: noValido = 2; break;
			case 5: noValido = ValidaRECP(formula,campo); break;
			default:noValido = 1; break;
	  }
	 }
	 else
	 {	///Cuando el pais seleccionado es distinto
	 	if (longitud == 0) noValido = 2;	//El campo CODIGO POSTAL para campos OBLIGATORIOS
	 	else
		{	if(longitud <= 10) noValido = ValidaRECPOthers(formula,campo);
			else noValido = 1;
		}
	}
	return noValido;
}

var INVOICE ={
    ValidaCodPosObliga : function(formula,country,campo){
        if (campo.value == "00000") campo.value = "";
        var longitud = campo.value.length;
    	var noValido = 0;
    	///Cuando el pais seleccionado es ESPAŅA
    	if (country == "ES")
    	{	switch (longitud)
    		{	case 0: noValido = 2; break;
    			case 5: noValido = ValidaRECP(formula,campo); break;
    			default:noValido = 1; break;
    	  }
    	}
    	else if(country == "GI" || country == "AD"){
            if (longitud == 0){ noValido = "";campo.value = "00000";}
        }
    	else
    	 {	///Cuando el pais seleccionado es distinto
    	 	if (longitud == 0) noValido = 2;	//El campo CODIGO POSTAL para campos OBLIGATORIOS
    	 	else
    		{	if(longitud <= 10) noValido = ValidaRECPOthers(formula,campo);
    			else noValido = 1;
    		}
    	}
    	return noValido;
    }
}




///funcion de inicializacion de idioma disponible
function iniIdioma(objPais,tipoAddress)
{		var objIdiDisp = document.getElementById("cnes_10");
		var objHidden = document.getElementById("cnes_11");
		///Comprobamos que el objeto existe
		if (objIdiDisp != null)
		{		var a = eval(formulario+".addressType");
				var s = 0;
				if (a != null)
				{		var objCmb2 = document.getElementById("dcia_052");
						if (objCmb2 != null)
						{		for (var ind=0;ind<a.length;ind++)
								{	if (a[ind].checked)
									{	s = parseInt(a[ind].value,10); }
								}
						}
				}
				if (s == tipoAddress)
				{		objIdiDisp.disabled = false;
						var paises = document.getElementById(objPais);
						var opValue = paises.value.split("/");
						if (opValue[0] == "ES")
						{		objIdiDisp.value = "es";
								objIdiDisp.disabled = true;
						}
						objHidden.value = objIdiDisp.value;
				}
		}
}

///funcion que iguala las direcciones
function rescribeCN(opcion,qLayer)
{	swNoCountry = 0;
	if (opcion == "-1") { swNoCountry = 1; opcion = idioma; }		
	
	switch(qLayer)
	{		case 1: calle = "addr_01"; numero = "addr_02";  break; 
			case 2: calle = "addr_012"; numero = "addr_022"; break;
	}
	
	opAddressVisible= (document.getElementById("lCNP"+qLayer).style.display=="block")?0:1;	
	provincia = "addr_06"+qLayer+opAddressVisible;
	estado = "addr_06"+qLayer+opAddressVisible;
	region = "addr_06"+qLayer+"2";
	/*
	if (swNoCountry)
	{	if (document.getElementById(calle+opAddressVisible)!=null) {document.getElementById(calle+opAddressVisible).value = "";}
		if (document.getElementById(numero+opAddressVisible)!=null) {document.getElementById(numero+opAddressVisible).value = "";}
		if (document.getElementById(provincia)!=null) {document.getElementById(provincia).selectedIndex=0;}
		if (document.getElementById(estado)!=null) {document.getElementById(estado).selectedIndex=0;}
		if (document.getElementById(region)!=null) { document.getElementById(region).value=""; }
	}
	*/
	opAddressInVisible=Math.abs(opAddressVisible-1);	
	document.getElementById(calle+opAddressInVisible).value=document.getElementById(calle+opAddressVisible).value;	
	document.getElementById(numero+opAddressInVisible).value=document.getElementById(numero+opAddressVisible).value;
	
}

///funcion que iguala las direcciones
function rescribeCN2(opcion,qLayer)
{	swNoCountry = 0;
	if (opcion == "-1") { swNoCountry = 1; opcion = idioma; }

	switch(qLayer)
	{		case 1: calle = "t_addr_01"; numero = "t_addr_02";  break;
			case 2: calle = "t_addr_012"; numero = "t_addr_022"; break;
	}

	opAddressVisible= (document.getElementById("ltCN"+qLayer).style.display=="block")?0:1;

	provincia = "t_addr_06"+qLayer+opAddressVisible;
	estado = "t_addr_06"+qLayer+opAddressVisible;
	region = "t_addr_06"+qLayer+"2";


	/*
	if (swNoCountry)
	{	if (document.getElementById(calle+opAddressVisible)!=null) {document.getElementById(calle+opAddressVisible).value = "";}
		if (document.getElementById(numero+opAddressVisible)!=null) {document.getElementById(numero+opAddressVisible).value = "";}
		if (document.getElementById(provincia)!=null) {document.getElementById(provincia).selectedIndex=0;}
		if (document.getElementById(estado)!=null) {document.getElementById(estado).selectedIndex=0;}
		if (document.getElementById(region)!=null) { document.getElementById(region).value=""; }
	}
	*/
	opAddressInVisible=Math.abs(opAddressVisible-1);
	document.getElementById(calle+opAddressInVisible).value=document.getElementById(calle+opAddressVisible).value;
	document.getElementById(numero+opAddressInVisible).value=document.getElementById(numero+opAddressVisible).value;

}
