
var GB_ROOT_DIR = "http://www.tarragonamesborrona.info/js/greybox/"; 

function displayPoll ()
{
	new Ajax.Updater({ success: 'poll', failure: 'poll' },'display-poll.php', {
					 method: 'get',
					 evalScripts: false					 
                    });
}

function displayPollResults (pollId)
{
	new Ajax.Updater({ success: 'poll-results', failure: 'poll-results' },'display-poll.php', {
					 method: 'get',
		             parameters: {action: 'results', poll_ident: pollId}	
					 });
}

function displayComments (newsId){   
    var aleatorio = Math.random() 
    

	new Ajax.Updater({ success: 'comments', failure: 'comments' }, 'http://www.tarragonamesborrona.info/index.php', {
					 method: 'get',
					 evalScripts: true,
					 encoding: 'iso-8859-1',
     				 parameters: {command: 'view_comments', news_id: newsId, aleator: aleatorio}	 
                    }); 
}

function fbs_click() {
            u=location.href;
            t=document.title;
            
            window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
            
            return false;
            }

function viewRules(){ 

 if ($('rules').style.display=='block') {
        $('rules').style.display='none';    
        }
        else{
        $('rules').style.display='block';    
        }    
   }
   
function view_issues()
{

    var id_issue=$('anios').value;      
    var fechas=document.getElementsByClassName('date_issue');    
    var counter=0;
    
    for (var index = 0; index < fechas.length; ++index) {
      var item = fechas[index];
      
       if (id_issue==item.value)
        {
        
          item.parentNode.style.display='block';
          counter++;
        }
        else 
        {
         item.parentNode.style.display='none';
        }
    }   
   }


/*function validaComentari(){   

    var n=true;
    var e=true;
    var t=true;
    
     
    t=validateField('title','msgerror','message','Si us plau, indiqueu un t\xEDtol', 'Si us plau, indiqueu un t\xEDtol');    
    n=validateField('name','msgerror','message','Si us plau, indiqueu el vostre nom', 'Si us plau, indiqueu el vostre nom');       
    e=validateEmail('email','msgerror','message','L\'adre\xE7a indicada no \xE9s correcta', 'L\'adre\xE7a indicada no \xE9s correcta'); 
    
    
    if (t!=false) t=true;
    if (n!=false) n=true;
    if (e!=false) e=true;
 
    
    if (t && n && e) 
    {
        document.getElementById('formsend').submit();
    }                 
}*/

function deleteNewsletEmail (){   
    var contentEmail = document.getElementById('nlemail').value;
    
     if (contentEmail=='e-mail') {
     document.getElementById('nlemail').value='';
    }		
	}


function openTargetBlank(e){
   
   var className = 'external';
   
   if (!e) var e = window.event;
   var clickedObj = e.target ? e.target : e.srcElement;
   
   if(clickedObj.nodeName == 'A' )
    {
      r=new RegExp("(^| )"+className+"($| )");
      if(r.test(clickedObj.className)){
         window.open(clickedObj.href);
         return false;
   
      }
    }
}

function execute(element,index,size) {
    return (function(){
        completed = false;
        if (index >= size) {
            index = 0;
            completed = true;
        }
        var call = execute (element,index+1,size);
        if (index > 0) {
            element[index-1].style.display='none';
        }
        element[index].style.display='block';
        if (completed) {
            element[size-1].style.display='none';
        }
        setTimeout(call,10000,size);
    });
}

/*function displayHeadlines () 
{    
    var news = document.getElementsByClassName("quadre-sup");
    var call = execute (news,0,news.length);
    setTimeout(call,100);
}*/


function banner(zone)
{
    if (!document.phpAds_used) document.phpAds_used = ',';
    phpAds_random = new String (Math.random()); phpAds_random = phpAds_random.substring(2,11);
   
    document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
    document.write ("http://www.tarragonamesborrona.info/openx/adjs.php?n=" + phpAds_random);
    document.write ("&amp;what=zone:"+zone);
    document.write ("&amp;exclude=" + document.phpAds_used);
    if (document.referrer)
       document.write ("&amp;referer=" + escape(document.referrer));
    document.write ("'><" + "/script>");
}

function validaNewsletterEs() {
    var e=true;
    
    e=validateEmail('nlemail', 'error-newsletter','msg-newsletter','adre\xE7a incorrecta', 'Indiqueu el e-mail');
   
    if (e!=false) e=true;
        
    if (e) {
        document.getElementById('subscribe').submit();
    }                 
} 

