/**
 * Please use ppcmixed2/ppccpwd77 as id/pwd for uk/legal.
 *
 * http://www.lexisnexis.com/uk/legal/api/version1/sr?csi=267991&sr=police and legal&hd=t
 * @constructor
 * @private -
 */
function UrlApiImp()
{
	throw new Error("V1SearchImp is a static class. Do not instantiate.");
}

UrlApiImp.DEBUG            = true;

UrlApiImp.DEFAULT_DOMAIN       = "www.lexisnexis.com";
UrlApiImp.DEFAULT_NEXIS_DOMAIN = "w3.nexis.com";
// UrlApiImp.DEFAULT_DOMAIN      = "w5cdc12.lexisnexis.com";
UrlApiImp.DEFAULT_ADAPTION     = "uk";
UrlApiImp.DEFAULT_TYPE         = "legal";
UrlApiImp.DEFAULT_API_VERSION  = "api/version1";
UrlApiImp.DEFAULT_VERB         = "sr";

// Create API Matrix
// The matrix is used to cast object elements into those used by the API.
UrlApiImp.matrix          = new Object();

// Filter out MQ fields that we don't want in the query string

UrlApiImp.matrix.adaption   = -1;
UrlApiImp.matrix.domain     = -1;
UrlApiImp.matrix.protocol   = -1;
UrlApiImp.matrix.type       = -1;
UrlApiImp.matrix.apiVersion = -1;
UrlApiImp.matrix.verb       = -1;
//UrlApiImp.matrix.searchtype = -1;
UrlApiImp.matrix.TOCTarget     = -1;	

UrlApiImp.matrix.host       = -1;

UrlApiImp.matrix.pwd    = "pw";
UrlApiImp.matrix.uid    = "ui";

// These fields don't exist in Rosetta
UrlApiImp.matrix.maxdocs  = -1;
UrlApiImp.matrix.zone     = -1;
UrlApiImp.matrix.citation = -1;
UrlApiImp.matrix.prod     = -1;


// Describe which field names you need to cast into other values
// specific to the API.

UrlApiImp.matrix.originationCode = "oc";
UrlApiImp.matrix.client = "pi";
UrlApiImp.matrix.search = "sr";
UrlApiImp.matrix.searchtype = "stp";

UrlApiImp.matrix.source = "csi";
UrlApiImp.matrix.sort   = "so";

// DOSSIER FIELDS
UrlApiImp.matrix.ticker = "tr";
UrlApiImp.matrix.name   = "nm";

// api.universe/search/searchform
UrlApiImp.matrix.id     = -1;


/**
 * Takes a chunnel object and submits it. The last step in the journey
 * of a chunnel object before it becomes a LN search result.
 */
UrlApiImp.submit = function(chunnel)
{
	try
	{
		// Preprocessor needs to convert generic after field 
		// to XLink's relativedate field
		UrlApiImp.preprocessor(chunnel);

		// If no target, set the target for the same window.	
		if (!Truth.isTrue(chunnel.target))
		{
			chunnel.target = "_self";
		}
		
		if (chunnel.get("verb") == "au")
		{
			UrlApiImp._submitAu(chunnel);
			return;
		}
		
		var linker = UrlApiImp.getLink(chunnel);
        if (((!Truth.isTrue(chunnel.pwd) && chunnel.get("verb") != "activate") || Truth.isTrue(chunnel.override))  && chunnel.method != "POST")
		{
			var w = window.open(linker, chunnel.target, NEW_WINDOW_FEATURES); 
		}
		else
		{
            var f = UrlApiImp.getPostForm(chunnel);
			
	        if (chunnel.get("robtest")) {if (!confirm("1: " + f)) return false};
			SearchImplementor.submitDiv(f);
		}
		
		chunnel = null;
	}
	catch(ex)
	{
		if (UrlApiImp.DEBUG)
		{
			alert(ex.message)
		}
	}
};

