check_digits = /[0-9]/;
check_alphas = /[a-zA-Z\ \,]/;
check_digits_length = /\d{0,5}/;

token_from_step_1 = $('#token_from_step_1').val();

function get_location_options(a){
	x = $('[name="what_city_' + a + '"]').val();
	loc = jQuery.trim(x);

	check = 'no';

	if(check_digits.test(loc)){
	}else if(check_alphas.test(loc)){
		if(loc.search(/\,/i) != -1){
			loc_array = loc.split(',');
			cT = jQuery.trim(loc_array[0]);

			if(loc_array.length == 2){
				if( cT.length >= 2 ){
					check = 'yes';
				}
			}
		}else{
			if( loc.length >= 2 ){
				check = 'yes';
			}
		}
	}

	if(check == 'yes'){
		$.get(
			"/form/form-php/city-suggestions.php",
			{
				location: loc,
				to_from: a,
				token: token_from_step_1,
				time: new Date().getTime()
			},
			function(data){
				if(data == 'NONE'){
					$('#city_suggestions_wrapper_' + a).hide(
						function(){
							$('#city_suggestions_box_' + a).html('');
						}
					);
				}else{
					$('#city_suggestions_box_' + a).html(data);

					showSearchautofillbox(a);
				}
			}
		);
	}else{
		$('#city_suggestions_wrapper_' + a).hide();
	}

	return false;
}

function showSearchautofillbox(a){
	$('#city_suggestions_wrapper_' + a).show();
}

function chooseOption(i,a){
	var c = $('#locationOption_' + a + i).val();
	$('[name="what_city_' + a + '"]').val(c);

	close_location_options(a);
}

function close_location_options(a){
	$('#city_suggestions_wrapper_' + a).hide(
		function(){
			$('#city_suggestions_box_' + a).html('');
		}
	);
}

$(document).ready(
	function(){
		$('#what_city_from, #what_city_to').attr('autocomplete','off');

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

				if(jQuery.trim(this_val.toLowerCase()) == 'city, state or zipcode' || jQuery.trim(this_val) == ''){
					$(this).attr('class','city-inputs');
					$(this).val('City, State or Zipcode');
				}else{
					$(this).attr('class','form-inputs');
					$(this).val(this_val);
				}
			}
		);

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

				if(jQuery.trim(this_val.toLowerCase()) == 'city, state or zipcode'){
					$(this).attr('class','form-inputs');
					$(this).val('');
				}else{
					$(this).attr('class','form-inputs');
					$(this).val(this_val);
				}
			}
		);

		$('input[name=what_city_from], input[name=what_city_to]').keypress(
			function(e){
				this_val = $(this).val();
				this_val = jQuery.trim(this_val);

				character = String.fromCharCode(e.which);

				allowed_key_which = new Array(8,9,13,27,37,39,46);

				if( jQuery.inArray(e.keyCode,allowed_key_which) > -1 || jQuery.inArray(e.which,allowed_key_which) > -1 ){
				}else if( check_digits.test(character) || check_alphas.test(character)){
				}else{
					e.preventDefault();
				}
			}
		).keyup(
			function(e){
				this_id = $(this).attr('id');
				if(this_id == 'what_city_from'){
					point = 'from';
				}else if(this_id == 'what_city_to'){
					point = 'to';
				}

				r = $('#city_suggestions_box_' + point).html();

				if (jQuery.trim(r) != '' && (e.keyCode == 40 || e.keyCode == 38 || e.keyCode == 13)){
					cityAutoSuggestHighlight = [];
					$('#city_suggestions_list_' + point + ' li').each(
						function(){
							cityAutoSuggestHighlight.push( $(this).attr('id') );
						}
					);

					var cityAutoSuggestHighlightId = 'none';
					for(i in cityAutoSuggestHighlight){
						if( $('#' + cityAutoSuggestHighlight[i]).attr('class') == 'current' ){
							cityAutoSuggestHighlightId = $('#' + cityAutoSuggestHighlight[i]).attr('id');
						}
					}

					if(cityAutoSuggestHighlightId == 'none'){
						number = 0;
					}else{
						number = cityAutoSuggestHighlightId.replace('city-auto-suggest-highlight-' + point + '-','');
						number = parseInt(number);
					}

					if(cityAutoSuggestHighlight.length != 0){
						if (e.keyCode == 40){
							if( cityAutoSuggestHighlight.length > number ){
								next = number + 1;
								if(number != 0){
									$('#city-auto-suggest-highlight-' + point + '-' + number).removeAttr('class');
								}
								$('#city-auto-suggest-highlight-' + point + '-' + next).attr('class','current');
							}
						}else if(e.keyCode == 38){
							if( number > 1 ){
								prev = number - 1;
								$('#city-auto-suggest-highlight-' + point + '-' + number).removeAttr('class');
								$('#city-auto-suggest-highlight-' + point + '-' + prev).attr('class','current');
							}else if( number == 1 ){
								$(this).focus();
							}
						}else if(e.keyCode == 13){
							if( $('#city-auto-suggest-highlight-' + point + '-' + number).attr('class') == 'current' ){
								var c = $('#locationOption_' + point + '' + number).val();
								$(this).val(c);
								close_location_options(point);
								return false;
							}
						}
					}
				}else{
					if(check_digits.test(character) || check_alphas.test(character) || e.keyCode == 8 || e.keyCode == 46){
						get_location_options(point);
					}
				}
			}
		);

		$('#destination_info').submit(
			function(){
				from_box = $('#city_suggestions_box_from').html();
				from_box = jQuery.trim(from_box);
				to_box = $('#city_suggestions_box_to').html();
				to_box = jQuery.trim(to_box);

				if(from_box == '' && to_box == ''){
					checkPassQuery();
				}
			}
		);
	}
);

