/*
*  set targets for Quote module, eSearch and Coverage Guide depending on environment
*/

// quote module URLs
var aarpQuoteQA = "https://qaautoquote.thehartford.com/sales";
var aarpQuoteProd = "https://autoquote.thehartford.com/sales";
var directQuoteQA = "https://qaautoquote.thehartford.com/sales";
var directQuoteProd = "https://autoquote.thehartford.com/sales";

// eSearch URLs
var esearchQA = "http://qaesearch.thehartford.com/esearchclient/search";
var esearchProd = "http://esearch.thehartford.com/esearchclient/search";

// Coverage Guide URLs
var coverageGuideQA = "http://qahartfordauto.thehartford.com/coverage-guide";
var coverageGuideProd = "http://hartfordauto.thehartford.com/coverage-guide";

// home lite URLs
var homeQuoteQA = "https://qapersonal02.thehartford.com/home/ConsumerHome.do";
var homeQuoteProd = "https://personal02.thehartford.com/home/ConsumerHome.do";

// Common Login URLs
var clIframe1QA = "https://qaservice.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_globalm.faces";
var clIframe1Prod = "https://service.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_globalm.faces";
var clIframe2QA = "https://qaservice.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_content.faces";
var clIframe2Prod = "https://service.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_content.faces";
var clIframe3QA = "https://qaservice.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_Homepage.faces";
var clIframe3Prod = "https://service.thehartford.com/ServiceWeb/marketinglogin/marketingLogin_Homepage.faces";
var clQueryString = "?source=marketing";
var clServiceLinkQA = "https://qaservice.thehartford.com/ServiceWeb/appmanager/service/guest";
var clServiceLinkProd = "https://service.thehartford.com/ServiceWeb/appmanager/service/guest";
var clRegisterQA = "https://qaservice.thehartford.com";
var clRegisterProd = "https://service.thehartford.com";

// production domains
var aarpMarketingProd = "aarp.thehartford.com";
var directMarketingProd = "hartfordauto.thehartford.com";
var esearchProdDomain = "esearch.thehartford.com";

// devdomains
var aarpMarketingDev = "aarp.resultsbuilder.com";
var directMarketingDev = "hartfordauto.resultsbuilder.com";

// QA domains
var aarpMarketingQA = "qaaarp.thehartford.com";
var directMarketingQA = "qahartfordauto.thehartford.com";
var esearchProdQA = "qaesearch.thehartford.com";

dojo.addOnLoad(function() {
	setModuleTargets();
});

