
function redirectToFile(dir,file_id,articulo_id,marca_id)
{
	if (file_id == "") 
	{
		alert("No se ha seleccionado un archivo");
		return false;
	}
	if (articulo_id == "") 
	{
		alert("No se ha seleccionado un tipo");
		return false;
	}
	
	var valor_marca =document.getElementById(marca_id).options[document.getElementById(marca_id).selectedIndex].value;
	var valor_art =document.getElementById(articulo_id).options[document.getElementById(articulo_id).selectedIndex].value;
	var uri = dir+"?id="+file_id+"&articulo_id="+valor_art+"&marca_id="+valor_marca;
	redirijir(uri);
  return ;
}

function redirijirPedido(dir,articulo_id,marca_id,pedido_id)
{	
	var marca =document.getElementById(marca_id).options[document.getElementById(marca_id).selectedIndex].value;
	var art =document.getElementById(articulo_id).options[document.getElementById(articulo_id).selectedIndex].value;
	var pedido =document.getElementById(pedido_id).value ;
	var uri = dir+"?articulo_id="+art+"&marca_id="+marca+"&pedido="+pedido;
	redirijir(uri);
  return ;
}
function copiarSelectaHidden(de, hacia)
{
	document.getElementById(hacia).value = document.getElementById(de).options[document.getElementById(de).selectedIndex].value;
	return true;
}

function submitClientePedido(de, hacia)
{
	var s=document.getElementById("formulario_pedidos");
	for (var i=0;i<s.length;i++)
  {
  	if (s.elements[i].type == 'button')
  	{ 
  		s.elements[i].disabled=true;			 
  	}
  }
	
	document.getElementById(hacia).value = document.getElementById(de).options[document.getElementById(de).selectedIndex].value;
	if (formulario_pedidos_validate())
	{
		document.getElementById('formulario_pedidos').submit();
	}
}

// recibe los nombres de los selects
function cambiarMarcas(select_articulo,select_marca)
{
	/*var artSelect = document.getElementsByName(articulo_id)[0];
	var marcSelect = document.getElementsByName(marca_id)[0];
	*/
	cargaContenido(select_articulo,select_marca);
	/*if (!s.checked) {
  	return error_message;
  }*/
  return ;
}

function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

function cargaContenido(select1,select2)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	if(valor==0)
	{
		// Si el usuario eligio la opcion "Elije", no voy al servidor y pongo todo por defecto
		combo=document.getElementById(select2);
		combo.length=0;
		var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Selecciona un tipo...";
		combo.appendChild(nuevaOpcion);	
		combo.disabled=true;
	}
	else
	{
		ajax=nuevoAjax();
		ajax.open("GET", main_host+"servicios/select_marcas.php?articulo_id="+valor+"&name_select="+select2, true);
		ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==1)
			{
				// Mientras carga elimino la opcion "Elije" y pongo una que dice "Cargando"
				combo=document.getElementById(select2);
				combo.length=0;
				var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Cargando...";
				combo.appendChild(nuevaOpcion); combo.disabled=true;	
			}
			if (ajax.readyState==4)
			{ 
				options_sel = ajax.responseText;
				document.getElementById(select2+"_div").innerHTML = options_sel;
			} 
		}
		ajax.send(null);
	}
}


// dir contiene toda la ruta incluso el nom de parametro que se va a completar con el valor que tenga el textox
// o sea 'script.php?nom_param=' y a eso se le concatena el sufijo
function redirectFromTBSubijo(dir,textBox_id,dir_sufijo, error_mensaje)
{
	var param =document.getElementById(textBox_id).value;
	if (!param || param==0 || param == '') {
    	alert(error_mensaje);
    	return false;
  }
	var uri = dir+param+dir_sufijo;
	redirijir(uri);
  return true;
}

/* Stack up window.onload event using this function from Simon Willison - http://www.sitepoint.com/blog-post-view.php?id=171578 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
 /*old popup code*/
function acpopup(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="resizable,scrollbars,height="+strHeight+",width="+strWidth;
window.open(strURL, '', strOptions);
}


/* new unobtrusive popup code */

/* XHTML target attribute */
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }
	
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var relIndex = anchor.rel;
		 if (relIndex){
		 var relSplit = relIndex.split("|");
		 /* XHTML compliant target attribute */
		 if (relSplit[0] == "external") 
            anchor.target = "_blank";
		/* XHTML compliant popup attribute */
   			else if (relSplit[0] == "popup") {
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
				anchor.popupMode = relSplit[3] ? relSplit[3] : 'console';
	      anchor.onclick = function() {acpopup(this.href,this.popupMode,this.popupWidth,this.popupHeight);return false;};
			}
		}
	   }
} 
function openwindow(url) 
{
	window.open(url, "popme", "width=560, height=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=auto, resizable=yes");
	focus();
} 

function openwin(url) 
{
	window.open(url, "popme", "width=560, height=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=auto, resizable=yes");
} 

addLoadEvent(function() {
	windowLinks();
});

//Determina el precio que corresponde a los productos segun el pais y el metodo de pago disponible
function cargarMetodoProductos(select1,div_prod,div_metodo)
{
	cargarProductos(select1,div_prod);
	cargarMetodos(select1,div_metodo);
	return true;
}


function cargarMetodoVentas(select1,div_prod,div_metodo)
{
	cargarProductosVentas(select1,div_prod);
	cargarMetodosVentas(select1,div_metodo);
	return true;
}


