﻿String.prototype.trim = function ()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function KeyPressToString(e) {					
	var keynum;
	var sKeyString = null;
	// IE
	if(window.event) { keynum = e.keyCode; }  
	// Netscape/Firefox/Opera
	else if(e.which)  { keynum = e.which; } 

	switch (keynum) {
		case 13:
			sKeyString = 'ENTER';
			break;
		default:
			sKeyString = null;
	}		
	return sKeyString;
}

function __changeCountry(oCountryElem, sProvTextId, sSelected, sProvinceSelId) {
	var oGeo = new WBEGeoInfo();
	var sProvId = oCountryElem.getAttribute("target");
	var oSel = document.getElementById(sProvId);
	var oField = eval("document.forms[0]." + sProvTextId);
	__selectArray(oCountryElem.options, sSelected); // Necesario para IE
	oGeo.changeGEOElement(oCountryElem, null, sSelected);
	if (sSelected!='ES') {
		oSel.style.display = 'none';
		oField.style.display = '';
		oGeo.clearSelect(oSel);
		oField.value = (sProvinceSelId!=null) ? sProvinceSelId : '';
	} else {
		oSel.style.display = '';
		oField.style.display = 'none';
		__selectArray(oSel.options, sProvinceSelId);
		__changeProvince(oSel, sProvTextId)
	}
}

function __changeProvince(oProvinceSel, sProvTextId) {
	var oField = eval("document.forms[0]." + sProvTextId);
	oField.value = (oProvinceSel.options != null && oProvinceSel.selectedIndex>=0)
		? oProvinceSel[oProvinceSel.selectedIndex].value
		: '';
}

function __selectArray(oArray, sValue) {
	var i;
	if (sValue==null) return;
	for (i=0;i<oArray.length;i++)
		if (oArray[i].value == sValue) oArray[i].selected=true;
}

function changedisplay(sIdName){
	var elem = document.getElementById(sIdName);
	if(elem != null){
		if(elem.style.display == 'none'){
			document.getElementById(sIdName).style.display = 'block';
		}else{
			document.getElementById(sIdName).style.display = 'none';
		}
	}
}

function displayblock(sIdName){
	var elem = document.getElementById(sIdName);
	if(elem != null){
		document.getElementById(sIdName).style.display = 'block';
	}
}

function load(sIdName){
	var sId = sIdName.split(';');
	var i;
	//alert(sIdName);
	//alert(sId.length);
	//alert(iContador);
	for(i=0;i<sId.length;i++)
	{
		//alert('sId[' + i + '] = ' + sId[i]);
		displayblock('menu'+sId[i]);
	}
}


function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=200');");
	if(eval("page" + id) == null)
		alert("Desactive el bloqueador de ventanas emergentes.");
}

