// Create a global array that will hold the value of each variable,
// that is in URL keyed by the name of the variable.
var GETDATA = new Array(14);


function item(name, perc)
{
        this.name = name;
        this.perc = perc;
}

startrek = new item(14);

startrek[0] = new item('James T. Kirk (Captain)', 0);
startrek[1] = new item('Spock', 0);
startrek[2] = new item('Leonard McCoy (Bones)', 0);
startrek[3] = new item('Mr. Scott', 0);
startrek[4] = new item('Chekov', 0);
startrek[5] = new item('Uhura', 0);
startrek[6] = new item('Mr. Sulu', 0);
startrek[7] = new item('Jean-Luc Picard', 0);
startrek[8] = new item('Beverly Crusher', 0);
startrek[9] = new item('Data', 0);
startrek[10] = new item('Geordi LaForge', 0);
startrek[11] = new item('Will Riker', 0);
startrek[12] = new item('Worf', 0);
startrek[13] = new item('Deanna Troi', 0);
startrek[14] = new item('An Expendable Character (Redshirt)', 0);


function get_random(num)
{
    // will get a random number between 0 and 1
    // change 1 to another number for higher numbers
	var ranNum = Math.round(Math.random()*num);
    return ranNum;
} // end function get_random()


function sortByPerc(a, b) {
    var x = a.perc;
    var y = b.perc;
	  
    if (x < y) return 1;
  	if (x > y) return -1;
  	if (x == y) return 0;
  	//document.write(x);
  	// reverse the -1 and the 1 to do reverse order list
}  // end function sortByPerc

function ClipBoard() 
{
	Copied = copyform.holdtext.createTextRange();
	Copied.execCommand("Copy");
	copyform.holdtext.focus();
}	

