/*
	Coded by:
	
	Shaun Russell
	shaun.d.russell@gmail.com
	
	for
	
	Still Water Interactive
	
*/
	
/* error construct */
function build_error(error, element, index)
{		
	this.error = error;
	this.element = element;
	this.index = index;
}

function focus_clear(element_name, text) {
	$("input[name='" + element_name +"']").focus(function(){
		if($(this).val() == text) {
			$(this).attr('value', '');
			$(this).css('color', '#2c2c2c');
		}
	}).blur(function(){
		if($(this).val() == '') {
			$(this).attr('value', text);
			$(this).css('color', '#c0c0c0');
		}
	});	
}

$(document).ready(function () {
	/* add 'required' to required fields */	
	$(".required").each(function() {
		if($(this).attr('value') == '') {
			$(this).attr('value', 'required');
			$(this).addClass('default-blank');
		}
	});

	$(".required").css('color', '#c0c0c0');
	$(".required").focus(function(){
		if($(this).val() == 'required') {
			$(this).attr('value', '');
			$(this).css('color', '#2c2c2c');
		}
	}).blur(function(){
		if($(this).val() == '' && $(this).hasClass('default-blank')) {
			$(this).attr('value', 'required');
			$(this).css('color', '#c0c0c0');
		}
	});	
	
	/* make hidden initially */
	$("#special-requirements-explain").hide();
	$("#conviction-true").hide();
	$("#applied-before").hide();
	$("div.errors").hide();

	/* toggle hidden inputs */
	$("input[name='special_requirements']").click(function() {
		if($(this).val() == 'yes') {
			$("#special-requirements-explain").show();
		}
		else {
			$("#special-requirements-explain").hide();
		}
	});
	
	$("input[name='convictions']").click(function() {
		if($(this).val() == 'yes') {
			$("#conviction-true").show();
		}
		else {
			$("#conviction-true").hide();
		}
	});
	
	$("input[name='previously_applied']").click(function() {
		if($(this).val() == 'yes') {
			$("#applied-before").show();
		}
		else {
			$("#applied-before").hide();
		}
	});	
	
	/* clear inputs on click */
	focus_clear("start_date", "mm/dd/yy");
	focus_clear("grammar_school", "grammar school");
	focus_clear("high_school", "high school");
	focus_clear("college_school", "college");
	focus_clear("trade_school", "trade, business or forrespondence school");
	focus_clear("other_school", "other schooling");
	focus_clear("work_dates1", "mm/yy to mm/yy");
	focus_clear("work_dates2", "mm/yy to mm/yy");
	focus_clear("work_dates3", "mm/yy to mm/yy");							
							
							
	$("#employment_form").submit(function() {				
		var $input = [];
		var $radio = [];
		
		var errors = [];
		var error_num = 0;
		
		specific_errors = false;
		
		/* find required fields */
		$(".required").each(function(intIndex) {
			$input[intIndex] = $(this);
		});						
		
		/* build errors */
		for(i=0;i<$input.length;i++) {
			/* clear the highlighted */
			if( $input[i].hasClass('highlight') ) {
				$input[i].removeClass('highlight');				
			}
			/* check empties */
			if($input[i].val() == '' || $input[i].val() == 'required') {
				errors[error_num] = new build_error("empty", $input[i].attr('name'), i);
				$input[i].addClass("highlight");
				error_num++;
			}
			/* phone number not == 10 digits */
			else if($input[i].attr('name') == 'phone_number') {
				var regex = /^\s*\d+\s*$/;
				if(!regex.test($input[i].val()) || $input[i].val().length != 10) {
					errors[error_num] = new build_error("invalid phone", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			/* if state is too short or numbers */
			else if($input[i].attr('name') == 'present_state') {
				var regex = /(?:[a-z][a-z])/;
				if(!regex.test($input[i].val()) || $input[i].val().length != 2) {
					errors[error_num] = new build_error("invalid state", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			/* if zipcode is too short or numbers */
			else if($input[i].attr('name') == 'present_zip_code') {
				var regex = /^\s*\d+\s*$/;
				if(!regex.test($input[i].val()) || $input[i].val().length != 5) {
					errors[error_num] = new build_error("invalid zip code", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			/* if start date mm/dd/yy  */
			else if($input[i].attr('name') == 'start_date') {
				var regex = /(?:0[1-9]|[1][0-2])\/(?:[0-2][1-9]|[3][0-1])\/(?:09|[1-9][0-9])/;
				if(!regex.test($input[i].val()) || $input[i].val().length != 8) {
					errors[error_num] = new build_error("invalid date", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			else if($input[i].attr('name').substring(0, $input[i].attr('name').length-1)  == 'work_dates') {				
				if($input[i].val() == 'mm/yy to mm/yy' || $input[i].val().length != 14) {
					errors[error_num] = new build_error("invalid dates", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			else if($input[i].attr('name').substring(0, $input[i].attr('name').length-1)  == 'reference_phone') {	
				var regex = /^\s*\d+\s*$/;
				if(!regex.test($input[i].val()) || $input[i].val().length != 10) {
					errors[error_num] = new build_error("invalid dates", $input[i].attr('name'), i);
					$input[i].addClass("highlight");
				}
			}
			else if($input[i].attr('name') == 'college_school' && $input[i].val() == 'college') {	
				$input[i].attr('value', '');
			}
			else if($input[i].attr('name') == 'trade_school' && $input[i].val() == 'trade, business or forrespondence school') {	
				$input[i].attr('value', ' ');
			}
			else if($input[i].attr('name') == 'other_school' && $input[i].val() == 'other schooling') {	
				$input[i].attr('value', ' ');
			}
		}
		
		/* find required radios */
		$(".required-radio").each(function(intIndex) {
			$radio[intIndex] = $(this);
		});
		
		/* check inside the divs for checked radios */
		error_num = errors.length;
		for(i=0;i<$radio.length;i++) {
			checked = false;
			if($radio[i].children("label").hasClass("red-text")) {
				$radio[i].children("label").removeClass("red-text");
			}
			$radio[i].children("input").each(function() {				
				if($(this).is(":checked")) {
					checked = true;
				}
			})
			if(!checked) {
				errors[error_num] = new build_error("unselected", $radio[i].attr('title'), i);
				$radio[i].children("label").addClass("red-text");
				error_num++;
			}
		}
		if (errors.length > 0 ) {
			var html = "<h3>Form Errors</h3>"
			
			if (specific_errors) {				
				var html = html + "<dl>";
				
				for(i=0;i<errors.length;i++) {
					html = html + "<dt>" + "Field: " + errors[i].element + "</dt>";
					html = html + "<dd>" + "Error: " + errors[i].error + "</dd>";
				}
				
				html = html + "</dl>";
			}
			else {
				html = html + "<p>Sorry, but the form is incomplete, please check it over - the problem fields have been highlighted.</p>";								
			}							
			$(".errors").html(html);
			$(".errors").show();
			window.scroll(0,0);
			return false;
		}
		else {
			return true;
		}
		
	});
	
	/* confirmation validation */
	$("#employment_confirm").submit(function() {
		if( $("input[name='signature']").hasClass('highlighted') ) {
			$("input[name='signature']").removeClass('highlighted');
		}
		$("label.invalid").remove();
		if( $("input[name='signature']").val().toLowerCase() != $("input[name='full_name']").val().toLowerCase())
		{
			$("input[name='signature']").addClass('highlighted');
			html = "<label class=\"invalid\">Your signature does not match your stated first and last name</label>";
			$("input[name='signature']").after(html);
			return false;	
		}

	});
	
});	