var list = new Array(1,2,3);
var module = new String ();
var event = new String ();
var htmlObjId;
var htmlObjId1;

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,target_object) {
 //alert('llegue met= '+metodo+' url= '+url+callback);exit;
 xmlhttp.open(metodo, url,async);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   callback(xmlhttp, target_object)
  }
 }
 xmlhttp.send(null)
}

function urlView( url, htmlId ) {
	//alert(url);exit;
	htmlObjId=htmlId;
	//startLoading();
 	xmlhttp=get_xmlhttp();
 	enviar(xmlhttp,"GET",url,true,urlGet);
 	//urlLink=url.substr(0,url.length-11);
 	//setFramework(urlLink);
    //document.getElementById("link").setAttribute("href",urlLink);
}

function urlView1( url, htmlId ) {
	//alert(htmlId);
	htmlObjId1=htmlId;
	//startLoading();
 	xmlhttp=get_xmlhttp();
 	enviar(xmlhttp,"GET",url,true,urlGet1);
 	//urlLink=url.substr(0,url.length-11);
 	//setFramework(urlLink);
    //document.getElementById("link").setAttribute("href",urlLink);
}

function urlGet(xmlhttp) {
//	 alert('llegue a urlGet');
	 val=""
	 for(i in xmlhttp) {
	 	try {
	 		val+="xmlhttp."+i+"="+xmlhttp[i]+"\n";
	 	} catch(e) {}
	 }
	 if(xmlhttp.status==404) {
	 	alert("Se supondría que esta página no existe")
	 }
	 if( xmlhttp.responseText!="") {
//		 document.getElementById(htmlObjId).innerHTML=xmlhttp.responseText;
		 document.getElementById(htmlObjId).innerHTML=xmlhttp.responseText;
//		 alert();
	 }
//	 stopLoading();
}

function urlGet1(xmlhttp) {
//	 alert('llegue a urlGet');
	 val=""
	 for(i in xmlhttp) {
	 	try {
	 		val+="xmlhttp."+i+"="+xmlhttp[i]+"\n";
	 	} catch(e) {}
	 }
	 if(xmlhttp.status==404) {
	 	alert("Se supondría que esta página no existe")
	 }
	 if( xmlhttp.responseText!="") {
//		 document.getElementById(htmlObjId).innerHTML=xmlhttp.responseText;
		 document.getElementById(htmlObjId1).innerHTML=xmlhttp.responseText;
//		 alert();
	 }
//	 stopLoading();
}


function startLoading(){
	load=document.getElementById("loading");
	loadImage=document.getElementById("loadingImage");
	/*loaded=document.getElementById(htmlObjId);
	load.style.left=loaded.offsetLeft;
	load.style.top=loaded.offsetTop;
	load.style.width=loaded.offsetWidth;
	load.style.height=loaded.offsetHeight;*/
	load.style.visibility='visible';
	loadImage.style.visibility='visible';
	//alert('Se muestra imagen sobre el objeto '+loaded.id+' \n left:'+loaded.offsetLeft+' top:'+loaded.offsetTop+' width'+loaded.offsetWidth+' Height'+loaded.offsetHeight);
}

function stopLoading(){
	/*if (!(module=='project' && event=='show')){
		document.getElementById("loading").style.visibility='hidden';
		document.getElementById("loadingImage").style.visibility='hidden';
		//alert('otra');
	}else{
		if(divId){
			//MM_preloadImages(imagesArray[i][0]);
			document.getElementById("loading").style.visibility='hidden';
			document.getElementById("loadingImage").style.visibility='hidden';
			//alert('desaparecio');
		}
	}*/
	//alert('No se muestra imagen');
	document.getElementById("loading").style.visibility='hidden';
	document.getElementById("loadingImage").style.visibility='hidden';
}



