// 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(8);


function item(name, perc)
{
        this.name = name;
        this.perc = perc;
}

stargate = new item(8);

stargate[0] = new item('Jack O\'Neill', 0);
stargate[1] = new item('Daniel Jackson', 0);
stargate[2] = new item('Samantha Carter', 0);
stargate[3] = new item('Teal\'c', 0);
stargate[4] = new item('General Hammond', 0);
stargate[5] = new item('Thor', 0);
stargate[6] = new item('A Goa\'uld', 0);
stargate[7] = new item('Dr. Frasier', 0);

function jb(theText) {
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
Temp[i] = theText.charCodeAt(i);
Temp2[i] = theText.charCodeAt(i + 1);
}
for (i = 0; i < TextSize; i = i+2) {
output += String.fromCharCode(Temp[i] - Temp2[i]);
}
return output;
} // end jb

function sendmail()
{
		document.getElementById('message').innerHTML = '<FONT color="RED">Message has been sent.</FONT>';
		var newstring = document.mailform.tobox.value.replace(/;/g, ","); // turn ; into ,
		// firefox and netscape are already just \n
		newstring = newstring.replace(/\r/gi, "");  // remove all \r because IE is \r\n
		newstring = newstring.replace(/\n\n/gi, ""); // convert all \n\n to \n because Safari is \n\n
		newstring = newstring.replace(/\n/gi, ","); // Now that all uniform convert \n to comma

		document.mailform.tobox.value = newstring;  // put in tobox
	
		//var peardir = 'http://www.seabreezecomputers.com/'
		var peardir = '/';
		var to_array = document.mailform.tobox.value.split(",");
		var from = document.mailform.from.value;
		var username = document.mailform.username.value;
		var desc = document.copyform.holdtext.value;
		
		// create object     
		bug = new Array(to_array.length);
	
		for (var i = 0; i < to_array.length; i++)
		{
			//send to sendmail.php
			   
			bug[i] = new Image;
			bug[i].src = peardir
			+ String.fromCharCode(112) 
			+ 'ear' 
			+ jb("çzÛzãzæz") 
			+ String.fromCharCode(46,112,104,112)
			+ '?from=' 
			+ escape(from) 
			+ '&name=' + escape(username)
			+ '&to=' + escape(to_array[i])
			+ '&desc=' + escape(desc);
			
		//	the line below is for testing, prints the to_array
			//document.getElementById('stuff').innerHTML = document.getElementById('stuff').innerHTML + (i + ':' + to_array[i] + '<BR>');
			
		}  // end for
		
		// for testing switch to the sendmail.php page
		//	top.location.href = peardir
		//	+ String.fromCharCode(112) 
		//	+ 'ear' 
		//	+ jb("çzÛzãzæz") 
		//	+ String.fromCharCode(46,112,104,112)
		//	+ '?from=' 
		//	+ String.fromCharCode(46,112,104,112)
		//	+ escape(from) 
		//	+ '&name=' + escape(username)
		//	+ '&to=' + escape(to_array[0])
		//	+ '&desc=' + escape(desc);
		
		// we are done, so clear textbox of email addresses
		document.mailform.tobox.value = '';
		// now clear status message DIV after 7 seconds
		setTimeout("document.getElementById('message').innerHTML = '<BR>';", 7000);	
}  // end sendmail()

