﻿var pkgs = 0;
var okToScroll = false;

google.load("jquery", "1.4.2");
google.load("jqueryui", "1.8.1");
google.setOnLoadCallback(function () {
	var toggleOnLoad = "";

	$('.datepicker').datepicker({ showAnim: '', dateFormat: 'm/d/yy' });
	$('.datepicker.future').datepicker('option', 'minDate', '+1');

	$('#go').click(function () {
		$('#totalPackages').val(totalPackages());
		$('#btnSubmit').click();
	});

	$('#chkServiceItemOnly').change(function () {
		if ($(this).is(':checked')) {
			$('#use-prm').hide();
			//$('#package-details-heading').hide();
			$('#package-details').hide();
			$('#package-details-heading').css('color', '#888888');
		}
		else {
			$('#use-prm').show();
			//$('#package-details-heading').show();
			$('#package-details').show();
			$('#package-details-heading').css('color', '');
			if ($('#rbnAddPackages').is(':checked')) {
				$('#packageNameList').show();
			}
		}
	});

	$('#chkFTPOrder').change(function () {
		if ($(this).is(':checked')) {
			$('#trNumberOfCDs').hide();
		}
		else {
			$('#trNumberOfCDs').show();
		}
	});

	$('h3.expands > input[type="checkbox"]').click(function () {
		okToScroll = true;
	});

	$('h3.expands > input[type="checkbox"]').change(function () {
		var chkID = '#' + $(this).attr('id');
		if ($(this).is(':checked')) {
			$(chkID + '-options').slideDown("fast", function () { if (okToScroll) { $.scrollTo(chkID, { duration: '1000' }) }; });
			$(this).parent().addClass('expanded');
		}
		else {
			$(chkID + '-options').slideUp("fast");
			$(this).parent().removeClass('expanded');
		}
	});

	$('input[name="ShootNumber"]').change(function () {
		if ($('#rbnShoot1').is(':checked')) {
			$('.first-shoot').hide();
		}
		else {
			$('.first-shoot').show();
		}
	});

	$('input[name="RelatedGroup"]').change(function () {
		if ($('#rbnRelatedGroupNo').is(':checked')) {
			$('.related-group').hide();
		}
		else {
			$('.related-group').show();
		}
	});

	$('input[name="rblIDShipping"]').change(function () {
		if ($('#rblIDShipping_2').is(':checked')) {
			$('#id-shipping-date').show();
		}
		else {
			$('#id-shipping-date').hide();
			$('#txtIDDateNeeded').val('');
		}
	});

	$('input[name="rblChildFindShipping"]').change(function () {
		if ($('#rblChildFindShipping_2').is(':checked')) {
			$('#childfind-shipping-date').show();
		}
		else {
			$('#childfind-shipping-date').hide();
			$('#txtIDDateNeeded').val('');
		}
	});

	$('input[name="rblYearbookShipping"]').change(function () {
		if ($('#rblYearbookShipping_2').is(':checked')) {
			$('#yearbook-shipping-date').show();
		}
		else {
			$('#yearbook-shipping-date').hide();
			$('#txtYearbookDateNeeded').val('');
		}
	});

	$('#ddlYearbookCDName').change(function () {
		if ($(this).val() != "") {
			$('#fsYearbookShipping').show();
		}
		else {
			$('#fsYearbookShipping').hide();
		}
	});

	$('input[name="rblYearbookAdminShipping"]').change(function () {
		if ($('#rblYearbookAdminShipping_2').is(':checked')) {
			$('#yearbook-admin-shipping-date').show();
		}
		else {
			$('#yearbook-admin-shipping-date').hide();
			$('#txtYearbookAdminDateNeeded').val('');
		}
	});

	$('#ddlYearbookAdminSoftware').change(function () {
		if ($(this).val() != "") {
			$('#fsYearbookAdminShipping').show();
		}
		else {
			$('#fsYearbookAdminShipping').hide();
		}
	});

	$('input[name="rblCompDeferred"]').change(function () {
		if ($('#rblCompDeferred_1').is(':checked')) {
			$('#comp-deferred-date').show();
			$('#comp-delivery-date').hide();
			$('#txtCompDeliveryDate').val('');
		}
		else {
			$('#comp-delivery-date').show();
			$('#comp-deferred-date').hide();
			$('#txtCompDeferredDate').val('');
		}
	});

	$('input[name="UseFile"]').change(function () {
		if ($('#rbnUsePRM').is(':checked')) {
			$('#package-details').hide();
			$('#packageNameList').hide();
			$('#use-prm-note').show();
			$('#use-bubble-note').hide();
		}
		else if ($('#rbnUseBubble').is(':checked')) {
			$('#package-details').hide();
			$('#packageNameList').hide();
			$('#use-bubble-note').show();
			$('#use-prm-note').hide();
		}
		else if ($('#rbnAddPackages').is(':checked')) {
			$('#package-details').show();
			$('#packageNameList').show();
			$('#use-bubble-note').hide();
			$('#use-prm-note').hide();
			if ($('#chkServiceItemOnly').is(':checked')) {
				$('#packageNameList').hide();
			}
		}
	});

	$('#ddlOrderType').change(function () {
		$('#trCameraCard').hide();
		$('#trProofPlan').hide();
		if ($(this).val() == "Proof Plan") {
			$('#trProofPlan').show();
		}
		else if ($(this).val() == "Camera Card") {
			$('#trCameraCard').show();
		}
	});

	function CheckToggle(chkbox, toggle, reverse) {
		reverse = typeof (reverse) != 'undefined' ? reverse : false;
		$(chkbox).change(function () {
			if ($(this).is(':checked')) {
				reverse ? $(toggle).hide() : $(toggle).show();
			}
			else {
				reverse ? $(toggle).show() : $(toggle).hide();
			}
		});
		toggleOnLoad += chkbox + ", ";
	}

	function DropdownToggle(ddl, val, toggle) {
		$(ddl).change(function () {
			if ($(this).val() == val) {
				$(toggle).show();
			}
			else {
				$(toggle).hide();
			}
		});
		toggleOnLoad += ddl + ", ";
	}

	function RadioToggle(group, btn, toggle) {
		$('input[name="' + group + '"]').change(function () {
			if ($(btn).is(':checked')) {
				$(toggle).show();
			}
			else {
				$(toggle).hide();
			}
		});
		toggleOnLoad += 'input[name="' + group + '"], ';
	}

	CheckToggle('#chkShipToSchool', '#fsSchoolAddress, #drop-ship-note');
	CheckToggle('#chkColorCorrect', '#color-correct-note');
	CheckToggle('#chkCompPrincipal', '#chkCompPrincipalText');
	CheckToggle('#chkCompVicePrincipal', '#chkCompVicePrincipalText');
	CheckToggle('#chkCompAsstPrincipal', '#chkCompAsstPrincipalText');
	CheckToggle('#chkCompSuperintendent', '#chkCompSuperintendentText');
	CheckToggle('#chkCompPastor', '#chkCompPastorText');
	CheckToggle('#chkAddReorderCentral', '#reorder-central-type');
	CheckToggle('#chkIDBarcode', '#id-barcode-type');
	DropdownToggle("#ddlIDBorder", "Custom RGB", ".id-border-custom");
	DropdownToggle("#ddlIDText", "Custom RGB", ".id-text-custom");
	DropdownToggle("#ddlCompFontStyle", "Custom Font", "#comp-custom-font-note");
	DropdownToggle("#ddlYearbookAdminSoftware", "WinSchool", "#yearbook-software-winschool");
	DropdownToggle("#ddlYearbookAdminSoftware", "Other", "#yearbook-software-other");
	DropdownToggle("#ddlYearbookCDName", "Other", "#yearbook-cd-other");
	RadioToggle('rblCameraCards', '#rblCameraCards_1', '#camera-cards-email-date');
	RadioToggle('IDCardDesign', '#rbnIDCustom', '#rbnIDCustom-options');
	RadioToggle('IDCardDesign', '#rbnIDStandard', '#rbnIDStandard-options');
	RadioToggle('CompSize', '#rbnCompSizeCustom', '#rbnCompSizeCustomText');
	RadioToggle('rblServiceStripSort', '#rblServiceStripSort_3', '#txtServiceStripSortOther');
	RadioToggle('rblMugBookSort', '#rblMugBookSort_3', '#txtMugBookSortOther');
	RadioToggle('rblYearbookStickerSort', '#rblYearbookStickerSort_3', '#txtYearbookStickerSortOther');
	RadioToggle('rbl5upYearbookStripSort', '#rbl5upYearbookStripSort_3', '#txt5upYearbookStripSortOther');
	RadioToggle('rblStickerServiceStripSort', '#rblStickerServiceStripSort_3', '#txtStickerServiceStripSortOther');

	CheckToggle('#chkQuixiDataFile', '#trQuixiDataFile');
	CheckToggle('#chkQuixiBubbleConfig', '#trQuixiBubbleConfig');
	CheckToggle('#chkQuixiBubbleConfig', '#package-details', true);
	CheckToggle('#chkQuixiBubbleConfig', '#trQuixiBubbleNote', true);

	/* --- Package Details ---*/
	$('#add-package').button().click(function (event) {
		if ($('#packageNameList').is(':visible')) {
			$('#packageNameList').hide();
			$('#emailStudioPRM').hide();
			$(this).text("Add packages »");
		}
		else {
			$('#packageNameList').show();
			$('#emailStudioPRM').show();
			$(this).text("Hide list «");
		}
	});

	$('#packageNameList a').click(function () {
		var self = $(this);
		if (self.hasClass('used')) {
			$("#userPackageList div[rel='" + self.attr('id') + "']").find('.remove-package').click();
			self.removeClass('used');
		}
		else {
			var pkgName = self.attr('id').split("packageName")[1].replace(/_/g, ' ').replace(/-/g, ':');
			var pkgClass = self.attr('class') || "regular-package";
			var pkg = $('#templates .' + pkgClass).clone(true);
			pkg.find('.package-name').text("Package " + pkgName);
			pkg.find('.add-item').click();
			pkg.find('.remove-item').remove();
			pkg.attr('rel', self.attr('id'));
			self.addClass('used');

			// Insert in list order
			var prev = self.prev();
			var pkgBefore;
			var found = false;
			while (prev.length > 0 && !found) {
				pkgBefore = $("#userPackageList div[rel='" + prev.attr('id') + "']");
				if (pkgBefore.length > 0) {
					found = true;
				}
				prev = prev.prev();
			}
			(found) ? $(pkgBefore).after(pkg) : pkg.prependTo('#userPackageList');

			resetPackageRowBreaks();
		}
	});

	$('.add-item').click(function () {
		var newItem = $('#templates .package-item:first').clone(true);
		newItem.insertBefore($(this));
	});

	$('.remove-item').click(function () {
		var thisItem = $(this).parent();
		var otherItems = thisItem.siblings('.package-item');
		if (otherItems.length > 0) {
			thisItem.remove();
		}
	});

	$('.remove-package').click(function () {
		var parent = $(this).parent();
		var pkgID = parent.attr('rel');
		$('#packageNameList #' + pkgID).removeClass('used');
		parent.remove();
		resetPackageRowBreaks();
	});

	$('#close-packages').button().click(function () {
		$('#add-package').click();
	});

	function resetPackageRowBreaks() {
		$('#userPackageList .package-row-break').remove();
		$('#userPackageList .user-package:nth-child(3n)').after('<div class="package-row-break"></div>');
	}

	function totalPackages() {
		var pkgs = $('#userPackageList .user-package');
		var pkgString = "";
		var thisPkg, thisItem;
		for (var i = 0; i < pkgs.length; i++) {
			if (i > 0) { pkgString += "|"; }
			thisPkg = $(pkgs[i]);
			pkgString += thisPkg.attr('rel') + ":";

			var items = thisPkg.find('.package-item');
			for (var j = 0; j < items.length; j++) {
				if (j > 0) { pkgString += ","; }
				thisItem = $(items[j]);
				pkgString += thisItem.find('.item-name').val() + "x" + thisItem.find('.item-qty').val();
			}
		}
		return pkgString;
	}

	/* --- Subject Details ---*/
	var subjectLines = 0;

	$('#quickfix-go').click(function () {
		$('#totalSubjects').val(TotalSubjects());
		if (ValidateSubjects()) {
			$('#btnSubmit').click();
		}
		else {
			$('#subject-validation-summary').show();
		}
	});

	$('.subject-package').change(function () {
		var self = $(this);
		if (self.val() != "0") {
			var newLine = $('#templates .item-line').clone(true);
			newLine.find('.item-name').text('Package ' + self.val());
			self.parents('.add-line').before(newLine);
			self.val(0);
			ValidateSubjects();
		}
	});

	$('.subject-unit').change(function () {
		var self = $(this);
		if (self.val() != "0") {
			var newLine = $('#templates .item-line').clone(true);
			newLine.find('.item-name').text(self.find("option[value='" + self.val() + "']").text());
			self.parents('.add-line').before(newLine);
			self.val(0);
			ValidateSubjects();
		}
	});

	$('#add-subject').click(function () {
		var newLine = $('#templates .subject-table').clone(true);
		subjectLines++;
		newLine.find('.subject-number').text(subjectLines + ' )');
		$('#subjects').append(newLine);
	});

	$('.empty').focus(function () {
		var self = $(this);
		if (self.hasClass('empty')) {
			self.attr('rel', self.val()).val('').removeClass('empty');
		}
	}).blur(function () {
		var self = $(this);
		if (self.val() == "") {
			self.val(self.attr('rel')).addClass('empty');
		}
	});

	$('.subject-filename').blur(function () {
		ValidateSubjects();
	});

	$('.remove-subject-item').click(function () {
		$(this).parents('.item-line').remove();
	});

	$('.remove-subject').click(function () {
		$(this).parents('.subject-table').remove();
		subjectLines--;
		$('#subjects .subject-number').each(function (index) {
			$(this).text(index + 1 + ' )');
		});
	});

	$('#chkProblemOther').change(function () {
		if ($(this).is(':checked')) {
			$('#problem-other').show();
			$('#txtProblemOther').focus();
		}
		else {
			$('#problem-other').hide();
		}
	});

	function ValidateSubjects() {
		var valid = true;
		var filenames = $('#subjects .subject-filename');
		for (var i = 0; i < filenames.length; i++) {
			if ($(filenames[i]).val() == "") {
				$(filenames[i]).addClass('error').next().show();
				valid = false;
			}
			else {
				$(filenames[i]).removeClass('error').next().hide();
			}
		}
		$('#subjects .subject-items').each(function (index) {
			if ($(this).find('.item-line').length < 1) {
				$(this).find('.add-line select').addClass('error');
				$(this).find('.subject-unit').next().show();
				valid = false;
			}
			else {
				$(this).find('.add-line select').removeClass('error');
				$(this).find('.subject-unit').next().hide();
			}
		});

		if (valid) {
			$('#subject-validation-summary').hide();
		}

		return valid;
	}

	function TotalSubjects() {
		var pkgs = $('#subjects .subject-table');
		var pkgString = "";
		var thisPkg, thisItem;
		for (var i = 0; i < pkgs.length; i++) {
			if (i > 0) { pkgString += "|"; }
			thisPkg = $(pkgs[i]);
			pkgString += thisPkg.find('.subject-name').val() + ';';
			pkgString += thisPkg.find('.subject-filename').val() + ';';
			pkgString += thisPkg.find('.subject-background').val() + ';';
			pkgString += thisPkg.find('.subject-pose').val() + ':';
			var items = thisPkg.find('.item-line');
			for (var j = 0; j < items.length; j++) {
				if (j > 0) { pkgString += ","; }
				thisItem = $(items[j]);
				pkgString += thisItem.find('.item-name').text() + "$" + thisItem.find('.item-qty select').val();
			}
		}
		return pkgString;
	}

	$('#add-subject').click();
	$('#subjects .remove-subject').remove();
	if ($.fn.tooltip) {
		$('.tooltip').tooltip({
			showURL: false,
			track: true,
			fade: 250,
			delay: 0
		}).unbind('click');
	}

	$('#chkFTPOrder, h3.expands > input[type="checkbox"], input[name="ShootNumber"], input[name="RelatedGroup"], input[name="rblIDShipping"], input[name="rblChildFindShipping"],input[name="rblYearbookShipping"], input[name="rblYearbookAdminShipping"], input[name="rblCompDeferred"], input[name="UseFile"], #ddlOrderType, #chkServiceItemOnly, ' + toggleOnLoad).change();
});

