function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function poplogin() {
	var pop_login = document.getElementById('pop_login_form');
	if(pop_login.style.display == 'block') pop_login.style.display = 'none';
	else pop_login.style.display = 'block';
}

function checkLogin()
{
	if(trim(document.getElementById('box_username').value) == '')
	{
		alert('You need to type username!');
		return false;
	}
	if(document.getElementById('box_password').value == '')
	{
		alert('You need to type password!');
		return false;
	}
	return true;
}

function checkLoginForm()
{
	if(trim(document.getElementById('username').value) == '')
	{
		alert('You need to type username!');
		return false;
	}
	if(document.getElementById('password').value == '')
	{
		alert('You need to type password!');
		return false;
	}
	return true;
}

function checkemail(email)
{
	if(email == '') return false;
	var reg = /^([a-z0-9_\-\.])+\@(?=.{4,100}$)(([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9]){0,1}\.)+[a-z0-9]{2,6}$)/i;
	return reg.test(email);
}

function checkusername(username)
{
	if(username == '') return false;
	var reg = /^(?=.{6,32}$)([a-z0-9]([a-z0-9\-\.]{4,30})[a-z0-9]$)/i;
	return reg.test(username);
}

function checkActivationForm()
{
	if(trim(document.getElementById('activeemail').value) == '')
	{
		alert('You need to type your email!');
		return false;
	}
	if(checkemail(trim(document.getElementById('activeemail').value)) == false)
	{
		alert('Please type a valid email!');
		return false;
	}
	
	if(document.getElementById('activation').value == '')
	{
		alert('You need to type your activation code!');
		return false;
	}
	return true;
}

function ManagerLimitText(limitField, limitCount, limitNum)
{
	if(limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		var fieldname = 'Description: ';
		if(limitNum == 64) fieldname = 'Name: ';
		if(limitCount) limitCount.innerHTML = fieldname + (limitNum - limitField.value.length) + ' chars left';
	}
}

function LimitText(limitField, limitCount, limitNum)
{
	if(limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		if(limitCount) limitCount.innerHTML = '(' + (limitNum - limitField.value.length) + ' chars left)';
	}
}

function checkKeyword()
{
	var keyword = trim(document.getElementById('keyword').value).toLowerCase();
	if(keyword == 'cigar search...' || keyword.length < 3)
	{
		alert('Keyword should be at least 3 characters!');
		return false;
	}
}
