// JavaScript Document

var xmlHttp;

function GetXmlHttpObject()	{
		
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		  	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function homeRefresh() {

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url="lib/ajax/next_lesson.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=homeHTML;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
		
	return false;	
}

function homeHTML() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var dataArray = new Array();
		var data = xmlHttp.responseText;
		dataArray = data.split("||");
		
		var nextLink = dataArray[0];
		var recentList = dataArray[1];
		
		document.getElementById("next_task").innerHTML = nextLink;
		document.getElementById("recent_lessons").innerHTML = recentList;
 	}
}

function onBlur() {
	document.body.className = 'blurred';
};
function onFocus(){
	document.body.className = 'focused';
};

function validatePSNominationForm(theForm) {
		
  var reason = "";

  reason += validateEmpty(theForm.first_name);
  reason += validateEmpty(theForm.last_name);
  reason += validateEmail(theForm.student_email);
  reason += validateEmpty(theForm.student_city);
  reason += validateEmpty(theForm.student_state);
  reason += validateEmpty(theForm.grad_year);
  reason += validateEmpty(theForm.reason);
  if(theForm.action == "http://www.preazyprep.com/nomination-submit.php?nommtd=other") {
	  reason += validateEmpty(theForm.nominator_name);
	  reason += validateEmpty(theForm.nominator_position);
	  reason += validateEmpty(theForm.nominator_email);
  }
  reason += validateEmpty(theForm.recaptcha_response_field);
  reason += validateCheckboxPS(theForm.lunch);
        		
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}

