
/* FUNCTIONS SECTION */

// classificacions
function init() 
{
	if (document.getElementById('infocolumn')!=null) {
		
		Element.cleanWhitespace('infocolumn');
		
		var stretchers = document.getElementsByClassName('box');
		var toggles = document.getElementsByClassName('tab');
		var myAccordion = new fx.Accordion(
			toggles, stretchers, {opacity: false, height: true, duration: 600}
		);
		//hash functions
			found = false;
			toggles.each(function(h3, i){			var div = Element.find(h3, 'nextSibling');				if (window.location.href.indexOf(h3.title) > 0) {					myAccordion.showThisHideOpen(div);					found = true;				}			});			if (!found) myAccordion.showThisHideOpen(stretchers[0]);
	}		
}



//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() 
{
	 if(window.ActiveXObject){
	                return new ActiveXObject("Microsoft.XMLHTTP"); //IE		
	} else if (window.XMLHttpRequest) {
	                return new XMLHttpRequest(); //Not IE
	} else {
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}	
	
//Get our browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();		
//Initiate the asyncronous request.

//Called every time our XmlHttpRequest objects state changes.
function handleResult() 
{
	if (receiveReq.readyState == 4) {
		document.getElementById('comments_content').innerHTML = receiveReq.responseText;	
		
	}
}


function handleResultComments() 
{
	if (receiveReq.readyState == 4) {
		document.getElementById('last_comments').innerHTML = receiveReq.responseText;	
		
	}
}
function getNews(html) 
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", html, true);
		receiveReq.onreadystatechange = handleResult; 
		receiveReq.send(null);
	}			
}

function getLastComments(html) 
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", html, true);
		receiveReq.onreadystatechange = handleResultComments; 
		receiveReq.send(null);
	}			
}

function loadLastComments()
{
	if ($("last_comments"))
	{
		getLastComments("last-comments.php");
	}
}

function loadComments(id)
{
	if ($("comments_content"))
	{
		getNews("index.php?command=view_comments&news_id="+id);
	}
}

/*
* 	Validacions de formularis
*/

function validaEnviar() 
{
    var e=true;
    var n=true;

    e=validateEmail('email', 'msgerror','message','L\'e-mail indicat no \351s v\340lid','Si us plau, indica el teu e-mail');
    n=validateField('name', 'msgerror','message','Si us plau, indica el teu nom', 'Si us plau, indica el teu nom');    
    
    if (e!=false) e=true;
    if (n!=false) n=true;
            
    if (e && n) 
    {
        $('formenviar').submit();
    }                 
}    
function validaComentari() 
{
    var e=true;
    var n=true;

    e=validateEmail('e-mail', 'msgerror','message','L\'e-mail indicat no \351s v\340lid','Si us plau, indica el teu e-mail');
    n=validateField('nom', 'msgerror','message','Si us plau, indica el teu nom', 'Si us plau, indica el teu nom');    
    
    if (e!=false) e=true;
    if (n!=false) n=true;
            
    if (e && n) 
    {
        $('formenviar').submit();
    }                 
}



/* ============ funcions auxiliars =========== */

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 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 validateDynamicFields(template, msgArea,msg,alertErrorText, alertVoidText)
{
    var areValid = true;
    var noFields = true;
   	for (var i=1;i<100;i++) {
        field = document.getElementById(template+i);
        if (field != null) {
            noFields = false;
            if(!check_field(field.value)){
                areValid = false;
                // change the color of text field
                field.style.borderColor = "red";
                field.focus(); 
            }
        }
    }
    if (noFields) {
        showMsg (msgArea,msg,alertVoidText,false);
        return false;
    }
    if (!areValid) {
        showMsg (msgArea,msg,alertErrorText,true);
        // make sure the form is not submitted
        return false;    
    }
}

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

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 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 trim(str)
{
    return str.replace(/^\s*|\s*$/g,"");
}

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

	
// Show video youtube

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);
    $(root_video).innerHTML=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 loadVideo() {
	var video = $('url_video_1');
	if ($('video-width')!=null && $('video-height')!=null) {        var width=$('video-width').value;        var height=$('video-height').value;    }
    
    if(video!=null) {
	    var videoLink = video.value.substring(video.value.lastIndexOf("=")+1);
		var videoLink2 = "http://www.youtube.com/v/"+videoLink;
	    add_video(videoLink2,'root_video_1',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;         } }

/* INIT SECTION */

addEvent(window, 'load', init);
addEvent(window, 'load', loadLastComments);
addEvent(window, 'load', loadVideo);
