
// BrowserCheck Object
// provides most commonly needed browser checking variables
// 19990326

// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/

function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	if (this.ie5) this.v = 5
	this.min = (this.ns||this.ie)
}

function EnviaPlana(document)
{
// automatically create the "is" object
is = new BrowserCheck()


if (is.ie4) document.submit.click()
if (is.ie5) document.submit()

}


texte=1 // Cadena de Caracters
codipostal=2 // Sequencia de 5 numeros
email=3 // camp tipus Correu
numeric=4 // camp tipus Numero
nif=5 // camp tipus NIF
textarea=6 // camp tipus TextArea
data=7 // camp tipus Data
lletra=8 // un sol Caracter
Real=9 

function ValidarTipus(camp,tipus,longitudMax)

{

if (tipus==texte && !Valido(camp)) {alert("Campo contiene carácteres no válidos (')"); return false}
if (tipus==codipostal && !EsCodiPostal(camp)) {alert("Campo código postal incorrecto. Formato (XXXXX) donde X ha de ser numérico."); return false}
if (tipus==email && (!EsMail(camp) || !Valido(camp))) {alert("Campo email con formato incorrecto o contiene carácteres no válidos.Formato (XXXX@XXXX.XXXX.XXXX)"); return false}
if (tipus==numeric && !EsPositiu(camp)) {alert("Campo no numérico"); return false}
if (tipus==nif && !EsNif(camp)) { alert("Campo no tiene formato de nif");return false}
if (tipus==lletra && (!EsLletra(camp) || !Valido(camp))) { alert("Campo ha de ser un carácter o contiene carácteres no válidos");return false}
if (tipus==textarea && !Valido(camp)) {alert("Campo contiene carácteres no válidos (')"); return false}
if (tipus==textarea && !mira_longitud(camp,"",longitudMax)) {alert("Campo supera la longitud máxima"); return false}
if (tipus==data && !EsData(camp)) {alert("Campo no tiene formato de fecha"); return false}
if (tipus==Real && !EsFloat(camp)) {alert("Campo no tiene formato real"); return false}


return true

}


// FUNCION /////////////////////////////////////////////////////
function MM_openBrWindow(theURL,winName,features) { file://v2.0
  window.open(theURL,winName,features);
}



// FUNCION /////////////////////////////////////////////////////
function EsMail(inputVal){
	inputStr = "" + inputVal
	oneArroba = false
	oneDecimal = false
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar == "@"&& !oneArroba && i!=0) {
			oneArroba =true
			continue
		}
		if (oneChar == "." && !oneDecimal && oneArroba&& i!=(inputStr.length-1)) {
			oneDecimal = true
			return true
		}
	}
	return false
}


// FUNCION /////////////////////////////////////////////////////
function precisio(inputval,precisio)
{
inputStr= inputval + ""
cadena=inputval + ""
cadena2=""
index=0
var onechar=inputStr.charAt(index);
while ((onechar!=".") && (index<cadena.length))
{
onechar=inputStr.charAt(index);
if (onechar!=".") index=index+1
}
if (onechar==".") 
		{
		index2=0
		if (index+precisio<cadena.length) tope=index+precisio
		if (index+precisio>=cadena.length) tope=cadena.length
		while (index2<tope)
		{
		cadena2=cadena2+inputStr.charAt(index2)
		index2=index2+1
		}
		return cadena2	
}
return cadena
}


// FUNCION /////////////////////////////////////////////////////
function mira_longitud(inputval,nom,longitudMax) 
{
cadena=inputval + ""
if (cadena.length>longitudMax)  { 
					longitud_sobre_pasa=cadena.length-longitudMax
					alert("Campo " + nom + " sobrepasa longitud máxima de "+longitud_sobre_pasa + " carácteres.")
					return false
			         }
if (cadena.length<=longitudMax) {	
				return true
			}
}


// FUNCION /////////////////////////////////////////////////////
function mira_longitud_password(inputval) 
{
cadena=inputval + ""
if (cadena.length<7)  { alert("La longitud del password debe ser de 7 dígitos como mínimo");return;}
return true;
}
			


// FUNCION /////////////////////////////////////////////////////
function evalua (inputval)
{
inputStr= "" + inputval
var oneChar=inputStr.charAt(0);
if (oneChar=="-") { return (-Math.abs(inputval)) }
if (oneChar!="-") { return (Math.abs(inputval)) }
}


// FUNCION /////////////////////////////////////////////////////
function EsNif (inputval)
{
inputStr= "" + inputval
if (inputStr.length!=8) return false
return EsPositiu(inputval)
}



// FUNCION /////////////////////////////////////////////////////
function EsPercentatge (inputval)
{
inputStr= "" + inputval
if (inputStr.length==3) return (inputval=='100')
for (var i=0; i < inputStr.length; i++ )
	{
	 var oneChar=inputStr.charAt(i)
		if ((oneChar < "0") || (oneChar > "9"))  return false 
	}
return true
}


