////////////////////////////////////////////////////////////////////////////
// Rutines Java de Psiquiatria -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Copyright (c) 1999-2003 Albert Alcaine i Peralta                       //
// http://www.alcaine.info                                                //
////////////////////////////////////////////////////////////////////////////

// Funcions botó dret -----------------------------------------------------
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
// ------------------------------------------------------------------------

// Funcions antisel·lecció ------------------------------------------------
document.onselectstart=new Function("return false");
// ------------------------------------------------------------------------
	
// Funcions afegeix a preferits -------------------------------------------
var bookmarkurl="http://www.alcaine.info/psiquiatria" 
var bookmarktitle="Web de Psiquiatria d'alcaine.info" 
function addbookmark(){ 
if (document.all) 
window.external.AddFavorite(bookmarkurl,bookmarktitle) 
} 
// ------------------------------------------------------------------------

// Funcions de la data ----------------------------------------------------
var dies = new Array(8);
dies[1] = "diumenge";
dies[2] = "dilluns";
dies[3] = "dimarts";
dies[4] = "dimecres";
dies[5] = "dijous";
dies[6] = "divendres";
dies[7] = "dissabte";
var mesos = new Array(13);
mesos[1] = "de gener";
mesos[2] = "de febrer";
mesos[3] = "de març";
mesos[4] = "d'abril";
mesos[5] = "de maig";
mesos[6] = "de juny";
mesos[7] = "de juliol";
mesos[8] = "d'agost";
mesos[9] = "de setembre";
mesos[10] = "d'octubre";
mesos[11] = "de novembre";
mesos[12] = "de desembre";
var ObjecteData = new Date();
var escriudia = dies[ObjecteData.getDay() + 1];
var escriumes = mesos[ObjecteData.getMonth() + 1];
var escriunumero = ObjecteData.getDate();
var escriuany = ObjecteData.getYear();
if (escriuany < 2000)
escriuany = escriuany + 1900;
// ------------------------------------------------------------------------

// Funcions de formularis colorejats --------------------------------------
var highlightcolor="#FFFFCC"
var ns6=document.getElementById&&!document.all
var previous=''
var eventobj
//var intended=/INPUT|TEXTAREA|SELECT|OPTION/
var intended=/INPUT|TEXTAREA|SELECT/
function checkel(which){
if (which.style&&intended.test(which.tagName)){
if (ns6&&eventobj.nodeType==3)
eventobj=eventobj.parentNode.parentNode
return true
}
else
return false
}
//Function to highlight form element
function highlight(e){
eventobj=ns6? e.target : event.srcElement
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
}
else{
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}
// ------------------------------------------------------------------------

// Funció de només enviar un cop ------------------------------------------
function submitonce(theform){
if (document.all||document.getElementById){
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
tempobj.disabled=true
}
}
}
// ------------------------------------------------------------------------

// Funció de tremolar -----------------------------------------------------
// Sintàxi: <img src="nom_arxiu_imatge.jpg" width="328" height="239" border="1" class="tremolaimatge" onMouseover="init(this);rattleimage()" onMouseout="stoprattle(this);top.focus()" onClick="top.focus()">
//configure shake degree (where larger # equals greater shake)
var rector=1
var stopit=0 
var valora=1
function init(which){
stopit=0
shake=which
shake.style.left=0
shake.style.top=0
}
function rattleimage(){
if ((!document.all&&!document.getElementById)||stopit==1)
return
if (valora==1){
shake.style.top=parseInt(shake.style.top)+rector
}
else if (valora==2){
shake.style.left=parseInt(shake.style.left)+rector
}
else if (valora==3){
shake.style.top=parseInt(shake.style.top)-rector
}
else{
shake.style.left=parseInt(shake.style.left)-rector
}
if (valora<4)
valora++
else
valora=1
setTimeout("rattleimage()",50)
}
function stoprattle(which){
stopit=1
which.style.left=0
which.style.top=0
}
// ------------------------------------------------------------------------

