// Copyright (c) NimbleCat, 2008

/*
* this object implements a modal dialog
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("uploaddialog.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("uploaddialog.js requires the NC JavaScript framework");
 }
 
 /*
  * constructor
  */
  
  NC.widget.UploadDialog = function(
 		sourceUrl, // the source URL for the dialog content
 		sourceParameters, //any source parameters to use in getting the dialog
 		targetDiv, // the target div to show the dialog in
 		targetFormName, // the target dialog's form name
 		iFrameName,		// the name of the iFrame which will receive the results from the upload
 		progressDivName, // the name of a div to show a progress message in
 		progressMessage, // the progress message
 		onUploadCompleteFunction, // what to do when the upload completes
 		checkValidFunction, // check if the data is valid
 		errorDivName,		// ignored if blank or null
 		showAsPopup)		// if true show as a popup, else show in a pane		
  {
		var fnSuperClass = NC.widget.UploadDialog.superclass.constructor;
		fnSuperClass.call( this, sourceUrl, sourceParameters, targetDiv, targetFormName);
 		this.iFrameName = iFrameName;
 		this.progressDivName = progressDivName;
 		this.progressMessage = progressMessage;
 		this.onUploadCompleteFunction = onUploadCompleteFunction;
 		this.checkValidFunction = checkValidFunction;
 		this.errorDivName = errorDivName;
 		this.showAsPopup = showAsPopup;
  }
  
  NC.lang.extend(NC.widget.UploadDialog, NC.widget.ModalDialog, 
  {
 	doFileUpload: function( ) {
 		if (this.checkValidFunction == null ||
 			this.checkValidFunction() == true) {
	    	var form = document.forms[this.targetFormName];
	    	form.target = this.iFrameName;
			/*
			 * this is a hack to fix the problem of files that we can't read being uploaded.
			 * it needs to be extended and generalized
			 */
			resumeFile = document.getElementById("ResumeFileName");
			var validext = false;
			if (resumeFile != null) {
				var filename = NC.util.Dom.getValue(resumeFile);
				if (filename != null && filename.length > 0) {
					var dot = filename.lastIndexOf('.');
					var ext = filename.substring(dot + 1);
					if (ext == 'txt' ||
					ext == 'doc' ||
					ext == 'htm' ||
					ext == 'php') {
						validext = true;
					}
					else {
						validext = false;
					}
				}
				else {
					validext = true;
				}
			}
			else {
				validext = true;
			}
			if (validext) {
				form.submit();
				var progressDiv = document.getElementById(this.progressDivName);
				if (progressDiv != null) {
					progressDiv.innerHTML = this.progressMessage;
				}
				else {
				}
				this.hide();
			}
			else {
				NC.widget.ResumeUploadManager.showError('This file format is not supported. Please enter a text, html or Word document(.doc) file.');
			}
		}
	},
	
	onUploadComplete: function() {
		if (this.onUploadCompleteFunction != null) {
			this.onUploadCompleteFunction();
		}
	}
 });
  
