// JavaScript Document

//la funcio formValidaPle
//mira que el camp no estigui vuit

//la funcio formValidaText
//en el cas que el camp estigui buit omple el label del costat amb un error
//rep el objecte del camp, si pot o no estar vuit, el label on apareixera l'error y el missatge

//la funcio formValidaFormulari
//envia el missatge de error als labels segons la variable



var esIE = (window.navigator.userAgent.indexOf("MSIE")> 0);  

if (! esIE){   
  HTMLElement.prototype.__defineGetter__("innerText",function () { return(this.textContent); });   
  HTMLElement.prototype.__defineSetter__("innerText",function (txt) { this.textContent = txt; });
}

var nav4 = window.Event ? true : false;
function acceptNum(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
var key = nav4 ? evt.which : evt.keyCode;	
return (key <= 13 || (key >= 48 && key <= 57));
}

/*confirmacio al eliminar una entrada de la base de dades*/
function ConfirmarEliminar(orden, gotook, gotoko, borrar){
	var resp=confirm("seguro que desea eliminar? "+orden);
	if(resp){
		if(document.all){
		
		document.getElementById(borrar).action=gotook;
		document.getElementById(borrar).submit();
	}else{

		document.getElementById(borrar).action=gotook;
		document.getElementById(borrar).submit();
	}
		
	}else{
		if(document.all){
			document.getElementById('borrar').action=gotoko;
			document.getElementById('borrar').submit();
		}else{		
  			document.forms.borrar.action=gotoko;
  			document.forms.borrar.submit();
		}
	}
}

//en el cas que el camp estigui buit omple el label del costat amb un error
//rep el objecte del camp, si pot o no estar vuit, el label on apareixera l'error y el missatge
function formValidaText(camp,requerit,labelErr,msgErr, tipus){
		if(requerit==1){
			if(formValidaPle(camp.value)==false || formValidarTipus(tipus, camp.value)==false ){
				//document.getElementById(labelErr).innerText=msgErr	
				document.getElementById(labelErr).className=msgErr;
				//return msgErr;
			}
			else{
				document.getElementById(labelErr).className="";
					//document.getElementById(labelErr).innerText=""
					//document.getElementById(labelErr).style="";
					//return "1";
			}
		}
}

//mira que el camp no estigui vuit
function formValidaPle(valor) {
	var j;
	for ( j = 0; j < valor.length; j++ ) {
		if ( valor.charAt(j) != " " ) {
			return true
		}
	}
	return false
}

function formValidarTipus(tipus, valor){
	switch(tipus){
		case 'cadena':
			/*no verifiquem res*/
			return true;	
		break;
		case 'enter':
			var num=!isNaN(valor);
			//if(num==false) alert("s\'esperava un numero "+valor);
			return num;
		break;
	}

}

 
function validaFormulari(gotook, gotoko, nomformulari, idTaula) {
	var formulari=document.getElementById(nomformulari);
	var mida=formulari.length;
	var control=0;
	var i=0;
	
	while (i<mida && control==0) {
		
		var nom=new String(formulari[i].onblur);
		camp=new String(formulari[i].onblur);
		if ( nom.search("campObligatoriTEST = 1")!= -1 ){
			if( formValidaPle(formulari[i].value)==false ) {
				control=1;
				
			}
		} 
		i++;
	}	
	if(control==0){
		
		//alert("control =0");
		//updateRTEs();
		//document.getElementById(nomformulari).action=gotook;
		//document.getElementById(nomformulari).submit();
		return true;
	}
	else{
		
		//alert("control =1");
		alert("El formulari no es pot inserir perque algun camp no ha estat omplert amb la informació necessaria");
		//alert("redirecciona al fitxer: "+gotoko);
		//document.getElementById(nomformulari).action=gotoko+"?idTaula="+idTaula;
		//document.getElementById(nomformulari).submit();
		return false;		
	}
}
	

/***************************************** FUNCIONS PER EL EDITOR DE TEXT ***************************************************/	
function submitForm() {
	updateRTEs();
	return true;
}
//Usage: initRTE(imagesPath, includesPath, cssFile)

//AJAX IMATGES
var nomCapa="con"
function generaContingut(nomCapa,inici,direccio,subproducte,idioma) {
		// URL de l'aplicació AJAX
		var urlAjax="generaFoto.php";
		var parametres="inici="+inici+"&direccio="+direccio+"&sub="+subproducte+"&idioma="+idioma;
		var urlAjaxGET=urlAjax+"?"+parametres;
		
		// Declarem i instanciem un objecte tipus petició HTTP
		var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!httpRequest) {
        alert("Error, no s'ha pogut crear una instància de AJAX");
        return false;
    }
    
		// Indiquem el call-back (a qui es cridarà quan tinguem la resposta assíncrona)
		//httpRequest.nomCapa=nomCapa;
		httpRequest.onreadystatechange = function() { mostraResultat(httpRequest); };
		
    // Fem la petició
    httpRequest.open('GET', urlAjaxGET, true);
    httpRequest.send(null);
}

function mostraResultat(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
		document.getElementById(nomCapa).innerHTML=httpRequest.responseText;
		} else {
			alert('Error amb la petició');
		}
	}
}


// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 0;
PositionY = 0;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
	if (isNN){
		imgWin=window.open('about:blank','',optNN);
	}
	if (isIE){
		imgWin=window.open('about:blank','',optIE);
	}
	with (imgWin.document){
	writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
	writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
	writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
	writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
	writeln('width=100-(document.body.clientWidth-document.images[0].width);');
	writeln('height=100-(document.body.clientHeight-document.images[0].height);');
	writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
	writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
	writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
	if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
	else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
	writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
	close();		
	}
}

function popUp(URL,titol,w,h){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h+",left = 200,top = 200');");
}










