function preloadImages() {    

      

    image_url = new Array();
    image_url[0] = "http://www.truekenet.com/themes/classic/img/btn_container2_hover.png";
    image_url[1] = "http://www.truekenet.com/themes/classic/img/btn_container_hover.png";
    image_url[2] = "http://www.truekenet.com/themes/classic/img/btn_green_hover.png";
    image_url[3] = "http://www.truekenet.com/themes/classic/img/btn_green_nb_hover.png";
    image_url[4] = "http://www.truekenet.com/themes/classic/img/btn_identify_hover.png";
    image_url[5] = "http://www.truekenet.com/themes/classic/img/btn_logout_hover.png";
    image_url[6] = "http://www.truekenet.com/themes/classic/img/btn_orange_hover.png";
    image_url[7] = "http://www.truekenet.com/themes/classic/img/btn_orange_nb_hover.png";
    image_url[8] = "http://www.truekenet.com/themes/classic/img/btn_purple_hover.png";
    image_url[9] = "http://www.truekenet.com/themes/classic/img/btn_register_hover.png";
    image_url[10] = "http://www.truekenet.com/themes/classic/img/btn_sub_green_hover.png";
    image_url[11] = "http://www.truekenet.com/themes/classic/img/btn_sub_orange_hover.png";
    image_url[12] = "http://www.truekenet.com/themes/classic/img/perfil_hover.gif";
    image_url[13] = "http://www.truekenet.com/themes/classic/img/red_hover.gif";
    image_url[14] = "http://www.truekenet.com/themes/classic/img/trueque_hover.gif";

    for(var i=0; i<image_url.length; i++) {
        preload_image_object = new Image();
        preload_image_object.src = image_url[i];
    }
}

function justNumbers(e) {
    var keynum = window.event ? window.event.keyCode : e.which;
    if ( keynum == 8 || keynum == 0) return true;
    return /\d/.test(String.fromCharCode(keynum));
}

function limitInput(e, obj, chars) {
    cadena = obj.value;
    var keynum = window.event ? window.event.keyCode : e.which;
    if ( keynum == 8 || keynum == 0) return true;
    return cadena.length < chars;
}

function passwordLevel (p){
    var l = 0;
    var v1 = new String("aeiou1234567890");
    var v2 = new String("AEIOUbcdfghjklmnpqrst");
    var v3 = new String("vxyzBCDFGHJKLMNPQRST");
    var v4 = new String("VXYZ$@#");

    p = new String(p);

    for (i = 0; i < p.length; i++){
	
        if (v1.indexOf(p.charAt(i)) != -1) l += 1;
        else if (v2.indexOf(p.charAt(i)) != -1) l += 2;
        else if (v3.indexOf(p.charAt(i)) != -1) l += 3;
        else if (v4.indexOf(p.charAt(i)) != -1) l += 4;
        else l += 5;
    }
    l *= 3;
    if(l > 100)l = 100;



    document.getElementById('password-sec-fill').style.width = l + 'px';

    if (l<33) {
        document.getElementById('password-sec-fill').style.background = 'red';
        document.getElementById('password-sec-alert-text').innerHTML = 'Seguridad baja';
        document.getElementById('password-sec-alert-text').style.color = 'red';
    }else if (l<66){
        document.getElementById('password-sec-fill').style.background = 'orange';
        document.getElementById('password-sec-alert-text').innerHTML = 'Seguridad media';
        document.getElementById('password-sec-alert-text').style.color = 'orange';
    }else{
        document.getElementById('password-sec-fill').style.background = '#00ff00';
        document.getElementById('password-sec-alert-text').innerHTML = 'Seguridad alta';
        document.getElementById('password-sec-alert-text').style.color = '#00ff00';
    }

}


function formEmptyField(ob) {
    ob.value = "";
}

function displayImageBox() {
    var ob;
    var baseID = "h_img";
    for (var i=1; i<=5; i++) {
        if ((ob=document.getElementById(baseID + i)) && ob.style.display == "none") {
            ob.style.display = "block";
            break;
        }
    }
}

function displayHidePanel(id) {
    if (document.getElementById(id))
    {
        switch (document.getElementById(id).style.display) {
            case 'block':
                hidePanel(id);
                break;

            default:
                displayPanel(id);
                break;
        }
    }
}

