// JavaScript Document

//función de Ajax
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;
}
//función de Ajax
function cargarContenido(liga){
var preloader;
//alert ("Si entra");
//contenedor = document.getElementById('contenedor');
preloader = document.getElementById('artic');
//t1 = document.getElementById('texto1').value;
//t2 = document.getElementById('texto2').value;
ajax=nuevoAjax();
ajax.open("GET",liga,true);
//alert("Si entra 2");
ajax.onreadystatechange=function() {
//alert("Si entra 3");
if(ajax.readyState==1){
	//preloader.innerHTML = "Cargando...";
//	//modificamos el estilo de la div, mostrando una imagen de	fondo
//	preloader.style.background = "url('loading.gif') norepeat";
	}else if(ajax.readyState==4){
	if(ajax.status==200){
	//mostramos los datos dentro de la div
	preloader.innerHTML = ajax.responseText;
	//preloader.innerHTML = "Si entra";
	}else if(ajax.status==404){
	preloader.innerHTML = "La página no existe";
	}else{
	//mostramos el posible error
	preloader.innerHTML = "Error: ".ajax.status;
	}
	}
}
ajax.send(null)
}

//cargar imagenes en banco de imagenes
function cargarPag(liga){
var preloader;
//alert ("Si entra");
//contenedor = document.getElementById('contenedor');
preloader = document.getElementById('imagenes');
//t1 = document.getElementById('texto1').value;
//t2 = document.getElementById('texto2').value;
ajax=nuevoAjax();
ajax.open("GET",liga,true);
//alert("Si entra 2");
ajax.onreadystatechange=function() {
//alert("Si entra 3");
if(ajax.readyState==1){
	//preloader.innerHTML = "Cargando...";
//	//modificamos el estilo de la div, mostrando una imagen de	fondo
//	preloader.style.background = "url('loading.gif') norepeat";
	}else if(ajax.readyState==4){
	if(ajax.status==200){
	//mostramos los datos dentro de la div
	preloader.innerHTML = ajax.responseText;
	//preloader.innerHTML = "Si entra";
	}else if(ajax.status==404){
	preloader.innerHTML = "La página no existe";
	}else{
	//mostramos el posible error
	preloader.innerHTML = "Error: ".ajax.status;
	}
	}
}
ajax.send(null)
}

//verifica los campos en los formularios de contacto
function verificar(){

	var nombre = document.formulario.Nombre.value;
	var mail = document.formulario.email.value;
	var msg = document.formulario.Mensaje.value;
	var edo = document.formulario.lstEstado.value;
	var ciu = document.formulario.lstMunicipio.value;
	var dir = document.formulario.Direccion.value;
	var tel = document.formulario.Telefono.value;
	var fecha = document.formulario.Fecha.value;
	var empresa = document.formulario.Empresa.value;
	var diremp = document.formulario.Diremp.value;
	var pais = document.formulario.Pais.value;
	
	//alert(cono);
	if (nombre == "" ||  mail == "" || msg == "" || edo == "" || empresa == "" || ciu == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
			var x = document.formulario.lstEstado.options[document.formulario.lstEstado.selectedIndex].id;
			var y = document.formulario.lstMunicipio.options[document.formulario.lstMunicipio.selectedIndex].id;
			document.formulario.estado.value = x;
			document.formulario.ciudad.value = y;
			var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		 	var regex = new RegExp(emailReg);
		 	if (regex.test(mail)){
				document.formulario.action = "enviar.php";
			 	document.formulario.submit();
			 }
			else{
			 	alert("FAVOR DE INTRODUCIR UNA DIRECCION E-MAIL VALIDA");
			 }
				
	}	

}

//verifica los campos en el formulario de solicitud de propuesta de proyecto
function verificarpresupuesto(){

	var nombre = document.formulario.Nombre.value;
	var mail = document.formulario.email.value;
	var msg = document.formulario.Mensaje.value;
	var edo = document.formulario.lstEstado.value;
	var ciu = document.formulario.lstMunicipio.value;
	var dir = document.formulario.Direccion.value;
	var tel = document.formulario.Telefono.value;
	var fecha = document.formulario.Fecha.value;
	var empresa = document.formulario.Empresa.value;
	var diremp = document.formulario.Diremp.value;
	var pais = document.formulario.Pais.value;
	
	//alert(cono);
	if (nombre == "" ||  mail == "" || msg == "" || edo == "" || empresa == "" || ciu == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
			var x = document.formulario.lstEstado.options[document.formulario.lstEstado.selectedIndex].id;
			var y = document.formulario.lstMunicipio.options[document.formulario.lstMunicipio.selectedIndex].id;
			document.formulario.estado.value = x;
			document.formulario.ciudad.value = y;
			var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		 	var regex = new RegExp(emailReg);
		 	if (regex.test(mail)){
				document.formulario.action = "enviar2.php";
			 	document.formulario.submit();
			 }
			else{
			 	alert("FAVOR DE INTRODUCIR UNA DIRECCION E-MAIL VALIDA");
			 }
				
	}	

}