// Funció de colorejar taules ---------------------------------------------
// Sintàxi: onMouseover="changeto(event, 'lightgreen')" onMouseout="changeback(event, 'white')"
var ns6=document.getElementById&&!document.all
var ie=document.all
function changeto(e,highlightcolor){
source=ie? event.srcElement : e.target
if (source.tagName=="TR"||source.tagName=="TABLE")
return
while(source.tagName!="TD"&&source.tagName!="HTML")
source=ns6? source.parentNode : source.parentElement
if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore")
source.style.backgroundColor=highlightcolor
}
function contains_ns6(master, slave) { //check if slave is contained by master
while (slave.parentNode)
if ((slave = slave.parentNode) == master)
return true;
return false;
}
function changeback(e,originalcolor){
if
(ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")||source.tagName=="TR"||source.tagName=="TABLE")
return
else if (ns6&&(contains_ns6(source, e.relatedTarget)||source.id=="ignore"))
return
if (ie&&event.toElement!=source||ns6&&e.relatedTarget!=source)
source.style.backgroundColor=originalcolor
}
// ------------------------------------------------------------------------

// Funció de cerca --------------------------------------------------------
var TRange = null;
var win = null;
var frameval = false;
var nom = navigator.appName.toLowerCase();
var agt = navigator.userAgent.toLowerCase();
var is_major  = parseInt(navigator.appVersion);
var is_minor  = parseFloat(navigator.appVersion);
var is_ie     = (agt.indexOf("msie") != -1);
var is_ie4up  = (is_ie && (is_major >= 4));
var is_nav    = (nom.indexOf('netscape')!=-1);
var is_nav4   = (is_nav && (is_major == 4));
var is_mac    = (agt.indexOf("mac")!=-1);
var is_gecko  = (agt.indexOf('gecko') != -1);
var is_rev=0
if (is_gecko) {
temp = agt.split("rv:")
is_rev = parseFloat(temp[1])
}
//  If you want to search another frame, change "self" below to
//  parent.frames["thisframe"]
//  where "thisframe" is the name of the target frame
//  eg: var frametosearch1 = parent.frames["thisframe"]
var frametosearch1 = self
function search(whichform, whichframe) {
if (is_ie4up && is_mac) return;
if (is_gecko && (is_rev <1)) return;
if(whichform.findthis.value!=null && whichform.findthis.value!='') {
       str = whichform.findthis.value;
       if(whichframe!=self)
       frameval=true;
       win = whichframe;
}
else return;  //  i.e., no search string was entered
var strFound;
if(is_nav4 && (is_minor < 5)) {
   
  strFound=win.find(str); // case insensitive, forward search by default
//  There are 3 arguments available:
//  searchString: type string and it's the item to be searched
//  caseSensitive: boolean -- is search case sensitive?
//  backwards: boolean --should we also search backwards?
//  strFound=win.find(str, false, false) is the explicit
//  version of the above
//  The Mac version of Nav4 has wrapAround, but
//  cannot be specified in JS
	}
if (is_gecko && (is_rev >= 1)) {
    if(frameval!=false) win.focus(); // force search in specified child frame
    strFound=win.find(str, false, false, true, false, frameval, false);
//  There are 7 arguments available:
//  searchString: type string and it's the item to be searched
//  caseSensitive: boolean -- is search case sensitive?
//  backwards: boolean --should we also search backwards?
//  wrapAround: boolean -- should we wrap the search?
//  wholeWord: boolean: should we search only for whole words
//  searchInFrames: boolean -- should we search in frames?
//  showDialog: boolean -- should we show the Find Dialog?
}
 if (is_ie4up) {
  if (TRange!=null) {
    TRange.collapse(false)
    strFound=TRange.findText(str)
    if (strFound) TRange.select();
  }
  if (TRange==null || strFound==0) {
    TRange=win.document.body.createTextRange()
    strFound=TRange.findText(str)
    if (strFound) TRange.select();
  }
 }
  if (!strFound) alert ("El text '"+str+"' no s'ha trobat en aquest document. Proveu amb altres paraules o accediu a la cerca global.") // string not found
}
// ------------------------------------------------------------------------

// Funció de links animats amb fade ---------------------------------------
fadeColor = "#FF0000";
stepIn = 23;
stepOut = 45;
autoFade = false;  
sloppyClass = false;
macCompat = true;

hexa = new makearray(16);
for(var i = 0; i < 10; i++)
    hexa[i] = i;
hexa[10]="a"; hexa[11]="b"; hexa[12]="c";
hexa[13]="d"; hexa[14]="e"; hexa[15]="f";

document.onmouseover = domouseover;
document.onmouseout = domouseout;

fadeColor = dehexize(fadeColor.toLowerCase());

var fadeId = new Array();

function dehexize(Color){
	var colorArr = new makearray(3);
	for (i=1; i<7; i++){
		for (j=0; j<16; j++){
			if (Color.charAt(i) == hexa[j]){
				if (i%2 !=0)
					colorArr[Math.floor((i-1)/2)]=eval(j)*16;
				else
					colorArr[Math.floor((i-1)/2)]+=eval(j);
			}
		}
	}
	return colorArr;
}

function domouseover() {
	if(document.all){
		var srcElement = event.srcElement;
		if ((srcElement.tagName == "A" && autoFade && srcElement.className != "nofade") || srcElement.className == "fade" || (sloppyClass && srcElement.className.indexOf("fade") != -1)) {
				if (!srcElement.startColor) {
					srcElement.startColor = (srcElement.style.color)? srcElement.style.color: srcElement.currentStyle.color;
					srcElement.startColor = dehexize(srcElement.startColor.toLowerCase());
				}
				var link = (macCompat? srcElement.name: srcElement.uniqueID);
				if (link) fade(srcElement.startColor,fadeColor,link,stepIn);				
				else if (macCompat) alert("Error: Mac Compatility mode enabled, but link has no name.");
		}
	}
}

function domouseout() {
	if (document.all){
		var srcElement = event.srcElement;
		if ((srcElement.tagName == "A" && autoFade && srcElement.className != "nofade") || srcElement.className == "fade" || (sloppyClass && srcElement.className.indexOf("fade") != -1)) {
			var link = (macCompat? srcElement.name: srcElement.uniqueID);
			if (link) fade(fadeColor,srcElement.startColor,link,stepIn);
		}
	}
}

function makearray(n) {
    this.length = n;
    for(var i = 1; i <= n; i++)
        this[i] = 0;
    return this;
}

function hex(i) {
    if (i < 0)
        return "00";
    else if (i > 255)
        return "ff";
    else
       return "" + hexa[Math.floor(i/16)] + hexa[i%16];
}

function setColor(r, g, b, element) {
      var hr = hex(r); var hg = hex(g); var hb = hex(b);
      element.style.color = "#"+hr+hg+hb;
}

function fade(s,e,element,step) {
	var sr = s[0]; var sg = s[1]; var sb = s[2];
	var er = e[0]; var eg = e[1]; var eb = e[2];
	
	if (fadeId[0] != null && fade[0] != element && eval(fadeId[0])) {
		var orig = eval(fadeId[0]);
		setColor(orig.startColor[0],orig.startColor[1],orig.startColor[2],orig);
		var i = 1;
		while(i < fadeId.length) {
			clearTimeout(fadeId[i]);
			i++;
		}
	}
		
	for(var i = 0; i <= step; i++) {
		fadeId[i+1] = setTimeout("setColor(Math.floor(" +sr+ " *(( " +step+ " - " +i+ " )/ " +step+ " ) + " +er+ " * (" +i+ "/" +
			step+ ")),Math.floor(" +sg+ " * (( " +step+ " - " +i+ " )/ " +step+ " ) + " +eg+ " * (" +i+ "/" +step+
			")),Math.floor(" +sb+ " * ((" +step+ "-" +i+ ")/" +step+ ") + " +eb+ " * (" +i+ "/" +step+ ")),"+element+");",i*step);
	}
	fadeId[0] = element;
}
// ------------------------------------------------------------------------

// Funció de personalització del nom --------------------------------------
// nom
// valor
// [caduca]
// [ruta]
// [domini]
// [segur]
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  alert("Totes les dades que el web havia enregistrat per a personalitzar la vostra navegació s'han esborrat del vostre ordinador.")
}
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
// ------------------------------------------------------------------------

// Funció de centrar la finestra ------------------------------------------
// Altres usos: onclick="ObreFinestra(this.href,'nom','400','400','yes');return false"
// Especifiqueu al final d'atributs: directories, location, menubar, status, toolbar, etc.
function ObreFinestra(pag,identificador,w,h,scroll){
PosicioEsquerra = (screen.width) ? (screen.width-w)/2 : 0;
PosicioSostre = (screen.height) ? (screen.height-h)/2 : 0;
atributs = 'height='+h+',width='+w+',top='+PosicioSostre+',left='+PosicioEsquerra+',scrollbars='+scroll+',resizable'
win = window.open(pag,identificador,atributs)
}
// ------------------------------------------------------------------------