function hidePanel(id) 		{
    document.getElementById(id).style.display = 'none';
}
function displayPanel(id) 	{
    document.getElementById(id).style.display = 'block';
}

function displayHidePanels(ids_arr) {

    for ( var i = 0; i < ids_arr.length; i++) {
        displayHidePanel(ids_arr[i]);
    }
}

function hidePanels(ids_arr) 	{
    for ( var i = 0; i < ids_arr.length; i++) hidePanel(ids_arr[i]);
}
function displayPanels(ids_arr) {
    for ( var i = 0; i < ids_arr.length; i++) displayPanel(ids_arr[i]);
}

function displayPanelsC(ids_arr, group)
{
    hidePanel('g1_' + group);
    displayPanel('g2_' + group);

    aux_arr1 = Array();
    aux_arr2 = Array();
    aux_arr3 = Array();
	
    for ( var i = 0; i < ids_arr.length; i++) {
        aux_arr1[i] = 'cat_' + ids_arr[i];
        aux_arr2[i] = 'l1_cat_' + ids_arr[i];
        aux_arr3[i] = 'l2_cat_' + ids_arr[i];
    }
	
    displayPanels(aux_arr1);
    hidePanels(aux_arr2);
    displayPanels(aux_arr3);
	
}

function hidePanelsC(ids_arr, group)
{
    hidePanel('g2_' + group);
    displayPanel('g1_' + group);
		
    aux_arr1 = Array();
    aux_arr2 = Array();
    aux_arr3 = Array();
	
    for ( var i = 0; i < ids_arr.length; i++) {
        aux_arr1[i] = 'cat_' + ids_arr[i];
        aux_arr2[i] = 'l1_cat_' + ids_arr[i];
        aux_arr3[i] = 'l2_cat_' + ids_arr[i];
    }
	
    hidePanels(aux_arr1);
    hidePanels(aux_arr3);
    displayPanels(aux_arr2);
}

function emptyDisableField(id) {
    if (document.getElementById(id).disabled == true) {
        document.getElementById(id).disabled = false;
    } else {
        document.getElementById(id).value = '';
        document.getElementById(id).disabled = true;
    }
}

function extendSearchPanel(value) {
    if (value=='demands')
        document.getElementById('extended_panel').style.display = 'none';
    else
        document.getElementById('extended_panel').style.display = 'block';
}


function utf8_encode (argString ) {
 
    var string = (argString+''); // .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 pendingFunction() {

    alert("Esta funci\xf3n estar\xe1 disponible en breve");
}

function pendingFunction2() {

    alert("Pedimos paciencia, esta funci\xf3n estar\xe1 disponible en breve. Mientras tanto puedes negociar el trueque contactando con el otro truekero mediante mensajes o con el chat.");
}

function chatById(id) {
    var username = document.getElementById(id).value;
    chatWith(username);
}


function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/str_replace    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
     for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}}
    }
    return sa ? s : s[0];
}


function number_format(number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;};
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);}
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }return s.join(dec);
}


function checkLimit(num, limit, value, who){
    id = "#" + who + num;
    amount = parseInt($(id).html());
    total = $("#" + who + "_total").html();
    total = str_replace('.', '', total);    
    total = parseInt(total);    
    new_subtotal = amount*value;
    
    subtotal = str_replace('.','',$(id + "_t").html());
    subtotal = parseInt(subtotal);
    
    diff = new_subtotal - subtotal;    
    $(id + "_t").html(number_format(new_subtotal, 0, '', '.'));
    $("#" + who + "_total").html(number_format(total+diff, 0, '', '.'));
    setTotal (who);
    if (limit && amount==limit)
        alert("Has seleccionado el m\xe1ximo de unidades disponibles");
}

function setTotal (who) {
    total = $("#" + who + "_total").html();
    total = str_replace('.', '', total);
    total = parseInt(total);
    credits = $("#" + who + "_credits_value").html();
    credits = str_replace('.', '', credits);
    credits = parseInt(credits);
    result = 0;
    if (total)      result+=total;
    if (credits)    result+=credits;
    $("#" + who + "_total2").html(number_format(result, 0, '', '.'));
}
