var mMessage = "Este Campo es Obligatorio rellenarlo."
var mPrecio = "La cifra no es correcta, por favor introduzca una cifra válida. Ej. 80,45"
var mCat = "Debe seleccionar la Familia a la que pertenece el Producto."
var mCantidad = "La Cantidad no es correcta.\nIntroduzca un Número Entero Positivo como Cantidad.\nEjemplo: 2"
var defaultEmptyOK = false

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function statBar (s)

{   window.status = s
}

function warnEmpty (theField)
{   theField.focus()
    alert(mMessage)
    statBar(mMessage)
    return false
}

function warnPrecio (theField)
{ 	theField.focus()
    alert(mPrecio)
    statBar(mPrecio)
    return false
}

function warnCantidad (theField)
{ 	theField.focus()
    alert(mCantidad)
    statBar(mCantidad)
    return false
}

function warnCategoria (theField)
{ 	theField.focus()
    alert(mCat)
    statBar(mCat)
    return false
}	

function mensaje(msg) {
	window.status =  ""+ msg +""
}

function isDigit (c){  
	return ((c >= "0") && (c <= "9"))
}

function isNumber (s) /*Numeros Decimales sin Signo Ej. 35,56 */

{   var i;
    var dotAppeared;
    dotAppeared = false;
    if (isEmpty(s)) 
       if (isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isNumber.arguments[1] == true);  
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if( i != 0 ) {
            if ( (c == ".")||(c == ",") ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else     
                if (!isDigit(c)) return false;
        } else { 
            if ( (c == ".")||(c == ",") ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } 
        }
    }

    return true;

}

function EsNumeroPositivo(Dato,Campo) /*Numeros enteros sin Signo Ej. 35 */
{
  var CadenaNumeros="0123456789";
  var EsteCaracter;
  var Contador=0;
  
  if (isEmpty(Dato)==false) 
  {  
  
	  for (i=0;i<Dato.length;i++)
		{
		  EsteCaracter=Dato.substring(i,i+1);
		  if (CadenaNumeros.indexOf(EsteCaracter)!=-1)
				Contador++;
		}
	  if (Contador==Dato.length)
		{
			return(true);
		}
	  else
		{
		  /*alert("Introduzca un Número Entero Positivo como Cantidad.");
		  Campo.focus();*/
		  return(false);
		}
  }
  else
  {
  	return(false);
  }
}

function isNumero (s) /* Numeros Enteros sin signo Ej. 23 */
{   var i;
    if (isEmpty(s)) 
       if (isNumero.arguments.length == 1) return defaultEmptyOK;
       else return (isNumero.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c)) return false;
        } else { 
            if (!isDigit(c)) return false;
        }
    }
    return true;
}

