var gsizeName;
var gcake=-1;
var gfilling=-1;
var gFillingMax=4;
//var gArr="0;2;4;6;8;10;12;14;16;18;20;22;";
var gJam=";2;3;8;9;14;15;20;21;";
var gFruit=";4;5;10;11;16;17;22;23;";
var gChoco=";1;3;5;7;9;11;13;15;17;19;21;23;"

function validateFilling(){
	//alert("Value of Index: "+str.indexOf(gcake+';')+" Value of filling: "+gfilling);
	var cType;
	var cTypeDesc;
	var fType;
	var fTypeDesc;
	var fCorrectType;
	var fChocoDesc;

	if (gChoco.indexOf(';'+gcake+';')>-1) fChocoDesc="Chocolate"; else fChocoDesc="Vanilla"

	if (gJam.indexOf(';'+gcake+';')>-1)	{cType='J'; cTypeDesc=fChocoDesc+' cake with Jam filling'; fCorrectType='one of Jam filling';}
	else if (gFruit.indexOf(';'+gcake+';')>-1) {cType='F'; cTypeDesc=fChocoDesc+' cake with Fruit filling'; fCorrectType='one of Fruit filling';}
	else {cType='R'; cTypeDesc=fChocoDesc+ ' with no filling'; fCorrectType='No filling';}
	
	if (gfilling == 0) {fType='R'; fTypeDesc='No filling'}
	else if (gfilling > 0 && gfilling <= gFillingMax) {fType='J'; fTypeDesc='Jam filling';}
	else if (gfilling > gFillingMax) {fType='F'; fTypeDesc='Fruit filling';}
	
	//alert("Cake Type: "+gcake +" "+cType+" Filling Type: "+gfilling+" "+fType);
	
	if (cType==fType){
		//alert('Correct! You have selected '+cTypeDesc+' with '+fTypeDesc);
		return true;
	}
	else {
		alert('Error in Cake Type and Filling Type!\nCake selected: '+cTypeDesc+'\nFilling selected: '+fTypeDesc+'\n\n'+
			'Please select \"' + fCorrectType + '\" before proceeding!');
		return false;
	}
}

function checkCakeFilling(){
	//If an error happens, let user go through the order
	//Don't hold order, we can deal with issues with customers
	try {
	    gcake=getCakeType();
	    if (gcake==-1) return true;
	    gfilling=getFillingType();
	    if (gfilling==-1) return true;
	    return validateFilling();
	}
	//
	catch (e) { return true; }
}

function getCakeType(){
	var radios = document.getElementsByTagName('input');
	//if (radios.name==undefined) return -1;
	var value=-1;
	for (var i = 0; i < radios.length; i++){    
		if (radios[i].type === 'radio') { 
			if (radios[i].name.indexOf('Size and Options')>0){
				if (radios[i].checked){        
					value = radios[i].value;           
					gsizeName = radios[i].name;
					return value;
				}
			}
		}
	}
	return value;
}
function getFillingType(){
	var radios = document.getElementsByName('product_options[Fillings]');
	var val=-1;
	for (var i = 0; i < radios.length; i++){    
		if (radios[i].checked){        
			val = radios[i].value;           
			return val;
		}
	}
	return val;
}