/**
 * @author facha
 */
function nuevoAjax(){
	var xmlhttp=false;
 	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 displayLoader(pFlag){
		if(pFlag){
			document.getElementById('loading').style.display="block";
		}else{
			document.getElementById('loading').style.display="none";
		}
	}


function habilitarUsuario(pURL, pId){
	displayLoader(true);//show loader
	var chk;
	chk = document.getElementById(pId).checked;
	if(chk){
		chk = "on";
	}else{
		chk = "off";
	}
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"habilitar_usuario.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);//hide loader
			//document.getElementById('msg_area').innerHTML = ajax.responseText;
			window.location.href = pURL+'index.php?S=clientes&M='+ajax.responseText;
			//document.getElementById('comentarios').value = "";
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+pId+"&habilitado="+chk);
}


function recibirNews(pURL, pId, pUser){// habilita/deshabilita a usuario a recibir newsletters

	displayLoader(true);
	var chk;
	chk = document.getElementById(pId).checked;
	if(chk){
		chk = "on";
	}else{
		chk = "off";
	}
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"recibir_news.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);
			//document.getElementById('msg_area').innerHTML = ajax.responseText;
			window.location.href = pURL+'index.php?S=clientes&M='+ajax.responseText;
			
			//document.getElementById('comentarios').value = "";
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+pUser+"&recibir="+chk);
	
}

function enviarNewsletter(pURL, pId, pSecc){
	displayLoader(true);
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"transact_newsletter.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);
			//document.getElementById('msg_area').innerHTML = ajax.responseText;
			window.location.href = pURL+'index.php?S=newsletters&M='+ajax.responseText;
	
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+pId+"&S="+pSecc);
	
}


function habilitarTodosLosUsuarios(pURL, pId, pFlag){// habilita/deshabilita a TODOS los usuarios para envio de newsletter 

	displayLoader(true);
	var chk;
	if(pFlag){
		chk = 'on';
	}else{
		chk = 'off';
	}
	//chk = document.getElementById(pId).checked;
	
	/*if(chk){
		chk = "on";
	}else{
		chk = "off";
	}*/
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"habilitar_todos.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//document.getElementById('msg_area').innerHTML = ajax.responseText;
			displayLoader(false);
			window.location.href = pURL+'index.php?S=clientes&M='+ajax.responseText;
	
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("habilitar="+chk);
	
}

function habilitarUsuariosPorPagina(pURL, pArr, pFlag, pPag, pSec, pSearch){
	
	displayLoader(true);
	var chk;
	if(pFlag){
		chk = 'on';
	}else{
		chk = 'off';
	}
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"habilitar_por_pagina.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//document.getElementById('msg_area').innerHTML = ajax.responseText;
			displayLoader(false);
			if(pSec == "clientes"){
				window.location.href = pURL+'index.php?S=clientes&_pagi_pg='+pPag+'&M='+ajax.responseText;	
			}else if(pSec == "search"){
				window.location.href = pURL+'index.php?S=search&txt_buscar='+pSearch+'&_pagi_pg='+pPag+'&M='+ajax.responseText;
			}
			
	
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("habilitar="+chk+"&arv="+pArr+"&_pagi_pg="+pPag);
	
}


function actualizarSubcategorias(pURL,pForm,pFlag,pFlagF){
	displayLoader(true);//show loader
	var cat_id = null;
	switch(pForm){
		case 'familia':
				cat_id = document.familia.categoria.options[document.familia.categoria.selectedIndex].value;
			break;
		case 'subfamilia':
		
				cat_id = document.subfamilia.categoria.options[document.subfamilia.categoria.selectedIndex].value;
				
			break;
			
		case 'productos':
				cat_id = document.productos.categoria.options[document.productos.categoria.selectedIndex].value;
		break;
			
			
	}
	
	
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"subcat_combo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);//hide loader
			
			var lines = '<label for="subcategorias">Sub - Categor&iacute;a:</label>';
			lines += '<select id="subcategorias" name="subcategorias"';
			if(pFlag){//if adding a new subfamily
				lines += ' onchange="actualizarFamilias(\''+pURL+'\',\''+pForm+'\','+pFlagF+')"';
			}
			lines += '>';
			lines += '<option value="-1">Seleccione una Sub-categor&iacute;a</option>';
			
			lines += ajax.responseText;
			
			//lines += '</select>&nbsp;<span class="required">*</span>';
			
			document.getElementById('subcategory_container').innerHTML = lines;
			
			actualizarFamilias(pURL,pForm,pFlagF);
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("cat_id="+cat_id);
	
}

