function removeQueryParam(href, name) {
	var last = 0;
	while (true) {
		var start = href.indexOf(name, last);
		if (start == -1)
			return href;
		if ((start == 0 || href.charAt(start - 1) == '&' || href
				.charAt(start - 1) == '?')
				&& start + name.length < href.length) {
			var newHref = href.substring(0, start);
			var end = href.indexOf('&', start + name.length);
			if (end != -1)
				newHref += href.substring(end + 1);
			else if (start > 0)
				newHref = newHref.substring(0, newHref.length - 1);
			return newHref
		}
		last = start + 1
	}
}

function appendQuery(href, query) {
	if (href.indexOf('?') == -1)
		return href + '?' + query;
	return href + '&' + query
}

function replaceQueryParam(href, name, value) {
	return appendQuery(removeQueryParam(href, name), name + '=' + value)
}

var g_chooserShowing = false;
function toggleLanguageChooser() {
	var chooser = $('language_chooser');
	if (chooser) {
		chooser.style.display = g_chooserShowing ? 'none' : '';
		g_chooserShowing = !g_chooserShowing
	}
}
function hideLanguageChooser(evt) {
	if (!g_chooserShowing)
		return;
	if (!evt)
		evt = window.event || {};
	var t = (evt.srcElement ? evt.srcElement : (evt.target ? evt.target : null));
	while (t) {
		if (t.className == 'language') {
			return
		}
		t = t.parentNode
	}
	toggleLanguageChooser()
}

if (!document.onclick){
	document.onclick = hideLanguageChooser;
}

function setLocale(locale) {
	var url = replaceQueryParam(location.href, 'lang', locale);
	if (typeof g_plx_template != 'undefined' && g_plx_template)
		url = replaceQueryParam(url, 't', g_plx_template);
	location.href = url
};

function showLearnMore(evt){
	var popup = $('learn_more');
	if (popup) {
		var x = (document.viewport.getWidth() - Element.getWidth(popup))/2;
		var y = (document.viewport.getHeight() - Element.getHeight(popup))/2;
		popup.setStyle({ top: y+'px', left: x+'px' });
		popup.show();
		$('fade').show();
	}
	showIFrameAd(false);
}

function hideLearnMore(evt){
	$('learn_more').hide();
	$('fade').hide();
	showIFrameAd(true);
}

function showIFrameAd(showAd) {
	var iframe = $("rightiframe");
	if(iframe){
		var toggle = iframe.contentWindow.toggleAdDisplay;
		if(toggle){
			toggle(showAd);
		}
	}
}

function appendEmailSuffix(textField, emailSuffix) {
	if(textField && textField.value && textField.value.length > 0 && textField.value.indexOf("@") < 0) {
		textField.value = textField.value + '@' + emailSuffix;
	}
}

function focusLoginForm(loginIdFieldId, passwordFieldId) {
// focus login fields
	var userInput = $(loginIdFieldId);
	if (userInput && userInput.type == 'text') {
		userInput.focus();
	}
	else {
		var passInput = $(passwordFieldId);
		if (passInput && passInput.type == 'password') {
			passInput.focus();
		}
	}
}

function setupLoginForm(loginIdFieldId, passwordFieldId, comcastEmailSuffix) {
	Event.observe(loginIdFieldId, 'blur', function(event){
		appendEmailSuffix(event.element(), comcastEmailSuffix);
	});

	focusLoginForm(loginIdFieldId, passwordFieldId);
}