
//========================================
//
// Project Invoice Upload/Download
//

// Add Project Invoice File
function addProjectInvoiceFile() {
	var filename = window.document.addInvoice.uploadedfile.value;
	var ext = getExt(filename);
	if(ext == "pdf") {
			
		// show the mmWait box ("Uploading")
		//
		// This assumes that the reloading of the page after the 
		// upload finishes (successful or not) will result in
		// the box going away.
		displayMMwait('30d_uploading',true);
		
		// submit
		window.document.addInvoice.action = "/site/invoiceUploader.php";
		window.document.addInvoice.submit();
	} else {
		alert("Upload available for PDF files only.");
	}
}

// Get Filename's Extension
function getExt(filename) {
	var dot_pos = filename.lastIndexOf(".");
	if(dot_pos == -1) {
		return "";
	}
	return filename.substr(dot_pos+1).toLowerCase();
}


// Remove Project Invoice File
function removeProjectInvoiceFile() {
	var answer = confirm("Are you sure you wish to remove this file?");	
	if(answer) {
		window.document.removeInvoice.action = "/site/invoiceUploadRemover.php";
		window.document.removeInvoice.submit();
	}
}

// Download Project Invoice File
function downloadProjectInvoiceFile(projectID, invoiceFile) {
	var myForm = addFormToWindow(window,'invoiceDownloaderHiddenForm');
	myForm.action = "/site/invoiceDownloader.php?id=" + projectID + "&fn=" + invoiceFile;
	myForm.submit();
	
}




//========================================
//
// Attachment File Upload/Download
//


// Add Project Attachment File
function addProjectAttachmentFile() {
	var filename = window.document.attachments.uploadedfile.value;
	var ext = getExt(filename);
	if(	(ext == "pdf") || 
		(ext == "jpg") || (ext == "gif") || (ext == "png") || 
		(ext == "doc") || (ext == "docx") ||
		(ext == "xls") || (ext == "xlsx")) {
		
		
		// show the mmWait box ("Uploading")
		//
		// This assumes that the reloading of the page after the 
		// upload finishes (successful or not) will result in
		// the box going away.
		displayMMwait('30d_uploading',true);
		
		window.document.attachments.action = "/site/projects/attachmentUploader.php";
		window.document.attachments.submit();
	} else {
		alert("Upload available for PDF, DOC, DOCX, XLS, XLSX, JPG, GIF, and PNG files only.");
	}
}

// Get Filename's Extension
function getExt(filename) {
	var dot_pos = filename.lastIndexOf(".");
	if(dot_pos == -1) {
		return "";
	}
	return filename.substr(dot_pos+1).toLowerCase();
}


// Remove Project Attachment File
function removeProjectAttachmentFile(filename) {
	var answer = confirm("Are you sure you wish to permanently remove\n" + filename + "?");	
	if(answer) {
		window.document.attachments.fileToDelete.value = filename;
		window.document.attachments.action = "/site/projects/attachmentUploadRemover.php";
		window.document.attachments.submit();
	}
}

// Download Project Attachment File
function downloadProjectAttachmentFile(projectID, attachmentFile) {
	
	var myForms = window.document.getElementsByName('attachments');
	if(myForms.length == 0) {
		myForms = window.document.getElementsByName('reportSelect');	
	}
	var myForm = myForms[0];
	var ext = getExt(attachmentFile);
	
	addPostToForm(myForm,'fileToDownload',attachmentFile);
	
//	window.document.attachments.fileToDownload.value = attachmentFile;
	if(ext == "pdf") {
		myForm.action = "/site/projects/attachmentDownloader.php?id=" + projectID;
		myForm.target = "_top";
	} else {
		myForm.action = "/site/attachmentUploads/contractors/general/" + projectID + "/" + attachmentFile;
		myForm.target = "_blank";
	}
	myForm.method = "post";
	myForm.submit();
}



