// Manages cookies

function setCookie(Name,Value,Expires)
{
    var pvalue   = escape(Value) ;
    var pExpires = Expires ;

    var pPath    = ';Path=/' ;

    // Get subdomain if more than one "." in the host name
    var pDomain  = document.URL;
    pDomain = pDomain.substring( pDomain.indexOf( '//' ) + 2 );
    idx = pDomain.indexOf( ':' );
    if ( idx >= 0 )
        pDomain = pDomain.substring( 0, idx );
    idx = pDomain.indexOf( '/' );
    if ( idx >= 0 )
        pDomain = pDomain.substring( 0, idx );
    idx = pDomain.indexOf( '.' );
    if ( idx != pDomain.lastIndexOf( '.' ) )
        pDomain = ';domain=' + pDomain.substring( idx );
    else
        pDomain = '';

    if ( pExpires == '' ) { pExpires = 'Fri, 31 Dec 2099 23:59:59 GMT;'; }
    document.cookie = (Name + '=' + pvalue + ';expires=' + pExpires + pPath + pDomain);
}

function getCookie(Name)
{
    var aCookie = document.cookie.split("; ");

    for ( var i=0 ; i < aCookie.length ; i++ )
    {
        var aCval = aCookie[i].split("=");
        if ( ( Name == aCval[0]) && ( aCval.length > 1 ) ) return unescape( aCval[1] ) ;
    }

    return ' ' ;
}

function deleteCookie(Name)
{
    var sValue = getCookie(Name) ;
    document.cookie = Name + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1990 23:59:59 GMT;";
}

function loginload() {
    var username = trimlr(getCookie('dluv'));
    var password = trimlr(getCookie('dlpv'));

    //this sucks
    if (document.login) {
    	if (username != '') document.login.username.value = username;
    	if (password != '') document.login.password.value = password;
		if ((username != '') && (password !=  '')) document.login.local_save_password.checked = true;
    }
	
	if (document.getElementById('sign_in_form')) {
		if (username != '') document.getElementById('sign_in_email').value = username;
		if (password != '') document.getElementById('sign_in_password').value = password;
		if ((username != '') && (password !=  '')) document.getElementById('local_save_password').checked = true;
	}
}

function loginPost()
{
    if ( document.login ) {
    	setCookie('dluv',document.login.username.value,'') ;
    	setCookie('dlpv',document.login.password.value,'') ;
    }
}

function loginNothing()
{
    setCookie('dluv','','') ;
    setCookie('dlpv','','') ;
}
