/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) Diciembre 2007, Benjamín Beviá - benjaminbevia@gmail.com  - http://benjamin.bevia.net //
// DISEÑO WEB PROFESIONAL ALICANTE - FOBOSTEC ::: DISEÑO WEB - Diseño web - Diseño web profesional     //
//                                                                                                     //
// Copyrigh ©2007 Benjamín Beviá Isidro                                                                //
// Prohibida la reproducción total o parcial del código, incluido su tratamiento informático,          //
// transmisión o utilización sin el previo permiso y por escrito del titular del copyright             //
/////////////////////////////////////////////////////////////////////////////////////////////////////////

function popup(theURL,winName,features) { 
  var w = window.open(theURL,winName,features);
  w.focus();
};

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
};


function agregar(){
   if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) {
      var url="http://www.tamborino.es"; 
      var titulo="CK PROPERTIES - real estate agents Costa Blanca, Costa del Sol";
      window.external.AddFavorite(url,titulo);
   } else { 
      if(navigator.appName == "Netscape") 
         alert("Presione Crtl+D para agregar este sitio en sus Bookmarks"); 
   }
};


function confirmar_borrado(nombre){
	return confirm('Do you really want to delete the selected PROPERTY: \n'+nombre+' ?.');
};


//pide confirmacion y lanza el borrado de todos los informes de un cliente dado
function borrarTodos(nombre,id){
	if (confirm('Realmente desea borrar todos los documentos \ndel usuario: '+nombre+' ?.'))
	{
		window.location = "http://www.ckproperties.info/admin/documentos_deleteall.php?id_user="+id;
	}
};

//filtra los informes por el cliente seleccionado en el dropdown
function filtrarClientes(){
	id_user = document.users.users_form.options[document.users.users_form.selectedIndex].value;
	window.location = "http://www.ckproperties.info/admin/documentos_admin.php?id_cliente="+id_cliente;
};


//filtra el loscaracteres y deja solo los numeros
//uso: <input name="precio_form" type="text" onkeypress="return numbersonly(event)" />
function numbersonly(e){
	var key;
	var keychar;
	if (window.event)
 		key = window.event.keyCode;
	else if (e)
 		key = e.which;
	else
 		return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
 		(key==9) || (key==13) || (key==27) )
 		return true;
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
 		return false;
};


//Comprueba que un campo de texto WHAT no tenga mas de LIMIT caracteres
//uso: onKeyPress="return maxchars(this, 280)" onBlur="return maxchars(this, 280)"
function maxchars(what,limit){
	if (what.value.length > limit){
		what.value = what.value.substring(0, limit);
	}
};



function comprobarFecha(campo){
	if(campo.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)){
		return true;
	}
	else{
		alert("Fecha no valida!");
		campo.focus();
		return false;
	}
};

function comprobarEmail(campo){
	if(campo.value.match(/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+.[a-z]{2,}$/i)){
		return true;
	}
	else{
		alert("Invalid email format!");
		campo.focus();
		return false;
	}
};

function comprobarWeb(campo){
	if(campo.value.match(/[a-z0-9]+.[a-z]{2,4}$/i)){
		return true;
	}
	else{
		alert("URL no valida!");
		campo.focus();
		return false;
	}
};

function comprobarEditBanners(formulario) {
	if(document.getElementById(formulario).ref_form.value == '')
		return false;
};


function comprobarinsertarcatalogo() {

	//Flag de allOK
	var allOK = true;
	var textError = "";

	if (document.catalogo.newresale_form.value=="") {
		textError = "ERROR: New/Resale option, is is a required field.";
		document.catalogo.newresale_form.focus();
		allOK = false;
	}
	
	if (document.catalogo.cat_form.value=="0") {
		textError += "\nERROR: Property type select box, is is a required field.";
		document.catalogo.cat_form.focus();
		allOK = false;
	}

	if (document.catalogo.ref_form.value=="") {
		textError += "\nERROR: Reference, is is a required field.";
		document.catalogo.ref_form.focus();
		allOK = false;
	}

	if (document.catalogo.nombre_en_form.value=="") {
		textError += "\nERROR: Name english, is is a required field.";
		document.catalogo.nombre_en_form.focus();
		allOK = false;
	}
	
	if (document.catalogo.precio_form.value=="") {
		textError += "\nERROR: Price, is is a required field.";
		document.catalogo.precio_form.focus();
		allOK = false;
	}
	
	if (document.catalogo.status_form.value=="") {
		textError += "\nERROR: Status, is is a required field.";
		document.catalogo.status_form.focus();
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};




function comprobarFormUsers(formulario) {

	//Flag de allOK
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);

	if (form.nombre_form.value=="") {
		textError += "\nERROR: Se requiere un NOMBRE de usuario.";
		form.nombre_form.focus();
		allOK = false;
	}

	if (form.apellidos_form.value=="") {
		textError += "\nERROR: Se requiere unos APELLIDOS de usuario.";
		form.direccion_form.focus();
		allOK = false;
	}
	
	if (!form.email_form.value.match(/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+.[a-z]{2,}$/i)) {
		textError += "\nERROR: Email no valido!.";
		form.email_form.focus();
		allOK = false;
	}

	if (form.user_form.value=="") {
		textError += "\nERROR: Se requiere un NOMBRE DE USUARIO.";
		form.user_form.focus();
		allOK = false;
	}

	if (form.password_form.value=="") {
		textError += "\nERROR: Se requiere una CONTRASEÑA de usuario.";
		form.password_form.focus();
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};


function comprobarFormDocumentos(formulario) {

	//Flag de allOK
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);

	if (!form.fecha_form.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)) {
		textError += "\nERROR: FECHA no valida!.";
		form.fecha_form.focus();
		allOK = false;
	}

	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un TITULO de documento.";
		form.titulo_form.focus();
		allOK = false;
	}

	if (form.documento_form.value=="") {
		textError += "\nERROR: Se requiere un Documento (PDF).";
		form.documento_form.focus();
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};


