/////////////////////////
// JavaScript Document //
/////////////////////////


// Asignar al onload les Funcions //

window.onload = function(){
	conteEnrere();
	ocultaTotes();
	if(typeof(_home) != 'undefined'){
		MM_preloadImages(
			_imagesHome+'/img_home_que_es_mon.jpg',
			_imagesHome+'/img_home_que_es_moff.jpg',
			_imagesHome+'/img_home_viatja_mon.jpg',
			_imagesHome+'/img_home_viatja_moff.jpg',
			_imagesHome+'/img_home_experimenta_mon.jpg',
			_imagesHome+'/img_home_experimenta_moff.jpg',
			_imagesHome+'/img_home_gaudeix_mon.jpg',
			_imagesHome+'/img_home_gaudeix_moff.jpg');
	}
}

// Ocultar TOTS els Menus //

function ocultaTotes(){
	for (var i = 1; i<=4; i++) {
		if (document.getElementById('submenuPastilla'+i)) {document.getElementById('submenuPastilla'+i).style.display='none';}
	}
}

// Mostrar Menus //

function mostra(id) {
	var d = document.getElementById(id);
	if (d) {d.style.display='block';}
	
	switch(id){
		case 'submenuPastilla1':
																var id2 = "submenuPastillaBanner1";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_que_es_mon_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla2':
																var id2 = "submenuPastillaBanner2";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_viatja_mon_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla3':
																var id2 = "submenuPastillaBanner3";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_experimenta_mon_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla4':
																var id2 = "submenuPastillaBanner4";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_gaudeix_mon_'+idioma+'.jpg)';}
																break;
		case 'default':							break;
	}
	
}

// Ocultar Menus //

function oculta(id) {
	
	var d = document.getElementById(id);
	if (d) {d.style.display='none';}
	
	switch(id){
		case 'submenuPastilla1':
																var id2 = "submenuPastillaBanner1";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_que_es_moff_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla2':
																var id2 = "submenuPastillaBanner2";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_viatja_moff_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla3':
																var id2 = "submenuPastillaBanner3";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_experimenta_moff_'+idioma+'.jpg)';}
																break;
		case 'submenuPastilla4':
																var id2 = "submenuPastillaBanner4";
																var f = document.getElementById(id2);
																if (f) {f.style.background='url('+_imagesHome+'/img_home_gaudeix_moff_'+idioma+'.jpg)';}
																break;
		case 'default':							break;
	}
	
}


// Validacion formularios //

$(document).ready(MM_findObj);
$(document).ready(MM_validateForm);

function MM_findObj(n, d) { //v4.01
	if(n)	n = n.toString();
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    //d=parent.frames[0].document; n=n.substring(0,p);} 
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}	
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
 
  for (i=0; i<(args.length-2); i+=3){ 
  	test=args[i+2]; val=MM_findObj(args[i]);
  	if (val){
  		nm=val.name; 
  		if ((val=val.value)!="") {
      	if (test.indexOf('isEmail')!=-1){ 
      		p=val.indexOf('@');
        	if (p<1 || p==(val.length-1)){
        		if(errors == '')   		errors+='Introdueix un '+nm+' vālid.\n';
        	}
      	}else if (test!='R' && test!='C' && test!='P') { 
      		num = parseFloat(val);
        	if (isNaN(val)){
        		if(errors == '')			errors+='Introdueix un '+nm+' vālid.\n';
        	}
        	if (test.indexOf('inRange') != -1) { 
        		p=test.indexOf(':');
          	min=test.substring(8,p); 
          	max=test.substring(p+1);
          	if (num<min || max<num){
          	 if(errors == '') 	 	errors+='Introdueix un '+nm+' vālid des de '+min+' fins a'+max+'.\n';
          	}
    			}     
    		} 
    	}else if (test.charAt(0) == 'R' && errors == ''){     
    					if(errors == '') errors += 'Omple el camp '+nm+' per continuar.\n';		    		
    	}
    }    
    if (test.charAt(0) == 'P' && errors == ''){
    	$originPassword = args[i+1];
		  lastval = MM_findObj($originPassword);
		  $originPasswordValue = lastval.value;
		  $repeatPasswordValue = val;		    			
			if($originPasswordValue != $repeatPasswordValue) errors = 'El camp '+nm+' no coincideix.\n';
   	}
    if (test.charAt(0) == 'C' && errors == ''){
			$name = args[i];
			$form = args[i+1];
			$isChecked = eval('window.document.'+$form+'.'+$name+'.checked');
			if($isChecked.toString() == "false"){
				errors += eval('window.document.'+$form+'.'+$name+'.name');
			}
    }
  } 
  
  if (errors) document.getElementById("MissatgeError").innerHTML =('\n'+errors);
  document.MM_returnValue = (errors == '');
  
}


// REMPLAZAR FUENTES
		//<![CDATA[
//		if(typeof sIFR == "function"){
//					sIFR.replaceElement(named({sSelector:"h1.tituloSeccion", sFlashSrc:"./sifr/calverMt.swf", sColor:"#392e2c", sWmode: "transparent"}));
//		};
		//]]>


function conteEnrere(){
	$any_limit = 2007;
	$mes_limit = 11;
	$dia_limit = 12;
	
	calculaDiesRestants($any_limit, $mes_limit, $dia_limit);
	setInterval("calculaDiesRestants($any_limit, $mes_limit, $dia_limit)",60000);
}

function calculaDiesRestants(any, mes, dia){
	
	var $array_mes = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var $fecha = new Date();
	
	var $fecha_y = $fecha.getYear();
	var $fecha_m = $fecha.getMonth();
	var $fecha_d = $fecha.getDate();
	
	if ($fecha_y < 1000)		$fecha_y += 1900;	
	
	var $cadena_hoy = $array_mes[$fecha_m]+" "+$fecha_d+", "+$fecha_y;
	var $cadena_fut = $array_mes[mes-1]+" "+dia+", "+any;
	
	var $restan=(Math.round((Date.parse($cadena_fut)-Date.parse($cadena_hoy))/(24*60*60*1000))*1);
	
	if($restan){
		if(document.getElementById("dias")) document.getElementById("dias").innerHTML = $restan;
	}
	
}

function MM_preloadImages() {
 	var d=document; 
 	if(d.images){ 
 		if(!d.MM_p) d.MM_p=new Array();
   		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
   		for(i=0;i<a.length; i++)
	   		if (a[i].indexOf("#")!=0){ 
	   			d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
	   		}
   	}
}


// Funcio per controlar si te el plugin de flash instalat o no
function checkFlash(){
	
	var flashinstalled = 0;
	var flashversion = 0;
	MSDetect = "false";
	if (navigator.plugins && navigator.plugins.length)
	{
		x = navigator.plugins["Shockwave Flash"];
		if (x)
		{
			flashinstalled = 2;
			if (x.description)
			{
				y = x.description;
				flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
		else
			flashinstalled = 1;
		if (navigator.plugins["Shockwave Flash 2.0"])
		{
			flashinstalled = 2;
			flashversion = 2;
		}
	}
	else if (navigator.mimeTypes && navigator.mimeTypes.length)
	{
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin)
			flashinstalled = 2;
		else
			flashinstalled = 1;
	}
	else
		MSDetect = "true";
		
		
	// IE flash detection.
	flashVersionIe = 0;
	for(var i=10; i>0; i--){
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
			flashVersionIe = i;
			flashinstalled=2;
		}
		catch(e){
		}
	}
		
	return flashinstalled;
	
}

function getElementsByAttribute(strTagName, strAttributeName, strAttributeValue){
    var oElm = document;
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}