// JavaScript Document
function AjaxSaveAssignment(id,type,val) {
	AjaxRequest.get( {
	'url':'Classes.php'
	,'parameters':{'a':'editAssignment', 'id':id, 'type':type, 'val':val}
	,'onSuccess':function(req) { 
		
	}
	} );
}
function AjaxNewAssignment(classID,userID) {
	var tbl = document.getElementById('assignmentTable');
  	var lastRow = tbl.rows.length;
 	// if there's no header row in the table, then iteration = lastRow + 1
  	var iteration = lastRow;
  	var row = tbl.insertRow(lastRow);
		
	//check cell
  	var cellcheck = row.insertCell(0);
  		
	// left cell
  	var cellLeft = row.insertCell(1);
  	var textNode = document.createElement('input');
  	textNode.type = "text";
  	textNode.name = "assignmentType[]";
  	textNode.maxlength = 10;
  	cellLeft.appendChild(textNode);
  
  	// right cell
  	var cellRight = row.insertCell(2);
  	var textNode1 = document.createElement('input');
  	textNode1.type = "text";
  	textNode1.name = "assignmentQTY[]";
  	textNode1.maxlength = 2;
  	cellRight.appendChild(textNode1);
    
  	// select cell
  	var cellRightSel = row.insertCell(3);
  	var textNode2 = document.createElement('input');
  	textNode2.type = "text";
  	textNode2.name = "assignmentPCT[]";
  	textNode2.maxlength = 3;
  	cellRightSel.appendChild(textNode2);
}
function subNumClasses(theForm) {
	if (theForm.numClasses.value != "" && theForm.numClasses.value > 0) {
	var status = AjaxRequest.submit(
    theForm
    ,{
      'onSuccess':function(req){ document.getElementById("academics_ClassesForm").innerHTML = req.responseText; }
    }
  );
  return status;
	} else {
		alert("You must enter at least 1 class.");
	}
}

function subClasses(theForm) {
	var status = AjaxRequest.submit(
    theForm
    ,{
      'onSuccess':function(req){ 
	  	//document.getElementById("academics_ClassesForm").innerHTML = req.responseText;
		//alert(req.responseText);
		//eval(req.responseText);
		window.location = 'Index.php';
		}
    }
  );
  return status;
}
var fullSlider;
var gradePaneOpen = false;
function addGrades(classID,userID,main) {
	fullSlider = main;
	if (gradePaneOpen) {
		closeGradePane();	
	}
	AjaxRequest.get( {
		'url':'Classes.php'
		,'parameters':{'a':'addGrades', 'id':classID, 'userID':userID}
		,'onSuccess':function(req) { 
			document.getElementById("assignmentArea").innerHTML = req.responseText;
			document.getElementById("assignmentArea").style.height = "auto";
			openGradePane();
		}
	} );
}
function fullToLeft(element1,element2) {
	var fx = new Fx.Styles(document.getElementById('classDetails'), {duration:500, wait:false});
 	fx.start({
		'width': (screen.width*.5)
	});
	var fx = new Fx.Styles(document.getElementById('assignmentArea'), {duration:500, wait:false});
 	fx.start({
		'width': (screen.width*.5)
	});
}
function leftToFull(element1,element2) {
		var fx = new Fx.Styles(document.getElementById('classDetails'), {duration:500, wait:false});
 	fx.start({
		'width': (screen.width*.97)
	});
	var fx = new Fx.Styles(document.getElementById('assignmentArea'), {duration:500, wait:false});
 	fx.start({
		'width': (screen.width*0)
	});
}
function openGradePane() {
	document.getElementById("assignmentArea").style.visibility = "visible";
	var fx = new Fx.Styles(document.getElementById("assignmentArea"), {duration:500, wait:true});
 	fx.start({
		'width': (screen.width*.5),
		'padding': '10px'
	});
	gradePaneOpen = true;
}
function closeGradePane() {
	var fx = new Fx.Styles(document.getElementById("assignmentArea"), {duration:500, wait:true});
	fx.start({
		'width': 0,
		'padding': 0
	});
	if (fullSlider) {
		leftToFull('classDetails','assignmentArea');	
	}
	gradePaneOpen = false;
}
function hidePane() {
	
}
function subAssignments(theForm) {
	var status = AjaxRequest.submit(
    theForm
    ,{
      'onSuccess':function(req){ 
	  	//document.getElementById("academics_ClassesForm").innerHTML = req.responseText;
		//eval(req.responseText);
		window.location = 'Index.php';
		}
    }
  );
  return status;
}

function addRowToAssignmentTable()
{
  var tbl = document.getElementById('assignmentTable');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createElement('input');
  textNode.type = "text";
  textNode.name = "assignmentType[]";
  textNode.maxlength = 10;
  cellLeft.appendChild(textNode);
  
  // right cell
  var cellRight = row.insertCell(1);
  var textNode1 = document.createElement('input');
  textNode1.type = "text";
  textNode1.name = "assignmentQTY[]";
  textNode1.maxlength = 2;
  cellRight.appendChild(textNode1);
    
  // select cell
  var cellRightSel = row.insertCell(2);
  var textNode2 = document.createElement('input');
  textNode2.type = "text";
  textNode2.name = "assignmentPCT[]";
  textNode2.maxlength = 3;
  cellRightSel.appendChild(textNode2);
}