function VerificarTodo()  {
		// Provincia
		if (document.frmInmueble.txtProvincia.value=="Seleccionar")
			{
				document.frmInmueble.txtProvincia.focus();
				alert("Debe indicar la Provincia a la que pertenece el Inmueble.");	
				return false;
			}
		// Poblacion
		if (isEmpty(document.frmInmueble.txtPoblacion.value))
			{
				warnEmpty (document.frmInmueble.txtPoblacion)
				return false;
			}
		// Tipo
		if (document.frmInmueble.txtTipo.value=="Seleccionar")
			{
				document.frmInmueble.txtTipo.focus();
				alert("Debe indicar el Tipo Inmueble.");	
				return false;
			}
		// Superficie
		if(isEmpty(document.frmInmueble.txtSuperficie.value)) {
				document.frmInmueble.txtSuperficie.focus();
				alert("Debe introducir la Superficie en m². Ej. 80,45");	
				return false;		
			}
		else {
			if (!isNumber(document.frmInmueble.txtSuperficie.value))
				{
					warnPrecio (document.frmInmueble.txtSuperficie)			
					return false;
				}
			}
		// Dormitorios
		if(document.frmInmueble.txtDormitorios.value=="No Procede")	{
			//Solo permito vacio si es un Garaje, Nave, Parcela o Solar
			if (document.frmInmueble.txtTipo.value=="Garaje" || document.frmInmueble.txtTipo.value=="Nave" || document.frmInmueble.txtTipo.value=="Parcela" || document.frmInmueble.txtTipo.value=="Local" || document.frmInmueble.txtTipo.value=="Solar") {
				//ok
			}
			else {
				document.frmInmueble.txtDormitorios.focus();
				alert ("Debe introducir el Nº de Dormitorios del Inmueble.");	
				return false;		
			}
		}			
		// Baños
		if(!isEmpty(document.frmInmueble.txtWC.value))	
			{				
			if (!EsNumeroPositivo(document.frmInmueble.txtWC.value,document.frmInmueble.txtWC))
				{
					warnCantidad (document.frmInmueble.txtWC)			
					return false;
				}
			}
		// Parcela
		if(!isEmpty(document.frmInmueble.txtParcela.value))	
			{			
			if (!isNumber(document.frmInmueble.txtParcela.value))
				{
					warnPrecio (document.frmInmueble.txtParcela)			
					return false;
				}
			}	
		// Plantas
		if(!isEmpty(document.frmInmueble.txtPlantas.value))	
			{						
			if (!EsNumeroPositivo(document.frmInmueble.txtPlantas.value,document.frmInmueble.txtPlantas))
				{
					warnCantidad (document.frmInmueble.txtPlantas)			
					return false;
				}
			}
		// Altura
		if(!isEmpty(document.frmInmueble.txtAltura.value))	
			{					
			if (!EsNumeroPositivo(document.frmInmueble.txtAltura.value,document.frmInmueble.txtAltura))
				{
					warnCantidad (document.frmInmueble.txtAltura)			
					return false;
				}
			}
			
		// Nombre
		if (isEmpty(document.frmInmueble.nombre.value))
			{
				warnEmpty (document.frmInmueble.nombre)
				return false;
			}
			
		// telefono
		if (isEmpty(document.frmInmueble.telefono.value))
			{
				warnEmpty (document.frmInmueble.telefono)
				return false;
			}
			
		// email
		if (isEmpty(document.frmInmueble.email.value))
			{
				warnEmpty (document.frmInmueble.email)
				return false;
			}
		
		return true;
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

function Corregir_euros(campo, tecla)
{	
	if (
		(tecla == 8) || 
			((tecla >= 48) && (tecla <= 57)) ||
			((tecla >= 96) && (tecla <= 105)) ||
		(tecla == 17) || (tecla == 18) || (tecla == 16) || (tecla == 32) ||
		(tecla == 37) || (tecla == 38) || (tecla == 39) || (tecla == 40) || (tecla == 93) || (tecla == 91) || (tecla == 92) ||
		(tecla == 186) || (tecla == 222) || (tecla == 20) || (tecla == 145) || (tecla == 19) || (tecla == 45) ||
		(tecla == 36)  || (tecla == 33) || (tecla == 35) || (tecla == 34) || (tecla == 113) || (tecla == 114) ||
		(tecla == 118) || (tecla == 119)  || (tecla == 120)  || (tecla == 121) || (tecla == 122) || (tecla == 123)
		)
	{
		if (
			(tecla == 37) || (tecla == 38) || (tecla == 39) || (tecla == 40) || (tecla == 17) || (tecla == 18) || (tecla == 16) || 
			(tecla == 93) || (tecla == 91) || (tecla == 92) || (tecla == 186) || (tecla == 222) || (tecla == 20) || (tecla == 145) ||
			(tecla == 19) || (tecla == 45) || (tecla == 36) || (tecla == 33) || (tecla == 35) || (tecla == 34) || (tecla == 113) ||
			(tecla == 114) || (tecla == 118) || (tecla == 119) || (tecla == 120)  || (tecla == 121) || (tecla == 122)  || (tecla == 123)
			)
		{
			return;
		}
	
	if ((tecla == 9) && (campo.value == ''))
	{		
			campo.value = '';
	}	
	
	if ((tecla == 188) && (campo.value.indexOf(',') != -1) && (campo.value.substring(campo.value.indexOf(',') + 1, campo.value.length)))
	{	
		// Borramos todas las comas que haya menos 1
		while (campo.value.indexOf(',') != campo.value.lastIndexOf(','))
			campo.value = campo.value.substring(0, campo.value.length - 1);
			
		// Si la coma que dejamos es el primer caracter o si antes hay un punto la borramos tb y dejamos sýlo el punto
		if (
			(campo.value.indexOf(',') == 0) || 
			(campo.value.indexOf('.') == (campo.value.indexOf(',') - 1))
			)
			campo.value = campo.value.substring(0, campo.value.length - 1);
			return;
	}
	else if (tecla == 188)
		{
			return;
		}
	
	// El tratamiento de los puntos es igual que el de las comas
	if ((tecla == 190) && (campo.value.indexOf('.') != -1) && (campo.value.substring(campo.value.indexOf('.') + 1, campo.value.length)))
	{
		while (campo.value.indexOf('.') != campo.value.lastIndexOf('.'))		
			campo.value = campo.value.substring(0, campo.value.length - 1);
			
		if (
			(campo.value.indexOf('.') == 0) || 
			(campo.value.indexOf(',') == (campo.value.indexOf('.') - 1))
			)
			campo.value = campo.value.substring(0, campo.value.length - 1);
		return;
	}
	else if (tecla == 190)
		{
			return;
		}
	
	if ((campo.value.length == 0) && ((tecla == 46) || (tecla == 8)))
	{		
		campo.value = '0';
		return;
	}
	numero = campo.value;

	// Quitamos los puntos
	var punto = numero.indexOf('.');
	while (punto != -1)
	{
		numero = numero.substring(0, punto) + numero.substring(punto + 1, numero.length);
		punto = numero.indexOf('.');
	}
	var coma = numero.indexOf(',');
	if (coma == 0)
	{
		return ('0' + numero);		
	}
	//alert(numero);
	var num = "";
	var decimal = "";	
				
		var coma = numero.indexOf(',');
		
		if (coma != -1)
		{		
			decimal = ',' + numero.substring(coma + 1, coma + 3);
			numero = numero.substring(0, coma);
		}
		else decimal = '';
		if (numero != '')
		{
	
			var str = "";
			while (numero > 999){
		
				n1 = Math.floor(numero/1000);
				n2 = numero - 1000 * n1;
				
				if (n2 < 10){
				   n2 = "00" + n2;
		        	}   
				else{
				   if (n2 < 100)
		       		      n2 = "0" + n2;
		                }
				str = "." + n2 + str;
				numero = n1;		
			}
			str = numero + str;		
		}	
		campo.value = str + decimal;
	}
	else campo.value = campo.value.substring(0, campo.value.length - 1);
}

function Corregir_miles(campo,destino)
{
    		var num_p;
    		num_p = campo.value;
    
    		longitud = 0;
    		longitud = num_p.length;
    		n = 0;
    
    		var num_sp = "";
    
    		while (n<longitud){
        
        		var car = ' ';
        		var car = num_p.charAt(n);
        		if (car != '.')
            			num_sp += num_p.charAt(n);
        		n++;
        
    		}
    		
    		var sin_puntos = parseInt(num_sp,10);
    			if (isNaN(sin_puntos)==false){
    				//return sin_puntos;
					//alert(sin_puntos);
					destino.value=sin_puntos;
					////alert(document.frmInmueble.PrecioAlquiler.value)
				}
}

function Devolver_miles(valor)
{
    		var num_p;
    		num_p = valor;
    
    		longitud = 0;
    		longitud = num_p.length;
    		n = 0;
    
    		var num_sp = "";
    
    		while (n<longitud){
        
        		var car = ' ';
        		var car = num_p.charAt(n);
        		if (car != '.')
            			num_sp += num_p.charAt(n);
        		n++;
        
    		}
    		
    		var sin_puntos = parseInt(num_sp,10);
			return sin_puntos;
}

function Vaciar(campo)
{
	var num_p;
	num_p = campo.value;

	longitud = 0;
	longitud = num_p.length;
	n = 0;

	var num_sp = "";

	while (n<longitud){

		var car = ' ';
		var car = num_p.charAt(n);
		if (car != '.')
				num_sp += num_p.charAt(n);
		n++;

	}
	
	var sin_puntos = parseInt(num_sp,10);
	
	if (isNaN(sin_puntos)==true){
		//vaciamos el campo
		campo.value='';
	}
}

