$(document).ready( function(  ) {

	if( $('#form_cont').length != 0 ) {
		document.apply_form.reset(  );
	}

});


var is_invalid = 0;
var ematch = 0;

function validatePhone( phone )
{

	var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	return phoneNumberPattern.test( phone );

}

function validateZip( zip )
{

    var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
    return zipCodePattern.test( zip );
}

function validEmail( email )
{

	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test( email );
	
}


function emailMatch(  )
{

	var email_val = $('#email').val(  );
	var conf_val = $('#confirm_email').val(  );
	
	return ( email_val == conf_val );

}

function IsNumeric( input )
{
	 return ( input - 0 ) == input && input.length > 0;
}


function filterNum( str )
{
	re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
	// remove special characters like "$" and "," etc...
	return str.replace(re, "");
	
}


$(document).ready(function(  ){

	$("#js_dis").hide(  );

	var e_v = '';





	$('#confirm_email').keyup( function(  ) {

		var vemail = $('#email').val(  );

		if( validEmail( vemail ) ) {
			$('#info_wrap').slideDown(  );
			e_v = $('#email').val(  );

			if( e_v != '' )
			{
				if( $(this).val(  ) != e_v )
				{
					$('#test').html('<!-- img src="assets/themes/carbon/images/invalid.png" / --><span class="fail">Email Addresses Do Not Match</span>');
					ematch = 0;
				}
				else
				{
					$('#test').html('<!-- img src="assets/themes/carbon/images/valid.png" / --><span class="success">Email Adresses Match</span>');
					ematch = 1;
				}
			}
			else
			{
				$('#test').html('');
			}

		}

	});
	
		$('#confirm_email').blur( function(  ) {

		var vemail = $('#email').val(  );

		if( validEmail( vemail ) ) {
			$('#info_wrap').slideDown(  );
			e_v = $('#email').val(  );

			if( e_v != '' )
			{
				if( $(this).val(  ) != e_v )
				{
					$('#test').html('<!-- img src="assets/themes/carbon/images/invalid.png" / --><span class="fail">Email Addresses Do Not Match</span>');
					ematch = 0;
				}
				else
				{
					$('#test').html('<!-- img src="assets/themes/carbon/images/valid.png" / --><span class="success">Email Adresses Match</span>');
					ematch = 1;
				}
			}
			else
			{
				$('#test').html('');
			}

		}

	});			



	$("#email").blur( function(  ) {

		var email = $("#email").val(  );
	 
		if( email != 0 )
		{

			if( validEmail( email ) )
			{
	 
				$("#e_is_valid").css({ "background-image": "url('assets/themes/carbon/images/valid.png')" });
				is_invalid = 0;

			} 

			else
			{
	 
				$("#e_is_valid").css({ "background-image": "url('assets/themes/carbon/images/invalid.png')" });
				is_invalid = 1;
	 
			}
	 
		}
	
		else
		{
		 
			$("#e_is_valid").css({ "background-image": "none" });
		 
		}
	});

	$("#email").keyup( function(  ) {

		var email = $("#email").val(  );
	 
		if( email != 0 )
		{

			if( validEmail( email ) )
			{
	 
				$("#e_is_valid").css({ "background-image": "url('assets/themes/carbon/images/valid.png')" });
				is_invalid = 0;

			} 

			else
			{
	 
				$("#e_is_valid").css({ "background-image": "url('assets/themes/carbon/images/invalid.png')" });
				is_invalid = 1;
	 
			}
	 
		}
	
		else
		{
		 
			$("#e_is_valid").css({ "background-image": "none" });
		 
		}
	});
	
	


	$('#ref_other').focus( function(  ) {

		$(this).css("border", "1px solid #ccc");
		$(this).css("background", "#fff");

	});

	function validateForm(  ) 
	{

	
		//var fsection = '#' + form_box;
		var hasErrors = 0;
		var error_list = '';
		var error_li = '';
		var email_error = '';
		var sal_start_error = '';
		var sal_end_error = '';
		var sal_e_less_error = '';
		var zip_error = '';
		var phone_error = '';
		var sal_s = $('#salary_range_start').val(  );
		var sal_e = $('#salary_range_end').val(  );
		var phone = $('#phone').val(  );
		var zip = $('#zip').val(  );
		var mo_match_error = '';

		if( !emailMatch(  ) )
		{
		
			//alert("no match");
			mo_match_error = "<li>Email Addresses do not match</li>";
			$('#confirm_email').addClass("required");
			hasErrors++;
		
		}
						
		if( !validatePhone( phone ) )
		{
		
			//alert("Phone Fail");
			phone_error = '<li>Invalid Phone Number Format - Examples:<ul class="error_sub"><li>555-555-5555</li><li>(555)555-5555</li></ul></li>';
			$('#phone').addClass("required");
			hasErrors++;
			
		
		}
		
		if( !validateZip( zip ) )
		{
		
			zip_error = '<li>Invalid Zip Code - Must be 5 digits</li>';
			$('#zip').addClass("required");
			hasErrors++;
		
		}


		if( !IsNumeric( sal_s ) )
		{

			sal_start_error = "<li>The field 'Salary Range Start' Must be numeric</li>";
			$('#salary_range_start').addClass("required");
			hasErrors++;
		
		}		

		if( !IsNumeric( sal_e ) )
		{
		
			sal_end_error = "<li>The field 'Salary Range End' Must be numeric</li>";
			$('#salary_range_end').addClass("required");
			hasErrors++;		

		
		}			
		
		if( IsNumeric( sal_e )  && IsNumeric( sal_e ) )
		{
			sal_e = parseFloat( sal_e );
			sal_s = parseFloat( sal_s );

			if( sal_e <= sal_s )
			{
				$('#salary_range_end').addClass("required");
				sal_e_less_error = "<li>The field 'Salary Range End must be larger than 'Salary Range Start'</li>";
				hasErrors++
		
		
			}
		
		}
			

			
		
		
		if( is_invalid == 1 )
		{

			email_error = '<li>Invalid email address</li>';
			hasErrors++;
		}
		else
		{

			email_error = '';

		}
	

		$('.req').each( function(  ) {
			var reqId = $(this).attr("id");
			var reqTitle = $(this).attr("title");


			if( $(this).val(  ) == '' )
			{
				$(this).addClass('required');

				$('#' + reqId + '_ins').css("visibility", "visible");

				//$(this).css("border", "1px solid #CC0000");
				//$(this).css("background", "#FDE4E4");

				hasErrors++;		

				error_li = '<li>The field \'' + reqTitle + '\' is Required</li>';
				error_list += error_li;
			}
			else 
			{ 
				//$(this).removeClass( 'required' );
				$('#' + reqId + '_ins').html( '' );
				$('#' + reqId + '_ins').css( "visibility", "hidden" );

			}

			$("#err_sum ul").html( phone_error + zip_error + mo_match_error + email_error + sal_start_error + sal_end_error + sal_e_less_error + error_list );
			$(  '.error_sum_cont').show(  );
			$(document).scrollTop(  );

		});

		if( hasErrors == 0 )
		{
			$('.error_sum_cont').slideUp(  );
			return true;
		}



	}

	$('#finish').click( function(  ) {

		if( validateForm(  ) )
		{

			var str_phone = $('#phone').val(  );
			$('#phone').val( filterNum( str_phone ) );

			document.apply_form.submit(  );

		}


	});

	$('.req').focus( function(  ) {

		//$('.req').css("border", "1px solid #ccc");

		//$(this).css("border", "1px solid #ffcc00");
		//$(this).css("background", "#fff");
		$(this).removeClass("req required");
		$(this).addClass("req");					

		var theId = $(this).attr( "id" ) + '_ins';

		$('#' + theId ).html( '' );
		$('#status_message').html( '' );

	});

	var refVal = $('#referred_by').val(  );

	if( refVal == 1999 )
	{
		$('#ref_other').addClass('req');
		$('#other_info').show(  );

	}

	$('#continue_1').click( function(  ) {


		if( validateForm( 'job_select_cont' ) )
		{
			$('.error_sum_cont').hide(  );
			$('#job_select_cont').hide(  );
			$('#emp_info_cont').show(  );			

		}

	});

	$('#continue_2').click( function(  ) {

		if( validateForm( 'emp_info_cont' ) )
		{
			$('.error_sum_cont').hide(  );
			$('#emp_info_cont').hide(  );
			$('#upload_file_cont').show(  );
		}

	});

	$('#back_1').click( function(  ) {
		$('email').val('');
		$('.req').removeClass("required");

		$('#emp_info_cont').hide(  );
		$('.error_sum_cont').hide(  );
		$('#job_select_cont').show(  );
		is_invalid = 0;


	});

	$('#back_2').click( function(  ) {

		$('#upload_file_cont').hide(  );
		$('.error_sum_cont').hide(  );
		$('#emp_info_cont').show(  );

	});

	$('#referred_by').change( function(  ) {

		$('#ref_other').css("border", "1px solid #ccc");
		$('#ref_other').css("background", "#fff");
		$('ref_other').val("");
		var ref_val = $('#referred_by').val(  );
		if( ref_val == 1999 )
		{
			$('#ref_other').addClass('req');
			$('#other_info').show(  );

		}
		else
		{
			$('#ref_other').removeClass('req');
			$('#other_info').slideUp("fast");
		}
	});

});

