var hasBeenEmpty = false;

function showNotification(todo){
	if(todo){
		$('notification_message_div').style.display = 'block';
	}
	else{
		$('notification_message_div').style.display = 'none';
	}
}

function showHideDiv(id){
	if($(id).style.display == 'none'){
		$(id).style.display = 'block';
	}
	else{
		$(id).style.display = 'none';
	}
}

function emptyLoginFields(){
	if(!hasBeenEmpty){
		$('username').value = '';
		$('password').value = '';
		hasBeenEmpty = true;
	}
}

function validateLogin(){
	var username = $('username').value;
	var password = $('password').value;
	
	if(username == '' || password == ''){
		alert('Du måste ange användarnamn och lösenord.');
		return;
	}
	
	new Ajax.Request('/ajax/checkLogin.php',{
		method:'post', parameters: {username: username, password: password}, onSuccess: function(transport){
			var response = transport.responseText || "0";
			if(response == '0'){
				alert("Inloggning misslyckades.");	
				return;
			}
			$('globalLogin').submit();
		},
		onFailure: function(){ 
			alert('Någonting gick fel. Försök igen.') 
		}
	});
	
}

function requiredEmpty(arr){
	for(i=0;i<arr.length;i++){
		if(arr[i] == ''){
			return true;
		}
	}
	return false;
}

function changeCaptcha(){
	$('captcha').src = '/captcha.php?id=' + Math.random();				
}

function enterLogin(){
	alert(window.event.keyCode);
	if (window.event && window.event.keyCode == 13){
		validateLogin();
	}
}

function validCharsHomepageURL(url){
	for(i=0;i<url.length;i++){
		switch(url.charAt(i)){
			case 'a': break;case 'A': break;
			case 'b': break;case 'B': break;
			case 'c': break;case 'C': break;
			case 'd': break;case 'D': break;
			case 'e': break;case 'E': break;
			case 'f': break;case 'F': break;
			case 'g': break;case 'G': break;
			case 'h': break;case 'H': break;
			case 'i': break;case 'I': break;
			case 'j': break;case 'J': break;
			case 'k': break;case 'K': break;
			case 'l': break;case 'L': break;
			case 'm': break;case 'M': break;
			case 'n': break;case 'N': break;
			case 'o': break;case 'O': break;
			case 'p': break;case 'P': break;
			case 'q': break;case 'Q': break;
			case 'r': break;case 'R': break;
			case 's': break;case 'S': break;
			case 't': break;case 'T': break;
			case 'u': break;case 'U': break;
			case 'v': break;case 'V': break;
			case 'w': break;case 'W': break;
			case 'x': break;case 'X': break;
			case 'y': break;case 'Y': break;
			case 'x': break;case 'X': break;
			case 'z': break;case 'Z': break;
			case '0': break;case '1': break;case '2': break;case '3': break;case '4': break;
			case '5': break;case '6': break;case '7': break;case '8': break;case '9': break;
			case '-': break;case '_': break;
			default: return false;
		}
	}
	return true;
}

function Borderlize( ref, blnBorder ) {
	if( blnBorder )
		ref.borderColor = "#000000";
	else
		ref.borderColor = "#cccccc";
}

function resetTextField(id, value){
	if($(id).value == value){
		$(id).value = '';
	}
}

function switchGlobalImage(img, id){
	$(id).src = img;
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
	}


function showMostRead(caller, hidden_mest){
	$('most_read_all_list').value = hidden_mest;
	$('show_today_most_read').className = 'pink';
	$('show_week_most_read').className = 'pink';
	$('show_year_most_read').className = 'pink';
	
	var show = 'show_'+caller;
	$(show).className = 'bold';
	
	$('today_most_read').style.display = 'none';
	$('week_most_read').style.display = 'none';
	$('year_most_read').style.display = 'none';
	
	$(caller).style.display = 'block';
}

function showMostCommented(caller, hidden_mest){
	$('most_commented_all_list').value = hidden_mest;
	$('show_today_most_commented').className = 'pink';
	$('show_week_most_commented').className = 'pink';
	$('show_year_most_commented').className = 'pink';
	
	var show = 'show_'+caller;
	$(show).className = 'bold';
	
	$('today_most_commented').style.display = 'none';
	$('week_most_commented').style.display = 'none';
	$('year_most_commented').style.display = 'none';
	
	$(caller).style.display = 'block';	
}

function goToMostRead(){
	var most_read_all_list = $('most_read_all_list').value;
	location.href = "/mest-lasta-artiklar/" + most_read_all_list;
}

function goToMostCommented(){
	var most_commented_all_list = $('most_commented_all_list').value;
	location.href = "/mest-kommenterade-artiklar/" + most_commented_all_list;
}

function switchMostRead(arg){
	if(arg == 'mostRead'){
		$('mostRead').style.backgroundColor = '#cc6699';
		$('mostRead').style.borderBottom = '3px solid #993366';
		$('mostCommented').style.backgroundColor = '#e9a6c7';
		$('mostCommented').style.borderBottom = '3px solid #cd84a8';
		$('mostReadtable').style.display = 'block';
		$('mostcommentstable').style.display = 'none';
	}
	else{
		$('mostRead').style.backgroundColor = '#e9a6c7';
		$('mostRead').style.borderBottom = '3px solid #cd84a8';
		$('mostCommented').style.backgroundColor = '#cc6699';
		$('mostCommented').style.borderBottom = '3px solid #993366';
		$('mostReadtable').style.display = 'none';
		$('mostcommentstable').style.display = 'block';
		//alert($('mostcommentstable').style.display);
	}
}