function displayScore()
{
	var dir = "http://www.seabreezecomputers.com/startrek/";
		
	//scorelist = new item(10);
	var scorelist = new Array(14);
	
	for (a = 0; a < 15; a++)
	{
		scorelist[a] = {name:startrek[a].name, perc:startrek[a].perc};
	} 
	

	scorelist.sort(sortByPerc);
	
	
	document.write('<BR>Your results:');
	document.write('<BR><B>You are <FONT SIZE=6>');
	document.write(scorelist[0].name + '</FONT></B>');
	document.write('<TABLE><TR><TD>');
	document.write('<TABLE>');
	for (a = 0; a < 15; a++)
	{
		document.write('<TR><TD>' + scorelist[a].name);
		document.write('</TD><TD><HR ALIGN=LEFT NOSHADE SIZE=4 WIDTH=' + scorelist[a].perc + '>');
		document.write('</TD><TD> ' + scorelist[a].perc + '%</TD></TR>');
	}	
	document.write('</TABLE>');
	document.write('</TD><TD>');
	if (scorelist[0].name == 'James T. Kirk (Captain)')
	{	
		var desc = "You are often exaggerated and over-the-top<BR>  in your speech and expressions.<BR>   You are a romantic at heart and a natural leader.<BR>  ";
		var picture = "kirk.jpg";
	}
	if (scorelist[0].name == 'Spock')
	{
		var desc = "You are skilled in knowledge and logic.<BR> You believe that the needs of the many<BR>  outweigh the needs of the few. <BR> ";
		var picture = "spock.jpg";		
	}
	if (scorelist[0].name == 'Leonard McCoy (Bones)')
	{
		var desc = "You are a pessimistic and bitter doctor, <BR> but you are skilled in the ways of medicine and science. <BR> ";
		var picture = "mccoy.jpg";		
	}
	if (scorelist[0].name == 'Mr. Scott')
	{
		var desc = "You are a fun-loving foreigner with an<BR>  amazing ability to get any job done on time.<BR>   Often described as a \"Miracle Worker\".<BR> ";
		var picture = "scotty.jpg";		
	}	
	if (scorelist[0].name == 'Chekov')
	{
		var desc = "Brash, rash and hasty,<BR>  but everyone loves you.<BR> ";
		var picture = "chekov.jpg";
	}
	if (scorelist[0].name == 'Uhura')
	{
		var desc = "You are a good communicator with a <BR> pleasant soft-spoken voice.<BR> Also a talented singer.<BR> ";
		var picture = "uhura.jpg";		
	}
	if (scorelist[0].name == 'Mr. Sulu')
	{
		var desc = "You are able to master many<BR>  skills such as swords,<BR>  plants and martial arts.<BR> ";
		var picture = "sulu.jpg";		
	}
	if (scorelist[0].name == 'Jean-Luc Picard')
	{
		var desc = "A lover of Shakespeare and other<BR>  fine literature. You have a decisive mind<BR>  and a firm hand in dealing with others.<BR> ";
		var picture = "picard.jpg";	
	}
	if (scorelist[0].name == 'Beverly Crusher')
	{
		var desc = "A good physician and a caring parent.<BR>   You are devoted to your children<BR>  and to your occupation.<BR> ";
		var picture ="beverly.jpg";
	}
	if (scorelist[0].name == 'Data')
	{
		var desc = "Even though you are a genius<BR>  you are always striving to be better.<BR> ";
		var picture = "data.jpg";	
	}
	if (scorelist[0].name == 'Geordi LaForge')
	{
		var desc = "You work well with others and often<BR>  fix problems quickly. Your romantic<BR>  relationships are often bungled.<BR> ";
		var picture = "geordi.jpg";		
	}
	if (scorelist[0].name == 'Will Riker')
	{
		var desc = "At times you are self-centered<BR>  but you have many friends.<BR> "
		+"You love many women, but the right<BR>  woman could get you to settle down.<BR> ";
		var picture = "riker.jpg";		
	}
	if (scorelist[0].name == 'Worf')
	{
		var desc = "You are trained in the art of combat<BR>  and are usually intimidating.<BR> ";
		var picture = "worf.jpg";	
	}
	if (scorelist[0].name == 'Deanna Troi')
	{
		var desc = "You are a caring and loving individual.<BR>  You understand people's emotions and <BR> you are able to comfort and counsel them.<BR> ";
		var picture = "troi.jpg";		
	}
	if (scorelist[0].name == 'An Expendable Character (Redshirt)')
	{
		var desc = "Since your accomplishments are seldom noticed,<BR> "
		+" and you are rarely thought of, you are expendable.<BR> "
		+"That doesn't mean your job isn't important but if you<BR> "
		+"were in Star Trek you would be killed off in the first<BR> "
		+"episode you appeared in.<BR> ";
		var picture = "redshirt.jpg";		
	}
	document.write(desc);
	document.write('<IMG SRC="pics/' + picture + '">');
	document.write('</TD></TR><TR><TD COLSPAN=2>');
	document.write('<A HREF="http://www.seabreezecomputers.com/startrek">');
	document.write('Click here to take the "Which startrek character are you?" quiz...</A><BR>');
	document.write('</TD></TR></TABLE>');
	
	document.write('<P>To email this quiz and your results to your friends ');
	document.write('highlight and copy the results above and paste in an ');
	document.write('email.');
	
	document.write('<P>To put the results of this survey on your website, ');
	document.write('highlight and copy the contents of the textbox below:<BR>');
	
	document.write('<TABLE><TR><TD VALIGN="TOP">');
	document.write('<FORM name="copyform">');
	document.write('<TEXTAREA id="holdtext" onFocus="select()" readonly COLS=65 ROWS=20>');
	
	document.write('Your results:');
	document.write('<BR><B>You are <FONT SIZE=6>');
	document.write(scorelist[0].name + '</FONT></B>\r');
	document.write('<TABLE><TR><TD>');
	document.write('<TABLE>');
	for (a = 0; a < 15; a++)
	{
		document.write('<TR><TD>' + scorelist[a].name);
		document.write('</TD>\r<TD><HR ALIGN=LEFT NOSHADE SIZE=4 WIDTH=' + scorelist[a].perc + '>');
		document.write('</TD><TD> ' + scorelist[a].perc + '%</TD>\r</TR>');
	}	
	document.write('</TABLE>');
	document.write('</TD>\r<TD>');
	
	document.write(desc);  // rewrite description for textbox
	document.write('\r<IMG SRC="' + dir + 'pics/' + picture + '">');
	
	document.write('</TD>\r</TR></TABLE>');
	document.write('<A HREF="http://www.seabreezecomputers.com/startrek">\r');
	
	var rnd_num = get_random(3);
	
	if (rnd_num == 0)
		document.write('Click here to take the "Which Star Trek character are you?" quiz...</A><BR>');
	if (rnd_num == 1)
		document.write('Click here to take the Star Trek Personality Test</A><BR>');
	if (rnd_num == 2)
		document.write('Click here to take the Star Trek Personality Quiz</A><BR>');
	if (rnd_num == 3)
		document.write('Click here to take the "Which Star Trek character am I?" quiz...</A><BR>');
	
	document.write('</TEXTAREA></FORM>');
	
	document.write('</TABLE>');
	
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
	{
		// if Internet Explorer 4 or above	
		document.write('<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>');
	}
	else // if netscape or firefox
	{
		document.write('Click in textbox and type CTRL-C to copy');
	}
	
} // end displayScore()



