/**
 * MasterQuery Implementor for XLink.
 * @constructor
 * @private
 */
function XLinkImp()
{
	throw new Error("XLinkImp is a static class. Do not instantiate.");
}

XLinkImp.DEFAULT_DOMAIN = "www.lexis.com";
XLinkImp.DEFAULT_ZONE_DOMAIN = "web.lexis-nexis.com";
// XLinkImp.DEFAULT_DOMAIN = "cert-2610.lexis.com";


// Create API Matrix
// The matrix is used to cast object elements into those used by the API.
XLinkImp.matrix          = new Object();

// Filter out MQ fields that we don't want in the query string
XLinkImp.matrix.pwd      = -1;
XLinkImp.matrix.uid      = -1;
XLinkImp.matrix.domain   = -1;
XLinkImp.matrix.protocol = -1;

// Describe which field names you need to cast into other values
// specific to the API.
XLinkImp.matrix.originationCode = "ORIGINATION_CODE";
XLinkImp.matrix.sort            = "sortby";
XLinkImp.matrix.TOCTarget       = "target";
XLinkImp.matrix.authToken       = "uisToken";
	
/**
 * Takes a chunnel object and submits it. The last step in the journey
 * of a chunnel object before it becomes a LN search result.
 */
XLinkImp.submit = function(chunnel)
{
    if ((Truth.isTrue(chunnel.uid)) || (Truth.isTrue(chunnel.pwd)))
        chunnel = SearchImplementor.tokenize(chunnel);
	
//alert(SearchImplementor.getQueryString(chunnel, XLinkImp.matrix));
	// Preprocessor needs to convert generic after field 
	// to XLink's relativedate field
	XLinkImp._preprocessor(chunnel);
	var linker = XLinkImp.getLink(chunnel);
	// If no target, set the target for the same window.	
	if (!Truth.isTrue(chunnel.target))
	{
		chunnel.target = "_self";
	}
	
    // prompt("", linker)
	
	if (XLinkImp.useGet(chunnel)) 
	{
		var w = window.open(linker, chunnel.target, NEW_WINDOW_FEATURES); 
	}
	else
	{
		var f = "<form method='post' action='";
		f += linker;
		f += "' id='masterQueryDynaForm' name='masterQueryDynaForm' target='";
		f += chunnel.target;
		f += "'>";
		f += FormUtil.getHiddenFieldVal('USER_ID', chunnel.getUID());
		f += FormUtil.getHiddenFieldVal('PASSWORD', chunnel.getPWD());
		f += FormUtil.getHiddenFieldVal('LNAUTHSCHEME', 'CP');
		f += "</form>";

		SearchImplementor.submitDiv(f);
	}
	chunnel = null;
};

/**
 * Take any actions that need to be made on the MQ object before it gets
 * submitted to its specific API.
 */
XLinkImp._preprocessor =  function(chunnel)
{
	var relativedate = LNParser.convertAfterToRelative(chunnel.getAfter());
	chunnel.c_relativedate = relativedate;
	chunnel.after = "";
	
	if (Truth.isTrue(chunnel.zone) && !Truth.isTrue(chunnel.get("toclink")))
	{
		chunnel.set("toclink", "off");
	}
	
	if (Truth.isTrue(chunnel.zone) && !Truth.isTrue(chunnel.get("hnlink")))
	{
		chunnel.set("hnlink", "off");
	}
	
	if (Truth.isTrue(chunnel.pwd) || Truth.isTrue(chunnel.authToken))
	{
		chunnel.protocol = "https:";
	}
	return chunnel;
};

/**
 * Returns the link that is the target for the API call.
 * This can be used as the action for a form or as the location
 * for a called to window.open.
 *
 * The query string is return based upon the chunnel values filtered 
 * through the implementor's matrix
 */
XLinkImp.getLink = function(chunnel)
{
	var domain;
	
	if (chunnel.zone && chunnel.zone !== "") 
	{
		domain = XLinkImp.DEFAULT_ZONE_DOMAIN;
	}
	else
	{
	    domain = XLinkImp.DEFAULT_DOMAIN;
	}
	
	if (chunnel.domain && chunnel.domain !== "") 
	{
		domain = chunnel.domain;
	}
	
	var action
	
	if (Truth.isTrue(chunnel.override))
	{
		action = chunnel.override;
	}
	else
	{
		action = chunnel.protocol + "//" + domain + "/research/xlink";
	}
	return action + "?" + SearchImplementor.getQueryString(chunnel, XLinkImp.matrix);

};

/**
 * Should we use get or post. If there's a username/pwd use post.
 */
XLinkImp.useGet = function(chunnel)
{
	if (Truth.isTrue(chunnel.pwd) && Truth.isTrue(chunnel.uid)) 
	{
		return false;
	}
	return true;
}
