var dpProps=new Array();

/*************** Initiation & Visualiasation ***************/

function dpInit(dpDivId, page, paragraph)
{
	dpProps[dpDivId] = new Object();
	dpProps[dpDivId].page = page;
	dpProps[dpDivId].paragraph = paragraph;
	
	dpProps[dpDivId].state = "VIEW";

	GetAsync(dpDivId);
}



/*************** Ajax methods ***************/

function GetAsync(dpDivId)
{
	var curDp = dpProps[dpDivId];

	if(curDp.paragraph == 1) {
		httpObjectDP1 = getHTTPObject();
		if (httpObjectDP1 != null) {
			httpObjectDP1.open("GET", "ajax.php?type=dp&method=GET&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph, true);
			httpObjectDP1.send(null);
			httpObjectDP1.onreadystatechange = CatchAsync1;
		}
	} else if (curDp.paragraph == 2) {
		httpObjectDP2 = getHTTPObject();
		if (httpObjectDP2 != null) {
			httpObjectDP2.open("GET", "ajax.php?type=dp&method=GET&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph, true);
			httpObjectDP2.send(null);
			httpObjectDP2.onreadystatechange = CatchAsync2;
		}
	} else if (curDp.paragraph == 3) {
		httpObjectDP3 = getHTTPObject();
		if (httpObjectDP3 != null) {
			httpObjectDP3.open("GET", "ajax.php?type=dp&method=GET&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph, true);
			httpObjectDP3.send(null);
			httpObjectDP3.onreadystatechange = CatchAsync3;
		}
	}  else if (curDp.paragraph == 4) {
		httpObjectDP4 = getHTTPObject();
		if (httpObjectDP4 != null) {
			httpObjectDP4.open("GET", "ajax.php?type=dp&method=GET&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph, true);
			httpObjectDP4.send(null);
			httpObjectDP4.onreadystatechange = CatchAsync4;
		}
	}  else if (curDp.paragraph == 5) {
		httpObjectDP5 = getHTTPObject();
		if (httpObjectDP5 != null) {
			httpObjectDP5.open("GET", "ajax.php?type=dp&method=GET&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph, true);
			httpObjectDP5.send(null);
			httpObjectDP5.onreadystatechange = CatchAsync5;
		}
	} 
}

function SaveAsync(dpDivId)
{
	var curDp = dpProps[dpDivId];
	var editText = document.getElementById(dpDivId+"TextArea").value;
	
	editText = editText.replace(/\n/g,"%0A"); 
	
	if(curDp.paragraph == 1) {
		httpObjectDP1 = getHTTPObject();
		if (httpObjectDP1 != null) {
			httpObjectDP1.open("GET", "ajax.php?type=dp&method=SAVE&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph+"&editText="+editText, true);
			httpObjectDP1.send(null);
			httpObjectDP1.onreadystatechange = CatchAsync1;
		}
	} else if (curDp.paragraph == 2) {
		httpObjectDP2= getHTTPObject();
		if (httpObjectDP2 != null) {
			httpObjectDP2.open("GET", "ajax.php?type=dp&method=SAVE&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph+"&editText="+editText, true);
			httpObjectDP2.send(null);
			httpObjectDP2.onreadystatechange = CatchAsync2;
		}
	} else if (curDp.paragraph == 3) {
		httpObjectDP3 = getHTTPObject();
		if (httpObjectDP3 != null) {
			httpObjectDP3.open("GET", "ajax.php?type=dp&method=SAVE&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph+"&editText="+editText, true);
			httpObjectDP3.send(null);
			httpObjectDP3.onreadystatechange = CatchAsync3;
		}
	} else if (curDp.paragraph == 4) {
		httpObjectDP4 = getHTTPObject();
		if (httpObjectDP4 != null) {
			httpObjectDP4.open("GET", "ajax.php?type=dp&method=SAVE&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph+"&editText="+editText, true);
			httpObjectDP4.send(null);
			httpObjectDP4.onreadystatechange = CatchAsync4;
		}
	} else if (curDp.paragraph == 5) {
		httpObjectDP5 = getHTTPObject();
		if (httpObjectDP5 != null) {
			httpObjectDP5.open("GET", "ajax.php?type=dp&method=SAVE&id="+dpDivId+"&page="+curDp.page+"&paragraph="+curDp.paragraph+"&editText="+editText, true);
			httpObjectDP5.send(null);
			httpObjectDP5.onreadystatechange = CatchAsync5;
		}
	} 
}

function CatchAsync1() {
	if(httpObjectDP1.readyState == 4){
		var response = httpObjectDP1.responseText.split("~");
		CatchAsync(response);
	}
}
function CatchAsync2() {
	if(httpObjectDP2.readyState == 4){
		var response = httpObjectDP2.responseText.split("~");
		CatchAsync(response);
	}
}
function CatchAsync3() {
	if(httpObjectDP3.readyState == 4){
		var response = httpObjectDP3.responseText.split("~");
		CatchAsync(response);
	}
}
function CatchAsync4() {
	if(httpObjectDP4.readyState == 4){
		var response = httpObjectDP4.responseText.split("~");
		CatchAsync(response);
	}
}
function CatchAsync5() {
	if(httpObjectDP5.readyState == 4){
		var response = httpObjectDP5.responseText.split("~");
		CatchAsync(response);
	}
}

function CatchAsync(response)
{
	//if(httpObject['x'].readyState == 4){
	//	var response = httpObject['x'].responseText.split("~");
		var dpDivId = response[0];
		var dpText = response[1];

		dpDiv = document.getElementById(dpDivId);
		switch(dpProps[dpDivId].state)
		{
		 case "VIEW":
		 	SetButton(dpDivId+'But2', '');
			SetButton(dpDivId+'But1', 'bewerken');
			dpDiv.innerHTML = nl2br(dpText);
			break;
		 case "SAVE":
		 	if(dpText) {
				dpProps[dpDivId].state = "VIEW";
				GetAsync(dpDivId);
			}
			else {
				dpProps[dpDivId].state = "EDIT";
				alert("Er is een fout opgetreden.\n\nDe tekst kon niet worden opgeslagen.\n\n"+dpText);
			}
			break;
		 case "EDIT":
			SetButton(dpDivId+'But2', 'opslaan');
			SetButton(dpDivId+'But1', 'annuleren');
			dpDiv.innerHTML = '<textarea cols="98" rows="10" id="'+dpDivId+'TextArea">'+dpText+'</textarea>';
			break;
		}
	//}
}


/*************** Html manipulation ***************/

function SetDivStyle(dpDivId, property, value)
{
	if(value != undefined) {
		var dpDiv = document.getElementById(dpDivId);
		dpDiv.style[property] = value;
	}
}

function SetDiv(dpDivId, property, value)
{
	var dpDiv = document.getElementById(dpDivId);
	dpDiv[property] = value;
}

function SetButton(buttonId, text) {
	var button = document.getElementById(buttonId);
	
	if(button) {
		if(text == '') {
			SetDivStyle(buttonId, 'background', '#eeeeee'); 
		}
		else {
			SetDivStyle(buttonId, 'background', '#e0e0e0'); 
		}
		button.innerHTML = '<a href="#void">'+text+'</a>';
	}
}



/*************** Button events ***************/

function buttonMouseOver(dpDivId, color, name, time) 
{
	if(dpProps[dpDivId].state == "VIEW")
	{
		SetDivStyle(dpDivId, 'background', color);
		SetDivStyle('Info_'+dpDivId, 'background', color);
		SetDiv('Info_'+dpDivId, 'innerHTML', '<i>Laatst bewerkt door '+name+' ('+time+')</i>');
	}
}

function buttonMouseOut(dpDivId, color) 
{
	SetDivStyle(dpDivId, 'background', color);
	SetDivStyle('Info_'+dpDivId, 'background', color);
	SetDiv('Info_'+dpDivId, 'innerHTML', '');
}

function buttonClick(dpDivId, butId, mouseOutColor)
{
	buttonMouseOut(dpDivId, mouseOutColor) ;
	var curDp = dpProps[dpDivId];

	switch(curDp.state)
	{
	 case "VIEW":
		if(butId == "But1") { // EDIT
			curDp.state = "EDIT";
			GetAsync(dpDivId);
		}
		break;
	 case "EDIT":
		switch(butId)
		{
		 case "But1": // Annuleren
			curDp.state = "VIEW";
			GetAsync(dpDivId);
			break;
		 case "But2": // Save
			SaveAsync(dpDivId);
			curDp.state = "SAVE";
			break;
		}
		break;
	}
}
