// -------------------- emptyCheck ------------------
// Joduba: 18/06/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// --------------------------------------------------



function emptyCheck(elementID) {
    // checks if the field is empty
    var filter = /^.*[A-Za-z0-9]\w*.*$/;
	if (!filter.test(elementID.value)) {
            alert("Please fill required fields");
            elementID.focus();
            return false;
    }
    return true;
};

function emptyCheckField(elementID,name) {

        // checks if the field is empty
        var filter = /^.*[A-Za-z0-9]\w*.*$/;
		if (!filter.test(elementID.value)) {
			alert("Field"+": "+strName+"\n"+"Please fill required fields");
				elementID.focus();
                return false;
        }
        return true;
};

// -------------------- nameCheck ------------------
// Joduba: 18/06/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// --------------------------------------------------

function nameCheck(elementID) {

        // checks if the field is empty
        var filter = /^.*[A-Za-z]\w*.*$/;
	if (!filter.test(elementID.value)) {
                alert("Please fill required fields");
                elementID.focus();
                return false;
        }
        return true;
};

function nameCheckField(elementID,strName) {

	if (elementID.value!="") return true;
	else{
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	/*
	// checks if the field is empty
	var filter = /^.*[A-Za-z]\w*.*$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	return true;
	*/
};

// -------------------- usernameCheck ------------------
// Joduba: 12/10/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// --------------------------------------------------

function usernameCheck(elementID) {

        // checks if the field is empty
        var filter = /^[a-z0-9]+[a-z_.\-0-9]*[a-z0-9]+$/;
	if (!filter.test(elementID.value)) {
                alert("Please fill required fields");
                elementID.focus();
                return false;
        }
        return true;
};

function usernameCheckField(elementID,strName) {

	if (elementID.value=="")  {
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	// checks if the field is empty
        var filter = /^[a-z0-9]+[a-z_.\-0-9]*[a-z0-9]+$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"Invalid format: use lowercase letter,  numbers and '_', '-', '.' characters.\n"+strName+" must start with a letter and finish with a letter or digit");
		elementID.focus();
		return false;
	}
	return true;
};

// -------------------- domainCheck ------------------
// Joduba: 31/10/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// --------------------------------------------------

function domainCheck(elementID) {

        // checks if the field is empty
	var filter=/^((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(elementID.value)) {
                alert("Please fill required fields");
                elementID.focus();
                return false;
        }
        return true;
};


function domainCheckField(elementID,strName) {

	if (elementID.value=="")  {
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	// checks if the field is empty
	var filter=/^((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"Invalid format: use lowercase letter,  numbers and '_', '-', '.' characters.\n"+strName+" must start with a letter and finish with a letter or digit");
		elementID.focus();
		return false;
	}
	return true;
};


// -------------------- identifierCheck ------------------
// jmserra: 04/04/2007
// --------------------------------------------------

function identifierCheck(elementID) {
	var filter = /^[a-zA-Z]+([a-zA-Z0-9]|[-]|[_])*$/;
	if (!filter.test(elementID.value)) {
                alert("Please fill required fields");
                elementID.focus();
                return false;
        }
        return true;
};

function identifierCheckField(elementID,strName) {

if (elementID.value=="")  {
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	// checks if the field is empty
	var filter = /^[a-zA-Z]+([a-zA-Z0-9]|[-]|[_])*$/;
	
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"Invalid Characters found. Use only letters(without accent marks) , numbers and '_', '-', '.'\n"+strName+" must start with a letter and finish with a letter or digit");
		elementID.focus();
		return false;
	}
	return true;

};

// -------------------- Comprovar si es email------------------
// Joduba: 03/02/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------
/*//Versio anterior
function emailCheck(elementID) {

	// checks if the e-mail address is valid
	//var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var filter=/^([a-zA-Z0-9_.-+])+@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(elementID.value)) {

		alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
		elementID.focus();
		return false;
	}
	
	return true;
}
*/

//He trobat el seguent codi per inet, esta molt lograt
// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd
function emailCheck(elementID) {
	addr=elementID.value;
	//function validateEmail(addr,man,db) {
	//Head anterior
	man=1;//per defecte
	db=1;//per defecte
	if (addr == '' && man) {
		alert("email address is mandatory");
		elementID.focus();
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
			alert("email address contains invalid characters");
			elementID.focus();
	      return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
			alert("email address contains non ascii characters");
			elementID.focus();
	      return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
		alert("email address must contain an @");
		elementID.focus();
	   return false;
	}
	if (atPos == 0) {
		alert("email address must not start with @");
		elementID.focus();
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
		alert("email address must contain only one @");
		elementID.focus();
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
		alert("email address must contain a period in the domain name");
		elementID.focus();
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
		alert("period must not immediately follow @ in email address");
		elementID.focus();
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
		alert("period must not immediately precede @ in email address");
		elementID.focus();
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
		alert("two periods must not be adjacent in email address");
		elementID.focus();
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'cat' && suffix != 'es' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
		//if (db)
			alert("invalid primary domain in email address");
			elementID.focus();
		//}
	   if (db)
	   return false;
	}
	return true;
}

function noRequiredEmailCheck(elementID) {

	// Check if empty
	if (elementID.value=="") return true;

	// checks if the e-mail address is valid
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(elementID.value)) {

		alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
		elementID.focus();
		return false;
	}
	
	return true;
}

