//Author: CodeProsTeam

/**
 *
 * @access public
 * @return void
 **/
function Sort(category){
	var order = document.getElementById("order");
	if(order.value == "ASC")
		order.value = "DESC";
	else
		order.value = "ASC";

	var orderField = document.getElementById("orderField");
	orderField.value = category;

	//Set action to perform
	var action = document.getElementById("action");
	action.value = "Search";

	document.mainForm.submit();
}

function previousPage(){
	var minLim = document.getElementById("minLim");
	var count = document.getElementById("count").value;
	
	if(minLim.value != 1){
		var pageSize = document.getElementById("pageSize").value;
		var maxLim = document.getElementById("maxLim");
		
		maxLim.value = minLim.value;
		minLim.value = parseInt(maxLim.value) - parseInt(pageSize);
		if(minLim.value <= 0)
			minLim.value = 1;
		
		//Set action to perform
		var action = document.getElementById("action");
		action.value = "Paging";
		
		document.mainForm.submit();
	}
}

function nextPage(){
	var maxLim = document.getElementById("maxLim");
	var count = parseInt(document.getElementById("count").value);
	
	if(maxLim.value != count){
		var pageSize = document.getElementById("pageSize").value;
		var minLim = document.getElementById("minLim");
	
		minLim.value = maxLim.value;
		maxLim.value = parseInt(maxLim.value) + parseInt(pageSize);
		
		if(maxLim.value > count)
			maxLim.value = count;
		
		//Set action to perform
		var action = document.getElementById("action");
		action.value = "Paging";
		
		document.mainForm.submit();
	}
}

function showRecipe(id){
	//Set action to perform
	var action = document.getElementById("action");
	action.value = "ViewRecipe";
	
	var recipe = document.getElementById("recipeID");
	recipe.value = id;
	
	document.mainForm.submit();
}

function backToSearch(){
	//Set action to perform
	var action = document.getElementById("action");
	action.value = "Search";
	
	document.mainForm.submit();
}

