 (function () {

 NC.widget.CandidateManager = function() {
	/* private variables and methods */
	var forwardResumeDialog = null;
	var emailCandidateDialog = null;
	var inviteDialog = null;
  	
 	 	return {
			showForwardResumePopup: function ( event, resumeId, jobId) {
				var pars = 'ResumeId=' + resumeId;
				pars += '&JobId=' + jobId;
				pars = encodeURI( pars);
				this.dialog = new NC.widget.ValidatedModalDialog(
					'forwardResumeForm.php', 
					pars, 
					NC.util.AjaxDialogDisplay.DialogWrapper, 
					'JsForwardResumeForm',
			 		function( updateUrl, updateParms, updateTarget) // submission function
			 		{
						var callback =
						{
			  				success:function( o) {
			  				},
			  				failure:function( o) {
			  				},
							scope: this    				
						};
						updateUrl += '?' + updateParms;
						var connection = new NC.util.AjaxConnection( 'GET', updateUrl, null);
						connection.executeMethod( callback);
			 			/*
						var update = new Ajax.Request(
							updateUrl, 
							{
								method: 'get', 
								parameters: updateParms, 
								onComplete: 
									function showResponse(originalRequest) {
									}
							}
						);
						* */
			 		}, 
			 		'do_forwardResume.php', // update url
			 		'', // update target
			 		function( form, errorDiv) // validation function
			 		{
			 			var rVal = false;
						var controls = form.elements;
						for (var ci = 0; ci < controls.length; ci++) {
							if (controls[ci].name == 'ForwardEmail') {
								rVal = NC.util.Dom.checkEmail(NC.util.Dom.getValue(controls[ci]));
							}
						}
						if (!rVal) {
							var element = document.getElementById(errorDiv);
							element.innerHTML = 'Please enter a valid email address';
							element.style.display = 'block';
						}
						return rVal;	
			 		},
			 		'ForwardResumeError',
			 		true
				);	
				forwardResumeDialog = this.dialog;
		 		// position the dialog
		 		var widthX = 375;
		 		var heightY = 250;
		 		var leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
		 		var topY = (NC.util.Dom.getViewportHeight() - heightY)/2;

				this.dialog.show( leftX, topY, widthX);	
//				this.dialog.show( event.clientX, Math.max(20, event.clientY - 150), 375);		
			},
			
	  	 	getForwardResumeDialog: function() {
	  	 		return forwardResumeDialog;
	  	 	},
	  	 	
			showEmailCandidatePopup: function ( event, resumeId, jobId)
			{
				var pars = 'ResumeId=' + resumeId;
				pars += '&JobId=' + jobId;
				pars = encodeURI( pars);
				this.dialog = new NC.widget.ValidatedModalDialog(
					'emailCandidateForm.php', 
					pars, 
					NC.util.AjaxDialogDisplay.DialogWrapper, 
					'JsEmailCandidateForm',
			 		function( updateUrl, updateParms, updateTarget) // submission function
			 		{
						var callback =
						{
			  				success:function( o) {
			  				},
			  				failure:function( o) {
			  				},
							scope: this    				
						};
						updateUrl += '?' + updateParms;
						var connection = new NC.util.AjaxConnection( 'GET', updateUrl, null);
						connection.executeMethod( callback);
			 		}, 
			 		'do_emailCandidate.php', // update url
			 		'', // update target
			 		function( form, errorDiv) // validation function
			 		{
			 			var rVal = false;
						var controls = form.elements;
						for (var ci = 0; ci < controls.length; ci++) {
							if (controls[ci].name == 'CandidateEmail') {
								rVal = NC.util.Dom.checkEmail(NC.util.Dom.getValue(controls[ci]));
							}
						}
						if (!rVal) {
							var element = document.getElementById(errorDiv);
							element.innerHTML = 'Please enter a valid email address';
							element.style.display = 'block';
						}
						return rVal;	
			 		},
			 		'EmailCandidateError',
			 		true
				);	
				emailCandidateDialog = this.dialog;
		 		// position the dialog
		 		var widthX = 550;
		 		var heightY = 400;
		 		var leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
		 		var topY = (NC.util.Dom.getViewportHeight() - heightY)/2;

				this.dialog.show( leftX, topY, widthX);	

//				this.dialog.show( event.clientX, Math.max(20, event.clientY - 150), 550);		
			},
			
	  	 	getEmailCandidateDialog: function() {
	  	 		return emailCandidateDialog;
	  	 	},

			showInvitePopup: function ( formSource, formName, targetSource, jobId, listHandler, purpose, singleResumeId) {
				var numCandidates = 0;
				if (listHandler != null) {
					numCandidates = listHandler.countCheckedBoxes();
				}
				else if (singleResumeId > 0) {
					numCandidates = 1;
				}
				if ((listHandler != null && numCandidates > 0) || listHandler == null) {
					var pars = '';
					if (jobId != null && jobId.length > 0) {
						pars += 'JobId=' + jobId;
					}
					if (pars.length > 0) {
						pars += '&';
					}
					pars += 'Purpose=' + purpose;
					pars += '&NumCandidates=' + numCandidates;
					if (listHandler != null) {
						pars += '&Candidates=' + listHandler.listCheckedBoxes();
					}
					else if (singleResumeId > 0){
						pars += '&Candidates=' + singleResumeId;
					}
			//		alert( pars);
					pars = encodeURI( pars);
					this.dialog = new NC.widget.ValidatedModalDialog(
						formSource + '?', 
						pars, 
						NC.util.AjaxDialogDisplay.DialogWrapper, 
						formName,
				 		function( updateUrl, updateParms, updateTarget) // submission function
				 		{
							var callback =
							{
				  				success:function( o) {
				  				},
				  				failure:function( o) {
				  				},
								scope: this    				
							};
							updateUrl += '?' + updateParms;
							var connection = new NC.util.AjaxConnection( 'GET', updateUrl, null);
							connection.executeMethod( callback);
							var callback =
							{
								ok: function(){ }
				  			};
							NC.widget.MessageManager.showDialog("invitationsent.php", NC.util.AjaxDialogDisplay.DialogWrapper, callback, true, -1);
				 		}, 
				 		targetSource, // update url
				 		'', // update target
				 		function( form, errorDiv) // validation function
				 		{
				 			var rVal = true;
				 			var errorMessage = '';
							var controls = form.elements;
							for (var ci = 0; ci < controls.length; ci++) {
									if (controls[ci].name == 'Message') {
										if (NC.util.Dom.getValue(controls[ci]).length <= 0) {
											rVal = false;
											if (errorMessage.length > 0) {
												errorMessage += '<br>';
											}
											errorMessage += 'Please enter a message';
										}
									}
									if (controls[ci].name == 'Subject') {
										if (NC.util.Dom.getValue(controls[ci]).length <= 0) {
											rVal = false;
											if (errorMessage.length > 0) {
												errorMessage += '<br>';
											}
											errorMessage += 'Please enter a subject';
										}
									}
							}
							if (!rVal) {
								var element = document.getElementById(errorDiv);
								element.innerHTML = errorMessage;
								element.style.display = 'block';
							}
							return rVal;	
				 		},
				 		'InviteError',
				 		true
					);	
					inviteDialog = this.dialog;
			 		// position thie dialog
			 		var widthX = 400;
			 		var heightY = 250;
			 		var leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
			 		var topY = (NC.util.Dom.getViewportHeight() - heightY)/2;
					
					this.dialog.show( leftX, topY, widthX);	
				}
				else {
					alert( "Please select at least one candidate to invite.")
				}
			},
			
			excludeCheckedCandidates: function() {
				var url = 'do_excludeCandidates.php?';
				url += '&Candidates=' + listHandler.listCheckedBoxes();
				var callback =
				{
	  				success:function( o) {
				      	NC.util.AjaxManager.showCurrentMainFrameContent();
	  				},
	  				failure:function( o) {
	  				},
					scope: this    				
				};
				var connection = new NC.util.AjaxConnection( 'GET', url, null);
				connection.executeMethod( callback);
			},
			
			getInviteDialog: function() {
				return inviteDialog;
			}
  	 	}
  }	
();

})(); 	
 

