function nco_validateInput(theInput) {
	var isValid = true;
	
	if (theInput.val() == '') {
		theInput.addClass('invalidInput');
		isValid = false;
	} else {
		theInput.removeClass('invalidInput');
		theInput.add('validInput');
	}
	
	return isValid;
}

function nco_validateCheckbox(theInput) {
	var isValid = true;
	
	if ($(theInput).is(':checked')) {
		$('label[for="' + theInput.attr('id') + '"]').removeClass('invalidInput');
		theInput.addClass('validInput');
	} else {
		$('label[for="' + theInput.attr('id') + '"]').addClass('invalidInput');
		isValid = false;
	}
	
	return isValid;
}

function nco_validateSelection(theSelector) {
	var errorMessages = new Array();
	
	$(theSelector + String(' :input.requiredCheckout')).each(function(index) {
		validationResult = nco_validateInput($(this));
		if (validationResult !== true)
			errorMessages[errorMessages.length] = validationResult;
	});
	
	$(theSelector + String(' :input.requiredCheckbox')).each(function(index) {
		validationResult = nco_validateCheckbox($(this));
		if (validationResult !== true)
			errorMessages[errorMessages.length] = validationResult;
	});
	
	return errorMessages;
}

function nco_jumpToStep(step) {
	$('#' + String(step)).click();
}

function nco_checkCustomerInfoAndContinue() {
	errorMessages = nco_validateSelection('#customerInformationGuts');
	
	if (errorMessages.length == 0) {
		$("#customerInformationGuts div.validationErrors").fadeOut('fast');
		if ($('#hasAdditional').val() == 'yes')
			nco_jumpToStep('additionalQuestionsBar');
		else if ($('#shippingIsOn').val() == 'yes')
			nco_jumpToStep('shippingInfoBar');
		else
			nco_jumpToStep('paymentInfoBar');
	} else {
		var message = errorMessages.length == 1 ? 'There is an error with 1 field. It has been highlighted.' : 'There is an error with ' + errorMessages.length + ' fields. They have been highlighted.';
		$("#customerInformationGuts div.validationErrors").html(message);
		$("#customerInformationGuts div.validationErrors").fadeIn('fast');
	}
}

function nco_checkAdditionalQuestionsAndContinue() {
	errorMessages = nco_validateSelection('#additionalQuestionsGuts');
	
	if (errorMessages.length == 0) {
		$("#additionalQuestionsGuts div.validationErrors").fadeOut('fast');
	
		if ($('#shippingIsOn').val() == 'yes')
			nco_jumpToStep('shippingInfoBar');
		else 
			nco_jumpToStep('paymentInfoBar');
	} else {
		var message = errorMessages.length == 1 ? 'Please complete the highlighted mandatory field.' : 'Please complete the highlighted mandatory field.';
		$("#additionalQuestionsGuts div.validationErrors").html(message);
		$("#additionalQuestionsGuts div.validationErrors").fadeIn('fast');
	}
}

function nco_checkShippingInfoAndContinue() {
	errorMessages = nco_validateSelection('#shippingMethodGuts');
	
	if (errorMessages.length == 0) {
		$("#shippingMethodGuts div.validationErrors").fadeOut('fast');
		nco_jumpToStep('paymentInfoBar');
	} else {
		var message = errorMessages.length == 1 ? 'Please complete the highlighted mandatory field.' : 'Please complete the highlighted mandatory field.';
		$("#shippingMethodGuts div.validationErrors").html(message);
		$("#shippingMethodGuts div.validationErrors").fadeIn('fast');
	}
}

function nco_checkPaymentInfoAndContinue() {
	errorMessages = nco_validateSelection('#paymentInformationGuts');
	
	if (errorMessages.length == 0) {
		$("#paymentInformationGuts div.validationErrors").fadeOut('fast');
		nco_jumpToStep('reviewBar');
	} else {
		var message = errorMessages.length == 1 ? 'Please complete the highlighted mandatory field.' : 'Please complete the highlighted mandatory field.';
		$("#paymentInformationGuts div.validationErrors").html(message);
		$("#paymentInformationGuts div.validationErrors").fadeIn('fast');
	}
}

function nco_confirmAndPlaceOrder() {
	errorMessages = nco_validateSelection('#reviewGuts');
	
	$('#confirmationGuts').html('<div style="padding: 20px; text-align: center;">Processing Order...<br /><img src="tickets/images/loader-bar.gif" /></div>');
	$('#checkoutErrorMessageContainer').fadeOut('fast', function() { $('#checkoutErrorMessageContainer').html(''); });
	
	nco_jumpToStep('confirmationBar');
	$.ajax({
		url: 'tickets/engine/checkout/doOrder.php',
		type: 'post',
		data: $('#checkoutForm').serializeArray(),
		success: function(transport) {
			$('#confirmationGuts').html(transport);
		}
	});
}