function validateEmail(id,msgArea,msg,alertText)
{
    // check the first email address ( the exclamation means "not" )
    field = document.getElementById (id);
    if(!check_email(field.value)){
        showMsg (msgArea,msg,alertText,true);
        // change the color of text field
        field.style.borderColor = "red";
        field.focus(); 
        // make sure the form is not submitted
        return false;
    }
    else {
        return true;
    }
}



function showMsg (msgArea, msg, msgText,isError)
{
    displayArea = document.getElementById (msgArea);
    displayElement =  document.getElementById (msg);
    displayArea.style.display = "";
    if (isError) {
        displayArea.style.backgroundColor="#ff6666";
    }
    else {
        displayArea.style.backgroundColor="#FFFF99";    
    }
    if (!isDOMCompliant) {
        alert (msgText);
    }
    else {
        
        changeElementText (displayElement,msgText);
        //document.location.href="#top";    
    }
}

function changeElementText (node,text) 
{
    while (node.hasChildNodes()) {
        node.removeChild(node.lastChild);
    }
    var textNode = document.createTextNode(text);
    node.appendChild(textNode );
}


function isDOMCompliant ()
{
        return (document.getElementsByTagName && document.createElement);
}

function check_email (emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}

function sendSome()
{
    var usuario = "fes-montjuic";
    var dominio = "xtec";
    var extension = "cat";
    var subject = "Info";

    emailCod=(usuario + '@' + dominio + '.' + extension + "?" + "subject" + "=" + subject);
    emailLabel=(usuario + '@' + dominio + '.' + extension);
    document.write('<a href="mailto:' + emailCod + '">' + emailLabel + '</a>');
}


function validaEnviarEs()
{
    var n=true;
    var e2=true;    
    var m=true;
    
    n=validateField('nom1','msgerror','message','Si us plau, indiqueu el vostre nom', 'Si us plau, indiqueu el vostre nom');
    e2=validateEmail('email2', 'msgerror','message','L\'adre\xE7a indicada no \xE9s correcta', 'Si us plau, indiqueu el vostre e-mail');
    m=validateTextarea('comentari', 'msgerror','message','Si us plau, escriviu un missatge', 'Si us plau, escriviu un missatge');    
    
    if (n!=false) n=true;
    if (e2!=false) e2=true;
    if (m!=false) m=true;
 
    
    if (n && e2 && m) 
    {
        document.getElementById('formenviar').submit();
    }                 
}


function validaEnviarContactEs()
{
    var n=true;
    var e1=true;  
    var m=true;
    
    n=validateField('nom1','msgerror','message','Si us plau, indiqueu el vostre nom', 'Si us plau, indiqueu el vostre nom');
    e1=validateEmail('email1', 'msgerror','message','L\'adre\xE7a indicada no \xE9s correcta', 'Si us plau, indiqueu el vostre e-mail');
    m=validateTextarea('comentari', 'msgerror','message','Si us plau, escriviu un missatge', 'Si us plau, escriviu un missatge');    
    
    if (n!=false) n=true;
    if (e1!=false) e1=true;
    if (m!=false) m=true;
 
    
    if (n && e1 && m) 
    {
        document.getElementById('formenviar').submit();
    }                 

}

function validaComentari(){   

    var n=true;
    var e=true;
    var t=true;
    
     
     t=validateField('title','msgerror','message','Si us plau, indiqueu un t\xEDtol', 'Si us plau, indiqueu un t\xEDtol');    
     n=validateField('name','msgerror','message','Si us plau, indiqueu el vostre nom', 'Si us plau, indiqueu el vostre nom');       
     e=validateEmail('email-com', 'msgerror','message','L\'adre\xE7a indicada no \xE9s correcta', 'Si us plau, indiqueu el vostre e-mail');
    
    
     if (t!=false) t=true;
     if (e!=false) e=true;
    if (n!=false) n=true;
 
    
    if (t && n && e) 
    {
        document.getElementById('formsend').submit();
    }                 
}


function validateField(id,msgArea,msg,alertText,isError)
{

        field = document.getElementById(id);
    
        if(!check_field(field.value)){
        showMsg(msgArea, msg, alertText,isError);
        // change the color of text field
        field.style.borderColor = "red";
        field.focus();
        // make sure the form is not submitted
        return false;
    }
    else {
        return true;
    }
}


function validateTextarea(id,msgArea,msg,alertText,isError)
{
    field = document.getElementById(id);
    if(!check_texta(field.value)){
    showMsg(msgArea, msg, alertText,isError);
    // change the color of text field
    field.style.borderColor = "red";
    field.focus();
    // make sure the form is not submitted
    return false;
    }
    else {
        return true;
    }
}