// -------------------- Password Generator ------------------
// Joduba: 04/09/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------
// -- Original:  Rob Kroll (rkroll@istar.ca) 


function pwgen(l) {
var selectionarray=new Array(62)
var length=62;
var i,j;
var tmpstr;
	selectionarray[1]="A"; selectionarray[2]="B"; selectionarray[3]="C"; selectionarray[4]="D";
	selectionarray[5]="E"; selectionarray[6]="F"; selectionarray[7]="G"; selectionarray[8]="H";
	selectionarray[9]="I"; selectionarray[10]="J"; selectionarray[11]="K"; selectionarray[12]="L";
	selectionarray[13]="M"; selectionarray[14]="N"; selectionarray[15]="O"; selectionarray[16]="P";
	selectionarray[17]="Q"; selectionarray[18]="R"; selectionarray[19]="S"; selectionarray[20]="T";
	selectionarray[21]="U"; selectionarray[22]="V"; selectionarray[23]="W"; selectionarray[24]="X";
	selectionarray[25]="Y"; selectionarray[26]="Z"; selectionarray[27]="0"; selectionarray[28]="1";
	selectionarray[29]="2"; selectionarray[30]="3"; selectionarray[31]="4"; selectionarray[32]="5";
	selectionarray[33]="6"; selectionarray[34]="7"; selectionarray[35]="8"; selectionarray[36]="9";
	selectionarray[37]="a"; selectionarray[38]="b"; selectionarray[39]="c"; selectionarray[40]="d";
	selectionarray[41]="e"; selectionarray[42]="f"; selectionarray[43]="g"; selectionarray[44]="h";
	selectionarray[45]="i"; selectionarray[46]="j"; selectionarray[47]="k"; selectionarray[48]="l";
	selectionarray[49]="m"; selectionarray[50]="n"; selectionarray[51]="o"; selectionarray[52]="p";
	selectionarray[53]="q"; selectionarray[54]="r"; selectionarray[55]="s"; selectionarray[56]="t";
	selectionarray[57]="u"; selectionarray[58]="v"; selectionarray[59]="w"; selectionarray[60]="x";
	selectionarray[61]="y"; selectionarray[62]="z";

	tmpstr="";
	i=0;
	do {
		var randscript = -1;
		while (randscript < 1 || randscript > length || isNaN(randscript)) {
			randscript = parseInt(Math.random()*(length));
		}
		j=randscript;
		tmpstr=tmpstr+selectionarray[j];
		i=i+1;
	} while (i<l);

	return tmpstr;
}

// -------------------- Comprovar si es email------------------
// Joduba: 04/09/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function checkPw(elementIDpw1,elementIDpw2) {

	if (elementIDpw1.value != elementIDpw2.value) {
		alert ("You did not enter the same new password twice. Please re-enter your password.");
		elementIDpw1.focus();
		return false;
	}
	if (elementIDpw1.value=="") {
		alert ("Password cannot be empty. Please re-enter your password.");
		elementIDpw1.focus();
		return false;
	}

	if (elementIDpw1.value.length<6) {
		alert ("Password must have at least 6 characters long.");
		elementIDpw1.focus();
		return false;
	} 

	return true;

}

