var singleSelect = true;  // Allows an item to be selected once only

var sortSelect = true;  // Only effective if above flag set to true

var sortPick = true;  // Will order the picklist in sort sequence


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function changePass(){

		document.changePassword.change.value = "1";
		if(document.changePassword.password.value == document.changePassword.conPassword.value){
			document.changePassword.submit();		
		}else{
			alert("Password and Confirm Password do not match. Please re-enter");
		}

}
function resetPass(){
	document.changePassword.password.value ="";
	document.changePassword.conPassword.value = "";
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function initIt() {

  var selectList = document.getElementById("availableCourses");

  var pickList = document.getElementById("assignedCourses");

  var pickOptions = pickList.options;

  //pickOptions[0] = null;  // Remove initial entry from picklist (was only used to set default width)

  //selectList.focus();  // Set focus on the selectlist

}
function callDelete(){
	//document.edit.deleteUser.value="1";
	document.edit.action = "edit_user.jsp?deleteUser=1";
	document.edit.submit();
}
function callUpdate(){

	var pickList = document.getElementById("assignedCourses");
  	var pickOptions = pickList.options;
 	var pickOLength = pickOptions.length;
	var  assignedCourseList = "";
	 for(var i=0;i<pickOLength;i++){
		if(i>0){
			assignedCourseList += ";";
		}
		assignedCourseList += pickOptions[i].value ;
	 }


		if(document.edit.password.value == document.edit.conPassword.value){

			document.edit.action = "edit_user.jsp?update=1&assignedCourseList="+assignedCourseList;
			
			document.edit.submit();		
		}else{
			alert("Password and Confirm Password do not match. Please re-enter");
		}
}
function addIt() {
	
  var selectList = document.getElementById("availableCourses");
  var selectIndex = selectList.selectedIndex;
  var selectOptions = selectList.options;
  var pickList = document.getElementById("assignedCourses");
  var pickOptions = pickList.options;
  var pickOLength = pickOptions.length;
  // An item must be selected
  if (selectIndex > -1) {
    pickOptions[pickOLength] = new Option(selectList[selectIndex].text);
    pickOptions[pickOLength].value = selectList[selectIndex].value;
    // If single selection, remove the item from the select list
    if (singleSelect) {
      selectOptions[selectIndex] = null;
    }
    if (sortPick) {
      var tempText;
      var tempValue;
      // Sort the pick list
      while (pickOLength > 0 && pickOptions[pickOLength].value < pickOptions[pickOLength-1].value) {
        tempText = pickOptions[pickOLength-1].text;
        tempValue = pickOptions[pickOLength-1].value;
        pickOptions[pickOLength-1].text = pickOptions[pickOLength].text;
        pickOptions[pickOLength-1].value = pickOptions[pickOLength].value;
        pickOptions[pickOLength].text = tempText;
        pickOptions[pickOLength].value = tempValue;
        pickOLength = pickOLength - 1;
      }
    }
  }
}

function delIt() {
  var selectList = document.getElementById("availableCourses");
  var pickList = document.getElementById("assignedCourses");

  var selectOptions = selectList.options;
  var selectOLength = selectOptions.length;

  var pickIndex = pickList.selectedIndex;
  var pickOptions = pickList.options;
  if (pickIndex > -1) {
    // If single selection, replace the item in the select list
    if (singleSelect) {
      selectOptions[selectOLength] = new Option(pickList[pickIndex].text);
      selectOptions[selectOLength].value = pickList[pickIndex].value;
    }
    pickOptions[pickIndex] = null;
    if (singleSelect && sortSelect) {
      var tempText;
      var tempValue;
      // Re-sort the select list
      while (selectOLength > 0 && selectOptions[selectOLength].value < selectOptions[selectOLength-1].value) {
        tempText = selectOptions[selectOLength-1].text;
        tempValue = selectOptions[selectOLength-1].value;
        selectOptions[selectOLength-1].text = selectOptions[selectOLength].text;
        selectOptions[selectOLength-1].value = selectOptions[selectOLength].value;
        selectOptions[selectOLength].text = tempText;
        selectOptions[selectOLength].value = tempValue;
        selectOLength = selectOLength - 1;
      }
    }
  }
}


function reset(){
	var doc = document.addForm;
	doc.user_ID.value="";
	doc.firstName.value="";
	doc.lastName.value="";
	doc.password.value!="";
}


function addUser(){
	// validate the values and submit.

	
	var doc = document.addForm;
	var pickList = document.getElementById("assignedCourses");
  	var pickOptions = pickList.options;
 	 var pickOLength = pickOptions.length;
	var assignedCourseList = "";
	 for(var i=0;i<pickOLength;i++){
	 	if(i > 0){
			assignedCourseList += ";";	
                          }
		assignedCourseList += pickOptions[i].value ;                          		
	 }

	

	if(doc.user_ID.value!="" && doc.firstName.value!="" && doc.lastName.value!="" && doc.password.value!=""){
		if(doc.password.value == doc.conPassword.value){
			doc.action = "add_user.jsp?add=1&assignedCourseList="+assignedCourseList;
			doc.submit();		
		}else{
			alert("Password and Confirm Password do not match. Please re-enter");
		}
		
	}else{
		alert("Please enter all the fields.");
	}

}

function cancel(){
	// redirect to appropriate page.
}
