function xmlhttp() {
    var xmlhttp;
    try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); }
        catch (e) { try { xmlhttp = new XMLHttpRequest(); }
        catch (e) { xmlhttp = false; }}}
    if (!xmlhttp) return null;
    else return xmlhttp;
}

function enviar(){

var login = document.getElementById('login').value;
var password = document.getElementById('password').value;


	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ajaxRequest.responseText==0){
				alert("login y/o password no pueden ser vacios");
			}
			if(ajaxRequest.responseText==1){
				location.href = "index.php";
			}
			if(ajaxRequest.responseText==2){
				alert("login y password no coinciden");
			}
		}
	}

	var valores = 'login=' + login + '&password=' + password;
	var queryString = 'includes/validarLogin.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);

}

function enviarDatosForm1(idProducto,modulo,accion){
var campos="";

if(accion=="agregar"){
var resp="agrego";
}else if(accion=="modificar"){
var resp="modifico";
}

if(modulo=='canales' || modulo=='puntosDeVentas' || modulo=='sucursales'){
	var nombre = document.getElementById('nombre').value;
	var id=document.getElementById('id').value;
	var campos='nombre=' + nombre + '&id=' + id + '&modulo=' + modulo + '&accion=' + accion;
}else if(modulo=='productos'){
	var elementos=document.getElementById('content').getElementsByTagName('select');
	if(document.getElementById('excento').checked==true){var excento=1}else{excento=0}
	var codigoBarra=document.getElementById('codigoBarra').value;
	for(var x=0; x<elementos.length;x++){
		if(elementos[x].value!=0){
			campos+="&"+elementos[x].id+"="+elementos[x].value;
		}
	}
	campos+="&codigoBarra="+codigoBarra+"&modulo="+modulo+"&accion="+accion+"&excento="+excento+"&idProducto="+idProducto;

}else{
	var nombre = document.getElementById('nombre').value;
	var campos='nombre=' + nombre + '&modulo=' + modulo + '&accion=' + accion;
}

	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ajaxRequest.responseText==0){
				alert("No se pudo "+ accion +" el "+modulo);
			}
			if(ajaxRequest.responseText==1){
				alert("Se "+ resp +" con exito el "+modulo);
			}
		}
	}

	var valores = campos;
	var queryString = 'modulos/ajax.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);
}

function enviarDatosForm3(modulo, accion, idMonitoreo){

	var encuestador = document.getElementById('encuestadores').value;
	var fecha = document.getElementById('fecha').value;
	var hora = document.getElementById('hora').value;
	var observacion = document.getElementById('observacion').value;
	var ciudad = document.getElementById('ciudades').value;	
	var canal = document.getElementById('canales').value;
	var puntoDeVenta = document.getElementById('pptoVenta').value;
	var sucursal = document.getElementById('sucursal').value;
	
	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		
			if(ajaxRequest.responseText==0){
				alert("No se agrego el monitoreo");
			}
			if(ajaxRequest.responseText==1){
				alert("Se agrego con exito el monitoreo");
			}
			if(ajaxRequest.responseText==2){
				alert("El producto seleccionado no esta en existencia");
			}
			
		}
	}

	var detalle = document.getElementById('detalle');
	var dinputs = detalle.getElementsByTagName('input');
	var dselects = detalle.getElementsByTagName('select');
	var valores = '&idMonitoreo='+idMonitoreo+'&accion='+accion+'&encuestador='+encuestador+'&fecha='+fecha+'&hora='+hora+'&observacion='+observacion+'&ciudad='+ciudad+'&canal='+canal+'&puntoDeVenta='+puntoDeVenta+'&sucursal='+sucursal;

	for($x=0;$x<dinputs.length;$x++){
		if(dinputs[$x].name != ''){
			valores += '&' + dinputs[$x].name + '=' + dinputs[$x].value;
		}
	}

	for($x=0;$x<dselects.length;$x++){
		if(dselects[$x].name != ''){
			valores += '&' + dselects[$x].name + '=' + dselects[$x].value;
		}
	}
	
	
	var queryString = 'modulos/'+modulo+'/ajax.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);

}

