//changeClassName change the class of the element given a className
//in: element: Element
//    className: name of the new class
function changeClass(className,element){
	element.className = className;
	element.style.className = className;
}


//Function to dinamically load the javascript file passed by parameter
//in: file: JS file to load
var jsfilesadded=""; //list of js files already added
var cssfilesadded=""; //list of css files already added

function loadJS(file){
	if (jsfilesadded.indexOf("["+file+"]")==-1){
  		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = file;
		document.getElementsByTagName('head')[0].appendChild(script);
  		
  		jsfilesadded+="["+file+"]"; //List of js files added in the form "[filename1],[filename2],etc"
  		return true;
 	}
 	
 	return false;
}

function loadCSS(file) {
	if (cssfilesadded.indexOf("["+file+"]")==-1){
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
	  	fileref.setAttribute("type", "text/css");
	  	fileref.setAttribute("href", file);
		document.getElementsByTagName("head")[0].appendChild(fileref);
		
		cssfilesadded+="["+file+"]"; //List of css files added in the form "[filename1],[filename2],etc"
		return true;
	}
	
	return false;
}

function loadModules(){
	//load third party libs
	loadJS("./js/otherlibs/prototype.js");
	loadJS("./js/otherlibs/rico.js");
	loadJS("./js/otherlibs/ricoComponents.js");
	loadJS("./js/otherlibs/ricoEffects.js");
	loadJS("./js/otherlibs/ricoCommon.js");
	loadJS("./js/otherlibs/ricoBehaviors.js");
	loadJS("./js/otherlibs/ricoStyles.js");
	loadJS("./js/otherlibs/yui_2.4.1/build/yahoo-dom-event/yahoo-dom-event.js");
	loadJS("./js/otherlibs/yui_2.4.1/build/animation/animation-min.js");
	//load own libs
	loadJS("./js/gmlibs/process_xml.js");
	loadJS("./js/gmlibs/utils.js");
	loadJS("./js/gmlibs/behaviours_all_news_circular.js");
	loadJS("./js/gmlibs/ajaxCalls.js");
}

function removeAllNodeChilds(node){
	if (node && node.hasChildNodes && node.removeChild) {
		while (node.hasChildNodes()) {
			node.removeChild(node.firstChild);
		}
	}	
}

function getLanguageContentFromFieldSetArray(langFields){
		//get language content 
		var content = '<langFields>';
		var i=0;			
		for(i=0 ; i<langFields.length ; i++){
			content = content + langFields[i].getXMLFieldValues();
		} 			
		content = content + '</langFields>';	
		
		return content;
}

function xmlLanguageResponseToArray(doc){
	var elements = doc.getElementsByTagName('language');
	var resultArray = new Array(elements.length);
	var i = 0;
	for( i = 0 ; i < elements.length; i++){
		//get the id and the description of the language
		var id = findAttributValueInNode(elements[i],"id");
		//var id = elements[i].getElementsByTagName('id');
		var description = elements[i].getElementsByTagName('name');
		
		if(description.length>0){
			description = findDirectTextNodeValue(description[0]);
			//id = findDirectTextNodeValue(id[0]);
			resultArray[i]={id: id, description: description};			
		}else{
			return null;
		}
	}
	return resultArray;	
}

/**
 * Find tag <failure> into the xml result, and if found returns the value
 */
function lookForXMLFailureResponse(doc){
	var elements = doc.getElementsByTagName('failure');
	if(elements.length > 0){
		var firstChild = elements[0].firstChild;
		if(firstChild != null){
			return firstChild.data;
		}else{
			return "no error info";
		}
	}else{
		return null;
	}
}

function getResponseText(doc){
	var elements = doc.getElementsByTagName('done');
	if(elements.length > 0){
		var firstChild = elements[0].firstChild;
		if(firstChild != null){
			return firstChild.data;
		}else{
			return "no result info";
		}
	}else{
		return null;
	}
}

function showInfoDialog(title, message){
    Ext.Msg.show({
        title: title,
        msg: message ,
        buttons: Ext.Msg.OK,
        icon: Ext.MessageBox.INFO
    });	
}

function showErrorDialog(title, message){
    Ext.Msg.show({
        title: title,
        msg: message ,
        buttons: Ext.Msg.OK,
        icon: Ext.MessageBox.ERROR
    });	
}

function ajaxGenericCall(url,callback,scope,params)
{
	var encodedURL = encodeURIComponent(url);
	//alert(encodedURL);
	//var encodedURL = url;
	//alert(encodedURL);
	Ext.Ajax.request({ 
		url: url,							
        success: function(conn, response, options){
				callback(conn, response, options);
        	},
		failure: function(){
				showErrorDialog('Error','Could not connect to server');
			},
		scope: scope,
		params: params
	});
}

/**
 * This function will unselect any selected text in the page
 */
function unselectAnyText(){
	moz = document.getElementById && !document.all;
	if (!moz) {
		oTextRange = document.selection.createRange()
		oTextRange.expand("word")
		oTextRange.execCommand("unselect")
	}
	else {
		oTextRange = window.getSelection()
		oTextRange.collapseToStart()
	}
}

//javascript replace all implementation
function replaceAll(str, strSrc, strDst){
	var intIndexOfMatch = str.indexOf(strSrc);
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1) {
		str = str.replace(strSrc, strDst);
		intIndexOfMatch = str.indexOf(strSrc);
	}	
	return str;	
}

function aleatorio(inferior,superior){
	numPosibilidades = superior - inferior
	aleat = Math.random() * numPosibilidades
	aleat = Math.round(aleat)
	return parseInt(inferior) + aleat
} 