function showTab(sTabID)
{
		//oculto todos
		document.getElementById('user_content_usuario').style.display='none';
		document.getElementById('user_content_acceso').style.display='none';
		document.getElementById('user_content_contacto').style.display='none';
		document.getElementById('user_content_empresa').style.display='none';
		document.getElementById('user_content_logo').style.display='none';
		
		document.getElementById('user_content_usuario_img').src='skin/images/ico_dat_user.gif';
		document.getElementById('user_content_acceso_img').src='skin/images/ico_user_acc.gif';
		document.getElementById('user_content_contacto_img').src='skin/images/ico_dat_emp.gif';
		document.getElementById('user_content_empresa_img').src='skin/images/ico_dat_cont.gif';
		document.getElementById('user_content_logo_img').src='skin/images/ico_logo.gif';
		
		document.getElementById('user_content_usuario_li').className ='';
		document.getElementById('user_content_acceso_li').className ='';
		document.getElementById('user_content_contacto_li').className ='';
		document.getElementById('user_content_empresa_li').className ='';
		document.getElementById('user_content_logo_li').className ='';
		
		switch(sTabID)
		{
			case 'user_content_usuario':
				document.getElementById('user_content_usuario').style.display='block';
				document.getElementById('user_content_usuario_img').src='skin/images/ico_dat_user_on.gif';
				document.getElementById('user_content_usuario_li').className ='selected';
				break;
			case 'user_content_acceso':
				document.getElementById('user_content_acceso').style.display='block';
				document.getElementById('user_content_acceso_img').src='skin/images/ico_user_acc_on.gif';
				document.getElementById('user_content_acceso_li').className ='selected';
				break;
			case 'user_content_contacto':
				document.getElementById('user_content_contacto').style.display='block';
				document.getElementById('user_content_contacto_img').src='skin/images/ico_dat_emp_on.gif';
				document.getElementById('user_content_contacto_li').className ='selected';
				break;
			case 'user_content_empresa':
				document.getElementById('user_content_empresa').style.display='block';
				document.getElementById('user_content_empresa_img').src='skin/images/ico_dat_cont_on.gif';
				document.getElementById('user_content_empresa_li').className ='selected';
				break;
			case 'user_content_logo':
				document.getElementById('user_content_logo').style.display='block';
				document.getElementById('user_content_logo_img').src='skin/images/ico_logo_on.gif';
				document.getElementById('user_content_logo_li').className ='selected';
				break;
		}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function AddCartAndBuyPrivateZone(sVerId)
{
	var oAjax = new WBE_AjaxClass();
	oAjax.addPostParameter('l_type_code', 'PRODUCT_LINE');
	oAjax.addPostParameter('l_content_version_id', sVerId);
	xmlObj = oAjax.throwEventXML('com_add_to_shopping_cart');
	window.location.href = 'proceso_compra_1.html';
}

function activar(pos){				
	var oList ;
	oList = document.getElementById('fecha_contrato_'+pos).value.split('/');
	var dia  = oList[0].toString();
	var mes  = oList[1].toString();
	var anyo  = oList[2].toString();
	
	if(document.getElementById('duracion_contrato_'+pos).value!=''){
		mes = parseInt(mes)+parseInt(document.getElementById('duracion_contrato_'+pos).value);
	}
	if (mes > 24){
		anyo = parseInt(anyo)+2;
		mes = mes-24;
	} else if (mes > 12){
		anyo = parseInt(anyo)+1;
		mes = mes -12;
	}
	dia.toString();
	mes.toString();
	anyo.toString();
	var today = new Date();
	var myDate=new Date();
	myDate.setFullYear(anyo, mes-1, dia);
	
	if (myDate>today)
	{
		document.getElementById('activo_'+pos).style.display='block';
	}
	else
	{
		document.getElementById('inactivo_'+pos).style.display='block';
	}
}

function getContractEnd(){				
	var oList ;
	oList = document.getElementById('fecha_contrato_1').value.split('/');
	var dia  = oList[0].toString();
	var mes  = oList[1].toString();
	var anyo  = oList[2].toString();
	
	if(document.getElementById('duracion_contrato_1').value!=''){
		mes = parseInt(mes)+parseInt(document.getElementById('duracion_contrato_1').value);
	}
	if (mes > 24){
		anyo = parseInt(anyo)+2;
		mes = mes-24;
	} else if (mes > 12){
		anyo = parseInt(anyo)+1;
		mes = mes -12;
	}
	dia.toString();
	if (mes==1){mes='Enero';}
	else if (mes==2){formatMes='Febrero';}
	else if (mes==3){formatMes='Marzo';}
	else if (mes==4){formatMes='Abril';}
	else if (mes==5){formatMes='Mayo';}
	else if (mes==6){formatMes='Junio';}
	else if (mes==7){formatMes='Julio';}
	else if (mes==8){formatMes='Agosto';}
	else if (mes==9){formatMes='Septiembre';}
	else if (mes==10){formatMes='Octubre';}
	else if (mes==11){formatMes='Noviembre';}
	else if (mes==12){formatMes='Diciembre';}
	anyo.toString();
	var formatDate = dia+' '+formatMes+' '+anyo;
	
	document.write(formatDate);
}
							
function getContractDays(){				
	var oList ;
	oList = document.getElementById('fecha_contrato_1').value.split('/');
	var dia  = oList[0].toString();
	var mes  = oList[1].toString();
	var anyo  = oList[2].toString();
	var mseg_dia=1000*60*60*24;
	var dias;
	
	if(document.getElementById('duracion_contrato_1').value!=''){
		mes = parseInt(mes)+parseInt(document.getElementById('duracion_contrato_1').value);
	}
	
	if (mes > 24){
		anyo = parseInt(anyo)+2;
		mes = mes-24;
	} else if (mes > 12){
		anyo = parseInt(anyo)+1;
		mes = mes -12;
	}
	dia.toString();
	mes.toString();
	anyo.toString();
	var today = new Date();
	var myDate=new Date();
	myDate.setFullYear(anyo, mes-1, dia);
	
	if (myDate>today)
	{
		dias = Math.ceil((myDate.getTime()-today.getTime())/(mseg_dia))
		document.write('<td><span>quedan '+dias+' días</span></td>');
	}
}	


function TPVConnect(xmlObj)
{
	if(xmlObj.childNodes[0].firstChild.nodeValue =='ok')
	{ 
		if(getCheckedValue(document.forms['InmediaFrm'].elements['formapago'])=='TPV-2')
			document.location.href = 'http://tpvonline.demini.com/Demini.Modules.TPV.Process/TPVProcess.aspx?TPVXUSER_ID=115&AMOUNT=' + xmlObj.childNodes[2].firstChild.nodeValue + '&ORDER=' + xmlObj.childNodes[1].firstChild.nodeValue;
		else
			document.location.href = 'proceso_compra_5.html?ORDER='+ xmlObj.childNodes[1].firstChild.nodeValue;
	}
	else
		alert('Se ha producido un error ');							
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


		
					
function getTypeNifFacturacion(elem,pos,index){
	
	var elem2 = document.getElementById('nif_' + index + '_'+pos);
	
	for( var x = 0; x < elem2.attributes.length; x++ ) {
		if( elem2.attributes[x].nodeName.toLowerCase() == 'expresionregular' ) {
			elem2.attributes[x].nodeValue=elem.value;
			if (elem.value == 'NIF'){
				document.getElementById('ejCif2').style.display = 'none';
				document.getElementById('ejNif2').style.display = 'block';
			} else if (elem.value == 'CIF'){
				document.getElementById('ejCif2').style.display = 'block';
				document.getElementById('ejNif2').style.display = 'none';
			}
		}
	}
	
}

function getTypeNifEntrega(elem,pos){
	
	var elem2 = document.getElementById('nif_1_'+pos);
	
	for( var x = 0; x < elem2.attributes.length; x++ ) {
		if( elem2.attributes[x].nodeName.toLowerCase() == 'expresionregular' ) {
			elem2.attributes[x].nodeValue=elem.value;
			if (elem.value == 'NIF'){
				document.getElementById('ejCif1').style.display = 'none';
				document.getElementById('ejNif1').style.display = 'block';
			} else if (elem.value == 'CIF'){
				document.getElementById('ejCif1').style.display = 'block';
				document.getElementById('ejNif1').style.display = 'none';
			}
		}
	}
	
}

function cambioPesetas(){
	document.getElementById("ver_euros").style.display="";
	document.getElementById("ver_pesetas").style.display="none";
	var i=0;
	var aObj = document.getElementsByName("unit_euros")
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}
	aObj = document.getElementsByName("unit_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}
	aObj = document.getElementsByName("tax_euros");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}
	aObj = document.getElementsByName("tax_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}
	aObj = document.getElementsByName("price_euros");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}
	aObj = document.getElementsByName("price_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}	
	document.getElementById("total_euros").style.display="none";
	document.getElementById("total_pesetas").style.display="";
	if(document.getElementById("pedido_euros")) document.getElementById("pedido_euros").style.display="none";
	if(document.getElementById("pedido_pesetas")) ocument.getElementById("pedido_pesetas").style.display="";
	if(document.getElementById("discount_euros")) document.getElementById("discount_euros").style.display="none";
	if(document.getElementById("discount_pesetas")) document.getElementById("discount_pesetas").style.display="";
	if(document.getElementById("recargo_euros")) document.getElementById("recargo_euros").style.display="none";
	if(document.getElementById("recargo_pesetas")) document.getElementById("recargo_pesetas").style.display="";	
}
function cambioEuros(){
	document.getElementById("ver_euros").style.display="none";
	document.getElementById("ver_pesetas").style.display="";
	var i=0;
	var aObj = document.getElementsByName("unit_euros")
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}
	aObj = document.getElementsByName("unit_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}
	aObj = document.getElementsByName("tax_euros");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}
	aObj = document.getElementsByName("tax_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}
	aObj = document.getElementsByName("price_euros");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="";
	}
	aObj = document.getElementsByName("price_pesetas");
	for(i=0;i<aObj.length;i++)
	{
		aObj[i].style.display="none";
	}			
	document.getElementById("total_euros").style.display="";
	document.getElementById("total_pesetas").style.display="none";
	if(document.getElementById("pedido_euros")) document.getElementById("pedido_euros").style.display="";
	if(document.getElementById("pedido_pesetas")) document.getElementById("pedido_pesetas").style.display="none";
	if(document.getElementById("discount_euros")) document.getElementById("discount_euros").style.display="";
	if(document.getElementById("discount_pesetas")) document.getElementById("discount_pesetas").style.display="none";
	if(document.getElementById("recargo_euros")) document.getElementById("recargo_euros").style.display="";
	if(document.getElementById("recargo_pesetas")) document.getElementById("recargo_pesetas").style.display="none";
	
}


/**************************************************************************************************/
/********************* PARA CODIGOS POSTALES Y LOCALIDADES ****************************************/
/**************************************************************************************************/
function GeoPostalCode()
{
	this.ajax = new WBE_AjaxClass();// Clase Ajax para lanzar los eventos

	this.changeElement = function  (oCurrentElement) 
	{	
		var oTargetElement = this.GetTargetElem(oCurrentElement);
		if (oTargetElement == null) return;	// No hay destino
		
		var sParentValue = '';
		if(oCurrentElement.selectedIndex) sParentValue = oCurrentElement[oCurrentElement.selectedIndex].text;
		else sParentValue = oCurrentElement[0].text;
		//Tipo de target
		var sTargetDataType = oTargetElement.getAttribute('data');
		var sSelectedValue = oTargetElement.getAttribute('defvalor');
		var sChildSelected;
		switch (sTargetDataType) {
			case "city":
					// Recupera las ciudades
					this.fillCities(oTargetElement, sParentValue, sSelectedValue)
				break;
			case "postal_code":
					// Recupera los paises y los rellena en el target
					this.fillPostalCodes(oTargetElement, sParentValue, sSelectedValue)
				break;
		}

		// Recursividad, procesa el hijo para resolver dependencias.
		if (sTargetDataType!=null) {
			this.changeElement(oTargetElement); 
		}
	}

	this.fillCities = function(oElem, sParentValue, sSelectedValue) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		this.clearSelect(oElem);
		oAjax.addPostParameter('province', sParentValue);
		var oXmlData = oAjax.throwEventXML("get_cities_postal_codes");
		if (oXmlData.tagName == 'cities') {
			return this.fillDataIntoSelect(oElem, oXmlData, sSelectedValue);
		}
	}

	this.fillPostalCodes = function(oElem, sParentValue, sSelectedValue) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		this.clearSelect(oElem);
		oAjax.addPostParameter('city', sParentValue);
		var oXmlData = oAjax.throwEventXML("get_cities_postal_codes");
		if (oXmlData.tagName == 'postal_codes') {
			return this.fillDataIntoSelect(oElem, oXmlData, sSelectedValue);
		}
	}
	
	/* ------------------------------------------------------------------------------------------------------------ */
	/* ----------------------------------------- Funciones internas ----------------------------------------- */
	/* ------------------------------------------------------------------------------------------------------------ */
	// Recupera el objeto target que modificará
	this.GetTargetElem = function(oCurrentElem) {
		var sTarget = oCurrentElem.getAttribute('target');
		if (sTarget!=null && sTarget!='') {
			var oElem = document.getElementById(sTarget);
			return oElem;
		}
		return null;
	}

	// Rellena el combo  
	this.clearSelect = function (oComboElem) {
		// Limpia el combo
		if (!oComboElem.options) return;
		for(var aux=oComboElem.options.length-1; aux>=0; aux--) oComboElem.options[aux] = null;
	}

	// Rellena el combo  
	this.fillDataIntoSelect = function (oComboElem, data, val_selected)
	{
		var value_selected = val_selected.toString();
		var opt;
		// Rellena el combo
		for(var i=0;i<data.childNodes.length;i++) {
			var oNode = data.childNodes[i];
			var sDesc = oNode.childNodes[1].firstChild.data;
			var sValue = oNode.childNodes[0].firstChild.data;
			var sDescription = sDesc.toString();
			opt = new Option(sDesc, sValue);
			if (value_selected!=null && (value_selected==sValue || sDescription.toUpperCase() == value_selected.toUpperCase())) {
				opt.selected = true;
			}
			oComboElem.options[oComboElem.length] = opt;
		}
		if (oComboElem.options.length>0) {
			oComboElem.options[0].value;
		}
	}
	
} // fin clase


