function agentlogin_keyhandler(in_key) {
	switch (in_key.toLowerCase()) {
		case 'enter':
			var username = $("#agentloginname").val().replace(/^\s+|\s+$/g,"");
			var password = $("#password").val().replace(/^\s+|\s+$/g,"");
			$.ajax( { type: "GET", 
				   	url: "/sellerlogin/sellerlogin.cfm?username="+username+"&password="+password,
					complete: agentsetcookie
			} );
			
			return false;
		case 'esc':
			$('#agentlogin')[0].deactivate();
			return false;
	}
	return true;
}

function agentsetcookie(req){
	
	if($(req.responseXML).find("loginstatus").text()== 'success' && $(req.responseXML).find("cookiestr").text()){
		var cookiestr = $(req.responseXML).find("cookiestr").text().split("|");
		$("#agentname").text(cookiestr[1] + " ");
		$("#agencyname").text(cookiestr[3]);
		//clear the username/password field, password managers sometimes prompt to remember if still filled in
		$("#agentloginname, #password").val("");
		var fullname = $(req.responseXML).find("name").text();
		document.cookie = "sellerresauth="+$(req.responseXML).find("cookiestr").text();
		document.cookie = "name="+fullname;
		$('#agentlogin')[0].deactivate();
		init();
		if($(req.responseXML).find("setup").text() == 0) {
			$("#setuperror").slideDown("normal");
			
			$("#rapportlaunch").slideUp("normal");
			showform = false;
		}
		//initialize IR form
		$("#irform input[name='hashstr']").val(cookiestr[4]);
		$("#irform input[name='fullname']").val(fullname);
		$("#irform input[name='userid']").val(cookiestr[0]);
		$("#irform input[name='username']").val(cookiestr[1]);
		$("#irform input[name='sellerid']").val(cookiestr[2]);
		$("#irform input[name='sellername']").val(cookiestr[3]);
	}else{
		alert("Invalid username or password");
	}
}

function sellerlogout(){
		var cookie_date = new Date();
		cookie_date.setTime(cookie_date.getTime() - 1);
		document.cookie = "sellerresauth=; expires=" + cookie_date;
		window.location.href="/";
}

function lostpassword() {
	lfjs.prompt("Enter your email address and we will send you your username and password.",
			lostpasswordgotname, { title: "Forgot your username or password?" } );
	return false;
}

function lostpasswordgotname(which) {
	if (which && which.length) {
		$.ajax({ url: "/sellerlogin/seller.cfc", type: "POST",
			data: { method:"sendpassword", username: which },
			success: lostpassworddone, error: lostpassworderror } );
	}
	$('#agentloginname')[0].focus();
}

function lostpassworddone(req) {
	var foundany = false;
	$(req).find("status").each(function(i) {
		alert($(this).text());
		foundany = true;
	});
	if (!foundany) {
		alert("Internal error: Could not re-send password");
	}
}

function lostpassworderror(req, textStatus) {
	alert("Internal error: Could not re-send password");
}

