// JavaScript Document
function ResizeTextArea(txtBox)
{
	nCols = txtBox.cols;
	sVal = txtBox.value;
	nVal = sVal.length;
	nRowCnt = 1;
	
	var lines = sVal.split("\n").length;
	txtBox.rows = lines;
	var special = 0;
	for (i=0;i<nVal;i++)
	{ 
		special++;
		if(escape( sVal.charAt( i+3 ) ) == "%0A" || escape( sVal.charAt( i+3 ) ) == "%20" )
		{
			special = 0;
		}
		
		if(special >= 45)
		{
			txtBox.value = txtBox.value.substr(0, i) + "\n" + txtBox.value.substr(i-1);  
			special = 0;
			//nRowCnt = nRowCnt + 1;
		}
		if(escape( sVal.charAt( i ) ) == "%0A")
		{
			nRowCnt +=1; 
			special = 0;
		}
		
	
	}
	
	if (nRowCnt < (nVal / nCols) )
	{ 
		var sven = 0;
		nRowCnt = 1 + (nVal / nCols);
	}
	txtBox.rows = nRowCnt;
	if( txtBox.rows < 3 )
		txtBox.rows = 3;
	if(txtBox.scrollHeight > txtBox.offsetHeight)
	{
		var test = ( ( txtBox.scrollHeight-txtBox.offsetHeight )/16 );
		txtBox.rows = txtBox.rows +Math.round(test);
	}
	
	if( txtBox.rows >=2)
	txtBox.style.height = "auto";

}

// JavaScript Document
var xmlHttp;

function createXMLHttpRequest() 
{ 
   if(window.ActiveXObject) 
   { 
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
   } 
   else if(window.XMLHttpRequest) 
   { 
	  xmlHttp = new XMLHttpRequest(); 
   } 
} 

function randomInt ( )
{
  rand = Math.floor(Math.random()*993287432);
}

function sendArtistComment( )
{
	document.artistComForm.button.disabled=true;
	document.artistComForm.wakkawakka.disabled=true;
	document.artistComForm.wiewie.disabled=true;
	document.artistComForm.wahawaha.disabled=true;
	
	name = document.artistComForm.wakkawakka.value;
	email = document.artistComForm.wiewie.value;
	comment = document.artistComForm.wahawaha.value;
	aID = document.artistComForm.aID.value;
	
	randomInt();
	createXMLHttpRequest(); 
	xmlHttp.onreadystatechange = handleStateChange; 
	xmlHttp.open("GET", "storeComments.php?rand="+rand+"&aID="+escape(aID)+"&name="+escape(name)+"&email="+escape(email)+"&comment="+escape(comment), true);
	xmlHttp.send(null);
}
function getCommentPage(page, aID )
{	
	randomInt();
	createXMLHttpRequest(); 
	xmlHttp.onreadystatechange = handlePageChange; 
	xmlHttp.open("GET", "sv/artister.php?rand="+rand+"&aID="+escape(aID)+"&page="+escape(page), true);
	xmlHttp.send(null);
}
	
	
	
function handlePageChange()
{
	if(xmlHttp.readyState == 4) 
	{ 
		var response = xmlHttp.responseText; 
		var update = new Array(); 
		if(response.indexOf('||' != -1)) 
		{ 
			update = response.split('||');
			document.getElementById("commentPage").innerHTML = update[0];
		}
	}
}

function login()
{
	username = document.getElementById("uUsername").value;
	password = document.getElementById("uPassword").value;
	//randomInt();
	//createXMLHttpRequest(); 
	//xmlHttp.onreadystatechange = handlePageChange; 
	//xmlHttp.open("GET", "login.php?rand="+rand+"&user="+escape(user)+"&pwd="+escape(password), true);
	//xmlHttp.send(null);
	
}


function handleStateChange()
{
	if(xmlHttp.readyState == 4) 
	{ 
		var response = xmlHttp.responseText; 
		var update = new Array(); 
		if(response.indexOf('||' != -1)) 
		{ 
			update = response.split('||');
			document.getElementById("artistComFormRespons").innerHTML = update[0];
			document.getElementById("artistComFormRespons").style.display = "block";
			if(update[0].indexOf("Fel:") != -1)
			{
				document.artistComForm.button.disabled=false;
				document.artistComForm.wakkawakka.disabled=false;
				document.artistComForm.wiewie.disabled=false;
				document.artistComForm.wahawaha.disabled=false;
			}
		}
	}
}

function initSearch()
{
	var string = document.getElementById("searchString").value;
	
	if( string == '')
	{
		alert("Fyll i minst ett sökord");
		return false;
	}
	
	var element = document.getElementById("middle");
	document.getElementById("middle").innerHTML = ''+
		'<div class="pergament">'+
			'<div class="pergament-top">Sökresultat</div>'+
			'<div class="pergament-middle">'+
				'<div class="pergament-content">'+
				'<div id="searchResultsBox"></div>'+
				'</div>'+
			'</div>'+
			'<div class="pergament-bottom" style="margin-bottom:20px;"></div>'+
		'</div>';
	
	// create new iframe
	var new_div = document.createElement('iframe');
	
	var new_iframe = document.createElement('iframe');
	new_iframe.src = "http://www.peaceandlove.nu/festival/sok.php?string="+escape(string);
	new_iframe.id = "iframe";
	new_iframe.style.display = 'none';
	element.appendChild(new_iframe);
}


function filterNews()
{
	var box = document.getElementById("month");
	var month = box.options[box.selectedIndex].value;
		
	location.href="/festival/nyheter/arkiv/" + month + "/";
}

function limitText(limitField, limitCount, limitNum) {
	limitCount = document.getElementById(limitCount);
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.innerHTML = limitField.value.length;
	}
}