function comprobarFormUpdateDocumentos(formulario) {

	//Flag de allOK
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);

	if (!form.fecha_form.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)) {
		textError += "\nERROR: FECHA no valida!.";
		form.fecha_form.focus();
		allOK = false;
	}

	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un TITULO de documento.";
		form.titulo_form.focus();
		allOK = false;
	}

	if ((form.update_documento.checked) && (form.documento_form.value=="")) {
		textError += "\nERROR: Se requiere un Documento (PDF).";
		form.documento_form.focus();
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};


function comprobarFormNoticias(formulario) {

	//Flag de allOK
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);

	if (form.fecha_form.value=="") {
		textError += "\nERROR: Se requiere una fecha.";
		form.fecha_form.focus();
		allOK = false;
	}

	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un titulo de noticia.";
		form.titulo_form.focus();
		allOK = false;
	}

	if (form.texto_form.value=="") {
		textError += "\nERROR: Se requiere un texto de noticia.";
		form.texto_form.focus();
		allOK = false;
	}

	if (!comprobarFecha(form.fecha_form)) {
		textError += "\nERROR: Fecha Invalida!.";
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};

function comprobarFormClientes(formulario) {

	//Flag de allOK
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);

	if (form.nombre_form.value=="") {
		textError += "\nERROR: Se requiere un nombre de empresa.";
		form.nombre_form.focus();
		allOK = false;
	}

	if (form.user_form.value=="") {
		textError += "\nERROR: Se requiere un nombre de usuario.";
		form.user_form.focus();
		allOK = false;
	}

	if (form.pass_form.value=="") {
		textError += "\nERROR: Se requiere un password.";
		form.pass_form.focus();
		allOK = false;
	}

	if (!allOK)
		alert(textError);

	return allOK;
};


function comprobarcontactar() {

	//Flag de allOK
	var allOK = true;
	var textError = "";

	if (document.contactar.nombre.value=="") {
		textError = "ERROR: Name is a required field.";
		document.contactar.nombre.focus();
		allOK = false;
	}

	if (document.contactar.telcontacto.value=="") {
		textError += "\nERROR: Phone is a required field.";
		document.contactar.telcontacto.focus();
		allOK = false;
	}
	
	if (document.contactar.acepto.checked == false) {
		textError += "\nERROR: I have read the Privacy policy is a required field.";
		document.contactar.acepto.focus();
		allOK = false;
	}

	if (document.contactar.email.value=="") {
		textError += "\nERROR: email is a required field.";
		document.contactar.email.focus();
		allOK = false;
	}
	
		if (!comprobarEmail(document.contactar.email)) {
		
		
		allOK = false;
		}
	
	if (!allOK)
		alert(textError);

	return allOK;
};

function comprobarcomprar() {
	//Flag de allOK
	var allOK = true;
	var textError = "";
	if (document.comprar.nombre.value=="") {
		textError = "ERROR: Se requiere un Nombre.";
		document.comprar.nombre.focus();
		allOK = false;
	}
	if (document.comprar.telcontacto.value=="") {
		textError += "\nERROR: Se requiere un Teléfono de contacto.";
		document.comprar.telcontacto.focus();
		allOK = false;
	}
	if (document.comprar.acepto.checked == false) {
		textError += "\nERROR: Ha de aceptar haber leído la política de privacidad.";
		document.comprar.acepto.focus();
		allOK = false;
	}
	if (document.comprar.email.value=="") {
		textError += "\nERROR: Se requiere un e-mail.";
		document.comprar.email.focus();
		allOK = false;
	}
		if (!comprobarEmail(document.comprar.email)) {
		allOK = false;
		}
	if (!allOK)
		alert(textError);
	return allOK;
};


function desactivador(origen, valor, objetivo){
	if(origen.value == valor)
		objetivo.disabled = false
	else
		objetivo.disabled = true;
};

function terrenos(origen){
	desactivador(origen, "1", document.fincas.texto1_form);
	desactivador(origen, "1", document.fincas.texto2_form);
};
