/**
 * @author Michael'd0dge'Fiedler
 * @copyright 2009 cycro-systems GbR
 * @name TabascoActions
 */
function TabascoActions(itid)
{
	this.chosenSauce	= document.getElementById("hChosenSauce");
	this.chosenMix		= document.getElementById("hChosenMix");
	this.recipeType		= document.getElementById("recipe_type");
	this.itid			= itid;
	this.saucePics		= new Array(3);
	this.mixPics		= null;
	this.elementCount	= 6;
	this.measureText	= new Array(
		"Maßeinheit","Gramm","Teil","Esslöffel","Teelöffel","Bund",
		"Spritzer","Zentiliter","Milliliter","Liter"
	);
	this.measures 	= new Array();
	this.delImgSrc	= null;
	this.home		= null;
	
	// method declarations
	this.setSauce				= setSauce;
	this.setIngredient			= setMix;
	this.tbClose				= tb_sc_remove;
	this.newRow					= addNewIngredientRow;
	this.renderSelectOptions	= renderSelectOptions;
	this.renderElementContainer	= renderElementContainer;
	this.deleteRow				= deleteRow;
	this.search					= commitAjaxSearch;
	this.deletePic				= deletePicture;
	this.saveComment			= saveComment;
	this.saveRating				= saveRating;
	this.chkStep				= validateSubmissionForm;
	this.sendRecommendation		= sendForm;
	this.sendReport				= sendForm;
}

function setSauce(choice)
{
	if(choice == 0 || !choice) return;
	//this.chosenSauce.value = choice;
	this.chosenSauce.value = choice;
	document.getElementById("iChosenSauce").src = this.saucePics[choice];
	this.tbClose();
	if(this.chosenSauce.value > 0 && this.chosenMix.value > 0)
		this.search();
}

function setMix(choice)
{
	if(choice == 0 || !choice) return;
	this.chosenMix.value	= choice;
	document.getElementById("iChosenMix").src = this.mixPics[choice];
	this.tbClose();
	if(this.chosenSauce.value > 0 && this.chosenMix.value > 0)
		this.search();
}

function commitAjaxSearch()
{
	targetLink	= "taba_action=4&reload=1&";
	targetLink	= targetLink+$("#mixform").formSerialize()+'&';
	
	ajax_get_and_put(targetLink,"recipe_results",0);
}

function saveComment()
{
	var author = document.getElementById("c_author");
	targetLink	= $("#comment_form").formSerialize()+"&reload=1";
	ajax_get_and_put(targetLink,"comments",0);
}

function saveRating()
{
	targetLink = $("#rating_form").formSerialize()+"&reload=1";
	ajax_get_and_put(targetLink,"ratings",0);
}
/**
 * this method will be used under several aliases (see constructor)
 * @param rId
 * @return
 */
function sendForm()
{
	targetLink = $("#recommend_form").formSerialize()+"&reload=1";
	ajax_get_and_put(targetLink,"comments",0);
}