function getXML( ) {

	xmlhttp=get_xmlhttp()
	document.getElementById("imgStatus").src="framework/presenter/images/sm_loading.gif";
// 	var username=document.getElementById("user_EMAIL").value;
	enviar(xmlhttp,"GET","index.php?module=galeria&event=getXmlImagesArray&menujava=1",true,getXmlBack);


}


function getXmlBack(xmlhttp) {
	val=""
	for(i in xmlhttp) {
		try {
		val+="xmlhttp."+i+"="+xmlhttp[i]+"\n"
		} catch(e) {}
	}
	if(xmlhttp.status==404) {
		alert("Se supondría que esta página no existe")
	}
	if( xmlhttp.responseText!='') {
// 		alert( xmlhttp.responseText);
	}
	if( xmlhttp.responseXML!='') {
		var xmldoc = xmlhttp.responseXML;
		var root = xmldoc.getElementsByTagName("root").item(0);
		try{
			for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
					var node = root.childNodes.item(iNode);
					for (i = 0; i < node.childNodes.length; i++) {
						var sibl = node.childNodes.item(i);
						var len = parseInt(sibl.childNodes.length / 2);
						var arr = new Array(len);
						var cnt = 0;
						for (x = 0; x < sibl.childNodes.length; x++) {
								var sibl2 = sibl.childNodes.item(x);
								var sibl3;
								if (sibl2.childNodes.length > 0) {
									sibl3 = sibl2.childNodes.item(0);
									arr[cnt] = sibl3.data;
									cnt++;
								}
						}
						addrow("mytable", arr);
					}
			}
		}
		catch (e) { }
	} else {

	}
	document.getElementById("imgStatus").src="framework/presenter/images/sm_stop.gif";
}

function getVariableFromURL (url,variable){
	//alert(url+" busco "+variable);
	if(inicio=url.indexOf(variable, 0)+1){
	//alert(inicio);
	fin=url.indexOf('&',inicio);
	//alert(fin);
	return url.substring (inicio+variable.length,fin);
	}else
		return false;
}

function setEvent(url){
	event=getVariableFromURL(urlLink,"event");
	if(!event)
		event='show';
}
function setModule(url){
	module=getVariableFromURL(urlLink,"module");
	if(!module)
		module='home';
}
function setFramework(url){
	setModule(url);
	setEvent(url);
}
function getCategory( code){
	urlView('?module=product_category&code='+code+'&presenter=none&event=getProductList','div_body_contenido');
	//selectActive("category",id);
	//urlView1('?module=product_category&id='+id+'&presenter=none&event=getFirstElementDetail','div_productDetail');
}
function getProduct(id){
	urlView('?module=product&event=showDetail&id='+id+'&presenter=none','div_productDetail');
	selectActive("product",id);
}
function test(){
	 selectActive("category",5);
}
function selectActive(name,id){
	nuevoActivo=document.getElementById(name+id);
	padre=nuevoActivo.parentNode;
	activos=padre.getElementsByTagName("a");
	//Equivalente a foreach
	for (var i in activos){
		activos[i].className="inactive";
	}
	nuevoActivo.className="active";
}
//Funciones del carrito de compras
function addProduct( code ) {
	xmlhttp=get_xmlhttp();
	enviar(xmlhttp,"GET","?module=product_pedido&event=addProduct&code="+code,true,callbackAgregado );
}
function callbackAgregado(){
	alert ( "El producto  ha sido agregado al pedido");
}
function updateCant( code ){
	cant=document.getElementById('cant_'+code).value;
	xmlhttp=get_xmlhttp();
	enviar(xmlhttp,"GET","?module=product_pedido&event=updateCant&code="+code+"&cant="+cant,true,callbackCantidad );
}
function callbackCantidad(){
	window.location.reload();
}
function delProd( code ){
	xmlhttp=get_xmlhttp();
	enviar(xmlhttp,"GET","?module=product_pedido&event=delProd&code="+code,true,callbackEliminado);
}
function callbackEliminado(){
	alert("Producto Eliminado");
	window.location.reload();
}