//las siguientes 4 funciones sirven para las imágenes dinámicas 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function verificar_encuesta(){  //verifica los campos en la encuesta

	var pregunta = document.form2.pregunta.value;
	var opcion1 = document.form2.opcion1.value;
	var opcion2 = document.form2.opcion2.value;

	if (pregunta == "" ||  opcion1 == "" || opcion2 == ""){
		alert ("Debe haber al menos 2 opciones de respuestas");
	}
	else{
				document.form2.submit();
			}	

}


function valida_producto(){  //verifica los campos en subir productos

	var modelo = document.form1.modelo.value;
	var precio = document.form1.precio.value;
			
	if (modelo == "" ||  precio == ""){ 
		alert ("Los campos de Modelo y Precio son obligatorios");
	}
	else{
			var number = isNaN(precio);
			if(number == false){
				document.form1.submit();
			}
			else{
				alert("El precio debe ser un número");	
			}
	}	

}


function showhide(elem1,elem2,state1,state2) //muestra y esconde los campos de datos de facturacion
	
	{ 
	var n = document.form1.mismo.value;
	var n = n * -1;
	document.form1.mismo.value = n;
	document.getElementById(elem1).style.display=state1; 
	
	document.getElementById(elem2).style.display=state2; 
	
	}
	
function comprueba_correo(){
	 var correo = document.form1.correonuevo.value;
	// alert(correo);
	 if(correo != ""){
		 var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		 var regex = new RegExp(emailReg);
		 if (regex.test(correo)){
			 document.form1.submit();
			 }
		 else{
			 alert("Favor de introducir un correo válido");
			 }
	 }
	}
	
function nuevo_cliente(){
	var pass = document.form1.pass.value;
	var nom1 = document.form1.nombre1.value;
	var ape1 = document.form1.apellido1.value;
	var dir1 = document.form1.direccion1.value;
	var ciudad1 = document.form1.ciudad1.value;
	var edo1 = document.form1.estado1.value;
	var cp1 = document.form1.cp1.value;
	var tel1 = document.form1.telefono1.value;
	
	var nom2 = document.form1.nombre2.value;
	var ape2 = document.form1.apellido2.value;
	var dir2 = document.form1.direccion2.value;
	var ciudad2 = document.form1.ciudad2.value;
	var edo2 = document.form1.estado2.value;
	var cp2 = document.form1.cp2.value;
	var tel2 = document.form1.telefono2.value;
	
	var s = document.form1.mismo.value;
	//alert(s);
	if( pass == "" || nom1 == "" || ape1 == "" || dir1 == "" || ciudad1 == "" || edo1 == "" || tel1 == "" || cp1 == ""){
	 alert("Todos los campos son obligatorios, favor de llenarlos");
	}
	else{
		 if(s == "1"){
			if( nom2 == "" || ape2 == "" || dir2 == "" || ciudad2 == "" || edo2 == "" || tel2 == "" || cp2 == ""){
				alert("Todos los campos son obligatorios, favor de llenarlos");
				}
				else{
					document.form1.submit();
					}
	 	 }
		 else{
			 document.form1.submit();
			 }
		}
}

function actualiza_cliente(){
		
	var nom2 = document.form11.nombre2.value;
	var ape2 = document.form11.apellido2.value;
	var dir2 = document.form11.direccion2.value;
	var ciudad2 = document.form11.ciudad2.value;
	var edo2 = document.form11.estado2.value;
	var cp2 = document.form11.cp2.value;
	var tel2 = document.form11.telefono2.value;
		
	//alert(s);
	if( nom2 == "" || ape2 == "" || dir2 == "" || ciudad2 == "" || edo2 == "" || tel2 == "" || cp2 == ""){
		alert("Todos los campos son obligatorios, favor de llenarlos");
	}
	else{
		document.form11.submit();
	}
	 	
}

