$(function() {
	//Set auto-jump events for phone number entry.
	$('#txtAreaCodeHomePhone').keyup(function() {
		jumpTextbox(this, 3, 'txtHomePhone1');
	});
	$('#txtHomePhone1').keyup(function() {
		jumpTextbox(this, 3, 'txtHomePhone2');
	});

	$('#txtAreaCodeWorkPhone').keyup(function() {
		jumpTextbox(this, 3, 'txtWorkPhone1');
	});
	$('#txtWorkPhone1').keyup(function() {
		jumpTextbox(this, 3, 'txtWorkPhone2');
	});

	$('#txtAreaCodeCellPhone').keyup(function() {
		jumpTextbox(this, 3, 'txtCellPhone1');
	});
	$('#txtCellPhone1').keyup(function() {
		jumpTextbox(this, 3, 'txtCellPhone2');
	});

	//Set visual change when rush survey is selected.
	$('#chkRushSurvey').click(function() {
		if(this.checked){
			$('#divRushSurvey').css('color', 'red');
		} else {
			$('#divRushSurvey').css('color', 'black');
		}
	});

	//Set the solution type drop-down.
	changeSolutionType();
	$('#selSolutionType').change(changeSolutionType);

	//Setup form Next and Back buttons.
	$('#locationNextButton').click(function() { displayUsageForm(); });
	$('#usageBackButton').click(function() { displayLocationForm(); });
	$('#usageNextButton').click(function() { displayConfirmationForm(); });
	$('#confirmationBackButton').click(function() { displayUsageForm(); });

	$('#submitButton').click(function() {
		hideErrorMessage();
		$.blockUI({ message: $('#workingMessage') });

		$('#formSiteSurvey').ajaxSubmit({
			success: function(responseText) {
				if (responseText != "") {
					$.unblockUI();
					showErrorMessage("Server Error: " + responseText + "|");
					scroll(0,0);
				} else {
					$.unblockUI();
					displayThankYouForm();
				}
			}
		});
	});

	displayLocationForm();

	/*
	Putting this after the displayLocationForm() call makes the cluetip go to
	the left, which is what I want. Putting it before displayLocationForm()
	seems to result in the cluetip going below the link.
	In the future we should probably determine exactly why this is happening.
	*/
	$('#rushSurveyLink').cluetip({
		sticky: true,
		closePosition: 'title',
		arrows: true,
		activation: 'click'
	});


});

function jumpTextbox(currentBox, maxSize, targetBox) {
	if(currentBox.value.length >= maxSize) {
		$('#'+targetBox).focus();
	}
}

function isBlank(inputID) {
	return($('#'+inputID).val() == "");
}

function setConfirmField(confirmID, inputID) {
	$('#'+confirmID).text($('#'+inputID).val());
}

function setConfirmPhoneNumber(confirmID, areaCodeID, firstPartID, lastPartID, extensionID) {
	if (
		$('#'+areaCodeID).val() != "" || 
		$('#'+firstPartID).val() != "" || 
		$('#'+lastPartID).val() != ""
	) {
		$('#'+confirmID).html(
			'(' + $('#'+areaCodeID).val() + ')&nbsp;' +
			$('#'+firstPartID).val() + '-' +
			$('#'+lastPartID).val()
		);
	}

	if (typeof extensionID != "undefined") {
		if ($('#'+extensionID).val() != "") {
			$('#'+confirmID).append(
				"&nbsp;Ext " + $('#'+extensionID).val()
			);
		}
	}
}

function setConfirmYesNoField(confirmID, inputID) {
	//ASSERT inputID is a check box element
	if ($('#'+inputID).get()[0].checked) {
		$('#'+confirmID).html('Yes');
	} else {
		$('#'+confirmID).html('No');
	}
}

function displayUsageForm() {
	$('#divUsageForm').show();
	$('#divLocationForm').hide();
	$('#divRequestForm').hide();
	$('#divThankYouForm').hide();

	$('#submitButton').attr('disabled', true);
	scroll(0,0);
}

function displayLocationForm() {
	$('#divUsageForm').hide();
	$('#divLocationForm').show();
	$('#divRequestForm').hide();
	$('#divThankYouForm').hide();

	$('#submitButton').attr('disabled', true);
	scroll(0,0);
}

function displayConfirmationForm() {
	populateConfirmPage();
	$('#divUsageForm').hide();
	$('#divLocationForm').hide();
	$('#divRequestForm').show();
	$('#divThankYouForm').hide();

	if ( isBlank('txtFirstName')&&
		isBlank('txtLastName')&&
		isBlank('txtFireNumber')&&
		isBlank('txtApartmentNumber')&&
		isBlank('txtRoad')&&
		isBlank('txtRR')&&
		isBlank('txtTown')&&
		isBlank('txtPostalCode') &&
		isBlank('txtAreaCodeHomePhone') &&
		isBlank('txtHomePhone1') &&
		isBlank('txtHomePhone2') &&
		isBlank('txtAreaCodeWorkPhone') &&
		isBlank('txtWorkPhone1') &&
		isBlank('txtWorkPhone2') &&
		isBlank('txtExtension') &&
		isBlank('txtAreaCodeCellPhone') &&
		isBlank('txtCellPhone1') &&
		isBlank('txtCellPhone2')
	) {
		showErrorMessage("No location data was entered. You must return to the previous form, by clicking the &quot;Back&quot; button at the bottom of this page,  and fill in some of the location fields before you can submit this request.");
		$('#submitButton').attr('disabled', true);
	} else {
		hideErrorMessage();
		$('#submitButton').attr('disabled', false);
	}
	scroll(0,0);
}

