var attachmentCount = 1;
var reflectionCount = 1;
var addPortfolioSlide;

function addAnotherAttachment() {
	if (attachmentCount <= 5) {
		attachmentCount++;
		var el = new Element('div', {'id':'field'}).setHTML('<label>Description</label><input type="text" name="attachment_description'+attachmentCount+'" size="50" maxlength="50"></div><div id="field"><label>File</label><input type="file" name="attachment'+attachmentCount+'">');
		el.injectInside($('moreattachments'));
	} else {
		alert('You can upload only 5 attachments at a time.');
	}
	if (attachmentCount == 5) {
		$('addattachmentspan').setHTML('You can upload 5 at a time.');
	}
}

//function addAnotherReflection() {
//	if (reflectionCount <= 5) {
//		reflectionCount++;
//		var el = new Element('div', {'id':'field'}).setHTML('<textarea name="reflection" id="reflection'+reflectionCount+'" cols="65" rows="5">');
//		el.injectInside($('morereflections'));
//	} else {
//		alert('You can upload only 5 attachments at a time.');
//	}
//	if (reflectionCount == 5) {
//		$('addreflectionspan').setHTML('You can have only 5 at a time.');
//	}
//}

function copyArtifact(el, cid, panel) {
	if (el.value) {
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=copyartifact'+"&cid="+cid+"&toid="+el.value, 
							  onComplete: function(resp) { 
								alert('copied');
								panel.hide();
							  }});
		myAjax.request();
	} else {
		alert('Please select a destination Standard & Criteria.');
	}
}

function copyAttachment(el, attachmentId, panel) {
	if (el.value && attachmentId) {
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=copyattachment&aid='+attachmentId+"&toid="+el.value, 
							  onComplete: function(resp) { 
								alert('copied');
								panel.hide();
							  }});
		myAjax.request();
	} else {
		alert('Please select a destination Standard & Criteria.');
	}
}

function generatePortfolio(form, panel) {
	if (!form.portfolio_type[0].checked && !form.portfolio_type[1].checked) {
		alert('Please choose to generate a public portfolio or download a zip file.');
		return false;
	}
	if (form.portfolio_type[1].checked && !form.description.value) {
		alert('Please enter a description.');
		return false;
	}
	$('generatesubmit').disabled=true;
	$('generatesubmit').value='Generating...';
	var myAjax = new Ajax(baseUrl+'ajax.pl', 
						 {method: 'post', 
						  data : form, 
						  onComplete: function(resp) { 
							alert('Your portfolio has been generated!');
							panel.hide();
						  }});
	myAjax.request();
	return false;
}

function standardNote(panel) {
	if ($('standardnotetext').value) {
		$('standardnotesubmit').disabled=true;
		$('standardnotesubmit').value='Saving...';
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=savestandardnote'+"&teacher_portfolio_id="+$('teacher_portfolio_id').value+"&standard_id="+$('standard_id').value+"&note="+$('standardnotetext').value+"&teacher_id="+$('teacher_id').value, 
							  onComplete: function(resp) { 
								$('s'+$('standard_id').value+'note').innerHTML = $('standardnotetext').value;
								$('s'+$('standard_id').value+'note').set({'styles':{'font-weight':'normal','font-style':'italic'}});
								alert('Note saved!');
								panel.hide();
								$('standardnotesubmit').disabled = false;
								$('standardnotesubmit').value = 'Save';
							  }});
		myAjax.request();
		panel.hide();
	} else {
		alert('Please enter a note.');
	}
	return false;
}

function deleteStandardNote(panel) {
	if ($('standardnotetext').value) {
		$('standardnotesubmit').disabled=true;
		$('standardnotesubmit').value='Deleting...';
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=deletestandardnote'+"&teacher_portfolio_id="+$('teacher_portfolio_id').value+"&standard_id="+$('standard_id').value+"&note="+$('standardnotetext').value+"&teacher_id="+$('teacher_id').value, 
							  onComplete: function(resp) { 
								$('s'+$('standard_id').value+'note').innerHTML = 'add note';
								$('s'+$('standard_id').value+'note').set({'styles':{'font-weight':'bold','font-style':'normal'}});
								alert('Note deleted!');
								panel.hide();
								$('standardnotesubmit').disabled = false;
								$('standardnotesubmit').value = 'Save';
							  }});
		myAjax.request();
		panel.hide();
	}
	return false;
}