function cargarProductos(select1,div_prod)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	ax_prod=nuevoAjax();
	ax_prod.open("GET", main_host+"mi_cuenta/registro/cargar_producto.php?pais="+valor, true);
	ax_prod.onreadystatechange=function() 
	{ 
		/*
		if (ax_prod.readyState==1)
		{
			// Mientras carga elimino la opcion "Elije" y pongo una que dice "Cargando"
			div_obj_prod = document.getElementById(div_prod);
			div_obj_prod.innerHTML="Cargando...";	
		}*/
		if (ax_prod.readyState==4)
		{ 
			resultados = ax_prod.responseText;
			div_obj_prod = document.getElementById(div_prod);
			div_obj_prod.innerHTML = resultados;
			
		} 
	}
	ax_prod.send(null);
	return true;
}

function cargarProductosVentas(select1,div_prod)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	ax_prod_vent=nuevoAjax();
	ax_prod_vent.open("GET", main_host+"ventas/cargar_producto.php?pais="+valor, true);
	ax_prod_vent.onreadystatechange=function() 
	{ 
		
		if (ax_prod_vent.readyState==4)
		{ 
			resultados = ax_prod_vent.responseText;
			div_obj_prod = document.getElementById(div_prod);
			div_obj_prod.innerHTML = resultados;
			
		} 
	}
	ax_prod_vent.send(null);
	return true;
}


function cargarMetodos(select1,div_metodo)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	ax_metodo=nuevoAjax();
	ax_metodo.open("GET", main_host+"mi_cuenta/registro/cargar_metodo.php?pais="+valor, true);
	ax_metodo.onreadystatechange=function() 
	{ 
		/*
		if (ax_metodo.readyState==1)
		{
			// Mientras carga elimino la opcion "Elije" y pongo una que dice "Cargando"
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML="Cargando...";	
		}*/
		if (ax_metodo.readyState==4)
		{ 
			resultados = ax_metodo.responseText;
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML = resultados;
			
		} 
	}
	ax_metodo.send(null);
	return true;
}

function cargarMetodosVentas(select1,div_metodo)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	ax_metodov=nuevoAjax();
	ax_metodov.open("GET", main_host+"ventas/cargar_metodo.php?pais="+valor, true);
	ax_metodov.onreadystatechange=function() 
	{ 
		/*
		if (ax_metodo.readyState==1)
		{
			// Mientras carga elimino la opcion "Elije" y pongo una que dice "Cargando"
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML="Cargando...";	
		}*/
		if (ax_metodov.readyState==4)
		{ 
			resultados = ax_metodov.responseText;
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML = resultados;
			
		} 
	}
	ax_metodov.send(null);
	return true;
}


function cargarMetodosConfirmarRegistro(select1,div_metodo)
{
	var valor=document.getElementById(select1).options[document.getElementById(select1).selectedIndex].value;
	ax_metodoreg=nuevoAjax();
	ax_metodoreg.open("GET", main_host+"mi_cuenta/cargar_metodo.php?pais="+valor, true);
	ax_metodoreg.onreadystatechange=function() 
	{ 
		/*if (ax_metodo.readyState==1)
		{
			// Mientras carga elimino la opcion "Elije" y pongo una que dice "Cargando"
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML="Cargando...";	
		}*/
		if (ax_metodoreg.readyState==4)
		{ 
			resultados = ax_metodoreg.responseText;
			div_obj_metodo = document.getElementById(div_metodo);
			div_obj_metodo.innerHTML = resultados;
			
		} 
	}
	ax_metodoreg.send(null);
	return true;
}


function requeridoMotivoOpcional()
{
		var valor = document.getElementsByName('select_radio_motivos');
		var value_radio = "";
		for(i = 0; i < valor.length; i++)
		{
			if (valor[i].checked == true)
				value_radio = valor[i].value;
		}
		
		if (value_radio == 'DENUNCIADO_OTRO_MOTIVO')
    {
    	var val = document.getElementsByName('denuncia_desc')[0].value;
    	if (val == '')
    	{
    		return false;	
    	}
    }
    return true;
}

function aleatorio(inferior,superior)
{
	numPosibilidades = superior - inferior;
	aleat = Math.random() * numPosibilidades;
	aleat = Math.round(aleat);
	return parseInt(inferior) + aleat;
} 

function submitNewMember(divResults, mainHost, prm) 
{
	objSubmitNewMember= nuevoAjax();
	main_host = mainHost;
	objSubmitNewMember.open("GET", main_host+"/distribuidores/pagos/asigna_revendedor.php?prm="+prm, true);
	objSubmitNewMember.onreadystatechange=function() 
	{ 
		if (objSubmitNewMember.readyState==1)
		{
			document.getElementById(divResults).innerHTML = "Procesando su solicitud, por favor espere...";
		}
		if (objSubmitNewMember.readyState==4)
		{ 
			resultGet = objSubmitNewMember.responseText;
			if (resultGet != 'error')
			{
				document.getElementById(divResults).innerHTML = "Se asigno la membresia correctamente al usuario.";
				document.getElementById('cantidadCuentas').innerHTML = resultGet;
			}
			else
			{
				document.getElementById(divResults).innerHTML = "Ocurrio un error, no se pudo asignar la membresia.";
			}
		}
	}
	objSubmitNewMember.send(null);
}

function validSubmitMember(texto,divResults, mainHost, prm)
{
	 if (confirm(texto))
	 {
       submitNewMember(divResults, mainHost, prm);
   }
    return false; 
}