var img_ok='<img src="/admission/images/icons/tick.png" alt="" />';
var img_err='<img src="/admission/images/icons/error.png" alt="" />'

var errortext = [];
errortext['affirm_us'] = 'This program is only available to US citizens/permanent residents who are residing within the United States.';
errortext['fname'] = 'we need your first name';
errortext['lname'] = 'we need your last name';
errortext['email'] = 'we need a valid email address';
errortext['phone'] = 'we need at least one phone number';
errortext['cellphone'] = 'we need at least one phone number';
errortext['address'] = 'we need your residential address';
errortext['city'] = 'we need your city of residence';
errortext['state'] = 'we need your state of residence';
errortext['zip'] = 'we need your zipcode';
errortext['highschool'] = 'we need to know what school you are attending';
errortext['gradyear'] = 'we need to know when you will graduate';
errortext['ethnicity'] = 'we need to know your ethnicity';
errortext['language'] = 'we need to know your first language';
errortext['gender'] = 'we need to know your gender to provide overnight hosting';
errortext['visited'] = 'we need to know whether you have visited us before';
errortext['household'] = 'we need to know the size of your family';
errortext['familyincome'] = 'we need to know your family\'s income';
errortext['gpa'] = 'we need to know your current GPA';
errortext['satcr'] = 'we need all three SAT scores (CR,M,W) or ACT';
errortext['satm'] = 'we need all three SAT scores (CR,M,W) or ACT';
errortext['satw'] = 'we need all three SAT scores (CR,M,W) or ACT';
errortext['act'] = 'we need all three SAT scores (CR,M,W) or ACT';
errortext['classes'] = 'we need to know what classes you are taking';
errortext['whywhitman'] = 'This answer is essential.';
errortext['whydiverse'] = 'This answer is essential.';


function checkinput(fieldname,noreq) {
	if(($("#input_"+fieldname).val() != '' && !(fieldname == 'affirm_us'))  || noreq == true ) {
		if (noreq == true)
		{
			$("#hint_"+fieldname).html('');
		} else {
			$("#hint_"+fieldname).html(img_ok);
		}
		if(fieldname == 'classes' || fieldname == 'whywhitman' || fieldname == 'whydiverse') {
			$("#input_"+fieldname).css({'background-color':'white'});
			$("#hint_"+fieldname).html(img_ok);
		}
		$("#input_"+fieldname).removeClass('errorfield');
		returnval = true;
		if (fieldname == 'email') {
			var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
			var regex = new RegExp(emailReg);
			returnval = returnval && regex.test($("#input_email").val());
			if(returnval == false) {
				$("#input_"+fieldname).addClass('errorfield');
				$("#hint_"+fieldname).html(img_err + ' ' + errortext[fieldname]);
				$("#input_email").addClass('errorfield');
			}
		}
	} else if (fieldname == "affirm_us") {
		if ($("#input_"+fieldname).is(":checked")) {
			$("#affirm_us_text").css({'background-color':'#ffea8f'});
			$("#hint_"+fieldname).html(img_ok);
			returnval = true;
		} else {
			$("#affirm_us_text").css({'background-color':'#ffea8f'});
			$("#hint_"+fieldname).html(img_err + ' ' + errortext[fieldname]);
			$("#input_"+fieldname).addClass('errorfield');
			returnval = false;
		}
	} else {
		$("#hint_"+fieldname).html(img_err + ' ' + errortext[fieldname]);
		$("#input_"+fieldname).addClass('errorfield');
		if(fieldname == 'classes' || fieldname == 'whywhitman' || fieldname == 'whydiverse') {
			$("#input_"+fieldname).css({'background-color':'#ffea8f'});
		}
		returnval = false;
	}
	return returnval;
}


function checkphone() {
	if($("#input_phone").val() == '' && $("#input_cellphone").val() == '') {
		$("#hint_phone").html(img_err + " at least one phone number is required");
		$("#input_cellphone").addClass('errorfield');
		$("#input_phone").addClass('errorfield');
		$("#hint_cellphone").html(img_err + " at least one phone number is required");
		returnval = false;
	} else {
		$("#input_phone").removeClass('errorfield');
		$("#input_cellphone").removeClass('errorfield');
		if ($("#input_phone").val() != '') {
			$("#hint_phone").html(img_ok);
		} else {
			$("#hint_phone").html('');
		}
		if ($("#input_cellphone").val() != '') {
			$("#hint_cellphone").html(img_ok);
		} else {
			$("#hint_cellphone").html('');
		}
		returnval = true;
	}
	return returnval;
}

function checkgender() {
	if (!($("#input_genderF:checked").val() || $("#input_genderM:checked").val() ))
	{
		$("#hint_gender").html(img_err + ' ' + errortext['gender']);
		$("#inputs_gender").addClass('errorfield');
		returnval = false;
	} else {
		$("#hint_gender").html(img_ok);
		$("#inputs_gender").removeClass('errorfield');
		returnval = true;
	}
	return returnval;
}


function checkvisited() {
	if (!($("#input_visitedN:checked").val() || $("#input_visitedY:checked").val() ))
	{
		$("#hint_visited").html(img_err + ' ' + errortext['visited']);
		$("#inputs_visited").addClass('errorfield');
		returnval = false;
	} else {
		$("#hint_visited").html(img_ok);
		$("#inputs_visited").removeClass('errorfield');
		returnval = true;
	}
	return returnval;
}

