function get_xmlhttp(){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp
}

function enviar(xmlhttp,metodo,url,async,callback){
	xmlhttp.open(metodo, url,async);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			callback(xmlhttp)
		}
	}
	xmlhttp.send(null)
}
var goBackid = '';
var goBackpage = '';
var goBackcat = '';
var goBackPriCat = '';
var goBackNivel = '';
var subIcoOld = 0;
//funciones que me sacan en pantalla el select con cada una de las imágenes del servidor
function subcategoria(id,page,cat,pri_cat,nivel) {
	var unique_id = new Date().getTime();
	document.getElementById("listProduct").innerHTML = '<table width="385" border="0" cellpadding="0" cellspacing="0" class="tablas"><tr><td align="center" class="categorias" height="300" style="border-top:1px #EFEFEF solid; font-size:12px;">buscando productos...</td></tr></table>';
	goBackid=id;
	goBackpage=page;
	goBackcat=cat;
	goBackPriCat=pri_cat;
	goBackNivel = nivel;
	xmlhttp=get_xmlhttp();
	enviar(xmlhttp,"GET","subcategorias.php?id="+encodeURIComponent(id)+"&page="+encodeURIComponent(page)+"&cat="+encodeURIComponent(cat)+"&pri_cat="+encodeURIComponent(pri_cat)+"&uid=" + unique_id,true,getSub);
	//resetSubSubmenu();
	migaDePan(id,pri_cat);
	if(nivel == 1){
		document.getElementById("subIco"+id).src = "images/icono_menos.gif";
	}else{
		document.getElementById("subIco"+id).src = "images/icono_subcat_on.gif";
		if(subIcoOld > 0 && subIcoOld != id){
				document.getElementById("subIco"+subIcoOld).src = "images/icono_subcat_off.gif";
		}
		subIcoOld = id;		
	}
}
function getSub(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById("listProduct").innerHTML = xmlhttp.responseText;
}
function goBack(){
	goid=goBackid;
	gopage=goBackpage;
	gocat=goBackcat;
	gopricat=goBackPriCat;
	gonivel = goBackNivel;
	subcategoria(goid,gopage,gocat,gopricat,gonivel);
}
function detalles(id,cat){
	document.getElementById("listProduct").innerHTML = '<table width="400" border="0" cellpadding="0" cellspacing="0" class="tablas"><tr><td align="center" class="categorias" height="300" style="border-top:1px #EFEFEF solid; font-size:12px;">buscando productos...</td></tr></table>';		
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","detalles.php?id="+encodeURIComponent(id)+"&cat="+encodeURIComponent(cat),true,getSub);
}

function migaDePan(id,cat){
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","migaDePan.php?id="+encodeURIComponent(id)+"&cat="+encodeURIComponent(cat),true,getMiga);
}

function getMiga(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}
	document.getElementById("showMiga").innerHTML = primeraMiga+xmlhttp.responseText;
}
function agrProyecto(id,accn){
	document.getElementById("listadoProyecto").innerHTML = '<table width="139" border="0" cellpadding="0" cellspacing="0" class="tablas"><tr><td align="center" class="categorias" style="font-size:9px;">creando lista...</td></tr></table>';		
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","miProyecto.php?id="+encodeURIComponent(id)+"&accn="+encodeURIComponent(accn),true,getProyecto);
}
function getProyecto(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	document.getElementById("listadoProyecto").innerHTML = '';
	document.getElementById("listadoProyecto").innerHTML = xmlhttp.responseText;
}
function enviarProyecto(){
	email = document.formProyecto.email.value;
	document.getElementById("listadoProyecto").innerHTML = '<table width="139" border="0" cellpadding="0" cellspacing="0" class="tablas"><tr><td align="center" class="categorias" style="font-size:9px;">enviando proyecto...</td></tr></table>';	
	if(emailCheck(email)){	
	xmlhttp=get_xmlhttp();
	enviar(xmlhttp,"GET","miProyecto_enviar.php?email="+encodeURIComponent(email),false,enviarResultado);
	}else{
		document.getElementById("listadoProyecto").innerHTML = '<table width="139" border="0" cellpadding="0" cellspacing="0" class="tablas" align="center"><tr><td align="center" class="categorias" style="font-size:9px;">El E-mail ingresado es incorrecto.</td></tr><tr><td align="center" class="categorias" style="height:20px; background-color:#406BAF;"><a href="javascript:agrProyecto(0,2)" class="categorias" style="font-size:11px; font-weigth:bold;" >ver mi proyecto</a></td></tr></table>';
	}
}
function enviarResultado(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}
	if(miarroba(xmlhttp.responseText)){	
		document.getElementById("listadoProyecto").innerHTML = xmlhttp.responseText;
	}else{
		document.formProyecto.email.value = '';
		document.getElementById("listadoProyecto").innerHTML = xmlhttp.responseText;
		gotoTuProyecto();
	}
}

function miarroba(emailStr){
	var arroba=/@/;
	var matchArroba=emailStr.match(arroba);
	if (matchArroba==null) {
		return false;
	}else{
		return true;
	}
}

function enviar_post(xmlhttp,metodo,url,async,callback,vars,id,cat){
	xmlhttp.open(metodo, url,async);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			callback(xmlhttp);
		}
	}
	xmlhttp.send(vars)
}

function recomendar(producto,id_prod,cat,cod_producto){
	email = document.getElementById("emailRec").value;
	xmlhttp=get_xmlhttp()	
	enviar_post(xmlhttp,"POST","recomendar.php",true,getrecomendar,"producto="+producto+"&id_prod="+id_prod+"&cat="+cat+"&codigo="+cod_producto+"&email="+email,id_prod,cat);
}

function getrecomendar(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}
	document.getElementById("emailRec").value = "";
	detalles(id,cat);
}


function getDescription(id){
	var detail_old = document.getElementById("listProduct").innerHTML;
	document.getElementById("listProduct").innerHTML = '<table width="139" border="0" cellpadding="0" cellspacing="0" class="tablas"><tr><td align="center" class="categorias" style="font-size:9px;">Cargando...</td></tr></table>';		
	xmlhttp=get_xmlhttp()	
	enviar(xmlhttp,"GET","miDescripcion.php?id="+encodeURIComponent(id),true,putDescription);
}
function putDescription(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
			val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Page not found.")
	}	
	if(xmlhttp.responseText != "false"){
		document.getElementById("listProduct").innerHTML = xmlhttp.responseText;
	}else{
		document.getElementById("listProduct").innerHTML = detail_old;
	}
}