function comprueba_usuario(){
		var usr = document.form2.correo.value;
		var pass = document.form2.pass.value;
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex = new RegExp(emailReg);
		
		if(usr == "" || pass == ""){
			alert("Por favor introduzca su correo y contraseña");
			}
		else{
			 if (regex.test(usr)){
				 	document.form2.submit();
				 }
				 else{
					alert("Favor de introducir un correo válido"); 
				 }
			}
	}

function comprar(){
	document.f1.submit();
	}
	
function verifica_terminos(){
	var acepta = document.form11.acepto.checked;
	//alert(acepta);
	if(acepta == true){
		document.form11.submit();	
	}
	else{
		alert("DEBE ACEPTAR LOS TERMINOS Y CONDICIONES PARA CONTINUAR CON SU COMPRA");
		}
	}
	
function verifica_terminos_ing(){
	var acepta = document.form11.acepto.checked;
	//alert(acepta);
	if(acepta == true){
		document.form11.submit();	
	}
	else{
		alert("YOU MUST AGREE WITH THE PRIVACY POLICY IN ORDER TO CONTINUE WITH YOUR PURCHASE");
		}
	}
	
function verifica_login(){
	var usr = document.form2.usuario.value;
	var pss = document.form2.password.value;
	
	if(usr == "" || pss == ""){
		alert("NO DEJE NINGUN CAMPO VACIO");
		}
	else{
		document.form2.submit();
		}
	}
	
function submenu(llamador)
{
  var valor = llamador.parentNode.getElementsByTagName('ul')[0];
  valor.style.display = valor.style.display == 'block' ? 'none' : 'block' ;
}

function showhide2(elem1,elem2,state1,state2)
{ 

	if(document.getElementById(elem1).style.display =='block'){
		document.getElementById(elem1).style.display='none'; 
	}
	else{
		document.getElementById(elem1).style.display='block'; 
	}
	
	if(document.getElementById(elem2).style.display == 'block'){ 
		document.getElementById(elem2).style.display='none'; 
	}
	else{
		document.getElementById(elem2).style.display='block'; 
		}
}
function showhide3(elem1,elem2)
{ 
document.getElementById(elem1).style.display='none'; 
document.getElementById(elem2).style.display='block'; 
}

function validaentrada(){
	var us = document.form1.user.value;
	var ps = document.form1.pass.value;
	var bus = document.form1.busqueda.value;
	var desde = document.form1.desde.value;
	if(us == "" || ps == ""){
		if(bus == ""){
			alert("NO DEJES NINGUN CAMPO VACIO");
			return false;
		}
		else{
			if(comprobar(desde,'ing.')){
				document.form1.action = "buscaring.php";		 
			}
			else{
				document.form1.action = "buscar.php";
			}			
			return true;
		}
	}
	else{
		document.form1.action = "control.php";
		return true;
		//document.form1.submit();
		}
	}


function buscar(){
	var bus = document.form2.busqueda.value;
	if(bus == ""){
		alert("Por favor escriba lo que desea buscar");	
		return false;
	}
	else{
		document.form2.action = "buscar.php";
		//document.form2.submit();
		return true;
		//alert("BUSCAR");
	}
}

function valida_articulo(){ //valida la seccion de subir archivos, pagina sube_archivos.php
	var nom = document.form2.nombre.value;
	var aut = document.form2.autor.value;
	var art = document.form2.userfile.value;
	var des = document.form2.descripcion.value;
	var precio = document.form2.precio.value;

	if(nom == "" || aut == "" || art == "" || des == ""){
		alert("Por favor no deje ningún campo vacío");
	}
	else{
		document.form2.submit();	
	}
   
}

function modal(enlace){ //popup para la descarga de archivos pagina archivos.php
	
//var setupWindow = 'center:yes;resizable:no;status:no;dialogWidth:500px;dialogHeight:280px;';
//var setupWindow = 'resizable:no;status:no;dialogWidth:500px;dialogHeight:280px;';
var en = "downd.php?num=" + enlace;
//var en = "localhost/password/password/" + enlace;
//alert(en);
//window.showModalDialog(en,"",setupWindow);
//var v1 = window.open(en,"","width=500px height=280px resizable=0 status=0 location=0 statusbar=0 scrollbars=0");
var v1 = window.open(en,"","");
//var v1 = window.open(en,"","toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=70,height=50,left = 390,top = 260'");
//document.form2.js.value = enlace;
//alert(document.form2.js.value);

}


