
function makelink( addr_name, addr_domain, subject, name ){
    document.write( '<a href="&#x6d;&#x61;&#x69;&#x6c;&#x74;&#x6f;:' );
    document.write( addr_name + '&#64;' );
    document.write( addr_domain );

    if ( subject != null && subject != "" )
	document.write( '?subject=' + subject );

    document.write( '">' );

    if( name != null && name != "" )
	document.write( name );
    else
	document.write( addr_name + '&#64;' + addr_domain );
    
    document.write( '</a>' );
}

function bigChomp( sIn ){
    sIn = sIn.toString();

    sIn = sIn.replace( /\s+$/, '' ); // chomp spaces from end
    sIn = sIn.replace( /^\s+/, '' ); // chomp spaces from beginning
    return sIn;
}

function cookieEncode( sIn ){
    var sOut;
    var i;
    var regexp;
    var match = "";
    var charCode, hexVal;

    if( !sIn ){
	return "";
    }

    sIn = sIn.toString();

    regexp = /(^[a-zA-Z0-9_. ]*).*/; // Yes, we allow spaces within the cookie string
    // and yes, one question mark is allowd to stay at the beginning.
    
    // encode most puntuation, except for a question mark that comes at the beginning of the string
    i = 0;
    if( sIn.indexOf( "?" ) == i ){
	sOut = "?";
	i++;
    }
    else{
	sOut = "";
    }

    
    while( i < sIn.length){
	match = sIn.substr(i).replace( regexp, "$1" );
	if( match.length >= 1 ){
	    sOut += match;
	    i += match.length;
	}
	else{
	    charCode = sIn.charCodeAt(i);
	    hexVal = charCode.toString(16);
	    if( hexVal.length < 2 ){
		sOut += '%0' + hexVal.toUpperCase();
	    }
	    else if( hexVal.length == 2 ){
		sOut += '%' + hexVal.toUpperCase();
	    }
	    // else ignore the character
	    i++;
	}
    }
    return sOut;
}

function cookieDecode( sIn ){
    var sOut;
    var i;
    var charCode, hexVal;

    if( !sIn ){
	return "";
    }

    sIn = sIn.toString();

    // The original regular expression which matched allowed (unencoded) characters:
    //    regexp = /(^[-a-zA-Z0-9_. ]*).*/; // Yes, we allow spaces within the cookie string

    sOut = "";
    i = 0;
    while( i < sIn.length ){
	if( sIn.substr(i,1) == "%" ){
	    hexVal = sIn.substr(i+1, 2);
	    hexVal = parseInt( hexVal, 16 );
	    sOut += String.fromCharCode( hexVal );
	    i += 3;
	}
	else if( sIn.substr(i,1) == "?" ){
	    // There should only ever be a leading ? in a cookie encoded string.
	    i++;
	}
	else{
	    sOut += sIn.substr(i,1);
	    i++;
	}
    }
    return sOut;
}
