
YAHOO.namespace('ext');

YAHOO.ext.DomHelper = Ext.DomHelper;
YAHOO.ext.grid = Ext.grid;
YAHOO.ext.grid.DataEditor = Ext.grid.DataEditor;


// creacion de metodo largoMaximo para los objetos de tipo string
String.prototype.largoMaximo = function(maxLength){
    if(this.length > maxLength){
        return this.substr(0, maxLength-3) + '...';
    }
    return this;
};
// creacion de metodo largoMaximo para los objetos de tipo string
String.prototype.quitarAcentos = function(){
	var ret = this;
	ret = ret.replace(/á/g,'a');
	ret = ret.replace(/é/g,'e');
	ret = ret.replace(/í/g,'i');
	ret = ret.replace(/ó/g,'o');
	ret = ret.replace(/ú/g,'u');
	ret = ret.replace(/Á/g,'A');
	ret = ret.replace(/É/g,'E');
	ret = ret.replace(/Í/g,'I');
	ret = ret.replace(/Ó/g,'O');
	ret = ret.replace(/Ú/g,'U');
    return ret;
};

// Decodifica un texto retornado via ajax  
String.prototype.htmlDecode = function(){
	var aux;
	var remplaza = /\+/gi; 
	
	aux= unescape(this);
	aux= aux.replace(remplaza, " ");
	return(aux);
};

function getEl(id) {
	return Ext.get(id);
}

document.getCookie = function (name){
  var cname = name + "=";               
  var dc = document.cookie;
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
};

document.setCookie = function (cookieName,cookieValue,nDays) {

	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
 	document.cookie = cookieName+"="+escape(cookieValue)
     	            + ";expires="+expire.toGMTString();
	return null;					
};

loadList = function(list, str, optionSep, valueSep, selected, defvalue, deftext){
	var ix = 0;
	if (!optionSep || !valueSep){return;}
	list.options.length = 0
	if (deftext || deftext){
		list.options[ix] = new Option(deftext,defvalue);ix++;
	}
	arr = str.split(optionSep)
	for(i = 0; i < arr.length-1; i++){
		arr2 = arr[i].split(valueSep)
		list.options[i+ix] = new Option(arr2[1],arr2[0])
		list.options[i+ix].selected = (arr2[0] == selected) ? true: false
	}
};

dateDiff = function(f1, f2){
	v1 = f1.value
	v2 = f2.value
	value = 0
	if (v1 && v2){
		datDate1 = Date.parse(Date.fromUKFormat(v1)); 
		datDate2 = Date.parse(Date.fromUKFormat(v2)); 
		value = (((datDate2-datDate1) / (24*60*60*1000))+1);
	}
	return value
};

Date.fromUKFormat = function(sUK) {   
	var A = sUK.split(/[\\\/]/); A = [A[1],A[0],A[2]];   
	return new Date(Date.parse(A.join('/'))); 
}; 

// validacion con keypress
isCtrlCar = function(k){
	return (key == 8 || key == 9)
}
// solo digitos
D = function(){
	key = (document.all)?event.keyCode:event.which;
	if(isCtrlCar(key)) return true
	if(key < 48 || key > 57){
		return false	
	}
	return true
}

validacionNumerica = function(){
	return D();
}

validacionHora = function (o){
	if(Ext.isIE){
		key = (document.all)?event.keyCode:event.which;
		
		if(isCtrlCar(key)) return true;
		if(((key >= 33 && key <= 39)||(key == 46))) return false;
		if (!((key >= 48 &&  key <= 57)||(key >= 96 &&  key <= 105))) return false;
		
		charKey = (key <= 57)? key-48: key-96;
		
		if(o.value.length == 0)  return (charKey >= 0 && charKey <= 2);
		if(o.value.length == 1){
				if ((o.value*10)+charKey < 24){
					o.value += charKey + ':';
					return false
				}
				return false
				
		}
		if(o.value.length == 2){
				if (charKey < 6){
					o.value += ':' + charKey;
					return false
				}
				return false
		
		}
		if(o.value.length == 3)  return (charKey >= 0 && charKey <= 5);
		if(o.value.length == 4)  return (charKey >= 0 && charKey <= 9);
		
		return false
	}
}

// funcion provisoria
crearMantenedor = function(titulo, tipo, padre, obj){
	config = {
		titulo    : titulo,
		dialog    : {
						 autoTabs	: true,
						 width		: 420,
						 height		: 500,
						 shadow		: true,
						 closable	: true,
						 collapsible: false,
						 modal		: true,
						 minWidth	: 150,
						 minHeight	: 80						 
					},
		tipoId	  : tipo,
		padreId	  : padre,
		listaSelect : obj
	};
	m = new MantenedorLV(config);
	m.init();
}

// funcion provisoria
crearMantenedorPTT = function(titulo, subTitulo, tipoId, obj){
	
	if (tipoId != '0'){
		config = {
			titulo    : titulo,
			subTitulo : 'Plantilla: ' + subTitulo,
			dialog    : {
							 autoTabs	: true,
							 width		: 630,
							 height		: 500,
							 shadow		: true,
							 closable	: true,
							 collapsible: false,
							 modal		: true,
							 minWidth	: 150,
							 minHeight	: 80						 
						},
			tipoId	  : tipoId,
			objeto 	  : obj
		};
		m = new MantenedorPTT(config);
		m.init();
	}
}


obtenerDV = function (rut) {
	// generalizar 
	var mult = 2;
	var indi = rut.length - 1;
	var suma = 0;
	while (indi >= 0) {
		suma += rut.charAt(indi) * mult;
		mult++;
		if (mult > 7) mult = 2;
		indi--;
	}
	rest = suma % 11;
	digito = 11 - rest;
	if (digito == 11) digito = 0;
	if (digito == 10) digito = "K";
	return digito;
}