function checkscores() {
	if (($("#input_satcr").val() != '' && $("#input_satm").val() != '' && $("#input_satw").val() != '' ) || ($("#input_act").val() != '' ))
	{
		jQuery.each(['satcr','satm','satw','act'], function() {
			$("#input_"+this).removeClass('errorfield');
			if ($("#input_"+this).val() != '')
			{
				$("#hint_"+this).html(img_ok);
			} else {
				$("#hint_"+this).html('');
			}
		});
		returnval = true;
	} else {
		jQuery.each(['satcr','satm','satw','act'], function() {
			$("#input_"+this).addClass('errorfield');
			$("#hint_"+this).html(img_err + ' ' + errortext[this]);
		});
		returnval = false;
	}
}

function validateform() {
	formok = true;
	formok = checkinput('affirm_us') && formok;
	formok = checkinput('fname') && formok;
	formok = checkinput('lname') && formok;
	formok = checkinput('email') && formok;
	formok = checkphone() && formok;
	formok = checkinput('address') && formok;
	formok = checkinput('city') && formok;
	formok = checkinput('state') && formok;
	formok = checkinput('zip') && formok;
	formok = checkinput('highschool') && formok;
	formok = checkinput('gradyear') && formok;
	formok = checkinput('ethnicity') && formok;
	formok = checkinput('language') && formok;
	formok = checkgender() && formok;
	formok = checkvisited() && formok;
	formok = checkinput('household') && formok;
	formok = checkinput('familyincome') && formok;
	formok = checkscores && formok;
	formok = checkinput('classes') && formok;
	formok = checkinput('whywhitman') && formok;
	formok = checkinput('whydiverse') && formok;
	if(formok) {
		$("#formerror").hide();
	} else {
		$("#formerror").show();
		scrollTo(0, 0);

	}
	return(formok);
}


function word_count(field, count) {
    var number = 0;
    var matches = $("#"+field).val().match(/\b/g);
    if(matches) {
        number = matches.length/2;
    }
    $("#"+count).text('~' + number + ' word' + (number != 1 ? 's' : ''));
	if(number > 0) {
		$("#"+count).show();
	} else {
		$("#"+count).hide();
	}
}


$(function(){
	if ($("#input_fname").length > 0) {
		$('input[type="text"]').focus(function() {
			$(this).removeClass("errorfield");
			$(this).addClass("focusfield");
		});
		$('input[type="text"]').blur(function() {
			$(this).removeClass("focusfield");
		});
		$('textarea').focus(function() {
			$(this).removeClass("errorfield");
			$(this).addClass("focusfield");
		});
		$("#input_genderF").focus(function() {
			$("#inputs_gender").removeClass("errorfield");
			$("#inputs_gender").addClass("focusfield");
		});
		$("#input_genderF").blur(function() {
			$("#inputs_gender").removeClass("focusfield");
		});
		$("#input_genderM").focus(function() {
			$("#inputs_gender").removeClass("errorfield");
			$("#inputs_gender").addClass("focusfield");
		});
		$("#input_genderM").blur(function() {
			$("#inputs_gender").removeClass("focusfield");
		});
		$("#input_visitedN").focus(function() {
			$("#inputs_visited").removeClass("errorfield");
			$("#inputs_visited").addClass("focusfield");
		});
		$("#input_visitedN").blur(function() {
			$("#inputs_visited").removeClass("focusfield");
		});
		$("#input_visitedY").focus(function() {
			$("#inputs_visited").removeClass("errorfield");
			$("#inputs_visited").addClass("focusfield");
		});
		$("#input_visitedY").blur(function() {
			$("#inputs_visited").removeClass("focusfield");
		});
		$("#input_affirm_us").click(function(){checkinput('affirm_us');});
		$("#input_fname").focus(function(){checkinput('affirm_us');});
		$("#input_fname").blur(function(){checkinput('fname');});
		$("#input_lname").blur(function(){checkinput('lname');});
		$("#input_nickname").blur(function(){checkinput('nickname',true);});
		$("#input_email").blur(function(){checkinput('email');});
		$("#input_phone").blur(function(){checkphone();});
		$("#input_cellphone").blur(function(){checkphone();});
		$("#input_address").blur(function(){checkinput('address');});
		$("#input_city").blur(function(){checkinput('city');});
		$("#input_state").blur(function(){checkinput('state');});
		$("#input_zip").blur(function(){checkinput('zip');});
		$("#input_highschool").blur(function(){checkinput('highschool');});
		$("#input_gradyear").blur(function(){checkinput('gradyear');});
		$("#input_ethnicity").blur(function(){checkinput('ethnicity');});
		$("#input_language").blur(function(){checkinput('language');});
		$("#input_genderF").blur(function(){checkgender();});
		$("#input_genderM").blur(function(){checkgender();});
		$("#input_visitedN").blur(function(){checkvisited();});
		$("#input_visitedY").blur(function(){checkvisited();});
		$("#input_household").blur(function(){checkinput('household');});
		$("#input_familyincome").blur(function(){checkinput('familyincome');});
		$("#input_gpa").blur(function(){checkinput('gpa');});
		$("#input_satcr").blur(function(){checkscores();});
		$("#input_satm").blur(function(){checkscores();});
		$("#input_satw").blur(function(){checkscores();});
		$("#input_act").blur(function(){checkscores();});
		$("#input_classes").blur(function(){checkinput('classes');});
		$("#input_whywhitman").blur(function(){checkinput('whywhitman');});
		$("#input_whydiverse").blur(function(){checkinput('whydiverse');});
		// word counting for textareas
		$(['whywhitman','whydiverse','whyextra']).each(function() {
			word_count('input_'+this,this+'_count');
			var v=this;
			$("#input_"+this).keyup(function() {word_count('input_'+v,v+'_count') });
		});
	}
});

