cardEditDialog = function(plantID, userplantID, cardName) {
	jQuery('#indicator').addClass('loading');
	jQuery("#cardEditDialog").load("index.cfm?fuseaction=benchcards.ajaxEdit&plantID="+plantID+"&userplantID="+userplantID, function(r,s,x){
		ajaxOnComplete(r,s,x);
		jQuery('#indicator').removeClass('loading');
		jQuery('#cardEditDialog').dialog('option','title','Edit ' + cardName);
		jQuery("#cardEditDialog").dialog("open");
		setupButtons( 0 );
	});
	return false;	
}

bindFormClicks = function(){
	jQuery("#theform input").bind("focus", function(){ setupButtons( 1 ) });
	jQuery("#theform input[type=checkbox]").bind("click", function(){ setupButtons(1) });
	jQuery("#theform input[type=radio]").bind("click", function(){ setupButtons(1) });
	jQuery("#theform select").bind("focus", function(){ setupButtons(1) });
	jQuery("#theform textarea").bind("focus", function(){ setupButtons(1) });
}


setupButtons = function( isDirty, isInLibrary ) {
	var isInLibrary = isInLibrary || jQuery("#theform input[name=isactive]").val(); 
	var buttons = {};

	if ( isDirty==1 ) {
		buttons['Save Changes'] = function(){ saveChanges(0) };
	}	else {
		buttons['Finish & Close'] = function(){ jQuery('#cardEditDialog').dialog('close'); };
	}
	
	if ( isInLibrary==1 ) {
		buttons["Remove From Library"] = function(){ toggleActive() };
	} else {
		buttons["Add To My Library"] = function(){ toggleActive() };
	}
	
	buttons["Preview"] = function(){ saveChanges(1) };
	buttons["Download"] = function(){ saveChanges(0); document.location.href='/index.cfm?fuseaction=benchcards.download&oneoff=' + jQuery('#theform input[name=userplantID]').val() };
	buttons["Download Sample"] = function(){ saveChanges(0); document.location.href='/index.cfm?fuseaction=benchcards.samples&oneoff=' + jQuery('#theform input[name=userplantID]').val() };
	
	jQuery('#cardEditDialog').dialog('option','buttons',buttons);
	return;
}

closeCardEditDialog = function() {
	jQuery('#cardEditDialog').dialog('close');
	return false;
}


cardPreview = function(plantID, userplantID) {
	jQuery("#pleaseWait").dialog("open");
	jQuery("#pleaseWait").load("index.cfm?fuseaction=benchcards.thumbnail&plantID="+plantID+"&userplantID="+userplantID, ajaxOnComplete);	
}



tabTo = function(e) {
	if (tab != e) {
		if (jQuery('#theform input[name=dirty]').val()==1) {
			saveChanges(0);
		}
		Element.removeClassName($(tab+"Tab"),"current");
		Element.addClassName($(e+"Tab"),"current");
		new Effect.Fade(tab, {
			duration:0.15, 
			afterFinish:function() {
				new Effect.Appear(e, {duration:0.35, afterFinish:function() { Element.hide("indicator"); } } );
				tab = e;
				document.theform.tab.value = e;
			}
		});
	}
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////



toggleActive = function() {
	if (jQuery("#theform input[name=isactive]").val()==0) {
		jQuery("#theform input[name=isactive]").val(1);
		saveChanges(0);
		//setupButtons(0,1);
		jQuery("#flag_"+jQuery("#theform input[name=plantID]").val()).attr("src","image/bullet-inlibrary.gif");
	} else {
		jQuery("#theform input[name=isactive]").val(0);
		saveChanges(0);
		//setupButtons(0,0);
		jQuery("#flag_"+jQuery("#theform input[name=plantID]").val()).attr("src","image/bullet-inactiveinlibrary.gif");
	}
	
}


//////////////////////////////////////////////////////
// AJAX Asyncronous (background) saving to database
//////////////////////////////////////////////////////
saveChanges = function(preview) {
	
	preview = preview || 0; 

	// show the progress indicator
	jQuery("#indicator").show(); 		
	
	jQuery.post(
		'index.cfm', 
		jQuery('#theform').serialize(), 
		function(r,s,x) {
			ajaxOnComplete(r,s,x);
			jQuery("#indicator").hide(); 		
			if (preview) cardPreview(jQuery('#theform input[name=plantID]').val(),jQuery('#theform input[name=userplantID]').val());
		}
	);
	
	// turn the save button to Finish/Close state
	setupButtons( 0 );
	
	if (jQuery("#theform input[name=isactive]").val()==1) {
		jQuery("#flag_"+jQuery("#theform input[name=plantID]").val()).attr("src","image/bullet-inlibrary.gif");
	} else {
		jQuery("#flag_"+jQuery("#theform input[name=plantID]").val()).attr("src","image/bullet-inactiveinlibrary.gif");
	}

	return false;
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////


addNetContents = function(myVal) {
	if (myVal != "") {
		var newOpt = document.createElement("option");
		newOpt.text = myVal;
		newOpt.value = myVal;
		try {
			$('decNetContents').add(newOpt,$('decNetContents').options[$('decNetContents').length-1]); // standards compliant
	    }
	    catch(ex) {
			$('decNetContents').add(newOpt,$('decNetContents').length-1); // IE
	    }
		$('decNetContents').options[$('decNetContents').length-2].selected=true;
	}
	Element.hide('newDeclaration');
	Element.show('decNetContents');
}



