


function changeStatus(sEvent, sCVId) 
{
	var xmlObj;
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter("ver_id", sCVId);
	xmlObj = oAjax.throwEventXML(sEvent);
	//this.processXml(oAjax, xmlObj, sCVId);
	return xmlObj;
}
	
function deleteVersion(sCVId) 
{
	//if (!confirm(this.msg+"\n ¿Está seguro de que desea continuar?")) return;
	
	var a_sCVIds = sCVId.split(',');
	for (var i=0; i<= a_sCVIds.length; i++) {
		var xmlObj = changeStatus("cms_delete_version", a_sCVIds[i]);
	}
	
	//if (this.bOK) this.showOk(sCVId);
	//else alert(this.msgNOK);
};




function change_provincia()
{
	if (!browserSupportCookies()){
		alert('Se deben habilitar las cookies en el navegador para poder usar esta funcionalidad');
		return false;
	}
	
	var value = $j('#cbo_user_privincia').val();
	//alert(value);
	
	//if (value == '') return false;
	
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter("user_province", value);
	var xmlObj = oAjax.throwEventXML("latin_set_province");
	if (xmlObj) {
		var sRes = oAjax.getXMLNodeValue(xmlObj, "code");
		if (sRes == "1") {} //alert('Ok');
		else alert('Se a producido un error estableciendo tus preferencias.');
	} else {
		alert('Se a producido un error estableciendo tus preferencias.');
	} 
	
	document.location.href = document.location.href;
}

function change_country()
{
	if (!browserSupportCookies()){
		alert('Se deben habilitar las cookies en el navegador para poder usar esta funcionalidad');
		return false;
	}
	
	var value = $j('#cbo_user_country').val();
	//alert(value);
	
	//if (value == '') return false;
	
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter("user_country", value);
	var xmlObj = oAjax.throwEventXML("latin_set_country");
	if (xmlObj) {
		var sRes = oAjax.getXMLNodeValue(xmlObj, "code");
		if (sRes == "1") {} //alert('Ok');
		else alert('Se a producido un error estableciendo tus preferencias.');
	} else {
		alert('Se a producido un error estableciendo tus preferencias.');
	} 
	
	document.location.href = document.location.href;
}


function browserSupportCookies()
{
	var cookieEnabled=(navigator.cookieEnabled)? true : false

	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	}
	
	return cookieEnabled;
}




/* Compara el array 'date_1' con el objeto Date() 'date_2' segun 'comp' --> '<' o '>'  */
function CompareDates(date_1,date_2,comp) {
	
	if (date_2.length != null){// La fecha 1 es un array y la 2 tambien
		dd = date_2[0];
		mm = date_2[1];
		yy = date_2[2];
	} else { // La fecha 1 es un array y la 2 es de la clase Date()
		dd = date_2.getDate();
		mm = date_2.getMonth()+1;
		yy = date_2.getFullYear();
	}
	
	//alert(date_1[0] + " - " + date_1[1] + " - " + date_1[2]);
	//alert(dd + " - " + mm + " - " + yy);
	switch (comp)
	{
		case '<': 
			if ( date_1[2] > yy ) return false;
			if ( date_1[2] < yy ) return true;
			if ( date_1[1] > mm ) return false;
			if ( date_1[1] < mm ) return true;
			if ( date_1[0] > dd ) return false;
			return true;
			break;
		case '>': 
			if ( date_1[2] > yy ) return true;
			if ( date_1[2] < yy ) return false;
			if ( date_1[1] > mm ) return true;
			if ( date_1[1] < mm ) return false;
			if ( date_1[0] < dd ) return false;
			return true;
			break;
	}
	return false;
}


/* Coge un compo text de una fecha y devuelve un array[3] con el dia mes y anyo  */
function SplitDate(dateField) {		
	if ( dateField != null ) {
		var arrDate = dateField.value.split("/");		
	}
	return arrDate;
}


function cache_reset() {		
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.async = true;
	oAjax.throwEvent2('cache_reset');
}


function check_captcha(input_id)
{
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter("captcha", document.getElementById(input_id).value);
	xmlObj = oAjax.throwEventXML('check_captcha');
	if (xmlObj) {
		var sRes = oAjax.getXMLNodeValue(xmlObj, "result");
		if (sRes == "OK") return true;
	} 
	
	return false;
}





/*
	CATEGORIAS
*/


// añade un option al select
function addOption(selectbox, text, value, bSelected)
{
	/*var optn = document.createElement("OPTION");
	optn.text = text;
	optn.innerHTML = text; // for IE
	optn.value = value;*/
	var optn = new Option(text, value);

	if (is_set(bSelected)) optn.selected = bSelected; else optn.selected = true;
	selectbox.options.add(optn);//, 0);
}



