// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var hideshow = new Array();
var timeoutID = new Array();

function show(id){
  hideshow[id] = document.getElementById(id);
  if (hideshow[id].style.display == 'none'){
    hideshow[id].style.display = '';
  }
  else {
    window.clearTimeout(timeoutID[id]);
  }
}

function hide(id){
  timeoutID[id]=setTimeout("hideshow['"+id+"'].style.display = 'none'", 100); //Set delay
}

function stopTimer(id){
  window.clearTimeout(timeoutID[id]);
}

if(typeof document.createStyleSheet === 'undefined') {
  document.createStyleSheet = (function() {
    function createStyleSheet(href) {
      if(typeof href !== 'undefined') {
        var element = document.createElement('link');
        element.type = 'text/css';
        element.rel = 'stylesheet';
        element.href = href;
      }
      else {
        var element = document.createElement('style');
        element.type = 'text/css';
      }
      document.getElementsByTagName('head')[0].appendChild(element);
      var sheet = document.styleSheets[document.styleSheets.length - 1];
      if(typeof sheet.addRule === 'undefined')
        sheet.addRule = addRule;
      if(typeof sheet.removeRule === 'undefined')
        sheet.removeRule = sheet.deleteRule;
      return sheet;
    }
    function addRule(selectorText, cssText, index) {
      if(typeof index === 'undefined')
        index = this.cssRules.length;
      this.insertRule(selectorText + ' {' + cssText + '}', index);
    }
    return createStyleSheet;
  })();
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

function select_series(html) {
  if(typeof instanceOne !== 'undefined'){
    instanceOne = null;
  }
  var span_msg_box = document.getElementById('msg_box');
  span_msg_box.innerHTML = '';
  var div_series_box = document.getElementById('series_box');
  div_series_box.innerHTML = '';
  div_series_box.innerHTML = '<div id="myImageFlow" class="imageflow">'+html+'</div>';
  var instanceOne = new ImageFlow();
  instanceOne.init({ ImageFlowID:'myImageFlow', imageFocusM: 1.8, aspectRatio: 3.0, imagesHeight: 0.75, reflections: false, reflectionP: 0.3, opacity: true, TextoCarregando: TextoCarregando, onClick: function() { return carregar_msgs( type, o, this.getAttribute('longdesc') ); } });
}

function enviar(acao) {
  //verifica se o browser tem suporte a ajax
  var ajax;

  try {
    ajax = new XMLHttpRequest();
  }
  catch(e) {
    try {
      ajax = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {
      try {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e) {
        alert("Esse browser não tem recursos para uso do Ajax");
        ajax = null;
      }
    }
  }
  //se tiver suporte ajax
  if(ajax) {
    f = document.getElementById(acao+'_form');
    var parms = f.elements[0].name+'='+utf8_encode(proc_texto(f.elements[0].value));
    for(i=1; i<f.elements.length; i++){
      if(f.elements[i].type == "checkbox" || f.elements[i].type == "radio"){
        if(f.elements[i].checked){
          parms += '&'+f.elements[i].name+'='+utf8_encode(proc_texto(f.elements[i].value));
        }
      }
      else {
        parms += '&'+f.elements[i].name+'='+utf8_encode(proc_texto(f.elements[i].value));
      }
    }
    //deixa apenas o elemento 1 no option, os outros são excluídos
    ajax.onreadystatechange = function() {
      //após ser processado - avisa enviado ou não
      if(ajax.readyState == 4 ) {
        if(ajax.responseText) {
          var texto = ajax.responseText;
          texto = texto.replace(/\+/g," ");
          texto = unescape(texto);
          if(acao == "select_series"){
            select_series(texto);
          }
          else {
            alert(texto);
          }
        }
        else {
          //caso não seja um arquivo texto emite a mensagem abaixo
          alert("Erro ao tentar realizar a operação! Por favor, tente novamente ou entre em contato.");
        }
      }
    }
    ajax.open("POST", "inc/acoes.php", true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("charset", "utf-8");
    ajax.send(parms);
  }
}

function proc_texto(texto){
  texto = str_replace("&Aacute;", "Á", texto);
  texto = str_replace("&Acirc;", "Â", texto);
  texto = str_replace("&Agrave;", "À", texto);
  texto = str_replace("&Atilde;", "Ã", texto);
  texto = str_replace("&Auml;", "Ä", texto);
  texto = str_replace("&Eacute;", "É", texto);
  texto = str_replace("&Ecirc;", "Ê", texto);
  texto = str_replace("&Egrave;", "È", texto);
  texto = str_replace("&Euml;", "Ë", texto);
  texto = str_replace("&Iacute;", "Í", texto);
  texto = str_replace("&Icirc;", "Î", texto);
  texto = str_replace("&Igrave;", "Ì", texto);
  texto = str_replace("&Iuml;", "Ï", texto);
  texto = str_replace("&Oacute;", "Ó", texto);
  texto = str_replace("&Ocirc;", "Ô", texto);
  texto = str_replace("&Ograve;", "Ò", texto);
  texto = str_replace("&Otilde;", "Õ", texto);
  texto = str_replace("&Uuml;", "Ö", texto);
  texto = str_replace("&Uacute;", "Ú", texto);
  texto = str_replace("&Ucirc;", "Û", texto);
  texto = str_replace("&Ugrave;", "Ù", texto);
  texto = str_replace("&Uuml;", "Ü", texto);
  texto = str_replace("&Yacute;", "Ý", texto);
  texto = str_replace("&Ccedil;", "Ç", texto);
  texto = str_replace("&Ntilde;", "Ñ", texto);
  texto = str_replace("&aacute;", "á", texto);
  texto = str_replace("&acirc;", "â", texto);
  texto = str_replace("&agrave;", "à", texto);
  texto = str_replace("&atilde;", "ã", texto);
  texto = str_replace("&auml;", "ä", texto);
  texto = str_replace("&eacute;", "é", texto);
  texto = str_replace("&ecirc;", "ê", texto);
  texto = str_replace("&egrave;", "è", texto);
  texto = str_replace("&euml;", "ë", texto);
  texto = str_replace("&iacute;", "í", texto);
  texto = str_replace("&icirc;", "î", texto);
  texto = str_replace("&igrave;", "ì", texto);
  texto = str_replace("&iuml;", "ï", texto);
  texto = str_replace("&oacute;", "ó", texto);
  texto = str_replace("&ocirc;", "ô", texto);
  texto = str_replace("&ograve;", "ò", texto);
  texto = str_replace("&otilde;", "õ", texto);
  texto = str_replace("&ouml;", "ö", texto);
  texto = str_replace("&uacute;", "ú", texto);
  texto = str_replace("&ucirc;", "û", texto);
  texto = str_replace("&ugrave;", "ù", texto);
  texto = str_replace("&uuml;", "ü", texto);
  texto = str_replace("&yacute;", "ý", texto);
  texto = str_replace("&yuml;", "ÿ", texto);
  texto = str_replace("&ccedil;", "ç", texto);
  texto = str_replace("&ntilde;", "ñ", texto);
  texto = str_replace("&ordm;", "º", texto);
  texto = str_replace("&ordf;", "ª", texto);
  texto = str_replace("&acute;", "´", texto);
  texto = str_replace("&circ;", "^", texto);
  texto = str_replace("&grave;", "`", texto);
  texto = str_replace("&tilde;", "~", texto);
  texto = str_replace("&uml;", "¨", texto);
  texto = str_replace("&#39", "'", texto);
  texto = str_replace("&quot;", '"', texto);
  texto = str_replace("&", "Eamp;", texto);
  //texto = str_replace("\r\n", "<br />", texto);
  //texto = str_replace("\r", "<br />", texto);
  //texto = str_replace("\n", "<br />", texto);

  return texto;
}

function str_replace(search, replace, subject) {
  var s = subject;
  var ra = r instanceof Array, sa = s instanceof Array;
  var f = [].concat(search);
  var r = [].concat(replace);
  var i = (s = [].concat(s)).length;
  var j = 0;
    
  while (j = 0, i--) {
    if (s[i]) {
      while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    }
  }

  return sa ? s : s[0];
}

function utf8_encode ( string ) {
  string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
  var utftext = "";
  var start, end;
  var stringl = 0;
  start = end = 0;
  stringl = string.length;
  for (var n = 0; n < stringl; n++) {
    var c1 = string.charCodeAt(n);
    var enc = null;
    if (c1 < 128) {
      end++;
    } else if((c1 > 127) && (c1 < 2048)) {
      enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
    } else {
      enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
    }
    if (enc != null) {
      if (end > start) {
        utftext += string.substring(start, end);
      }
      utftext += enc;
      start = end = n+1;
    }
  }
  if (end > start) {
    utftext += string.substring(start, string.length);
  }
  return utftext;
}

function urlencode( str ) {
 
  var histogram = {}, tmp_arr = [];
  var ret = (str+'').toString();
 
  var replacer = function(search, replace, str) {
    var tmp_arr = [];
    tmp_arr = str.split(search);
    return tmp_arr.join(replace);
  };
 
  // The histogram is identical to the one in urldecode.
  histogram[' '] = '%20';
  histogram['!'] = '%21';
  histogram['"'] = '%22';
  histogram['#'] = '%23';
  histogram['$'] = '%24';
  histogram['&'] = '%26';
  histogram["'"] = '%27';
  histogram['('] = '%28';
  histogram[')'] = '%29';
  histogram['*'] = '%2A';
  histogram['+'] = '%2B';
  histogram[','] = '%2C';
  histogram['-'] = '%2D';
  histogram['.'] = '%2E';
  histogram['/'] = '%2F';
  histogram[':'] = '%3A';
  histogram[';'] = '%3B';
  histogram['<'] = '%3C';
  histogram['='] = '%3D';
  histogram['>'] = '%3E';
  histogram['?'] = '%3F';
  histogram['@'] = '%40';
  histogram['['] = '%5B';
  histogram['\\'] = '%5C';
  histogram[']'] = '%5D';
  histogram['^'] = '%5E';
  histogram['_'] = '%5F';
  histogram['`'] = '%60';
  histogram['{'] = '%7B';
  histogram['|'] = '%7C';
  histogram['}'] = '%7D';
  histogram['~'] = '%7E';
  histogram[''] = '%7F';
  histogram['€'] = '%80';
  histogram[''] = '%81';
  histogram['‚'] = '%82';
  histogram['ƒ'] = '%83';
  histogram['„'] = '%84';
  histogram['…'] = '%85';
  histogram['†'] = '%86';
  histogram['‡'] = '%87';
  histogram['ˆ'] = '%88';
  histogram['‰'] = '%89';
  histogram['Š'] = '%8A';
  histogram['‹'] = '%8B';
  histogram['Œ'] = '%8C';
  histogram[''] = '%8D';
  histogram['Ž'] = '%8E';
  histogram[''] = '%8F';
  histogram[''] = '%90';
  histogram['‘'] = '%91';
  histogram['’'] = '%92';
  histogram['“'] = '%93';
  histogram['”'] = '%94';
  histogram['•'] = '%95';
  histogram['–'] = '%96';
  histogram['—'] = '%97';
  histogram['˜'] = '%98';
  histogram['™'] = '%99';
  histogram['š'] = '%9A';
  histogram['›'] = '%9B';
  histogram['œ'] = '%9C';
  histogram[''] = '%9D';
  histogram['ž'] = '%9E';
  histogram['Ÿ'] = '%9F';
  histogram['¡'] = '%A1';
  histogram['¢'] = '%A2';
  histogram['£'] = '%A3';
  histogram['¤'] = '%A4';
  histogram['¥'] = '%A5';
  histogram['¦'] = '%A6';
  histogram['§'] = '%A7';
  histogram['¨'] = '%A8';
  histogram['©'] = '%A9';
  histogram['ª'] = '%AA';
  histogram['«'] = '%AB';
  histogram['¬'] = '%AC';
  histogram['­'] = '%AD';
  histogram['®'] = '%AE';
  histogram['¯'] = '%AF';
  histogram['°'] = '%B0';
  histogram['±'] = '%B1';
  histogram['²'] = '%B2';
  histogram['³'] = '%B3';
  histogram['´'] = '%B4';
  histogram['µ'] = '%B5';
  histogram['¶'] = '%B6';
  histogram['·'] = '%B7';
  histogram['¸'] = '%B8';
  histogram['¹'] = '%B9';
  histogram['º'] = '%BA';
  histogram['»'] = '%BB';
  histogram['¼'] = '%BC';
  histogram['½'] = '%BD';
  histogram['¾'] = '%BE';
  histogram['¿'] = '%BF';
  histogram['À'] = '%C0';
  histogram['Á'] = '%C1';
  histogram['Â'] = '%C2';
  histogram['Ã'] = '%C3';
  histogram['Ä'] = '%C4';
  histogram['Å'] = '%C5';
  histogram['Æ'] = '%C6';
  histogram['Ç'] = '%C7';
  histogram['È'] = '%C8';
  histogram['É'] = '%C9';
  histogram['Ê'] = '%CA';
  histogram['Ë'] = '%CB';
  histogram['Ì'] = '%CC';
  histogram['Í'] = '%CD';
  histogram['Î'] = '%CE';
  histogram['Ï'] = '%CF';
  histogram['Ð'] = '%D0';
  histogram['Ñ'] = '%D1';
  histogram['Ò'] = '%D2';
  histogram['Ó'] = '%D3';
  histogram['Ô'] = '%D4';
  histogram['Õ'] = '%D5';
  histogram['Ö'] = '%D6';
  histogram['×'] = '%D7';
  histogram['Ø'] = '%D8';
  histogram['Ù'] = '%D9';
  histogram['Ú'] = '%DA';
  histogram['Û'] = '%DB';
  histogram['Ü'] = '%DC';
  histogram['Ý'] = '%DD';
  histogram['Þ'] = '%DE';
  histogram['ß'] = '%DF';
  histogram['à'] = '%E0';
  histogram['á'] = '%E1';
  histogram['â'] = '%E2';
  histogram['ã'] = '%E3';
  histogram['ä'] = '%E4';
  histogram['å'] = '%E5';
  histogram['æ'] = '%E6';
  histogram['ç'] = '%E7';
  histogram['è'] = '%E8';
  histogram['é'] = '%E9';
  histogram['ê'] = '%EA';
  histogram['ë'] = '%EB';
  histogram['ì'] = '%EC';
  histogram['í'] = '%ED';
  histogram['î'] = '%EE';
  histogram['ï'] = '%EF';
  histogram['ð'] = '%F0';
  histogram['ñ'] = '%F1';
  histogram['ò'] = '%F2';
  histogram['ó'] = '%F3';
  histogram['ô'] = '%F4';
  histogram['õ'] = '%F5';
  histogram['ö'] = '%F6';
  histogram['÷'] = '%F7';
  histogram['ø'] = '%F8';
  histogram['ù'] = '%F9';
  histogram['ú'] = '%FA';
  histogram['û'] = '%FB';
  histogram['ü'] = '%FC';
  histogram['ý'] = '%FD';
  histogram['þ'] = '%FE';
  histogram['ÿ'] = '%FF';
 
  // Begin with encodeURIComponent, which most resembles PHP's encoding functions
  ret = encodeURIComponent(ret);
 
  for (search in histogram) {
    replace = histogram[search];
    ret = replacer(search, replace, ret) // Custom replace. No regexing
  }
 
  // Uppercase for full PHP compatibility
  return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
    return "%"+m2.toUpperCase();
  });
 
  return ret;
}
 
 
function urldecode( str ) {
 
  var histogram = {};
  var ret = str.toString();
 
  var replacer = function(search, replace, str) {
    var tmp_arr = [];
    tmp_arr = str.split(search);
    return tmp_arr.join(replace);
  };
 
  // The histogram is identical to the one in urlencode.
  histogram[' '] = '%20';
  histogram['!'] = '%21';
  histogram['"'] = '%22';
  histogram['#'] = '%23';
  histogram['$'] = '%24';
  histogram['&'] = '%26';
  histogram["'"] = '%27';
  histogram['('] = '%28';
  histogram[')'] = '%29';
  histogram['*'] = '%2A';
  histogram['+'] = '%2B';
  histogram[','] = '%2C';
  histogram['-'] = '%2D';
  histogram['.'] = '%2E';
  histogram['/'] = '%2F';
  histogram[':'] = '%3A';
  histogram[';'] = '%3B';
  histogram['<'] = '%3C';
  histogram['='] = '%3D';
  histogram['>'] = '%3E';
  histogram['?'] = '%3F';
  histogram['@'] = '%40';
  histogram['['] = '%5B';
  histogram['\\'] = '%5C';
  histogram[']'] = '%5D';
  histogram['^'] = '%5E';
  histogram['_'] = '%5F';
  histogram['`'] = '%60';
  histogram['{'] = '%7B';
  histogram['|'] = '%7C';
  histogram['}'] = '%7D';
  histogram['~'] = '%7E';
  histogram[''] = '%7F';
  histogram['€'] = '%80';
  histogram[''] = '%81';
  histogram['‚'] = '%82';
  histogram['ƒ'] = '%83';
  histogram['„'] = '%84';
  histogram['…'] = '%85';
  histogram['†'] = '%86';
  histogram['‡'] = '%87';
  histogram['ˆ'] = '%88';
  histogram['‰'] = '%89';
  histogram['Š'] = '%8A';
  histogram['‹'] = '%8B';
  histogram['Œ'] = '%8C';
  histogram[''] = '%8D';
  histogram['Ž'] = '%8E';
  histogram[''] = '%8F';
  histogram[''] = '%90';
  histogram['‘'] = '%91';
  histogram['’'] = '%92';
  histogram['“'] = '%93';
  histogram['”'] = '%94';
  histogram['•'] = '%95';
  histogram['–'] = '%96';
  histogram['—'] = '%97';
  histogram['˜'] = '%98';
  histogram['™'] = '%99';
  histogram['š'] = '%9A';
  histogram['›'] = '%9B';
  histogram['œ'] = '%9C';
  histogram[''] = '%9D';
  histogram['ž'] = '%9E';
  histogram['Ÿ'] = '%9F';
  histogram['¡'] = '%A1';
  histogram['¢'] = '%A2';
  histogram['£'] = '%A3';
  histogram['¤'] = '%A4';
  histogram['¥'] = '%A5';
  histogram['¦'] = '%A6';
  histogram['§'] = '%A7';
  histogram['¨'] = '%A8';
  histogram['©'] = '%A9';
  histogram['ª'] = '%AA';
  histogram['«'] = '%AB';
  histogram['¬'] = '%AC';
  histogram['­'] = '%AD';
  histogram['®'] = '%AE';
  histogram['¯'] = '%AF';
  histogram['°'] = '%B0';
  histogram['±'] = '%B1';
  histogram['²'] = '%B2';
  histogram['³'] = '%B3';
  histogram['´'] = '%B4';
  histogram['µ'] = '%B5';
  histogram['¶'] = '%B6';
  histogram['·'] = '%B7';
  histogram['¸'] = '%B8';
  histogram['¹'] = '%B9';
  histogram['º'] = '%BA';
  histogram['»'] = '%BB';
  histogram['¼'] = '%BC';
  histogram['½'] = '%BD';
  histogram['¾'] = '%BE';
  histogram['¿'] = '%BF';
  histogram['À'] = '%C0';
  histogram['Á'] = '%C1';
  histogram['Â'] = '%C2';
  histogram['Ã'] = '%C3';
  histogram['Ä'] = '%C4';
  histogram['Å'] = '%C5';
  histogram['Æ'] = '%C6';
  histogram['Ç'] = '%C7';
  histogram['È'] = '%C8';
  histogram['É'] = '%C9';
  histogram['Ê'] = '%CA';
  histogram['Ë'] = '%CB';
  histogram['Ì'] = '%CC';
  histogram['Í'] = '%CD';
  histogram['Î'] = '%CE';
  histogram['Ï'] = '%CF';
  histogram['Ð'] = '%D0';
  histogram['Ñ'] = '%D1';
  histogram['Ò'] = '%D2';
  histogram['Ó'] = '%D3';
  histogram['Ô'] = '%D4';
  histogram['Õ'] = '%D5';
  histogram['Ö'] = '%D6';
  histogram['×'] = '%D7';
  histogram['Ø'] = '%D8';
  histogram['Ù'] = '%D9';
  histogram['Ú'] = '%DA';
  histogram['Û'] = '%DB';
  histogram['Ü'] = '%DC';
  histogram['Ý'] = '%DD';
  histogram['Þ'] = '%DE';
  histogram['ß'] = '%DF';
  histogram['à'] = '%E0';
  histogram['á'] = '%E1';
  histogram['â'] = '%E2';
  histogram['ã'] = '%E3';
  histogram['ä'] = '%E4';
  histogram['å'] = '%E5';
  histogram['æ'] = '%E6';
  histogram['ç'] = '%E7';
  histogram['è'] = '%E8';
  histogram['é'] = '%E9';
  histogram['ê'] = '%EA';
  histogram['ë'] = '%EB';
  histogram['ì'] = '%EC';
  histogram['í'] = '%ED';
  histogram['î'] = '%EE';
  histogram['ï'] = '%EF';
  histogram['ð'] = '%F0';
  histogram['ñ'] = '%F1';
  histogram['ò'] = '%F2';
  histogram['ó'] = '%F3';
  histogram['ô'] = '%F4';
  histogram['õ'] = '%F5';
  histogram['ö'] = '%F6';
  histogram['÷'] = '%F7';
  histogram['ø'] = '%F8';
  histogram['ù'] = '%F9';
  histogram['ú'] = '%FA';
  histogram['û'] = '%FB';
  histogram['ü'] = '%FC';
  histogram['ý'] = '%FD';
  histogram['þ'] = '%FE';
  histogram['ÿ'] = '%FF';
 
  for (replace in histogram) {
    search = histogram[replace]; // Switch order when decoding
    ret = replacer(search, replace, ret) // Custom replace. No regexing   
  }
 
  // End with decodeURIComponent, which most resembles PHP's encoding functions
  ret = decodeURIComponent(ret);
 
  return ret;
}

function preg_replace(pattern, replacement, subject, limit){  
  if(typeof limit == "undefined") limit=-1;
  if (subject.match(eval(pattern))) {
    if (limit == -1) { //no limit
      return subject.replace(eval(pattern + "g"), replacement);
    }
    else {
      for(x=0;x<limit;x++)
      {
        subject=subject.replace(eval(pattern),replacement);
      }
      return subject;
    }
  }
  else {
    return subject;
  }
}

function substr (str, start, len) {
  str += '';
  var end = str.length;
  if (start < 0) {
    start += end;
  }
  end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
  return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
}

function strtolower (str) {
  return (str+'').toLowerCase();
}

function clean_up_file_name(string) {
  string = proc_texto(string);
  reg = /\s+/;
  string = preg_replace(reg, "_", string);
  string = preg_replace("/[^a-zA-Z0-9_-]+/", "", string);
  string = preg_replace("/_{2,}/", "_", string);
  if (string.lenght > 16) {
    return substr(strtolower(string), -16);
  }
  else{
    return strtolower(string);
  }
}

function onSilverlightError(sender, args) {
  var appSource = "";

  if (sender != null && sender != 0) {
    appSource = sender.getHost().Source;
  }
  var errorType = args.ErrorType;
  var iErrorCode = args.ErrorCode;

  var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";
  errMsg += "Code: " + iErrorCode + "    \n";
  errMsg += "Category: " + errorType + "       \n";
  errMsg += "Message: " + args.ErrorMessage + "     \n";

  if (errorType == "ParserError") {
    errMsg += "File: " + args.xamlFile + "     \n";
    errMsg += "Line: " + args.lineNumber + "     \n";
    errMsg += "Position: " + args.charPosition + "     \n";
  }
  else if (errorType == "RuntimeError") {
    if (args.lineNumber != 0) {
      errMsg += "Line: " + args.lineNumber + "     \n";
      errMsg += "Position: " + args.charPosition + "     \n";
    }
    errMsg += "MethodName: " + args.methodName + "     \n";
  }

  //document.getElementById('silverlightControlHost').innerHTML = errMsg;
  throw new Error(errMsg);
}

