var swappableDomains = new Array()
swappableDomains["http://localunivision."] = "http://local."
swappableDomains["http://qaunivision."] = "http://qa."
swappableDomains["http://uatunivision."] = "http://wuat."
swappableDomains["https://uatunivision."] = "https://uat."
swappableDomains["http://univision."] = "http://www."
swappableDomains["https://univision."] = "https://wumt."

var netSageLaunchPages = [
	"mtReceiver"
]

var netSagePages = [
	"mtReceiver",
	"mtSender",
	"mtServices",
	"mtPayment",
	"mtReviewp",
	"mtReceipt",
	"mtNonServiceable",
	"wuHeader"
]

function customGetCountry(country) {
	if (country == "U1") {
		return "US"
	} else if (country == "R1") {
		return "RU"
	} else {
		return null
	}
}

function customGo(where, closePopup, region, params, anchorField, target, isPopupCloser) {
	if (isPopupCloser && useNetSage == "true" && netSage == "true") {
		close()
		return true
	}
	return false
}

function customGoHome() {
	var loc = document.location.toString().toLowerCase()
	var domain = getDomain()
	if (
		loc.indexOf("univision") != -1 &&
		loc.indexOf(domain) + domain.length + 1  >= loc.length
	) {
		return ["index_consumer.asp?country=US&language=sp","US"]
	} else {
		return null
	}
}

function customLookupCountryDomain(url) {
	if (url.indexOf("netsage.finali.com") != -1) {
		return "https://wumt.westernunion.com"
	} else if (url.indexOf("staging.finali.com") != -1) {
		return "https://uat.westernunion.com"
	} else if (url.indexOf("localhost") != -1) {
		return "http://local.westernunion.com"
	} else {
		return null
	}
}

function customPageIsUnloading() {
	var pageName = getForm().PAGE_NAME.value.toLowerCase()
	var result = getForm().RESULT.value.toLowerCase()
	var country = getRegion()
	var lang = getLanguage()
	if (country == "US" && lang == "en" && pageName.indexOf("mtreceipt_d2b") > -1) {
		//SCR5621, SCR5883, SCR6160(end survey)
		//takeSurveyRandom("TMJV84", 1, null, true)
	} else if (("go,submit,error,refresh,display".indexOf(result) == -1) && (pageName.indexOf("header") == -1))  {
		if (country == "GB" && pageName.indexOf("osmoneytransferoptions") > -1) {
			//SCR5255
			takeSurveyFirstUsers("6CGGUK", 20, null, true)
		} else if (country == "FR" && lang == "fr" && pageName.indexOf("mtreceiver") > -1) {
			//SCR5280
			takeSurveyRandom("HXBV8D", .25, null, true)
		} else if (country == "US" && getCookie("CNY") == "true" && getCookie("mtReceipt") != "true") {
					takeSurveyRandom("KXMJ2H", .2, "http://firstdatacorp.inquisiteasp.com/surveys/KXMJ2H")
		} else if (country == "US" && lang == "en") {
			var sawASurvey = false
			if (pageName == "mtreceipt") {
					//SCR6054 add survey
					//scr6615 remove survey
					//sawASurvey = takeSurveyRandom("p6350l", .2, "http://www.sendyouropinions.com/survey.asp?project=p6350l")
				} else if (pageName == "mtreceiptsf") {
					//SCR6054 add survey
					//scr6615 remove survey
					//sawASurvey = takeSurveyRandom("p6350s", .2, "http://www.sendyouropinions.com/survey.asp?project=p6350s")
				}
			if (!sawASurvey) {
				if (pageName.indexOf("mtreceipt") > -1 || pageName.indexOf("qcreceipt") > -1) {
					//SCR5241
					takeSurveyRandom("JGYCYV", .05, null)
				} else if (pageName.indexOf("qcreceiver") > -1 || pageName.indexOf("qcsender") > -1 || pageName.indexOf("qcpayment") > -1 || pageName.indexOf("qcreview") > -1 || pageName.indexOf("osquickcollectoptions") > -1) {
					  //SCR5775, SCR6182(end survey)
					  //takeSurveyRandom("PM7Z5D", 1, null, true)
				} else {
					//SCR5240
					takeSurveyRandom("DHECT3", .05, null)
				}
			}
		}  else if (lang == "nl")  {
			if (pageName.indexOf("osmoneytransferoptions") > -1 || pageName.indexOf("mtreceiver") > -1 || pageName.indexOf("mtservices") > -1 || pageName.indexOf("mtpayment") > -1 || pageName.indexOf("mtreview") > -1) {
				//SCR5643
				//SCR6461 remove survey
				//takeSurveyRandom("MTAARX", 1, null, true)
			}
		}
	}
}


