// ImageGen XML creator

function generateIMGenXML( gentype ) {
	 	
	if( gentype == "alt" ) var recipecommands = generateAltRecipeCommands();
	else var recipecommands = generateRecipeCommands( CurrentStep );

	if(recipecommands===false) return false;
 
	if( gentype == "alt" ) var sourceimagesstring = generateAltSourceImagesString( CurrentBag );
	else var sourceimagesstring = generateSourceImagesString( CurrentBag );


	var XMLreturn = '<?xml version="1.0" encoding="utf-8"?><recipe>'
	+ '<cache>yes</cache><SiteID>1</SiteID><InputMethod>db'
	+ '</InputMethod><InputURL>'+window.location.hostname
	+ '</InputURL><SourceImages>'+sourceimagesstring
	+ '</SourceImages><StoredRecipies>'+recipecommands
	+ '</StoredRecipies></recipe>';

	return XMLreturn;
}//end function

function generateSourceImagesString( StyleID ) {
	var fabricstring = "";

	//foreach step...grab fabric id's
	$$('.bag_step_table').each(function(el){

		//obtain step number
		s = el.getAttribute('id');
		s = s.substr( (s.length-1),1);
		
		//alert( $('BagStepFabricID' + s).value) );								
		//concat name: value to finalName
		if($('BagStepRecipes'+s).value != "") fabricstring += "<image source=\"0\">" + $('BagStepFabricID' + s).value + "</image>";
	});

	return fabricstring + "<image source=\"1\">" + StyleID + "</image>";
	
}//end function

function generateRecipeCommands( thestep ) {
	var commandstring = "";
	
	if(thestep > HighestRecipeStep) HighestRecipeStep = thestep;

	//alert("called generateRecipeCommands( " + HighestRecipeStep + " ) ");


	if($('BagStepRecipes' + HighestRecipeStep) && $('BagStepRecipes' + HighestRecipeStep).value!="") {
	
		RecipesString = $('BagStepRecipes' + HighestRecipeStep).value;
	errorMsg("* $('BagStepRecipes" + HighestRecipeStep + "').value = "+RecipesString+"*");
	
		RecipesArray = RecipesString.split(',');
		for(var i=0; i<RecipesArray.length; i++) {
			commandstring += "<recipe>" + RecipesArray[i] + "</recipe>";	
		}
		return commandstring;
		
	} else { return false; }
	
}//end function

function generateAltRecipeCommands() {
	var commandstring = "";
	
	
	if(CurrentStep==2 && $('BagStepImage' + CurrentStep) && $('BagStepImage' + CurrentStep).src=="" ) {
		alert("Please choose a fabric.");
		return false;
	}

	// check if current step both has an alt recipe and a fabric selected
	if($('AltBagRecipe' + CurrentStep) && $('AltBagRecipe' + CurrentStep).value!="" && $('BagStepImage' + CurrentStep) && $('BagStepImage' + CurrentStep).src!="") {
		stepToUse = CurrentStep;
	} else if( $('AltBagRecipe' + HighestAltRecipeStep) && $('AltBagRecipe' + HighestAltRecipeStep).value!="" ) {
		stepToUse = HighestAltRecipeStep;
	} else {
		alert("There are no alternate views associated with this bag style.");
		return false; 
	}
	
	if(stepToUse) {
		RecipesString = $('AltBagRecipe' + stepToUse).value;
	errorMsg("* $('AltBagRecipe" + stepToUse + "').value = "+RecipesString+"*");
	
		RecipesArray = RecipesString.split(',');
		for(var i=0; i<RecipesArray.length; i++) {
			commandstring += "<recipe>" + RecipesArray[i] + "</recipe>";	
		}

		return commandstring;

		
	} else { 
		//alert("generateAltRecipeCommands =  false");
		alert("There are no alternate views associated with this bag style.");
		return false; 
	}
	
}//end function


function generateAltSourceImagesString( StyleID ) {
	var fabricstring = "";

	//foreach step...grab fabric id's
	$$('.bag_step_table').each(function(el){

		//obtain step number
		s = el.getAttribute('id');
		s = s.substr( (s.length-1),1);
		
		thisValue = $('BagStepFabricID' + s).value;
		
		//alert( thisValue );								
		//concat name: value to finalName
		fabricstring += "<image source=\"0\">" + thisValue + "</image>";
	});

	return fabricstring + "<image source=\"1\">" + StyleID + "</image>";
	
}//end function

