function moveUp(element) {
    var j;
    var who;
    for(i = 0; i < element.options.length; i++) {
        if(element.options[i].selected == true) {
            if(i != 0) {
                var text1 = element.options[i-1].text;
                j = text1.indexOf('.');
                who = text1.substr(j+1);
                j = text1.substr(0,j);
                text1=(parseInt(j)+1)+"."+who;
                var text2 = element.options[i].text;
                j = text2.indexOf('.');
                who = text2.substr(j+1);
                j = text2.substr(0,j);
                text2=(j-1)+"."+who;
                var temp = new Option(text1,element.options[i-1].value);
                var temp2 = new Option(text2,element.options[i].value);
                element.options[i-1] = temp2;
                element.options[i-1].selected = true;
                element.options[i] = temp;
            }
        }
    }
}

function moveDown(element) {
    var j;
    var who;
    for(i = (element.options.length - 1); i >= 0; i--) {
        if(element.options[i].selected == true) {
            if(i != (element.options.length - 1)) {
                var text1 = element.options[i+1].text;
                j = text1.indexOf('.');
                who = text1.substr(j+1);
                j = text1.substr(0,j);
                text1=(j-1)+"."+who;
                var text2 = element.options[i].text;
                j = text2.indexOf('.');
                who = text2.substr(j+1);
                j = text2.substr(0,j);
                text2=(parseInt(j)+1)+"."+who;
                var temp1 = new Option(text1 ,element.options[i+1].value);
                var temp2 = new Option(text2,element.options[i].value);
                element.options[i+1] = temp2;
                element.options[i+1].selected = true;
                element.options[i] = temp1;
            }
        }
    }
}


function addToList(list,from) {
    var len = list.options.length;
    if (len == 1 && list.options[0].value == '0') {
        list.options[0] = null;
        len = 0;
    }
    var found = false;
    var val = '';
    var name = '';
    var j = 0;
    for (var i =0; i < from.player.length; i++) {
        if (from.player[i].checked) {
            found = false;
            val = from.player[i].value;
            j = val.indexOf(',');
            name = val.substr(j+1);
            val = val.substr(0, j);
            for (j = 0; j < list.options.length; j++) {
                if (list.options[j].value == val) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                name = (len+1)+". "+name;
                list.options[len] = new Option(name, val);
                len++;
            }
            from.player[i].checked=false;
        }
    }
}

function checkPid(pid,from)
{
	var j=0,val,lpid;
	for (var i=0; i<from.player.length; i++) {
         val = from.player[i].value;
         j = val.indexOf(',');
         val = val.substr(0, j);
		 if (val==pid) {from.player[i].checked=true;}
	}
}

function view(list, url) {
    for (var i = 0; i < list.options.length; i++) {
        if(list.options[i].selected == true) {
            window.open(url+list.options[i].value+".html", "draftWindow");
            break;
        }
    }
}

function removeFromList(list, whoText) {
    var found, text, i, j, who, temp, fcnt = 0, message, choice;
    for (i = 0; i < list.options.length; i++) {
        if (list.options[i].selected === true) {
            message = "Really remove " + list.options[i].text + "?";
            //choice=confirm(message);
            choice = true;
            if (choice === true) {
                list.options[i] = null;
                found = i;
                fcnt = fcnt + 1;
                i--;
            }

        }
        if (i >= found) {
            text = list.options[i].text;
            j = text.indexOf('.');
            who = text.substr(j + 1);
            j = text.substr(0, j);
            text = (j - fcnt) + "." + who;
            temp = new Option(text, list.options[i].value);
            list.options[i] = temp;
        }
    }
    if (list.options.length == 0) {
        temp = new Option("Select " + whoText + "s below", 0);
        list.options[0] = temp;
    }
}


function checkCount(elem, count, label, gmID) {
   var cnt=elem.options.length;
   var voteID;
   if (gmID!="") {
     for (var j=0; j<cnt; j++) {
       voteID=elem.options[j].value;
       if (voteID==gmID){
         alert ("You may not vote for yourself");
         return false;
       }
     }
   }
   if ((cnt!=count)||((cnt==1)&&(elem.options[0].value==0))) {
     alert("You must vote for "+count+" "+label+"s!");
     return false;
   }
   else {
     return true;
   }
}

function moveList(fromElem, toElem) {
    toElem.value = '';
    for (var i=0; i<fromElem.options.length; i++) {
        if (toElem.value == '') {
            if (fromElem.options[i].value!=0) {toElem.value =  fromElem.options[i].value;}
        }
        else {
            toElem.value = toElem.value + '|' + fromElem.options[i].value;
        }
    }
    return true;
}