function check_texta(f)
{
    if (f) {
        var field = trim(f);
        if (field.length == 0) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}




function check_field (f)
{
    var field = trim(f);
    if (field.length == 0) {
        return false;
    }
    else {
        return true;
    }
} 

function trim(str)
{
    return str.replace(/^\s*|\s*$/g,"");
}


// prototype.js needed 

function montre(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {
		document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}


function startList(){
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


function printDate() {
    Fecha = new Date();
    document.write(Fecha.getDate()+' de ');
    document.write(getMes(Fecha.getMonth())+' de ');
    document.write(Fecha.getFullYear());
}

function startTime()
{
    var today=new Date()
    var h=today.getHours()
    var m=today.getMinutes()
    var s=today.getSeconds()
    // add a zero in front of numbers<10
    m=checkTime(m)
    s=checkTime(s)
    document.getElementById('hora').innerHTML=h+":"+m+":"+s
    t=setTimeout('startTime()',500)
}

function checkTime(i)
{
    if (i<10) 
      {i="0" + i}
      return i
}

function busca_ejemplar() 
{
    var fecha=document.getElementById('sel1').value;
    if (fecha!='')
    {
        var seccion=document.getElementById('seccion').value.split("_");
        var ejemplar = issues[seccion[0]+"_"+fecha];
        if (ejemplar == null) {
            alert ("Ho sentim. Actualmene no hi ha disponible cap not\xEDcia a la secci\363 i data seleccionades");
        } else {
            document.getElementById('form_hemeroteca').action='index.php?command=show_archive_summary&section_id='+seccion[1]   +'&issue_id='+ejemplar;
            document.getElementById('form_hemeroteca').submit();
        }
    } 
    else 
    {
        document.getElementById('not_date').style.display='inline';
        document.getElementById('cal-button-1').style.border='1px solid #c00';
    }
}

function show_years()
{
    var nodes=document.getElementsByName('exemplar');
    for (var i=0; i<nodes.length; i++)
    {
        nodes[i].style.display='none';
    }
}

function show_months(className, id_publication, year)
{
    var elements=document.getElementsByClassName(className);
    for (i=0; i<elements.length;i++)
    {
        elements[i].style.display='none';
    }
    $('block-mesos-'+id_publication+'-'+year).style.display='block';    
}

function show_exemplar(className, id_publication, year, month)
{
    show_years();

    var visibles=document.getElementsByClassName('exemplar-'+year+'-'+month);    

    for (k=0; k<visibles.length;k++)
    {
        visibles[k].style.display='block';
    }
}

function DOMToString(doc) 
{
	var e = doc.firstChild;
	return ElementToString(e);
}

function ElementToString(e) 
{
	 var attr = "";
	 var cont = "";
	 if (e.hasChildNodes == false) return "<"+e.nodeName+"\/>";
	 
	 for (var i=0;i<e.childNodes.length;i++) {
	     var c = e.childNodes.item(i);
	     if (c.nodeType=="2") {
	     	if(c.nodeValue != null && c.nodeValue != "" && c.nodeValue != "inherit") 
	     		attr += " "+c.nodeName+"=\""+c.nodeValue+"\" ";
	     }
	     else if (c.nodeType=="1") cont+=ElementToString(c);
	     else if (c.nodeType=="3") cont+=c.nodeValue;                              
	 }
	 
	 var a = e.attributes;
	 for (var i=0;i<a.length;i++) {
	     if(a[i].nodeValue != null && a[i].nodeValue != "" && c.nodeValue != "inherit") 
	     	attr += " "+a[i].nodeName+"=\""+a[i].nodeValue+"\" ";
	 }
	 
	 return "<"+e.nodeName+attr+">"+cont+"<\/"+e.nodeName+">";
}

function create_param(node,src,value)
{
    var param=node.createElement('param');
        param.setAttribute("value", value);
        param.setAttribute("name",src);
        return param;
}

function create_object(node, url)
{
        var object=node.createElement('object');

        object.type="video/quicktime";
        object.data=url;
        object.height="300";
        object.width="320";
        object.className="iesucks";
        alert("object atribute");
        object.appendChild(create_param(node, "autoplay","false"));
        alert("object atribute2");
        object.appendChild(create_param(node, "controller","true"));
         alert("object end");
    return object;
}

function add_video(url, root_video, link, link_close)
{
if(navigator.appName=="Microsoft Internet Explorer")
{
    var text="<embed src=\""+url+"\"width=\"300\" height=\"320\" autoplay=\"false\">"
    +"<param name=\"cache\" value=\"true\">"
    +"<param name=\"type\" value=\"video/quicktime\">"
    +"<param name=\"bgcolor\" value=\"#FFFFFF\">"
    +"<param name=\"controller\" value=\"true\">"
    +"<param name=\"showcontrols\" value=\"1\"/>"
    +"<param name=\"enablejavascript\" value=\"TRUE\">"
    +"<param name=\"showdisplay\" value=\"1\">"
    +"<param name=\"qtnext1\" value=\"javascript:GAMIndexAds.endMovie(GAMIndexAds.oCurrentMovie);\">"
    +"</embed>";

} else {
    var text="<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0\" id=\"movie\" height=\"300\" width=\"320\">"
    +"<param name=\"src\" value=\""+url+"\">"
    +"<param name=\"autoplay\" value=\"false\">"
    +"<param name=\"cache\" value=\"true\">"
    +"<param name=\"type\" value=\"video/quicktime\">"
    +"<param name=\"bgcolor\" value=\"#FFFFFF\">"
    +"<param name=\"controller\" value=\"true\">"
    +"<param name=\"showcontrols\" value=\"1\"/>"
    +"<param name=\"enablejavascript\" value=\"TRUE\">"
    +"<param name=\"showdisplay\" value=\"1\">"
    +"<param name=\"qtnext1\" value=\"javascript:GAMIndexAds.endMovie(GAMIndexAds.oCurrentMovie);\">"
    +"<object type=\"video/quicktime\" data=\""+url+"\" height=\"300\" width=\"320\" class=\"iesucks\">"
    +"<param name=\"autoplay\" value=\"false\"/>"
    +"<param name=\"controller\" value=\"true\"/>"
    +"<param name=\"showcontrols\" value=\"1\"/>"
    +"<param name=\"cache\" value=\"true\">"
    +"</object>"
    +"</object>";
}

     document.getElementById(root_video).innerHTML=text;
     document.getElementById(link).style.display='none';
     document.getElementById(link_close).style.display='block';

}

function remove_video(root_video, link, link_close)
{
     document.getElementById(root_video).innerHTML="";
     document.getElementById(link).style.display='block';
     document.getElementById(link_close).style.display='none';
}

function add_audio(url, root_audio, link, link_close)
{
if(navigator.appName=="Microsoft Internet Explorer")
{
  var text="<embed src=\""+url+"\"width=\"200\" height=\"30\" autoplay=\"false\" cache=\"true\" type=\"video/quicktime\">"
    +"<param name=\"enablejavascript\" value=\"TRUE\">"
    +"</embed>";
}
else
{
    var text="<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0\" id=\"movie\" height=\"30\" width=\"200\">"
    +"<param name=\"src\" value=\""+url+"\">"
    +"<param name=\"autoplay\" value=\"false\">"
    +"<param name=\"cache\" value=\"TRUE\">"
    +"<param name=\"type\" value=\"video/quicktime\">"
    +"<param name=\"bgcolor\" value=\"#FFFFFF\">"
    +"<param name=\"controller\" value=\"TRUE\">"
    +"<param name=\"enablejavascript\" value=\"TRUE\">"
    +"<param name=\"qtnext1\" value=\"javascript:GAMIndexAds.endMovie(GAMIndexAds.oCurrentMovie);\">"
    +"<object type=\"video/quicktime\" data=\""+url+"\" height=\"30\" width=\"200\" class=\"iesucks\">"
    +"<param name=\"autoplay\" value=\"false\"/>"
    +"<param name=\"controller\" value=\"true\"/>"
    +"</object>"
    +"</object>";
}
     document.getElementById(root_audio).innerHTML=text;
     document.getElementById(link).style.display='none';
     document.getElementById(link_close).style.display='block';
}
function remove_audio(root_audio, link, link_close)
{
     document.getElementById(root_audio).innerHTML="";
     document.getElementById(link).style.display='block';
     document.getElementById(link_close).style.display='none';
}

function new_human_date()
{
    var string=$('sel1').value;
    var ano=string.substring(0,4);
    var mes=string.substring(5,7);
    var dia=string.substring(8,10);
    if (dia.substring(0,1)=='0')
    {
        dia=dia.substring(1,2);
    }
    var mesTexto="";
    
    switch (mes) {
        case '01' : mesTexto="enero"; break;
        case '02' : mesTexto="febrero"; break;        
        case '03' : mesTexto="marzo"; break;
        case '04' : mesTexto="abril"; break;
        case '05' : mesTexto="mayo"; break;
        case '06' : mesTexto="junio"; break;
        case '07' : mesTexto="julio"; break;
        case '08' : mesTexto="agosto"; break;
        case '09' : mesTexto="septiembre"; break;
        case '10' : mesTexto="octubre"; break;
        case '11' : mesTexto="noviembre"; break;
        case '12' : mesTexto="diciembre"; 
    }
    
    changeElementText('human_date',dia+" de "+mesTexto+" de "+ano);
}


function view_section()
{
    var publicacions=$('publicaciones').value;
    $('secciones-8').style.display='none';
    $('secciones-9').style.display='none';
    $('secciones-10').style.display='none';
    $('secciones-11').style.display='none';
    if (publicacions!=0)
    {
        $('secciones-'+publicacions).style.display='block';
    }
    
    var options0=document.getElementsByClassName('section-0');

    options0.each(function(i){
        i.selected='selected';
    });    
}

function view_anyMes()
{
    var index = $('anios').selectedIndex;
    var any = $('anios').options[index].value 
    var mes=$('meses').value;   
    
    if (!$(any+"-"+mes)) {
        $('no_issues').style.display='block';
       
    } else {
        var issueId = $(any+"-"+mes).value;                
          
        location.href="http://www.tarragonamesborrona.info/index.php?command=show_publication&publication_id=1&issue_id="+issueId;
    }    
}




/*function view_issues()
{
    var id_issue=$('anios').value+"-"+$('meses').value+"-"+$('actual_section').value;
    var fechas=document.getElementsByClassName('date_issue');
    var counter=0;
    
    fechas.each(function(i)
    {
        if (id_issue==i.value)
        {
          i.parentNode.style.display='block';
          counter++;
        }
        else 
        {
         i.parentNode.style.display='none';
        }
    });   

    if (counter==0) {
        $('no_issues').style.display='block';
        $('issues_found').style.display='none';
    } else
    {
        $('no_issues').style.display='none';
        $('issues_found').style.display='block';
    } 
}*/

function hide_issues()
{
    var fechas=document.getElementsByClassName('date_issue');
    fechas.each(function(i)
    {
        i.parentNode.style.display='none';
    });    
}

function hide_months()
{
    $('fila-mes').style.display='none';
}
function hide_years()
{
    $('fila-anios').style.display='none';
}
function view_months()
{
    $('fila-mes').style.display='block';
    $('meses-0').selected='selected';
    
}

function view_years()
{
    $('fila-anios').style.display='block';
    $('anios-0').selected='selected';
}

function change_section(id_select)
{
    var nou = $('select_sections-'+id_select).value;
    $('actual_section').value=nou;
}

load() 

function load()
{
    //addEvent(window, 'load', displayHeadlines);
    addEvent(window, 'load', loadVideos);
}

function addEvent(obj, evType, fn){ 
    if (obj.addEventListener){ 
            obj.addEventListener(evType, fn, false); 
            return true; 
            } else if (obj.attachEvent){ 
            var r = obj.attachEvent("on"+evType, fn); 
            return r; 
            } else { 
            return false; 
        } 
}

function loadVideos()
{
    var videos=$$('.urlvideo');
    if ($('video-width')!=null && $('video-height')!=null)
    {
        var width=$F('video-width');
        var height=$F('video-height');
    }

    for (i=0; i<videos.length;i++)
    {
        add_video(videos[i].value,'root_video_'+videos[i].id.substring(10),width,height);
    }
}

function add_video(url,root_video,width,height)
{

 if(navigator.appName=="Microsoft Internet Explorer")
    {
        var text="<embed src=\""+url+"\" width=\""+width+"\" height=\""+height+"\" autoplay=\"false\" wmode=\"transparent\">"
        +"<param name=\"cache\" value=\"true\">"
        +"<param name=\"type\" value=\"video/quicktime\">"
        +"<param name=\"bgcolor\" value=\"#FFFFFF\">"
        +"<param name=\"controller\" value=\"true\">"
        +"<param name=\"showcontrols\" value=\"1\"/>"
        +"<param name=\"enablejavascript\" value=\"TRUE\">"
        +"<param name=\"showdisplay\" value=\"1\">"
        +"<param name=\"wmode\" value=\"transparent\"/>"
        +"<param name=\"qtnext1\" value=\"javascript:GAMIndexAds.endMovie(GAMIndexAds.oCurrentMovie);\">"
        +"</embed>";
    
    } else {
        var text="<object width=\""+width+"\" height=\""+height+"\">"
            +"<param name=\"movie\" value=\""+url+"\"/>"
            +"<param name=\"wmode\" value=\"transparent\"/>"
            +"<embed src=\""+url+"\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\""+width+"\" height=\""+height+"\"/>"
            +"</object>";
    }
    $(root_video).update(text);
}



        
        

