$(document).ready(
	function(){
		token_from_step_2 = $('#token_from_step_2').val();

		w = $(document).width();
		h = $(document).height();

		$('#step2_box_at_the_back').css(
			{
				'width' : w,
				'height' : h
			}
		);

		$('#add_ext').click(
			function(){
				$('#ext_trigger').hide(
					function(){
						$('#ext_input').show();
					}
				);
			}
		);

		$('#cancel_ext').click(
			function(){
				$('#ext_input').hide(
					function(){
						$('#ext_trigger').show(
							function(){
								$('#ext').val('');
								submit_ext('');
							}
						);
					}
				);
			}
		);

		$('#add_phone_2').click(
			function(){
				$('#phone_2_trigger').slideUp(
					function(){
						$('#phone_2_input').slideDown();
					}
				);
			}
		);

		$('#cancel_phone_2').click(
			function(){
				$('#phone_2_input').slideUp(
					function(){
						$('#phone_2_trigger').slideDown();
					}
				);
			}
		);

		$.fn.thank_you = function(){
			$(window.location).attr('href', '/thank-you');
		}

		function one_moment(){
			ht = $('#step2').height();

			mt = Math.round(ht/2);

			$('#step2_box_in_front').css('margin-top','-' + '' + mt + 'px');
			$('#step2_box_in_front').css('top','50%');
			$('#step2_box_in_front').css('height',ht + 'px');
			$('#step2_box_in_front').show();
		}

		$('#quote').submit(
			function(){
				$(this).delay(2000).final_submit();
			}
		);

		$.fn.final_submit = function(){
			$.post(
				"/form/form-php/submission.php",
				{
					submit_quote: '',
					step_2: '',
					token: token_from_step_2,
					time: new Date().getTime()
				},
				function(data){
					if(jQuery.trim(data.substring(0,11)) == 'READY TO GO'){
						one_moment();
						$('#roll_up_when_complete, #step2_box_in_front').delay(2000).slideUp(
							function(){
								$('#roll_down_when_complete').html('Your quote has been submitted. One moment please.').slideDown().thank_you();
							}
						);
					}else if(jQuery.trim(data.substring(0,6)) == 'ERRORS'){
						alert(data);
					}
				}
			);
		}

		$('#year_month_day').change(
			function(){
				option = $(this).val();

				$.post(
					"/form/form-php/validations.php",
					{
						year_month_day: option,
						step_2: '',
						token: token_from_step_2,
						time: new Date().getTime()
					},
					function(data){
						if(jQuery.trim(data) == 'DATE NOT ENTERED'){
							$('#check_year_month_day').attr('class','incomplete-mark');
						}else if(jQuery.trim(data) == 'DATE ENTERED NOT VALID'){
							$('#check_year_month_day').attr('class','error-mark');
						}else{
							$('#check_year_month_day').attr('class','check-mark');
						}
					}
				);
			}
		);

		$('#number_of_rooms').change(
			function(){
				option = $(this).val();

				$.post(
					"/form/form-php/validations.php",
					{
						number_of_rooms: option,
						step_2: '',
						token: token_from_step_2,
						time: new Date().getTime()
					},
					function(data){
						if(jQuery.trim(data) == 'NUMBER OF ROOMS NOT ENTERED'){
							$('#check_number_of_rooms').attr('class','incomplete-mark');
						}else{
							$('#check_number_of_rooms').attr('class','check-mark');
						}
					}
				);
			}
		);

		$('input[name="first_name"], input[name="last_name"]').keypress(
			function(e){
				if(e.keyCode == 13 || e.which == 13){
					$(this).name_submission();
				}
			}
		);

		$('#first_name, #last_name').blur(
			function(){
				this_val = $(this).val();
				this_id = $(this).attr('id');

				if(this_id == 'first_name' && (jQuery.trim(this_val.toLowerCase()) == 'first' || jQuery.trim(this_val) == '')){
					$(this).attr('class','name-box');
					$(this).val('First');
					$('#check_name').attr('class','incomplete-mark');
				}else if(this_id == 'last_name' && (jQuery.trim(this_val.toLowerCase()) == 'last' || jQuery.trim(this_val) == '')){
					$(this).attr('class','name-box');
					$(this).val('Last');
					$('#check_name').attr('class','incomplete-mark');
				}else{
					$(this).attr('class','input-box');
					$(this).val(this_val);
				}

				$(this).name_submission();
			}
		);

		$.fn.name_submission = function(){
			first_name = $('#first_name').val();
			last_name = $('#last_name').val();

			$.post(
				"/form/form-php/validations.php",
				{
					first_name: first_name,
					last_name: last_name,
					step_2: '',
					token: token_from_step_2,
					time: new Date().getTime()
				},
				function(data){
					if(jQuery.trim(data) == 'NAME ENTERED NOT VALID'){
						$('#check_name').attr('class','error-mark');
					}else if(
						(jQuery.trim(first_name) == '' || jQuery.trim(first_name.toLowerCase()) == 'first') ||
						(jQuery.trim(last_name) == '' || jQuery.trim(last_name.toLowerCase()) == 'last')
					){
						$('#check_name').attr('class','incomplete-mark');
					}else{
						$('#check_name').attr('class','check-mark');
					}
				}
			);
		};

		$('#first_name, #last_name').focus(
			function(){
				this_val = $(this).val();
				this_id = $(this).attr('id');

				if(this_id == 'first_name' && jQuery.trim(this_val.toLowerCase()) == 'first'){
					$(this).attr('class','input-box');
					$(this).val('');
				}else if(this_id == 'last_name' && jQuery.trim(this_val.toLowerCase()) == 'last'){
					$(this).attr('class','input-box');
					$(this).val('');
				}else{
					$(this).attr('class','input-box');
					$(this).val(this_val);
				}
			}
		);

		$('input[name="email"]').blur(
			function(){
				$(this).email_submission();
			}
		);

		$.fn.email_submission = function(){
			email = $(this).val();

			$.post(
				"/form/form-php/validations.php",
				{
					email: email,
					step_2: '',
					token: token_from_step_2,
					time: new Date().getTime()
				},
				function(data){
					if(jQuery.trim(data) == 'EMAIL NOT ENTERED'){
						$('#check_email').attr('class','incomplete-mark');
					}else if(jQuery.trim(data) == 'EMAIL ENTERED NOT VALID'){
						$('#check_email').attr('class','error-mark');
					}else{
						$('#check_email').attr('class','check-mark');
					}
				}
			);
		};

		$('input[name="email"]').keypress(
			function(e){
				if(e.keyCode == 13 || e.which == 13){
					$(this).email_submission();
				}
			}
		);

		$('input[name="ext"]').keypress(
			function(e){
				if(e.keyCode == 13 || e.which == 13){
					ext = $(this).val();
					submit_ext(ext);
				}
			}
		);

		$('input[name="phone_1"], input[name="phone_2"]').keypress(
			function(e){
				if(e.keyCode == 13 || e.which == 13){
					$(this).phone_submission('no-add-attempt');
				}
			}
		);

		$('input[name="ext"]').blur(
			function(){
				ext = $(this).val();
				submit_ext(ext);
			}
		);

		check_number = /[0-9]/;

		$('#phone_1, #phone_2').live(
			'input paste',
			function(){
				this_id = $(this).attr('id');

				phone = $(this).val();

				phone = $(this).format_phone_input_value('');

				$(this).val(phone);

				$(this).phone_submission('no-add-attempt');
			}
		);

		$('input[name="phone_1"], input[name="phone_2"]').blur(
			function(){
				$(this).phone_submission('no-add-attempt');
			}
		);

		check_reformatted_number = /\(+([\d+]{3,3})+\)\ [\d+]{3,3}\ \-\ [\d+]{4,4}/;

		$('input[name="phone_1"], input[name="phone_2"]').mask(
			"(999) 999 - 9999",
			{
				placeholder:" ",
				completed:function(){
					$(this).phone_submission('add-attempt');
				}
			}
		);
	}
);