function validateUserForm(theForm) {
	
  var reason = "";

  reason += validateEmpty(theForm.name_1);
  reason += validateEmpty(theForm.name_2);
  reason += validateEmail(theForm.email_1);
  reason += validateMatch(theForm.email_1, theForm.email_2);
  reason += validateEmpty(theForm.address_1);
  reason += validateEmpty(theForm.city);
  reason += validateEmpty(theForm.state);
  reason += validateEmpty(theForm.zip);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.school);
  reason += validateUsername(theForm.user);
  reason += validatePassword(theForm.password_1);
  reason += validateMatch(theForm.password_1, theForm.password_2);
  reason += validateEmpty(theForm.recaptcha_response_field);
  reason += validateCheckbox(theForm.agreement);
        		
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 16)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 8) || (fld.value.length > 20)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function trim(s) {
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

function validateMatch(fld1, fld2) {
    var error = "";
 
    if (fld1.value != fld2.value) {
        fld1.style.background = 'Yellow'; 
		fld2.style.background = 'Yellow'; 
        error = "The fields must match.\n"
    } else {
        fld1.style.background = 'White';
		fld2.style.background = 'White';
    }
    return error;  
}

function validateCheckbox(fld) {
    var error = "";
 
    if (fld.checked == false) {
        fld.style.background = 'Yellow'; 
        error = "You must agree to the User Agreement and Privacy Policy before continueing.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCheckboxPS(fld) {
    var error = "";
 
    if (fld.checked == false) {
        fld.style.background = 'Yellow'; 
        error = "The student must be eligable for the National Free and Reduced Lunch Program to be nominated for Project Success.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function checkLoginForm() {
	if(document.loginform.preazyprep_login.value == '' ){
		alert('Please Enter Your User Name');
		document.loginform.preazyprep_login.focus();
		return false;
	}
	if(document.loginform.preazyprep_password.value == '' ){
		alert('Please Enter Your Password');
		document.loginform.preazyprep_password.focus();
		return false;
	}
	return true;
}

function checkLoginPage() {
	if(document.loginpage.preazyprep_login.value == '' ){
		alert('Please Enter Your User Name');
		document.loginpage.preazyprep_login.focus();
		return false;
	}
	if(document.loginpage.preazyprep_password.value == '' ){
		alert('Please Enter Your Password');
		document.loginpage.preazyprep_password.focus();
		return false;
	}
	return true;
}

function openurl() {
	var mypage = 'http://www.preazyprep.com/password-popup.php';
	window.open(mypage,'prod','width=220,height=145','status=no')
}

function openLesson(page) {
	OpenWin = this.open(page,"", "toolbar=no, menubar=no, location=no, height=680, width=900, scrollbars=yes, resizable=yes");
}

function openTest(page) {
	OpenWin = this.open(page,"", "toolbar=no, menubar=no, location=no, height=750, width=975, scrollbars=yes, resizable=yes");
}

function openReview(page) {
	OpenWin = this.open(page,"", "toolbar=no, menubar=no, location=no, height=760, width=960, scrollbars=yes, resizable=yes");
}

function openNotice(page) {
	OpenWin = this.open(page,"", "toolbar=no, menubar=no, location=no, height=250, width=450, scrollbars=yes, resizable=yes");
}

function openPassword() {
	OpenWin = this.open("http://www.preazyprep.com/forgot-password.php","", "toolbar=no, menubar=no, location=no, height=350, width=400, scrollbars=yes, resizable=yes");
}

function getSelectedValue(dropDown) {
	
	var selectedIndex = dropDown.selectedIndex;
	return dropDown.options[selectedIndex].value;
}

function sumGrades(ncaaForm) {

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var scores = new Array();
	
	scores[0] = getSelectedValue(ncaaForm.english_1);
	scores[1] = getSelectedValue(ncaaForm.english_2);
	scores[2] = getSelectedValue(ncaaForm.english_3);
	scores[3] = getSelectedValue(ncaaForm.english_4);
	scores[4] = getSelectedValue(ncaaForm.english_5);
	scores[5] = getSelectedValue(ncaaForm.english_6);
	scores[6] = getSelectedValue(ncaaForm.english_7);
	scores[7] = getSelectedValue(ncaaForm.english_8);

	scores[8] = getSelectedValue(ncaaForm.math_1);
	scores[9] = getSelectedValue(ncaaForm.math_2);
	scores[10] = getSelectedValue(ncaaForm.math_3);
	scores[11] = getSelectedValue(ncaaForm.math_4);
	scores[12] = getSelectedValue(ncaaForm.math_5);
	scores[13] = getSelectedValue(ncaaForm.math_6);

	scores[14] = getSelectedValue(ncaaForm.physical_1);
	scores[15] = getSelectedValue(ncaaForm.physical_2);
	scores[16] = getSelectedValue(ncaaForm.physical_3);
	scores[17] = getSelectedValue(ncaaForm.physical_4);

	scores[18] = getSelectedValue(ncaaForm.additional_1);
	scores[19] = getSelectedValue(ncaaForm.additional_2);

	scores[20] = getSelectedValue(ncaaForm.social_1);
	scores[21] = getSelectedValue(ncaaForm.social_2);
	scores[22] = getSelectedValue(ncaaForm.social_3);
	scores[23] = getSelectedValue(ncaaForm.social_4);

	scores[24] = getSelectedValue(ncaaForm.other_1);
	scores[25] = getSelectedValue(ncaaForm.other_2);
	scores[26] = getSelectedValue(ncaaForm.other_3);
	scores[27] = getSelectedValue(ncaaForm.other_4);
	scores[28] = getSelectedValue(ncaaForm.other_5);
	scores[29] = getSelectedValue(ncaaForm.other_6);
	scores[30] = getSelectedValue(ncaaForm.other_7);
	scores[31] = getSelectedValue(ncaaForm.other_8);

	var count = 0;
	var gradeSum = 0;

	for(var i = 0; i < scores.length; i++) {
		if(scores[i] != 0) {
			gradeSum = gradeSum + parseFloat(scores[i]);
			count++;
		}
	}
	
	var gpa = 0;
	
	if(count != 0) {
		gpa = (gradeSum * 1000) / count;
	}
	
	var url="lib/ajax/ncaa_score.php";
	url=url+"?gpa="+gpa;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=displayScore;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	return false;
}


function displayScore() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var dataArray = new Array();
		var data = xmlHttp.responseText;
		dataArray = data.split("||");
		
		var gpa_score = dataArray[0];
		var act_score = dataArray[1];
		
		document.getElementById("gpa_div").innerHTML = gpa_score;
		document.getElementById("act_div").innerHTML = act_score;
		document.getElementById("signup_prompt").style.visibility = 'visible';
 	}
}

function PSnominationDecision() {

	alert("How a student is nominated has no bearing on whether the student will be chosen to participate in Project Success.");
}

function PSnominateMyself() {
	
	if(document.getElementById("nominate_myself").className == "nominate-selected") {
		return false;
	} else {
		document.getElementById("nominate_myself").className = "nominate-selected";
		document.getElementById("nominate_a_student").className = "nominate-not-selected";
		
		document.getElementById("student_name").innerHTML = "Your Name";
		document.getElementById("student_email").innerHTML = "Your Email Address";
		document.getElementById("student_city").innerHTML = "Your City";
		document.getElementById("nominate_reason").innerHTML = "Why do you want to participate in Project Success? (150 word max)";
		document.getElementById("consent_statement").innerHTML = "I qualify for the National Free and Reduced Lunch Program";
		document.getElementById("tr_nominator_name").style.display = "none";
		document.getElementById("tr_nominator_position").style.display = "none";
		document.getElementById("tr_nominator_email").style.display = "none";
		document.nomination_form.action = "http://www.preazyprep.com/nomination-submit.php?nommtd=self";
	}
}

function PSnominateStudent() {
	
	if(document.getElementById("nominate_a_student").className == "nominate-selected") {
		return false;
	} else {
		document.getElementById("nominate_a_student").className = "nominate-selected";	
		document.getElementById("nominate_myself").className = "nominate-not-selected";
		
		document.getElementById("student_name").innerHTML = "Student's Name";
		document.getElementById("student_email").innerHTML = "Student's Email Address";
		document.getElementById("student_city").innerHTML = "Student's City";
		document.getElementById("nominate_reason").innerHTML = "Why would this student be a good candidate for Project Success? (150 word max)";
		document.getElementById("consent_statement").innerHTML = "This student qualifies for the National Free and Reduced Lunch Program";
		document.getElementById("tr_nominator_name").style.display = "";
		document.getElementById("tr_nominator_position").style.display = "";
		document.getElementById("tr_nominator_email").style.display = "";
		document.nomination_form.action = "http://www.preazyprep.com/nomination-submit.php?nommtd=other";
	}
}
