 
var GB_ROOT_DIR = "http://www.tarragona21.cat/js/greybox/"; 

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://ads.tarragona21.cat/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 mobil(){
   if(screen.width <= 800){
      location.replace('http://www.tarragona21.cat/index.php?command=transform&style=error-mobile') 
       }          
    }
    
function aproved_mobile(){
   if(screen.width <= 800){
      location.replace('http://www.tarragona21.cat/index.php?command=transform&style=commend_aproved-mobile') 
       }          
    }
    
function denied_mobile(){
   if(screen.width <= 800){
      location.replace('http://www.tarragona21.cat/index.php?command=transform&style=commend_denied-mobile') 
       }          
    }

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 validaContact()
{
    var n=true;
    
    n=validateCheckbox('avis','errorName','Heu d\'acceptar l\'av\xEDs legal');
    
    if (n!=false) n=true;

    if (n)
    {
        $('contact-form').submit();
    }
}

function validateCheckbox(id,msgArea,alertText)
{
    field = $(id);
    
    if(field.checked!=true)
    {
        showMsgBox(msgArea, alertText);
        // change the color of text field
       field.addClassName('fieldError');
        field.focus();
        // make sure the form is not submitted
        return false;
    }
    else 
    {
        field.removeClassName('fieldError');
        $(msgArea).hide();
        return true;
    }
}

function showMsgBox (msgArea, msgText)
{
    $(msgArea).style.display='block';
    
    $(msgArea).update(msgText);
}



function validaNewsletter() { 
    var e=true;
        
        e=validateEmail('nlemail', 'error-newsletter','msg-newsletter','L\'adre\xE7a indicada no \xE9s correcta', 'Si us plau, indiqui el seu e-mail');    
   
    if (e!=false) e=true;
        
    if (e) {
        document.getElementById('newsletter').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="#f2f2f2";
    }
    else {
        displayArea.style.backgroundColor="#f2f2f2";    
    }
    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 validaEnviar()
{
    var n=true;
    var e2=true;
    
    var emissor = document.getElementById('email1').value; 
    
     if (emissor==''){
     document.getElementById('email1').value=":";
     }
    
    
    n=validateField('name','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');  
    
    if (n!=false) n=true;
    if (e2!=false) e2=true;
 
    
    if (n && e2) 
    {
        document.getElementById('contact-form').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','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) 
    {
        stripHTML(document.getElementById('comment'));
        document.getElementById('formsend').submit();
    }                 
}

function stripHTML(){
    var re = /(<([^>]+)>)/gi;
    for (i=0; i<arguments.length; i++)
    arguments[i].value=arguments[i].value.replace(re, "");
}


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_field (f)
{
    var field = trim(f);
    if (field.length == 0) {
        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 trim(str)
{
    return str.replace(/^\s*|\s*$/g,"");
}

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 deleteSearch ()
{   
    var search = document.getElementById('match').value;
    
     if (search=='Cercar not\xEDcies') {
     document.getElementById('match').value='';
    }		
}

function deleteContentTitle (){  
    var contentName = document.getElementById('title').value;
    
    if (contentName=='T\xEDtol') {
     document.getElementById('title').value='';
    }       
	}


function deleteContentName (){   
    var contentName = document.getElementById('name').value;
    
    if (contentName=='Nom') {
     document.getElementById('name').value='';
    }       
	}
	
function deleteContentEmail (){   
    var contentEmail = document.getElementById('email').value;
    
     if (contentEmail=='e-mail') {
     document.getElementById('email').value='';
    }		
	}
	
function deleteNewsletEmail (){   
    var contentEmail = document.getElementById('nlemail').value;
    
     if (contentEmail=='e-mail') {
     document.getElementById('nlemail').value='';
    }		
	}
	

// Incrustar video  
function add_video2(url, root_video,width,height)
{
    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);
   
}

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);
}


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 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 printDate() 
{
    Fecha = new Date();
    $('date').update(Fecha.getDate()+" "+getMonth(Fecha.getMonth())+' de '+Fecha.getFullYear());   
}

function getMonth(id) 
{
    switch (id) {
        case 0 : mes="de gener"; break;
        case 1 : mes="de febrer"; break; 
        case 2 : mes="de mar\xE7"; break; 
        case 3 : mes="d'abril"; break; 
        case 4 : mes="de maig"; break; 
        case 5 : mes="de juny"; break; 
        case 6 : mes="de juliol"; break; 
        case 7 : mes="d'agost"; break; 
        case 8 : mes="de setembre"; break; 
        case 9 : mes="d'octubre"; break; 
        case 10 : mes="de novembre"; break;
        case 11 : mes="de desembre"; break; 
    }
    return mes;
}

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) {
    //            Effect.Fade(element[index-1]);
              element[index-1].style.display='none';
            }
        //    Effect.Appear(element[index],{delay:1});
        element[index].style.display='block';
            if (completed) {
//                Effect.Fade(element[size-1]);
            element[size-1].style.display='none';
            }
            setTimeout(call,6000,size);
        });
    }
    

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

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

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

function displayPollList ()
{
	new Ajax.Updater({ success: 'poll-results', failure: 'poll-results' },'http://www.tarragona21.cat/pollphp/db/other_polls.php', {
					 method: 'get',
		             parameters: {action: 'results'}	
					 });
}


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 viewRules(){ 

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


function displayHeadlines() 
    {    
        var news = $$('#marquee a');
        var vilaweb = $$('#ticker_vila a');
        
        if (news.length>0)
        {
            var call = execute (news,0,news.length);
            setTimeout(call,100);    
        }
        
        if (vilaweb.length>0)
        {
            var call2 = execute (vilaweb,0,vilaweb.length);
            setTimeout(call2,100);    
        }
        
        
    }


 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", "");
                    }
                }
            }
        }
    } 

window.onload=function() {
    fontsize(cookieGrab('fontSize'));
 };
 
function fontsize(v) { 
    if(v) {
        if (document.getElementById('block-1')){
            $('block-1').style.fontSize=v+'px';
            cookieStab('fontSize',v);
        }
    }
   
} 

function cookieGrab(f) {
    var c=document.cookie.split(';'); 
    f=f+"=";
    
for (var i = 0;i<c.length;i++) {
  var v=trim2(c[i]);  
  
  if(v.indexOf(f)==0) {       
        return(v.substr(f.length,v.length)); 
   } 
}

}

function trim2(v) { 
    return(v.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,""));
     }

function cookieStab(f,v) {
    miliseconds=parseInt(365*24*60*60*1000);
    var date = new Date();  
    
    time=date.getTime();
    total=date.setTime(parseInt(time+miliseconds));
      
    document.cookie=f+'='+v+'; path=/'+'; expires=' + date.toGMTString();
 } 
     
function load()
{
    addEvent(window, 'load', startList);
    addEvent(window, 'load', printDate);
    addEvent(window, 'load', displayHeadlines);
    addEvent(window, 'load', loadVideos);
}
    
load()