package JScript;
use strict;
use vars qw (@ISA @EXPORT_OK);
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK = qw(Move MoveAll BumpUp Up Down SortAsc SortDesc 
                ExptsetArrays Relate SelectAll DeleteOption 
                RelateConc RelateTime RelateDefault WinOpen);

###################################################################################
#
# Author : Gavin Sherlock
# Date   : 31st March 2000
#
# This module provides functions that will return strings that encode specific
# JavaScript functionality.
#
###################################################################################


###################################################################################
sub Move{
###################################################################################
# This function allows a user to Move a selected entry in one box to another box.
# This function MUST also be combined with a call to BumpUp

    return <<EOF;
    function move(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
	    if(fbox.options[i].selected && fbox.options[i].value != "") {
		var no = new Option();
		no.value = fbox.options[i].value;
		no.text = fbox.options[i].text;
		tbox.options[tbox.options.length] = no;
		fbox.options[i].value = "";
		fbox.options[i].text = "";
	    }
	}
	BumpUp(fbox);
    }

EOF

}


##################################################################################
sub MoveAll{
##################################################################################
# This functions moves all the contents of one box into another box.
# This function MUST also be combined with a call to BumpUp

    return <<EOF;
    function MoveAll(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
	    var no = new Option();
	    no.value = fbox.options[i].value;
	    no.text = fbox.options[i].text;
	    tbox.options[tbox.options.length] = no;
	    fbox.options[i].value = "";
	    fbox.options[i].text = "";
	}
	BumpUp(fbox);
	tbox.focus();
    }

EOF

}

###################################################################################
sub BumpUp{
###################################################################################
# This function Bumps up the contents of a box, after one element has been removed

    return <<EOF;
    function BumpUp(box)  {
	for(var i=0; i<box.options.length; i++) {
	    if(box.options[i].value == "")  {
		for(var j=i; j<box.options.length-1; j++)  {
		    box.options[j].value = box.options[j+1].value;
		    box.options[j].text = box.options[j+1].text;
		}
		var ln = i;
		break;
	    }
	    
	}

	if(ln < box.options.length)  {
	    box.options.length -= 1;
	    BumpUp(box);
	}
    }

EOF

}

###################################################################################
sub Up{
###################################################################################
# This function will move the selected entry(s) in a box up one position
# If the top entry is selected, it will not move.  If the top entry, plus other
# entries are selected, the other entries WILL not move either.

    return <<EOF;
    function Up(box)  {
	if(!box.options[0].selected) {
	    for(var i=1; i<box.options.length; i++) {
		if(box.options[i].selected) {
		    if (i==0) { break }
		    var tempv = box.options[i-1].value;
		    var tempt = box.options[i-1].text;
		    box.options[i-1].value = box.options[i].value;
		    box.options[i-1].text = box.options[i].text;
		    box.options[i-1].selected = true;
		    box.options[i].selected = false;
		    box.options[i].value = tempv;
		    box.options[i].text = tempt;
		}
	    }
	}
    }

EOF

}

###################################################################################
sub Down{
###################################################################################
# This function will move the selected entry(s) in a box down one position
# If the bottom entry is selected, it will not move.  If the bottom entry, plus other
# entries are selected, the other entries WILL not move either.

    return <<EOF;
    function Down(box)  {
	if (!box.options[box.options.length-1].selected) {
	    for(var i = box.options.length-1; i>=0; i--) {
		if(box.options[i].selected) {
		    if (i == box.options.length-1) { break };
		    var tempv = box.options[i+1].value;
		    var tempt = box.options[i+1].text;
		    box.options[i+1].value = box.options[i].value;
		    box.options[i+1].text = box.options[i].text;
		    box.options[i+1].selected = true;
		    box.options[i].selected = false;
		    box.options[i].value = tempv;
		    box.options[i].text = tempt;
		}
	    }
	}
    }

EOF

}

###################################################################################
sub SortAsc{
###################################################################################
# This function will the the contents of the box in Ascending Order, based on a text
# sort.  It sets up a temporary array of objects, into which are put the entries in
# sorted order.  The temporary array is then copied over into the box array.

    my $JScript = &MakeSort("SortAsc", ">");

    return $JScript;

}

#####################################################################################
sub SortDesc{
#####################################################################################
# This function will the the contents of the box in Descending Order, based on a text
# sort.  It sets up a temporary array of objects, into which are put the entries in
# sorted order.  The temporary array is then copied over into the box array.

    my $JScript = &MakeSort("SortDesc", "<");

    return $JScript;

}

#####################################################################################
sub MakeSort{
#####################################################################################
# This subroutine actually makes a sort routine, depending on the operator, the sort
# will be either Ascending or Descending

    my ($name, $operator) = @_; 

    return <<EOF;
    function $name(box)  {
	var temp_opts = new Array();
	var temptext = new Object();
        var tempval = new Object();
 
	for(var i=0; i<box.options.length; i++)  {
	    temp_opts[i] = box.options[i];
	}
	
	for(var x=0; x<temp_opts.length-1; x++)  {
	    for(var y=(x+1); y<temp_opts.length; y++)  {
		if(temp_opts[x].text.toLowerCase() $operator 
                   temp_opts[y].text.toLowerCase())  {
		    temptext = temp_opts[x].text;
                    tempval = temp_opts[x].value;
		    temp_opts[x].text = temp_opts[y].text;
                    temp_opts[x].value = temp_opts[y].value;
		    temp_opts[y].text = temptext;
                    temp_opts[y].value = tempval;
		}
	    }
	}
	
	for(var i=0; i<box.options.length; i++)  {
	    box.options[i].value = temp_opts[i].value;
	    box.options[i].text = temp_opts[i].text;
	}
    }

EOF

}



####################################################################################
sub SelectAll{
####################################################################################
# This subroutine sets the selected parameter to true for every member of a scrolling
# list that is passed in

    return <<EOF;
    function SelectAll(box){
	for(var i=0; i<box.options.length; i++) {
	    box.options[i].selected = true;
	}
    }

EOF

}



######################################################################
sub WinOpen
######################################################################
# this function opens a popup of defined width and size
# not currently used?
{



    return <<EOF;

	if (document.layers)
	    isNS = true
	else
	    isNS = false
      	if (document.all)
    	    isIE = true
	else
	    isIE = false
	if (isNS)
            var gBorder = 20; // Minimum image border to avoid scrollbars in Netscape
       	else
	    var gBorder = 25; // Minimum image border to avoid scrollbars in IE 


        function WinOpen( argWidth, argHeight, argURL, argTitle) {

	    var title = argTitle;
	    var FullURL = argURL;
	    var gWidth  = argWidth + gBorder;  // popup width and height
	    var gHeight = argHeight + gBorder;
	    winFeatures = "resize=no,width="+gWidth+",height="+gHeight+",screenX=0,screenY=10,top=20,left=40";
 

	    var popup = window.open("","FULL",winFeatures);
	    popup.document.write("<HTML><HEAD><TITLE>"+title+"</TITLE></HEAD>");
	    popup.document.write("<FRAMESET Rows=*,45 BORDER=0>");
	    popup.document.write("<FRAME Name=def SRC="+FullURL+" Scrolling=No>");
	    popup.document.write("<FRAME Name=close SRC="http://www-genome.stanford.edu/microarray/closewindow.html" Scrolling=No>");
	    popup.document.write("</FRAMESET>");
	    popup.document.write("</HTML>");
	    popup.document.close();
	    popup.focus();

	}


    }


EOF

}


1;
