﻿document.write('<script type="text/javascript" src="/vistas/js/prototype.js"></script>');

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/g,"");
}

var windowRef;

//Abre un popup
function MM_openBrWindow ( theURL , winName , features ) { //v2.0
  windowRef = window.open ( theURL , winName , features ) ;
}

//Realiza la validacion del control que se le pasa como parametro en pObj, valida si esta vacio o no y ademas controla que el dato ingresado sea del tipo pTipo, sino devuelve "false" y ademas genera un mensaje de error.
function VerificarForm ( pObj , pControl , pTipo ) {
	pObj = document.getElementById ( pObj ) ;
	if ( (pObj.value.trim() == '' || pObj.value == null || typeof pObj.value == 'undefined') && pTipo != 'validmail' && pTipo != 'validdate') {
		alert(pControl + ' es requerido');
		pObj.focus();
		return false;
	} else {
		//Ahora debo corroborar que el dato sea del tipo que se especifica en el parametro pTipo
		switch ( pTipo ) {
			case "mail":
				//Corroborar que el dato sea una direccion de email valida
				
				if ( pObj.value.indexOf ('@', 0) == -1 || pObj.value.indexOf ('.' , 0) == -1 ) {
					
					alert(pControl + ' no es un e-mail válido');
					
					pObj.focus();
					
					return false ;
		
				} else {
					
					return true ;
				
				}
				
				break;

			case "validmail":
				
				if (pObj.value == '') return true;

				if ( pObj.value.indexOf ('@', 0) == -1 || pObj.value.indexOf ('.' , 0) == -1 ) {
					
					alert(pControl + ' no es un e-mail válido');
					
					pObj.focus();
					
					return false ;
		
				} else {
					
					return true ;
				
				}
				
				break;			
			
			case 'validdate':
				if (pObj.value == '') return true;
			case 'date':
				if (!isDate(pObj.value)) {
					pObj.focus();
					return false;
				}
				return true;
				break;
			
		}
		
		return true;
		
	}
	
}

function CheckDistribucion() {
	var Retorno = true ;
	if (Retorno) {
		Retorno = VerificarForm ( 'nombreyapellidos' , 'Nombre y apellidos' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'empresa' , 'Empresa' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'ciudad' , 'Ciudad' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'provincia' , 'Provincia' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'telefono' , 'Teléfono' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'email' , 'E-mail' , "mail") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'consulta' , 'Consulta' , "txt") ;	
	}
	chkFormaContacto = 0;
	if (Retorno) {
		var el_collection = document.getElementsByName('formacontacto');
		for ( c = 0 ; c < el_collection.length ; c++ ) {
			if (!el_collection[c].checked) {
				chkFormaContacto++;
			}
		}
		if (chkFormaContacto==2) {
			alert("Debe seleccionar al menos una forma de contacto");
			Retorno = false;
		}
	}
	if ( Retorno ) {
		//Si todo esta correcto, devuelvo true
		return true ;
	} else {
		//Si alguno de los controles no esta completo o no tiene los datos correctos, devuelvo false
		return false ;
	}
}

function CheckContacto() {
	var Retorno = true ;
	if (Retorno) {
		Retorno = VerificarForm ( 'nombreyapellidos' , 'Nombre y apellidos' , "txt") ;	
	}
	if (Retorno) {
		var el_collection = document.getElementsByName('tipo[]') ;
		if (el_collection[0].checked) {
			Retorno = VerificarForm('empresa','Empresa',"txt");
		}
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'telefono' , 'Teléfono' , "txt") ;	
	}
	
	if (Retorno) {
		Retorno = VerificarForm ( 'cp' , 'Código Postal' , "txt") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'email' , 'E-mail' , "mail") ;	
	}
	if (Retorno) {
		Retorno = VerificarForm ( 'asunto' , 'Asunto' , "txt") ;	
	}
	
	if (Retorno) {
		Retorno = VerificarForm ( 'categorias' , 'Categorías' , "txt") ;	
	}
	
	if (Retorno) {
		Retorno = VerificarForm ( 'consulta' , 'Comentarios' , "txt") ;	
	}
	
	if ( Retorno ) {
		return true ;
	} else {
		return false ;
	}
}

function ContactoControlTipo(value) {
	if (value=='empresa') {
		document.getElementById('empresa').disabled = false;
	} else {
		document.getElementById('empresa').value = '';
		document.getElementById('empresa').disabled = true;
		
	}
}
function MensajeDistEnviado() {
	alert("El mensaje se ha enviado correctamente. Pronto nos comunicaremos con usted.");
	document.location.href = "/distribucion.php";
}

function MensajeDistNoEnviado() {
	alert("El mensaje no se ha enviado. Por favor vuelva a intentar.");
	document.location.href = "/distribucion.php";
}

function VerSubcategorias(idcatsec) {
	if (document.getElementById('tablacat'+idcatsec).style.display=='') {
		document.getElementById('tablacat'+idcatsec).style.display = 'none';
	} else {
		document.getElementById('tablacat'+idcatsec).style.display = '';
	}
}

var nav4 = window.Event ? true : false;
function solonumeros ( evt ) { 
	var key = nav4 ? evt.which : evt.keyCode;
	
	return (key <= 13 || (key >= 48 && key <= 57));

}