/////////////////agregar trim
	
String.prototype.trim=function()
{	
	return this.replace(/^[\s\t\r\n]+|[\s\t\r\n]+$/g,'');	
};

String.prototype.isEmpty=function()
{	
	return this.trim().length==0?true:false;
};
	
/////////////////////////////////

/////////agregar escape//////
String.prototype.escape=function()
{
	return escape(this);
};
/////////////////////////////////

String.prototype.parseInt=function()
{
	return parseInt(this,10);
};

String.prototype.parseFloat=function()
{
	return parseFloat(this);
};

String.prototype.isInt=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	var regex=/^[\d]+$/
	return regex.test(this);
};

String.prototype.isIntSigned=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	var regex=/^-?[\d]+$/
	return regex.test(this);
};

String.prototype.isFloat=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	regex=/^-?[\d]*([\.][\d])?[\d]*$/
	return regex.test(this)
};

String.prototype.isAlpha=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	regex=/^([a-z])+$/
	return regex.test(this.toLowerCase());
};

String.prototype.isNomina=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	regex=/^L(\d){8}$/
	return regex.test(this)
};

String.prototype.isDate=function()
{	
	regex=/^[\d]{1,2}-[\d]{2}-[\d]{4}$/
	if(!regex.test(this))
	{
		return false;
	}
	var fechas=this.split('-');	
	var dia=fechas[0];
	var mes=fechas[1];
	var anio=fechas[2];
	if(dia.parseInt()>31 || mes>12 )
	{
		return false;
	}
	var meses=new Array(1,3,5,7,8,10,12);
	if(!meses.inArray(mes.parseInt()))
	{
		if(dia.parseInt()>30)
		{
			return false;
		}
	}
	if(!isAnioBisiesto(anio.parseInt()) && mes.parseInt()==2 && dia.parseInt()>28 )
	{
		return false;
	}
	if(isAnioBisiesto(anio.parseInt()) && mes.parseInt()==2 && dia.parseInt()>29)
	{
		return false;
	}
	return true;	
};

String.prototype.isTime=function()
{
	regex=/^[\d]{2}:[\d]{2}(:[\d]{2})?$/
	if(!regex.test(this))
	{
		return false;
	}
	var mHora=this.split(':');
	if(mHora[0].parseInt()>24 || mHora[1].parseInt()>59)
	{
		return false;
	}
	if(mHora[2])
	{
		if(mHora[2].parseInt()>59)
		{
			return false;
		}
	}
	return true;
};


////////////////////////////////////////////////////

String.prototype.isEmail = function (vacio) 
{ 
	vacio=vacio==null?false:vacio;
	if(!vacio)
	{
		if(this.isEmpty())
		{
			return true;
		}
	}
	regex=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	return regex.test(this.trim())
};

///////////////////////////
String.prototype.isIntFormated=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	var regex=/^[\d]([,]?[\d])*$/
	return regex.test(this);
};


//////////////////////////////////////////////////

function teclas(e)
{
	var key;	
	if(document.all)
	{
		key=e.keyCode;
	}
	else
	{
		key=e.which;
	}	
	 return key;
};

/////////////agregar inArray a la clase Array
///////Para saber si un dato se encuentra dento de un array
Array.prototype.inArray=function(dato,regex)
{
	if(dato==null)
	{
		return false;
	}		
	for(var i=0;i<this.length;i++)
	{
		if(regex)
		{			
			if(dato.test(this[i]))
			{
				return true;
			}
			continue;
		}
		if(dato==this[i])
		{
			return true;
		}
	}
	return false;	
};
function selText(inicio,fin,campo)
{ 
	input=campo;
	if(typeof document.selection != 'undefined' && document.selection)
	{ 
		tex=input.value; 
		input.value=''; 
		input.focus(); 
		var str = document.selection.createRange(); 
		input.value=tex; 
		str.move('character', inicio); 
		str.moveEnd("character", fin-inicio); 
		str.select(); 
	} 
	else if(typeof input.selectionStart != 'undefined')
	{ 
		input.setSelectionRange(inicio,fin); 
		input.focus(); 
	} 
}