function setModuleTargets() {
	var currentSite = window.location.hostname;
	var currentLocationURL = window.location.href;
	var isServiceClaimsPage = false; //isService_policy_holder_login
	var isPolicyHolderPage = false;
	var isPolicyHolderRootDir = false;
	if( currentLocationURL != null && currentLocationURL != undefined && currentLocationURL.length > 0){
		if (currentLocationURL.indexOf("policyholder-login") != -1 )
		 {
			isPolicyHolderPage = true;
		 }
		 var locLength = currentLocationURL.length;
		 var lastIndexOfserviceRoot = currentLocationURL.lastIndexOf("Online-Insurance-Service/");
		 if( lastIndexOfserviceRoot  == (locLength - 25))
		 {
			 isPolicyHolderRootDir = true;
		 }
	}
	if( isPolicyHolderPage == true || isPolicyHolderRootDir == true)
	{
		isServiceClaimsPage = true;
	}
	dojo.forEach(dojo.query("form[action*='" + aarpQuoteQA + "'], form[action*='" + aarpQuoteProd + "'], form[action*='" + directQuoteQA + "'],	form[action*='" + directQuoteProd + "']"), function(node) {
		if (currentSite.indexOf("aarp") != -1) {
			// for aarp domains
			if (currentSite == aarpMarketingProd) {
				// Production
				node.action = aarpQuoteProd;
			} else {
				// default all else to QA
				node.action = aarpQuoteQA;
			}
		} else {
			// for direct domains
			if (currentSite == directMarketingProd) {
				// Production
				node.action = directQuoteProd;
			} else {
				// default all else to QA
				node.action = directQuoteQA;
			}
		}
	});
	// set URLs for eSearch module forms
	dojo.forEach(dojo.query("form[action*='" + esearchQA + "'], form[action*='" + esearchProd + "']"), function(node) {
		switch (currentSite) {
			case aarpMarketingProd:
				node.action = esearchProd;
				break;
			case directMarketingProd:
				node.action = esearchProd;
				break;
			case esearchProdDomain:
				node.action = esearchProd;
				break;
			default:
				node.action = esearchQA;
				break;
		}
	});
	// set links on page for coverage guide
	setLinkTarget(coverageGuideQA, coverageGuideProd);
	// set target for common login module
	dojo.forEach(dojo.query("iframe[src*='" + clIframe1QA + "'], iframe[src*='" + clIframe1Prod + "']"), function(node) {
		switch (currentSite) {

			case aarpMarketingProd:
				if(isServiceClaimsPage)
				{
					node.src = clIframe1Prod + clQueryString + "&login_location=aarp_svc-clm";
				}
				else
				{
					node.src = clIframe1Prod + clQueryString + "&login_location=aarp_mktg";
				}
				break;
			case directMarketingProd:
				if(isServiceClaimsPage)
				{
					node.src = clIframe1Prod + clQueryString + "&login_location=direct_svc-clm";
				}
				else
				{
					node.src = clIframe1Prod + clQueryString + "&login_location=direct_mktg";
				}
				break;
			case esearchProdDomain:
				node.src = clIframe1Prod + clQueryString;
				break;
			case aarpMarketingQA:
				if(isServiceClaimsPage)
				{
					node.src = clIframe1QA + clQueryString + "&login_location=aarp_svc-clm";
				}
				else
				{
					node.src = clIframe1QA + clQueryString + "&login_location=aarp_mktg";
				}
				break;
			case directMarketingQA:
				if(isServiceClaimsPage)
				{
					node.src = clIframe1QA + clQueryString + "&login_location=direct_svc-clm";
				}
				else
				{
					node.src = clIframe1QA + clQueryString + "&login_location=direct_mktg";
				}
				break;
			default:
				node.src = clIframe1QA + clQueryString + "&login_location=direct_mktg";
				break;
		}
	});
	dojo.forEach(dojo.query("iframe[src*='" + clIframe2QA + "'], iframe[src*='" + clIframe2Prod + "']"), function(node) {
		switch (currentSite) {
			case aarpMarketingProd:
				node.src = clIframe2Prod + clQueryString + "&login_location=aarp_svc-clm";
				break;
			case directMarketingProd:
				node.src = clIframe2Prod + clQueryString + "&login_location=direct_svc-clm";
				break;
			case esearchProdDomain:
				node.src = clIframe2Prod + clQueryString;
				break;
			case aarpMarketingQA:
				node.src = clIframe2QA + clQueryString + "&login_location=aarp_svc-clm";
				break;
			case directMarketingQA:
				node.src = clIframe2QA + clQueryString + "&login_location=direct_svc-clm";
				break;
			default:
				node.src = clIframe2QA + clQueryString + "&login_location=direct_svc-clm";
				break;
		}
	});
	dojo.forEach(dojo.query("iframe[src*='" + clIframe3QA + "'], iframe[src*='" + clIframe3Prod + "']"), function(node) {
		switch (currentSite) {
			case aarpMarketingProd:
				node.src = clIframe3Prod + clQueryString + "&login_location=aarp_mktg";
				break;
			case directMarketingProd:
				node.src = clIframe3Prod + clQueryString + "&login_location=direct_mktg";
				break;
			case esearchProdDomain:
				node.src = clIframe3Prod + clQueryString;
				break;
			case aarpMarketingQA:
				node.src = clIframe3QA + clQueryString + "&login_location=aarp_mktg";
				break;
			case directMarketingQA:
				node.src = clIframe3QA + clQueryString + "&login_location=direct_mktg";
				break;
			default:
				node.src = clIframe3QA + clQueryString + "&login_location=direct_mktg";
				break;
		}
	});
	// set links for common login
	setLinkTarget(clServiceLinkQA, clServiceLinkProd);
	setLinkTarget(clRegisterQA, clRegisterProd, true);
}

/*
*  This function will set update the href property of all links on the page
*  for the specified QA and Prod URL depending on the environment
*/
function setLinkTarget(qaURL, prodURL, exactMatch) {
	var comparisonOperator = "*";
	if (exactMatch) {
		comparisonOperator = "";
	}
	// update QA-to-QA and Prod-to-Prod links
	dojo.forEach(dojo.query("a[href" + comparisonOperator + "='" + qaURL + "'], a[href" + comparisonOperator + "='" + prodURL + "']"), function(node) {
		// handle query string parameters
		var queryString = "";
		if ((qaURL.indexOf("?") == -1) && (prodURL.indexOf("?") == -1) && (node.href.indexOf("?") != -1)) {
			queryString = "?" + node.href.split("?")[1];
		}
		// set new link href if needed
		switch (window.location.hostname) {
			case aarpMarketingProd:
				node.href = prodURL + queryString;
				break;
			case directMarketingProd:
				node.href = prodURL + queryString;
				break;
			case esearchProdDomain:
				node.href = prodURL + queryString;
				break;
			default:
				node.href = qaURL + queryString;
				break;
		}
	});
}

/*
*  This function will set update the target of the quote form if
*  the selection for line of business is either Home/Condo/Renters.
*  Takes the LOB selection and the form as input parameters.
*/
function setHomeQuoteTarget(selectedLOB, quoteForm) {
	if ((selectedLOB == "Home") || (selectedLOB == "Condo") || (selectedLOB == "Renters")) {
		// add KEY parameter needed by home lite application
		var newField = document.createElement("input");
		newField.setAttribute("type", "hidden");
		newField.setAttribute("id", "KEY");
		newField.setAttribute("name", "KEY");
		newField.setAttribute("value", "AARP_HOME");
		quoteForm.appendChild(newField);
		// check for QA versus production marketing site
		if (window.location.hostname == aarpMarketingProd) {
			// point form to home lite prod
			quoteForm.action = homeQuoteProd;
		} else {
			// all other environments, point form to home lite QA
			quoteForm.action = homeQuoteQA;
		}
	}
}