function enviarReporte(reporte){

var marca="";
var ciudad =document.getElementById('ciudad').value;
var canal =document.getElementById('canal').value;
var pdv =document.getElementById('pdv').value;
var categoria =document.getElementById('categorias0').value;
var sistema=document.getElementById('sistemas0').value;
var subCategoria=document.getElementById('subCategorias0').value;

if(reporte=='precios' || reporte=='presencia' || reporte=='promociones' || reporte=='planograma'){
var marca='&marca[]='+document.getElementById('Marcas0').value;
document.getElementById('envioReporte').disabled=true;
}else if(reporte=='graficaPrecio' || reporte=='graficaTendencia'){
	var presentacion=document.getElementById('presentacion').value;
	var objMarca=document.getElementById('Marcas0');
	for(var x=0;x<objMarca.length;x++){
		if (objMarca[x].selected==true){
			marca+='&marca[]='+objMarca[x].value;
		}		
	}
	
	if(categoria==0 || sistema==0 || subCategoria==0 || marca==0 || presentacion==0){
		alert('debe llenar todos los campos requeridos (*) de productos');
		return false;
	}else{
	document.getElementById('envioReporte').disabled=true;
	}
	
}

var mesDesde=document.getElementById('mesDesde').value;
var anioDesde=document.getElementById('anioDesde').value;
var mesHasta=document.getElementById('mesHasta').value;
var anioHasta=document.getElementById('anioHasta').value;

	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('principal');
			ajaxDisplay.innerHTML =ajaxRequest.responseText;
			if(reporte!='graficaPrecio' && reporte!='graficaTendencia'){
				document.getElementById('excel').style.display='';
			}
			document.getElementById('imprimir').style.display='';
		}
	}

	var valores ='&ciudad='+ciudad+'&canal='+canal+'&pdv='+pdv+'&mesDesde='+mesDesde+'&anioDesde='+anioDesde+'&mesHasta='+mesHasta+'&anioHasta='+anioHasta+'&reporte='+reporte+'&categoria='+categoria+'&sistema='+sistema+'&subCategoria='+subCategoria+marca+'&presentacion='+presentacion;
	var queryString = 'modulos/monitoreos/ajaxReportes.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);
	

}

function agrandar(campo){
    
	campo.style.width = 150;
	
}

function reducir(campo){
	
	campo.style.width = 40;
}

function enviarProducto(campo, fila){
	
	reducir(campo);
	//var idName=campo.id;
	//var cantCaracteres = (idName.length)-1;
	//var name=idName.substring(0,cantCaracteres);
	

	var arreglo=campo.id;
	var name="";
	
	for(var x=0;x<arreglo.length;x++){
		
		if(isNaN(arreglo.charAt(x))==true){
			name+=arreglo.charAt(x);
		}
	}
	

	
	switch(name){
	
	case 'categorias':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idValores="&idCategoria="+idCategoria;
		var idCampo='sistema'+fila;			
	break;

	case 'sistemas':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema;
		var idCampo='subCategoria'+fila;
	break;

	case 'subCategorias':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idSubCategoria=document.getElementById('subCategorias'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema+"&idSubCategoria="+idSubCategoria;
		var idCampo='marca'+fila;
	break;

	case 'Marcas':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idSubCategoria=document.getElementById('subCategorias'+fila).value;
		var idMarca=document.getElementById('Marcas'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema+"&idSubCategoria="+idSubCategoria+"&idMarca="+idMarca;
		var idCampo='presentacion'+fila;
	break;

	case 'Presentaciones':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idSubCategoria=document.getElementById('subCategorias'+fila).value;
		var idMarca=document.getElementById('Marcas'+fila).value;
		var idPresentacion=document.getElementById('Presentaciones'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema+"&idSubCategoria="+idSubCategoria+"&idMarca="+idMarca+"&idPresentacion="+idPresentacion;
		var idCampo='sabor'+fila;
	break;
	
	case 'Sabores':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idSubCategoria=document.getElementById('subCategorias'+fila).value;
		var idMarca=document.getElementById('Marcas'+fila).value;
		var idPresentacion=document.getElementById('Presentaciones'+fila).value;
		var idSabor=document.getElementById('Sabores'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema+"&idSubCategoria="+idSubCategoria+"&idMarca="+idMarca+"&idPresentacion="+idPresentacion+"&idSabor="+idSabor;
		var idCampo='envase'+fila;
	break;
	
	}
	
	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(idCampo);
			ajaxDisplay.innerHTML =ajaxRequest.responseText;
		}
	}

	var valores = idValores + '&name=' + name + '&fila='+fila;
	var queryString = 'includes/ajaxCambiarProducto.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);
	
}

function enviarProductoReportes(campo, fila, reporte){

	reducirCamposReporte(campo);

	//var id=campo.value;
	//var idName=campo.id;
	//var cantCaracteres = (idName.length)-1;
	//var name=idName.substring(0,cantCaracteres);
	//console.log(name);
	
	var arreglo=campo.id;
	var name="";
	
	for(var x=0;x<arreglo.length;x++){
		
		if(isNaN(arreglo.charAt(x))==true){
			name+=arreglo.charAt(x);
		}
	}
	
	switch(name){
	
	case 'categorias':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idValores="&idCategoria="+idCategoria;
		var idCampo='sistema'+fila;			
	break;

	case 'sistemas':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema;
		var idCampo='subCategoria'+fila
	break;

	case 'subCategorias':
		var idCategoria=document.getElementById('categorias'+fila).value;
		var idSistema=document.getElementById('sistemas'+fila).value;
		var idSubCategoria=document.getElementById('subCategorias'+fila).value;
		var idValores="&idCategoria="+idCategoria+"&idSistema="+idSistema+"&idSubCategoria="+idSubCategoria;
		var idCampo='marca'+fila
	break;

	case 'Marcas':
		return false;
	break;
	
	}
	

	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(idCampo);
			ajaxDisplay.innerHTML =ajaxRequest.responseText;
		}
	}

	var valores = idValores + '&name=' + name + '&fila='+fila + '&reporte='+reporte;
	var queryString = 'includes/ajaxCambiarProductoReporte.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);
	
}