// FUNCION //////////////Obliga a ser 10 posicions 2 pel dia , 2 pel mes i 4 per l'any
/*function EsData (inputval)
{
var oneChar
inputStr= "" + inputval;
if (inputStr.length!=10) return false;
oneChar=inputStr.charAt(0);
if ((oneChar < "0") || (oneChar > "3"))  return false ;
if (oneChar=="3") 
	{
	oneChar=inputStr.charAt(1);
	if ((oneChar < "0") || (oneChar > "1"))  return false ;
	}
if (oneChar!="3") 
	{
	oneChar=inputStr.charAt(1);
	if ((oneChar < "0") || (oneChar > "9"))  return false ;
	}
oneChar=inputStr.charAt(2);
if (oneChar!="-") return false;
oneChar=inputStr.charAt(3);
if ((oneChar < "0") || (oneChar > "1"))  return false ;
if (oneChar=="1")
	{
	oneChar=inputStr.charAt(4);
if ((oneChar < "0") || (oneChar > "2"))  return false ;
	}
if (oneChar=="0")
	{
	oneChar=inputStr.charAt(4);
if ((oneChar < "0") || (oneChar > "9"))  return false ;
	}
oneChar=inputStr.charAt(5);
if (oneChar!="-") return false;
oneChar=inputStr.charAt(6);
if ((oneChar < "0") || (oneChar > "9"))  return false ;
oneChar=inputStr.charAt(7);
if ((oneChar < "0") || (oneChar > "9"))  return false ;
oneChar=inputStr.charAt(8);
if ((oneChar < "0") || (oneChar > "9"))  return false ;
oneChar=inputStr.charAt(9);
if ((oneChar < "0") || (oneChar > "9"))  return false ;
return true;
}*/



// FUNCION /////////////////////////////////////////////////////
function LongitudNula (inputval)
{
inputStr= "" + inputval
if (inputStr.length==0) return true
return false
}


// FUNCION /////////////////////////////////////////////////////
function EsCodiPostal(inputval)
{
inputStr= "" + inputval
if (inputStr.length!=5) return false
for (var i=0; i < inputStr.length; i++ )
	{
	 var oneChar=inputStr.charAt(i)
		if ((oneChar < "0") || (oneChar > "9"))  return false 
	}
return true
}


// FUNCION ///////////////////////////////////////////////////
function EsPositiu(inputval)
{
inputStr= "" + inputval
if (inputStr.length==0) return true
for (var i=0; i < inputStr.length; i++ )
	{
	 var oneChar=inputStr.charAt(i)
		if ((oneChar < "0") || (oneChar > "9"))  return false 
	}
return true
}


// FUNCION /////////////////////////////////////////////////////
function EsFloat(inputval)
{
inputStr= "" + inputval
punt=false
if (inputStr.length==0) return true
for (var i=0; i < inputStr.length; i++ )
	{
	 var oneChar=inputStr.charAt(i)
		if ((oneChar==".") && (punt==true)) return false
		punt=(oneChar==".")
		if (((oneChar < "0") || (oneChar > "9")) && (oneChar!=".")) return false 
	}
return true
}


// FUNCION /////////////////////////////////////////////////////
function Valido (inputval)
{
inputStr= "" + inputval
if (inputStr.length==0) return true
for (var i=0; i < inputStr.length; i++ )
	{
	 var oneChar=inputStr.charAt(i)
		//if (oneChar == "'") return false 
			
	}
return true
}


// FUNCION /////////////////////////////////////////////////////
function EsLletra(inputval)
{
inputStr= "" + inputval
if (inputStr.length!=1) return false
var oneChar=inputStr.charAt(0)
if (((oneChar >= "a") && (oneChar <= "z"))  || ((oneChar >= "A") && (oneChar <= "Z"))) return true
return false
}


// FUNCION /////////////////////////////////////////////////////
function ConfirmaOperacio(texte,NumeroCops)
{
var index;
index=1;
while (index<=NumeroCops) {
if (!confirm(texte)) return false;
index++;
}
return true
}


// FUNCION /////////////////////////////////////////////////////
function EsData(dateStr) 
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) 
	{
		alert("Por favor introduzca un fecha en el formato dd/mm/yyyy o dd-mm-yyyy.");
		return false;
	}
	
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[5];
	
	if (month < 1 || month > 12) 
	{ // check month range
		alert("El mes tiene que estar entre 1 y 12.");
		return false;
	}
	
	if (day < 1 || day > 31) 
	{
		alert("El día debe estar entre 1 y 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert("Atención, el mes "+month+" no tiene 31 días!")
		return false;
	}
	
	if (month == 2)
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
			alert("Febrero del " + year + " no tiene " + day + " días!");
			return false;
		}
	}
	return true; // date is valid
}




