//
// Piazzza Splash Page Script
// ==========================
// Author: Chris Lewis
// Date: July 20, 2010
//

$(document).ready(function() {
	
	/*
	 * Variables
	 */
	var whiteColor    = '#FFFFFF';
	var invalidColor  = '#FFE4E4';
	var emailField    = $("input#email");
	var classField    = $("input#class");
	var sampleClasses = "e.g. CS110, AP Microeconomics";
	var loginEmailField = $("input#login-email");
	var loginPwordField = $("input#login-password");
	
	
	/*
	 * Image Rollover Effect
	 */
	$(".rollover").hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    });
	
	/*
	 * Sample Text Handling (in the Class field)
	 */
	classField.val(sampleClasses);
	
	classField.focus(function()
	{
		// Clear the sample text.
		if (classField.val() == sampleClasses) {
			classField.val("");
		}
	});
	
	classField.blur(function()
	{
		// Replace sample text, if necessary.
		if (classField.val() == "") {
			classField.val(sampleClasses);
		}
	});
	
	
	/*
	 * Signup Form Handling
	 */
	$("#request-invite").click(function()
	{ 	
		// Validate the form.
		var valid = validateForm();
		if (!valid) { return false; }

        var roleField     = $("input[name='role']:checked");
        var whereField    = $("input[name='where']:checked");

		// Build a data string.
		var email  = emailField.val(); 
		var course = classField.val(); // using "course" because "class" is a reserved keyword in IE >:(
		var role   = roleField.val();		
        var where  = whereField.val();
		var dataString = '&email=' + email  +
						 '&class=' + course +
						 '&role='  + role   +
						 '&where=' + where;
		
        if (!role) {
          alert("Please specify whether you are a student or a professor");
          return false;
        }
        if (!where) {
          alert("Please specify whether you are from Stanford");
          return false;
        }
		// Send a POST request.
		$.ajax({
			type: 	 "POST",  
			url:  	 "settings/splash_apply",  
			data: 	 dataString,  
			success: function(data) {
				$('form').hide();
				$("#signup").html("<h2>Thank you!</h2>" + "<p align=\"left\">Thanks, we received your information! In the meanwhile, feel free to drop us a note at team@piazzza.com. We'd love to hear from you!</p>");
				
                if (data == 'exists') {
                	location.href='/settings/login?email=' + email;
                }
			}
		});
		
		return false;
	});
	
	
	/* 
	 * Function: 	validateForm(emailField, classField)
	 * Return Type: Boolean
	 * ==================================
	 * This function returns true if the value in each form field is
	 * valid. Otherwise, it returns false and alerts the user where 
	 * the problem is. (Note: This function only validates the email
	 * and course name fields at present.)
	 */
	function validateForm()
	{
		// Case 2: Email field is invalid.
		if (entryIsInvalid(emailField)) {
			alert("Please enter a valid email address!");
			emailField.css('background-color', invalidColor);
			emailField.focus();
			return false;  
		}
		
		// Case 4: All fields are valid!
		return true;
	}


	/* 
	 * Function: 	entryIsInvalid(field)
	 * Return Type: Boolean
	 * ==================================
	 * This function returns true if the value in the given field is
	 * empty or invalid. Otherwise, it returns false. (Note: This function
	 * only validates the email and course name fields at present.)
	 */
	function entryIsInvalid(field)
	{
		var entry = field.val();
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		
		// Case 1: Field is empty.
		if (entry == "") {
			return true;
		}
		
		// Case 2: Email address is invalid.
		if (field == emailField) {
			if (entry.search(emailRegEx) < 0)
				return true;
		}
		
		// Case 3: No class name entered.
		if (field == classField) {
			if (entry == sampleClasses)
				return true;
		}
		
		// Case 4: The entry is valid!
		return false;
	}

	/*
	 * Login Panel Handling
	 */
	/*$("#login h2").hide();
	$("#login form").hide();
	$("#login-button").hide();
	
	$("#toggle-button").toggle(function() {
		$("#signup").hide();
		$("#already-text").hide();
		$("#login h2").show();
		$("#login form").show();
		$("#login-button").show();
		$("#toggle-button").html("Cancel");
		$("#login").css('text-align', 'right');
		$("#login a").css('line-height', '35px');
	}, function() {
		$("#signup").show();
		$("#already-text").show();
		$("#login h2").hide();
		$("#login form").hide();
		$("#login-button").hide();
		$("#toggle-button").html("Login");
		$("#login").css('text-align', 'center');
		$("#login a").css('line-height', 'inherit');
	});
	
	$("#login-button").click(function() {
		//
		// location.href='/settings/login?email=' + loginEmailField.val();
		//
		// Renars, I'm not quite sure how to craft this data string. Would you
		// mind handling this one? :) We can also reuse the entryIsInvalid function
		// to verify that the email provided is a valid one.
		//
		// - Chris L., 7/20/10
		//
	});*/
	
});