var output = '';
var ACTorSAT = '';
var error = false;

window.addEvent('domready', function() {
$('scholarship-form').addEvent('submit', function(event) { 
	var error = false;
	
	output = '';
	new Event(event).stop();
	
	if($('instate').get('checked')) {
		var iostate = 'instate';
	} else if($('outofstate').get('checked')){
		var iostate = 'outofstate';	
	}
	
	var gpa =  $('gpa').get('value');
	var gpa2 =  $('gpa2').get('value');
	
	if(gpa2 > 4) {
	    var gpaConv = 4/gpa2;
		gpa = gpa * gpaConv;
		gpa = Math.round(gpa*100)/100;
	}
		
	var act = $('act').get('value');
	var sat = $('sat').get('value');
	var classRank1 = $('classRank1').get('value');
	var classRank2 = $('classRank2').get('value');
	
	
	var userGPA = gpa;
	var userACT = $('act').get('value');
	var userSAT = $('sat').get('value');

	var userClassRank1 = $('classRank1').get('value');
	var userClassRank2 = $('classRank2').get('value');

    var option = 0;
	
	if(iostate) {
		option = 1;	
	}

    if(option != 1) {
        error = true;
		return false;
    }

  	if(userGPA >= 5.00)  {  
        error = true;
		return false;
		
    } else if(0 > userGPA) {
        error = true;
		return false;
		
    } else if(userGPA > 4.00) {
		if(5.00 > userGPA) {
			gpa = 4.00;
		}
    }

    if(userACT == "") {
		if(userSAT == "") {
        	error = true;
			return false;
		}
    }
	
    if(userACT != "") { 
        if(0 > userACT) { 
            error = true;
			return false;
        } else if(userACT > 36) {
            error = true;
			return false;
        }
		ACTorSAT = "act";

    }
    else if(userACT == "") {
		if(userSAT != "") {
			if(400 > userSAT) {
				error = true;
				return false;
			} else if(userSAT > 1600) {
				error = true;
				return false;
			} 
			ACTorSAT = "sat";
		}
    } 
	
    if(userClassRank1 == "") {
		userClassRank1 = 1;
    } else if(userClassRank2 == "") {
		userClassRank2 = 1;
    } else if(0 > userClassRank1) {
		error = true;
		return false;
    } else if(userClassRank2 > 1) {
		if(40 > userClassRank2) {
			classRank1 = 1;
			classRank2 = 1;
		}
		
    } else if((userClassRank1 - userClassRank2) > 0) {
        error = true;
		return false;
    } else if(userClassRank1 == 1) {
          if(userClassRank2 == 1) {
			  
		  }
	} 
	
	var measure1 = gpa * 25;
	var measure2 = 0; 
	var measure3 = 0;
	var studentMeasure = 0;
	var award = 0;
	
	if(!classRank1) {
		classRank1 = 1;	
	}
	if(!classRank2) {
		classRank2 = 1;	
	}

	measure2 = ((100 * (classRank2 - classRank1)) / classRank2);

	if(ACTorSAT == "undefined") {
    	alert("There has been an internal error calculating test score information");
	} else if(ACTorSAT == "act") {
    	measure3 = act * 2.77778;
	} else {
    	measure3 = (sat * 0.0625);
	}

	if(classRank1 == 1 && classRank2 == 1) {
		studentMeasure = measure1 * 1.5 + measure3 * 1.5;
		var smallClass = true;
	} else {
		studentMeasure = measure1 + measure2 + measure3;
	}

	if(iostate == "instate") {
		if(studentMeasure >= 270) {
			award = 4000;
		} else if(studentMeasure >= 245 && studentMeasure < 270) {
			award = 2000;
		} else if(studentMeasure >= 225 && studentMeasure < 245) {
			award = 1000;
		}
	} else if(iostate == "outofstate") {
	    if(studentMeasure >= 270) {
        	award = 12000;
    	} else if(studentMeasure >= 245 && studentMeasure < 270) {
	        award = 10000;
    	} else if(studentMeasure >= 225 && studentMeasure < 245) {
	        award = 7000;
    	}
	}
		
	output += ("<p><b>Information Entered:</b><br>");
	output += ("<b>Residency:</b> ");
	
	if(iostate == "instate") {
		output += ("Michigan Resident<br>");
	} else {
		output += ("US (non-Michigan) Resident or Resident of Canadian Province or Territory<br>");
	}
	
	output += ("<b>GPA:</b> " + gpa + "<br>");
	output += ("<b>Test Scores:</b> ");
	
	if(ACTorSAT == "act") {
		output += ("ACT with composite score of " + act + "<br>");
	} else {
		output += ("SAT with combined score of " + sat + "<br>");
	}
	
	if(!smallClass) {
		output += ("<b>Class Rank:</b> " + classRank1 + " out of " + classRank2 + " students.");
	} 
	
	output += ("<br><br></p>");
	
	if(award != 0) {
		if(iostate == "instate") {
			if(award == 4000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>Presidential Scholarship of Excellence</b>. Your award would likely be for <b>$4,000 a year</b>--renewable for four years! $16,000! Now that is not an amount to be taken lightly. You've worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded. It's serious money, because we're serious about you.");
			} else if(award == 2000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>Presidential Scholarship of Distinction</b>. Your scholarship award would likely be for <b>$2,000 a year</b>--renewable for four years! You've worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded.");
			} else if(award == 1000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>Presidential Scholarship of Achievement</b>. Your scholarship award would likely be for <b>$1,000 a year</b>--renewable for four years! You've worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded.");
			}
		} else if(iostate == "outofstate") {
			if(award == 12000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>National Scholarship of Excellence</b>. Your scholarship award would likely be for <b>$12,000 a year</b>--renewable for four years! $48,000! Now that is not an amount to be taken lightly. You have worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded. It's serious money, because we're serious about you.");
			} else if(award == 10000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>National Scholarship of Distinction</b>. Your scholarship award would likely be for <b>$10,000 a year</b>--renewable for four years! $40,000! Now that is not an amount to be taken lightly. You have worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded. It's serious money, because we're serious about you.");
			} else if(award == 7000) {
				output += ("<p>Good news! Based on our estimations, you would be eligible for a <b>National Scholarship of Achievement</b>. Your scholarship award would likely be for <b>$7,000 a year</b>--renewable for four years! $28,000! Now that is not an amount to be taken lightly. You have worked hard to develop your skills, and at Michigan Tech, we believe you should be rewarded. It's serious money, because we're serious about you.");
			}
		}
		
		output += ("<p>We know that as a high-achiever, you have a lot of options as to where to go to college--a lot of very good options. While you're sure to excel anywhere you go, we're pretty confident that you'd feel most at home with us. Michigan Tech has a proud tradition of attracting hard-working, motivated, and adventurous students, and we're known as a place where it is cool to be smart. We think that going to college among other students who also demand excellence both of themselves and their institution will help you to ensure that you maintain the high standards that you have kept for yourself so far.</p><p>Because the criteria are rather involved, we can't guarantee you an award until you see your letter in the mail. However, when you do get that letter, it will have been worth the wait.</p><p>We realize you have many gifts and talents, most of which can't be measured by GPA, test scores, and class rank. We're committed to doing all that we can to make it possible for you to develop your talents here at Michigan Tech. <a href=\"http://www.mtu.edu/finaid/students/prospective/\">Click here</a> to check out the many additional sources of financial aid to you.</p><p>To begin the process for other forms of financial aid, complete the Free Application for Federal Student Aid, (FAFSA) at <A href=\"http://www.fafsa.ed.gov\">www.fafsa.ed.gov</a>. Questions? Don't hesitate to contact our <a href=\"http://www.mtu.edu/finaid\">Office of Financial Aid</a> toll free at 888-MTU-1885.</p><p>Learn more at <a href=\"http://www.mtu.edu/finaid\">www.mtu.edu/finaid/</a>.</p>");
	} else if(award == 0) {
	
		if(iostate == "instate") {
			output += ("<p>We know you're smart, or you wouldn't be considering Michigan Tech in the first place. While it looks like the information you entered will not qualify you for the Presidential Scholars Program, it is only one of many sources of financial aid.</p>");
		} else if(iostate == "outofstate") {
			output += ("<p>We know you're smart, or you wouldn't be considering Michigan Tech in the first place. While it looks like the information you entered will not qualify you for the National Scholars Program, it is only one of many sources of financial aid.</p>");
		}
		
		output += ("<p>Remember, this is only an estimate. Whatever you do, don't let it stop you from applying to or considering Michigan Tech.</p><p>Michigan Tech is committed to making it possible for qualified students to attend, regardless of financial circumstances. More than 80 percent of our students receive some form of financial assistance, which comes in the form of scholarships, grants, student loans, and part-time student employment. In fact, each year Michigan Tech students collectively receive approximately $50 million annually in financial assistance.</p><p>You have many gifts and talents, most of which can't be measured by GPA, test scores, and class rank. We're committed to doing all that we can to make it possible for you to develop your talents here at Michigan Tech. <a href=\"http://www.mtu.edu/finaid/students/prospective/\">Click here</a> to check out many additional sources of financial aid available to you.</p><p>To begin the process for other forms of financial aid, complete the Free Application for Federal Student Aid, (FAFSA) at <A href=\"http://www.fafsa.ed.gov\">www.fafsa.ed.gov</a>. Questions? Don't hesitate to contact our <a href=\"http://www.mtu.edu/finaid\">Office of Financial Aid</a> toll free at 888-MTU-1885.</p><p>Learn more at <a href=\"http://www.mtu.edu/finaid\">www.mtu.edu/finaid/</a>.</p>");
	
	}
	
	if(error != true) {
		$('results').set('html', output);
		new Asset.css('http://www.mtu.edu/mtu_resources/styles/squeezebox.css');
		new Asset.javascript('http://www.mtu.edu/mtu_resources/script/squeezebox.js', {onload: function() {
			SqueezeBox.initialize();
			SqueezeBox.setOptions({size: {x: 700, y: 400}}).setContent('clone', $('results'));
		}});	
	} else {
		error = false;	
	}
	});
});