/**
 * Enviar formulari
 */

function send_form(name) {
  document.getElementById(name).submit();
}

/**
 * Desplegar
 */

function show_hide(id) {
  var targetElement;

  targetElement = document.getElementById(id);
  if(targetElement.style.display=="block") {
    targetElement.style.display = "none";
  } else {
    targetElement.style.display = "block";
  }
}

/**
 * Eliminar etiquetes i espais inici/final d'una cadena
 */

function strip_tags(str, allowed_tags) {
  var key = '', tag = '', allowed = false;
  var matches = allowed_array = [];
 
  var replacer = function(search, replace, str) {
    return str.split(search).join(replace);
  };
  // Build allowes tags associative array
  if (allowed_tags) {
    allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
  }
  str += '';
  matches = str.match(/(<\/?[^>]+>)/gi); // Match tags
 
  for (key in matches) {
    if (isNaN(key)) { // IE7 Hack
      continue;
    }
    html = matches[key].toString();// Save HTML tag
    allowed = false;// Is tag not in allowed list? Remove from str!
 
    for (k in allowed_array) {
      allowed_tag = allowed_array[k];
      i = -1;
 
      if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
      if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
      if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
      if (i == 0) {
        allowed = true;
        break;
      }
    }
    if (!allowed) {
      str = replacer(html, "", str); // Custom replace. No regexing
    }
  }
  return str;
}

function trim(string) {
  string = strip_tags(string ,''); // Eliminem etiquetes html
  return string.replace(/\t/g, '').replace(/\r/g, '').replace(/\n/g, ''); // Eliminem salts de linea
  return string.replace(/^[\s]+|[\s]+$/g, ''); // Eliminem espais a principi i final
}

/**
 * Afegir camps
 */
 
var camp_counter = 0;

function moreFields() {
	camp_counter++;
	var newFields = document.getElementById('readroot').cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++) {
		var theName = newField[i].name
		if (theName)
			newField[i].name = theName + camp_counter;
	}
	var insertHere = document.getElementById('new_content');
	insertHere.parentNode.insertBefore(newFields,insertHere);
}

/**
 * AJAX Basics v1.0 - Crear connexió i carregar fragment en un element
 */
 
var xmlhttp = getXmlHttpObject();

function getXmlHttpObject(){
    var xmlhttp;

    /*@cc_on
    @if (@_jscript_version >= 5)
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try{
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function loadFragmentInToElement(fragment_url, element_id) { 
  var element = document.getElementById(element_id); 
  xmlhttp.open("GET", fragment_url); 
  xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      element.innerHTML = xmlhttp.responseText; 
    } 
  } 
  xmlhttp.send(null); 
}
