// JavaScript Document

var _stdCMS={
	
	rayarTabla:function (idTabla,estilo,ignorar){
		var tabla=document.getElementById(idTabla);
		var trs=tabla.getElementsByTagName('TR');
		
		j=0;
		for(var i=0; i<trs.length; i++){
			if(i<ignorar){
				continue;
			}
			//alert(i);
			trs[i].className=estilo+j;
			if(j==0){
				j=1;
			}
			else{
				j=0;
			}
		}
	 },
	 
	 disableEnterKey:function (e){
     	var key;
     	if(window.event){
          key = window.event.keyCode;     //IE
		}else{
          key = e.which;     //firefox
		}
     	if(key == 13){
          return false;
		}else{
          return true;
		}
	}
}

function _stdCMSListado(){
	this.rowModel=null;
	this.areaListado=null;
	this.areaPaginacion=null;
	this.paginaInicio=0;
	this.objListado={'data':{'inicio':0,'qty':-1,'next':-1,'prev':-1,'total':-1}};
	this.formatos=new Array();
	this.urlBasico=null;
	this.varInicio=null;
	this.varQty=null;
	this.modelRow=null;
	this.indFrom=null;
	this.indTo=null;
	this.indQty=null;
	this.idForm=null;
	
	
}
_stdCMSListado.prototype.setForm=function(n){
	this.idForm=n;
}
_stdCMSListado.prototype.setVarInicio=function(n){
	this.varInicio=n;
	
	this.objListado.data.inicio=$("#"+this.idForm+" input[name='"+this.varInicio+"']").attr('value');
	//alert(this.objListado.data.inicio);
}
_stdCMSListado.prototype.setVarQty=function(n){
	this.varQty=n;
	this.objListado.data.qty=$("#"+this.idForm+" input[name='"+this.varQty+"']").attr('value');
}
_stdCMSListado.prototype.setIndicadorFrom=function(n){
	this.indFrom=n;
}
_stdCMSListado.prototype.setIndicadorTo=function(n){
	this.indTo=n;
}
_stdCMSListado.prototype.setIndicadorQty=function(n){
	this.indQty=n;
}
_stdCMSListado.prototype.setPaginacion=function(n){
	this.objListado.data.qty=n;
}
_stdCMSListado.prototype.getListado=function(pagina,callBack){
	var from;
	
	
	if(pagina=='first'){
		from=0;
	}
	if(pagina=='next'){
		if(this.objListado.data.next!=''){
			from=this.objListado.data.next;
		}
		else{
			from=this.objListado.data.inicio;
		}
	}
	if(pagina=='refresh'){
		from=this.objListado.data.inicio;
	}
	if(pagina=='prev'){
		if(this.objListado.data.prev!=''){
			from=this.objListado.data.prev;
		}
		else{
			from=this.objListado.data.inicio;
		}
	}
	if(pagina=='last'){
		if((this.objListado.data.total%this.objListado.data.qty)==0){
			from=this.objListado.data.total-this.objListado.data.qty;
			
		}
		else{
			from=parseInt(this.objListado.data.total/this.objListado.data.qty)*this.objListado.data.qty;
			
		}
	}
	$("#"+this.idForm+" input[name='"+this.varInicio+"']").attr('value',from);
	//_stdAjax.getString(this.urlBasico+'&'+this.varInicio+'='+from+'&'+this.varQty+'='+this.objListado.data.qty+''+'&rand='+Math.random(),callBack);
	_stdAjax.sendForm(this.idForm,$("#"+this.idForm).attr('action'),callBack);
}
_stdCMSListado.prototype.loadListado=function(string){
		
	this.objListado=eval(unescape(string));
	
	
	if(this.modelRow==null){
		this.modelRow=$('#'+this.rowModel).html();
	}
	$('#'+this.rowModel).hide();
	$('#'+this.rowModel).html('');
	
	if(this.indFrom!=null){
		
		if(this.objListado.data.inicio!=''){
			if(this.objListado.data.inicio==0 && this.objListado.data.total!=0){
				$('#'+this.indFrom).html('1');
			}
			else{
				$('#'+this.indFrom).html(this.objListado.data.inicio);
			}
		}
		else{
			$('#'+this.indFrom).html(0);
		}
	}
	if(this.indTo!=null){
		if(this.objListado.data.next!=''){
			$('#'+this.indTo).html(this.objListado.data.next);
		}
		else{
			$('#'+this.indTo).html(this.objListado.data.total);
		}
	}
	if(this.indQty!=null){
		$('#'+this.indQty).html(this.objListado.data.total);
	}
	
	
	var row='';
	for(var i=0; i<this.objListado.data.rows.length; i++){
		row=this.modelRow;
		for(valor in this.objListado.data.rows[i]){
			var reg=new RegExp( '#'+valor+'#', "g" );
			row=row.replace(reg,this.format(valor,this.objListado.data.rows[i][valor]));
		}
		//alert(row);
		$('#'+this.rowModel).append(row);
	}
	$('#'+this.rowModel).show();
	
	
	
	
	
}
_stdCMSListado.prototype.getRegistro=function(campo,valor){
	var ArrayData=new Array();
	for(var i=0; i<this.objListado.data.rows.length; i++){
		if(this.objListado.data.rows[i][campo]==valor){
			ArrayData.unshift(this.objListado.data.rows[i]);
		}
	}
	return ArrayData;
}
_stdCMSListado.prototype.addFormat=function(obj){
	this.formatos[obj.tag]=obj;
}
_stdCMSListado.prototype.format=function(valor,data){
	
	
	if(!this.formatos[valor]){
		return data;
	}
	
	obj=this.formatos[valor];
	//alert(obj)
	switch(obj.tipoFormato){
		case 'HTML':
			data=this.htmlEntities(data);
		break;
		case 'DATE':
			data=this.fecha(data, obj.param);
		break;
		case 'NUMBER':
			data=this.formatNumber(data, obj.param);
		break;
		case 'REPLACE':
			data=this.formatReplace(data, obj.param);
		break;
	}
	return data;
}
_stdCMSListado.prototype.htmlEntities=function(text){
	
	var reg;
	text = text.replace( /&/g, '&amp;' );
	text = text.replace( /</g, '&lt;' );
	text = text.replace( />/g, '&gt;' );
	text = text.replace( /"/g, '&quot;' );
	text = text.replace( /í/g, '&iacute;' );
	text = text.replace( /á/g, '&aacute;' );
	text = text.replace( /é/g, '&eacute;' );
	text = text.replace( /ó/g, '&oacute;' );
	text = text.replace( /ú/g, '&uacute;' );
	
	text = text.replace( /Í/g, '&Iacute;' );
	text = text.replace( /Á/g, '&Aacute;' );
	text = text.replace( /É/g, '&Eacute;' );
	text = text.replace( /Ó/g, '&Oacute;' );
	text = text.replace( /Ú/g, '&Uacute;' );
	
	reg=new RegExp( unescape('%D1'), "g" );
	text = text.replace( reg, '&Ntilde;' );
	reg=new RegExp( unescape('%F1'), "g" );
	text = text.replace( reg, '&ntilde;' );

	
	reg=new RegExp( unescape('%A1'), "g" );
	text = text.replace( reg, '&iexcl;' );
	reg=new RegExp( unescape('%A2'), "g" );
	text = text.replace( reg, '&cent;' );
	reg=new RegExp( unescape('%A3'), "g" );
	text = text.replace( reg, '&pound;' );
	reg=new RegExp( unescape('%A4'), "g" );
	text = text.replace( reg, '&curren;' );
	reg=new RegExp( unescape('%A5'), "g" );
	text = text.replace( reg, '&yen;' );
	reg=new RegExp( unescape('%A6'), "g" );
	text = text.replace( reg, '&brvbar;' );
	reg=new RegExp( unescape('%A7'), "g" );
	text = text.replace( reg, '&sect;' );
	reg=new RegExp( unescape('%A8'), "g" );
	text = text.replace( reg, '&uml;' );
	reg=new RegExp( unescape('%A9'), "g" );
	text = text.replace( reg, '&copy;' );
	reg=new RegExp( unescape('%AA'), "g" );
	text = text.replace( reg, '&ordf;' );
	reg=new RegExp( unescape('%AB'), "g" );
	text = text.replace( reg, '&laquo;' );
	reg=new RegExp( unescape('%AC'), "g" );
	text = text.replace( reg, '&not;' );
	reg=new RegExp( unescape('%AD'), "g" );
	text = text.replace( reg, '&shy;' );
	reg=new RegExp( unescape('%AE'), "g" );
	text = text.replace( reg, '&reg;' );
	reg=new RegExp( unescape('%AF'), "g" );
	text = text.replace( reg, '&macr;' );
	reg=new RegExp( unescape('%B0'), "g" );
	text = text.replace( reg, '&deg;' );
	reg=new RegExp( unescape('%B1'), "g" );
	text = text.replace( reg, '&plusmn;' );
	reg=new RegExp( unescape('%B2'), "g" );
	text = text.replace( reg, '&sup2;' );
	reg=new RegExp( unescape('%B3'), "g" );
	text = text.replace( reg, '&sup3;' );
	
	reg=new RegExp( unescape('%B5'), "g" );
	text = text.replace( reg, '&micro;' );
	reg=new RegExp( unescape('%B6'), "g" );
	text = text.replace( reg, '&para;' );
	reg=new RegExp( unescape('%B7'), "g" );
	text = text.replace( reg, '&middot;' );
	reg=new RegExp( unescape('%B8'), "g" );
	text = text.replace( reg, '&cedil;' );
	reg=new RegExp( unescape('%B9'), "g" );
	text = text.replace( reg, '&sup1;' );
	reg=new RegExp( unescape('%BA'), "g" );
	text = text.replace( reg, '&ordm;' );
	reg=new RegExp( unescape('%BB'), "g" );
	text = text.replace( reg, '&raquo;' );
	reg=new RegExp( unescape('%BC'), "g" );
	text = text.replace( reg, '&frac14;' );
	reg=new RegExp( unescape('%BD'), "g" );
	text = text.replace( reg, '&frac12;' );
	reg=new RegExp( unescape('%BE'), "g" );
	text = text.replace( reg, '&frac34;' );
	reg=new RegExp( unescape('%BF'), "g" );
	text = text.replace( reg, '&iquest;' );
	reg=new RegExp( unescape('%C0'), "g" );
	text = text.replace( reg, '&Agrave;' );
	
	reg=new RegExp( unescape('%C2'), "g" );
	text = text.replace( reg, '&Acirc;' );
	reg=new RegExp( unescape('%C3'), "g" );
	text = text.replace( reg, '&Atilde;' );
	reg=new RegExp( unescape('%C4'), "g" );
	text = text.replace( reg, '&Auml;' );
	reg=new RegExp( unescape('%C5'), "g" );
	text = text.replace( reg, '&Aring;' );
	reg=new RegExp( unescape('%C6'), "g" );
	text = text.replace( reg, '&AElig;' );
	reg=new RegExp( unescape('%C7'), "g" );
	text = text.replace( reg, '&Ccedil;' );
	reg=new RegExp( unescape('%C8'), "g" );
	text = text.replace( reg, '&Egrave;' );
	
	reg=new RegExp( unescape('%CA'), "g" );
	text = text.replace( reg, '&Ecirc;' );
	reg=new RegExp( unescape('%CB'), "g" );
	text = text.replace( reg, '&Euml;' );
	reg=new RegExp( unescape('%CC'), "g" );
	text = text.replace( reg, '&Igrave;' );
	
	reg=new RegExp( unescape('%CE'), "g" );
	text = text.replace( reg, '&Icirc;' );
	reg=new RegExp( unescape('%CF'), "g" );
	text = text.replace( reg, '&Iuml;' );
	reg=new RegExp( unescape('%D0'), "g" );
	text = text.replace( reg, '&ETH;' );
	
	reg=new RegExp( unescape('%D2'), "g" );
	text = text.replace( reg, '&Ograve;' );
	
	reg=new RegExp( unescape('%D4'), "g" );
	text = text.replace( reg, '&Ocirc;' );
	reg=new RegExp( unescape('%D5'), "g" );
	text = text.replace( reg, '&Otilde;' );
	reg=new RegExp( unescape('%D6'), "g" );
	text = text.replace( reg, '&Ouml;' );
	reg=new RegExp( unescape('%D7'), "g" );
	text = text.replace( reg, '&times;' );
	reg=new RegExp( unescape('%D8'), "g" );
	text = text.replace( reg, '&Oslash;' );
	reg=new RegExp( unescape('%D9'), "g" );
	text = text.replace( reg, '&Ugrave;' );
	reg=new RegExp( unescape('%DA'), "g" );
	text = text.replace( reg, '&Uacute;' );
	reg=new RegExp( unescape('%DB'), "g" );
	text = text.replace( reg, '&Ucirc;' );
	reg=new RegExp( unescape('%DC'), "g" );
	text = text.replace( reg, '&Uuml;' );
	reg=new RegExp( unescape('%DD'), "g" );
	text = text.replace( reg, '&Yacute;' );
	reg=new RegExp( unescape('%DE'), "g" );
	text = text.replace( reg, '&THORN;' );
	reg=new RegExp( unescape('%DF'), "g" );
	text = text.replace( reg, '&szlig;' );
	reg=new RegExp( unescape('%E0'), "g" );
	text = text.replace( reg, '&agrave;' );
	reg=new RegExp( unescape('%E1'), "g" );
	text = text.replace( reg, '&aacute;' );
	reg=new RegExp( unescape('%E2'), "g" );
	text = text.replace( reg, '&acirc;' );
	reg=new RegExp( unescape('%E3'), "g" );
	text = text.replace( reg, '&atilde;' );
	reg=new RegExp( unescape('%E4'), "g" );
	text = text.replace( reg, '&auml;' );
	reg=new RegExp( unescape('%E5'), "g" );
	text = text.replace( reg, '&aring;' );
	reg=new RegExp( unescape('%E6'), "g" );
	text = text.replace( reg, '&aelig;' );
	reg=new RegExp( unescape('%E7'), "g" );
	text = text.replace( reg, '&ccedil;' );
	reg=new RegExp( unescape('%E8'), "g" );
	text = text.replace( reg, '&egrave;' );
	
	reg=new RegExp( unescape('%EA'), "g" );
	text = text.replace( reg, '&ecirc;' );
	reg=new RegExp( unescape('%EB'), "g" );
	text = text.replace( reg, '&euml;' );
	reg=new RegExp( unescape('%EC'), "g" );
	text = text.replace( reg, '&igrave;' );
	
	reg=new RegExp( unescape('%EE'), "g" );
	text = text.replace( reg, '&icirc;' );
	reg=new RegExp( unescape('%EF'), "g" );
	text = text.replace( reg, '&iuml;' );
	reg=new RegExp( unescape('%F0'), "g" );
	text = text.replace( reg, '&eth;' );
	
	reg=new RegExp( unescape('%F2'), "g" );
	text = text.replace( reg, '&ograve;' );
	
	reg=new RegExp( unescape('%F4'), "g" );
	text = text.replace( reg, '&ocirc;' );
	reg=new RegExp( unescape('%F5'), "g" );
	text = text.replace( reg, '&otilde;' );
	reg=new RegExp( unescape('%F6'), "g" );
	text = text.replace( reg, '&ouml;' );
	reg=new RegExp( unescape('%F7'), "g" );
	text = text.replace( reg, '&divide;' );
	reg=new RegExp( unescape('%F8'), "g" );
	text = text.replace( reg, '&oslash;' );
	reg=new RegExp( unescape('%F9'), "g" );
	text = text.replace( reg, '&ugrave;' );
	reg=new RegExp( unescape('%FA'), "g" );
	text = text.replace( reg, '&uacute;' );
	reg=new RegExp( unescape('%FB'), "g" );
	text = text.replace( reg, '&ucirc;' );
	reg=new RegExp( unescape('%FC'), "g" );
	text = text.replace( reg, '&uuml;' );
	reg=new RegExp( unescape('%FD'), "g" );
	text = text.replace( reg, '&yacute;' );
	reg=new RegExp( unescape('%FE'), "g" );
	text = text.replace( reg, '&thorn;' );
	reg=new RegExp( unescape('%FF'), "g" );
	text = text.replace( reg, '&yuml;' );
	
	return text;
   
}
_stdCMSListado.prototype.fecha=function(input, modelo){
    if(input==""){
		return "";
	}
	var hora='';
	var minuto='';
	
	input=input.replace(/^\s*/, "").replace(/\s*$/, "");
	
	if(input.indexOf(' ')!=-1){
		var partesPrincipales=input.split(' ');
		if(partesPrincipales[0].length>partesPrincipales[1].length){
			input=partesPrincipales[0];
			time=partesPrincipales[1];
		}else{
			input=partesPrincipales[1];
			time=partesPrincipales[0];
		}
		var separador_time=null;
		if(time.indexOf(':')!=-1){
			separador_time=':';
		}
		if(separador_time!=null){
			partes_time=time.split(':');
			hora=partes_time[0];
			minuto=partes_time[1];
		}
		else{
			hora=time.substr(0,2);
			minuto=time.substr(2,2);
		}
	}

	var dia=null;
	var mes=null;
	var anio=null;
	var separador=null;
	
	if(input.indexOf('/')!=-1){
		separador='/';
	}
	if(input.indexOf('-')!=-1){
		separador='-';
	}
	if(input.indexOf('.')!=-1){
		separador='.';
	}
	if(separador!=null){
		var partes=input.split(separador);
		if(partes[0].length==4){
			anio=partes[0];
			mes=partes[1];
			dia=partes[2];
		}
		else{
			anio=partes[2];
			mes=partes[1];
			dia=partes[0];
		}
	}
	else{
		
		if(input.length==8){
			anio=input.substr(0,4);
			mes=input.substr(4,2);
			dia=input.substr(6,2);
		}
		if(input.length==12){
			anio=input.substr(0,4);
			mes=input.substr(4,2);
			dia=input.substr(6,2);
			hora=input.substr(8,10);
			minuto=input.substr(10,12);
		}
	}
	anio_2cifras=anio.substr(2,2);
	fecha_final=modelo;
	modelo=modelo.replace('Y',anio);
	modelo=modelo.replace('m',mes);
	modelo=modelo.replace('d',dia);
	modelo=modelo.replace('y',anio_2cifras);
	modelo=modelo.replace('h',hora);
	modelo=modelo.replace('i',minuto);
	return modelo;
}
_stdCMSListado.prototype.formatNumber=function(input, modelo){
	var cant_enteros=null;
	var cant_decimales=null;
	var separador=',';
	var partes;
	if(modelo.indexOf('d')!=-1){
		partes=modelo.split('d');
		cant_enteros=partes[0].substr(partes[0].length-1,1);
	}
	
	if(modelo.indexOf('f')!=-1){
		partes=modelo.split('f');
		cant_decimales=partes[0].substr(partes[0].length-1,1);
	}
	if(modelo.indexOf('.')!=-1){
		separador='.';
	}
	if(modelo.indexOf(',')!=-1){
		separador=',';
	}
	
	
	var enteros;
	var decimales;
	
	if(input.indexOf('.')==-1 && input.indexOf(',')==-1){
		decimales='';
		enteros=input;
	}
	if(input.indexOf('.')!=-1){
		partes=modelo.split('.');
		decimales=parseInt(partes[1]);
		enteros=parseInt(partes[0]);
	}
	if(input.indexOf(',')!=-1){
		partes=modelo.split(',');
		decimales=parseInt(partes[1]);
		enteros=parseInt(partes[0]);
	}
	
	
	
	if(cant_decimales!=null && cant_decimales!='0'){
		cant_decimales=parseInt(cant_decimales);
		var decimales='0';
		
		while(decimales.length<cant_decimales){
			decimales=decimales+'0';
		}
	}
	
	
	if(cant_enteros!=null){
		var enterosStr=new String(enteros);
		if(parseInt(cant_enteros)>enterosStr.length){
			
			while(enterosStr.length<cant_enteros){
				enterosStr='0'+enterosStr;
			}
		}
	}
	else{
		var enterosStr=new String(parseInt(input));
	}
	
	return enterosStr+separador+decimales;
}
_stdCMSListado.prototype.formatReplace=function(input, modelo){
	for(id in modelo){
		input=input.replace(id,modelo[id]);
	}
	return input;
}

// JavaScript clase Ajax
var _stdAjax={
	
	nuevoAjax:function(){
		var xmlhttp;
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2) {
				xmlhttp=false;
			}
		//alert(xmlhttp);
		}
		if (typeof xmlhttp=='boolean' && typeof XMLHttpRequest!='undefined')	{
				xmlhttp=new XMLHttpRequest();
		}
		return xmlhttp;
	},

	getString:function(pagina,callBack){
		var dato="";
		var ajax=_stdAjax.nuevoAjax();
		ajax.open("get", pagina,true);
		ajax.onreadystatechange=function(){
			if (ajax.readyState==4){
				dato='window.'+callBack+"('"+escape(ajax.responseText)+"')";
				//alert (dato);
				eval(dato);
				
			}
			else{

			}
		}
		ajax.send(null);
	},
	
	sendString:function(pagina){
		var ajax=_stdAjax.nuevoAjax();
		ajax.open("get", pagina,true);
		ajax.send(null);
	},
	
	sendForm:function(idForm,pagina,callBack){
		var parameters=_stdAjax.packForm(idForm);
		var ajax=_stdAjax.nuevoAjax();
		ajax.open("post", pagina,true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	ajax.setRequestHeader("Content-length", parameters.length);
   		ajax.setRequestHeader("Connection", "close");
   		ajax.send(parameters);
		ajax.onreadystatechange=function(){
			if (ajax.readyState==4){
				dato='window.'+callBack+"('"+escape(ajax.responseText)+"')";
				//alert (ajax.responseText);
				eval(dato);
				
			}
			else{

			}
		}
	},
	
	packForm:function(idForm){
		var formulario=document.getElementById(idForm);
		var largo=formulario.length;
		var parametros="";
		for (i=0; i<largo; i++){
			if (formulario.elements[i].type=='radio' || formulario.elements[i].type=='checkbox'){
				if (formulario.elements[i].checked==false){
				//alert (formulario.elements[i].type+" "+formulario.elements[i].checked);
				continue;
				}
			}
			parametros=parametros+formulario.elements[i].name+'='+ encodeURI(formulario.elements[i].value)+'&';
		}
		parametros=parametros+'rand='+Math.random();
		
		return parametros;
	}
	
	
}