$.fn.phone_submission = function(attempt){
	this_id = $(this).attr('id');
	phone = $(this).val();
	phone = jQuery.trim(phone);

	$.post(
		"/form/form-php/validations.php",
		{
			which_phone: this_id,
			phone: phone,
			attempt: attempt,
			step_2: '',
			token: token_from_step_2,
			time: new Date().getTime()
		},
		function(data){
			if(jQuery.trim(data) == 'PHONE NOT ENTERED'){
				$('#check_' + this_id).attr('class','incomplete-mark');
				if(this_id == 'phone_1'){
					remove_ext();
					remove_phone_2();
				}
			}else if(jQuery.trim(data) == 'PHONE ENTERED NOT VALID'){
				$('#check_' + this_id).attr('class','error-mark');
				if(this_id == 'phone_1'){
					remove_ext();
					remove_phone_2();
				}
			}else{
				$('#check_' + this_id).attr('class','check-mark');
				if(this_id == 'phone_1'){
					allow_ext();
					allow_phone_2();
				}
			}
		}
	);
};

$.fn.force_remove_field = function(){
	this_id = $(this).attr('id');
	phone = $(this).val();

	$.post(
		"/form/form-php/kill.php",
		{
			field: this_id,
			token: token_from_step_2,
			time: new Date().getTime()
		},
		function(data){}
	);
};