UrlApiImp.libfileToCSI = function(libfile)
{
	if (libfile.lastIndexOf(";") != -1) {
		var http = new HttpRequest();
		http.setTarget(MasterQuery.baseURL + "getCSI.asp");
		http.setForm(new URLBuilder("libfile", libfile))
		var csi = http.get();
		if (csi)
		{
			return csi;
		}		
	} 
	return libfile;
}

/**
 * Submit using the authentication verb
 */
UrlApiImp._submitAu =  function(chunnel)
{

    chunnel.setProtocol("https:")

    var f = UrlApiImp.getPostForm(chunnel);

	if (chunnel.get("robtest")) {if (!confirm("2: " + f)) return false};

	SearchImplementor.submitDiv(f);
}

UrlApiImp._submitSo =  function(chunnel)
{
	window.open(UrlApiImp.getVerbPath(chunnel), chunnel.target, NEW_WINDOW_FEATURES); 
}

/**
 * Take any actions that need to be made on the MQ object before it gets
 * submitted to its specific API.
 */
UrlApiImp.preprocessor =  function(chunnel)
{
	if (!Truth.isTrue(chunnel.get("adaption")))
	{
		chunnel.set("adaption", UrlApiImp.DEFAULT_ADAPTION);
	}
	
	format = "us";
	
	if (chunnel.get("adaption") != "nexis")
	{
		format = "uk";
	}

	chunnel = LNParser.convertDateToQuery(chunnel, format);
	
	if (Truth.isTrue(chunnel.getSort()))
	{
		chunnel.setSort("da");
	}
	
	if (Truth.isTrue(chunnel.pwd))
	{
		// not working for auth
		// chunnel.protocol = "https:";
	}
	
	if (!Truth.isTrue(chunnel.get("type")))
	{
		chunnel.set("type", UrlApiImp.DEFAULT_TYPE);
	}
	
	//Make alerts work
	if (!Truth.isTrue(chunnel.get("icvrpg")))
	{
		chunnel.set("icvrpg", true);
	}
	
	if (!Truth.isTrue(chunnel.get("verb")))
	{
		chunnel.set("verb", UrlApiImp.DEFAULT_VERB);
	}
	
	
	// For UK Rebranding.  Changes uk/business mq calls to use uk/nexis
	if ((chunnel.get("adaption") == "uk") && (chunnel.get("type") == "business"))
	{
	    chunnel.set("type", "nexis");
	}
	
	if (chunnel.get("rt"))
	    chunnel.set("rt", chunnel.get("rt").toLowerCase().replace(new RegExp("uk/business", "g"), "uk/nexis").replace("sourceinfo.do", "sourceInfo.do"));
    
    
	//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	// Auto-convert libfile to CSI numbers
	
	//chunnel.setSource(UrlApiImp.libfileToCSI(chunnel.getSource()));
	
	//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	// LEXSEE & LEXSTAT
	
	var type = chunnel.getSearchtype();
	
	switch(type)
	{
		case "get":
		case "lexsee":
		case "lxe":
		case "lexstat":
		
			// Make sure that the source isn't already set
			if (!Truth.isTrue(chunnel.getSource())) 
			{
				// http://psc744:6060/ssdf/guide/dynmff/bin/dynmff.pl?type=library&csi=12660&lvl=3
				// Source: MEGA
				// chunnel.setSource("12660")
				chunnel.setSource("138150")
				// 
			}
			break;
	
		default:
			break;
	}
	
	//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	// CUI BUILDER CITATION
	
	// &citation=1+us+1
	if (Truth.isTrue(chunnel.get("citation")))
	{
		if (!Truth.isTrue(chunnel.getSource()) && !Truth.isTrue(chunnel.getSearch()))
		{
			chunnel.setSearch(chunnel.get("citation"))
			chunnel.setSource("12660")
		} 
	}
	
	//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	// Nexis searchforms
	
	if (chunnel.get("id")) 
	{

		chunnel.set("verb", "sf")
		
		switch(chunnel.get("id"))
		{
			// Power Search
			case "-99":
				chunnel.set("sfi", "US00NBGenSrch");
				break;
			// US Reports // Company Profile Page
			case "1928":
				chunnel.set("sfi", "US02NBCmpSrch");
				break;
			// SEC Filings
			case "1940":
			case "2378":
				chunnel.set("sfi", "US02NBSECFilSrch");
				break;
			// Mergers & Acquisitions 
			case "1945":
				chunnel.set("sfi", "US02NBMergAcqSrch");
				break;
			// Business Locator/Assets
			case "1930":
				chunnel.set("sfi", "US04NBBusLocSrch");
				break;
			// Asset Locator
			case "2515":
				chunnel.set("sfi", "US04NBAssetLocSrch");
				break;
			// Bankruptcy Filings
			case "1375":
			case "2517":
				chunnel.set("sfi", "US04NBBankrupSrch");
				break;	
			// Judgments
			case "2519":
				chunnel.set("sfi", "US04NBJudLiensSrch");
				break;
			// Licenses
			case "2522":
				chunnel.set("sfi", "US04NBLicensesSrch");
				break;
			// Civil &amp; Criminal Filings
			case "2523":
				chunnel.set("sfi", "US04NBCivCrimSrch");
				break;	
			// D&B
			case "2524":
				chunnel.set("sfi", "US02NBDunBradSrch");
				break;
			// Motor Vehicles Registrations 
			case "2727":
				chunnel.set("sfi", "US04NBDriverSrch");
				break;
			// Motor Vehicle Registrations
			case "2729":
				chunnel.set("sfi", "US04NBMotVehSrch");
				break;
			// General News 
			case "2888":
				chunnel.set("sfi", "US01NBSimplSrch");
				break;
			// Edgar - SEC Real Time Filings
			case "2911":
				chunnel.set("sfi", "US02NBEdgarSrch");
				break;
		}
	}

	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
 */
UrlApiImp.getLink = function(chunnel)
{
	var qs = SearchImplementor.getQueryString(chunnel, UrlApiImp.matrix);
	
	if (Truth.isTrue(qs))
	{
		return UrlApiImp.getVerbPath(chunnel) + "?" + qs;
	}
	else
	{
		return UrlApiImp.getVerbPath(chunnel);
	}
};

/**
 * Returns the form HTML to be used for a Post call.
 **/
UrlApiImp.getPostForm = function(chunnel)
{
    var f = "<form method='post' action='";
    f += UrlApiImp.getVerbPath(chunnel);
	f += "' id='masterQueryDynaForm' name='masterQueryDynaForm' target='";
	f += chunnel.target;
	f += "'>";
    f += SearchImplementor.getPostFields(chunnel, UrlApiImp.matrix)
	f += "</form>";
	return f;
};

/**
 * 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
 */
UrlApiImp.getVerbPath = function(chunnel)
{
	var domain = UrlApiImp.DEFAULT_DOMAIN;

	if (chunnel.domain !== "" && chunnel.get("adaption") == "nexis")
	{
		domain = UrlApiImp.DEFAULT_NEXIS_DOMAIN;
	}
	
	if (chunnel.domain && chunnel.domain !== "") 
	{
		domain = chunnel.domain;
	}
	
	if (Truth.isTrue(chunnel.override))
	{
		return chunnel.override;
	}
	else
	{
		var adaption = chunnel.get("adaption")
		
		if (adaption == "nexis")
		{
			adaption = "new"
		}
	
		var adLink = chunnel.protocol + "//" + domain + "/" + adaption;
	
		if (chunnel.get("adaption") != "nexis")
		{
			adLink += "/" + chunnel.get("type")
		}
		
		if (chunnel.get("verb") == "home")
		{
			return adLink + "/home/home.do";
		}
		else
		{
			var apiVersion = UrlApiImp.DEFAULT_API_VERSION;
			
			if (chunnel.get("verb") == "accessGW") {
				var apiVersion = "api";
			}
			
			if (Truth.isTrue(chunnel.get("apiVersion")))
			{
				apiVersion = chunnel.get("apiVersion");
			}

			return adLink + "/" + apiVersion + "/" + chunnel.get("verb");	
		}
	}
};