function operacionesReporte(operacion){

var categoria=document.getElementById('categoria').value;
var sistema=document.getElementById('sistema').value;
var subCategoria=document.getElementById('subCategoria').value;
var marca=document.getElementById('marca').value;
var presentacion=document.getElementById('presentacion').value;
var reporte=document.getElementById('reporte').value;
var ciudad=document.getElementById('ciudad').value;
var canal=document.getElementById('canal').value;
var pdv=document.getElementById('pdv').value;
var anioDesde=document.getElementById('anioDesde').value;
var mesDesde=document.getElementById('mesDesde').value;
var anioHasta=document.getElementById('anioHasta').value;
var mesHasta=document.getElementById('mesHasta').value;

var valores='&categoria='+categoria+'&sistema='+sistema+'&subCategoria='+subCategoria+'&marca='+marca+'&presentacion='+presentacion+'&reporte='+reporte+'&ciudad='+ciudad+'&canal='+canal+'&pdv='+pdv+'&anioDesde='+anioDesde+'&mesDesde='+mesDesde+'&anioHasta='+anioHasta+'&mesHasta='+mesHasta+'&operacion='+operacion;


window.open ("modulos/monitoreos/reporteExcel.php?"+valores,"mywindow","status=1");

}

function setFocus(){
var obj=document.getElementById('login');
	if(obj){
	obj.focus();
	}
}

function enviarCsv(){
	
	var extension=($('archivo').value.substring($('archivo').value.lastIndexOf('.'))).toLowerCase();
	
	if($('archivo').value==""){
	alert('No selecciono ningun archivo para cargar');
	return false;
	}
	
	if(extension!='.csv'){
	alert("El archivo que selecciono no es un archivo .csv");
	$('archivo').value="";
	return false;
	}
	
	return true;
		
}

function cargarPresentaciones(){

	var categoria=document.getElementById('categorias0').value;
	var sistema=document.getElementById('sistemas0').value;
	var subCategoria=document.getElementById('subCategorias0').value;

	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById('content').style.display='';
			var ajaxDisplay = document.getElementById('presentaciones');
			ajaxDisplay.innerHTML =ajaxRequest.responseText;
		}
	}

	var valores ='&categoria=' + categoria + '&sistema='+sistema + '&subCategoria='+subCategoria;
	var queryString = 'includes/ajaxCambiarPresentaciones.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);

}

function modificarCamposReporte(campo){

var ancho=0;

var elementWidth = new Element('spam', {id: 'ElementWidth'});

elementWidth.inject($('principal'));

arrayOptions=campo.options;

for(var x=0;x<arrayOptions.length;x++){

	elementWidth.set('text', arrayOptions[x].text);
	if(ancho < $('ElementWidth').getWidth()){
		ancho = $('ElementWidth').getWidth();
	}
	elementWidth.set('text',"");
}

if(Browser.detect().name=="gecko"){

	campo.style.width=(ancho+41)+"px";

}else if(Browser.detect().name=="trident"){

	campo.style.width=(ancho+63)+"px";

}

elementWidth.dispose();

}

function reducirCamposReporte(campo){

	campo.style.width="105px";

}

function logUsuarios(){
if(!$('contenedor')){
	var divContenedor  = new Element('div', {
		'id': 'contenedor',
		'styles': {
			'text-align':'center', 
			'margin':'auto', 
			'width':'800px',
			'height':'400px',
			'overflow-y':'scroll'
		}
	});

	var espacio=new Element('br');
	espacio.inject($(document.body));

	divContenedor.inject($(document.body));

	var divLog  = new Element('div',{
	'id':'log'
	});

	divLog.inject(divContenedor);
}

var usuario=$('usuario').value;
var fecha=$('fecha').value;
$('fecha').value="";

	ajaxRequest = xmlhttp();
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('log');
			ajaxDisplay.innerHTML =ajaxRequest.responseText;
		}
	}

	var valores ='&usuario='+usuario+'&fecha='+fecha;
	var queryString = 'log.php';
	ajaxRequest.open("POST", queryString, true);
	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.send(valores);

}