// JavaScript Document
// GetXmlHttpObject()
function GetXmlHttpObject() {

	var xmlHttp=null;

	try {

		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	
	}
	catch (e) {

		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
		
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Esta aplicación solo funciona con exploradores que soportan el objeto GetXmlHttpObject");
			}
		}
	}

	return xmlHttp;

}

// Popup Window
function genPopup(url, target, width, height, resize) {
	window.open(url, target, 'width='+width+',height='+height+',toolbar=0,location=0,resizable='+resize);
}

// show div
function showLoadingDiv() {
	userHeight	= window.screen.availHeight;
	userWidth	= window.screen.availWidth;
	userMidH	= userHeight / 2;
	userMidW	= userWidth	/ 2;
	posX		= userMidW - 70;
	posY		= userMidH - 90;
	obj	= document.getElementById('loadingDiv');
	obj.style.left		= posX + 'px';
	obj.style.top		= posY + 'px';
	obj.style.display = 'block';
}

function hideLoadingDiv() {
	document.getElementById('loadingDiv').style.display = 'none';
}

// enviar forma
function checkFormContacto() {
	var error	= false;
	var msj		= "Por favor verifica los siguientes datos:\n";
	var params	= "";
	nombre		= document.contacto.nombre.value;
	email		= document.contacto.email.value;
	conf		= document.contacto.confirmacion.value;
	edo_mun		= document.contacto.estado_municipio.value;
	telefono	= document.contacto.telefono.value;
	mensaje		= document.contacto.mensaje.value;

	if(nombre=="") {
		error	= true;
		msj 	= msj + "   - Falta tu nombre\n";
	}
	if(email=="") { 
		error	= true;
		msj 	= msj + "   - Falta tu email\n";
	}
	if(email!=conf) { 
		error	= true;
		msj 	= msj + "   - Tus correos no coinciden\n";
	}
	if(edo_mun=="") { 
		error	= true;
		msj		= msj + "   - Falta tu estado y municipio\n";
	}
	if(telefono=="") { 
		error	= true;
		msj		= msj + "   - Falta tu telefono\n";
	}
	if(mensaje=="") { 
		error	= true;
		msj		= msj + "   - Falta tu mensaje\n";
	}
	if(error) {
		alert(msj);
		return false;
	} else {
		// parametros
		params = "nombre="+nombre+"&email="+email+"&edo_mun="+edo_mun+"&telefono="+telefono+"&mensaje="+mensaje;
		// mostrar loading div
		showLoadingDiv();
		// llamar sendmail.php
		var xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('formularioDiv');
			obj.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET", "sendmail.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	// ocultar loading div
	hideLoadingDiv();
	}
}

// img div
function showDiv() {
	userHeight	= window.screen.availHeight;
	userWidth	= window.screen.availWidth;
	userMidH	= userHeight / 2;
	userMidW	= userWidth	/ 2;
	posX		= userMidW - 400;
	posY		= userMidH - 400;
	obj	= document.getElementById('imgDiv');
	obj.style.left		= posX + 'px';
	obj.style.top		= posY + 'px';
	obj.style.display = 'block';
}

function showDiv2() {
	userHeight	= window.screen.availHeight;
	userWidth	= window.screen.availWidth;
	userMidH	= userHeight / 2;
	userMidW	= userWidth	/ 2;
	posX		= userMidW - 160;
	posY		= userMidH - 220;
	obj	= document.getElementById('imgDiv2');
	obj.style.left		= posX + 'px';
	obj.style.top		= posY + 'px';
	obj.style.display = 'block';
}

function showDivX(div) {
	userHeight	= window.screen.availHeight;
	userWidth	= window.screen.availWidth;
	userMidH	= userHeight / 2;
	userMidW	= userWidth	/ 2;
	posX		= userMidW - 400;
	posY		= userMidH - 400;
	obj	= document.getElementById('imgDiv'+div);
	obj.style.left		= posX + 'px';
	obj.style.top		= posY + 'px';
	obj.style.display = 'block';
}

function swapImage(img, title) {
	obj	= document.getElementById('imgTarget');
	obj.title = title;
	obj.src = 'images/' + img;
}

// verificacion formulario cotizacion
function checkFormCotizacion() {
	var error	= false;
	var msj		= "Por favor verifica los siguientes datos:\n";
	nombre		= document.cotizacion.nombre.value;
	email		= document.cotizacion.email.value;
	edo_mun		= document.cotizacion.estado_municipio.value;

	if(nombre=="") {
		error	= true;
		msj 	= msj + "   - Falta tu nombre\n";
	}
	if(email=="") { 
		error	= true;
		msj 	= msj + "   - Falta tu email\n";
	}
	if(edo_mun=="") { 
		error	= true;
		msj		= msj + "   - Falta tu estado y municipio";
	}
	if(error) {
		alert(msj);
		return false;
	} else {
		return true;
	}
}

