/************************
SelectUtilities.js
A collection of select list utilities
**************************/
function selectAllOptions(box)
{
	for(i=0; i < box.options.length; i++)
	{
    
    if(box.options[i].value != "" )
    {
      box.options[i].selected=true;
    }   
    else
    {
      box.options[i].selected=false;

    }
  }
}
function unSelectAllOptions(box)
{
	for(i=0; i < box.options.length; i++)
	{
      box.options[i].selected=false;
  }
}


/******************************
isEmpty
Tells you if a select list has any options with non-blank value
******************************/
function isEmpty(box)
{
  var empty=true;	
	for(i=0; i < box.options.length; i++)
	{
      if(box.options[i].value != "" )
      {
          empty=false;
          break;
      }
	} 
	return empty;
}

/**
 * Tests if at least one value in the given select box is selected.
 */
function isOptionSelected(selectBox) {

    for (i = 0; i < selectBox.options.length; i++) {
	if(selectBox.options[i].selected && selectBox.options[i].value != "") {
	    return true;
	}
    }
    return false;
}

/******************************
getOptionCount
Gets the non-blank option count in a select list
******************************/
function getOptionCount(box)
{
  var ct=0;	
  for(i=0; i < box.options.length; i++)
  {
      if(box.options[i].value != "" )
      {
	ct++;
      }
  } 
  return ct;
}


/******************************
repeat
This takes a string and repeats it by the number specified
str= string
num= number of times to repeat
******************************/
function repeat(str, num)
{
	var out="";
	for(i=1; i <= num; i++)
	{
		out=out+str;
	} 
	return out;
}

/******************************
move
This takes a string and repeats it by the number specified
tbox= select list you're moving to
fbox= select list you're moving from
blanksize= size of blank option at end of select list
******************************/
function moveold(fbox,tbox, blanksize) 
{

	var toindex= tbox.options.length;
	var blanks=0;


	for(var i=0; i<tbox.options.length; i++) 
	{
		if(tbox.options[i].selected)
		{
			toindex=i;
		}
		if(tbox.options[i].value=="")
		{
			removeOption(tbox, (i));
			if(toindex > i)
			{
				blanks++;
			}

		}	
	}
	toindex=toindex-blanks;

	var values = new Array();
	var texts = new Array();
	var deletes = new Array();

	for(var i=0; i<fbox.options.length; i++) 
	{
		if(fbox.options[i].selected)
		{
			values[values.length]=fbox.options[i].value;
			texts[texts.length]=fbox.options[i].text;
			deletes[deletes.length]=i;

		}
		if(fbox.options[i].value=="")
		{
			deletes[deletes.length]=i;
		}	
	}
	for(var i=deletes.length-1; i>=0; i--) 
	{
		removeOption(fbox, deletes[i]);
		
	}
	for(var i=0; i<values.length; i++) 
	{
		var no = new Option();
		no.value = values[i];
		no.text = texts[i];
		addOption(tbox, no, toindex);
		toindex++;
	}

	no = new Option();
	no.value = "";
	no.text = repeat(' ', blanksize);
	tbox.options[tbox.options.length]=no;
	no = new Option();
	no.value = "";
	no.text = repeat(' ', blanksize);

	fbox.options[fbox.options.length]=no;
}

/******************************
addOption
This adds and option to a select list
box= select list you're adding to
option= option you want to add
index= index of where you want to add it
******************************/
function addOption(box, option, index)
{

	//box.options.length=box.options.length+1;

	for(var i=box.options.length; i > index; i--)  
	{
		

		var no = new Option();
		no.value = box.options[i-1].value;
		no.text = box.options[i-1].text;
		box.options[i]=no;

   	}
	box.options[index]=option;

}

/******************************
removeOption
This removes an option to a select list
box= select list you're adding to
index= index of option you want to remove
******************************/
function removeOption(box, index)
{

	for(var i=index; i < box.options.length-1; i++)  
	{
		var no = new Option();
		no.value = box.options[i+1].value;
		no.text = box.options[i+1].text;
		box.options[i]=no;
   	}
	box.options.length = box.options.length-1;

} 


/******************************
insertOption
This inserts an option into a select list from a text box
box= select list you're adding to
optValue= value of option 
optText= text of option
blanksize= size of blank option at end of select list
*****************************/
function insertOption(box, optValue, optText, blanksize)
{
	var toindex= box.options.length;
	var blanks=0;


	for(var i=0; i<box.options.length; i++) 
	{
		if(box.options[i].selected)
		{
			toindex=i;
		}
		if(box.options[i].value=="")
		{
			removeOption(box, (i));
			if(toindex > i)
			{
				blanks++;
			}

		}	
	}
	toindex=toindex-blanks;

	var no = new Option();
	no.value = optValue;
	no.text = optText;
	addOption(box, no, toindex);

	no = new Option();
	no.value = "";
	no.text = repeat(' ', blanksize);
	box.options[box.options.length]=no;


}
/******************************
removeSelected
This removes all selected values from a select list;
box- the select list
blanksize= the size of the blank option list at end of select list
*****************************/

