var servicesHash = new Array()
servicesHash["Q"] = "Quick Collect"
servicesHash["P"] = "Prepaid Services"

var countryHash = new Array()
var revCountryHash = new Array()
var currenciesHash = new Array()
var revCurrenciesHash = new Array()
var countryCurrenciesHash = new Array()
var optionsHash = new Array()
var countryOptionsHash = new Array()

var plural = true
var singular = false
var inEnglish = true

var regExp = new RegExp("& ")
for (var i = 0; i < countryNames.length; i++) {
	var intlCountryName = countryNames[i][1].replace(regExp, "&")
	var enCountryName = countryNames[i][2].replace(regExp, "&").toUpperCase()
	countryHash[countryNames[i][0]] = [intlCountryName, enCountryName]
	revCountryHash[enCountryName] = countryNames[i][0]
	revCountryHash[intlCountryName] = countryNames[i][0]
}

for (var i = 0; i < currencyNames.length; i++) {
	currenciesHash[currencyNames[i][0]] = [currencyNames[i][1], currencyNames[i][2], currencyNames[i][3]]
	revCurrenciesHash[currencyNames[i][1]] = currencyNames[i][0]
	revCurrenciesHash[currencyNames[i][2]] = currencyNames[i][0]
}
for (var i = 0; i < countryCurrencies.length; i++) {
	if (countryHash[countryCurrencies[i][0]] == null) continue
	countryCurrenciesHash[countryCurrencies[i][0]] = countryCurrencies[i][1]
}
for (var i = 0; i < optionNames.length; i++) {
	optionsHash[optionNames[i][0]] = optionNames[i][1]
}
for (var i = 0; i < countryOptions.length; i++) {
	countryOptionsHash[countryOptions[i][0]] = countryOptions[i][1]
}

function getCountryCode(country) {
	if (country == "SAO TOME AND PRINCIPE" ) return "ST"
	if (country == "CYPRUS NORTHERN" ) return "C2"
	if (country == "" || revCountryHash[country] == null) return ""
	else return revCountryHash[country]
}

function getCountryName(countryCode, inEnglish) {
	if (countryCode == "" || countryHash[countryCode] == null) return ""
	else if (inEnglish == true) return countryHash[countryCode][1]
	else return convertEntities(countryHash[countryCode][0])
}

function getCountryCurrencies(countryCode) {
	var countryCurrencies
	if (countryCode == "" || (countryCurrencies = countryCurrenciesHash[countryCode]) == null) return null
	var currencies = new Array()
	var currenciesHash = new Array()
	for (var i = 0; i < countryCurrencies.length; i++) {
		if (currenciesHash[countryCurrencies[i]] == null) {
			currenciesHash[countryCurrencies[i]] = true
			currencies[currencies.length] = countryCurrencies[i]
		}
	}
	return currencies
}

function getDefaultCountryCurrency(countryCode) {
	if (countryCode == "") {
		return "USD"
	} else if (countryCurrenciesHash[countryCode] == null) {
		if (countryCode == "CA") return "CAD"
		else return "USD"
	}
	else return countryCurrenciesHash[countryCode][0]
}

function getCountryOptions(countryCode) {
	if (countryCode == "") return ""
	var optionsArray = countryOptionsHash[countryCode]
	if (optionsArray == null) return ["AGT"]
	else return optionsArray
}

function getCurrencyCode(currencyName) {
	if (currencyName == "" || revCurrenciesHash[currencyName] == null) return ""
	else return revCurrenciesHash[currencyName]
}

function getCurrencyName(currencyCode, plural) {
	if (currencyCode == "" || currenciesHash[currencyCode] == null) return ""
	else if (plural == true) return currenciesHash[currencyCode][1]
	else return currenciesHash[currencyCode][0]
}

function getCurrencySymbol(currencyCode) {
	if (currencyCode == "" || currenciesHash[currencyCode] == null) return ""
	else if (currenciesHash[currencyCode][2] != null) return currenciesHash[currencyCode][2]
	else return ""
}

function getLanguageName(languageCode) {
	if (languageCode == "" || languageHash[languageCode] == null) return ""
	else return languageHash[languageCode]
}

function getOptionName(optionCode) {
	if (optionCode == "" || optionsHash[optionCode] == null) return ""
	else return optionsHash[optionCode]
}

function placeLocalCurrencyFirst(currencyArray) {
	var origCountry = fields["ORIG_COUNTRY"]
	if (origCountry != null) {
		origCountry = origCountry.getValue()
	} else {
		origCountry = getRegion()
	}
	var localCurrency = getDefaultCountryCurrency(origCountry)
	var prunedArray = [localCurrency]
	if (currencyArray != null) {
		for (var i = 0; i < currencyArray.length; i++) {
			if (currencyArray[i] != null && currencyArray[i] != localCurrency) {
				prunedArray[prunedArray.length] = currencyArray[i]
			}
		}
	}
	return prunedArray
}

function convertEntities(data) {
	var regExp = new RegExp("&#([^;]*);", "g")
	var matchArray = regExp.exec(data)
	if (matchArray != null && matchArray.length > 1) {
		for (var i = 1; i < matchArray.length; i++) {
			var re = new RegExp("&#" + matchArray[i] + ";", "g")
			data = data.replace(re, String.fromCharCode(matchArray[i]))
		}
	}
	return data
}
