var TablaCODIGO = new Array();
var TablaDESCRIPCION = new Array();
var TablaPVD = new Array();
var CabEquipo = new Array();
var NumItems = 0;
var NumEquipos = 0;
var numEqu = 0;
var ItemSel = 0;
var IVA = 1.18;
var PVD = 1;
var PVP = 0.85;
var PV = PVP;
var COLUMNAS = 0;
var Equipo = new Array();
var conectado = 0;
var configurador = 0;
var confirmacion = 0;
var fabricantes = 0;

var GlobalMemoria =0;
var modulosseleccionados = 0;
var modulosseleccionados_old = 0;
var modulosvariables = 0;
var moduloscapacidad = '';
var moduloscapacidadtotal = '';

function acceso(){
	var password = document.getElementById('password').value;
	var username = document.getElementById('Nombre').value;
	P = password.toLowerCase();
	U = username.toLowerCase();
	passcode = 1;
	usercode = 1;
	for(i = 0; i < P.length; i++) { passcode *= P.charCodeAt(i);}
	for(x = 0; x < U.length; x++) { usercode *= U.charCodeAt(x);}
	if(usercode==12500972000&&passcode==2243049880780800){
		Cookie.set('usuario','1',{path: '/'},{duration: 365});
		conectado = 1;
		estado_conectado();
		}
		else{alert("Usuario y/o contraseña incorrecto")}
	}

function acceso2(){
	var x=0;
	var password = document.getElementById('password').value;
	var username = document.getElementById('Nombre').value;
http_request = false;
if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) {http_request.overrideMimeType('text/xml');}
} else if (window.ActiveXObject) { try {http_request = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {http_request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}}}
        if (!http_request) {alert('Falla :( No es posible crear una instancia XMLHTTP'); return false;}
	var url = "http://www.verybox.com/datos/login.php?codigo=" + username +"&password=" + password;		
	http_request.open("GET", url ,true);
 	http_request.onreadystatechange=function() {
  	if (http_request.readyState==4) {
   	xmldoc = http_request.responseXML;
	x = xmldoc.getElementsByTagName('LOGIN')[0].firstChild.data;
  	}
	}
 	http_request.send(null)
	
	if(x == 1 ){
	
		Cookie.set('usuario','1',{path: '/'},{duration: 365});
		conectado = 1;
		estado_conectado();
		}
		else{alert("Usuario o contraseña incorrecto")}
	}


function salir() {
		Cookie.set('usuario','0',{path: '/'},{duration: 365});
		conectado = 0;
		estado_conectado();
	}

function estado_conectado(){
	if (conectado==1){ 
		document.getElementById('conexion').title = 'Estás conectado';
		document.getElementById('conexion').src = 'http://www.verybox.com/pictures/interface/background/on.gif';
		PV = PVD;
		}
		else {
			document.getElementById('conexion').title = 'Estás desconectado';
			document.getElementById('conexion').src = 'http://www.verybox.com/pictures/interface/background/off.gif';
			PV = PVP;
		}
	Rellena_Precios();
	resetlogin();
	}
	
function resetlogin() {
	document.getElementById('Nombre').value = 'Usuario';
	document.getElementById('password').value = 'Contraseña';
	}
	
function cargadatos(sino) {
	configurador = sino;
	LoadXML('http://www.verybox.com/datos/equipos.xml','equipos');
}

/*----------------------------------------------------------------------------*/

/*Carga de datos*/
    function LoadXML(url,tipo) {
        http_request = false;
        if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) {http_request.overrideMimeType('text/xml');}
        } else if (window.ActiveXObject) { try {http_request = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {http_request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}}}
        if (!http_request) {alert('Falla :( No es posible crear una instancia XMLHTTP'); return false;}
        http_request.onreadystatechange = estado;
        http_request.open('GET', url, true);
        http_request.send(null);
        function estado() {
        if (http_request.readyState == 4) { 
            if (http_request.status == 200) {
				xmldoc = http_request.responseXML;
				if (tipo == 'equipos'){
					NumEquipos = xmldoc.getElementsByTagName('EQUIPO').length;
					x = xmldoc.getElementsByTagName('EQUIPO')[0].childNodes;
					for (i=0;i<x.length;i++) { 
						if (x[i].nodeType==1)  { 
							Equipo[COLUMNAS] = new Array();
							COLUMNAS += 1;
							CabEquipo[COLUMNAS-1] = x[i].nodeName;
							for (n=0;n<NumEquipos;n++){ Equipo[COLUMNAS-1][n] = xmldoc.getElementsByTagName(CabEquipo[COLUMNAS-1]).item(n).firstChild.data; }
						}
					}
					Equipo[COLUMNAS] = new Array();
//					alert('Equipo[0][0]: '+Equipo[0][0]+' | Equipo[COLUMNAS-1][NumEquipos-1]: '+Equipo[COLUMNAS-1][NumEquipos-1]);	
LoadXML('http://www.verybox.com/datos/tarifaxml.php','tarifa')
//LoadXML('http://www.verybox.com/datos/tarifa.xml','tarifa')
						
				}
				if (tipo == 'tarifa'){
					NumItems = xmldoc.getElementsByTagName('COMPONENTE').length;
					for (i=0;i<NumItems;i++){ TablaCODIGO[i] = xmldoc.getElementsByTagName('CODIGO').item(i).firstChild.data; }
					for (i=0;i<NumItems;i++){ TablaDESCRIPCION[i] = xmldoc.getElementsByTagName('DESCRIPCION').item(i).firstChild.data; }
					for (i=0;i<NumItems;i++){ TablaPVD[i] = xmldoc.getElementsByTagName('PVD').item(i).firstChild.data; }
					Precio_Equipos();
				}	
            } else { alert('Hubo problemas con la petición.'); }
        }
		}
    }
    
