/**
 * Métodos agregados a objetos del DOM
 *  - document.location.read
 *  - document.location.write
 *  - document.location.set
 *  - document.location.get
 */

/**
 * Hack para implementar la propiedad .innerText para todos los navegadores
 * http://www.lacaraoscura.com/2006/03/29/firefox-y-el-innertext/
 */   
  	  
if( window.navigator.userAgent.indexOf("MSIE") == -1 )
{   
 HTMLElement.prototype.__defineGetter__("innerText",function () { return(this.textContent); });   
 HTMLElement.prototype.__defineSetter__("innerText",function (txt) { this.textContent = txt; });
}
             
/**
 * Devuelve un array a partir de una cadena de variables
 * 
 * @function document.location.read
 * @param [ sQueryString ] ( String )
 * @return ( Array )
 * @see document.location
 */
       
document.location.read = function( sQueryString )
{	
 if( !sQueryString )
  sQueryString = this.search.replace( /^\?/, "" );

 var oVariableList = new Array();

 if( sQueryString.length > 0 )
 {
  var oQueryList = sQueryString.replace( "?", "&" ).split( "&" );
     
  for( var i = 0; i < oQueryList.length; i++ )
   oVariableList[ oQueryList[ i ].split( "=" )[ 0 ] ] = oQueryList[ i ].split( "=" )[ 1 ];
 }
 
 return( oVariableList );					      		   
}

/**
 * Devuelve una cadena de variables a partir de un array
 * 
 * @function document.location.write
 * @param oVariableList ( Array )
 * @return ( String )
 */

document.location.write = function( oVariableList )
{
 var oQueryList = new Array();

 for( var i in oVariableList )
  oQueryList.push( i + "=" + oVariableList[ i ] );
 	
 this.search = oQueryList.join( "&" );	 
}

/**
 * Modifica una o varias variables GET
 * 
 * @function document.location.set
 * @param sVariableName ( String )
 * @param sVariableValue ( String )
 * @return ( undefined )
 */

document.location.set = function( sVariableName, sVariableValue )
{
 var oVariableList = this.read();
 oVariableList[ sVariableName ] = sVariableValue;
 
 this.write( oVariableList );
}

/**
 * Devuelve una variable GET
 * 
 * @function document.location.get
 * @param sVariableName ( String )
 */

document.location.get = function( sVariableName )
{
 var oVariableList = this.read();
 
 if( typeof( oVariableList[ sVariableName ] ) != "undefined" )
  return( oVariableList[ sVariableName ] );
}



// =====================================




 function setCookie(CookieName, CookieVal, CookieExp, CookiePath, CookieDomain, CookieSecure)

    {

 	    var CookieText = escape(CookieName) + '=' + escape(CookieVal); //escape() : Encodes the String

	    CookieText += (CookieExp ? '; EXPIRES=' + CookieExp.toGMTString() : '');

	    CookieText += (CookiePath ? '; PATH=' + CookiePath : '');

	    CookieText += (CookieDomain ? '; DOMAIN=' + CookieDomain : '');

	    CookieText += (CookieSecure ? '; SECURE' : '');

    	

	    document.cookie = CookieText;

    }



    // This functions reads & returns the cookie value of the specified cookie (by cookie name) 

    function getCookie(CookieName)

    {

 	    var CookieVal = null;

	    if(document.cookie)	   //only if exists

	    {

       	    var arr = document.cookie.split((escape(CookieName) + '=')); 

       	    if(arr.length >= 2)

       	    {

           	    var arr2 = arr[1].split(';');

       		    CookieVal  = unescape(arr2[0]); //unescape() : Decodes the String

       	    }

	    }

	    return CookieVal;

    }



    // To delete a cookie, pass name of the cookie to be deleted

    function deleteCookie(CookieName)

    {

 	    var tmp = getCookie(CookieName);

	    if(tmp) 

	    { 

	        setCookie(CookieName,tmp,(new Date(1))); //Used for Expire 

	    }

    }


