/*
 * Filename: 		namelabels.js 
 * Title: 			NameLabels Client-Side Functions 
 * Description: 	Custom function set for NoMine name labels websites. 
 * Author: 			Sean Hamill - Clicapic Media 
 * Author URI: 		http://www.clicapic.com/
 * Copyright: 		Copyright 2011, Clicapic Media
 * Date: 			25 April 2011 
 */

function checkName() {
	if ($('#name1').val()) { $('#step1_1').addClass("done"); }
	else { $('#step1_1').removeClass("done"); }
}

function checkAddress() { 
	addressLabels = [30, 307, 129, 132, 134];
	addressRequired = false;
	for (k=0; k<5; k++) {
		if ($('#qty'+addressLabels[k]).val() != "0") { addressRequired = true; } 
	}
	if (addressRequired)        { $('#step1_2').removeClass("hidden"); }
	if (($('#address1').val()) || ($('#address2').val()) || ($('#address3').val()) || ($('#address4').val())) { $('#step1_2').removeClass("attention"); $('#step1_2').addClass("done"); }
	if ((!($('#address1').val())) && (!($('#address2').val())) && (!($('#address3').val())) && (!($('#address4').val()))) { $('#step1_2').addClass("attention"); $('#step1_2').removeClass("done"); }
	if (!addressRequired) 		{ $('#step1_2').addClass("hidden"); }
}

function checkProducts() {
	labels   = [8, 30, 307, 23, 39, 309, 42];
	packages = [125, 129, 132, 134, 136, 138, 582];
	labelsOrdered = false;
	packagesOrdered = false;
	for (i=0; i<7; i = i+1) {
		if ($('#qty'+labels[i]).val() != 0) { labelsOrdered = true; }						
	}
	for (j=0; j<7; j = j+1) {
		if ($('#qty'+packages[j]).val() != 0) { packagesOrdered = true; } 
	}
	if (labelsOrdered) 	 { $('#step2_1').addClass("done"); $('#step2_2').removeClass("attention"); } else { $('#step2_1').removeClass("done"); }
	if (packagesOrdered) { $('#step2_2').addClass("done"); $('#step2_1').removeClass("attention"); } else { $('#step2_2').removeClass("done"); }
}

function checkEffect() {
	if (typeof effectOptions !== 'undefined') {
		if ($('input[name=effect]:checked').val()) { $('#step3_4').addClass("done"); }	else { $('#step3_4').removeClass("done"); } 
		if ($('#radio000').is(':checked')) { 
			$('#step3_4').addClass("hidden");
			$('#options-effect').addClass("hidden");
			$('#effects').addClass("hidden");
		} 
		else { 
			$('#step3_4').removeClass("hidden");
			$('#options-effect').removeClass("hidden");
			$('#effects').removeClass("hidden");
		}
	}
}	

function total(id, qty, price){
	oldSubTotal = parseFloat(document.getElementById('subtotal'+id).value); if (!oldSubTotal) { oldSubTotal = 0; } 
	newSubTotal = parseFloat(price * qty).toFixed(2);
	change = newSubTotal - oldSubTotal; 									
	oldTotal = parseFloat(document.getElementById('total').value); if(!oldTotal) { oldTotal = 0; }
	newTotal = (oldTotal + change).toFixed(2);
	document.getElementById('subtotal'+id).setAttribute('value', newSubTotal);
	document.getElementById('total').setAttribute('value', newTotal);									
	if (qty != 0) { document.getElementById('product'+id).setAttribute('class', 'highlight'); }
	else { document.getElementById('product'+id).removeAttribute('class', 'highlight'); }
}