$.fn.format_phone_input_value = function(given_phone){
	if(given_phone.length != 0){
		phon = given_phone;
	}else{
		phon = $(this).val();
	}

	phon = phon.replace(/ /g,'');

	p = phon.split('');

	pon = [];

	y = 0;
	for(i in p){
		if( check_number.test(p[i]) ){
			pon[y] = p[i];
			y++;
		}
	}

	if(typeof pon[3] != 'undefined'){
		var str = '(';
	}else{
		var str = '';
	}

	for(x in pon){
		str+= pon[x];

		if(x == 2 && pon.length > 3){
			str+= ') ';
		}else if(x == 5 && pon.length > 6){
			str+= ' - ';
		}
	}

	return str.substring(0,16);
}

function remove_ext(){
	$('#ext').val('');
	$('#ext_trigger').hide();
	$('#ext_input').hide();
	$('#ext').force_remove_field();
}

function remove_phone_2(){
	$('#phone_2').val('');
	$('#phone_2_trigger').hide();
	$('#phone_2_input').hide();
	$('#phone_2').force_remove_field();
}

function submit_ext(ext){
	$.post(
		"/form/form-php/validations.php",
		{
			ext: ext,
			step_2: '',
			token: token_from_step_2,
			time: new Date().getTime()
		},
		function(data){
		}
	);
}

function allow_ext(){
	$('#ext_input').hide();
	$('#ext_trigger').show();
}

function allow_phone_2(){
	$('#phone_2_input').hide();
	$('#phone_2_trigger').show();
}

$(document).ready(
	function(){
		$(document).keyup(
			function(e){
				if(e.keyCode == 27){
					$('#step2').slideUp(
						function(){
							$('#step2_box_at_the_back').fadeTo(
								'fast',
								.0,
								function(){
									$('#step2_box_at_the_back').css('display','none');
								}
							);
						}
					);
				}
			}
		);

		$('#close_box_from, #close_box_to').click(
			function(){
				$('#step2').slideUp(
					function(){
						$('#step2_box_at_the_back').fadeTo(
							'fast',
							.0,
							function(){
								$('#step2_box_at_the_back').css('display','none');
							}
						);
					}
				);
			}
		);
	}
);

$.fn.prepare_step_2_top_margin = function(){
	ht = $(this).height();

	mt = Math.round(ht/2);

	$(this).css('margin-top','-' + '' + mt + 'px');
	$(this).css('top','50%');

	$(this).slideDown();
};

$(window).resize(
	function(){
		w = $(document).width();
		h = $(document).height();

		$('#step2_box_at_the_back').css(
			{
				'width' : w,
				'height' : h
			}
		);
	}
);

function positionScroll(){
	p = $('body').position();
	var t = p.top;

	$('html,body').animate(
		{
			scrollTop: t
		},
		500,
		function(){
			$('#step2_box_at_the_back').fadeTo(
				'fast',
				.5,
				function(){
					$('#step2').prepare_step_2_top_margin();
				}
			);
		}
	);
}
