﻿// JScript File

function JQLoad(divname, filename, preloadcontent) {

    if (preloadcontent == "0") {
        preloadcontent = "<img src='http://www.pennantchase.com/images/loading_sm.gif' style='padding:5px' />";
    }

    var div_id = document.getElementById(divname);
    div_id.innerHTML = preloadcontent;

    divname = '#' + divname;
    $(divname).load(filename);
}

// 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;
var ajaxDivName = 'mainAjaxDiv'; //change to display where content loads

function SetNameLookupFile(txtboxID)
{
var txtbox = document.getElementById(txtboxID);

lookupfile = "checkName.aspx?name=";
lookupfile = lookupfile + txtbox.value;
BuildLookupFile(lookupfile);
}

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 getLookupResult()
{
    var divcontent;

    if (xhr.readyState < 4)
    {
        divcontent = "<img src='http://www.pennantchase.com/images/loading_sm.gif' style='padding:5px' />";
        writeAjaxResponse(ajaxDivName, divcontent);
    }
    if (xhr.readyState == 4)
    {
        if (xhr.status == 200) 
        {
            window.setTimeout("writeAjaxResponse(ajaxDivName,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();
    }

}

function loadInnerInfo(linkid, pageurl, wpx) {
    //position the div based on recapLink
    var newTop, newLeft;
    var linkobj = document.getElementById(linkid);
    if (linkobj.offsetParent) {
        newTop = linkobj.offsetTop;
        newLeft = linkobj.offsetLeft;
        while (linkobj = linkobj.offsetParent) {
            newTop += linkobj.offsetTop;
            newLeft += linkobj.offsetLeft;
        }
    }

    newTop -= 45;
    document.getElementById('innerInfoDiv').style.width = "0px";
    document.getElementById('innerInfoDiv').style.top = newTop + "px";
    //document.getElementById('innerInfoDiv').style.left = newLeft + "px";

    //show div
    showDiv('innerInfoDiv');
    //slide div out
    $('#innerInfoDiv').animate({
        width: '+=' + wpx
    }, 1000, function () {
        // Animation complete.
    });

    //set ajax div name
    ajaxDivName = 'innerInfoDiv';
    BuildLookupFile(pageurl);
}

function closeInnerDiv() {
    wpx = document.getElementById('innerInfoDiv').style.width;
    //slide div in
    $('#innerInfoDiv').animate({
        width: '-=0' + wpx
    }, 1000, function () {
        // Animation complete.
        hideDiv('innerInfoDiv');
    });
}