function validate() { 
	errors = 0; // Reset Errors
	msg = "";   // Reset Error Messages
	checkProducts();
	
	if (!($('#name1').val())) // Validate Name Field				
			{ $('#step1_1').addClass("attention"); errors++; msg += "\n -Please enter a name";} 	
	   else { $('#step1_1').addClass("done"); }
	   
	if ((!($('#address1').val())) && (!($('#step1_2').hasClass("hidden")))) // Validate Address Entry
			{ $('#step1_2').addClass("attention"); errors++; msg += "\n -Please enter an address";} 
	   else { $('#step1_2').addClass("done"); }
	   
	if (!($('#step2_1').hasClass("done")) && (!($('#step2_2').hasClass("done")))) // Validate Products			
			{ $('#step2_1').addClass("attention"); errors++; msg += "\n -Please select the labels/packages you wish to purchase";} 
	   else { $('#step2_1').addClass("done"); }
	   
	if (!($('input[name=font]:checked').val())) // Validate Font 
			{ $('#step3_1').addClass("attention"); errors++; msg += "\n -Please choose a font";} 
	   else { $('#step3_1').addClass("done"); }
	   	   
	if (!($('input[name=colour]:checked').val())) // Validate Colour	
			{ $('#step3_2').addClass("attention"); errors++; msg += "\n -Please choose a colour";} 
	   else { $('#step3_2').addClass("done"); }
	   
	if (!($('input[name=icon]:checked').val())) // Validate Icon	
			{ $('#step3_3').addClass("attention"); errors++; msg += "\n -Please choose an icon or select 'No Icon'";} 
	   else { $('#step3_3').addClass("done"); }
	
	if (typeof effectOptions !== 'undefined') {  
		if ((!($('input[name=effect]:checked').val())) && (!($('#step3_4').hasClass("hidden")))) // Validate Effect
				{ $('#step3_4').addClass("attention"); errors++; msg += "\n -Please choose an effect";} 
		if ($('input[name=effect]:checked').val()) { $('#step3_4').addClass("done"); }
	}
	if (errors == 0) { 
		document.classicForm.submit();
	}
	if (errors == 1) {
		alert("There was a small problem with your input, please check the error below and re-submit the form\n"+msg); 
	}
	if (errors > 1) { 
		alert("There were some problems with your input, please check the errors below and re-submit the form\n"+msg);
	}
}		

function clearForm() {
	$("#name1").val("");
	$("#name2").val("");
	$("#address1").val("");
	$("#address2").val("");
	$("#address3").val("");
	$("#address4").val("");	
	productId = [8, 30, 307, 23, 39, 309, 42, 125, 129, 132, 134, 136, 138, 582];
	for (i = 0; i < 14; i++) {
		$("#qty"+productId[i]).val(0);
		total(productId[i], 0, 0) 
	}
	$("input:radio").attr("checked", false)
	$(".highlight").removeClass("highlight");
	$(".done").removeClass("done");
	$(".attention").removeClass("attention");
	scrollToPoint("details");
}

var lastFont 	= 0;
var lastColour 	= 0;
var lastIcon	= 0;
var lastEffect	= 0;

function toggleFont(id){
	if (lastFont) { $("#"+lastFont).removeClass("highlight") };
	$("#"+id).addClass("highlight");
	if ($('#step3_1')) { $('#step3_1').addClass("done"); }
	lastFont = id;
}

function toggleColour(id){
	if (lastColour) { $("#"+lastColour).removeClass("highlight") };
	$("#"+id).addClass("highlight");
	if ($('#step3_2')) { $('#step3_2').addClass("done"); }
	lastColour = id;
}

function toggleIcon(id){
	if (lastIcon) { $("#"+lastIcon).removeClass("highlight") };
	$("#"+id).addClass("highlight");
	if ($('#step3_3')) { $('#step3_3').addClass("done"); }
	lastIcon = id;
	checkEffect();
}

function toggleEffect(id) {
	if (typeof effectOptions !== 'undefined') {
		if (lastEffect) { $("#"+lastEffect).removeClass("highlight") };
		$("#"+id).addClass("highlight");
		if ($('#step3_4')) { $('step3_4').addClass("done"); }
		lastEffect = id;
		checkEffect();
	}
}

function scrollToPoint(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'300');
}

;(function($) {
    $.fn.textfill = function(options) {
        var fontSize = options.maxFontPixels;
        var ourText = $('.resizeable', this);
        var maxHeight = $(this).height();
        var maxWidth = $(this).width();
        var textHeight;
        var textWidth;
        do {
			ourText.css('font-size', fontSize);
            textHeight = ourText.height();
            textWidth = ourText.width();
            fontSize = fontSize - 1;
        } while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3);
        return this;
    }
})(jQuery);