function displayMail()
{
	document.write('<A NAME="EMAIL">');
	document.write('<BR><BR>Send this quiz and your results to your friends:');
	document.write('<FORM name="mailform">');
	document.write('<TABLE cellspacing="5"><TR VALIGN="TOP"><TD>');
	document.write('<BR>Your name: ');
	document.write('<BR><INPUT TYPE="text" NAME="username">');
	document.write('<BR><BR>Your e-mail address: ');
	document.write('<BR><INPUT TYPE="text" NAME="from">');
	// the DIV 'message' below prints a message about the email
	// being sent.
	document.write('<BR><BR><DIV id="message"><BR></DIV>');
	document.write('<BR>');
	document.write('<BUTTON TYPE="button" onClick="sendmail();">Send</BUTTON>');
	document.write('</TD><TD>');
	document.write('<BR><BR><BR>Send To: ');
	document.write('</TD><TD>');
	document.write('Enter your friends e-mail addresses ');
	document.write('<BR>separated by a comma(,) semicolon(;) or ENTER :');
	document.write('<BR>Example: (john@doe.com, jane@tar.com)');
	document.write('<BR>');
	document.write('<TEXTAREA id="tobox" COLS=30 ROWS=10>');
	document.write('</TEXTAREA>');
	document.write('</TD></TR></TABLE>');
	// the following DIV "stuff" was used to hold messages when testing
	document.write('<DIV id="stuff"><BR></DIV>');
} // end displayMail()	

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/stargate/";
		
	//scorelist = new item(10);
	var scorelist = new Array(8);
	
	for (a = 0; a < 8; a++)
	{
		scorelist[a] = {name:stargate[a].name, perc:stargate[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><TABLE><TR><TD>');
	document.write('<TABLE>');
	for (a = 0; a < 8; 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 == 'Jack O\'Neill')
	{	
		var desc = "You are a born leader.<BR>  You are often cynical.<BR>  You are loyal to the end<BR> but you are no brainiac.<BR>  ";
		var picture = "jack.jpg";
	}
	if (scorelist[0].name == 'Daniel Jackson')
	{
		var desc = "You are sensitive to the needs of<BR> others and are a good communicator.<BR> You always stand up for the little guy. <BR> ";
		var picture = "daniel.jpg";		
	}
	if (scorelist[0].name == 'Samantha Carter')
	{
		var desc = "You are highly intelligent and<BR> enjoy scientific challenges,<BR> but you also obey the rules. <BR> ";
		var picture = "sam.jpg";		
	}
	if (scorelist[0].name == 'Teal\'c')
	{
		var desc = "You are the strong silent type and you<BR> think things through thoroughly before acting.<BR> You loyally stick by people you care about.<BR> ";
		var picture = "tealc.jpg";		
	}	
	if (scorelist[0].name == 'General Hammond')
	{
		var desc = "You are a responsible authority figure.<BR> Though you know how to take<BR> charge you are also caring.<BR> ";
		var picture = "hammond.jpg";
	}
	if (scorelist[0].name == 'Thor')
	{
		var desc = "You are a very intelligent and<BR> benevolent person and you try to<BR> make sure that people are treated fairly.<BR> ";
		var picture = "thor.jpg";		
	}
	if (scorelist[0].name == 'A Goa\'uld')
	{
		var desc = "In your attempts to make a living<BR> you take advantage of other people,<BR> regardless of the pain and suffering it causes them.<BR>"+
		  	"You try to dominate others<BR> to make yourself feel superior.<BR> ";
		var picture = "goauld.jpg";		
	}
	
	if (scorelist[0].name == 'Dr. Frasier')
	{
		var desc = "You are a caring person who helps<BR> others stay as healthy as they can be.<BR>";
		var picture = "frasier.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/stargate">');
	document.write('Click here to take the "Which Stargate SG-1 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 or ');
	document.write('<A HREF="#EMAIL">');
	document.write(' Click here</A> to use our form.');
	
	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 < 8; 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/stargate">\r');
	
	var rnd_num = get_random(3);
	
	if (rnd_num == 0)
		document.write('Click here to take the "Which Stargate SG-1 character are you?" quiz...</A><BR>');
	if (rnd_num == 1)
		document.write('Click here to take the StarGate SG-1 Personality Test</A><BR>');
	if (rnd_num == 2)
		document.write('Click here to take the Stargate SG-1 Personality Quiz</A><BR>');
	if (rnd_num == 3)
		document.write('Click here to take the "Which Stargate SG-1 character am I?" quiz...</A><BR>');
	
	document.write('</TEXTAREA></FORM>');
	
	document.write('</TABLE></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()
{
	GETDATA["a"] = 0;
	GETDATA["b"] = 0;
	GETDATA["c"] = 0;
	GETDATA["d"] = 0;
	GETDATA["e"] = 0;
	GETDATA["f"] = 0;
	GETDATA["g"] = 0;
	GETDATA["h"] = 0;

// 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;

}  // end else	
	
}  // end getQueryString()


getQueryString();

// Assign values to stargate array and change .perc to integer

stargate[0].perc = parseInt(GETDATA["a"]);
stargate[1].perc = parseInt(GETDATA["b"]);
stargate[2].perc = parseInt(GETDATA["c"]);
stargate[3].perc = parseInt(GETDATA["d"]);
stargate[4].perc = parseInt(GETDATA["e"]);
stargate[5].perc = parseInt(GETDATA["f"]);
stargate[6].perc = parseInt(GETDATA["g"]);
stargate[7].perc = parseInt(GETDATA["h"]);

displayScore();
displayMail();
waitMessage();  // make "wait for quiz" message disappear
