﻿var ctrlPrefix = "ctl01_PopupWrapper_";
var form = document.forms[0];
var popupMPE = null;
var resBuilderProxy = null;
var responseDivName = null;
var itsAPopup = false;
var resultData = null;
var infoBoxData = null;

function pageLoad(sender, args)
{
	if (typeof (resumeBuilderLoad) != "undefined")
		resumeBuilderLoad(sender, args);
	else if (typeof (loadRenewalReminder) != "undefined" && loadRenewalReminder && !args._isPartialLoad)
		ShowRenewalReminder();
	else if (typeof (vertScrollCoord) != "undefined" && vertScrollCoord > 0)
		window.scrollBy(0, vertScrollCoord);
}

function ShowChooseResume(action)
{
	InitializeResBuilderProxy();
	responseDivName = "ChooseResumeResponseDiv";
	resBuilderProxy.GetControlHtml("ChooseResume", true, 0,
		currentPersonId, action, RenderPopup, HandlePopupError, 500);
}

function ShowHelpPopup(helpId)
{
	var url = "Help.aspx?Id=" + helpId;
	var name = "HelpPopup";
	var width = 600;
	var height = 500;	
	var topVal = CenterVert(height);
	var leftVal = CenterHorz(width);
	var status = "yes";
	var toolbar = "no";
	var menubar = "no";
	var location = "no";
	var resizable = "no";
	var scrollbars = "no";

	ShowPopUp(url, name, height, width, topVal, leftVal, status, 
		toolbar, menubar, location, resizable, scrollbars)
}

function ShowRenewalReminder(action, reason)
{
	if (action == null) action = 0;
	if (reason == null) reason = "";
	InitializeResBuilderProxy();
	resBuilderProxy.GetControlHtml("RenewalReminder", true, action,
		currentPersonId, reason, RenderPopup, HandlePopupError, 500);
}

function DisplayHtmlResumeWindow(resumeId, linkId, data)
{
	var href = "PrintHtml.aspx?Type=Resume";
	
	if (resumeId != null && resumeId != "")
		href += "&ResumeId=" + resumeId;
		
	if (linkId != null && linkId != "")
		href += "&LinkId=" + linkId;

	if (data != null & data != "")
		href += "&Data=" + data;

	var nWin = window.open(href, "HtmlResumeWindow", "width=750,scrollbars=yes,resizable=yes,menubar=yes");
	return nWin;
}

function CloseReminder(which)
{
	var dontShow = $get(ctrlPrefix + "DontShowCheckBox");
	if (dontShow != null && dontShow.checked)
	{
		var exdate = new Date();
		var expiredays = which == "Renewal" ? 30 : 15;
		var cName = "SuppressRenewalReminder";

		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = cName + "=Yes;expires=" + exdate.toGMTString();
	}

	CancelPopup();
}

function ShowCoverLetterPopup(coverLetterId)
{
	InitializeResBuilderProxy();
	window.selectedCoverLetterId = coverLetterId;
	responseDivName = "CoverLetterResponseDiv";
	resBuilderProxy.GetControlHtml("CoverLetterEdit", true, coverLetterId,
				currentPersonId, null, RenderPopup, HandlePopupError, 600);
}

function ShowEmailMessageDetail(messageId)
{
	InitializeResBuilderProxy();
	window.selectedMessageId = messageId;
	responseDivName = "MessageDetailResponseDiv";
	resBuilderProxy.GetControlHtml("EmailMessageDetail", true, messageId,
		currentPersonId, null, RenderPopup, HandlePopupError, 600);
}

function ShowQuickReference(mode)
{
	InitializeResBuilderProxy();
	resBuilderProxy.GetControlHtml("QuickReference", true, mode,
		currentPersonId, null, RenderPopup, HandlePopupError, 700);
}

function ShowQuickVideo(mode)
{
	InitializeResBuilderProxy();
	resBuilderProxy.GetControlHtml("QuickVideo", true, mode,
		currentPersonId, null, RenderPopup, HandlePopupError, 550);
}