function addRowToAssignmentTable()
{
  var tbl = document.getElementById('assignmentTable');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createElement('input');
  textNode.type = "text";
  textNode.name = "assignmentType[]";
  textNode.maxlength = 10;
  cellLeft.appendChild(textNode);
  
  // right cell
  var cellRight = row.insertCell(1);
  var textNode1 = document.createElement('input');
  textNode1.type = "text";
  textNode1.name = "assignmentQTY[]";
  textNode1.maxlength = 2;
  cellRight.appendChild(textNode1);
    
  // select cell
  var cellRightSel = row.insertCell(2);
  var textNode2 = document.createElement('input');
  textNode2.type = "text";
  textNode2.name = "assignmentPCT[]";
  textNode2.maxlength = 3;
  cellRightSel.appendChild(textNode2);
}

var gradesDropdown = "";

function addRowToGradeTable(classID)
{
  var tbl = document.getElementById('gradeTable');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createElement('input');
  textNode.type = "text";
  textNode.name = "gradesName[]";
  textNode.maxlength = 30;
  cellLeft.appendChild(textNode);
  
  // right cell
  var cellRight = row.insertCell(1);
  var textNode1 = document.createElement('select');
  textNode1.name = "gradesType[]";
  AjaxRequest.get( {
		'url':'Classes.php'
		,'parameters':{'a':'JSSelectClasses', 'id':classID}
		,'onSuccess':function(req) { 
			eval(req.responseText);
 		 	cellRight.appendChild(textNode1);
    
  			var cellRightSel = row.insertCell(2);
  			var textNode2 = document.createElement('input');
  			textNode2.type = "text";
  			textNode2.name = "gradesGrade[]";
  			textNode2.maxlength = 4;
  			cellRightSel.appendChild(textNode2);
		}
	} );
 
}
function confirmDelete(name,id,cid) {
	var del = confirm("Are you sure that you want to delete " + name + "?");	
	if (del) {
		window.location = "Index.php?a=deleteGrade&id=" + id + "&cid=" + cid;
	} else {
		return false;
	}
}

var highlightbehavior="TR"

var ns6=document.getElementById&&!document.all
var ie=document.all

function changeto(e,highlightcolor){
source=ie? event.srcElement : e.target
if (source.tagName=="TABLE")
return
while(source.tagName!=highlightbehavior && source.tagName!="HTML")
source=ns6? source.parentNode : source.parentElement
if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore")
source.style.backgroundColor=highlightcolor
}

function contains_ns6(master, slave) { //check if slave is contained by master
while (slave.parentNode)
if ((slave = slave.parentNode) == master)
return true;
return false;
}

function changeback(e,originalcolor){
if (ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")||source.tagName=="TABLE")
return
else if (ns6&&(contains_ns6(source, e.relatedTarget)||source.id=="ignore"))
return
if (ie&&event.toElement!=source||ns6&&e.relatedTarget!=source)
source.style.backgroundColor=originalcolor
}

function confirmDrop(cn,id) {
	var doDrop = confirm("Are you sure that you want to drop " + cn + "? All assignments will also be deleted.");
	if (doDrop) {
		window.location = 'Index.php?a=dropClass&id=' + id;
	} else {
		return false;
	}
}
function setEmail(pid) {
	if (pid != "") {
		document.getElementById("email").value = pid+"@vt.edu";	
	}
}
function checkPass() {
	v1 = document.getElementById("password1");
	v2 = document.getElementById("password2");
	if (v1.value != v2.value){
		alert("The two passwords you entered do not match.");
		v1.value = "";
		v2.value = "";
		v1.focus();
	}
}
function findUsers(level,group) {
	closeGradePane();	
	AjaxRequest.get( {
	'url':'Users.php'
	,'parameters':{'a':'findForm', 'level':level, 'group':group}
	,'onSuccess':function(req) { 
		document.getElementById("assignmentArea").innerHTML = req.responseText;
		document.getElementById("assignmentArea").style.height = "auto";
		openGradePane();
		//fullSlider = true;
		//fullToLeft('classDetails','assignmentArea');
	}
	} );
}

function changeFindBy() {
	sel = document.getElementById("findBy");
	selI = sel.selectedIndex;
	selV = sel.options[selI].value;
	switch (selV) {
		case "PID":
			document.getElementById("pidForm").style.display = "block";
			document.getElementById("coForm").style.display = "none";
			document.getElementById("yearForm").style.display = "none";
			document.getElementById("gradeForm").style.display = "none";
		break;
		case "Company":
			document.getElementById("pidForm").style.display = "none";
			document.getElementById("coForm").style.display = "block";
			document.getElementById("yearForm").style.display = "none";
			document.getElementById("gradeForm").style.display = "none";
		break;
		case "Year":
			document.getElementById("pidForm").style.display = "none";
			document.getElementById("coForm").style.display = "none";
			document.getElementById("yearForm").style.display = "block";
			document.getElementById("gradeForm").style.display = "none";
		break;
		case "Grade":
			document.getElementById("pidForm").style.display = "none";
			document.getElementById("coForm").style.display = "none";
			document.getElementById("yearForm").style.display = "none";
			document.getElementById("gradeForm").style.display = "block";
		break;
	}
}

function subSearch(theForm) {
	closeGradePane();
	var status = AjaxRequest.submit(
    theForm
    ,{
      'onSuccess':function(req){ 
	  	document.getElementById("assignmentArea").innerHTML = req.responseText; 
	  	openGradePane();
	  }
    }
  );
  return status;
}