

/**********
Edit Description SECTION


var sample=document.getElementById('sample');
var el = document.createElement('textarea');
el.id="sample";
el.rows=20; //Number of rows
el.cols=20; //Number of columns
el.style.backgroundColor="#FFFFFF";
el.style.width="120px" ;
el.style.height="120px";
sample.appendChild(el);
 
**************/
 

//mouse events for picCaptionContainer div


function editCaption(picId) {

	//set the current tag for later editing
	var currentPicId = getNode("currentId");
	currentPicId.value = picId;
		
	var picCaption = getNode(picId + "picCaption");

	picCaption.parentNode.removeChild(picCaption);

	var newEditDiv = createEditCaptionDiv(picId);	
	//alert("here");	
	var picCaptionContainer = getNode(picId + "picCaptionContainer");
	//if there's an extra child here, kill it.

	picCaptionContainer.appendChild(newEditDiv);	
}

function createEditCaptionDiv(picId) {
	var newCell = document.createElement('Div');
	newCell.id=picId + "picCaptionEdit";
	
	var nodeTextArea = document.createElement('textarea');
	nodeTextArea.id=picId + "picCaptionText";
	nodeTextArea.rows=60; //Number of rows
	nodeTextArea.cols=20; //Number of columns
	nodeTextArea.style.backgroundColor="#FFFFFF";
	nodeTextArea.style.width="400px" ;
	nodeTextArea.style.height="120px";
	//set value of tags to that in hiddent text box
	var hiddenCaptionText = getNode(picId + "CaptionHidden");
	nodeTextArea.value = hiddenCaptionText.value;		
		
	//var fct = function() { saveTags(id);}
	//create save button
	var newButton = document.createElement("BUTTON");
	newButton.className="saveButton";
	var buttext = document.createTextNode('SAVE');
	newButton.onclick=function () {saveCaption(); };
	newButton.appendChild(buttext);	
	
	//create cancel button
	var cancelButton = document.createElement("BUTTON");
	cancelButton.className="cancelButton";
	var buttextCancel = document.createTextNode('Cancel');
	cancelButton.appendChild(buttextCancel);
	cancelButton.onclick=function () {cancelCaptionClick(); };	
		
	newCell.appendChild(nodeTextArea);
	newCell.appendChild(newButton);
	newCell.appendChild(cancelButton);
	return newCell;
}

//if cancel button clicked, go back to just displaying the caption
function cancelCaptionClick() {
	var currentPicId = getNode("currentId");
	picId = currentPicId.value;
	var captionHidden = getNode(picId + "CaptionHidden");
	
	returnToCaptionDisplay(picId, captionHidden.value);
}

function saveCaption() {	
	
	var currentId = getNode("currentId");
	picId = currentId.value;
	var picCaptionText = getNode(picId + "picCaptionText");
	caption = picCaptionText.value
	//set  in hidden text box
	var hiddenCaption = getNode(picId + "CaptionHidden");
	hiddenCaption.value = caption;
//alert("here");	
	saveCaptionToDB(picId, replaceSubstring(caption, " ", "%20"));
	
	returnToCaptionDisplay(picId, caption);
}

//saves the new tags to the database
function saveCaptionToDB(id, caption) {

	var url = "/Pictures/saveDescription.asp";
	var postData = "pid=" + id + "&c=" + caption;

	sendDataYahooDescription(url, postData);
}

//returns the display to just show the list of tags
function returnToCaptionDisplay(picId, caption) {

	var picCaptionEdit = getNode(picId + "picCaptionEdit");
	picCaptionEdit.parentNode.removeChild(picCaptionEdit);
	var picCaptionContainer = getNode(picId + "picCaptionContainer");
	
	//if  there's an extra child here, kill it
	if(picCaptionContainer.firstChild) {
		picCaptionContainer.removeChild(picCaptionContainer.firstChild);
	}
	
	var captionTextDiv = document.createElement("DIV");
	captionTextDiv.id = picId + "picCaption";	
	var captionTextNode = document.createTextNode(caption);

	captionTextDiv.appendChild(captionTextNode);
	picCaptionContainer.appendChild(captionTextDiv);	
}


/****
data handling section
***/

function sendDataYahooDescription(url, postData) {
	var handleSuccess = function(o){	
	//alert("success");
	 	//var div = getNode("errorMsg");

//div.innerHTML = "Transaction id: " + o.tId;   
//div.innerHTML += "HTTP status: " + o.status;   
//div.innerHTML += "Status code message: " + o.statusText;   
//div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";   
//div.innerHTML += "PHP response: " + o.responseText;   
//div.innerHTML += "Argument object: " + o.argument;  
	}
	var handleFailure = function(o){
	
 	var div = getNode("errorMsg");
//o.getResponseHeader[ ]  
//o.getAllResponseHeaders  
//o.responseText  
//o.responseXML  
//o.argument 
div.innerHTML = "Transaction id: " + o.tId;   
div.innerHTML += "HTTP status: " + o.status;   
div.innerHTML += "Status code message: " + o.statusText;   
div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";   
div.innerHTML += "PHP response: " + o.responseText;   
div.innerHTML += "Argument object: " + o.argument;  

		//alert("data was not saved. status = " + o.status + "; text=" + o.statusText + "; o.responseText=" + o.responseText);	
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure,
	  argument: ['foo','bar']
	}	
	
	//alert("url=" + url + "; postData=" + postData);
var request = YAHOO.util.Connect.asyncRequest('POST', url , callback, postData);
}


