// vc_id = "$Id: logIn.js 12276 2008-12-08 22:52:28Z jgiven $"
var SAVE_TO_FAV = null;

function initLogInForm ( idFromDetail ) {
	//var logInForm = document.getElementById('log-in-form');	
	//var logInLink = document.getElementById('logInLinkHolder');
	
	if (idFromDetail) {
		SAVE_TO_FAV = idFromDetail;
	}
	
	alert('Please log in using the form above and we will save your listing.  Thank you.');
}

function authenticateLogIn ( formEl ) {
	var qString = queryStringFromForm(formEl,'&');
	var url = 'index.cfm?fuseaction=utility.LogIn&' + qString;
	//var logInErrorMessage = document.getElementById('log-in-error-message');
	
	// First Validate
	with (formEl) {
		if ((user.value == '') || (pass.value == '')) {
			alert('**Both username & Password are required.');
		} else {
			//document.getElementById('hd3').style.display = 'block';
			NEW_HEADER_LOG_IN.setAction( url );
			NEW_HEADER_LOG_IN.run( null );
		}
	}
	return false;
}

function processLogIn ( xhr ) {
	var logInStatusHolder = document.getElementById('log-in-message');
	
	if ( xhr.success( ) ) {
		
		// If Correct log-in is passed give user log-out link.
		if ((xhr.responseText().indexOf('Incorrect') == -1) && (xhr.responseText().indexOf('mutp=1') == -1)) {  
			document.header_log_in_form.reset();
			document.getElementById('header_login_formFields').style.display = 'none';
			document.getElementById('header_login_submit').style.display = 'none';
			logInStatusHolder.innerHTML += xhr.responseText() + ' | <a href="javascript: logOut();">Log Out</a>';
			
			if (SAVE_TO_FAV != null) {
				var lID = SAVE_TO_FAV;
				SAVE_TO_FAV = null;
				window.contentPropertyFrame.location = 'index.cfm?fuseaction=account.actAddFavorite&listingID=' + lID + '&XFA_returnFA=listing.PP3ListingDetail';
			} else if (isDefined(window.contentPropertyFrame)) {
				window.contentPropertyFrame.location = window.contentPropertyFrame.location.href;
			}
			else{
				//window.location = 'index.cfm?fuseaction=account.savedSearches';
			}
			
		} else if (xhr.responseText().indexOf('mutp=1') > 1) {
			window.location = 'index.cfm?fuseaction=account.actLogin&username=' + document.header_log_in_form.user.value + '&password=' + document.header_log_in_form.pass.value;
		} else {
			//alert('Incorrect username and/or password. Please try again.');
			alert('**Incorrect username and/or password. Please try again.');
		}
	}
}

function logOut (  ) {
	var url = 'index.cfm?fuseaction=utility.LogIn&logout=1';
	
	NEW_HEADER_LOG_OUT.setAction( url );
	NEW_HEADER_LOG_OUT.run( null );
}

function processLogOut ( xhr ) {
	var logInStatusHolder = document.getElementById('log-in-message');
	if ( xhr.success( ) ) {
		document.getElementById('header_login_formFields').style.display = 'block';
		document.getElementById('header_login_submit').style.display = 'block';
		logInStatusHolder.innerHTML = '';
		window.location = 'index.cfm?fuseaction=about.home';
	}
}

function preLoggedIn ( fN,lN,uN ) {
	var logInStatusHolder = document.getElementById('log-in-message');
	document.getElementById('header_login_formFields').style.display = 'none';
	document.getElementById('header_login_submit').style.display = 'none';
	logInStatusHolder.innerHTML = 'Welcome home: <strong>' + uN + '</strong><br><a href="index.cfm?fuseaction=account.favorites">Favorites</a> | <a href="index.cfm?fuseaction=account.savedSearches">Saved Searches</a>' + ' | <a href="javascript: logOut();">Log Out</a>'; //'Welcome, <strong> ' + fN + ' ' + lN + '</strong> 
}

function submitloginform(e) {
	var keycode;

	if ( window.event ) { keycode = window.event.keyCode; } else if ( e ) { keycode = e.which; } else { return true; }
	
	if (keycode == 13) {
		authenticateLogIn(document.header_log_in_form);
	   return false;
	} else {
	   return true;
	}
}
