var fixParseInt = parseInt;
parseInt = function(val, base) {
	return fixParseInt(val, base || 10);
}

function count( mixed_var, mode ) {
    var key, cnt = 0;
    if (mode == 'COUNT_RECURSIVE' ) {
        mode = 1;
    }
    if (mode != 1 ) {
        mode = 0;
    }
    for (key in mixed_var){
        cnt++;
        if (mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object)){
            cnt += count(mixed_var[key], 1);
        }
    }
    return cnt;
}

function array_search(needle, haystack) {
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] == needle) {
            return i;
        }
    }
    return false;
}

function array_unset(key, haystack) {
    var tmp = new Array();
    for (var i = 0; i < haystack.length; i++) {
        if (i !== key) {
            tmp[tmp.length] = haystack[i];
        }
    }
    return tmp;
}

function array_unique(array) {
    var arrRet = new Array();
    for (key in array) {
        if (false === array_search(array[key], arrRet)) {
            arrRet[key] = array[key];
        }
        delete array[key];
    }
    return arrRet;
}

function bajar(hash) {
    var objDiv = YAHOO.util.Dom.get(hash);
    if (objDiv) {
        var cambio = YAHOO.util.Dom.getNextSibling(objDiv);
        if (cambio) {
            YAHOO.util.Dom.insertBefore(cambio, objDiv);
        }
    }
}

function subir(hash) {
    var objDiv = YAHOO.util.Dom.get(hash);
    if (objDiv) {
        var cambio = YAHOO.util.Dom.getPreviousSibling(objDiv);
        if (cambio) {
            YAHOO.util.Dom.insertAfter(cambio, objDiv);
        }
    }
}

function enlacesExternal() {
    els = document.getElementsByTagName('a');
    for(var i = 0, len = els.length; i < len; ++i){
        if (els[i].getAttribute('href') && els[i].getAttribute('rel') == 'external') {
            els[i].onclick = new Function('window.open(this.href); return false;');
        }
    }
}

formatImporteDecimal=function(importe){
    strImporte=importe.toString();
    var tmp="";
    var c="";
    for (i=0;i<strImporte.length;i++){
        c=strImporte.charAt(i);
        if (c==","){
            c=".";
        }
        tmp+=c;
    }
    var tmp=parseFloat(tmp);
    tmp=Math.round(tmp*100)/100
    strTmp=tmp.toString();
    tmp="";
    c="";
    var decimales=false;
    var contadorDecimales=0;
    for (i=0;i<strTmp.length;i++){
        c=strTmp.charAt(i);
        if (decimales){
            contadorDecimales++;
        }
        if (c=="."){
            c=",";
            decimales=true;
        }
        tmp+=c;
    }
    if (contadorDecimales==0){
        tmp=tmp+",00";
    } else if (contadorDecimales==1){
        tmp=tmp+"0";
    }
    return tmp;
}


funciones_someter_onload[funciones_someter_onload.length] = "enlacesExternal";

function strtr(str, from, to) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: uestla
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Alan C
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: $trans = {'hello' : 'hi', 'hi' : 'hello'};
    // *     example 1: strtr('hi all, I said hello', $trans)
    // *     returns 1: 'hello all, I said hi'
    // *     example 2: strtr('�aaba�ccasde�oo', '���','aao');
    // *     returns 2: 'aaabaaccasdeooo'
    // *     example 3: strtr('��������', '�', 'a');
    // *     returns 3: 'aaaaaaaa'
    // *     example 4: strtr('http', 'pthxyz','xyzpth');
    // *     returns 4: 'zyyx'
    // *     example 5: strtr('zyyx', 'pthxyz','xyzpth');
    // *     returns 5: 'http'

    var fr = '', i = 0, j = 0, lenStr = 0, lenFrom = 0;
    var tmpFrom = [];
    var tmpTo   = [];
    var ret = '';
    var match = false;

    // Received replace_pairs?
    // Convert to normal from->to chars
    if (typeof from === 'object') {
        for (fr in from) {
            tmpFrom.push(fr);
            tmpTo.push(from[fr]);
        }

        from = tmpFrom;
        to   = tmpTo;
    }

    // Walk through subject and replace chars when needed
    lenStr  = str.length;
    lenFrom = from.length;
    for (i = 0; i < lenStr; i++) {
        match = false;
        if(j){
            for (j = 0; j < lenFrom; j++) {
                if (str.substr(i, from[j].length) == from[j]) {
                    match = true;

                    // Fast forward
                    i = (i + from[j].length)-1;

                    break;
                }
            }
        }

        if (false !== match) {
            ret += to[j];
        } else {
            ret += str[i];
        }
    }

    return ret;
}

function formatMonADec(strImporte){
    strImporte=strImporte.replace(".","");
    strImporte=strImporte.replace(",",".");
    fImporte = parseFloat(strImporte);
    if (!isNaN(fImporte)){
      return fImporte;
    }else{
      return strImporte;
    }
}

function formatDecAMon(importe){
  if (!isNaN(importe)){
      importe=Math.round(importe*100)/100
      strImporte=importe.toString();
      var sParteEntera = "";
      var sParteDecimal = "";
      var posicionDecimal = strImporte.indexOf(".");
      if (posicionDecimal>=0){
        sParteEntera = strImporte.substring(0,posicionDecimal);
        sParteDecimal = strImporte.substring(posicionDecimal+1);
    }else{
      sParteEntera = strImporte;
    }
    if(!isNaN(sParteEntera)){
      cad2 = "";
      tres=0;
      for (k=sParteEntera.length-1;k>=0;k--){
        cad1 = sParteEntera.charAt(k);
        cad2 = cad1 + cad2;
        tres++;
        if((tres%3) == 0){
          if(k!=0){
            cad2 = "." + cad2
          }
        }
      }
      strImporte = cad2;
        if (sParteDecimal!=""){
          if (sParteDecimal.length==0){
            strImporte+=",00";
          } else if (sParteDecimal.length==1){
            strImporte+=","+sParteDecimal+"0";
          } else if (sParteDecimal.length>=2){
            strImporte+=","+sParteDecimal.substring(0,2);
          }
        }
    }
      return strImporte;

  }else{
    return importe;
  }
}

/**
 * Muestra u oculta un panel
 *
 * @param string id     ID del elemento al que se le asignará el listener
 * @param string div    Elemento que mostrará
 * @param string div    Elemento que ocultará
 * @return void
 */
function mostrarOcultar(id, div, div2) {
    var o = document.getElementById(id);
    var d = document.getElementById(div);
    var d2 = document.getElementById(div2);

    YAHOO.util.Event.addListener(o, 'click', changeDisplay, d, true);

    function changeDisplay(ev, obj) {
        obj.style.display = ((obj.style.display == 'none') || (obj.style.display == '')) ? 'block' : 'none';
        if(obj.style.display == 'block' && d2) {
            d2.style.display = 'none';
        }
    }
}

function trim(str, charlist) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6

    var whitespace, l = 0, i = 0;
    str += '';

    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }

    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }

    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }

    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function initShadowbox(){
    var options = {
            keysClose : [27]
        };
    try{
        Shadowbox.init(options);
    }catch(e){
    }

}

try{
    YAHOO.util.Event.onDOMReady(initShadowbox);
}catch(e){
}
