var UK_CONVERSION_RATE = 0.87;
var US_CONVERSION_RATE = 1.4;

$(document).ready( function() {
	try {
		if (typeof(periods) != "undefined" && typeof(resources) != "undefined") {
			var region = get_region();
			update_prices_redirects(region);
		} 
	} catch(err) {}
});

function get_region() {
    var region = get_cookie("selected_currency_region");
    if (region == "") {
	var country = geoip_country_code().toLowerCase();

        if (country == "gb") {
            region = "uk";
        } else if(country == "us") {
            region = "us";
        }
        region = "eur";
    }
    return region
}

function set_active_currency_link(region) {
	var links = $("table[id = 'currency-select'] td");
	links.each( function() {
		if ($(this).attr("id") == region) {
			$(this).attr("class", "active-currency");		
		} else {
			$(this).attr("class", "");
		}
	});
}

function update_prices_redirects(region) {
	if (typeof(periods) != "undefined" && typeof(resources) != "undefined") {
		change_currency(region);
		//update_store_redirect_urls(region);
		//update_shop_redirect_urls(region);
		//set_active_currency_link(region);
	}
}

function change_currency(region) {
	set_region_period_prices(region);
	set_region_resource_prices(region);
	set_conversions_by_rate(region);
	//Store the selected region in a cookie for future visits
	set_cookie("selected_currency_region", region);
}

function set_conversions_by_rate(region) {
    var periods_to_change = $("[name ^= 'period-convert_']");
	periods_to_change.each( function() {
		var eur_amt = $(this).attr("name").replace("period-convert_", "");
		if (region == "uk") {
			var converted_amt = eur_amt * UK_CONVERSION_RATE;
		} else if (region == "us") {
			var converted_amt = eur_amt * US_CONVERSION_RATE;
		} else {
			var converted_amt = eur_amt;
		}
		var converted_price_str = get_currency_symbol(region) + parseFloat(converted_amt).toFixed(2);
		$(this).html(converted_price_str);
		
	});
	
}

function set_region_period_prices(region) {
    var periods_to_change = $("[name ^= 'period_']");
    periods_to_change.each( function() {
        var period_id = $(this).attr("name").replace("period_", "");
        if (!isNaN(period_id) && array_key_exists(period_id, periods)) {
            var period = periods[period_id];
            var region_price = period["price_" + region] * period["duration"];
            region_price = parseFloat(region_price).toFixed(2);
            var currency_symbol = get_currency_symbol(region);
            assign_price($(this), region_price, currency_symbol);
        }
    });
	
	// Added this block to provide period rates for the domains.html page
	var periods_to_change = $("[name ^= 'period-rate_']");
    periods_to_change.each( function() {
        var period_id = $(this).attr("name").replace("period-rate_", "");
        if (!isNaN(period_id) && array_key_exists(period_id, periods)) {
            var period = periods[period_id];
            var region_price = period["price_" + region];
            region_price = parseFloat(region_price).toFixed(2);
            var currency_symbol = get_currency_symbol(region);
            assign_price($(this), region_price, currency_symbol);
        }
    });

	var setups_to_change = $("[name ^= 'period-setup_']");
    setups_to_change.each( function() {
        var period_id = $(this).attr("name").replace("period-setup_", "");
        if (!isNaN(period_id) && array_key_exists(period_id, periods)) {
            var period = periods[period_id];
            var region_price = period["setup_" + region] * period["duration"];
            region_price = parseFloat(region_price).toFixed(2);
            var currency_symbol = get_currency_symbol(region);
            assign_price($(this), region_price, currency_symbol);
        }
    });
}


function set_region_resource_prices(region) {
    var resources_to_change = $("[name ^= 'resource_']");
    resources_to_change.each( function() {
        var resource_id = $(this).attr("name").replace("resource_", "");
        var exists = array_key_exists(resource_id, resources);
        if (!isNaN(resource_id) && array_key_exists(resource_id, resources)) {
            var region_price = resources[resource_id]["price_" + region];
            region_price = parseFloat(region_price).toFixed(2);
            var currency_symbol = get_currency_symbol(region);
            assign_price($(this), region_price, currency_symbol);
        }
    });

    var setups_to_change = $("[name ^= 'resource-setup_']");
	setups_to_change.each( function() {
        var resource_id = $(this).attr("name").replace("resource-setup_", "");
        if (!isNaN(resource_id) && array_key_exists(resource_id, resources)) {
            var region_price = resources[resource_id]["setup_" + region];
            region_price = parseFloat(region_price).toFixed(2);
            var currency_symbol = get_currency_symbol(region);
            assign_price($(this), region_price, currency_symbol);
        }
    });
}

function assign_price(price_elem, region_price, currency_symbol) {
    var elem_type = price_elem[0].tagName.toLowerCase();

    if (elem_type == "span") {
        price_elem.html(currency_symbol + region_price);
    } else if (elem_type == "input") {
        price_elem.attr("value", region_price);
    }
}

function update_shop_redirect_urls(region) {
	var hrefs = $("a[name = 'shop-redirect']");
	hrefs.each( function() {
		var addr = $(this).attr("href");


		// Temporarily replace hosted-exchange-shop.html with hosted-exchange-shop-test.html, same for shared shop
		addr = addr.replace("hosted-exchange-shop.html", "hosted-exchange-shop-test.html");
		addr = addr.replace("shared-hosting-shop.html", "shared-hosting-shop-test.html");

		if (!addr.match(/region=(eur|us|uk)/)) {
		    if (!addr.match(/\?/)) {
			    addr = addr + "?"
		    } else {
		        addr = addr + "&"
			}
		    addr = addr + "region=" + region;
		} else {
			addr = addr.replace(/region=(eur|us|uk)/, "region=" + region);
		}
		$(this).attr("href", addr);
	});
}

function update_store_redirect_urls(region) {
	var hrefs = $("a[name = 'store-redirect-url']");
	hrefs.each( function() {
		var curr_url = $(this).attr("href");

		if (region == "eur") {
	        $(this).attr("href", curr_url.replace(/:\/\/(ukstore|usstore)\.blacknight\.com/, "://store.blacknight.com"));
	    } else if(region == "us") {
	        $(this).attr("href", curr_url.replace(/:\/\/(store|ukstore)\.blacknight\.com/, "://usstore.blacknight.com"));
		} else if(region == "uk") {
	        $(this).attr("href", curr_url.replace(/:\/\/(store|usstore)\.blacknight\.com/, "://ukstore.blacknight.com"));
	    }

	});
}

function get_currency_symbol(region) {
	if (region == "eur") {
	        return "&euro;";
	} else if(region == "us") {
        	return "$";
	} else if(region == "uk") {
		return "&pound;";
	}

}

function array_key_exists(key, search) {
	return (typeof search[key] != "undefined");
}