function customSetFormAction(form, filePath, language, region) {
	if (useNetSage == "true") {
		var currentPage = lookupNameValuePair(form.SESSION.value, "CURRENT_PAGE")
		var currentPageIsFinali = false
		for (var i = 0; i < netSagePages.length; i++) {
			if (currentPage == netSagePages[i]) {
				currentPageIsFinali = true
				break
			}
		}
		var destPageIsFinali = false
		for (var i = 0; i < netSagePages.length; i++) {
			if (filePath.indexOf(netSagePages[i]) != -1) {
				destPageIsFinali = true
				break
			}
		}
		
		if (language == "en" && region == "US") {
			var result = form.RESULT.value
			for (var i = 0; i < netSageLaunchPages.length; i++) {
				if (filePath.indexOf("/" + netSageLaunchPages[i]) != -1 && result != "MODIFY" && result != "ERROR") {
					var finaliParams = "?customer=wu&iid=wu-mt&content=" + escape(filePath)
					if ((document.location + "").indexOf("https") != -1) {
						var finaliPath = (environment.indexOf("prod") != -1 ? "netsage" : "staging") + ".finali.com/netsage"
						if (environment.indexOf("dev") == -1) {
							filePath = (filePath.indexOf("https") != -1 ? "https" : "http") + "://" + finaliPath + finaliParams + "&chosts=" + serverName
							form.SESSION.value = convertChar(form.SESSION.value, "_", "%5F")
						} else { 
							filePath += (filePath.indexOf("?") == -1 ? "?" : "&") + "netSage=" + finaliPath
						}
					} else {
						filePath = buildLocalFilePath("asp/netsageRedirect.asp" + finaliParams, region)
					}
					break
				}
			}
			if (currentPageIsFinali && !destPageIsFinali) {
				form.target = "_top"
				filePath += (filePath.indexOf("?") == -1 ? "?" : "&") + "removeNetsage=true"
			}
		}
	}
	return filePath
}

function customSwapDomains(url, country) {
	if (USsites.indexOf(country) != -1) return null
	var segment = url.split(".")[0] + "."
	var newSegment = swappableDomains[country + ":"+ segment]
	if (newSegment == null) {
		newSegment = swappableDomains[segment]
	}
	if (newSegment != null) {
		return url.replace(new RegExp(segment), newSegment)
	} else {
		return null
	}
}

function customUpdateSessionOnNewRegion() {
	var form = getForm()
	if ((linkShareID = getCookie("LinkShareID")) != null) {
		form.SESSION.value += 
			"LinkShareID=" + linkShareID + ";"+
			"LinkShareMerchantID=" + ((merchantID = getCookie("LinkShareMerchantID")) == null ? "": merchantID) + ";"+
			"LinkShareDateStamp=" + getCookie("LinkShareDateStamp") + ";"
	}
	if ((tns_survey = getCookie("TNS_SURVEY")) != null) {
		form.SESSION.value += "TNS_SURVEY=" + tns_survey + ";"
	}
}

function takeSurveyFirstUsers(survey,count, surveyURL, fullWindow) {
  if (getCookie(survey) != "true" && getCookie(survey) != "false") {
  
			// Create an instance of the XML HTTP Request object
			var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
				
			// Prepare the XMLHTTP object for a HTTP GET to counter value
			var sURL = "/info/AppCounter.asp?counter=UK_MTOPTIONS_DROP_COUNTER";
			oXMLHTTP.open( "GET", sURL, false );
			
			// Execute the request
			oXMLHTTP.send();
			
			//alert(oXMLHTTP.responseText)
			if (oXMLHTTP.responseText <= count) {
				 setCookie(survey, "true", "")
			   takeSurvey(survey, surveyURL, fullWindow)
			   return true
			} else {
				return false
			}
	}
}	

function takeSurveyRandom(survey, pct, surveyURL, fullWindow) {
	if (getCookie(survey) != "true" && getCookie(survey) != "false") {
		if (Math.random() < pct) {
			takeSurvey(survey, surveyURL, fullWindow)
			return true
		} else {
			setCookie(survey, "false", "")
			return false
		}
	}
}

function takeSurvey(which, surveyURL, fullWindow) {
	setCookie(which, "true", "")
	if (surveyURL == null) {
		var url = document.location.toString()
		url =  url.substring(url.indexOf("//") + 2)
		var offset = url.indexOf("/")
		var domain =  url.substring(0, offset).toLowerCase()
		url = url.substring(offset)
		if (
			domain == "wumt.westernunion.com" || 
			domain == "netsage.finali.com"
		) {
			domain = "www.westernunion.com"
		} else if (
			domain == "uat.westernunion.com" || 
			domain == "staging.finali.com"
		) {
			domain = "wuat.westernunion.com"
		}
		surveyURL = "http://firstdatacorp.inquisiteasp.com/surveys/" + which 
	}
	if (fullWindow == true) {
		popup(surveyURL, "survey")
	} else {
		popup(surveyURL, "survey", 700, 500, "scrollbars,resizable,status", null, null, true)
	}
}

