$(function() {
});

function CheckFields(objID) {
	var elem = document.getElementById(objID).elements;
	
	for(var a = 0; a < elem.length; a++) {
		if(!elem[a].getAttribute("checkMandatory")) {
			continue;
		}

		if(elem[a].value == '' || elem[a].value == ' ') {
			alert('Please complete the ' + elem[a].getAttribute("checkMandatory") + ' before continuing.');
			elem[a].focus();
			return false;
		}
	}
	return true;
}

function OpenDialog(payload) {
	payload += '&KeepThis=true&TB_iframe=true&width=700&height=550';
	tb_show(null, payload, false);
}

function OpenDialog2(payload) {
	$('<div>').dialog({
		modal: true,
		open: function () {
			$(this).load(payload);
		},
		height: 650,
		width: 600,
		title: 'Paolos Gelato'
	});
}

function ConfirmRedirect(msg, payload) {
	if(confirm(msg)) {
		window.location = payload;
	}
}

function Toggle_FlashVideo() {
	$('#ProductVideoIntro').fadeOut('slow');
}

function Toggle_FadeInVideoIntro() {
	$('#ProductVideoIntro').fadeIn(2000);
}

var mgm_not_selected = '/images/mgm_not_selected.gif';
var mgm_selected = '/images/mgm_selected.gif';

function Toggle_MGM_Step1(step1Opt) {
	var prevVal = $('#Step1').val();
	
	$('.Step1IMG').each(function() {
		$(this).attr('src', mgm_not_selected);
	});
	
	$('#Step1').val(step1Opt);
	$('#Step1_' + step1Opt).attr('src', mgm_selected);
}

function Toggle_Submit_Step1() {
	if($('#Step1').val() == "") {
		alert("Please select a Base Flavor before continuing.");
	} else {
		$('#Step1Form').submit();
	}
}

function Toggle_MGM_Step2(step2Opt) {
	var CurPosition = null;
	
	//	Loop through the already selected values to see if they are unchecking one...
	for(var a = 1; a <= 6; a++) {
		if($('#Step2_' + a).val() == step2Opt) {
			$('#Step2_' + a).val('');
			$('#Step2_' + step2Opt).attr('src', mgm_not_selected);
			
			return false;
		}
	}
	
	//	Loop through the available values...
	for(var a = 1; a <= 6; a++) {
		if($('#Step2_' + a).val() == '') {
			CurPosition = a;
			break;
		}
	}
	
	if(CurPosition == null) {
		alert('Sorry, but you have already chosen 6 Flavors.\n\nPlease uncheck an ingredient to choose more.');
	} else {
		var prevVal = $('#Step2').val();
		
		$(this).attr('src', mgm_selected);
		
		$('#Step2_' + CurPosition).val(step2Opt);
		$('#Step2_' + step2Opt).attr('src', mgm_selected);
	}
}

function Toggle_Submit_Step2() {
	var TotalSelected = 0;
	var Atleast1Set = false;
	
	for(var a = 1; a <= 6; a++) {
		if($('#Step2_' + a).val() != "") {
			TotalSelected++;
			Atleast1Set = true;
		}
	}
	
	/*alert(TotalSelected);
	if(!Atleast1Set) {
		alert("Please choose at least 1 Ingredient flavor.");
	} else */if(TotalSelected != 1 && TotalSelected != 2 && TotalSelected != 3 && TotalSelected != 6) {
		alert("Please choose an Ingredient combination of either 1 flavor, 2 flavors, 3 flavors or 6 flavors.");
	} else {
		$('#Step2Form').submit();
	}
}

function Toggle_MGM_Step3(step3Opt) {
	var prevVal = $('#Step3').val();
	
	$('.Step3IMG').each(function() {
		$(this).attr('src', mgm_not_selected);
	});
	
	$('#Step3_Pickup').val(step3Opt);
	$('#Step3_' + step3Opt).attr('src', mgm_selected);
}

function Toggle_Submit_Step3() {
	if($('#Step3_Name').val() == "") {
		alert("Please enter a Gelato Name before continuing.");
	} else if($('#Step3_Pickup').val() == "") {
		alert("Please select a Pick Up location for your Gelato.");
	} else {
		$('#Step3Form').submit();
	}
}
