function toDateSelect(calendar, date) {
	selectDropDownItemWithValue('showForm','toMth',  '' + (  calendar.date.getMonth() + 1    )  );
	selectDropDownItemWithValue('showForm','toDay',  '' + (  calendar.date.getDate()         )  );
	selectDropDownItemWithValue('showForm','toYr',   '' + (  calendar.date.getFullYear()     )  );
	if (calendar.dateClicked) { 
		calendar.callCloseHandler();
	}
}

function fromDateSelect(calendar, date) {
	selectDropDownItemWithValue('showForm','fromMth',  '' + (  calendar.date.getMonth() + 1    )  );
	selectDropDownItemWithValue('showForm','fromDay',  '' + (  calendar.date.getDate()         )  );
	selectDropDownItemWithValue('showForm','fromYr',   '' + (  calendar.date.getFullYear()     )  );
	if (calendar.dateClicked) { 
		calendar.callCloseHandler();
	}
}

function selectDropDownItemWithValue(form, element, value) {
	if (value != null && value.length > 0) {
		for (i=0; i < document.forms[form].elements[element].options.length; i++) {
			if(document.forms[form].elements[element].options[i].value==value ) {
				document.forms[form].elements[element].selectedIndex=i;
			}
		}
	}
}

function updateFeaturedItem(node){

	var opts = {
			method: 'post', 
			parameters: 'documentCode=' + node.rel, 
			asynchronous: true, 
			evalScripts:true,
			onSuccess: updateFeaturedItemSucceeded, 
			onFailure: updateFeaturedItemFailed
	};
		
	var ajaxRequest = new Ajax.Request('/html/updateFeaturedItem.php', opts);
	
	return false;	
	
}

function updateFeaturedItemSucceeded(response){

	var documentCode;
	
	var node = $$('span.featured')[0];
	
	
	if( node != null ) {
		
		documentCode = node.id.substring("makeFeature_".length);
		
		var a  = new Element('a', {
			href: '#',
			rel: documentCode
		}).update("Show on Profile");	
		
		a.observe('click', 
			function (e) {			
				updateFeaturedItem(e.target);
				Event.stop(e);
			}
		);
		
		node.appendChild(a);
		node.appendChild(document.createTextNode(" | "));
		node.removeClassName("featured");
		
		var n = $( 'makeFeature_' + response.responseText );
		n.update();
		n.addClassName('featured');		
		
		
		
	} else {
		
		node = $$('span.featuredFolder')[0];
		
		if( node != null ) {
		
			documentCode = node.id.substring("makeFeature_".length);
			
			var a  = new Element('a', {
				href: '#',
				rel: documentCode
			});
			
			var img = new Element('img', {
					src: '/images/editBlank.gif',
					alt: 'Show on Profile',
					border: 0
			});
			a.appendChild(img); // <a href="#" rel="documentCode"><img src="/images/editBlank.gif" alt="Show on Profile" border="0" /></a>
			
			a.observe('click', 
				function (e) {			
					updateFeaturedItem(e.target.parentNode);
					Event.stop(e);
				}
			);
			
			node.update(); // clear the node
			node.appendChild(a);
			//node.appendChild(document.createTextNode(""));
			node.removeClassName("featuredFolder");
			
			
			
			
			
			
			var n = $( 'makeFeature_' + response.responseText );
			img = new Element('img', {
					src: '/images/editFeatured.gif',
					alt: 'Featured',
					border: 0
			})
			n.update(); // clear the node
			n.appendChild(img);
			n.addClassName('featuredFolder');
		
		
		}
	}



}

function updateFeaturedItemFailed(response){ }

function goDelete( documentcode, memberid) {
	
	if (true){
		var opts = {
				method: 'post', 
				parameters: 'documentCode=' + documentcode + '&memberId=' + memberid, 
				asynchronous: true, 
				evalScripts:true,
				onSuccess: deleteDataSucceeded, 
				onFailure: deleteDataFailed
		};
		var ajaxRequest = new Ajax.Request('/html/goDelete.php', opts);

	} else {}
	return false;
}