function showStandardNotePanel(event, args) {
	YAHOO.example.container.notePanel = new YAHOO.widget.Panel("notePanel", { width:"475px", visible:false, constraintoviewport:true, context:['criteria'+args.index,'tl','bl']} );
	//YAHOO.example.container.notePanel.setBody($('snotedivsource').innerHTML);
	if ($('s'+args.index+'note').innerHTML != 'add note') {
		$('standardnotetext').value = $('s'+args.index+'note').innerHTML;
	} else {
		$('standardnotetext').value = '';
	}
	$('standard_id').setAttribute('value', args.index);
	YAHOO.example.container.notePanel.render("notediv");
	YAHOO.example.container.notePanel.show();
}

function exportOne(desc, inclAttachments, criteriaId, panel) {
	if (desc) {
		var incl = 'N';
		if (inclAttachments) {
			incl = 'Y';
		}
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=exportone&description='+desc+"&include_attachments="+incl+"&cid="+criteriaId, 
							  onComplete: function(resp) { 
								alert('Exported!');
								panel.hide();
							  }});
		myAjax.request();
	} else {
		alert('Please enter a description.');
	}
	return false;
}

function addPortfolio() {
	var myAjax = new Ajax(baseUrl+'ajax.pl', 
						 {method: 'post', 
						  data : 'action=addportfolio&id='+$('portfolio_id').value+'&description='+$('description').value, 
						  onComplete: function(resp) { 
							  $('portfoliodiv').setHTML(resp);
							  if (addPortfolioSlide) {
									addPortfolioSlide.hide();
							  }
						  }});
	myAjax.request();
	return false;
}

function activatePortfolio(id) {
	var myAjax = new Ajax(baseUrl+'ajax.pl', 
						 {method: 'post', 
						  data : 'action=updateactiveportfolio&id='+id, 
						  onComplete: function(resp) { 
							  $('portfoliodiv').setHTML(resp);
						  }});
	myAjax.request();
}

function deletePortfolio(id) {
	var really = confirm('Are you sure you want to delete this portfolio?');
	if (really) {
		var myAjax = new Ajax(baseUrl+'ajax.pl', 
							 {method: 'post', 
							  data : 'action=deleteportfolio&id='+id, 
							  onComplete: function(resp) { 
								  $('portfoliodiv').setHTML(resp);
							  }});
		myAjax.request();
	}
	return false;
}