function addNewIngredientRow()
{
	var mother 	= document.getElementById("row_container");
	// amount field
	var amnt	= document.createElement("input");
	amnt.setAttribute("name","txt_amount["+this.elementCount+"]");
	amnt.setAttribute("type","text");
	amnt.setAttribute("maxlength","4");
	var cl01 = document.createAttribute("class");
	cl01.nodeValue = "i_eintragen_left";
	amnt.setAttributeNode(cl01);
	var containerA = renderElementContainer(1);
	containerA.appendChild(amnt);
	// ingredient name
	var ingr	= document.createElement("input");
	ingr.setAttribute("name","txt_ingr["+this.elementCount+"]");
	ingr.setAttribute("type","text");
	ingr.setAttribute("maxlength","200");
	var cl02 = document.createAttribute("class");
	cl02.nodeValue = "i_eintragen_right";
	ingr.setAttributeNode(cl02);
	var containerC = renderElementContainer(3);
	containerC.appendChild(ingr);
	// measure select box
	var sb	= document.createElement("select");
	sb.setAttribute("name","sb_measure["+this.elementCount+"]");
	var cl03 = document.createAttribute("class");
	cl03.nodeValue = "pos_select";
	sb.setAttributeNode(cl03);
	this.renderSelectOptions(sb);
	var containerB = renderElementContainer(2);
	containerB.appendChild(sb);
	// delete button
	var del = document.createElement("a");
	var img	= document.createElement("img");
	//del.setAttribute("onmouseup","deleteRow("+this.elementCount+");");
	var evt = document.createAttribute("onmouseup");
	evt.nodeValue = "deleteRow("+this.elementCount+");";
	del.setAttributeNode(evt);
	del.setAttribute("href","#playfield");
	img.setAttribute("alt","delete");
	img.setAttribute("src",this.delImgSrc);
	del.appendChild(img);
	var containerD = renderElementContainer(4);
	containerD.appendChild(del);
	// cleaner
	var containerE = renderElementContainer(5);
	// box 'em up
	var row = this.renderElementContainer(0);
	row.appendChild(containerA);
	row.appendChild(containerB);
	row.appendChild(containerC);
	row.appendChild(containerD);
	row.appendChild(containerE);
	mother.appendChild(row);
	this.elementCount++;
}
/**
 * @param int type
 * @return Object
 */
function renderElementContainer(type)
{
	var div = document.createElement("div");
	var cl	= document.createAttribute("class");
	switch(type){
		// major row
		case 0:{
			//div.setAttribute("class","wrap_eintragen");
			cl.nodeValue = "wrap_eintragen";
			var id = document.createAttribute("id");
			id.nodeValue = "d_"+this.elementCount;
			div.setAttributeNode(id);
			div.setAttribute("style","margin-top:5px;");
			//var dStyle2 = document.createAttribute("style");
			//dStyle2.nodeValue = "margin-top:5px;";
			//div.setAttributeNode(dStyle2);
		}
		break;
		// amount
		case 1:	cl.nodeValue = "eintragen_left"; break;
		// measure
		case 2:	cl.nodeValue = "eintragen_middle"; break;
		// ingredient
		case 3:	cl.nodeValue = "eintragen_right"; break;
		// delete button
		case 4:	cl.nodeValue = "row_delete"; break;
		// cleaner
		case 5:cl.nodeValue = "cleaner";break;
	}
	div.setAttributeNode(cl);
	return div;
}
/**
 * @param Object obj
 * @return void
 */
function renderSelectOptions(obj)
{
	for(i=0;i<this.measures.length;i++){
		var o	=	document.createElement("option");
		o.setAttribute("value",this.measures[i]);
		var t	=	document.createTextNode(this.measures[i]);
		o.appendChild(t);
		obj.appendChild(o);
	}
	
}
/**
 * 
 * @param int id
 * @return void
 */
function deleteRow(id)
{
	var container = document.getElementById("row_container");
	var victim	  = document.getElementById("d_"+id);
	alert(id);
	container.removeChild(victim);
}

function deletePicture(picName)
{
	document.getElementById("delPic").value = 1;
	document.upload_form.submit();
}

function validateSubmissionForm(step)
{
	switch(step){
		case 0:
		    document.location.href = nextstep;
		case 1:
			if(document.getElementById("r_name").value != ""){
				for(i=0;i<this.elementCount;i++){
					amount 	= document.getElementById("txt_amount_"+i).value;
					ingr	= document.getElementById("txt_ingr_"+i).value;
					if((amount == "" || amount == " ") || (ingr == "" || ingr == " "))
						return;
					else
						document.new_recipe_form.submit();
				}
			}
			else return;
		break;
		case 2:
			if(document.user_data_form.r_creator.value != ""){
				if(document.user_data_form.terms_of_participation.checked == false){
					alert("Ohne Zustimmung zu den Teilnahme- und \nDatenschutzbedingungen kann Dein Rezept nicht verschickt werden.");
					return;
				}
				document.user_data_form.submit();
			}
			else return;
		break;
	}
}