function SaveAddress1(id)
{
	var oPostalCode = document.getElementById("codigo_postal_2_" + id);
	var oPostalCode1 = document.getElementById("codigo_postal_1_" + id);
	if (!oPostalCode1.options.length > 1) 
	{
		var oCode = oPostalCode[document.getElementById("codigo_postal_2_" + id).selectedIndex];
		var opt = new Option(oCode.text, oCode.value);
		opt.selected = true;
		oPostalCode1.options[0] = opt;
	}
	else
	{		
		for(var aux=oPostalCode1.options.length-1; aux>=0; aux--) oPostalCode1.options[aux] = null;
		for(var aux=0; aux < oPostalCode.options.length; aux++)
		{
			var opt = new Option(oPostalCode[aux].text, oPostalCode[aux].value);
			oPostalCode1.options[aux] = opt;				
		}
		oPostalCode1.selectedIndex = oPostalCode.selectedIndex;
	}
	var oLocation = document.getElementById("localidad_2_" + id);
	var oLocation1 = document.getElementById("localidad_1_" + id);
	if (!oLocation1.options.length > 1) 
	{				
		var oLoc = oLocation[document.getElementById("localidad_2_" + id).selectedIndex];
		var opt2 = new Option(oPostalCode.text, oPostalCode.value);
		opt2.selected = true;
		oLocation1.options[0] = opt2;			
	}
	else
	{
		for(var aux=oLocation1.options.length-1; aux>=0; aux--) oLocation1.options[aux] = null;
		for(var aux=0; aux < oLocation.options.length; aux++)
		{
			var opt = new Option(oLocation[aux].text, oLocation[aux].value);
			oLocation1.options[aux] = opt;
		}
		oLocation1.selectedIndex = oLocation.selectedIndex;
	}
	
	document.getElementById("nombre_1_" + id).value = document.getElementById("nombre_2_" + id).value;
	document.getElementById("apellidos_1_" + id).value = document.getElementById("apellidos_2_" + id).value;
	document.getElementById("typenif_1_" + id).value = document.getElementById("typenif_2_" + id).value;		
	getTypeNifEntrega(document.getElementById("typenif_1_" + id),id)
	document.getElementById("nif_1_" + id).value = document.getElementById("nif_2_" + id).value;
	document.getElementById("tipo_direccion_1_" + id).value = document.getElementById("tipo_direccion_2_" + id).value;
	document.getElementById("tipo_via_1_" + id).value = document.getElementById("tipo_via_2_" + id).value;
	document.getElementById("direccion_1_" + id).value = document.getElementById("direccion_2_" + id).value;	
	eval("document.InmediaFrm.provincia_nombre_1_" + id).selectedIndex = eval("document.InmediaFrm.provincia_nombre_2_" + id).selectedIndex;
	document.getElementById("pais_1_" + id).value = document.getElementById("pais_2_" + id).value;
	document.getElementById("telefono_1_" + id).value = document.getElementById("telefono_2_" + id).value;
	document.getElementById("telefono2_1_" + id).value = document.getElementById("telefono2_2_" + id).value;
	document.getElementById("fax_1_" + id).value = document.getElementById("fax_2_" + id).value;
}