function removeSelected(box, blanksize)
{

	for(var i = box.options.length-1; i >= 0; i--) 
	{
		if(box.options[i].selected)
		{
			removeOption(box, i);
		}
		
	}
	var no = new Option();
	no.value = "";
	no.text = repeat(' ', blanksize);
	box.options[box.options.length]=no;

}


/******************************
moveup
this takes the selected values in the box and moves them up one.
box- the name of the select box
blanksize- the size of the blanks
******************************/
function moveUp(box, blanksize) 
{
	var toindex=-1;
	var opt = new Option();

	for(var i=0; i<box.options.length; i++) 
	{
		if(box.options[i].selected && box.options[i].value != "")
		{
			toindex=i;
			opt.value = box.options[i].value;
			opt.text = box.options[i].text;
			opt.selected=true;
		}
	}
	if(toindex > 0)
	{
		removeOption(box, toindex);
		addOption(box, opt, toindex-1);
	}

}
/******************************
moveup
this takes the selected values in the box and moves them up one.
box- the name of the select box
blanksize- the size of the blanks
******************************/
function moveDown(box, blanksize) 
{
	var toindex=-1;
	var opt = new Option();

	for(var i=0; i<box.options.length; i++) 
	{
		if(box.options[i].selected && box.options[i].value != "")
		{
			toindex=i;
			opt.value = box.options[i].value;
			opt.text = box.options[i].text;
			opt.selected=true;
		}
	}
	if(toindex > -1 && toindex < box.options.length-1)
	{
		removeOption(box, toindex);
		addOption(box, opt, toindex+1);

	}

}




/******************************
moveall
this takes everything from one box and appends it to the other
******************************/
function moveAll(from, to) {
    moveAllOptions(from, to, false);
}

function moveAllOptions(from, to, sortTo) {

    if(to.options.length > 0 && to.options[to.options.length-1].value=="") {
	removeOption(to, to.options.length-1);
    }
    for(var i=0; i<from.options.length; i++) {
	opt= new Option();
	opt.value = from.options[i].value;
	opt.text = from.options[i].text;
	opt.selected = true;
	if(opt.value != "" && opt.text != "") {
	    to.options[to.options.length]=opt;
	}
		
    }
    
    from.options.length=0;

    if (sortTo) sortSel(to);
}

function move(fbox,tbox, blanksize) {
    moveOption(fbox, tbox, false);
}

function moveOption(fbox, tbox, sortTo) {

    if(tbox.options.length > 0 && tbox.options[tbox.options.length-1].value=="") {
	tbox.options.length=tbox.options.length-1;
    }

    // move option to the end of the box
    var toindex= toindex=tbox.options.length;

    for(var i=0; i<fbox.options.length; i++) {
	if(fbox.options[i].selected) {
	    var no = new Option();
	    no.value = fbox.options[i].value;
	    no.text = fbox.options[i].text;
	    no.selected = true;
	    addOption(tbox, no, toindex);
	    toindex++;
	    fbox.options[i]=null;
	    i--;
	}
    }

    if (sortTo) sortSel(tbox);
}

// sorts the options from a select
function sortSel(theForm) {

     var tempArray =[];
     for (var i = 0; i < theForm.options.length; i++) {
          tempArray[tempArray.length] = new Option(
	      text = theForm.options[i].text, 
	      value = theForm.options[i].value,
	      selected = theForm.options[i].selected
	  );
     }
     tempArray.sort(
         function(a,b) { 
	     if ((a.text+"") < (b.text+"")) { 
		 return -1; 
	     }
	     if ((a.text+"") > (b.text+"")) { 
		 return 1; 
	     }
	     return 0;
         } 
     );

     for (var i=0; i < tempArray.length; i++) {
	 theForm.options[i]=new Option(
             text = tempArray[i].text, 
	     value = tempArray[i].value,
	     selected = tempArray[i].selected
	 );
     }
}

function selectValue(box,val) 
{
	for(var i=0; i<box.options.length; i++) 
	{
		if(box.options[i].value==val)
		{
      box.options[i].selected=true;
		}
    else
		{
      box.options[i].selected=false;
		}
    
	}
}
function validEmail(myString)
{
  
  var regex = /^[a-zA-Z0-9._!#$%^&*+='-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(myString);
  
}

function urlEncode(txt) 
{                             
   var newtxt='';
   if( typeof(txt) == "string" && txt !=null) 
   {
      for(i=0; i< txt.length; i++)
      {
        var str;
        if(txt.charCodeAt() >32)
        {
          str=escape(txt.charAt(i));
        }
        else
        {
          str=txt.charAt(i);
        }
        newtxt=newtxt+str;
      }
   }   
   return newtxt;
}
