var Email_Class = function(oArgs) {
	this.init(oArgs);
}

Email_Class.prototype.init = function(oArgs) {

	this.worksheetID = oArgs.id || "emailWorksheet";
	this.page = oArgs.page || "interactive";

	this.btn = $("#" + this.worksheetID);

}

Email_Class.prototype.buildAdvisors = function() {

	var elAdvisors = $("input#advisorsList")[0];
	
	if (elAdvisors) {
		
		this.advisors = common.json(elAdvisors.value);	
	
	} else {
	
		this.advisors = [];
		
	}
	
	var i, len;
	
	len = this.advisors.length;
	
	if (len > 0) {
		
		var oOptions = [];
		
		oOptions.push('<option value=""></option>');
		
		for (i = 0; i < len; i++) {
			
			var advisor = this.advisors[i];
			
			oOptions.push('<option value="' + advisor.advisor + "|" + advisor.email + '">' + advisor.advisor + '</option>');
			
		}
			
		return '<select id="selectAdvisors">' + oOptions.join("") + '</select>';
		
	} else {
		
		return "";	
	
	}
	
}

Email_Class.prototype.build = function() {
	
	this.btn.bind("click", {ref: this}, this.buildWorksheet);
	
}

Email_Class.prototype.buildWorksheet = function() {

	var btn = this;
	var _data = arguments[0].data;

	/*
		Process:
			1. Open dialog containing text entry for an e-mail address
			2. Include list of advisors - pull through ajax
			3. Include cancel and submit buttons
				a. on cancel - close dialog
				b. on submit - 
					1. verify entered e-mail address as valid and existent
						* - field can be empty if an advisor is selected.
					2. create obj of selected values for completed when and where along with major
						that will be sent to the server via an ajax call
					3. the code on the server will rebuild the form for the email by pulling the form
						data from the database.
				
	*/
		
		
	/*
	 	Note: The list of advisors has been temporarily (?) removed because the intended use of the tool is by 
	 	those external to the University and they would not have an indication who the advisors are. 
	*/
	
	var emailContainer = oElement.create("div", {}, [
		oElement.create("div", {id: "wktInstruction"}, 
			oElement.create("label", {htmlFor: "emailWorksheetInput"}, "Please enter an e-mail address to send this worksheet to:")
		)
		, oElement.create("div", {id: "wktInput"}, 
			oElement.create("input", {type: "text", id: "emailWorksheetInput", value: ""}, null)
			//oElement.create("textarea", {id: "emailWorksheetInput"}, "")
		)
		//, oElement.create("div", {className: "offsetLeft10 offsetBottom10"}, "Note: To send to multiple e-mail addresses, separate each address with a semi-colon.") 
		, oElement.create("div", {className: "offsetLeft10 offsetBottom10 bold"}, "OR")
		, oElement.create("div", {className: "offsetLeft10"}, "E-mail the worksheet to an advisor at the CSU-Pueblo Teacher Education Program:")
		, oElement.create("div", {className: "offsetLeft20 offsetBottom20"}, 
			_data.ref.buildAdvisors()
		)
		//, Element.create("div", {className: "offsetLeft10 offsetBottom20"}, "Note: TBD")
	]);
	
	var formData = _data.ref.grabFormData();
	
		
	msgBox.display({
		title: "E-mail Worksheet:"
		, displayTitle: true
		, content: emailContainer
		, contentFunction: function() {
			
			window.scrollTo(0,0);
			setTimeout(function(){
				$("input#emailWorksheetInput")[0].focus();
			}, 0);
			
		}
		, buttons: {
		   cancel: {
		   		label: "Cancel"
				, id: "cancelButton"
				, className: "buttonCancel"
				, oData: {ref: "cancelButton"}
				, actionFunc: function() {
					msgBox.close();	
				}
		   }
		   , okay: {
		       label: "Submit"
		       , id: "okayButton"
		       , className: "buttonProceed"
		       , oData: {ref: "okayButton"}
		       , actionFunc: function() {
		       	
					// Check email input and select drop-down for values.
					var emailInput = $("#emailWorksheetInput")[0];
					var emailSelect = $("select#selectAdvisors")[0];
					
					var bError = true;
					var sErrorMessage = "Error: ";
					var formEmail = "";
					var whichForm = "";

				// if the input has a value, ignore the drop down menu.
		
					if (!error.isEmpty(emailInput)) {
					
						if (!error.isEmail(emailInput)) {
							bError = true;
							sErrorMessage  = sErrorMessage + " Please enter a valid e-mail address."; 
						} else {
							bError = false;
							formEmail = emailInput.value;
							whichForm = "input";
						}					
					
					} else {
					
						if (emailSelect.selectedIndex == 0) {
							sErrorMessage  = sErrorMessage + " An advisor must be selected if an e-mail address is not specified.";
							bError = true;
						} else {
							bError = false;
							formEmail = emailSelect.value;
							whichForm = "select";
						}
					}
				
				
					if (bError) {
						
						alert(sErrorMessage);
						
					} else {
						common.ajax({
							filePath: "server/email.asp",
							data: {
								inputs: formData
								, email: formEmail
								, from: whichForm
							},
							success: function(data, success){
								
								var sContent = String(data).split("|");
						
								var mContent = oElement.create("div", {
									className: "offset5"
								}, sContent[1]);
								
								common.handleMask("off");
								
								common.showDefaultMsg(mContent, sContent[0] + ":");
								
							}
						});
						
						msgBox.close();
					}
					
		       		
		       }
		   }
		}	
	});
}

Email_Class.prototype.grabFormData = function() {

	var oObj = {
		major: $("input#majorType")[0].value
		, when: ""
		, where: ""
	};
	
	var _when = $("select.infoYear");
	
	var i, len;
	var rWhen = [];
	
	for (i = 0, len = _when.length; i < len; i++) {
	
		rWhen.push(_when[i].value);
		
	}
	
	oObj.when = rWhen.join("|");
	
	var _where = $("div.infoInner");

	var rWhere = [];
	
	for (i = 0, len = _where.length; i < len; i++) {
		
		rWhere.push(_where[i].innerHTML);
		
	}
	
	oObj.where = rWhere.join("|");

	return escape('{"major":"' + oObj.major + '","when":"' + oObj.when + '","where":"' + oObj.where + '"}');
	
}
