// JavaScript Document
// inventario[chosen].paypal = inventario[chosen].paypal.substring(9,inventario[chosen].paypal.length - 4);
function invenObject(duracion,tipo,rapidpoints,precio,paypal,moneybookers){
    this.duracion = duracion;
    this.tipo = tipo;
    this.rapidpoints = rapidpoints;
    this.precio = precio;
	this.paypal = paypal;
	this.moneybookers = moneybookers;
}

function setObject(duracion,tipo,rapidpoints,precio,paypal,moneybookers) {
    inventario[objectArrayIndex++] = new invenObject(duracion,tipo,rapidpoints,precio,paypal,moneybookers);
}

var objectArrayIndex = 0;
var inventario = new Array();
var contarRState = 0;
var toditostateonof = 0;

function getXML(){
	if(window.ActiveXObject){
		var xmlObject = new ActiveXObject("Microsoft.XMLDOM");
		xmlObject.async="false";
		xmlObject.onreadystatechange = function(){
			if (xmlObject.readyState == 4) 
			{
				xmlObj=xmlObject.documentElement;
				fillarray(xmlObj);
			}
		}
		xmlObject.load("en/productos.xml");
	//////
	/////
	}else if(window.XMLHttpRequest){
		var xmlObject = new XMLHttpRequest();
		xmlObject.onreadystatechange = function(){
			if(xmlObject.readyState == 4 && xmlObject.status == 200){
				var response = xmlObject.responseXML.documentElement;
				nodesQuan = response.getElementsByTagName('duracion').length;
				for(var x=0; x<nodesQuan; x++){
					var xmlduracion = response.getElementsByTagName('duracion')[x].firstChild.data;
					var xmltipo = response.getElementsByTagName('tipo')[x].firstChild.data;
					var xmlrapidpoints = response.getElementsByTagName('rapidpoints')[x].firstChild.data;
					var xmlprecio = response.getElementsByTagName('precio')[x].firstChild.data;
					setObject(xmlduracion,xmltipo,xmlrapidpoints,xmlprecio,"");
				}
				linksPaypal();
			}
		}
		xmlObject.open("GET","en/productos.xml",true);
		xmlObject.send(null);
	}
};

var linkcontadorPP = 0;
if (window.XMLHttpRequest){
	xmlpaypalobj = new XMLHttpRequest();
	xmlmoneybookersobj = new XMLHttpRequest();
}
else if (window.ActiveXObject){
	xmlpaypalobj = new ActiveXObject("Microsoft.XMLHTTP");
	xmlmoneybookersobj = new ActiveXObject("Microsoft.XMLHTTP");
}

function linksPaypal(){
	xmlpaypalobj.onreadystatechange = function(){
		if(xmlpaypalobj.readyState == 4 && xmlpaypalobj.status == 200){
			if(linkcontadorPP<inventario.length){
				inventario[linkcontadorPP].paypal = xmlpaypalobj.responseText;
				linkcontadorPP++;
				linksPaypal();
			}else{
				linkcontadorPP = 0;
				linksMoneyBookers();
			}
		}
	}
	xmlpaypalobj.open("GET","getelements.php?accion=getpaypal&productid="+linkcontadorPP,true);
	xmlpaypalobj.send(null);
}

function linksMoneyBookers(){
	xmlmoneybookersobj.onreadystatechange = function(){
		if(xmlmoneybookersobj.readyState == 4 && xmlmoneybookersobj.status == 200){
			if(linkcontadorPP<inventario.length){
				inventario[linkcontadorPP].moneybookers = xmlmoneybookersobj.responseText;
				var container = document.getElementById("columna_izq");
				var productos = container.getElementsByTagName("a");
				Core.addEventListener(productos[linkcontadorPP], "click", setDetalle);
				linkcontadorPP++;
				linksMoneyBookers();
			}else{
			/////////////////Aqui finaliza la carga y aparecen los productos que estan hidden///////////////////////////////////////	
				document.getElementById("loadingprod").innerHTML="";
				document.getElementById("loadingprod").style.height="2px";
				document.getElementById("loadingprod").style.margin = "0";
                var classtexts = Core.getElementsByClass("text");
				classtexts[0].style.visibility = "visible";
				classtexts[0].style.height = "auto";
			}
		}
	}
	xmlmoneybookersobj.open("GET","getelements.php?accion=getmoneybookers&productid="+linkcontadorPP,true);
	xmlmoneybookersobj.send(null);
}