function updateRecipe(name,category,holiday,source){
	var ingredCtrl = document.getElementById("ingredText");
	ingred = ingredCtrl.innerHTML.replace(/<br>/g,"\n");
	
	var dirCtrl = document.getElementById("dirText");
	dir = dirCtrl.innerHTML.replace(/<br>/g,"\n");
	
	var title = document.getElementById("title");
	title.style.display = "none";
	
	var recipeInfo = document.getElementById("recipeInfo");
	recipeInfo.style.display = "none";
	
	var content = document.getElementById("content");
	var form = document.getElementById("recipeForm");
	
	var table = document.createElement("table");
	table.className = "class_table";
	table.align = "center";
	table.width = "100%";
	var tbody = document.createElement("tbody");
	
	var header = document.createElement("tr");
	header.className = "class_th";
	header.style.backgroundColor = "#e2e0cb";
	var fieldName = document.createElement("td");
	fieldName.className = "class_td";
	fieldName.width = "30%";
	fieldName.align = "center";
	fieldName.style.fontWeight = "bold";
	fieldName.appendChild(document.createTextNode("Field"));
	header.appendChild(fieldName);
	var fieldValue = document.createElement("td");
	fieldValue.className = "class_td";
	fieldValue.width = "70%";
	fieldValue.align = "center";
	fieldValue.style.fontWeight = "bold";
	fieldValue.appendChild(document.createTextNode("Field Value"));
	header.appendChild(fieldValue);
	tbody.appendChild(header);
	
	//Add name
	var trName = document.createElement("tr");
	trName.className = "class_tr";
	trName.style.backgroundColor = "#fcfaf6";
	var nameTitle = document.createElement("td");
	nameTitle.className = "class_td";
	nameTitle.align = "center";
	nameTitle.style.fontWeight = "bold";
	nameTitle.appendChild(document.createTextNode("* Name"));
	trName.appendChild(nameTitle);
	var nameField = document.createElement("td");
	nameField.className = "class_td";
	var inputName = document.createElement("input");
	inputName.name = "recipeName";
	inputName.id = "recipeName";
	inputName.type="text";
	inputName.value = name;
	inputName.style.width = "100%";
	nameField.appendChild(inputName);
	trName.appendChild(nameField);
	tbody.appendChild(trName);
	
	//Add category
	var trCat = document.createElement("tr");
	trCat.className = "class_tr";
	trCat.style.backgroundColor = "#fcfaf6";
	var catTitle = document.createElement("td");
	catTitle.className = "class_td";
	catTitle.align = "center";
	catTitle.style.fontWeight = "bold";
	catTitle.appendChild(document.createTextNode("Category"));
	trCat.appendChild(catTitle);
	var catField = document.createElement("td");
	catField.className = "class_td";
	var inputCat = document.createElement("input");
	inputCat.name = "recipeCat";
	inputCat.id = "recipeCat";
	inputCat.type="text";
	inputCat.value = category;
	inputCat.style.width = "100%";
	catField.appendChild(inputCat);
	trCat.appendChild(catField);
	tbody.appendChild(trCat);
	
	//Add holiday
	var trHol = document.createElement("tr");
	trHol.className = "class_tr";
	trHol.style.backgroundColor = "#fcfaf6";
	var holTitle = document.createElement("td");
	holTitle.className = "class_td";
	holTitle.align = "center";
	holTitle.style.fontWeight = "bold";
	holTitle.appendChild(document.createTextNode("Holiday"));
	trHol.appendChild(holTitle);
	var holField = document.createElement("td");
	holField.className = "class_td";
	var inputHol = document.createElement("input");
	inputHol.name = "recipeHol";
	inputHol.id = "recipeHol";
	inputHol.type="text";
	inputHol.value = holiday;
	inputHol.style.width = "100%";
	holField.appendChild(inputHol);
	trHol.appendChild(holField);
	tbody.appendChild(trHol);
	
	//Add ingredients
	var trIng = document.createElement("tr");
	trIng.className = "class_tr";
	trIng.style.backgroundColor = "#fcfaf6";
	var ingTitle = document.createElement("td");
	ingTitle.className = "class_td";
	ingTitle.align = "center";
	ingTitle.valign = "middle";
	ingTitle.style.fontWeight = "bold";
	ingTitle.appendChild(document.createTextNode("Ingredients"));
	trIng.appendChild(ingTitle);
	var ingField = document.createElement("td");
	ingField.className = "class_td";
	var txtIng = document.createElement("textarea");
	txtIng.name = "recipeIngred";
	txtIng.id = "recipeIngred";
	txtIng.style.width = "100%";
	txtIng.rows = "4";
	txtIng.appendChild(document.createTextNode(ingred));
	ingField.appendChild(txtIng);
	trIng.appendChild(ingField);
	tbody.appendChild(trIng);
	
	//Add directions
	var trDir = document.createElement("tr");
	trDir.className = "class_tr";
	trDir.style.backgroundColor = "#fcfaf6";
	var dirTitle = document.createElement("td");
	dirTitle.className = "class_td";
	dirTitle.align = "center";
	dirTitle.valign = "middle";
	dirTitle.style.fontWeight = "bold";
	dirTitle.appendChild(document.createTextNode("Directions"));
	trDir.appendChild(dirTitle);
	var dirField = document.createElement("td");
	dirField.className = "class_td";
	var dirIng = document.createElement("textarea");
	dirIng.name = "recipeDir";
	dirIng.id = "recipeDir";
	dirIng.style.width = "100%";
	dirIng.rows = "4";
	dirIng.appendChild(document.createTextNode(dir));
	dirField.appendChild(dirIng);
	trDir.appendChild(dirField);
	tbody.appendChild(trDir);
	
	//Add source
	var trSrc = document.createElement("tr");
	trSrc.className = "class_tr";
	trSrc.style.backgroundColor = "#fcfaf6";
	var srcTitle = document.createElement("td");
	srcTitle.className = "class_td";
	srcTitle.align = "center";
	srcTitle.style.fontWeight = "bold";
	srcTitle.appendChild(document.createTextNode("Source"));
	trSrc.appendChild(srcTitle);
	var srcField = document.createElement("td");
	srcField.className = "class_td";
	var inputSrc = document.createElement("input");
	inputSrc.name = "recipeSrc";
	inputSrc.id = "recipeSrc";
	inputSrc.type="text";
	inputSrc.value = source;
	inputSrc.style.width = "100%";
	srcField.appendChild(inputSrc);
	trSrc.appendChild(srcField);
	tbody.appendChild(trSrc);
	
	
	table.appendChild(tbody);
	form.appendChild(table);
	content.appendChild(document.createElement("br"));
	
	var links = document.createElement("table");
	links.className = "class_table";
	links.align = "center";
	links.width = "100%";
	var linksBody = document.createElement("tbody");
	var tr = document.createElement("tr");
	tr.className = "class_th";
	tr.style.backgroundColor = "#e2e0cb";
	var td = document.createElement("td");
	td.className = "class_td";
	td.align = "right";
	td.style.fontWeight = "bold";
	
	var innerHTML = "<a class=\"class_a\" style=\"text-decoration: none;cursor:pointer; font-weight:bold;\" title=\"Update\" href=\"javascript:validateForm()\">Update</a>&nbsp;&nbsp;";
	innerHTML += "<a class=\"class_a\" style=\"text-decoration: none;cursor:pointer; font-weight:bold;\" title=\"Cancel\" href=\"javascript:history.back();\">Cancel</a>&nbsp;&nbsp;&nbsp;&nbsp;";
	
	td.innerHTML = innerHTML;
	
	tr.appendChild(td);
	linksBody.appendChild(tr);
	links.appendChild(linksBody);
	content.appendChild(links);
}