//var mySlide;
//var drop;
//var drop2;
//var drop3;
//var dropFx;
//var dropFx2;
//var dropFx3;
//
//function init_slider(divname, linkname) {
//	mySlide = new Fx.Slide(divname).hide();
//	$(linkname).addEvent('click', function(e){
//		e = new Event(e);
//		mySlide.toggle();
//		if (this.innerHTML == 'show') {
//			this.innerHTML = 'hide';
//		} else {
//			this.innerHTML = 'show';
//		}
//		e.stop();
//	});
//}
//
//function init_items() {
//	if (!$('absentcart') && !$('tardycart') && !$('itemscart')) {
//		return;
//	}
//	drop = $('absentcart');
//	drop2 = $('tardycart');
//	drop3 = $('itemscart');
//	dropFx = drop.effect('background-color', {wait: false}); // wait is needed so that to toggle the effect,
//	dropFx2 = drop2.effect('background-color', {wait: false}); // wait is needed so that to toggle the effect,
//	dropFx3 = drop3.effect('background-color', {wait: false}); // wait is needed so that to toggle the effect,
//	 
//	$$('.item').each(function(item){
//		if (item.getParent().getProperty('id') == 'itemscart') {
//			$('itemscart').setStyle('height', 36 + $('itemscart').getStyle('height').toInt());
//		}
//		item.addEvent('mousedown', function(e) {
//			e = new Event(e).stop();
//
//			var clone = this.clone()
//				.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
//				.setStyles({'opacity': 0.7, 'position': 'absolute'})
//				.addEvent('emptydrop', function() {
//					this.remove();
//					drop.removeEvents();
//				}).inject(document.body);
//	 
//			var clone2 = this.clone()
//				.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
//				.setStyles({'opacity': 0.7, 'position': 'absolute'})
//				.addEvent('emptydrop', function() {
//					this.remove();
//					drop2.removeEvents();
//				}).inject(document.body);
//
//			var clone3 = this.clone()
//				.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
//				.setStyles({'opacity': 0.7, 'position': 'absolute'})
//				.addEvent('emptydrop', function() {
//					this.remove();
//					drop3.removeEvents();
//				}).inject(document.body);
//	 
//			drop.addEvents({
//				'drop': function() {
//					$('attend_'+item.getProperty('name')).value = 'A';
//					if (item.getParent().getProperty('id') == 'itemscart') {
//						$('itemscart').setStyle('height', $('itemscart').getStyle('height').toInt() - 45);
//					}
//					drop.removeEvents();
//					clone.remove();
//					item.inject(drop);
//					dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx));
//				},
//				'over': function() {
//					dropFx.start('98B5C1');
//				},
//				'leave': function() {
//					dropFx.start('ffffff');
//				}
//			});
//			drop2.addEvents({
//				'drop': function() {
//					$('attend_'+item.getProperty('name')).value = 'T';
//					if (item.getParent().getProperty('id') == 'itemscart') {
//						$('itemscart').setStyle('height', $('itemscart').getStyle('height').toInt() - 45);
//					}
//					drop2.removeEvents();
//					clone2.remove();
//					item.inject(drop2);
//					dropFx2.start('7389AE').chain(dropFx2.start.pass('ffffff', dropFx2));
//				},
//				'over': function() {
//					dropFx2.start('98B5C1');
//				},
//				'leave': function() {
//					dropFx2.start('ffffff');
//				}
//			});
//
//			drop3.addEvents({
//				'drop': function() {
//					$('attend_'+item.getProperty('name')).value = 'P';
//					if (item.getParent().getProperty('id') != 'itemscart') {
//						$('itemscart').setStyle('height', $('itemscart').getStyle('height').toInt() + 35);
//					}
//					drop3.removeEvents();
//					clone3.remove();
//					item.inject(drop3);
//					dropFx3.start('7389AE').chain(dropFx3.start.pass('ffffff', dropFx3));
//				},
//				'over': function() {
//					dropFx3.start('98B5C1');
//				},
//				'leave': function() {
//					dropFx3.start('ffffff');
//				}
//			});
//	 
//			var drag = clone.makeDraggable({
//				droppables: [drop]
//			}); // this returns the dragged element
//			var drag2 = clone2.makeDraggable({
//				droppables: [drop2]
//			}); // this returns the dragged element
//			var drag3 = clone3.makeDraggable({
//				droppables: [drop3]
//			}); // this returns the dragged element
//	 
//			drag.start(e); // start the event manual
//			drag2.start(e); // start the event manual
//			drag3.start(e); // start the event manual
//		});
//	 
//	});
//}
//
//function saveClass() {
//	if ($('class_name').value == '' || $('class_name').value == 'Class Name') {
//		$('class_name').focus();
//		alert('Please enter a class name.');
//		return false;
//	}
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : $('addclassform'), 
//						  onComplete: function(resp) {
//							if (resp != 'failure') {
//								$('classtable').setHTML(resp);
//								$('class_name').value = '';
//								$('class_name').focus();
//								if (!$('addanother').checked) {
//									mySlide.toggle();
//								}
//							}
//						  }});
//	myAjax.request();
//}
//
//function deleteClass(id) {
//	var really = confirm('Are you sure you want to delete this class?');
//	if (really)	{
//		var myAjax = new Ajax(baseUrl+'index.pl', 
//							 {method: 'post', 
//							  data : 'action=ajaxdeleteclass&class_id='+id, 
//							  onComplete: function(resp,xml) { 
//								if (resp != 'failure') {
//									$('classtable').setHTML(resp);
//								}
//							  }});
//		myAjax.request();
//	}
//}
//
//function archiveClass(id) {
//	var really = confirm('Are you sure you want to archive this class?');
//	if (really)	{
//		var myAjax = new Ajax(baseUrl+'index.pl', 
//							 {method: 'post', 
//							  data : 'action=ajaxarchiveclass&class_id='+id, 
//							  onComplete: function(resp) { 
//								if (resp != 'failure') {
//									$('classtable').setHTML(resp);
//								}
//							  }});
//		myAjax.request();
//	}
//}
//
//function unarchiveClass(id) {
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : 'action=ajaxunarchiveclass&class_id='+id, 
//						  onComplete: function(resp) { 
//							if (resp != 'failure') {
//								$('classtable').setHTML(resp);
//							}
//						  }});
//	myAjax.request();
//}
//
//function saveStudent() {
//	if ($('student_first_name').value == '' || $('student_first_name').value == 'First Name') {
//		alert('Please enter a student first name.');
//		$('student_first_name').focus();
//		return false;
//	}
//	if ($('student_class').selectedIndex == 0) {
//		alert('Please select a class for the student.');
//		return false;
//	}
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : $('addstudentform'), 
//						  onComplete: function(resp) { 
//							if (resp != 'failure') {
//								$('studenttable').setHTML(resp);
//								if (!$('addanother').checked) {
//									mySlide.toggle();
//								}
//								$('student_first_name').value = '';
//								$('student_last_name').value = '';
//								$('student_first_name').focus();
//							}
//						  }});
//	myAjax.request();
//}
//
//function editStudent(id) {
//	$('studenttr'+id).setStyle('display', 'none');
//	$('edittr'+id).setStyle('display', 'table-row');
//	$('editname'+id).setStyle('padding', '0px');
//}
//
//function editClass(id) {
//	$('classtr'+id).setStyle('display', 'none');
//	$('edittr'+id).setStyle('display', 'table-row');
//	$('new_description'+id).setStyle('padding', '0px');
//}
//
//function revertStudent(id) {
//	$('studenttr'+id).setStyle('display', 'table-row');
//	$('edittr'+id).setStyle('display', 'none');
//}
//
//function revertClass(id) {
//	$('classtr'+id).setStyle('display', 'table-row');
//	$('edittr'+id).setStyle('display', 'none');
//}
//
//function deleteStudent(id) {
//	var really = confirm('Are you sure you want to delete this student?');
//	if (really)	{
//		var myAjax = new Ajax(baseUrl+'index.pl', 
//							 {method: 'post', 
//							  data : 'action=ajaxdeletestudent&class_id='+id, 
//							  onComplete: function(resp) { 
//								if (resp != 'failure') {
//									$('studenttable').setHTML(resp);
//								}
//							  }});
//		myAjax.request();
//	}
//}
//
//function deleteAttendance() {
//	var really = confirm('Are you sure you want to delete the attendance for this day?');
//	if (really)	{
//		$('action').value = 'deleteattendance';
////		var myAjax = new Ajax(baseUrl+'index.pl', 
////							 {method: 'post', 
////							  data : 'action=ajaxdeleteattendance&class_id='+$('class').value, 
////							  onComplete: function(resp) { 
////								if (resp != 'failure') {
////									$('studenttable').setHTML(resp);
////								}
////							  }});
////		myAjax.request();
//	} else {
//		return false;
//	}
//}
//
//function updateStudent(id) {
//	$('studentname'+id).setText($('new_first_name'+id).value + ' ' + $('new_last_name'+id).value);
//	$('studenttr'+id).setStyle('display', 'table-row');
//	$('edittr'+id).setStyle('display', 'none');
//	var pageData = "id="+id+"&action=ajaxupdatestudent&first_name="+$('new_first_name'+id).value+"&last_name="+$('new_last_name'+id).value+"&class="+$('new_class'+id).value;
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : pageData, 
//						  onComplete: function(resp) { 
//							if (resp != 'failure') {
//								$('message').innerHTML = 'Student saved.'
//								var drop = $('message');
//								dropFx = drop.effect('background-color', {wait: false, duration: 1500});
//								dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx)).chain(function() {	$('message').innerHTML = '&nbsp;';});
//							}
//							//tick.removeClass('ajax-loading');
//						  }});
//	myAjax.request();
//}
//
//function updateClass(id) {
//	$('classdescription'+id).setText($('new_description'+id).value);
//	$('classtr'+id).setStyle('display', 'table-row');
//	$('edittr'+id).setStyle('display', 'none');
//	var pageData = "id="+id+"&action=ajaxupdateclass&description="+$('new_description'+id).value;
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : pageData, 
//						  onComplete: function(resp) { 
//							if (resp != 'failure') {
//								$('message').innerHTML = 'Class saved.'
//								var drop = $('message');
//								dropFx = drop.effect('background-color', {wait: false, duration: 1500});
//								dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx)).chain(function() {	$('message').innerHTML = '&nbsp;';});
//							}
//							//tick.removeClass('ajax-loading');
//						  }});
//	myAjax.request();
//}
//
//function updateStudents() {
//	Cookie.set('selected_class', $('class').value);
//	var tick = $('ticker').empty().addClass('ajax-loading');
//	var pageData = 'action=ajaxgetstudents&class_id='+$('class').value;
//	if ($('newdate')) {
//		pageData = pageData + "&newdate="+$('newdate').value;
//	}
//	var myAjax = new Ajax(baseUrl+'index.pl', 
//						 {method: 'post', 
//						  data : pageData, 
//						  onComplete: function(resp) { 
//							if (resp != 'failure') {
//								var ind = 0;
//								if (resp.indexOf("!!!") > 0) {
//									var dayType = resp.substring(0,resp.indexOf("!!!"));
//									if (dayType == 'N') {
//										ind = 0;
//									} else if (dayType == 'E') {
//										ind = 1;
//									} else if (dayType == 'L') {
//										ind = 2;
//									} else if (dayType == 'S') {
//										ind = 3;
//									}
//									resp = resp.substring(resp.indexOf("!!!")+3);
//								}
//								$('day_type').selectedIndex = ind;
//								$('content').setHTML(resp);
////								$('studenttable').setHTML(resp);
//								init_items();
//								init_slider('notesdiv','shownotes');
//							}
//							tick.removeClass('ajax-loading');
//						  }});
//	myAjax.request();
//}
//
//function changeDate() {
//	var saveDate = $('newdate').value;
//	if (saveDate) {
//		var tick = $('ticker2').empty().addClass('ajax-loading');
//		var myAjax = new Ajax(baseUrl+'index.pl', 
//							 {method: 'post', 
//							  data : 'action=ajaxgetdate&newdate='+$('newdate').value+"&class_id="+$('class').value, 
//							  onComplete: function(resp) { 
//								if (resp != 'failure') {
//									var ind = 0;
//									if (resp.indexOf("!!!") > 0) {
//										var dayType = resp.substring(0,resp.indexOf("!!!"));
//										if (dayType == 'N') {
//											ind = 0;
//										} else if (dayType == 'E') {
//											ind = 1;
//										} else if (dayType == 'L') {
//											ind = 2;
//										} else if (dayType == 'S') {
//											ind = 3;
//										}
//										resp = resp.substring(resp.indexOf("!!!")+3);
//									}
//									$('day_type').selectedIndex = ind;
//									$('content').setHTML(resp);
//									init_slider('notesdiv','shownotes');
//									init_items();
//									$('newdate').value = saveDate;
//								}
//								tick.removeClass('ajax-loading');
//							  }});
//		myAjax.request();
//	} else {
//		alert('Please select a date.');
//	}
//}