function deleteDataSucceeded(response){
	
	//alert(response.responseText);
	
	//alert('action_field_' + response.responseText);
	
	
	//Element.remove($('lbContent'));
	$('lbContent').remove(); 
	
	$('overlay').style.display = 'none';
	$('lightbox').style.display = 'none';

	if (browser == "Internet Explorer"){
	//	this.setScroll(0,this.yPos);
	//	this.prepareIE("auto", "auto");
	//	this.hideSelects("visible");
	}

	
	var n = $( 'action_field_' + response.responseText );
	
	$('action_field_' + response.responseText).remove(); 
	//Element.remove( $('action_field_' + response.responseText));
	
	var pid = n.parentNode;
	pid.className ='editCellRight';
	
	if ( 0 == $('myMovies').select('td.editCell').length && 0 == $('myMovies').select('td.editCellRight').length) {
		//Element.remove( $('myMovies'));
		$('myMovies').remove(); 
	}
	
	
}

function deleteDataFailed(response){
	alert( "Failed to Delete Data, sorry it didnt work out.");
}

function removeNodeWithId( id ) {
	var node = document.getElementById(id);
	if (null != node){
		node.parentNode.removeChild( node );	
	}
}


function removeImageRow(rowid) {
	var removerow = document.getElementById(rowid);
	removerow.parentNode.removeChild(removerow);
}

function addImageRow() {
	var parentTable = document.getElementById("imagesTable");
	var numberofrows = document.getElementById("imagecount").value;
	document.getElementById("imagecount").value = parseInt(numberofrows) + 1;
	
	/* TR */
	var newtr = document.createElement("TR");
	newtr.id = "imageRow" + numberofrows;
	newtr.className = "";
	parentTable.appendChild(newtr);
	
	/* TD */
	var newtd = document.createElement("TD");
	newtd.align = "left";
	newtr.appendChild(newtd);
	
	/* TD content */
	var newinput = document.createElement("INPUT");
	newinput.id = "images[" + numberofrows + "][file]";
	newinput.name = "images[" + numberofrows + "][file]";
	newinput.type = "file";
	newinput.className = "editBrowse";
	newtd.appendChild(newinput);
	
	var newspan = document.createElement("SPAN");
	newspan.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	newtd.appendChild(newspan);
	
	var newa = document.createElement("A");
	newa.href = "javascript:removeImageRow('imageRow" + numberofrows + "');";
	newa.innerHTML = '<img src="/images/minus.gif" alt="Remove image" border="0" width="25" height="23" align="absmiddle" />';
	newtd.appendChild(newa);
	
}

function addTagRow() {

	var inputValue = trim(document.getElementById("tag0").value);
	
	/* split the result by comma */
	var mySplitResult = inputValue.split(",");
	
	/* loop through each result one by one adding a row */
	for(i = 0; i < mySplitResult.length; i++) {
		
		mySplitResult[i] = trim(mySplitResult[i]);
		
		if(1 > mySplitResult[i].length )
			continue;
		
		var parentTable = document.getElementById("tagTable");
		var numberofrows = document.getElementById("tagcount").value;
		
		document.getElementById("tag0").value = "";
		document.getElementById('tag0').focus();
		document.getElementById("tagcount").value = parseInt(numberofrows) + 1;
		
		/* TR */
		var newtr = document.createElement("TR");
		newtr.id = "tagRow" + numberofrows;
		newtr.className = "";
		parentTable.appendChild(newtr);
		
		/* TD */
		var newtd = document.createElement("TD");
		newtd.align = "left";
		newtr.appendChild(newtd);
		
		/* TD content */
		var newinput = document.createElement("INPUT");
		newinput.type = "text";
		newinput.value = mySplitResult[i];
		newinput.disabled = "disabled";
		newinput.className = "editTagInput";   
		newtd.appendChild(newinput);
		
		var newinput = document.createElement("INPUT");
		newinput.id = "tag"+numberofrows;
		newinput.name = "tags[]";
		newinput.type = "hidden";
		newinput.value = mySplitResult[i];
		newtd.appendChild(newinput);
		
		var newspan = document.createElement("SPAN");
		newspan.innerHTML = "&nbsp;";
		newtd.appendChild(newspan);
		
		var newa = document.createElement("A");
		newa.href = "javascript:removeImageRow('tagRow" + numberofrows + "');";
		newa.innerHTML = '<img src="/images/minus.gif" alt="Remove tag" border="0" align="absmiddle" />';
		newtd.appendChild(newa);
	
	}
}

function removeTagRow(rowid) {
	var removerow = document.getElementById(rowid);
	removerow.parentNode.removeChild(removerow);
}

var clear = 0;
function clearTxt()  {  
	if(clear == 0 ) {
		document.getElementById('folder').value="";
		document.getElementById('folder').focus();
		clear=1;
	}
}
function txtBack()  {  
	if(clear == 1 && !document.getElementById('folder').value) {
		document.getElementById('folder').value="Collection name here...";
		clear=0;
	}
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