//Markus -> li passem un nou parametre, required, per validar si es correcte el mail introduit
//en cas que no sigui requerit
function checkNewPw(elementIDpw1,elementIDpw2,required) {

	if(!required){
		if (elementIDpw1.value=="" && elementIDpw1.value==elementIDpw2.value) return true;
	}

	if (elementIDpw1.value=="") {
		alert ("Password cannot be empty. Please re-enter your password.");
		elementIDpw1.focus();
		return false;
	}

	if (elementIDpw1.value != elementIDpw2.value) {
		alert ("You did not enter the same new password twice. Please re-enter your password.");
		elementIDpw1.focus();		
		return false;
	}

	if (elementIDpw1.value.length<6) {
		alert ("Password must have at least 6 characters long.");
		elementIDpw1.focus();
		return false;
	} 

	return true;

}

// To generate an scrolling news text

function IwithScroll_start() {
	var IwithScrollIdSupported = false;
	IwithScrollId_WIDTH = document.getElementById(IwithScrollId).style.width;
	var img = "<img src=IwithScrollId_space.gif width="+IwithScrollId_WIDTH+" height=0>";

	if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1) {
		document.getElementById(IwithScrollId).innerHTML = "<TABLE  cellspacing='0' cellpadding='0' width='100%'><TR><TD nowrap='nowrap'>"+img+"<SPAN style='"+IwithScrollStyle+"' ID='"+IwithScrollId+"-iwspace' width='100%'>&nbsp;</SPAN>"+img+"</TD></TR></TABLE>";
		IwithScrollIdSupported = true;
	}
	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
		document.getElementById(IwithScrollId).innerHTML = "<DIV nowrap='nowrap' style='width:100%;'>"+img+"<SPAN style='"+IwithScrollStyle+"' ID='"+IwithScrollId+"-iwspace' width='100%'></SPAN>"+img+"</DIV>";
		IwithScrollIdSupported = true;
	}
	if(!IwithScrollIdSupported) document.getElementById(IwithScrollId).outerHTML = ""; else {
		document.getElementById(IwithScrollId).scrollLeft = IwithScrollDirection ? document.getElementById(IwithScrollId).scrollWidth - document.getElementById(IwithScrollId).offsetWidth : 0;
		document.getElementById(IwithScrollId+"-iwspace").innerHTML = IwithScrollContent;
		document.getElementById(IwithScrollId).style.display="block";
		IwithScroll_do();
	}
}

function IwithScroll_do() {
	if(!IwithScrollPause) document.getElementById(IwithScrollId).scrollLeft += IwithScrollSpeed * (IwithScrollDirection ? -1 : 1);
	if(IwithScrollDirection && document.getElementById(IwithScrollId).scrollLeft <= 0) document.getElementById(IwithScrollId).scrollLeft = document.getElementById(IwithScrollId).scrollWidth - document.getElementById(IwithScrollId).offsetWidth;
	if(!IwithScrollDirection && document.getElementById(IwithScrollId).scrollLeft >= document.getElementById(IwithScrollId).scrollWidth - document.getElementById(IwithScrollId).offsetWidth) document.getElementById(IwithScrollId).scrollLeft = 0;
	window.setTimeout("IwithScroll_do()", 30);
}

// -------------------- Comprovar si es Phone number------------------
// Markus: 15/07/2006
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function phoneCheckField(elementID,strName,required) {
	// checks if the phone number is valid
	
	if (!required){
		if (elementID.value=="") return true;
	}
	
	var filter=/^\+?(\d|\w|-|.)*\d$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"Your phone number seems incorrect.  Please try again using '+','.','-','numbers'");
		elementID.focus();
		return false;
	}
	
	return true;
}


// -------------------- Comprovar si es Number------------------
// Markus: 15/07/2006
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function numberCheckField(elementID,strName,required) {
	// checks if the number is valid

	if (!required){
		if (elementID.value=="") return true;
	}
	//antes ->> ^\d(\d|'|,|.|e|E|\+|-)*\d$
	var filter=/^\d(\d|\,|\'|\.)*(e|\�)?$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The number seems incorrect.");
		elementID.focus();
		return false;
	}
	
	return true;
}


// --- Comprovar si es REALNumber---
// jsmerra: 21/05/2007
// ---------------------------------

function realnumberCheckField(elementID,strName,required) {
	if (!required){
		if (elementID.value=="") return true;
	}
	var filter=/^(\d|'|,|.|e|E|\+|-)*\d$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The number seems incorrect.");
		elementID.focus();
		return false;
	}
	
	return true;
}


