/* 

EJ Kidd Quote Script
v. 1.0

last mod: 10-6-08
by: Mark

jQuery based onloads. Most of this is just example code. Please remove all unnecessary code (including this comment).


*/


/*################################################################################## ABT BASE Functions */


var filteredList = "";
var currentPage = 1;
var QuoteID = 0;
var ProdID = 0;
var lastShowcase = "";

function filterResults() {
	
	$("#refine-results-form form input:checkbox").click(function() {
		filteredList = "";
		$("#refine-results-form form input:checkbox:checked").each(function(){
			if ( filteredList != '') { 
				filteredList += ', ';
			}
			filteredList += $(this).val();
		});
		showGalleryItems(); 
	});
}


function filterShowCaseResults() {
	var elm = $("#refineFormShowcase");
	if (elm.length > 0) {
		$("#refine-results-form form input:radio").click(function() {
			showShowcaseItems($(this).val());
		});
		Shadowbox.setup( $('.sBox a'));
	}
}

function showShowcaseItems(v) {
	
	lastShowcase = v;
	currentPage = 1;
	displayShowcaseItems();
}

function displayShowcaseItems(){
	$("#product-showcase").load( 
		'/showcase-items.asp', 
		{ type: lastShowcase, page: currentPage }, 
		function(){ 
			Shadowbox.setup( $('.sBox a'));
		});
}

function showGalleryItems() {
	var elm = $("#product-gallery");
	if (elm.length > 0) {
		//alert( filteredList);
		elm.load( 
			'/gallery-items.asp', 
			{ filter: filteredList, page: currentPage }, 
			function(){ 
				Shadowbox.setup( $('.sBox a'));
			});
	}
}

function changePage(Page) {
	currentPage = Page;
	showGalleryItems();
	displayShowcaseItems();
}

function setQuoteOpenings() {
	$("#arch-info-form form input#add").click(function() {
		saveQuoteForm(true);
	});
	$("input#save").click(function() {
		saveQuoteForm(false);
	});
}

function saveQuoteForm(errorMsg){
	var isvalid = true;
	if ($("#qty").val() == "") { isvalid = false };
	if ($("#width").val() == "") { isvalid = false };
	if ($("#height").val() == "") { isvalid = false };
	if ($("#jamb").val() == "") { isvalid = false };
	if ($("#archtype").val() == "") { isvalid = false };
	if ($("#archtype").val() == "") { isvalid = false };
	if ($("#archcasing").val() == "") { isvalid = false };
	if ($("#columncasing").val() == "") { isvalid = false };
	if ($("#gradetype").val() == "") { isvalid = false };

	if (isvalid) {
		$("#line-item-body").load(
			'/quote-items.asp', { 
				quote: QuoteID,
				id: $("#openingID").val(), 
				qty: $("#qty").val(), 
				width: $("#width").val(), 
				height: $("#height").val(), 
				jamb: $("#jamb").val(), 
				archtype: $("#archtype").val(), 
				column: $("#column").val(), 
				keystone: $("#keystone").is(":checked"),
				bottom: $("#bottoms").is(":checked"), 
				capital: $("#capitals").is(":checked"), 
				archcasing: $("#archcasing").val(), 
				columncasing: $("#columncasing").val(),
				gradetype: $("#gradetype").val() });
		generateQuoteForm(0);
	} else {
		if (errorMsg) { document.getElementById("error-message").innerHTML = "<p>All fields are required!</p>"; }
	}
}


function deleteQuote(d){
	if ( confirm('Are you sure you want to delete this opening?')) {
		$("#line-item-body").load(
			'/quote-items.asp', 
			{ quote: QuoteID, del: d },
			function(){
				var iter = document.getElementById("openingIter").innerHTML;
				document.getElementById("openingIter").innerHTML = parseInt(iter) - 1;
			}
		);
	}
}

function editQuote(e){
	generateQuoteForm(e);
}

function generateQuoteForm(q){
	var elm = $("#add-line-form");
	if (elm.length > 0) {
		elm.load(
			'/quote-form.asp', 
			{ quote: QuoteID, id: q, ProdID:  ProdID}, 
			function(){ 
				setQuoteOpenings();
				$("#line-item-body").load( '/quote-items.asp', { quote: QuoteID });
				Shadowbox.setup( $('a.sBox'));
			});
	}
}

function setQuoteID(q){
	QuoteID = q;
}
function setProdID(p){
	ProdID = p;
}

function showReview(){
	var elm = $("#review-line-item-body");
	if (elm.length > 0) {
		elm.load(
			'/quote-items.asp', 
			{ quote: QuoteID, review: "review" }
		);
	}
}

$(document).ready(function() {	
	$("#refine-results-form form p.buttons").addClass("hide");
	$("#refine-results-form form").append("<div><p id=\"see-results\">See Results Below:</p></div>");
	showGalleryItems();
	
	filterResults();	
	filterShowCaseResults();
	generateQuoteForm(0);
	showReview();
	contactForm()
});

function contactForm(){
	var elm = $("#contact-form");
	if (elm.length > 0) {
		$('.jquery').hide();
		$('.jquery').prepend('<p id="jqueryGuide"></p>');
		$('.jquerySelect').change(function(){
			switch(jQuery.trim($(this).val())){
				case 'Archcraft':
					$('#jqueryGuide').html('');
					$('.jquery').hide();
					break;
				case 'Mouldings':
					$('#jqueryGuide').html('If you would like an estimate for mouldings, please include your preferred type, profile and species.');
					$('.jquery').show();
					break;
				case 'Shutters':
					$('#jqueryGuide').html('If you would like an estimate for shutters, please include your preferred material, style and measurements.');
					$('.jquery').show();
					break;
				case 'General Inquiry':
					$('#jqueryGuide').html('');
					$('.jquery').hide();
					break;
			}
		});
	}
}