function populateConfirmPage() {
	setConfirmField('confirmCompanyName',       'txtCompanyName');
	setConfirmField('confirmFirstName',         'txtFirstName');
	setConfirmField('confirmLastName',          'txtLastName');
	setConfirmField('confirmFireNumber',        'txtFireNumber');
	setConfirmField('confirmApartmentNumber',   'txtApartmentNumber');
	setConfirmField('confirmRoad',              'txtRoad');
	setConfirmField('confirmRoadDirection',     'selRoadDirection');
	setConfirmField('confirmRR',                'txtRR');
	setConfirmField('confirmTown',              'txtTown');
	setConfirmField('confirmPostalCode',        'txtPostalCode');
	$('#confirmEntitlement').text($('input[name=radioEntitlement]:checked').val());
	setConfirmField('confirmEntitlementOther',  'txtEntitlementOther');
	setConfirmField('confirmEmail',             'txtEmail');
	setConfirmField('confirmDirections',        'txtDirections');
	setConfirmField('confirmReference',         'selReference');
	setConfirmField('confirmPromitionCode',     'txtPromotionCode');
	setConfirmField('confirmNumberOfComputers', 'txtComputerNumber');
	setConfirmField('confirmSolutionType',      'selSolutionType');

	setConfirmPhoneNumber('confirmHomePhone', 'txtAreaCodeHomePhone', 'txtHomePhone1', 'txtHomePhone2');
	setConfirmPhoneNumber('confirmWorkPhone', 'txtAreaCodeWorkPhone', 'txtWorkPhone1', 'txtWorkPhone2', 'txtExtension');
	setConfirmPhoneNumber('confirmCellPhone', 'txtAreaCodeCellPhone', 'txtCellPhone1', 'txtCellPhone2');

	setConfirmYesNoField('confirmRushSurvey',  'chkRushSurvey');
	setConfirmYesNoField('confirmVPN',         'chkVPN');
	setConfirmYesNoField('confirmPhotographs', 'chkPhotographs');
	setConfirmYesNoField('confirmAudioVideo',  'chkAudioVideo');
	setConfirmYesNoField('confirmYoutube',     'chkYoutube');
	setConfirmYesNoField('confirmVoip',        'chkVoip');
	setConfirmYesNoField('confirmP2P',         'chkP2P');
	setConfirmYesNoField('confirmMusic',       'chkMusic');
}

function displayThankYouForm() {
	populateThankYouPage();
	$('#divUsageForm').hide();
	$('#divLocationForm').hide();
	$('#divRequestForm').hide();
	$('#divThankYouForm').show();
	scroll(0,0);
}

function populateThankYouPage() {
	var thankYouMessage = 
		"<p>" + $('#confirmFirstName').text() + "&nbsp;" + $('#confirmLastName').text() + ",</p>" +
		"<p>Thank you for taking the time to complete our online request for site survey.</p>";
	if ($('#chkRushSurvey').get()[0].checked) {
		thankYouMessage += "<p>An Everus Team will be dispatched to your location within<br />3 business days.</p>";
	} else {
		thankYouMessage += "<p>An Everus Team will be dispatched to your location within 4 weeks.</p>";
	}
	thankYouMessage +=
		"<p>" + 
		"You will be contacted within 2 business days of the completed survey with" +
		"the results of your location's site survey." +
		"</p>" +
		"<p>The following is the contact information we have for you:</p>" +
		"<ul>" +
		"<li>" + $('#confirmFirstName').text() + "&nbsp;" + $('#confirmLastName').text() + "</li>";
	if ($('#confirmHomePhone').text() != "") {
		thankYouMessage += "<li>" + $('#confirmHomePhone').text() + "</li>";
	}
	if ($('#confirmWorkPhone').text() != "") {
		thankYouMessage += "<li>" + $('#confirmWorkPhone').text() + "</li>";
	}
	if ($('#confirmCellPhone').text() != "") {
		thankYouMessage += "<li>" + $('#confirmCellPhone').text() + "</li>";
	}
	if ($('#confirmEmail').text() != "") {
		thankYouMessage += "<li>" + $('#confirmEmail').text() + "</li>";
	}

	thankYouMessage += "</ul>" +
		"<p>We look forward to speaking with you soon!</p>";

	$('#thankYouMessage').append(thankYouMessage);
}

function changeSolutionType() {
	if ($('#selSolutionType').val() == 'Residential') {
		//Location page
		$('#companyNameRow').hide();
		$('#txtCompanyName').attr('disabled', true);
		$('#firstnameBusinessPrefix').hide();
		$('#lastnameBusinessPrefix').hide();
		$('#contact_disclaimer').html('(Please ensure that the contact name is a resident of the household.)');
		//Confirmation page
		$('#confirmCompanyNameItem').hide();
		$('#confirmFirstnameBusinessPrefix').hide();
		$('#confirmLastnameBusinessPrefix').hide();
	} else {
		//Location page
		$('#companyNameRow').show();
		$('#txtCompanyName').attr('disabled', false);
		$('#firstnameBusinessPrefix').show();
		$('#lastnameBusinessPrefix').show();
		$('#contact_disclaimer').html('');
		//Confirmation page
		$('#confirmCompanyNameItem').show();
		$('#confirmFirstnameBusinessPrefix').show();
		$('#confirmLastnameBusinessPrefix').show();
	}
}

function showErrorMessage(message) {
	$('#message').show();
	$('#message').html(message);
}

function hideErrorMessage() {
	$('#message').hide();
}
