 (function () {

 NC.widget.ResumeWizard = function() {
	/* private variables and methods */
 	var wizard = null;
 	var domDocument = null;
 	var info = null;
 	
 	var sendResumeInfo =  function sendResumeInfo() {
 		alert( 'test');
 	}
 	
 	 	return {
			StrWizardFieldPrefix: "NCWizardField_", // prepended to all wizard edit fields
			PrivacyDefault: "0",   // default value for privacy
			NewRow: "1",		 // new row bit for row flags
			DeletedRow: "2",	 // deleted row bit for row  flags
			
			StrNoContent: "-",
 	 		/* these are string constants that refer to the resume update information schema. If they are changed there, 
 	 		 * they must be changed here
 	 		 */
			StrErrorcode: "errorcode",
			StrErrormessage: "errormessage",
			StrResumeinfo: "resumeinfo",
			StrResumeid: "resumeid",
			StrMd5id: "md5id",
			StrEmail: "email",
			StrZipcode: "zipcode",
			StrRandomzipcode: "randomzipcode",
			StrRandomcity: "randomcity",
			StrRandomstate: "randomstate",
			StrRecommendation: "recommendation",
			StrFirstname: "firstname",
			StrFullname: "fullname",
			StrJobinfo: "jobinfo",
			StrCategory: "category",
			StrCategoryid: "categoryid",
			StrCategoryweight: "categoryweight",
			StrCategoryname: "categoryname",
			StrTotaljobs: "totaljobs",
			StrTotaljobsforcategory: "totaljobsforcategory",
			StrTopcategorylabel: "topcategorylabel",
			StrStartdate: "startdate",
			StrEnddate: "enddate",
			StrCompanyandtitle: "companyandtitle",
			StrPrivacy: "privacy",
			StrFlags: "flags",
			StrWorkexperience: "workexperience",
			StrEducationexperience: "educationexperience",
			StrSchool: "school",
			StrSchoolType: "schooltype",
			StrSchoolGpa: "gpa",
			StrDegreecode: "degreecode",
			StrDegreeDate: "degreedate",
			StrModified: "modified",
			StrHomephone: "homephone",
			StrWorkphone: "workphone",
			StrMobilephone: "mobilephone",
			StrStreet: "street",
			StrCity: "city",
			StrState: "state",
			StrCountry: "country",
			StrAddress: "address",
			StrAddress2: "address2",
			StrAddress3: "address3",
			StrPhonenumbers: "phonenumbers",
			StrCategoryList: "categoryList",
			StrTopjobs: "topjobs",
			StrTotalJobAlerts : "totalJobAlerts",
			StrWorklist: "worklist",
			StrEducationlist: "educationlist",
			StrLastname: "lastname",
			StrMiddleinitial: "middleinitial", 	 		
			StrNamesuffix: "namesuffix",
			StrNameprefix: "nameprefix",
			StrNCScore: "ncscore",
			StrTopCategoryName : "topcategoryname",
			StrTopCategoryId : "topcategoryid",
			StrTopCategoryScore: "topcategoryscore",
			StrManagerCategory: "managercategory",
			StrManagerScore: "managerscore",
			StrPublicProfile: "publicprofile",
			MangerCategory_Other: 1,
			ManagerCategory_Manager: 2,
			ManagerCategory_Exec: 4,
			StrExplanation: "explanation",
			StrEmploymentCount: "employmentCount",
			StrAverageEmpDescLength: "averageEmpDescLength",
			StrEducationCount: "educationCount",
			StrResumeLength: "resumeLength",
			StrResumeTextLength: "resumeTextLength",			
			StrAddButton: "add",
			StrDeleteButton: "delete",
			// Page labels
			
			StrNCWizScorePage: "NCWizScorePage",
			StrNCWizWorkHistoryPage: "NCWizWorkHistoryPage",
			StrNCWizContactPage: "NCWizContactPage",
			StrNCWizEducationPage: "NCWizExperiencePage",
			
 		/* public methods */
			create: function( displayDivId, _domDocument, updateUrl, onFinish, onCancel, enableAllPageFinish) {
				domDocument = _domDocument;
				info = domDocument.getDocumentElement();
				var pages = new Array(3);
				pages[0] = new NC.widget.ContactInfoPage( domDocument);
				pages[1] = new NC.widget.WorkHistoryPage( domDocument);
				pages[2] = new NC.widget.EducationHistoryPage( domDocument);
				if (onFinish == null) {
					onFinish = this.updateResumeInfo;
				}
				wizard =  new NC.widget.Wizard (
					"Review Resume Information",
				 	 displayDivId,				// div into which wizard page content will be placed
				 	 pages,						// array of Wizard page objects
				 	 0,							// first page to display
				 	 onFinish,					// on finish
				 	 onCancel,					// on cancel
				 	 null,						// on update
//				 	 './updateResumeInfo.php' 	// update url
				 	 updateUrl,				 	// update url
				 	 enableAllPageFinish		// enable finish button on all pages
				 	 );					
			 	 
			 	 wizard.compose();
				 return wizard;
			},
			
			display: function() {
				wizard.init();
			},
			
			addPageToBeginning: function( page) {
				wizard.addPageToBeginning( page);
			},

			addPageToEnd: function( page) {
				wizard.addPageToEnd( page);
			},
			
			addValidationToPage: function( pagelabel, validationFn) {
			  	var page = wizard.getPageByLabel( pagelabel);
			  	page.setValidateFn( validationFn);
			},
			
			/*
			 * update information
			 */
			updateResumeInfo: function( onSuccess) {
				var encoded = encode64(info.toString());
				var encoded = encodeURIComponent( encoded);
//				var updateUrl = './updateResumeInfo.php';
				var postData = 'EncodedResumeInfo=' + encoded;

				var callback =
				{
	  				success:function( o) {
	  					if (onSuccess != null) {
	  						onSuccess();
	  					}
	  					else { // if no function is specified, simply display what the call returned
	  						var targetDiv = document.getElementById( wizard.targetDivId);
	  						targetDiv.innerHTML = o.responseText;
	  					}
	  				},
	  				failure:function( o) {
	  				},
					scope: this    				
				};
				var connection = new NC.util.AjaxConnection( 'POST', wizard.updateUrl, postData);
				connection.addUnprecedentedParameter();
				connection.executeMethod( callback);
				
//				var updater = new NC.util.AjaxUpdate( 'POST', wizard.updateUrl, wizard.targetDivId,  wizard.targetDivId, postData);
//				updater.doUpdate();
			},
			
		  	addWorkExperienceRow: function( rowIndex) {
		  		var workHistoryPage = wizard.getPageByLabel( this.StrNCWizWorkHistoryPage);
		  		workHistoryPage.addRowBefore( rowIndex);
		  		wizard.renderCurrentPage();
		  	},
  	
		  	addWorkExperienceRowAtEnd: function( ) {
		  		var workHistoryPage = wizard.getPageByLabel( this.StrNCWizWorkHistoryPage);
		  		workHistoryPage.addRowAtEnd( );
		  		wizard.renderCurrentPage();
		  	},
  	
		  	deleteWorkExperienceRow: function( rowIndex) {
		  		var workHistoryPage = wizard.getPageByLabel( this.StrNCWizWorkHistoryPage);
		  		workHistoryPage.deleteRow( rowIndex);
		  		wizard.renderCurrentPage();
		  	},
  	
		  	addEducationRow: function( rowIndex) {
		  		var educationHistoryPage = wizard.getPageByLabel( this.StrNCWizEducationPage);
		  		educationHistoryPage.addRowBefore( rowIndex);
		  		wizard.renderCurrentPage();
		  	},
  	
		  	addEducationRowAtEnd: function( rowIndex) {
		  		var educationHistoryPage = wizard.getPageByLabel( this.StrNCWizEducationPage);
		  		educationHistoryPage.addRowAtEnd( );
		  		wizard.renderCurrentPage();
		  	},
  	
		  	deleteEducationRow: function( rowIndex) {
		  		var educationHistoryPage = wizard.getPageByLabel( this.StrNCWizEducationPage);
		  		educationHistoryPage.deleteRow( rowIndex);
		  		wizard.renderCurrentPage();
		  	},
  	
			makeResumeEditFieldName: function( name) {
				return NC.widget.ResumeWizard.StrWizardFieldPrefix + name;
			},
			
			getFieldValue: function(name) {
				var fieldName = this.makeResumeEditFieldName(name);
				var el = document.getElementById( fieldName);
				var value = '';
				if (el != null) {
					value = NC.util.Dom.getValue( el);
				}
				else {
//					alert( fieldName + " not found");
				}
  				return value;
			},

			getFieldValueNull: function(name) {
				var fieldName = this.makeResumeEditFieldName(name);
				var el = document.getElementById( fieldName);
				var value = null;
				if (el != null) {
					value = NC.util.Dom.getValue( el);
				}
				else {
//					alert( fieldName + " not found");
				}
  				return value;
			},
			
			updateEncodedFieldValue: function( name) {
				var value = this.getFieldValue(name);
				if(value == null) {
					value = '';
				}
  				NC.util.Dom.updateEncodedField( domDocument, name, value);
			},

			updateFieldValue: function( name) {
				var value = this.getFieldValue(name);
				if (value == null) {
					value = '';
				}
  				NC.util.Dom.updateField( domDocument, name, value);
			},
			
			getDomDocument: function() {
				return domDocument;
			},
			
			validateDate: function( dateString) {
				if (dateString.length > 0) {
					var result = dateString.split( '\/');
					if (result.length == 2) {
						if ( result[0] > 0 && result[0] < 13 &&
							 result[1] > 1900) {
							return true;
 						}
					}
				}
				return false;
			},

			getDateMonths: function( dateString) {
				if (dateString.length > 0) {
					var result = dateString.split( '\/');
					if (result.length == 2) {
						if ( result[0] > 0 && result[0] < 13 &&
							 result[1] > 1900) {
							return (result[1] - 1900) *12 + (result[0] - 1);
 						}
					}
				}
				return 0;
			},
			
			validateDateRange: function( strFrom, strTo) {
				var dateFrom = this.getDateMonths( strFrom);
				var dateTo = this.getDateMonths( strTo);
				if (dateFrom > 0 && dateTo > 0 && dateTo > dateFrom) {
					return true;
				}
				else {
					return false;
				}
			},
			
			validatePhoneNumber: function( phoneString) {
//				var phoneMatcher = new RegExp('^[+]*[0-9]*[- ]*[\\(]?(\\d{3})[\\)]?[- ]*(\\d{3})[- ](\\d{4})$');
				var phoneMatcher = new RegExp('^[+]*[0-9]*[- ]*[\\(]?(\\d{3})[\\)]?[- ]?(\\d{3})[- ]?(\\d{4})$');
				
				if (phoneString.search( phoneMatcher) == -1) {
					return false;
				}
				else {
					return true;
				}
			}
 	 	}
  }	
();

})(); 	
 