function validateForm(){
	var nameValue = document.getElementById("recipeName");
	if (nameValue.value != "") {
		//Set action to perform
		var action = document.getElementById("recipeAction");
		action.value = "Update";
		
		document.recipeForm.submit();
	}
	else {
		nameValue.style.backgroundColor = "orange";
		alert("Invalid Name");
	}
}

function emailRecipe(){

	//Name
	var nameCtrl = document.getElementById("nameLabel")	;
	mailBody = nameCtrl.firstChild.nodeValue+"\n\n";
	
	//Ingredients
	var ingredCtrl = document.getElementById("ingredText")	;
	mailBody += "INGREDIENTS:\n"+ingredCtrl.innerHTML.replace(/<br>/g,"\n")+"\n\n";
	
	//Directions
	var dirCtrl = document.getElementById("dirText");
	mailBody += "DIRECTIONS\n"+dirCtrl.innerHTML.replace(/<br>/g,"\n")+"\n\n";
	
	//Source
	var srcCtrl = document.getElementById("sourceText");
	mailBody += srcCtrl.firstChild.nodeValue;
	
	var form = document.getElementById("recipeForm");
	
	var input = document.createElement("input");
	input.name = "mailBody";
	input.id = "mailBody";
	input.type = "hidden";
	input.value = mailBody;
	form.appendChild(input);
	
	input = document.createElement("input");
	input.name = "emailAddress";
	input.id = "emailAddress";
	input.type = "hidden";
	form.appendChild(input);
	
	var emailWindow = window.open("emailForm.php","emailWindow","width=350, height=200");
	
}

function printRecipe(){
	var printWindow = window.open("print_window.htm","printWindow","menubar=yes, resizable=yes, width=1000, height=700, scrollbars=yes");
}

function addRecipe(){
	//Set action to perform
	var action = document.getElementById("addAction");
	action.value = "AddNewRecipe";
	
	document.addRecipeForm.submit();
}

//check user input
function submitAddForm(){
	var form = document.getElementById("addRecipeForm");
	var inputName = document.getElementById("Name");
	if (inputName.value == "") {
		alert("Invalid Name");
		inputName.style.backgroundColor = "orange";
	}
	else {
		//Set action to perform
		var action = document.getElementById("addAction");
		action.value = "SubmitRecipe";
		
		form.submit();
	}
}

function loadRecipeInfo(){
	var opener = window.opener;
	var recipeInfo = opener.document.getElementById("recipeInfo");
	var content = recipeInfo.cloneNode(true);
	
	var parent = document.getElementById("content");
	parent.innerHTML = content.innerHTML;
	
	//remove links
	var links = document.getElementById("linksRow");
	var parent = links.parentNode;
	
	parent.removeChild(links);
	
	//remove print option
	var print = document.getElementById("printOpt");
	parent = print.parentNode;
	
	parent.removeChild(print);
	
	//remove email option
	var email = document.getElementById("emailOpt");
	parent = email.parentNode;
	
	parent.removeChild(email);
}

function updatePaging(minLim,maxLim){
	var max = document.getElementById("maxLim");
	max.value = maxLim;
	
	var min = document.getElementById("minLim");
	min.value = minLim;
}


