
var formInputElements =  new Array();

function about() {
	alert("Copyright\n Enrico Bravi\n&\nHarm van den Dorpel");
}

function logOut() {
	location.href="/admin/";
}

function addLayer() {
	
}

function deletePublication(id) {
	if (confirm("Are you sure you want to delete this publication?")) {
		deletePublicationAJAX(id);
	}
}

function cancelEditPublication() {
	if (confirm("Are you sure you want to close this window, your possible changes will not be saved.")) {
		location.href="publications.asp";
	}
}

function deletePublicationAJAX(id){

	ajaxUpdate = new sack("deletepublication.asp");
	ajaxUpdate.method = "POST";
	ajaxUpdate.onCompletion = function() { 
		location.href = "publications.asp";
	}

	ajaxUpdate.setVar("id", id);

	ajaxUpdate.runAJAX();
	
	//location.href="deletelayer.asp?layerid=" + layerID;
}

function addPublication() {
/*	var pubUL = $("publications");
	
	
	var newLI = document.createElement("LI");
	newLI.innerHTML = "<a class='normal' id='newPublication' href='javascript:showPublicationDetails();'>Untitled publication</a>";	

	pubUL.appendChild(newLI);*/
	showPublicationDetails(null,"newPublication");
}

	
function resetForm() {
	for (var i =0;i < formInputElements.length;i++) {
		$(formInputElements[i]).value = "";
	}
}

function setActivePanel(id) {
	$("panelUpload").style.display = "none";
	$("panelChooseFile").style.display = "none";
	$(id).style.display = "inline";
}

function replaceLayerDB(filename, pictureID, w, h) {
	ajaxUpdate = new sack("updatepicture.asp");
	ajaxUpdate.method = "POST";

	ajaxUpdate.onCompletion = function() { 
		alert(ajaxUpdate.response);
		location.href = location.href+"&random=" + Math.round(Math.random()*10000);
	}
	
	ajaxUpdate.setVar("filename", filename);
	ajaxUpdate.setVar("w", w);
	ajaxUpdate.setVar("h", h);

	ajaxUpdate.setVar("pictureid", pictureID);

	ajaxUpdate.runAJAX();
}

function addLayerToDBAjax(filename, projectID, w, h) {
	ajaxUpdate = new sack("createlayer.asp");
	ajaxUpdate.method = "POST";
	ajaxUpdate.onCompletion = function() { 
		//alert("ajaxUpdate.onCompletion");
		alert(ajaxUpdate.response);
		location.href = location.href;//+"&random=" + Math.round(Math.random()*10000);
		
	}
	
	ajaxUpdate.setVar("filename", filename);
	ajaxUpdate.setVar("w", w);
	ajaxUpdate.setVar("h", h);
	ajaxUpdate.setVar("projectid", projectID);

	ajaxUpdate.runAJAX();
}


function handleAdminClickLayer() {
	var projectID = getProjectID();
	populateProperties(projectID);
	showHideEditPanel(true);
	
	$("reUploadFormPictureID").value = getCurrentPictureID();
	updateFormHiddenInputCurrentProjectID();
	editing = true;
}


function populateProperties(projectID) {
	//alert(projectID);
	var index = getMetadataProjectIndex(projectID);
	if (index != null) {
		var propertyArray = projectData[index][1];
	
		for (var i =0;i < propertyArray.length;i++) {
			populateProperty(propertyArray[i]);
		}
	}
	
}


function populateProperty(arr) {
	var inputName = "input_" + arr[1];
	$(inputName).value = replaceHTML(arr[4]);
}


function replaceHTML(s) {
	s = s.replace(/<br>/g, "\n");
	s = s.replace(/&quot;/g,"\"");
	return s;
}

function getProjectID() {
	for (var i =0; i < layerArr.length;i++) {
		if (layerArr[i][LAYER_CSSID] == dd.obj.name) {
			var projectID = layerArr[i][LAYER_PROJECTID]
			return projectID;
		}
	}
	
}

function getCurrentPictureID() {
	for (var i =0; i < layerArr.length;i++) {
		if (layerArr[i][LAYER_CSSID] == dd.obj.name) {
			var pictureID = layerArr[i][LAYER_ID]
			return pictureID;
		}
	}
	
}

function updateFormHiddenInputCurrentProjectID() {
	
	$("propertyFormProjectID").value = 
	$("adminFormProjectID").value = 
	getProjectID();
	
}