// -------------------- Comprovar si es NumberRange------------------
// Markus: 15/07/2006
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function numberrangeCheckField(elementID,min,max,strName,required) {
	// checks if the numberrange is valid, min<=elementID.value or elementID.value<=max
	
	if (!required){
		if (elementID.value=="") return true;
	}
	
	if (!(min<=elementID.value && elementID.value<=max)){
		alert("Field"+": "+strName+", "+"the range is"+": ["+min+"-"+max+"]\n"+"The number seems incorrect.");
		elementID.focus();
		return false;
	}
	
	return true;
}


// -------------------- Comprovar si Integer------------------
// Markus: 15/07/2006
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function integerCheckField(elementID,strName,required) {
	// checks if the integer is valid

	if (!required){
		if (elementID.value=="") return true;
	}
	
	var filter=/^(\d)*\d$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The integer seems incorrect.");
		elementID.focus();
		return false;
	}
	
	return true;
}


// -------------------- Comprovar si Decimal------------------
// Markus: 15/07/2006
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function decimalCheckField(elementID,strName,required) {
	// checks if the integer is valid
	
	if (!required){
		if (elementID.value=="") return true;
	}
	
	//nomenclatura UE -> decimal (,|')
	//USA -> decimal (.)
	var filter=/^(\d)*('|,|.)(\d)*\d$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The decimal seems incorrect.");
		elementID.focus();
		return false;
	}
	
	return true;
}


// -------------------- Comprovar si URL ------------------
// Markus: 15/07/2006
//
// Original from: http://www.webbedwonder.com
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------


function urlCheckField(elementID,strName,required){

	if (!required){
		if (elementID.value=="") return true;
	}

	var filter=/^((http(s)?|ftp):(\d)*\/\/)?(\w*)\.([\%\_\-\+a-z0-9]*)\.(\w*)/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The url seems incorrect.");
		elementID.focus();
		return false;
	}

	return true;
}


// -------------------- Comprovar si la hora es correcte-----
// Markus: 16/07/2006
//
// Original from: my mind, juasjuas
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function check_time_h(elementID,strName,required){

	if(!required){
		if (elementID.value=="") return true;
	}

	if (elementID.value==""){
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	if (!(0<=elementID.value && elementID.value<=23)){
		alert("Field"+": "+strName+"\n"+"The time (hour) seems incorrect.");
		elementID.focus();
		return false;
	}

	return true;
}

function check_time_m(elementID,strName,required){

	if(!required){
		if (elementID.value=="") return true;
	}

	if (elementID.value==""){
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	if (!(0<=elementID.value && elementID.value<=59)){
		alert("Field"+": "+strName+"\n"+"The time (minute) seems incorrect.");
		elementID.focus();
		return false;
	}

	return true;
}

// -------------------- Comprovar si la data es correcte-----
// Markus: 16/07/2006
//
// Original from: my mind, juasjuas
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function dateCheckField(elementID, strName,required){
	
	if(!required){
		if (elementID.value=="") return true;
	}
	
	var filter=/^\d{2,4}(\-|\/)\d{2}\1\d{2,4}$/;
	if (!filter.test(elementID.value)) {
		alert("Field"+": "+strName+"\n"+"The date seems incorrect.");
		elementID.focus();
		return false;
	}

	return true;
	
}

// -------------------- Comprovar si la data es correcte-----
// Markus: 16/07/2006
//
// Original from: my mind, juasjuas
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

function emptyTextCheck(elementID,strName) {

	// Check if empty
	if (elementID.value==""){
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	return true;
}

function optionCheck(elementID,strName){
	
	//elementID.value ens dona el valor de la key selected.
	if (elementID.value==""){
		alert("Field"+": "+strName+"\n"+"Please fill required fields");
		elementID.focus();
		return false;
	}
	
	return true;
}

//------------------------------------------------
// toogleDiv: Hide and make visible a given division
// author: Joduba
// date: 15/07/2006
//--------------------------------------------------
function iwith_toggleDiv(divId, iconId, headerId, collapse, expand) 
{ 
    if (collapse==undefined ) collapse="Click here to collapse this section";
    if (expand==undefined ) expand="Click here to expand this section";

    divElement = document.getElementById(divId);
    imgElement = document.getElementById(iconId);
    tableElement = document.getElementById(headerId);

    if (divElement.nextSibling.style.display != "none") { 
	divElement.nextSibling.style.display = "none"; 
	imgElement.src="/i-images/collapserright.gif";
	imgElement.title=expand;
	tableElement.title=expand;
    }
    else { 
	divElement.nextSibling.style.display = "block"; 
	imgElement.src="/i-images/collapserdown.gif";
	imgElement.title=collapse;
	tableElement.title=collapse;
    } 
}
