﻿/*
            Autor:		Apolinar Plancarte Yáñez
            Carrera:	Ingeniero en Sistemas Computacionales
            Fecha:		19/Julio/2006
*/



//-------------------------------------------------------------------------------------
//                   Definimos la plantilla de la clase y sus métodos                  |
//-------------------------------------------------------------------------------------
function ApyTools()
{
	//Metodo1: Se usa para extraer el valor de un parámetro dado respecto una URL
	this.getParamValue		= _getParamValue;

	//Metodo2: Se usa para extraer todos los parámetros respecto una URL
	this.getQueryString		= _getQueryString;
	
	//Metodo3: Se usa para codificar textos que se enviarán como parámetros por URL
	this.codificarURL		= _codificarURL;

}



//-------------------------------------------------------------------------------------
function _getParamValue(paramName)
{
	var url = location.href.replace("#1","").replace("#here","");

	paramName = paramName.toLowerCase();
	var indiceParams = url.indexOf("?");
	if(indiceParams!=-1)
	{
		var params = url.substring(indiceParams+1).split('&') ;
		var param;
		for (i=0; i < params.length; i++ )
		{
			param = params[i].substring(0, params[i].indexOf('='))
			if(param.toLowerCase()==paramName.toLowerCase())
				return params[i].substring(params[i].indexOf('=') + 1);
		}
	}
	return "";
}



//-------------------------------------------------------------------------------------
function _getQueryString()
{
	var url = location.href.replace("#1","").replace("#here","");

	var indiceParams = url.indexOf("?");
	if(indiceParams!=-1)
		return url.substring(indiceParams + 1);

	return "";
}


//-------------------------------------------------------------------------------------
function _codificarURL(texto)
{
	// caracteres que no se tendran en cuenta
	var caracteres 	 = [ " ", "\"",  "#",  "&",  "(",  ")",  "+",  ",",  ".",  "/",  ":",  ";",  "<",  "=",  ">",  "?",  "@",  "[", "\\",  "]",  "^",  "'",  "{",  "|",  "}",  "~",  "á",  "é",  "í",  "ó",  "ú",  "Á",  "É",  "Í",  "Ó",  "Ú",  "‘",  "’" ];
	var equivalentes = ["20", "22", "23", "26", "28", "29", "2B", "2C", "2E", "2F", "3ª", "3B", "3C", "3D", "3E", "3F", "40", "5B", "5C", "5D", "5E", "60", "7B", "7C", "7D", "7E", "E1", "E9", "ED", "F3", "FA", "C1", "C9", "CD", "D3", "DA", "22", "22" ];

	for (i=0; i<caracteres.length; i++)
		while (texto.indexOf(caracteres[i])!=-1)
			texto = texto.replace(caracteres[i], "%"+ equivalentes[i])
			
	return texto;
}




//Creamos un objeto que accese a los métodos desarrollados de la clase
var objAPY = new ApyTools();