function deleteLayer(layerID, projectID) {
	if (confirm("Are you sure you want to delete this layer?")) {
		deleteLayerAJAXDB(layerID);
	}
}


function deleteLayerAJAXDB(layerID) {
	ajaxUpdate = new sack("deletelayer.asp");
	ajaxUpdate.method = "POST";
	ajaxUpdate.onCompletion = function() { 
		location.href = location.href;
	}

	ajaxUpdate.setVar("layerid", layerID);

	ajaxUpdate.runAJAX();
	
	//location.href="deletelayer.asp?layerid=" + layerID;
}

function onEnterPropertyEdit(sender) {
	var editPanel = $(sender);
	editPanel.style.display = "inline";
}

function showHideEditPanel(show) {
	var editPanel = $("adminProperties");
	if (show) {
		

		editPanel.style.display = "inline";
	} else {
		editPanel.style.display = "none";
		resetForm();
	}
}

function cancelEdit() {
	showHideEditPanel(false);
	editing = false;
}



// OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD OUD 
/*function addPropertiesButtons(div, currentProjectID) {
	var addButton = document.createElement("input");
	addButton.value = "Add property...";
	addButton.setAttribute("onClick", "addPropertyField(\"properties\","+currentProjectID+");");
	addButton.type = "button";
	div.appendChild(addButton);

	div.appendChild(document.createElement("br"));

}

function createEditModeProperty(div, descr, value, typeID, propertyID, projectID) {
	var	 editPanelID = "editPropertyID" + propertyID;
	var inputCSSID = "inputCSSID" + propertyID;
	var inputSelectCSSID = "inputSelect" + propertyID
	
	var selectBox = document.createElement("select");
	selectBox.style.width = "185px";
	selectBox.id = inputSelectCSSID;
	
	for (var i=0;i< propertyTypesArr.length;i++) {
		var optionElement = document.createElement("option");
		optionElement.text = propertyTypesArr[i][1];
		optionElement.value = propertyTypesArr[i][0];
		if (parseInt(typeID)==parseInt(propertyTypesArr[i][0])) {
			optionElement.setAttribute("selected","true");
		}
		selectBox.appendChild(optionElement);
	}
	div.appendChild(selectBox);
	selectBox.setAttribute("onChange","onEnterPropertyEdit('"+editPanelID+"');");

	
	if (propertyID == "") {
		propertyID  = -1;
	}
	
	div.appendChild(document.createElement("br"));
	//input text
	var inputBox = document.createElement("textarea")
	inputBox.id = inputCSSID;
	//inputBox.type = "text";
	inputBox.style.width = "180px";
	inputBox.style.heigh = "40px";

	inputBox.value = value;
	inputBox.setAttribute("onClick","onEnterPropertyEdit('"+editPanelID+"');");
	div.appendChild(inputBox);

	// div 
	var buttonContainer = document.createElement("div");
	buttonContainer.id = editPanelID;
		var saveButton = document.createElement("input");
		saveButton.type = "button";
		saveButton.value = "save";
		saveButton.setAttribute("onClick","updateProperty('"+ editPanelID +"', "+propertyID+", '" + inputCSSID+"', '"+inputSelectCSSID+"', " + projectID + ");");
		
		var cancelButton = document.createElement("input");
		cancelButton.type = "button";
		cancelButton.value = "cancel";
		cancelButton.setAttribute("onClick","hideEditPanel('"+ editPanelID +"');");

		var deleteButton = document.createElement("input");
		deleteButton.type = "button";
		deleteButton.value = "x";
		deleteButton.setAttribute("onClick", "deleteProperty('"+ editPanelID +"'," + propertyID + ");");


	buttonContainer.appendChild(saveButton);
	buttonContainer.appendChild(cancelButton);
	buttonContainer.appendChild(deleteButton);

	div.appendChild(buttonContainer);
	hideEditPanel(buttonContainer);
	div.appendChild(document.createElement("br"));
	div.appendChild(document.createElement("br"));


	/*result = "<select onClick=\"onEnterPropertyEdit('"+editPanelID+"');\" style='width:185px;' id='" + inputSelectCSSID+ "'>";
	for (var i=0;i< propertyTypesArr.length;i++) {
		if (!propertyTypesArr[i][2]) {
			if (parseInt(typeID)==parseInt(propertyTypesArr[i][0])) {
				var selected = "selected";
			} else {
				var selected = "";
			}
			result += "<option value='"+propertyTypesArr[i][0]+"' " + selected + ">"+propertyTypesArr[i][1]+"</option>";
		}
	}
	result += "</select>";

	if (propertyID == "") {
		propertyID  = -1;
	}
	result += "<br>"+
	
	"<input id='"+ inputCSSID +"' type='text'  style='width:185px;' onClick=\"onEnterPropertyEdit('"+editPanelID+"');\">"+
	"<div id='" + editPanelID + "' class='editProperty'>" +
		"<input type='button' value='save' onClick=\"updateProperty('"+ editPanelID +"', "+propertyID+", '" + inputCSSID+"', '"+inputSelectCSSID+"', " + projectID + ");\">" + 
		"<input type='button' value='cancel' onClick=\"hideEditPanel('"+ editPanelID +"');\">" + 
		"<input  type='button' value='x' onClick=\"deleteProperty('"+ editPanelID +"'," + propertyID + ");\">"+
	"</div>"+
	"<BR><BR>";
	//div.innerHTML = result;
}*/



