var username = null;


function signIn()
{
	//Set SIGN OUT stuff
	xAddEventListener($('signOutCtrl'), 'click', switchToSignedOutState, false);
        
	xAddEventListener($('signInForm'), 'submit', signInCheckForm, false);


	xAddEventListener($('sun'), 'blur', inputBlur, false);
	xAddEventListener($('spw'), 'blur', inputBlur, false);

	xAddEventListener($('sun'), 'focus', inputFocus, false);
	xAddEventListener($('spw'), 'focus', inputFocus, false);

	signInCheck();
}


function inputFocus(e)
{
	var target = getEventTarget(e);
	target.className = 'selected';
}

function inputBlur(e)
{
	var target = getEventTarget(e);
	target.className = '';
}



function signInCheckForm(e)
{
        if (e && e.preventDefault)
        {
                e.preventDefault();
                var form = e.target;
        }
        else if (window.event)
        {
                window.event.returnValue = false;
                //var form = window.event.srcElement.parentNode;
                var form = window.event.srcElement;
        }


        var username = form.elements['username'].value;  //xmlhttp.send('rating=' + target.elements['rating'].value);
        var pw           = form.elements['pw'].value;



        if(username && pw)
        {
                signInSendData(username, pw);
        }
        else
        {
                signInError('You must enter a <B>username</B> and <B>password</B>');
        }
}


function signInError(msg)
{
	message = document.getElementById('signInMessage');
	message.innerHTML = msg;
	message.style.display = 'inline';
}





function signInSendData(username, pw, async)
{
	if(async == null)
		async = true;
	else
		async = false;

	var dataStr = "username=" + username +"&pw=" + pw;


	makeHttpRequest('ajax/signIn/signIn-request.php',
			'POST',
			dataStr,
			'signInHandleResults',
			true,
			async
	);
}



function signInHandleResults(response_xml)
{
	var response = response_xml.getElementsByTagName('response')[0];

	if(response.firstChild.tagName == 'success')
	{
		$('signedIn').style.display = 'inline';
		$('signedOut').style.display = 'none';
                signInResetForm();
		$('signedInUserName').firstChild.nodeValue = response.firstChild.firstChild.nodeValue;
	}
	else if(response.firstChild.tagName == 'failure')
	{
		$('signedIn').style.display = 'none';
		$('signedOut').style.display = 'inline';

		signInError(response.firstChild.firstChild.nodeValue);
	}

	if(typeof authComplete == 'function')
		authComplete();
}


function signInResetForm()
{
	message = document.getElementById('signInMessage');
	$('sun').value='';
	$('spw').value='';

	message.innerHTML = '&nbsp;';
	message.style.display = 'none';
}





//===============================================================================================
function signInCheck()
{
        makeHttpRequest('ajax/signIn/signIn-request.php',
                        'POST',
                        'getCookies=true',
                        'signInCheckHandleResults', 
                        true
        );
}


function signInCheckHandleResults(response_xml)
{
        var response = response_xml.getElementsByTagName('response')[0];
	switchToSignedInState(response_xml);
}

function switchToSignedInState(response_xml)
{
        var response = response_xml.getElementsByTagName('response')[0];

	if(response.firstChild.tagName == 'success')
	{
		$('signedIn').style.display = 'inline';
		$('signedOut').style.display = 'none';
                signInResetForm();
		username = $('signedInUserName').firstChild.nodeValue = response.firstChild.firstChild.nodeValue;
	}
	else if(response.firstChild.tagName == 'failure')
	{
		$('signedIn').style.display = 'none';
		$('signedOut').style.display = 'inline';
	}

//CONSIDER REMOVING THE NEXT 2 LINES - authcomplete COULD be run on onload, rather than waiting for signin section to finish loading, upon a page load.
	if(typeof authComplete == 'function')
		authComplete();
}

//===============================================================================================
function switchToSignedOutState()
{
        makeHttpRequest('ajax/signIn/signIn-request.php?logged=off',
                        'GET',
                        null,
                        'switchToSignedOutStateHandleResults',
                        true,
			false
        );
}


function switchToSignedOutStateHandleResults(response_xml)
{
        var response = response_xml.getElementsByTagName('response')[0];

	if(response.firstChild.tagName == 'offline')
	{
		$('signedOut').style.display = 'inline';
		$('signedIn').style.display = 'none';
	}

	if(typeof authComplete == 'function')
		authComplete();
}