function ShowInfoPopup(contentId)
{
	InitializeResBuilderProxy();
	resBuilderProxy.GetAnonymousHtml("InfoPopup", contentId, null, RenderPopup, HandlePopupError, 600);
}

function HandleChooseResume(action)
{
	var chooseResumeList = document.forms[0].ctl01$PopupWrapper$ChooseResumeList;
	var responseDiv = $get(responseDivName);
	var summary = GetValidationSummary(responseDiv);
	var firstEl = null;
	var errMsg = "";
	var href = location.href.substring(0, location.href.lastIndexOf("/")).replace("https", "http");

	switch (action)
	{
		case "Edit":
			href += "/ResumeBuilder.aspx?ResumeId=";
			break;
		case "Print":
			href += "/PrintResume.aspx?ResumeId=";
			break;
		default:
			var msg = "An appropriate action was not specified.";
			alert(msg);
			return;
	}

	if (chooseResumeList.selectedIndex == -1)
	{
		AddValidationSummaryItem(summary, "A resume selection is required.");
		if (firstEl == null) firstEl = chooseResumeList;
	}

	if (firstEl != null)
		ShowValidationSummary(responseDiv, summary, firstEl);
	else
		document.location = href + chooseResumeList.value;
}

function RenderPopup(result, userContext, methodName)
{
	var wrapper = InitializeWrapper(userContext);
	wrapper.innerHTML = result.HTML;
	
	//turn off item library drag drop while modal popup is visible
//	document.body.onmousemove = "";
//	document.body.onmouseup = "";
//	document.body.onselectstart = "";
//	document.body.ondragstart = "";
	
	popupMPE = $find(clientPopupId);
	popupMPE.show();
//	popupMPE._dragHandleElement = $get("ClientPopupTitleBar");
//	popupMPE._attachPopup();
//	
	itsAPopup = true;

	if (result.Script.length > 0)
		eval(result.Script);

	if (result.Data != null)
		resultData = result.Data;

	if (result.InfoBoxData != null)
	{
		infoBoxData = result.InfoBoxData;
		for (var i = 0; i < infoBoxData.length; i++)
		{
			AddInfoBoxText(infoBoxData[i].Key, infoBoxData[i].Value);
		}
	}

	//fromEmployers is set in the RecyclePositionPopup
	//method when the Manager Employers dialog is closed
	//and the Position Edit dialog is reopened.
	if (typeof (fromEmployers) != "undefined" && fromEmployers == true)
	{
		ReloadPositionInfo();
		fromEmployers = false;
	}

	document.body.onkeypress = HandleEnterKey;
	window.setTimeout("ScrollIt()", 250);
}

function ScrollIt()
{
	window.scrollBy(0, 1);
	window.scrollBy(0, -1);
}

function HandleEnterKey(e)
{
	var evnt = (e) ? e : (event) ? event : null;
	if (evnt.keyCode == 13)
	{
		if ($get("SaveButton") != null)
			$get("SaveButton").onclick();
		else if ($get(ctrlPrefix + "SaveButton") != null)
			$get(ctrlPrefix + "SaveButton").onclick();
		else if ($get(ctrlPrefix + "SelectButton") != null)
			$get(ctrlPrefix + "SelectButton").onclick();

		return false;
	}
}

function HandlePopupError(error, userContext, methodName)
{
	var summary = GetValidationSummary();
	AddValidationSummaryItem(summary, error);
	$get(userContext).appendChild(summary);
}

function ShowValidationSummary(responseDiv, summary, firstEl)
{
	responseDiv.appendChild(summary);
	window.scrollBy(0, 1);

	if (firstEl != null)
	{
		firstEl.focus();
		firstEl = null;
	}
}

function GetValidationSummary()
{
	var VS = document.getElementById("ValidationSummary");
	if (VS != null) VS.parentNode.removeChild(VS);

	var div = document.createElement("div");
	div.id = "ValidationSummary";
	div.className = "ValidationSummary";

	div.innerHTML = "The following error(s) occurred:";

	var ul = document.createElement("ul");
	ul.id = "ValidationSummaryUL";

	div.appendChild(ul);
	return div;
}