function fillarray(myxml){
	var nodesQuan = myxml.childNodes.length;
	for(var x=0; x<nodesQuan; x++){
		xmlduracion = myxml.childNodes(x).getElementsByTagName("duracion");
		xmltipo = myxml.childNodes(x).getElementsByTagName("tipo");
		xmlrapidpoints = myxml.childNodes(x).getElementsByTagName("rapidpoints");
		xmlprecio = myxml.childNodes(x).getElementsByTagName("precio");
		setObject(xmlduracion(0).text,xmltipo(0).text,xmlrapidpoints(0).text,xmlprecio(0).text,"");
	}
	linksPaypal();
}

var pageloaded = {
	init: function(){
		getXML();
		/*$.ajax({
			type: "GET",
			url: "en/productos.xml",
			dataType: "xml",
			success: function(xml) {
				$(xml).find('producto').each(function(){
					var xmlduracion = $(this).find('duracion').text();
					var xmltipo = $(this).find('tipo').text();
					var xmlrapidpoints = $(this).find('rapidpoints').text();
					var xmlprecio = $(this).find('precio').text();
					var xmlpaypal = $(this).find('paypal');
					//setObject(xmlduracion,"Cuenta Recuperada","",xmlprecio,"");
				});
			}
		});*/
		var toditobtn = document.getElementById("toditocash");
		Core.addEventListener(toditobtn, "click", showPago);
		var banamex = document.getElementById("banamex");
		Core.addEventListener(banamex, "click", setbanamex);
		var banorte = document.getElementById("banorte");
		Core.addEventListener(banorte, "click", setbanorte);
		var bancoopel = document.getElementById("bancoopel");
		Core.addEventListener(bancoopel, "click", setbancoopel);
		var moneygram = document.getElementById("moneygram");
		Core.addEventListener(moneygram, "click", setmoneygram);
		var santander = document.getElementById("santander");
		Core.addEventListener(santander, "click", setsantander);
		var toditosubmit = document.getElementById("toditosubmit");
		Core.addEventListener(toditosubmit, "click", checktodito);
		var mailersubmit = document.getElementById("mailersubmit");
		Core.addEventListener(mailersubmit, "click", checkmailer);
	}
};


function checkmailer(event){
	var mailermail = document.getElementById("mailermail");
	var s = mailermail.value;
	var alerta = 0;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	//if (s.length < 3 )
	if (filter.test(s)) alerta = 1;
	if (alerta==0){
		alert("Ingrese una direcci\xf3n de correo valida");
		Core.preventDefault(event);
	}
}

function checktodito(event){
	var toditomail = document.getElementById("toditomail");
	var s = toditomail.value;
	var alerta = 0;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	//if (s.length < 3 )
	if (filter.test(s)) alerta = 1;
	var toditotarjeta = document.getElementById("toditotarjeta").value;
	var toditopass = document.getElementById("toditopass").value;
	if (toditotarjeta.length == 0){
		alert("El campo tarjeta no puede estar vacio");
		Core.preventDefault(event);
	}else if (toditopass.length == 0){
		alert("El campo password no puede estar vacio");
		Core.preventDefault(event);
	}else if (alerta==0){
		alert("Ingrese una direcci\xf3n de correo valida");
		Core.preventDefault(event);
	}
}

function setbanamex(event){
	var banco = document.getElementById("banco");
	banco.setAttribute("value","banamex");
	document.getElementById("nombrebancom").innerHTML = "Banamex";
	showMailer();
	Core.preventDefault(event);
	}
	
function setmoneygram(event){
	var banco = document.getElementById("banco");
	banco.setAttribute("value","moneygram");
	document.getElementById("nombrebancom").innerHTML = "MoneyGram";
	showMailer();
	Core.preventDefault(event);
}
	
function setsantander(event){
	var banco = document.getElementById("banco");
	banco.setAttribute("value","santander");
	document.getElementById("nombrebancom").innerHTML = "Santander";
	showMailer();
	Core.preventDefault(event);
	}

	
function setbanorte(event){
	var banco = document.getElementById("banco");
	banco.setAttribute("value","banorte");
	document.getElementById("nombrebancom").innerHTML = "Banorte";
	showMailer();
	Core.preventDefault(event);
	}

	
function setbancoopel(event){
	var banco = document.getElementById("banco");
	banco.setAttribute("value","bancoopel");
	document.getElementById("nombrebancom").innerHTML = "Bancoopel";
	showMailer();
	Core.preventDefault(event);
	}

