/********************
 *
 *	JAVASCRIPT_CUSTOMLIST.JS
 *	Custom-list callbacks javascript for ITC
 * 
********************/

// Custom 'add to list' function for the model number
function customList_AddItem_RecommendedItem() {
	SelectObj = document.ITCItemsForm.RecommendedItem;
	ElmntObj  = SelectObj[SelectObj.selectedIndex];
	return ElmntObj.text + '&ListItemID=' + ElmntObj.value;
}


// Custom 'add to list' function for the weblink
function customList_AddItem_WebLink() {
	with (document) {
		ObjWebLink = getElementById('WebLink_ID');
		ObjWebLinkText = getElementById('WebLinkText_ID');
	}
	
	if (!ObjWebLink.value) {
		alert('You did not enter a weblink');
		return;
	}
	
	ReturnOutput = ObjWebLink.value + '|' + ObjWebLinkText.value;
	
	ObjWebLink.value     = '';
	ObjWebLinkText.value = '';
	
	return ReturnOutput;
}


// Custom 'add to list' function for the weblink
function customList_AddItem_AttachedFile() {
	
	AttachedFileField = document.getElementById('AttachedFile_ID');
	if (!AttachedFileField.value) {
		alert('You did not select a file');
		return;
	}
	
	generateWaitDiv('Uploading File');
	
	FormObj = document.forms[0];
	
	OldAction  = FormObj.action;
	OldMethod  = FormObj.method;
	OldTarget  = FormObj.target;
	OldEncType = FormObj.getAttribute('enctype');
	
	FormObj.action = getScriptName() + '?CustomListAction=AttachedFile&NewValue=' +
	document.getElementById('AttachedFileTmpDir_ID').value;
	
	FormObj.target = 'AttachtedFileProcessingFrame';
	FormObj.method = 'POST';
	FormObj.setAttribute('enctype', 'multipart/form-data');
	
	FormObj.submit();
	
	FormObj.action = OldAction;
	FormObj.method = OldMethod;
	FormObj.target = OldTarget;
	FormObj.setAttribute('enctype', OldEncType);
	
	return null;
}


// Rename an attached file
function renameAttachedFile(ListItemID) {
	custom_prompt('<b>Enter a new name for the file</b><br><i>(invalid characters will be removed when saving this item)</i>', '',
	"with (document) { getElementById('AttachedFileName_" + ListItemID + "').value = NewAnswer; getElementById('" +
	"AttachedFilenameText_" + ListItemID + "').innerHTML = NewAnswer; } ");
}


// Rename an attached file
function renameCategory(ListItemID) {
	custom_prompt('<b>Enter a new name for the category</b>', '',
	"with (document) { getElementById('CategoryName_" + ListItemID + "_ID').value = NewAnswer; getElementById('" +
	"CategoryNameText_" + ListItemID + "_ID').innerHTML = NewAnswer; } ");
}

// Runs the upload process for a category image
function uploadCategoryImage(ListItemID) {
	
	generateWaitDiv('Uploading Image');
	
	with (document.forms[0]) {
		OldTarget = target;
		OldAction = action;
		
		action = 'form_items.php?UploadCategoryImage&ListItemID=' + ListItemID;
		target = 'CategoryImageProcessingIframe';
		submit();
		
		target = OldTarget;
		action = OldAction;
	}
}


// Toggles the stock status of a serial number
function setSerialNumberStockStatus(ListItemID) {
	StatusField = document.getElementById('StockStatus_' + ListItemID + '_ID');
	IconImg     = document.getElementById('StockStatsIcon_' + ListItemID);
	
	if (StatusField.value == 1) {
		StatusField.value  = '0';
		with (IconImg) {
			src   = IconImg.src.replace('_closed.gif', '_open.gif');
			title = 'Currently marked as OUT.  Click to mark as IN';
		}
	}
	
	else {
		StatusField.value  = '1';
		with (IconImg) {
			src   = IconImg.src.replace('_open.gif', '_closed.gif');
			title = 'Currently marked as IN.  Click to mark as OUT';
		}
	}		
}


// Deletes a category image
function deleteCategoryImage(ListItemID) {
	with (document.getElementById('CategoryImageDisplay_' + ListItemID)) {
		src    = JS_HTTPROOT + '/images/please_upload_image_25x25.gif';
		title  = 'No image is currently uploaded for this category';
		cursor = 'normal';
	}
	
	document.getElementById('DeleteCategoryImage_' + ListItemID + '_ID').value = '1';
}
