//utils lib

/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}


function array_delEmpty(array) {
	//le borro las comillas a las keywords
	for (i = 0; i < array.length; i++) {
		array[i] = array[i].replace(/"/ig, "");
	}
	
	var tmp = [];
	//elimino los elementos en blanco o vacios
	for (i = 0; i < array.length; i++) {
		if ( (array[i] != ' ') && (array[i] != '') )
			tmp[tmp.length++] = array[i];
	}
	
	return tmp;
}


/**
 * Formato de dinero a un string
 *
 */
function fmtMoney(n, c, d, t){
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
        /(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
    return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
        "$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
}
/**
 * Ordenamiento de arrays
 *
 */
function SortRank(a,b) {
if (a['RANK']<b['RANK']) return -1;
  if (a['RANK']>b['RANK']) return 1;
  return 0;
}

function SortPrice(a,b){
if (a['PRICE']<b['PRICE']) return -1;
  if (a['PRICE']>b['PRICE']) return 1;
  return 0;
}
function SortCat(a,b){
if (a[1]>b[1]) return -1;
  if (a[1]<b[1]) return 1;
  return 0;
}

/**
 * string al cual se le reemplazan 
 * los caracteres especiales por "-"
 */
function GetURL(name) {
name = name.replace(/[\\\|\\/\:\*\?\<\>\ ]/g, '-');

	name = name.toLowerCase();
	return(name);
}

function adjust_text(text, line_length) {
	
	var guion = text.split('-');
	var guiones = guion.length;
	var space = text.split(' ');
	var spaces = space.length;
	
		if (spaces >= guiones) {var array = space;}
		else {var array = guion;}
	
	var result = new Array(2);
	var _name = String('');
	var _descr = String('');
	
	for (i=0; i < array.length; i++) {
		newlen = _name.length + array[i].length;
			if ( newlen <= line_length){
				_name+= array[i]+' ';
			} else {
				last = i;
				break;
			}
			last=i+1;
	}
	
	for (i=last; i < array.length; i++) {_descr+= array[i]+' ';}
	
	
	var texto = String(text);
	result[0]= _name.substr(0, 25);
	result[1]= (_descr.length > 25) ? _descr.substr(0, 22)+'...' : _descr;
	
	//result[0] = texto.substr(0, 25);
	//result[1] = texto.substr(25)
	
	return (result);
}

function getCategoStr(id) {
	
	if (id == 0) return 'resultados';
	
	for (var i = 0; i < categorias.length; i++) {
		if ( id == categorias[i]['ID'] ) return categorias[i]['NAME'].toLowerCase();
	}
}

function getCategoStr2(id) { 
	if (id == 0) return false;
	
	for (var d = 0; d < categorias.length; d++) {
		if ( id == categorias[d]['ID'] ) return categorias[d]['NAME'];
	}
}

function keywordsInStr(keys, str) {
	for (i = 0; i < keys.length; i++) {
		if ( str.toLowerCase().indexOf(keys[i].toLowerCase()) == -1 ) return false;
	}
	return true;
}

function disable(bool) {
	
	var query = document.getElementById('query');
	var okbutt = document.getElementById('ok');
	
	if (!bool) {
		query.value = '';
		//query.removeAttribute("disabled");
		//okbutt.removeAttribute("disabled");
		//okbutt.disabled = false;
		//query.disabled = false;

	} else {
	//okbutt.disabled = true;
	//query.disabled = true;
	query.value = 'Espere por favor';
	}
}


function include(file, id){
var head = document.getElementsByTagName('head').item(0)
var scriptTag = document.getElementById('db'+id);
  if(scriptTag) {
	  head.removeChild(scriptTag); 
	}
  script = document.createElement('script');
  script.src = file;
	script.type = 'text/javascript';
	script.id = 'db'+id;
	head.appendChild(script);
}


function include_files(_cat) {

	if (_cat == 0) {
		articulos = []; 
		disable(true);
		include("db/db0.js", 0);
	} else { 

		for (var k = 0; k < document.browser.elements['where'].length ; k++) {
			if ( document.browser.elements['where'][k].checked ) {
				_cat = document.browser.elements['where'][k].value; 
				break;
			}
		}
		
		articulos = []; 
		include("db/db" + _cat + ".js", _cat);
	}	
}

function include_db(_categoria) {
if (_categoria == 0) {
	include_files(0);
	} else {
	articulos = []; 
	include("db/db" + _categoria + ".js", _categoria);
	}
}

/*
// DEPRECATED
function include_tiendas() {
tiendas = [];
include('db/tiendas.js', 'tiendas');
}*/

function getTopList(array) {
	array.sort(SortRank);

	return array[0];	
}