var chosenonetodito = 0;
function setDetalle(event){
	document.getElementById("detalle").style.visibility = "visible";
	document.getElementById("msgclick").style.height = "1px";
	document.getElementById("msgclick").style.width = "1px";
	document.getElementById("msgclick").style.overflow = "hidden";
	document.getElementById("msgclick").style.visibility = "hidden";
	var detalle = document.getElementById("detalle_texto");
	var container = document.getElementById("columna_izq");
	var productos = container.getElementsByTagName("a");
	for(var x=0; x<inventario.length ; x++){
		if(productos[x]==this){
			var chosen = x;
			chosenonetodito = chosen;
		}
	}
	// Duracion
	var duraciontx = document.createTextNode(inventario[chosen].duracion);
	var duracion = document.createElement("p");
	duracion.appendChild(duraciontx);
	duracion.setAttribute("id","duracion");
	detalle.replaceChild(duracion,document.getElementById("duracion"));
	// Tipo
	if(inventario[chosen].tipo == "Extension de Cuenta"){
		var tipotx = document.createTextNode("Extensión de Cuenta");
	}else{
		var tipotx = document.createTextNode(inventario[chosen].tipo);
	}
	var tipo = document.createElement("p");
	tipo.appendChild(tipotx);
	tipo.setAttribute("id","tipo");
	detalle.replaceChild(tipo,document.getElementById("tipo"));
	// Rapidpoints
	if(inventario[chosen].rapidpoints=="0"){
		inventario[chosen].rapidpoints = ""
	}
	var rapidpointstx = document.createTextNode(inventario[chosen].rapidpoints+" Rapidpoints");
	var rapidpoints = document.createElement("p");
	rapidpoints.appendChild(rapidpointstx);
	rapidpoints.setAttribute("id","rapidpoints");
	detalle.replaceChild(rapidpoints,document.getElementById("rapidpoints"));
	// Precio
	var preciotx = document.createTextNode("$" + inventario[chosen].precio + ".00 mx");
	var precio = document.createElement("p");
	precio.appendChild(preciotx);
	precio.setAttribute("id","precio");
	detalle.replaceChild(precio,document.getElementById("precio"));
	//Todito
/*	var toditomonto = document.getElementById("toditomonto");
	var toditoconcepto = document.getElementById("toditoconcepto");
	toditomonto.setAttribute("value",inventario[chosen].precio);
	toditoconcepto.setAttribute("value",inventario[chosen].tipo + " " + inventario[chosen].duracion);*/
	//paypal
	document.getElementById("paypalcode").innerHTML = inventario[chosen].paypal;
	//moneybookers
	document.getElementById("moneybookerscd").innerHTML = inventario[chosen].moneybookers;
	//
	var producto = document.getElementById("producto");
	producto.setAttribute("value",chosen);
	if(toditostateonof==1){
		showPago(event);
	}else{
		Core.preventDefault(event);
	}
}

function showPago(event){
	hideMailer();
	var toditoform = document.getElementById("pagotodito");
	toditoform.style.height = "auto";
	toditoform.style.width = "auto";
	toditoform.style.visibility = "visible";
	precioTInterger = parseInt(inventario[chosenonetodito].precio);
	precioTInterger = (Math.round(precioTInterger*1.10*100))/100;
	var preciotx = document.createTextNode("$" + precioTInterger.toFixed(2) + " mx");
	var precio = document.createElement("p");
	precio.appendChild(preciotx);
	precio.setAttribute("id","precio");
	document.getElementById("detalle_texto").replaceChild(precio,document.getElementById("precio"));
	// Todito
	var toditomonto = document.getElementById("toditomonto");
	var toditoconcepto = document.getElementById("toditoconcepto");
	toditomonto.setAttribute("value",precioTInterger);
	toditoconcepto.setAttribute("value",inventario[chosenonetodito].tipo + " " + inventario[chosenonetodito].duracion);
	toditostateonof=1;
	Core.preventDefault(event);
	}
	
function hidePago(){
	var toditoform = document.getElementById("pagotodito");
	toditoform.style.height = "2px";
	toditoform.style.width = "2px";
	var preciotx = document.createTextNode("$" + inventario[chosenonetodito].precio + ".00 mx");
	var precio = document.createElement("p");
	precio.appendChild(preciotx);
	precio.setAttribute("id","precio");
	document.getElementById("detalle_texto").replaceChild(precio,document.getElementById("precio"));
	toditostateonof=0;
	toditoform.style.visibility = "hidden";
	}
	
function showMailer(){
	hidePago();
	var mailer = document.getElementById("mailer");
	mailer.style.height = "auto";
	mailer.style.width = "auto";
	mailer.style.visibility = "visible";
	}
	
function hideMailer(event){
	var mailer = document.getElementById("mailer");
	mailer.style.height = "2px";
	mailer.style.width = "2px";
	mailer.style.visibility = "hidden";
	document.getElementById("nombrebancom").innerHTML = "";
	}
Core.start(pageloaded);