function ajax_login_display()
{
	var new_content = "<div style=\"margin-left: 40px; float: right;\">&nbsp;";
	new_content += "Our Clients : <a href=\"https://myaccount.customtollfree.com\">Login</a></div>";
	
	new_content += "<div style=\"float: right;\">Advanced Site Access : <a href=\"user/register\">Register</a>&nbsp;&nbsp;|&nbsp;&nbsp; Login</div>";

	new_content += "<br/><div id='ajax_login_form'>Username: <input id='ajax_username_field' onKeyPress='checkEnter(event);' type='text'/>&nbsp;";
	new_content += "Password: <input id='ajax_password_field' onKeyPress='checkEnter(event);' type='password'/>&nbsp;";
	new_content += "<input type='image' id='ajax_submit' src='/images/submittrans.png' onclick='javascript:ajax_login_submit();' value='Submit'><br/>";
	new_content += "  <a href=\"/user/password\">Forgot Password?</a><br/><span id='ajax_form_error'></span></div>";
	
	// Lower the page content
	document.getElementById('header').style.marginTop = '45px';
	document.getElementById('topline_ajax').innerHTML = new_content;
	//
	return false;
}

var g_xmlHttp_login = null; 

function getXmlHttpLoginObjectEx()
{           
    var objXMLHttp = null;
	
    if(window.XMLHttpRequest)
    {   
        objXMLHttp = new XMLHttpRequest();
    }       
    else if(window.ActiveXObject)
    {   
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }       
	
    return objXMLHttp;
}


function ajax_login_submit()
{
	// Input validation
	// Copy values form form and trim whitespace
	var username = document.getElementById('ajax_username_field').value.replace(/\s+/g, '');;
	var password = document.getElementById('ajax_password_field').value.replace(/\s+/g, '');;
		
	// Send the request
	var url = "login_async.php?";
	url += "username=" + username;
	url += "&password=" + password;
	
	var params = "username=" + username + "&password=" + password;
	
    g_xmlHttp_login = getXmlHttpLoginObjectEx();
    g_xmlHttp_login.onreadystatechange = ajax_login_return;
    g_xmlHttp_login.open("POST", url, true);
	
	g_xmlHttp_login.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	g_xmlHttp_login.setRequestHeader("Content-length", params.length);
	g_xmlHttp_login.setRequestHeader("Connection", "close");

	g_xmlHttp_login.send(params);
}


function ajax_login_return()
{

	if(g_xmlHttp_login.readyState == 4 || g_xmlHttp_login.readyState == "complete")
    {
		//confirm(g_xmlHttp_login.responseText);
		switch (g_xmlHttp_login.responseText) 
		
		{
			case '1':
				document.getElementById('ajax_username_field').disabled = true;
				document.getElementById('ajax_username_field').style.background = "#EEEEEE";
				document.getElementById('ajax_username_field').style.color = "#222222";
				
				document.getElementById('ajax_password_field').disabled = true;
				document.getElementById('ajax_password_field').style.background = "#EEEEEE";
				document.getElementById('ajax_password_field').style.color = "#222222";		
				document.getElementById('ajax_form_error').innerHTML = "&nbsp;";
				
				location.reload(true);
			break;
				
			case '2':
				document.getElementById('ajax_form_error').innerHTML = "Your account has not been activated yet.";
				document.getElementById('header').style.marginTop = '60px';
			break;
				
			default :
				document.getElementById('ajax_form_error').innerHTML = "Your username or password is incorrect.<br/>";
				document.getElementById('header').style.marginTop = '60px';
			break;
				
				
		}
	}
}

// Enter detection
// modified based on code from jennifermadden.com

function checkEnter(e)
{
	
	var characterCode 
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		ajax_login_submit();
		return false 
	}
	else{
		return true 
	}
	
}