function vacio(q) { //la variable q contiene el valor del texbox
for ( i = 0; i < q.length; i++ ) { //la funcion q.length devuelve el tamaño de la palabra contenia por el textbox
	if ( q.charAt(i) != " " ) {//la funcion q.charAt nos deuelve el caracter contenido en la posicion i de la variable q
		return true
	}
}
	return false;
}


function verifica_registro(){//verifica el registro de clientes en la pagina registro.php
	var nombre = document.formulario.nombre1.value;
	var mail = document.formulario.mail.value;
	var ape = document.formulario.apellido1.value;
	var edo = document.formulario.lstEstado.value;
	var ciu = document.formulario.lstMunicipio.value;
	var dir = document.formulario.direccion1.value;
	var tel = document.formulario.telefono1.value;
	var cp = document.formulario.cp1.value;
	var con = document.formulario.pass1.value;
	//alert(cono);
	//alert(vacio(nombre));
	if (nombre == "" ||  mail == "" || ape == "" || edo == "" || cp == "" || dir == "" || tel == "" || con == "" || vacio(nombre) == false ||  vacio(mail) == false || vacio(ape) == false || vacio(edo) == false || vacio(cp) == false || vacio(dir) == false || vacio(tel) == false || vacio(con) == false){
		alert ("Favor de llenar todos los campos");
		return false;
	}
	else{
			var x = document.formulario.lstEstado.options[document.formulario.lstEstado.selectedIndex].id;
			if (edo == '33'){
				var y = "_";
			}
			else{
				var y = document.formulario.lstMunicipio.options[document.formulario.lstMunicipio.selectedIndex].id;
			}
//			alert(x);
//		    alert(y);
			document.formulario.estado.value = x;
			document.formulario.ciudad.value = y;
			var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		 	var regex = new RegExp(emailReg);
		 	if (regex.test(mail)){
				document.formulario.action = "reg.php";
			 	//document.formulario.submit();
				return true;
			 }
			else{
			 	alert("FAVOR DE INTRODUCIR UNA DIRECCION E-MAIL VALIDA");
				return false;
			 }	
	}	
}

function verifica_registro2(){//verifica el registro de clientes en la pagina registro2.php
	
	
	var edo = document.formulario.lstEstado.value;
	var ciu = document.formulario.lstMunicipio.value;
	var dir = document.formulario.direccion1.value;
	var tel = document.formulario.telefono1.value;
	var cp = document.formulario.cp1.value;
	var con = document.formulario.pass1.value;
	//alert(cono);
	if (edo == "" || cp == "" || dir == "" || tel == "" || con == "" || vacio(edo) == false || vacio(cp) == false || vacio(dir) == false || vacio(tel) == false || vacio(con) == false){
		alert ("Favor de llenar todos los campos");
		return false;
	}
	else{
			var x = document.formulario.lstEstado.options[document.formulario.lstEstado.selectedIndex].id;
			if (edo == '33'){
				var y = "_";
			}
			else{
				var y = document.formulario.lstMunicipio.options[document.formulario.lstMunicipio.selectedIndex].id;
			}
//			alert(x);
//		    alert(y);
			document.formulario.estado.value = x;
			document.formulario.ciudad.value = y;
			
				document.formulario.action = "reg2.php";
				return true;
			 	//document.formulario.submit();
			
	}	
}

function cambiacolordiv(did,txt){
	var num = document.form1.num.value;
	num = num * -1;
	//alert(num);
	//var objeto = document.getElementById(did);
	var cad = "texto";
	var cad2 = "div";
	var x=1;
	
	for(x=1; x <= 15; x++){
		cad = "texto" + x;
		cad2 = "div" + x;
		document.getElementById(cad).style.display = 'none';
		var objeto = document.getElementById(cad2);
		objeto.className = "estilo15";
	}
	
	
		var objeto = document.getElementById(did);
		objeto.className = "estilo10";
		document.getElementById(txt).style.display = 'block';
		document.form1.num.value = num;
	
}

function esconde(did,txt){
	var num = document.form1.num.value;
	num = num * -1;
	var objeto = document.getElementById(did);

		objeto.className = "estilo15";
		document.getElementById(txt).style.display = 'none';
		document.form1.num.value = num;
	
}

function verifica_comentario(){//verifica todos los campos al publicar un comentario en blog_articulos.php

	var nombre = document.formulario.name.value;
	var com = document.formulario.comment.value;
	var capt = document.formulario.captcha.value;
	//alert(document.formulario.captchaid.value);
	if (nombre == "" ||  com == "" || capt == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
		document.formulario.submit();
	}	
}