/*
function addPropertyField(divName, projectID) {
	var propertyID = -1;
	var propertyDiv = document.createElement("div");	
	propertyDiv.setAttribute("class", "property");
	propertyDiv.id = "property" + propertyID;
	
	createEditModeProperty(propertyDiv,"","","",propertyID,projectID);
	$(divName).appendChild(propertyDiv);
}

function createLongInfoTextArea(div, propertyID, projectID,value) {
	var result = "";
	
	result = 
		"<form id='longTextForm'><textarea id='longTextTextArea' onClick='showLongTextButtons()'>"+value+"</textarea>" +
		"<div style='background-color:white;' id='longTextButtons'><input type='button' value='save' onClick='saveLongText(\"longTextTextArea\"," + propertyID + ","+projectID+");'>" +
		"<input type='button' onClick='cancelEditLongText();' value='cancel'><br>For newline use &lt;br&gt;</div></form>"
	;
	div.innerHTML = result;
}

function showLongTextButtons() {
	$("longTextButtons").style.display = "inline";
}

function cancelEditLongText() {
	$("longTextForm").reset();
	$("longTextButtons").style.display = "none";
}

function saveLongText(sender, propertyID, projectID) {
	var propertyTypeLongInfo = 11;
	var typeID = propertyTypeLongInfo;
	var editPanel = $(sender);

	ajaxUpdate = new sack("updateproperty.asp");
	ajaxUpdate.method = "POST";
	
	ajaxUpdate.onCompletion = function() { 
		loadProjectXML(true);
	}

	var value = $F(sender);
	
	ajaxUpdate.setVar("propertyid", propertyID);
	ajaxUpdate.setVar("value", 		value);
	ajaxUpdate.setVar("typeid", 	typeID);
	ajaxUpdate.setVar("projectid", 	projectID);

	ajaxUpdate.runAJAX(null);
}

function deleteProperty(sender, id) {
	if(confirm("Are you sure you want to delete this property?")) {
		var editPanel = $(sender);

		ajaxUpdate = new sack("deleteproperty.asp");
		ajaxUpdate.method = "POST";
		
		ajaxUpdate.onCompletion = function() { 
			hideEditPanel(sender)
			loadProjectXML(); //reload xml
			editPanel.parentNode.parentNode.removeChild(editPanel.parentNode);//remove property from dhtml
		}
		
		ajaxUpdate.setVar("propertyid", id);
		ajaxUpdate.runAJAX(null);
	}
}

function updateProperty(sender, ID, inputCSSID, selectTypeID, projectID) {

	var typeID = $F(selectTypeID);
	var editPanel = $(sender);

	ajaxUpdate = new sack("updateproperty.asp");
	ajaxUpdate.method = "POST";
	
	ajaxUpdate.onCompletion = function() { 
		hideEditPanel(sender)
		loadProjectXML(true); //reload xml
/*		var propertyID = parseInt(ajaxUpdate.response);
		alert(propertyID);*/
/*	}

	var value = $(inputCSSID).value;
	ajaxUpdate.setVar("propertyid", ID);
	ajaxUpdate.setVar("value", 		value);
	ajaxUpdate.setVar("typeid", 	typeID);
	ajaxUpdate.setVar("projectid", 	projectID);

	ajaxUpdate.runAJAX(null);

}

*/


