function textCounter(field, count_id, minlimit, maxlimit)
{
    var cleanString = field.value.replace(/[\n\r]+/g,'')
    if (field.value.length != cleanString.length) {
        // now we just remove the new lines
        field.value = cleanString;
    }
    var cleanString = cleanString.replace(/\s{2,}/g, ' ');
    
	var counter = document.getElementById(count_id);
	var send_btn = document.getElementById('send_btn');
	if (cleanString.length < minlimit) {
		counter.style.backgroundColor = "#eca2a4";
	} else if (cleanString.length > maxlimit) {
		counter.style.backgroundColor = "#eca2a4";
	} else {
		counter.style.backgroundColor = "#fcfcfc";
	}
	
	counter.innerHTML = cleanString.length;

	if(story_lock > 0) {
		lock_story();
	}
	
}
function change_cat() {
	var catEl = document.getElementById('cat');
	switch (parseInt(catEl.value)) {
		case 1:
			min_chr = 40;
			max_chr = 200;
			break;
		case 2:
			min_chr = 200;
			max_chr = 400;
			break;
		case 3:
			min_chr = 400;
			max_chr = 600;
			break;
		case 4:
			min_chr = 600;
			max_chr = 800;
			break;
		case 5:
			min_chr = 800;
			max_chr = 1000;
			break;
		case 6:
			min_chr = 1000;
			max_chr = 60000;
			break;
	}
	textCounter(document.getElementById('text'), 'countr', min_chr, max_chr);
}
var min_chr = 40;
var max_chr = 200;

var story_lock = 0;
var countDownTimeout = null;

var LOCK_SECONDS = 60;
function lock_story() {
	var lock_btn = document.getElementById('lock_btn');

	// Lås story på servern
	// Om det inte går visa felmedelande.

	//story_lock = LOCK_SECONDS;
	
	if(story_lock <= 0) {
		story_lock = LOCK_SECONDS; 
		update_server_lock();
	} else {
		story_lock = LOCK_SECONDS;
		clearTimeout(countDownTimeout);
		lock_btn.value = "Lock ("+story_lock+")";
		countDownTimeout = setTimeout("countDownLock();", 1000);
	}


	
}

function countDownLock() {
	var lock_btn = document.getElementById('lock_btn');
	story_lock--;

	if(story_lock > 0) {
		lock_btn.value = "Lock ("+story_lock+")";
		countDownTimeout = setTimeout("countDownLock();", 1000);
	} else {
		lock_btn.value = "Lock";
		countDownTimeout = null;
	}
}

function update_server_lock() {
	if(story_lock > 0) {
		
		xmlHttp=GetXmlHttpObject();
		xmlHttp.onreadystatechange=stateChangedUpdateLock;
		
		xmlHttp.open("GET","/lock/"+story_id+"-"+story_lock+".html",true);
		
		xmlHttp.send(null);
		
		// update hos server
		//setTimeout("update_server_lock();", story_lock*1000);
	}
}

function GetXmlHttpObject(){var xmlHttp=null;try{xmlHttp=new XMLHttpRequest();}catch(e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}} return xmlHttp;}


function stateChangedUpdateLock() {
	var lock_btn = document.getElementById('lock_btn');
	if (xmlHttp.readyState==4) { 
		if (xmlHttp.status == 200) {
			if(xmlHttp.responseText == "0") {
				alert("The story is already locked by someone else!");
				lock_btn.value = "Lock";
				story_lock = 0;
			} else if(xmlHttp.responseText == "1") {
				alert("You are not logged in!");
				lock_btn.value = "Lock";
				story_lock = 0;
			} else if(xmlHttp.responseText == "2") {
				alert("Server limit error!");
				lock_btn.value = "Lock";
				story_lock = 0;
			} else {
				setTimeout("update_server_lock();", story_lock*1000);
				
				if(countDownTimeout == null) {
					lock_btn.value = "Lock ("+story_lock+")";
					countDownTimeout = setTimeout("countDownLock();", 1000);
				}
			}
		}
		else
		{
			alert("Server response error! The story could not be locked.");
			lock_btn.value = "Lock";
			story_lock = 0;
		}
	}
}

/*onunload = function() {
 update_server_lock();
}*/
function reportDialog(storyId, partId) {
	var reportText = window.prompt("Please give a short detailed description of the issue.", "");
	
	if (reportText != null) {
		if (reportText.length < 3) {
			return false;
		}
		xmlHttp=GetXmlHttpObject();
		xmlHttp.onreadystatechange=reportStateChange;
		
		xmlHttp.open("GET","/report/"+storyId+"-"+partId+"-"+reportText.toLowerCase().replace(/[^a-z 0-9]+/g,'').replace(/[ ]+/g,'-')+".html",true);
		
		xmlHttp.send(null);
	}
	return false;
}

function reportStateChange() {
if (xmlHttp.readyState==4) { 
		if (xmlHttp.status == 200) {
			alert("Thank you for taking your time to fill in this report. The report will be handled as soon as possible.");
		} else {
			alert("Server error! The report could not be made.");
			document.write(xmlHttp.responseText);
		}
	}
}
window.onload = function () {
	var el = document.getElementById('email');
	if (el != null) {
		el.focus()
	}
}