function AddValidationSummaryItem(VS, text)
{
	var li = document.createElement("li");
	li.innerHTML = text;

	for (var i = 0; i < VS.childNodes.length; i++)
	{
		if (VS.childNodes[i].id == "ValidationSummaryUL")
		{
			VS.childNodes[i].appendChild(li);
			break;
		}
	}
}

function AddInfoBoxText(id, msg)
{
	if (typeof (infoBoxText) != "undefined" && infoBoxText[id] != "undefined")
	{
		infoBoxText[id] = msg;
	}
}

function InitializeWrapper(width)
{
	var wrapper = $get("ClientPopupOuterWrapper");
	wrapper.style.width = width + "px";
	ClearWrapper(wrapper);
	return wrapper;
}

function ClearWrapper(wrapper)
{
	if (wrapper.childNodes.length > 0)
	{
		for (var i = wrapper.childNodes.length - 1; i >= 0; i--)
		{
			wrapper.removeChild(wrapper.childNodes[i]);
		}
	}
}

function InitializeResBuilderProxy()
{
	if (resBuilderProxy == null)
		resBuilderProxy = AshleyGroup.ThatResumeSite.Web.WebServices.ResumeBuilderService;
}

function ArgsHash()
{
	this.Hash = "";

	this.Add = function(key, value)
	{
		var concat = "";
		if (this.Hash.length > 0) concat = ",";
		this.Hash += concat + key + ":" + (typeof (value) == "string" ? SafeString(value) : value);
	}
}

function CancelPopup()
{
	ClearWrapper($get("ClientPopupOuterWrapper"));
	popupMPE.hide();
	itsAPopup = false;
	if (document.location.href.indexOf("ResumeBuilder.aspx") == -1)
		document.body.onkeypress = "";

	//turn item library drag drop back on 
//	document.body.onmousemove = ConfigDrag_MouseMove;
//	document.body.onmouseup = ConfigDrag_MouseUp;
//	document.body.onselectstart = cancelSelectionEvent;
//	document.body.ondragstart = cancelEvent;
}

function doNothing()
{
	return;
}

/************************************************
INFO BOX
************************************************/
var sourceX = 0;
var sourceY = 0;
var infoBox = null;

function ShowInfoBox(tmpEl, textId, width, height)
{
	infoBox = document.getElementById("InfoBoxWrapper");
	sourceX = 0;
	sourceY = 0;

	if (infoBox.style.display != "block")
	{
		while (tmpEl != null && tmpEl.tagName != null && tmpEl.tagName != "" && tmpEl.tagName.toLowerCase() != "body")
		{
			sourceX += tmpEl.offsetLeft;
			sourceY += tmpEl.offsetTop;
			tmpEl = tmpEl.offsetParent;
		}

		var infoBoxContent = document.getElementById("InfoBoxContent");
		infoBoxContent.innerHTML = infoBoxText[textId];
		
		if (height > 0)
			infoBoxContent.style.height = height + "px";

		if (width > 0)
			infoBox.style.width = width + "px";

		var scrollHeight = 0;
		if (typeof (itsAPopup) != 'undefined' && itsAPopup == true)
			scrollHeight = GetScrollDimensions()[1];

		infoBox.style.left = (sourceX + 12) + "px";
		infoBox.style.top = ((sourceY + scrollHeight) - 25) + "px";
		infoBox.style.display = "block";
	}
}

function HideInfoBox(e)
{
	if (infoBox != null && infoBox.style.display == "block")
	{
		var tmpEl = GetCrossBrowserSource(GetCrossBrowserEvent(e));

		while (tmpEl.tagName != null && tmpEl.tagName != "" && tmpEl.tagName.toLowerCase() != "body")
		{
			if (tmpEl.className == "InfoBoxWrapper" || tmpEl.className == "InfoLink") return;
			tmpEl = tmpEl.parentNode;
		}

		sourceX = 0;
		sourceY = 0;
		infoBox.style.display = "none";
	}
}

var infoBoxText = new Array();

document.body.onmouseover = HideInfoBox;
