﻿// JScript File

// Define a list of Microsoft XML HTTP ProgIDs.
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
	"Msxml2.XMLHTTP.7.0",
	"Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0",
	"Msxml2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP"
);

//-----------------------------------------------------------------------------
// Returns an XMLHttpRequest object.
//-----------------------------------------------------------------------------
function getXMLHttpRequest()
{
	var httpRequest = null;

	// Create the appropriate HttpRequest object for the browser.
	if (window.XMLHttpRequest != null)
		httpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null)
	{
		// Must be IE, find the right ActiveXObject.
		var success = false;
		for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
		{
			try
			{
				httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
				success = true;
			}
			catch (ex)
			{}
		}
	}

	// Display an error if we couldn't create one.
	if (httpRequest == null)
		alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");

	// Return it.
	return httpRequest;
}

var xhr = getXMLHttpRequest();
var name_result;
var lookupfile;

function SetLookupFile(txtboxID)
{
var txtbox = document.getElementById(txtboxID);

lookupfile = "http://www.pennantchase.com/checkName.aspx?name=";
lookupfile = lookupfile + txtbox.value;
UserNameLookup();
}

function BuildLookupFile(filepth)
{
    lookupfile = filepth;
    DoLookup();
}

function DoLookup(event)
{
    // Abort any currently active request.
    xhr.abort();

    xhr.onreadystatechange = getLookupResult;
    xhr.open("GET", lookupfile, true);
    xhr.send(null);
}

function UserNameLookup(event)
{
    // Abort any currently active request.
    xhr.abort();
    
    xhr.onreadystatechange = getNameResult;
    xhr.open("GET",lookupfile,true);
    xhr.send(null);
}

function getNameResult()
{ 
    var divcontent;
    
    if(xhr.readyState < 4)
	{
	    divcontent = "Checking name...";
	    writeAjaxResponse('checkName', divcontent);
	}
	if(xhr.readyState == 4)
		{
		    if (xhr.status == 200) 
			{
			    window.setTimeout("writeAjaxResponse('checkName',xhr.responseText)", 1000);
			}
		}
}

function getLookupResult()
{
    var divcontent;

    if (xhr.readyState < 4)
    {
        divcontent = "<img src='http://www.pennantchase.com/images/loading_sm.gif' />";
        writeAjaxResponse('mainAjaxDiv', divcontent);
    }
    if (xhr.readyState == 4)
    {
        if (xhr.status == 200) 
        {
            window.setTimeout("writeAjaxResponse('mainAjaxDiv',xhr.responseText)", 1000);
        }
    }
}

function writeAjaxResponse(divname, divcontent) 
{
    //undefined check
    if (divcontent.indexOf("undefined") != -1) 
    {
        divcontent = "Request was unsuccessful, please wait a moment and try again.";
    } 
    
    var div_id = document.getElementById(divname);
    div_id.innerHTML = divcontent;
    
    //for social media success    
    if (divcontent.indexOf("Success") != -1) 
    {
        userLoggedIn();
    }

}

