var editBox = {
	message: null,
	message2: null,

	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#edit_form').focus();
				});

			});
		});
	},
	show: function (dialog) {
	  $(".form_tabs:not(:first)").hide();
	  
	  $('#form_link_row a').click(function(){
		var myNum = $(this).attr('id');
		var whichTab = myNum.split("~");
		var theTab = "#" + whichTab[1];
		$(".form_tabs").hide("fast") ;
		$(theTab).show("fast");
		
		
		return false;
			
	  });

	  $('#editform').submit(function(e) {
		e.preventDefault();
	
		// first, decide what form we are validating
		var typeOfForm = $('#typeOfForm').val();
		var go;
		if (typeOfForm == 'task') {
			go = editBox.validateTask();
		} else if (typeOfForm == 'oe'){
			go = editBox.validateOE();
		} else if (typeOfForm == 'event'){
			go = editBox.validateEvent();
		} else if (typeOfForm == 'newdraft01'){
			go = editBox.validateNewDraft01();
		} else if (typeOfForm == 'schedule-pilot'){
			go = editBox.validateSchedPilot();
		} else if (typeOfForm == 'newDraftPrimary'){
			go = editBox.validateNewDraftPrimary();
		} else if (typeOfForm == 'newDraftSecondary'){
			go = editBox.validateNewDraftSecondary();
		} else if (typeOfForm == 'stage1'){
			go = editBox.validateStage1();
		} else if (typeOfForm == 'stage2'){
			go = editBox.validateStage2();
		} else if (typeOfForm == 'stage3'){
			go = editBox.validateStage3();
		} else {
			 go = true;
		}
				
		if (go) {
		  // passed validation
		var myFieldsAry = [];
        $(':text', this).each(function() {
            //var temptext = this.value;
        	//var temptext = escape(this.value);
        	var temptext = encodeURIComponent(this.value);
        	//temptext = temptext.replace("+", "%2B");
        	//temptext = temptext.replace(/\+/g, "%2B");
            //myFieldsAry.push(this.name + '=' + escape(this.value));
            myFieldsAry.push(this.name + '=' + temptext);
        })
        $(':option-sel', this).each(function() {
         	var mySelName = escape(this.value);
         	var myTempSelAry = mySelName.split("_");
         	// alert(myTempSelAry[0] + '=' + myTempSelAry[1]);
            myFieldsAry.push(myTempSelAry[0] + '=' + myTempSelAry[1]);
            //myFieldsAry.push(myTempSelAry[0] + '=' + myTempSelAry[1]);
            //alert(myTempSelAry[0] + '=' + encodeURIComponent(myTempSelAry[1]));
            
        })
        $(':hidden', this).each(function() {
        	var temptext = encodeURIComponent(this.value);
        	//var temptext = escape(this.value);
        	//temptext = temptext.replace(/\+/g, "%2B");
        	myFieldsAry.push(this.name + '=' + temptext);
            //myFieldsAry.push(this.name + '=' + escape(this.value));
        })
        $(':file', this).each(function() {
            myFieldsAry.push(this.name + '=' + escape(this.value));
        })
        
		var myFields = myFieldsAry.join('&');
		//alert(myFields);
		
        jQuery.ajax({
            // data: inputs.join('&'),
            type: "POST",
            data: myFields,
            url: this.action,
            timeout: 99999,
            error: function() {
              console.log("Failed to submit - ");
            },
            success: function(r) { 
              $('.content').html(r);
            }
        }) // checkout http://jquery.com/api for more syntax and options on this method.
        
        } else {
        // failed validation
        	if (editBox.message != '') {
        	  alert(editBox.message);
        	}
        }
		
		return false;
	  });

	  $('#fileform').submit(function(e) {
		e.preventDefault();
	
		// first, decide what form we are validating
		var typeOfForm = $('#typeOfForm').val();
		var go;
		if (typeOfForm == 'task') {
			go = editBox.validateTask();
		} else if (typeOfForm == 'oe'){
			go = editBox.validateOE();
		} else {
			 go = true;
		}
				
		if (go) {
		  // passed validation
		var myFieldsAry = [];
        $(':text', this).each(function() {
            myFieldsAry.push(this.name + '=' + escape(this.value));
        })
        $(':option-sel', this).each(function() {
         	var mySelName = escape(this.value);
         	var myTempSelAry = mySelName.split("_");
         	// alert(myTempSelAry[0] + '=' + myTempSelAry[1]);
            myFieldsAry.push(myTempSelAry[0] + '=' + myTempSelAry[1]);
        })
        $(':hidden', this).each(function() {
            myFieldsAry.push(this.name + '=' + escape(this.value));
        })
        $(':file', this).each(function() {
            myFieldsAry.push(this.name + '=' + escape(this.value));
        })
        
		var myFields = myFieldsAry.join('&');
		//alert(myFields);
		
        jQuery.ajax({
            // data: inputs.join('&'),
            data: myFields,
            url: this.action,
            timeout: 2000,
            error: function() {
              console.log("Failed to submit");
            },
            success: function(r) { 
              $('.content').html(r);
            }
        }) // checkout http://jquery.com/api for more syntax and options on this method.
        
        } else {
        // failed validation
            
            if (editBox.message != '') {
        	  alert(editBox.message);
        	}
        }
		
		return false;
	  });
		
		
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		});
	},
	
	validateTask: function () {
		editBox.message = '';
		editBox.message2 = '';
		
		if (!$('#editform #cTitle').val()) {
			editBox.message += 'A task title is required. \n';
		}
		if ($('#editform .sCog:checked').length<1) {
			editBox.message2 += 'Please check at least one of the cognitive categories. \n';
		}
		if ($('#editform .sGroupType:checked').length<1) {
			editBox.message2 += 'Please check at least one of the grouping types. \n';
		}
		if ($('#editform .sFormat:checked').length<1) {
			editBox.message2 += 'Please check at least one of the assessment formats. \n';
		}
		/*
		if (!$('#editform #tSubject').val()) {
			editBox.message += 'Standardized subject is required. \n';
		}
		if (!$('#editform #sTitleVerb').val()) {
			editBox.message += 'Standardized task name is required. \n';
		}
		*/
		if (!$('#editform #tasksummary').val()) {
			editBox.message += 'A description of the task is required. \n';
		}
		
		if (editBox.message2.length > 0) {
		  var gonext = confirm("You've not made a selection in at least one of the assessment categories.\n\n To stop and complete the category selections, click cancel.  To continue without completing the categories, click continue.");
		  if (!gonext) {
		    editBox.message = '';
		    return false;
		  }
		} 

		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateOE: function () {
		editBox.message = '';
		editBox.message2 = '';
		
		if (!$('#editform #cTitle').val()) {
			editBox.message += 'In section 1, a custom title is required. \n';
		}
		if ($('#editform .sCog:checked').length<1) {
			editBox.message2 += 'Please check at least one of the cognitive categories. \n';
		}
		if ($('#editform .sGroupType:checked').length<1) {
			editBox.message2 += 'Please check at least one of the grouping types. \n';
		}
		if ($('#editform .sFormat:checked').length<1) {
			editBox.message2 += 'Please check at least one of the assessment formats. \n';
		}
		if (!$('#editform #description').val()) {
			editBox.message += 'In section 1, a description of the evidence is required. \n';
		}
		
		if (editBox.message2.length > 0) {
		  var gonext = confirm("You've not made a selection in at least one of the assessment categories.\n\n To stop and complete the category selections, click cancel.  To continue without completing the categories, click continue.");
		  if (!gonext) {
		    editBox.message = '';
		    return false;
		  }
		} 
				
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}

	},
	
	validateEvent: function () {
		editBox.message = '';
		editBox.message2 = '';
		
		if (!$('#editform #cTitle').val()) {
			editBox.message += 'In section 1, title is required. \n';
		}
		if ($('#editform .sTeachType:checked').length<1) {
			editBox.message2 += 'Please check at least one of the teaching types. \n';
		}
		if ($('#editform .sGroupType:checked').length<1) {
			editBox.message2 += 'Please check at least one of the student grouping types. \n';
		}
		if ($('#editform .sEventType:checked').length<1) {
			editBox.message2 += 'Please check at least one of the event types. \n';
		}
		/*
		if (!$('#editform #tSubject').val()) {
			editBox.message += 'In section 2, you must specify a general topic. \n';
		}
		//alert($('#editform .sTitleVerb:checked').length);
		if ($('#editform .sTitleVerb:checked').length<1) {
			editBox.message += 'In section 2, please check at least one of the event type classifications. \n';
		}
		*/
		if (!$('#editform #activity').val()) {
			editBox.message += 'In section 1, a description of the event is required. \n';
		}
		if (!$('#editform #days').val()) {
			$('#editform #days').val('0');
		} else {
			if (isNaN($('#editform #days').val())) {
				editBox.message += 'Please enter only numbers in the minutes field in section 1. \n';
			}	
		}	
		
		if (editBox.message2.length > 0) {
		  var gonext = confirm("You've not made a selection in at least one of the event categories on tabs 2 and 3.\n\n To stop and complete the category selections, click cancel.  To continue without completing the categories, click continue.");
		  if (!gonext) {
		    editBox.message = '';
		    return false;
		  }
		} 
		
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateStage1: function () {
		editBox.message = '';
		
		if (!$('#editform #stage1type').val()) {
			editBox.message += 'You must choose an element type. \n';
		}
		if (!$('#editform #stage1text').val()) {
			
			editBox.message += "You haven't entered any text for your " + $('#editform #stage1type option:selected').text() + ". \n";
		}

		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateStage2: function () {
		editBox.message = '';
		
		if (!$('#editform #stage2type').val()) {
			editBox.message += 'You must choose an element type. \n';
		}
		if (!$('#editform #stage2title').val()) {
			
			editBox.message += "You haven't entered a title for your " + $('#editform #stage2type option:selected').text() + ". \n";
		}
		if (!$('#editform #stage2text').val()) {
			
			editBox.message += "You haven't entered any description for your " + $('#editform #stage2type option:selected').text() + ". \n";
		}

		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateStage3: function () {
		editBox.message = '';
		
		if (!$('#editform #stage3title').val()) {
			
			editBox.message += "You haven't entered a title for your event. \n";
		}
		if (!$('#editform #stage3text').val()) {
			
			editBox.message += "You haven't entered any text for your event.";
		}

		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateNewDraft: function () {
		editBox.message = '';
		
		if (!$('#editform #unitTitle').val()) {
			editBox.message += 'A unit title is required. \n';
		}
		if (!$('#editform #program').val()) {
			editBox.message += 'Please choose a subject. \n';
		}
		if (!$('#editform #grade').val()) {
			editBox.message += 'A grade designation is required. \n';
		}
						
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateNewDraftPrimary: function () {
		editBox.message = '';
		
		if (!$('#editform #programid').val()) {
			editBox.message += 'Please choose a subject. \n';
		}
		if (!$('#editform #grade').val()) {
			editBox.message += 'A grade designation is required. \n';
		}
						
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateNewDraftSecondary: function () {
		editBox.message = '';
		
		if (!$('#editform #program').val()) {
			editBox.message += 'Please choose a subject. \n';
		}
		if (!$('#editform #grade').val()) {
			editBox.message += 'A grade designation is required. \n';
		}
						
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateNewDraft01: function () {
		editBox.message = '';
		
		if (!$('#editform #unitTitle').val()) {
			editBox.message += 'A unit title is required. \n';
		}
		if (!$('#editform #schoolType').val()) {
			editBox.message += 'You must choose a type of school. \n';
		}
						
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	
	validateSchedPilot: function () {
		editBox.message = '';
		var isOkay = 'y';
		if (!$('#editform #startMonth').val()) {
			isOkay = 'n';
			editBox.message += 'sm';
		}
		if (!$('#editform #startDay').val()) {
			isOkay = 'n';
			editBox.message += 'sd';
		}
		if (!$('#editform #startYear').val()) {
			isOkay = 'n';
			editBox.message += 'sy';
		}
		if (!$('#editform #endMonth').val()) {
			isOkay = 'n';
			editBox.message += 'em';
		}
		if (!$('#editform #endDay').val()) {
			isOkay = 'n';
			editBox.message += 'ed';
		}
		if (!$('#editform #endYear').val()) {
			isOkay = 'n';
			editBox.message += 'ey';
		}
		
		if (isOkay!='y') {
			editBox.message += 'Please complete your date selections. \n';
		}
		
		if (editBox.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	}


};