// rellena el combo con el arbol completo de categorias hijas
function fillCategoryAndChildsCombo(sParentCatCode, oElem, Separator, bIsTag, sMainLevel, bCdata) {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter('category_parent_code', sParentCatCode);
	if (bCdata) oAjax.addPostParameter('include_cdata_name', '1');
	
	var xmlObj;
	if(bIsTag) xmlObj = oAjax.throwEventXML('tractes_get_category_list_tags');        
	else xmlObj = oAjax.throwEventXML('tractes_get_category_list');

	if (xmlObj) {
		var a_sValues = new Array();
		var a_sTexts = new Array();
		var a_bIsOptionGroup = new Array();
		if (Separator == undefined) Separator = '';
		
		//alert(sParentCatCode + ' ' + xmlObj.childNodes.length);

		for (var i = 0; i < xmlObj.childNodes.length; i++) {
			var oNode = xmlObj.childNodes[i];
			var cat_value = oNode.childNodes[0].firstChild.data;
			var lvl = '';
			var name = '';
			if (oNode.childNodes[2] && oNode.childNodes[2].firstChild) lvl = oNode.childNodes[2].firstChild.data;
			if (bCdata && false) {
				if (oNode.childNodes[1] && oNode.childNodes[1].childNodes[0] && oNode.childNodes[1].childNodes[0].firstChild) name = oNode.childNodes[1].childNodes[0].firstChild.data;
			} else {
				if (oNode.childNodes[1] && oNode.childNodes[1].firstChild) name = oNode.childNodes[1].firstChild.data;
			}
			//alert(i + ': ' + name);
			if (i > 2 && (i < 5)){
				//alert(i + ': ' + name);
			}
			
			
			var cat_text = this.getCategoryIdent(lvl) + name;
			//var cat_text = name;
			
			//if (oNode.childNodes[1].firstChild != null) {
			if (name != null) {
				a_sValues.push(cat_value);
				a_sTexts.push(cat_text);
				//if(lvl.toString() == sMainLevel) a_bIsOptionGroup.push(true);
				//else a_bIsOptionGroup.push(false);
			}
			else {
				a_sValues.push('VACIO');
				a_sTexts.push('VACIO');
			}
		}

		// rellena el combo
		if(!bIsTag)
		{
			var current_group = null;
			for (var i = 0; i < a_sValues.length; i++)
			{
				/*if((a_bIsOptionGroup[i] && a_bIsOptionGroup[i+1]) || (a_bIsOptionGroup[i] && a_bIsOptionGroup[i+1] == null)) continue; //Dos optgroups seguidos, o último item optgroup, o sea, no tiene hijos
			
				if(a_bIsOptionGroup[i] && !a_bIsOptionGroup[i+1])
				{
					current_group = document.createElement('optgroup');
					//current_group.value = a_sTexts[i];
					current_group.label = a_sTexts[i];
					oElem.appendChild(current_group);//.options.add(current_group);
				}
				else 
				{
					if(current_group != null)
					{*/
						/*var optn = document.createElement("OPTION");
						optn.text = a_sTexts[i];
						optn.innerHTML = a_sTexts[i]; // for IE
						optn.value = a_sValues[i];
						current_group.appendChild(optn);*/
						//oElem.options.add(new Option(a_sTexts[i], a_sValues[i]));
						
						addOption(oElem, a_sTexts[i], a_sValues[i], false)
						
					/*}
				}*/
			}
		}
		else for (var i = 0; i < a_sValues.length; i++) oElem.options.add(new Option(a_sTexts[i], a_sValues[i]));
			
		
	}  	    
}  
       


// rellena el combo con el arbol completo de categorias hijas
function fillCategoryAndChildsCombo_only_lvl_2(sParentCatCode, oElem, Separator, bIsTag, sMainLevel, bCdata) {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter('category_parent_code', sParentCatCode);
	if (bCdata) oAjax.addPostParameter('include_cdata_name', '1');
	
	var xmlObj;
	if(bIsTag) xmlObj = oAjax.throwEventXML('tractes_get_category_list_tags');        
	else xmlObj = oAjax.throwEventXML('tractes_get_category_list');

	if (xmlObj) {
		var a_sValues = new Array();
		var a_sTexts = new Array();
		var a_bIsOptionGroup = new Array();
		if (Separator == undefined) Separator = '';
		
		//alert(sParentCatCode + ' ' + xmlObj.childNodes.length);

		for (var i = 0; i < xmlObj.childNodes.length; i++) {
			var oNode = xmlObj.childNodes[i];
			var cat_value = oNode.childNodes[0].firstChild.data;
			var lvl = '';
			var name = '';
			if (oNode.childNodes[2] && oNode.childNodes[2].firstChild) lvl = oNode.childNodes[2].firstChild.data;
			if (bCdata && false) {
				if (oNode.childNodes[1] && oNode.childNodes[1].childNodes[0] && oNode.childNodes[1].childNodes[0].firstChild) name = oNode.childNodes[1].childNodes[0].firstChild.data;
			} else {
				if (oNode.childNodes[1] && oNode.childNodes[1].firstChild) name = oNode.childNodes[1].firstChild.data;
			}
			//alert(i + ': ' + name);
			if (i > 2 && (i < 5)){
				//alert(i + ': ' + name);
			}
			
			
			var cat_text = this.getCategoryIdent(lvl) + name;
			//var cat_text = name;
			
			if (lvl == 1) cat_value = '';
			
			//if (oNode.childNodes[1].firstChild != null) {
			if (name != null) {
				a_sValues.push(cat_value);
				a_sTexts.push(cat_text);
				//if(lvl.toString() == sMainLevel) a_bIsOptionGroup.push(true);
				//else a_bIsOptionGroup.push(false);
			}
			else {
				a_sValues.push('VACIO');
				a_sTexts.push('VACIO');
			}
		}

		// rellena el combo
		if(!bIsTag)
		{
			var current_group = null;
			for (var i = 0; i < a_sValues.length; i++)
			{
				addOption(oElem, a_sTexts[i], a_sValues[i], false);
			}
		}
		else for (var i = 0; i < a_sValues.length; i++) oElem.options.add(new Option(a_sTexts[i], a_sValues[i]));
			
		
	}  	    
}  
   
	   
// auxiliar, añade texto para indicar el nivel de la categoria
function getCategoryIdent(iLevel, Separator) {
	var s = '';
	if (!Separator) Separator = '----';
	for (var i = 1; i < iLevel; i++) s += Separator;
	return s;
}
 