function hasValue(thisinput, isCheckbox, totCount) {
  var count = 0;

  // Single Element Radio Button
  if (thisinput.checked) {return true;}

  // Multi Element Radio Button
  for (var j=0; j<thisinput.length; j++) {
    if (thisinput[j].checked) {
      if (isCheckbox) {
        count++;
      }
      else {
        return true;
      }
    }
  }

  // Checkbox
  if (isCheckbox) {
    if (count == totCount) {
      return true;
    }
  }
  return false;
}

function formValidation(thisform,splitOF) {
  var bool = true;
  var msg = "You have not selected the following positions: ";
  if (!hasValue(thisform.pitcher, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Pitcher";
    bool = false;
  }
  if (!hasValue(thisform.catcher, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Catcher";
    bool = false;
  }
  if (!hasValue(thisform.firstbase, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "First Base";
    bool = false;
  }
  if (!hasValue(thisform.secondbase, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Second Base";
    bool = false;
  }
  if (!hasValue(thisform.thirdbase, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Third Base";
    bool = false;
  }
  if (!hasValue(thisform.shortstop, false, 1)) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Shortstop";
    bool = false;
  }
  if ((splitOF==1)&&(!hasValue(thisform.leftfield, false, 1))) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Left Field";
    bool = false;
  }
  if ((splitOF==1)&&(!hasValue(thisform.centerfield, false, 1))) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Center Field";
    bool = false;
  }
  if ((splitOF==1)&&(!hasValue(thisform.rightfield, false, 1))) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Right Field";
    bool = false;
  }
  if ((splitOF==0)&&(!hasValue(thisform["outfield[]"], true, 3))) {
    if (!bool) {
      msg = msg + ", ";
    }
    msg = msg + "Outfield (select 3)";
    bool = false;
  }
  if (!bool) {
   alert(msg);
  }
  return bool;
}


function asFormValidation(thisform,numVotes) {
  var bool = true;
  var isCheck = false;
  var msg = "You must select exactly " + numVotes + " player";

  if (numVotes>1)
  {
	  msg = msg + "s";
	  isCheck=true;
	  if (!hasValue(thisform["players[]"], isCheck, numVotes)) {bool = false;}
  }
  else
  {
	  if (!hasValue(thisform.players, isCheck, 1)) {bool = false;}
  }
  
  if (!bool) {
   alert(msg);
  }
  return bool;
}


function moveGGList(fromElem, toElem, splitOF) {
    var val;
    toElem.value='';

    //Pitcher
    elem=fromElem.pitcher;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    //Catcher
    elem=fromElem.catcher;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    //First Base
    elem=fromElem.firstbase;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    //Second Base
    elem=fromElem.secondbase;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    //Third Base
    elem=fromElem.thirdbase;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    //Shortstop
    elem=fromElem.shortstop;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    if (splitOF==1) {
        //Left Field
        elem=fromElem.leftfield;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
        //Center Field
        elem=fromElem.centerfield;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
        //Right Field
        elem=fromElem.rightfield;
    if (elem.checked)
    {
	    val=elem.value;
	    if (toElem.value == '') {
		    toElem.value = val;
	    }
	    else {
		    toElem.value = toElem.value + '|' + val;
	    }
    }
    else
    {
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
			    val=elem[i].value;
			    if (toElem.value == '') {
				    toElem.value = val;
			    }
			    else {
				    toElem.value = toElem.value + '|' + val;
			    }
		    }
	    }
    }
    }
    else
    {
        //Outfield
        elem=fromElem['outfield[]'];
        for (var i=0; i<elem.length; i++) {
            if (elem[i].checked) {
               val=elem[i].value;
               if (toElem.value == '') {
                   toElem.value = val;
               }
               else {
                  toElem.value = toElem.value + '|' + val;
               }
           }
        }
    }
    return true;
}

function moveASList(fromElem, toElem, numVotes) {
    var val;
    toElem.value='';

    if (numVotes>1)
    {
    	    elem=fromElem['players[]'];
    	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
	     		    val=elem[i].value;
	     		    if (toElem.value == '') {
		 		    toElem.value = val;
	     		    }
	     		    else {
		  		    toElem.value = toElem.value + '|' + val;
	     		    }
	 	    }
    	    }
    }
    else
    {
	    elem=fromElem.players;
	    for (var i=0; i<elem.length; i++) {
		    if (elem[i].checked) {
		 	    val=elem[i].value;
		 	    if (toElem.value == '') {
		     		    toElem.value = val;
		 	    }
		 	    else {
		      		    toElem.value = toElem.value + '|' + val;
		 	    }
	     	    }
	    }
    }
    return true;
}
