function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function rememberMe() {
    if (readCookie('rememberMe') == 1) {
        document.getElementById('username').value = readCookie('username');
        document.getElementById('password').value = readCookie('password');
        document.getElementById('rememberMe').checked = readCookie('rememberMe');
    }
}

    function createCookie( name, value, expires, path, domain, secure ) 
    {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    var str = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    document.cookie = str;
    }

// this deletes the cookie when called
function eraseCookie( name, path, domain ) {
if ( readCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function doLogin() {
    $('loginMsg').className = '';
    //$('loginMsg').innerHTML = 'Logging in...';
    $('Send').className = 'send-btn-anim';
    var username = ($('username'));
    new Ajax.Request('/includes/ajax.php',{parameters: 'function=getUserId&username='+$('username').value, asynchronous:true, onSuccess:getUserIdReturn});
}
function getUserIdReturn(req) {
    var response = req.responseXML.documentElement;    
    var userId = response.getAttribute('userid');
    var encryptedPassword = hex_md5('%^'+$('password').value+'_'+userId);   
    new Ajax.Request('/includes/ajax.php',{parameters: 'function=doLogin&username='+$('username').value+'&password='+encryptedPassword+'&rememberMe='+$('rememberMe').checked, asynchronous:true, onSuccess:doLoginReturn});
}
function doLoginReturn(req) {
    var response = req.responseXML.documentElement;    
    var success = response.getAttribute('success');
    $('Send').className = 'send-btn';
    var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
    if (success == 1) {
        if ($('rememberMe').checked == true) {
            createCookie('rememberMe',1,360,'/');
            createCookie('username',$('username').value,360,'/');
            createCookie('password',$('password').value,360,'/');
            if(parent.frames.length == 0){
            	if(sPage == 'search.php'){
            		window.location.reload();
            	}else{
            		top.location.href = '/my_esession/song_space/inner-1.php';
            	}
            } else {
            	parent.frames["menu"].location.reload();
            }
            /* top.location.href = '/?Menu=SubscribeHome'; */    
        } else {
            eraseCookie('rememberMe','/');
            eraseCookie('username','/');
            eraseCookie('password','/');
           if(parent.frames.length == 0){
           		if(sPage == 'search.php'){
            		window.location.reload();
            	}else{
            		top.location.href = '/my_esession/song_space/inner-1.php';
            	}
            } else {
            	parent.frames["menu"].location.reload();
            }  
        }
    } else {
        $('loginMsg').className = 'loginError';
        $('loginMsg').innerHTML = 'Invalid Entry! <a href="/songpage/songpagetabs/songteam/accept/index.php?action=badlogin" target="_self">Hint/Reset Password?</a>';        
    }
}