function getQueryString()
{


// Get the string that follows the "?" in the window's location.
var sGet = window.location.search;
if (sGet) // if has a value...
{
    // Drop the leading "?"
    sGet = sGet.substr(1);
    
    // Generate a string array of the name value pairs.
    // Each array element will have the form "foo=bar"
    var sNVPairs = sGet.split("&");
    
    // Now, for each name-value pair, we need to extract
    // the name and value.
    for (var i = 0; i < sNVPairs.length; i++)
    {
        // So, sNVPairs[i] contains the current element...
        // Split it at the equals sign.
        var sNV = sNVPairs[i].split("=");
        
        // Assign the pair to the GETDATA array.
        var sName = sNV[0];
        var sValue = sNV[1];
        GETDATA[sName] = sValue;
    }  // end for
}  // end if
else
{
	GETDATA["a"] = 0;
	GETDATA["b"] = 0;
	GETDATA["c"] = 0;
	GETDATA["d"] = 0;
	GETDATA["e"] = 0;
	GETDATA["f"] = 0;
	GETDATA["g"] = 0;
	GETDATA["h"] = 0;
	GETDATA["i"] = 0;
	GETDATA["j"] = 0;
	GETDATA["k"] = 0;
	GETDATA["l"] = 0;
	GETDATA["m"] = 0;
	GETDATA["n"] = 0;
	GETDATA["o"] = 0;
}  // end else	
	
}  // end getQueryString()


getQueryString();

// Assign values to startrek array and change .perc to integer

startrek[0].perc = parseInt(GETDATA["a"]);
startrek[1].perc = parseInt(GETDATA["b"]);
startrek[2].perc = parseInt(GETDATA["c"]);
startrek[3].perc = parseInt(GETDATA["d"]);
startrek[4].perc = parseInt(GETDATA["e"]);
startrek[5].perc = parseInt(GETDATA["f"]);
startrek[6].perc = parseInt(GETDATA["g"]);
startrek[7].perc = parseInt(GETDATA["h"]);
startrek[8].perc = parseInt(GETDATA["i"]);
startrek[9].perc = parseInt(GETDATA["j"]);
startrek[10].perc = parseInt(GETDATA["k"]);
startrek[11].perc = parseInt(GETDATA["l"]);
startrek[12].perc = parseInt(GETDATA["m"]);
startrek[13].perc = parseInt(GETDATA["n"]);
startrek[14].perc = parseInt(GETDATA["o"]);

displayScore();
waitMessage();
