function onTextInputFocusIn(input)
{
	var c = input.style.color;
	
	if (!input.modified) {
		input.value = '';
		input.style.color = '#000000';
	}
}

function onTextInputChange(input)
{
	input.modified = true;
}

function onTextInputFocusOut(input, defaultValue)
{
	if (input.value == '') {
		input.modified = false;
		input.value = defaultValue;
		input.style.color = '#aaaaaa';
	} else {
		input.modified = true;
	}
}

function getAbsolutePosition(target)
{
	var x = 0;
	var y = 0;
	
	if (target) {
		while(target != null) {
			x += target.offsetLeft;
			y += target.offsetTop;
			target = target.offsetParent;
		}
	}
	
	return {x: x, y: y};
}

function openRSSPopup(target, initialPageId)
{
	var cbs = document.getElementsByTagName('input');
	for (var i = 0; i < cbs.length; i++) {
		var cb = cbs[i];
		if (cb.id.indexOf('cb_rss_feed_') > -1) {
			cb.checked = (cb.value == initialPageId);
		}
	}
	
	updateRSSLink();
	
	openPopup('popup_rss', target);
}

var isMouseOverPopup = false;
var popupCloseTimeout = 250;
var popupCloseTimer = 0;

function showPopupMenu(popupMenuId, target)
{
	// Set a flag to know that mouse is over a popup menu
	isMouseOverPopup = true;
	
	// If popupMenuId and target have been defined, open the needed popup menu
	if (popupMenuId && target) {
		var elId = 'popup_menu_' + popupMenuId;
		openPopup(elId, target, 0, false);
	}
}

/**
 * Prepare to close the popup menu
 * @return
 */
function hidePopupMenu()
{
	isMouseOverPopup = false;
	popupCloseTimer = window.setTimeout(_hidePopupMenu, popupCloseTimeout);
}

function _hidePopupMenu()
{
	if (!isMouseOverPopup) closePopup();
}

var currentPopup;

function openPopup(popupId, target, yOffset, appendToTarget)
{
	closePopup();
	
	if (appendToTarget == null) appendToTarget = false;
	if (yOffset == null) yOffset = 8;
	
	var popup = document.getElementById(popupId);
	if (!popup) return;
	
	if (target && appendToTarget) {
		if (target.insertAdjacentElement){
			target.insertAdjacentElement('beforeEnd', popup);
		} else if (target.appendChild) {
			target.appendChild(popup);
		}
	}
	
	var pos = getAbsolutePosition(target);
	var x = pos.x;
	var y = pos.y;
	
	popup.style.display = 'block';
	
	var docWidth = (document.width != undefined) ? document.width : document.body.clientWidth;
	
	x = x + (target.offsetWidth - popup.offsetWidth) / 2;
	x = Math.max(0, Math.min(docWidth - popup.offsetWidth - 20, x));
	y += target.offsetHeight + yOffset;
	
	popup.style.left = x + 'px';
	popup.style.top = y + 'px';
	
	currentPopup = popup;
	
	return popup;
}

function closePopup(popupId)
{
	var popup = (popupId != null) ? document.getElementById(popupId) : currentPopup;
	if (popup != null) popup.style.display = 'none';
}

function updateRSSLink()
{
	var url = '';
	var cbs = document.getElementsByTagName('input');
	for (var i = 0; i < cbs.length; i++) {
		var cb = cbs[i];
		if (cb.id.indexOf('cb_rss_feed_') > -1 && cb.checked) url += '$' + cb.value;
	}
	
	url = (url == '') ? 'javascript:void(0);' : '/rss/' + url + '.rss';
	
	var link = document.getElementById('rss_feed_link');
	link.href = url;
	
}

function submitForm(formElementId)
{
	var formElement = document.forms[formElementId];
	alert('id: ' + formElementId + ', form: ' + formElement);
	var formElement = document.getElementById(formElementId);
	alert('id: ' + formElementId + ', form: ' + formElement);
	
	if (formElement != null) {
		formElement.submit();
	}
}

function checkPasswordStrength(el, strength_indicator_id) {
	
	var wrap = document.getElementById('yypwd_strength_wrap');
	wrap.style.display = 'block';
	
	var strength = document.getElementById(strength_indicator_id);
	var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
	var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
	var enoughRegex = new RegExp("(?=.{6,}).*", "g");
	var pwd = el;
	if (pwd.value.length==0) {
		strength.className = "yy_password_indicator strength_1";
		strength.innerHTML = 'Kirjoita salasana';
	} else if (false == enoughRegex.test(pwd.value)) {
		strength.className = "yy_password_indicator strength_1";
		strength.innerHTML = 'Liian lyhyt';
	} else if (strongRegex.test(pwd.value)) {
		strength.className = "yy_password_indicator strength_3";
		strength.innerHTML = 'Vahva';
	} else if (mediumRegex.test(pwd.value)) {
		strength.className = "yy_password_indicator strength_2";
		strength.innerHTML = 'Keskiverto';
	} else {
		strength.className = "yy_password_indicator strength_1";
		strength.innerHTML = 'Heikko';
	}
}

function checkPasswordMatch(element_id_1, element_id_2, indicator_id) {
	el1 = document.getElementById(element_id_1);
	el2 = document.getElementById(element_id_2);
	ind_el = document.getElementById(indicator_id)
	if (el1.value != el2.value) {
		ind_el.innerHTML = "Salasanat eiv&auml;t t&auml;sm&auml;&auml;";
	} else {
		ind_el.innerHTML = "";
	}
}