function actualizarFamilias(pURL,pForm,pFlag){
	displayLoader(true);//show loader
	var subcat_id = null;
	switch(pForm){
		case 'familia':
				subcat_id = document.familia.subcategorias.options[document.familia.subcategorias.selectedIndex].value;
			break;
		case 'subfamilia':
				//alert( document.subfamilia.subcategorias.options[document.subfamilia.subcategorias.selectedIndex].value);
				subcat_id = document.subfamilia.subcategorias.options[document.subfamilia.subcategorias.selectedIndex].value;
			break;
			
		case 'productos':
				subcat_id = document.productos.subcategorias.options[document.productos.subcategorias.selectedIndex].value;
		break;
	}
	
	
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"fam_combo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);//hide loader
			
			var lines = '<label for="family">Familia:</label>';
			
			lines += '<select id="family" name="family"';
			if(pFlag){
				lines += ' onchange="actualizarSubFamilias(\''+pURL+'\',\''+pForm+'\')"';
			}
			lines += '>';
			
			lines += '<option value="-1">Seleccione una Familia</option>';
			
			lines += ajax.responseText;
			
			lines += '</select>&nbsp;<span class="required">*</span>';
			
			document.getElementById('family_container').innerHTML = lines;
			
			actualizarSubFamilias(pURL,pForm);
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("subcat_id="+subcat_id);
}

function actualizarSubFamilias(pURL,pForm){
	displayLoader(true);//show loader
	var fam_id = null;
	switch(pForm){
		case 'familia':
				fam_id = document.familia.subcategorias.options[document.familia.subcategorias.selectedIndex].value;
			break;
		case 'subfamilia':
				//alert( document.subfamilia.subcategorias.options[document.subfamilia.subcategorias.selectedIndex].value);
				fam_id = document.subfamilia.subcategorias.options[document.subfamilia.subcategorias.selectedIndex].value;
			break;
			
		case 'productos':
			fam_id = document.productos.family.options[document.productos.family.selectedIndex].value;
		break;
	}
	
	
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"subfam_combo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);//hide loader
			
			var lines = '<label for="subfamily">Sub-Familia:</label>';
			lines += '<select id="subfamily" name="subfamily">';
			
			lines += '<option value="-1">Seleccione una Sub-Familia</option>';
			
			lines += ajax.responseText;
			
			lines += '</select>&nbsp;<span class="required">*</span>';
			
			document.getElementById('subfamily_container').innerHTML = lines;
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("fam_id="+fam_id);
}


function actualizarLocalidades(pURL, pForm){
	
	displayLoader(true);//show loader
	var dep_id = null;
	dep_id = document.agregar.departamento.options[document.agregar.departamento.selectedIndex].value;
	
	ajax=nuevoAjax();
	ajax.open("POST", pURL+"locality_combo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			displayLoader(false);//hide loader
			
			var lines = '<label for="localidad">Localidad:</label>';
			lines += '<select id="localidad" name="localidad"';
			
			lines += '>';
			lines += '<option value="-1">Seleccione una Localidad</option>';
			
			lines += ajax.responseText;
			
			lines += '</select>&nbsp;<span class="required">*</span>';
			
			document.getElementById('localidad_container').innerHTML = lines;
			
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("dep_id="+dep_id);
	
}