function numeroAleatorio(tamano)
{
	var cad="";
	for(var i=0;i<tamano;i++)
	{
		var randomnumber=Math.floor(Math.random()*11);
		cad+=randomnumber;
	}
	return cad.parseInt();
}
function isAnioBisiesto(iAnio)
{
	if(iAnio %4==0)
	{
		if(iAnio%100==0 )
		{
			if(iAnio%400==0)
			{
				return true;
			}
			return false;
		}
		return true;
	}
	return false;
}
function cambiarFondo(campo,tipo,color,tool)
{
	campo=$(campo);
	if(tipo==1)
	{
		if(campo.up(".contenedorCampo"))
		{
			var clase=color=="#FFB0B0"?"contenedorCampoRosa":"contenedorCampoAmarillo";
			campo.up(".contenedorCampo").addClassName(clase);
			campo.setStyle({backgraund:"transparent"});
			return;
		}
		campo.setStyle({background:color+' url(../images/icons/warning.gif) no-repeat scroll right'});
	}
	else
	{
		$(campo).onFocus=null;
		if(campo.up(".contenedorCampo"))
		{
			campo.up(".contenedorCampo").removeClassName("contenedorCampoRosa");
			campo.setStyle({backgraund:"transparent"});
			return;
		}
		color=color==null?"white":color;
		campo.setStyle({background:color,paddingRight:"0"});
	}
}

function cambiarBackground(elemento,color)
{
	$(elemento).setStyle({background:color});
}

function makeDate(dias,meses,anios,campoModificar)
{			
	var dia=dias.value;
	var mes=meses.value;
	var anio=anios.value;
	switch(mes.parseInt())
	{		
		case 2:
		{
			if(dia.parseInt()>=29)
			{				
				var iDia=dia.parseInt();				
				var iAnio=anio.parseInt();
				if(isAnioBisiesto(iAnio))
				{
					dias.options[29].selected=true;
					break;
				}				
				dias.options[28].selected=true;
			}
			break;
		}		
		case 4:
		case 6:
		case 9:
		case 11:
		{			
			if(dia.parseInt()>30)
			{
				dias.options[29].selected=true;
			}
			break;
		}		
	}
	dia=dias.value;
	var fecha=anio+"-"+mes+"-"+dia;	
	campoModificar.value=fecha;			
	return true;
}


function cambiarFecha(fecha)//cambia la fecha de dd-mm-aa A aa-mm-dd
{
	if(fecha.isEmpty())
	{		
		return fecha;
	}
	var matFecha=fecha.split('-');
	var matResultado=new Array(matFecha[2],matFecha[1],matFecha[0]);
	return(matResultado.join('-'));
}

function ventana(direccion,nombre)
{
	var parametros="width=800,height=600,directories=no,location=no,status=no,toolbar=no,resizable=yes,scrollbars=yes";
	window.open(direccion,nombre,parametros);
}

function insertarMatrizSelect(select,matriz)
{		 
	select=$(select);
	select.length=matriz.length;
	for(var i=0;i<select.length;i++)
	{
		select.options[i].value=matriz[i].value;
		select.options[i].text=matriz[i].text;
	}
}

function insertarMatrizSelect2(select,matriz)
{	
	select=$(select);
	select.length=matriz.length;
	for(var i=0;i<select.length;i++)
	{
		select.options[i].value=matriz[i].value;
		select.options[i].text=matriz[i].display;
	}
}

function addMatrizSelect(select,matriz)
{	
	select=$(select);
	var n=select.length;
	var j=0;
	select.length+=matriz.length;
	for(var i=n;i<select.length;i++)
	{
		select.options[i].value=matriz[j].value;
		select.options[i].text=matriz[j++].text;
	}
}

function cargarSelect(select,valor)
{
	var sel=$(select);
	for(var i=0;i<sel.options.length;i++)
	{
		if(sel.options[i].value==valor)
		{
			sel.options[i].selected=true;
			return;
		}
	}
}