//select estado
function selectEstado() {
	
	var estado_id = document.getElementById('estado').value;
	window.location='zonas.php?estado_id='+estado_id;
	
}

//select estado
function selectMunicipio() {
	
	var estado_id = document.getElementById('estado').value;
	var municipio_id = document.getElementById('municipio').value;
	window.location='zonas.php?estado_id='+estado_id+'&municipio_id='+municipio_id;
	
}

//add client - select state
function formSubmitState() {
	var obj;
	obj = document.getElementById('form_save');
	obj.value = 0;
	obj = document.getElementById('form_state');
	obj.value = 1;
	obj = document.getElementById('form_add_client');
	obj.submit();
}

//add client - select city
function formSubmitCity() {
	var obj;
	obj = document.getElementById('form_save');
	obj.value = 0;
	obj = document.getElementById('form_state');
	obj.value = 0;
	obj = document.getElementById('form_add_client');
	obj.submit();
}

//mostrar/ocultar imagen wait
function displayDivWait(div,display) {
	var obj;
	obj = document.getElementById(div);
	if(display==1)
		obj.style.display = 'block';
	else
		obj.style.display = 'none';
}

//cargar combo con todos municipios
function loadDropDownAllCities(value, size) {

	// parametros
	params = "cmd=load_all_cities&estado_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitCity',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownCity');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// quitar dropdown suburbs
	document.getElementById('divDropDownSuburb').innerHTML = '';

	// ocultar loading div
	displayDivWait('divImgWaitCity',0);
	
}

//cargar combo de todas las colonias
function loadDropDownAllSuburbs(value, size) {

	// parametros
	params = "cmd=load_all_suburbs&municipio_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitSuburb',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownSuburb');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// ocultar loading div
	displayDivWait('divImgWaitSuburb',0);

}

//cargar combo de todas las colonias para registro
function loadDropDownAllSuburbsRegistro(value, size) {

	// parametros
	params = "cmd=load_all_suburbs_registro&municipio_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitSuburbRegistro',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownSuburbRegistro');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// ocultar loading div
	displayDivWait('divImgWaitSuburbRegistro',0);

}

//cargar combo municipios con clientes
function loadDropDownCities(value, size) {

	// parametros
	params = "cmd=load_cities&estado_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitCity',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownCity');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// quitar dropdown suburbs
	document.getElementById('divDropDownSuburb').innerHTML = '';

	// ocultar loading div
	displayDivWait('divImgWaitCity',0);
	
}

//cargar combo municipios para registro
function loadDropDownCitiesRegistro(value, size) {

	// parametros
	params = "cmd=load_all_cities_registro&estado_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitCityRegistro',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownCityRegistro');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// quitar dropdown suburbs
	document.getElementById('divDropDownSuburbRegistro').innerHTML = '';

	// ocultar loading div
	displayDivWait('divImgWaitCityRegistro',0);
	
}

//cargar combo colonias con clientes
function loadDropDownColonias(value, size) {

	// parametros
	params = "cmd=load_suburbs&municipio_id="+value+"&size="+size;
	
	// mostrar imagen loading
	displayDivWait('divImgWaitColonia',1);

	// llamar ajaxLoad.php
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		
		if(xmlHttp.readyState==4) {
			obj	= document.getElementById('divDropDownColonia');
			obj.innerHTML = xmlHttp.responseText;
		}
		
	}
	
	xmlHttp.open("GET", "dev.ajax.lib.php?rd="+Math.random() + "&" + params,true);
	xmlHttp.send(null);
	
	// ocultar loading div
	displayDivWait('divImgWaitColonia',0);

}

function checkFormPaginaPrincipalBusqueda() {
	
	if(!document.getElementById('client_suburb') || document.getElementById('client_suburb').value==0 || document.getElementById('client_suburb').value=="") {
		
		alert("Seleccione el municipio antes de realizar su busqueda");
		return false;
		
	}
	
	return true;
	
}

// check delete clientes_editar_catalogo.php
function itemDelete(item_id) {
	
	if(item_id!=0 && confirm('Esta seguro que desea eliminar este producto/servicio?') ) {
		
		window.location = 'micuenta_catalogo.php?cmd=delete_item&item_id='+item_id;
		
	} else {
		
		return false;
		
	}
	
}
