var item;
var INPUT_NAME_PREFIX = 'items';
var tableRows = 0;
var itemArray = new Array();
var newForm = 1;
var theItems="";
var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var ROW_BASE = 1; // first number (for display)
var nextRow = 0;
var xmlHttp=null;

function addRowToIngrTable()
{
	//Inititialize the variables for use
	this.qtyInput = document.form1.qty.value;
	this.unitInput = document.form1.unit.value;
	this.itemInput = document.form1.items.value;	
	
	if(this.qtyInput == '') {
		alert("Please enter the quantity!");
		return;
	}
	if(this.itemInput == ''){
		alert("Please enter an ingredient!");
		return;
	}
	this.pad = false;
	var pattern = new RegExp("\\d+\\s\\d+\\/\\d+");
	if(this.unitInput == 'cup' || this.unitInput == 'stick')
		if(parseInt(this.qtyInput) > 1 || pattern.test(this.qtyInput)) {
			this.unitInput += 's';
			pad = true;
		}
	
	addRowToIngrTableHelper(this.qtyInput, this.unitInput, this.itemInput, this.pad );
}

//Add the user's input dynamically.  Num 
function addRowToIngrTableHelper(qtyInput, unitInput, itemInput, pad)
{	
	var tbl = document.getElementById('ingredientTable');		
	var iteration = nextRow + ROW_BASE;
	
	var rowDiv = document.createElement('div');
	rowDiv.setAttribute('id', nextRow);
	rowDiv.setAttribute('class', 'ingritems');
	if(iteration%2 == 1) 
		rowDiv.style.background = '#f1f5fa';
 
	var qtytxt = document.createElement('p');
	qtytxt.setAttribute('class', 'item1');
	qtytxt.appendChild(document.createTextNode(qtyInput));
	
	var unittxt = document.createElement('p');
	unittxt.setAttribute('class', 'item2');
	unittxt.appendChild(document.createTextNode(unitInput));
	
	var itemtxt = document.createElement('p');
	if(pad)
		itemtxt.setAttribute('class', 'item3 negpadtxt');
	else 
		itemtxt.setAttribute('class', 'item3');
	itemtxt.appendChild(document.createTextNode(itemInput));

	var btnEl = document.createElement('img');
	btnEl.setAttribute('src', '../Graphics/cross.gif');
	btnEl.setAttribute('id', item);
	btnEl.setAttribute('alt', nextRow);
	btnEl.style.border = 'none';
	btnEl.setAttribute('class', 'ingrdeleteBtn');
	btnEl.onclick = function () {deleteCurrentRow(this)};
	
	if(navigator.appName == "Microsoft Internet Explorer") {
		qtytxt.style.display = 'inline';
		qtytxt.style.margin = '8px 8px -4px 10px';
		qtytxt.style.padding = '0 4px 0 0';
		unittxt.style.display = 'inline';
		unittxt.style.margin = '8px 8px -4px 0';
		unittxt.style.padding = '0 4px 0 0';
		itemtxt.style.display = 'inline';
		itemtxt.style.margin = '8px 8px -4px 0';
		itemtxt.style.padding = '0 4px 0 0';
		btnEl.style.cursor = 'pointer';
		btnEl.style.margin = '-3px 0 0 0';
		btnEl.style.left = '50%';
		btnEl.style.position = 'absolute';
		rowDiv.style.margin = '0 0 0 25px';
	}
	rowDiv.appendChild(qtytxt);
	rowDiv.appendChild(unittxt);
	rowDiv.appendChild(itemtxt);
	rowDiv.appendChild(btnEl);
	
	if(this.nextRow==0) {
		tbl.appendChild(rowDiv);
	} else {
		var nodes = tbl.getElementsByTagName('div');
		var topDiv = nodes[0].id; //get the very top div to append above
		var prevRow = document.getElementById(topDiv);
		tbl.insertBefore(rowDiv,prevRow);
	}
	
	nextRow = nextRow+1;	
	document.form1.qty.value = "";
	document.form1.items.value = "";
	document.form1.qty.focus();
}

function setIngredientsandTitleElement()
{
	var xml = constructXML();
	if(!xml)
		return false;
	var recipe_xml = document.getElementById('recipe_xml');
	recipe_xml.setAttribute('value',encodeURIComponent(xml));
	return true;
}

function constructXML() {
	//
	var xml = "<recipe category='"+document.getElementById('recipecat').value+"' prep_time='"+document.getElementById("prep_time").value+"' cook_time='"+document.getElementById("cook_time").value+"'>";
	if(document.getElementById('disptitle').value == '') {
		alert('Please enter a Title for the recipe');
		return false;
	}
		
	xml += "<title>"+document.getElementById('disptitle').value+"</title>";
	var ingrTable = document.getElementById('ingredientTable');
	var ingritems = ingrTable.getElementsByTagName('div');
	if(ingritems.length > 0) {
		for(var i = 0; i < ingritems.length; i++)
		{
			var items = ingritems[i];
	
			xml += "<ingredient amount='"+items.childNodes[0].childNodes[0].nodeValue+"' unit='"+items.childNodes[1].childNodes[0].nodeValue+"'>"+items.childNodes[2].childNodes[0].nodeValue+"</ingredient>";	
		}
	} else {
		alert('Please add ingredients before saving recipes');
		return false;
	}
	xml += "<instructions>";
	xml += "<cook>";
	var steps = document.getElementById('steps').value;
	var steptokens = steps.split("\n");
	for(var j = 0; j < steptokens.length; j++) {
		if(steptokens[j] != "" && steptokens[j] != null)
			xml += "<step>"+steptokens[j]+"</step>";
	}
	xml += "</cook>";
	xml += "</instructions>";
	xml += "</recipe>";
	return xml;
}
function deleteCurrentRow(obj) {
	var num = obj.alt;
	tbl = document.getElementById('ingredientTable');
	cell = document.getElementById(num);
	tbl.removeChild(cell);
	var inner = item;		
	deleteItem = inner+"#";
	theItems = theItems.replace(deleteItem,"");
	if(theItems == ""){
		var subm = document.getElementById('submitItems');	
		if(subm != null)
			subm.disabled = true;
	}
	nextRow--;
}


function email_friend_current_recipe(){
	box =  document.getElementById('selected_recipe');
	var recipe_id = box.innerHTML;
	window.location= "?id="+recipe_id+"&action=friend";
}

function check_all_boxes(table_name){
	//box = document.getElementById("selected_items[]");
	//alert(box.value);
	
	var rows = document.getElementById(table_name).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                /*
				if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
				*/
            }
        }
    }
	
}//end function


function check_all_boxes_div(table_name) {
	rows = document.getElementById(table_name).getElementsByTagName('input');
	//var rows = document.getElementById(table_name).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {
        checkbox = rows[i];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                /*
				if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
				*/
            }
        }
    }
	
}//end function
function print_current_recipe(){
	var id = document.getElementById('recipe_id');
	var recipe_id = id.value;
	NewWindow("../recipes/print_recipe.php?id="+recipe_id,"list",'550','550',"yes","center");
}

function setRecipeId_Access(recipe_id) {
	var access_id = document.getElementById('access_recipe_id');
	access_id.setAttribute('value', recipe_id);	
}