function validarFormulario(f)
{	
	var colorVacio="#FFB0B0";
	var colorError="#FFFF99";
	var flag=true;
	var campo=null;
	if(f==null)
	{
		var formu=$("formu");
	}
	else
	{
		formu=$(f);
	}
	var mat;
	if(formu.tagName.toLowerCase()=="form")
	{
		mat = $$("#" + formu.id + " *[val],#"+formu.id+" *[max],#"+formu.id+" *[ref],#"+formu.id+" *[ext]");
	}
	else
	{
		mat=new Array(formu);
	}
	mat.each(function(temp)
	{
		temp.setStyle({background:'white'});
		if((temp.disabled || temp.readOnly) && !temp.readAttribute("forceVal"))
		{
			    return;
		}
		if (temp.getAttribute("val")) 
		{
			var matVal = temp.getAttribute("val").split('-');
			
			if (matVal.inArray('v')) 
			{
				if (temp.value.isEmpty()) 
				{
					cambiarFondo(temp, 1, colorVacio,"El campo no puede estar vacio");
					flag = false;
					campo = !campo ? temp : campo;
				}
				else
				{
					cambiarFondo(temp);
				}
			}
			if(temp.value.isEmpty())
			{
				return;
			}
			if (matVal.inArray('i')) 
			{
				if (!temp.value.isInt()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('if')) 
			{
				if (!temp.value.isIntFormated()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('f')) 
			{
				if (!temp.value.isFloat()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('d'))//Valida fecha ejemplo 30-12-2001
			{
				if (!temp.value.isDate()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('si')) 
			{
				if (!temp.value.isIntSigned()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('e'))//Validar e-mail
			{
				if (!temp.value.isEmail()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('t')) //Validar hora ejem: 12:59
			{
				if (!temp.value.isTime()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('alpha')) //Validar que sean puras letras del abecedario
			{
				if (!temp.value.isAlpha()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('nomina')) //Validar que sea numero de nómina válido del ITESM
			{
				if (!temp.value.isNomina()) 
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
			if (matVal.inArray('user')) //Validar que sea un nombre de usuario
			{
				if(temp.value.isEmpty())
				{
					return false;
				}
				regex=/^[a-zA-Z]\w{1,8}$/
				if(!regex.test(temp.value))
				{
					cambiarFondo(temp, 1, colorError);
					flag = false;
					campo = !campo ? temp : campo;
				}
			}
		}
		if(temp.readAttribute("max"))
		{
			if($F(temp).length>temp.readAttribute("max").parseInt())
			{
				cambiarFondo(temp,1,colorError);
				flag=false;
				campo=!campo?temp:campo;
			}
		}
		if(temp.readAttribute("valorMin"))
		{
			if($F(temp).parseFloat()<temp.readAttribute("valorMin").parseFloat())
			{
				cambiarFondo(temp,1,colorError);
				flag=false;
				campo=!campo?temp:campo;
			}
		}
		if(temp.readAttribute("valorMax"))
		{
			if($F(temp).parseFloat()>temp.readAttribute("valorMax").parseFloat())
			{
				cambiarFondo(temp,1,colorError);
				flag=false;
				campo=!campo?temp:campo;
			}
		}
		if(temp.readAttribute("ref"))
		{
			if(!$(temp.readAttribute("ref")))
			{
				return;
			}
			if($F(temp)!=$F(temp.readAttribute("ref")))
			{
				cambiarFondo(temp,1,colorError);
				cambiarFondo($(temp.readAttribute("ref")),1,colorError);
				flag=false;
				campo=!campo?temp:campo;
			}
			else
			{
				cambiarFondo(temp);
			}
		}
		if(temp.readAttribute("ext"))
		{
			var mImg;
			if(temp.readAttribute("ext")=="img")
			{
				mImg=$w("jpg gif png bmp");
			}
			else
			{
				mImg=temp.readAttribute("ext").split(',');
			}
			if(!temp.value.isEmpty())
			{
				var exts=temp.value.split('.');
				if(!mImg.inArray(exts[exts.length-1]))
				{
					cambiarFondo(temp,1,colorError);
					flag=false;
					campo=!campo?temp:campo;
				}
			}
			
		}
		if(temp.readAttribute("noext"))
		{
			var mImg;
			if(temp.readAttribute("noext")=="pages")
			{
				mImg=$w("html php js css htm asp jsp");
			}
			else
			{
				mImg=temp.readAttribute("noext").split(',');
			}
			if(!temp.value.isEmpty())
			{
				var exts=temp.value.split('.');
				if(mImg.inArray(exts[exts.length-1]))
				{
					cambiarFondo(temp,1,colorError);
					flag=false;
					campo=!campo?temp:campo;
				}
			}
			
		}
	});
	
	if(!flag)
	{
		$(campo).activate();
	}
	return flag;
}

function obtenerPagina()
{
	var pagina=null;
	if($("tablaResultados"))
	{
		pagina=$("tablaResultados").readAttribute("pagina");
	}
	return pagina;
}

var GET;
var COOKIES;


function setCookies() {
	var cookiesArr = new Array();
	var cookies = document.cookie.split(";");
	var i = 0;
	var value;
	while(cookies[i] != null) {
		value = cookies[i].split("=");
		cookiesArr[trim(value[0])] = value[1];
		i++;
	}
	COOKIES = cookiesArr;
}

function ocultarFlash(valor)
{
	if(!(Prototype.Browser.Gecko && navigator.platform.toLowerCase().indexOf('linux')!=-1))
	{
		return;
	}
	if(valor)
	{
		$$("object").invoke("hide")
		return;
	}
	$$("object").invoke("show");
}

function ocultarSelect()
{
	$$("select").each(function(item){
		item.setStyle({visibility:'hidden'});
	});
}

function mostrarSelect()
{
	$$("select").each(function(item){
		item.setStyle({visibility:'visible'});
	});
}

function getScrollXY() 
{
  var scrOfX = 0;
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else 
  if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else 
  if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return { x:scrOfX,y:scrOfY };
}

function getScreenSize()
{	
	var sx=document.documentElement.clientWidth>document.body.clientWidth?document.documentElement.clientWidth:document.body.clientWidth;
	var sy=document.documentElement.clientHeight>document.body.clientHeight?document.documentElement.clientHeight:document.body.clientHeight;
	return {x:sx,y:sy};	
}

function getMaxSize()
{
	var sx=0;
	var sy=0;
	if(window.scrollMaxX || window.scrollMaxY )
	{
		var sx=window.scrollMaxX;
		var sy=window.scrollMaxY;	
	}	
	return {x:sx,y:sy};	
}



function getFullScreenSize()
{
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) 
	{
		xScroll = document.body.scrollWidth;
		yScroll = getScreenSize().y + window.scrollMaxY;
	} 
	else
	{
		if (document.body.scrollHeight > document.body.offsetHeight)
		{ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} 
		else 
		{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	}
		
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{ // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else 
	{
		if (document.documentElement && document.documentElement.clientHeight) 
		{ // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} 
		else
		{
			if (document.body) 
			{ // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
		}
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{
		pageWidth = windowWidth;
	} 
	else 
	{
		pageWidth = xScroll;
	}	
	return {x:pageWidth,y:pageHeight};
	
}

function aceptarNumeros(campo)
{
	campo.value=campo.value.gsub("a+","");	
}

function aceptarReales(campo)
{
	campo.value=campo.value.gsub("a+","");	
}

function json2select(json,select,value,text,primerElemento,valorPrimerElemento)
{
	select=$(select);
	var inicio=0;
	var limite=json.length;
	select.length=0;
	select.options.length=limite;
	if(primerElemento!=null)
	{
		inicio=1;
		limite++;
		select.options.length=limite;
		select.options[0].value=valorPrimerElemento;
		select.options[0].text=primerElemento;
	}
	for(var i=inicio;i<limite;i++)
	{
		var j=i;
		j=primerElemento!=null?j-1:j;
		select.options[i].value=json[j][value];
		select.options[i].text=json[j][text];
	}
	return select;
}

function enc(texto)
{
	var cad="";
	for(var i=0;i<texto.length;i++)
	{
		if(i==texto.length-1)
		{
			cad+=texto.charAt(i).charCodeAt();
			continue;
		}
		cad+=texto.charAt(i).charCodeAt()+"-";
	}
	return cad;
}