function nco_setBillingToCustomer() {
	if ($('#billingSameAsShipping').is(':checked')) {
		$('.customerInfoInput').each(function(index) {
			$('#billing_' + String($(this).attr('id'))).val($(this).val());
		});
	}
}

function nco_setBillingToShippingOrNot() {
	nco_setBillingToCustomer();
}

function nco_loadErrorAndSwitch(jumpTo, errorMessage) {
	nco_jumpToStep(jumpTo);
	$('#checkoutErrorMessageContainer').html(Base64.decode(errorMessage));
	$('#checkoutErrorMessageContainer').fadeIn('fast');
}

function nco_clearCart() {
	updateTopRight();
	postToCart({});
}

function nco_hideLoginShowForm() {
	$('#inlineLoginBlock').slideUp('fast');
	$('#outerAccordionCheckout').slideDown('fast');
}

function nco_doInlineLogin() {
	var parameters = [ { name: 'loginTask', value: 'login' }, { name: 'login_username', value: $('#inlineLogin_username').val() }, { name: 'login_password', value: $('#inlineLogin_password').val() }];
	$('#inlineLoginBlock').html('<div style="text-align: center; padding: 20px;"><br /><img src="tickets/images/loader-circle.gif" /></div>');
	$.ajax({
		url: 'tickets/engine/pages/inlineLoginNew.php',
		type: 'POST',
		data: parameters,
		success: function(transport) {
			$('#inlineLoginBlock').html(transport);
		}
	});
}

function nco_continueWithoutLoggingIn() {
	nco_hideLoginShowForm();
}

function nco_updatePartyMember1() {
	try { $('#partyMembers_0').val($('#firstName').val() + ' ' + $('#lastName').val()); } catch (caught) {}
	
	try { $('#partyMembersEmail_0').val($('#emailAddress').val()); } catch (caught) {}
	
	try { $('#partyMemberBirthMonths_0').val($('#birthMonth').val()); } catch (caught) {}
	try { $('#partyMemberBirthDays_0').val($('#birthDay').val()); } catch (caught) {}
	try { $('#partyMemberBirthYears_0').val($('#birthYear').val()); } catch (caught) {}
}

function nco_updateReviewOrder() {
	var shippingString = '';
	shippingString += $('#firstName').val() + ' ' + $('#lastName').val() + '<br />';
	shippingString += $('#address').val() + '<br />' + $('#city').val() + ', ' + $('#state').val() + ' ' + $('#zip').val() + '<br />';
	shippingString += 'Email: ' + $('#emailAddress').val() + '<br />Phone: ' + $('#phone').val() + '<br />';
	$('#shippingSummary').html(shippingString);
	
	var billingString = '';
	billingString += $('#billing_firstName').val() + ' ' + $('#billing_lastName').val() + '<br />';
	billingString += $('#billing_address').val() + '<br />' + $('#billing_city').val() + ', ' + $('#billing_state').val() + ' ' + $('#billing_zip').val() + '<br />';
	billingString += 'Email: ' + $('#billing_emailAddress').val() + '<br />Phone: ' + $('#billing_phone').val() + '<br />';
	$('#billingSummary').html(billingString);
	
	//payment info
	var cardInfo = '';
	if ($('#paymentMethod').val() == 'later') {
		$('#creditReviewBox').hide();
		$('#laterReviewBox').show();
	} else {
		var cardNumber;
		if ($('#cardNumber').val() > 4) {
			cardNumber = Array($('#cardNumber').val().length - 4 + 1).join("*");
			cardNumber += $('#cardNumber').val().substring($('#cardNumber').val().length - 5, $('#cardNumber').val().length - 1);
		} else if ($('#cardNumber').val() > 0) {
			cardNumber = Array($('#cardNumber').val().length + 1).join("*");
		} else
			cardNumber = '';
		
		cardExpiration = $('#cardMonth').val() + '/' + $('#cardYear').val();
		
		cardCcv = '***';
		
		cardInfo = '<div class="cardNumberBox">' + cardNumber + '</div>';
		cardInfo += '<div class="expirationBox">' + cardExpiration + '</div>';
		cardInfo += '<div class="ccvBox">' + cardCcv + '</div>';
		
		$('#billingMethodSummaryText').html(cardInfo);
	
		$('#laterReviewBox').hide();
		$('#creditReviewBox').show();
	}
}

function nco_reQuantifyTicketsAndHighlight() {
	parameters = $('#cartQuantityForm').serializeArray();
	parameters[parameters.length] = { name: 'cartTask', value: 'update' };
	var usingSmallCart = ($('#usingSmallCart').val() == '1' ? true : false);
	$.ajax({
		url: (usingSmallCart ? 'tickets/engine/checkout/smallCart.php' : 'tickets/engine/pages/cart.php'),
		type: 'POST',
		data: parameters,
		success: function(transport) {
			$('#shoppingCartContents').html(transport);
			$('.quantitySmall .cartInput').effect("highlight", {}, 1000);
		}
	});
}