function checkPassQuery(){
	from = $('[name="what_city_from"]').val();
	from = jQuery.trim(from);
	to = $('[name="what_city_to"]').val();
	to = jQuery.trim(to);

	if(from.toLowerCase() == 'city, state or zipcode'){
		from = '';
	}

	if(to.toLowerCase() == 'city, state or zipcode'){
		to = '';
	}

	error = '';

	if( from == '' ){
		error+= '- We don\'t know where you\'re Moving From.';
		if(to == ''){
			error+= '\n';
		}
	}else if(check_digits.test(from)){
		if(from.length != 5){
			error+= '- We don\'t know where you\'re Moving From. The Zipcode you\'re Moving From should be 5 digits.';
			if(to == ''){
				error+= '\n';
			}
		}
	}

	if( to == '' ){
		error+= '- We don\'t know where you\'re Moving To.';
	}else if(check_digits.test(to)){
		if(to.length != 5){
			error+= '- We don\'t know where you\'re Moving To. The Zipcode you\'re Moving To should be 5 digits.';
		}
	}

	if(error.length != 0){
		alert(error);
	}else{
		$.post(
			"/form/form-php/submission.php",
			{
				from: from,
				to: to,
				step_1: '',
				token: token_from_step_1,
				time: new Date().getTime()
			},
			function(data){
				if(data.substring(0,6) == 'STEP 2'){
					datasplit = data.split(':');
					locationfrom = jQuery.trim(datasplit[1]);
					locationto = jQuery.trim(datasplit[2]);
					$('#moving_from').text(locationfrom);
					$('#moving_to').text(locationto);
					positionScroll();
				}else if(data.substring(0,6) == 'Errors'){
					alert(data);
				}
			}
		);
	}

	return false;
}

function hover_over_city_suggestion(this_id,to_from){
	sze = $('#city_suggestions_list_' + to_from + ' li').size();

	for(i = 1; i <= sze; i++){
		if('city-auto-suggest-highlight-' + to_from + '-' + i != this_id){
			$('#city-auto-suggest-highlight-' + to_from + '-' + i).removeAttr('class');
		}else{
			$('#city-auto-suggest-highlight-' + to_from + '-' + i).attr('class','current');
		}
	}
}