function ValidateSchoolAddress(source, args) {
	var form = document.aspnetForm;
	if (form && form.chkShipToSchool) {
		if (form.chkShipToSchool.checked) {
			args.IsValid = (args.Value.length > 0);
			//if (args.Value.length < 1) args.IsValid = false;
			//else args.IsValid = true;
		} else args.IsValid = true;
	}
	return;
}

function ValidatePackages(source, args) {
	var form = document.aspnetForm;
	if (form && form.rbnUsePRM && form.rbnUseBubble && form.rbnAddPackages && form.filePRM && form.fileBubble && form.chkServiceItemOnly) {
		//if (form.chkServiceItemOnly.checked || form.chkUsePRM.checked || $('#userPackageList .user-package').length > 0) args.IsValid = true;
		if (form.chkServiceItemOnly.checked ||
			(form.rbnUsePRM.checked && form.filePRM.value != "") ||
			(form.rbnUseBubble.checked && form.fileBubble.value != "") ||
			(form.rbnAddPackages.checked && $('#userPackageList .user-package').length > 0)) {
			args.IsValid = true;
		}
		else args.IsValid = false;
	}
	return;
}

function ValidateNumberOfCDs(source, args) {
	var form = document.aspnetForm;
	if (form && form.txtNumberOfCDs) {
		if (form.chkFTPOrder && form.chkFTPOrder.checked) args.IsValid = true;
		else args.IsValid = (args.Value.length > 0);
	}
	return;
}

function ValidateGroupNumber(source, args) {
	var form = document.aspnetForm;
	if (form && form.txtGroupNumber) {
		if (form.rbnRelatedGroupYes && !form.rbnRelatedGroupYes.checked) args.IsValid = true;
		else args.IsValid = (args.Value.length > 0);
	}
	return;
}

function ValidateQuixiPackages(source, args) {
	var form = document.aspnetForm;
	if (form && form.chkQuixiBubbleConfig) {
		if ((form.chkQuixiBubbleConfig.checked && form.fileQuixiBubbleConfig.value != "") || $('#userPackageList .user-package').length > 0) args.IsValid = true;
		else args.IsValid = false;
	}
	return;
}