function verifica_post(nombre){//verifica todos los campos al publicar un comentario en blog_articulos.php
//alert(nombre);
	var tit = document.formulario.title.value;
	var art = document.formulario.article.value;
	
	//alert(document.formulario.captchaid.value);
	if (tit == "" ||  art == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
		document.formulario.accion.value = nombre;
		//alert(document.formulario.accion.value);
		document.formulario.submit();
	}	
}

function verifica_podcast(){//verifica todos los campos al publicar un comentario en blog_articulos.php
//alert(nombre);
	var tit = document.form2.pregunta.value;
	var desc = document.form2.descripcion.value;
	
	//alert(document.formulario.captchaid.value);
	if (tit == "" ||  desc == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
		//document.formulario.accion.value = nombre;
		//alert(document.formulario.accion.value);
		//document.getElementById('subiendo').style.display = 'block';
		document.form2.submit();
		document.getElementById('subiendo').style.display = 'block';
	}	
}

function verifica_imagen(){//verifica todos los campos al publicar un comentario en blog_articulos.php
//alert(nombre);
	var tit = document.form2.pregunta.value;
	var precio = document.form2.precio.value;
	
	//alert(document.formulario.captchaid.value);
	if (tit == "" || precio == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
		//document.formulario.accion.value = nombre;
		//alert(document.formulario.accion.value);
		//document.getElementById('subiendo').style.display = 'block';
		//alert("Si entra");
		document.form2.submit();
		document.getElementById('subiendo').style.display = 'block';
	}	
}

function verifica_partner(){//verifica todos los campos al publicar un comentario en blog_articulos.php
//alert(nombre);
	var tit = document.form2.pregunta.value;
	var precio = document.form2.precio.value;
	var img = document.form2.userfile.value;
	//alert(img);
	//alert(document.formulario.captchaid.value);
	if (tit == "" || precio == "" || img == ""){
		alert ("Favor de llenar todos los campos");
	}
	else{
		//document.formulario.accion.value = nombre;
		//alert(document.formulario.accion.value);
		//document.getElementById('subiendo').style.display = 'block';
		//alert("Si entra");
		document.form2.submit();
		document.getElementById('subiendo').style.display = 'block';
	}	
}

function audio(ar){
	prel = document.getElementById('rep');
	prel.innerHTML = "<p><object type='application/x-shockwave-flash' data='podcast/player.swf' width='200' height='20'><param name='movie' value='podcast/player.swf' /><param name='FlashVars' value='son=" + ar + "' /></object></p><a href='"+ ar +"' title='Bajar Audio' class='estilo10 style29'>Bajar Audio</a>";
}

function IsNumeric(expression)   
{   
    return (String(expression).search(/^\d+$/) != -1);   
} 

function IsDecimal(expression)   
{   
    return (String(expression).search(/^\d+(\.\d+)?$/) != -1);   
} 

function verifica_pago(){
	var usr = document.form2.concepto.value;
	var pss = document.form2.precio.value;
	
	if(usr == "" || pss == ""){
		alert("Los campos de concepto y cantidad son obligatorios");
		return false;
		}
	else{
		pss = pss.replace(/,/,"");
		pss = pss.replace(/$/,"");
		if(IsDecimal(pss)){
			//alert("Si es número");
			document.form2.precio.value = pss;
			return true;
			}
		else{
			alert("El campo de cantidad no es número, por favor escriba una cantidad correcta");
			return false;
			}
		//document.form2.submit();
		}
	}
	
function muestradetalle(elem1){
	
	if(document.getElementById(elem1).style.display =='block'){
		document.getElementById(elem1).style.display='none'; 
	}
	else{
		document.getElementById(elem1).style.display='block'; 
	}
	
}

function verificapedido(){
	var usr = document.form2.txtDia.value;
	//alert(usr);
	if(usr == ""){
		alert("Por favor especifique una fecha");		
	}
	else{				
		document.form2.submit();
	}
}

function comprobar(mivalor,palabra)
{
	var mivalor = mivalor;
	var palabra = palabra;
	if(mivalor.indexOf(palabra)!=-1){
		return true;
		//alert('La frase\n'+ mivalor +' \nContiene la palabra\n'+palabra);
	}else{
		return false;
		//alert('La frase\n'+ mivalor +' \nNO Contiene la palabra\n'+palabra);
	}
}