function buscarPVD(texto){ for (i = 0; i < NumItems; i++){ if (TablaCODIGO[i] == texto){ItemSel = i; break;} else {ItemSel = -1;} } return Math.round(TablaPVD[ItemSel]*100)/100;}

function buscarDESCRIPCION(texto){ for (i = 0; i < NumItems; i++){ if (TablaCODIGO[i] == texto){ItemSel = i; break;} else {ItemSel = -1;} } return TablaDESCRIPCION[ItemSel];}

function buscarCOMPONENTE(modelo,tipo){ for (j = 0; j < COLUMNAS; j++){ if (CabEquipo[j] == tipo) { ColSel = j; break;} } 
										for (k = 0; k < NumEquipos; k++){ if (Equipo[0][k] == modelo) {FilSel = k; break;} } 
										return Equipo[ColSel][FilSel]; }

function Precio_Equipos() {
	var precio = 0;
	for (k=0;k<NumEquipos;k++) {
		for (j=1; j<COLUMNAS; j++){ 
			if (Equipo[j][k]!=0){ precio += buscarPVD(Equipo[j][k]);}
			};
		Equipo[COLUMNAS][k] = precio;
		precio = 0;
		}
	Rellena_Precios();
	}

function Rellena_Precios() {
	if (fabricantes==1) {
	for (i=0; i<NumItems; i++) {
		x = document.getElementById(TablaCODIGO[i]); 
		if (x){
			if (PV==PVP){x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((buscarPVD(TablaCODIGO[i]))/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público | IVA incluido</p></span>";}
				else {x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((buscarPVD(TablaCODIGO[i]))/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público | IVA incluido</p></span>" + "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVD </span>" +  Math.round((buscarPVD(TablaCODIGO[i]))*100/PVD)/100+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio Venta a Distribución | IVA no incluido</p></span>";}
			}
		}
	} else if (fabricantes==2){
	for (i=0; i<NumEquipos; i++) {
		x = document.getElementById(Equipo[0][i]); 
		if (x){
			if (PV==PVP){x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((Equipo[COLUMNAS][i])/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público | IVA incluido</p></span>";}
				else {x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((Equipo[COLUMNAS][i])/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público | IVA incluido</p></span>" + "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVD </span>" +  Math.round((Equipo[COLUMNAS][i])/PVD)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio Venta a Distribución | IVA no incluido</p></span>";}
			}
		}
	} else {
	for (i=0; i<NumEquipos; i++) {
		x = document.getElementById(Equipo[0][i]); 
		if (x){
			if (PV==PVP){x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((Equipo[COLUMNAS][i])/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público<br />IVA incluido | Envío gratuito en península</p></span>";}
				else {x.innerHTML = "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVP </span>" +  Math.round(((Equipo[COLUMNAS][i])/PVP)*IVA)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Público recomendado<br />IVA incluido | Envío gratuito en península</p></span>" + "<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>PVD </span>" +  Math.round((Equipo[COLUMNAS][i])/PVD)+"<span style='font: 0.6em Arial; line-height: 0.8em; letter-spacing: 4px;'>&#8364;</span><span style='font: 0.4em Arial; line-height: 1em; letter-spacing: 0px;'><p>Precio de Venta a Distribución<br />IVA no incluido | Envío gratuito en península</p></span>";}
			}
		}
	}
	if (configurador==1) {
		GlobalMemoria =0;
		modulosseleccionados = 0;
		modulosseleccionados_old = 0;
		modulosvariables = 0;
		Constructor();
		document.getElementById('EquipoPedido').value = "Pedido de " + modelo;
		document.getElementById('emailform').value = "mail@verybox.com";
		GeneraMail();
		}
	if (confirmacion==1) {confirmacionjs(conectado);}
	if (confirmacion==2) {tarifajs(conectado);}
	}

/*----------------------------------------------------------------------------*/


/* Inicio */
function inicio() {
	if (Cookie.get("usuario")==1) { conectado=1; }	else { conectado=0;}
	estado_conectado();
	}
	
function iniciocondatos4() {fabricantes = 2; inicio();cargadatos(0);} //Equipos sin transporte (industriales)
function iniciocondatos3() {fabricantes = 1; inicio();cargadatos(0);} //Productos sueltos, barebones y componentes
function iniciocondatos2() {inicio();cargadatos(1);} //Equipos con mail de pedido
function iniciocondatos() {inicio();cargadatos(0);} //Equipos
function iniciosindatos(estado) {if (estado>0) {confirmacion=estado;}; inicio();}

