function non () {

}

function initialize (prevEnt) {
		//alert ("Enteries "+prevEnt);
	// if the prevEnt is 0 then nothing was entered yet. Initial loading
	if (prevEnt==0){createRow(prevEnt);}
	// Else a value has been filled and we need to refill those values.
	else{createPrevRow(prevEnt);}
}



function createPrevRow(prevRows) {
	/////////////
	// the structure of the prevRows are Count=prodid:prodquantity and so on
	////////////
	var fromZero =0;
	//alert ("prevRows is = "+prevRows);
	count=prevRows.split("=");
	// this is the total of product ids and quanttities so We have to divide it by 2 to find the count
	NumEnt=count[0];
	NumEnts= NumEnt/2;
	//alert ("Number of products requested = "+NumEnts);
	/////////////////////////////////////////////////////
	// This is the product id and quantities in the array
	ValEnt=count[1];
	// now we split it into an array for product ids and one for product quantities
	products=ValEnt.split(":");
	
	//alert ("NumEnts are "+NumEnts+" products: "+products);
	//alert products;
	//get all ID's
	var prodID;
	
	
	//get the tbody node
	var table_element = document.getElementById("formTable");
	var tbody_element = table_element.lastChild;
	var holder=0;
for (var x = 0; x < count[0]; x++)
{
	//alert ("holer: "+holder);
	
	if (holder<NumEnts){
		//alert ("will set y to 0");
		y = 0;}
	holder++;
	
	prodID = products[x];
	prodquant = products[x+1];
	//alert (prodquant);
////////////////////////////////////////////////////////
//          Only for postcards
////////////////////////////////////////////////////////

	if (prodID==67){ ProdName=("Postcards"); Productid="67"; 
	
	//alert (ProdName+""+prodquant+""+prodID);
	//increment the row number
	var new_row_num = fromZero++;

	//assemble the names of the new objects	
	var new_row_name = "row" + new_row_num;
	var new_prod_name = "prodname" + new_row_num;
	var new_quant_name = "quant" + new_row_num;
//alert (new_prod_name);
	//get last row
	var add_prod_row = table_element.lastChild.lastChild;

	//create a new row
	var new_row = document.createElement("tr");
	new_row.setAttribute("id", new_row_name);
	
	//insert new row before last row
	table_element.lastChild.insertBefore(new_row, add_prod_row);
	
	/* the rest of this function assembles and adds
		the new row's elements	*/
	var new_cell = document.createElement("td");
	new_row.appendChild(new_cell);
	
    

	var new_select = document.createElement("select");
	new_select.setAttribute("name", new_prod_name);	
	new_select.setAttribute("id", new_prod_name);
	new_select.setAttribute("onChange", "change_thumb(this);");
	new_select.setAttribute("onblur", "Eval_values(this.value, 'Prod_name');");
	new_cell.appendChild(new_select);
	//for each product, create an option element


				var new_option = document.createElement("option");
				new_option.setAttribute("value", Productid);		
				new_select.appendChild(new_option);
		
				var new_text = document.createTextNode(ProdName);
				new_option.appendChild(new_text);
			//alert ("passed");
				//Quantity Section
				new_cell = document.createElement("td");
				new_row.appendChild(new_cell);
			//alert ("again passed");
				var new_input = document.createElement("input");
				new_input.setAttribute("type", "text");	
				new_input.setAttribute("name", new_quant_name);
				new_input.setAttribute("id", new_quant_name);
				new_input.setAttribute("size", "5");
				new_input.setAttribute("maxlength", "5");
				new_input.setAttribute("value", prodquant);
				new_input.setAttribute("onKeyPress", "return only_num(event);");
				new_input.setAttribute("onblur", "Eval_values(this.value, 'Prod_quantity');");
				//new_input.onChange("return only_num(event);");
				new_cell.appendChild(new_input);	
				//Remove this request section
				new_cell = document.createElement("td");
				new_row.appendChild(new_cell);
			
				var remove_div = document.createElement("div");
				remove_div.setAttribute("id", "rlink" + new_row_num);
				new_cell.appendChild(remove_div);
				
					var remove_link = document.createElement("a");
					remove_link.setAttribute("href", "javascript: remove_one_request(" + new_row_num + ");");	
					remove_div.appendChild(remove_link);	
			
					var remove_text = document.createTextNode("<< Remove.");
					remove_link.appendChild(remove_text);
		
				
	}
			else{
	//alert ("|| product id:"+prodID+" || product quantity"+prodquant);
	for (var y = 0; y < 22; y++)
{
		//alert (y);
		//alert (table[y][0]);
////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
if (table[y][0]== prodID)
	{
		
		ProdName=table[y][1]; Productid=table[y][0];
//increment the row number
	var new_row_num = fromZero++;

	//assemble the names of the new objects	
	var new_row_name = "row" + new_row_num;
	var new_prod_name = "prodname" + new_row_num;
	var new_quant_name = "quant" + new_row_num;

	//get last row
	var add_prod_row = table_element.lastChild.lastChild;

	//create a new row
	var new_row = document.createElement("tr");
	new_row.setAttribute("id", new_row_name);
	
	//insert new row before last row
	table_element.lastChild.insertBefore(new_row, add_prod_row);
	
	/* the rest of this function assembles and adds
		the new row's elements	*/
	var new_cell = document.createElement("td");
	new_row.appendChild(new_cell);
	
    

	var new_select = document.createElement("select");
	new_select.setAttribute("name", new_prod_name);	
	new_select.setAttribute("id", new_prod_name);
	new_select.setAttribute("onChange", "change_thumb(this);");
	new_select.setAttribute("onblur", "Eval_values(this.value, 'Prod_name');");
	new_cell.appendChild(new_select);
	//for each product, create an option element

	for (rw_num in table) {
				if (table[rw_num][0] == prodID)
				{
				var new_option = document.createElement("option");
				new_option.setAttribute("value", table[rw_num][0]);		
				new_select.appendChild(new_option);
		
		
				var new_text = document.createTextNode(table[rw_num][1]);
				new_option.appendChild(new_text);
			
				//Quantity Section
				new_cell = document.createElement("td");
				new_row.appendChild(new_cell);
				
				var new_input = document.createElement("input");
				new_input.setAttribute("type", "text");	
				new_input.setAttribute("name", new_quant_name);
				new_input.setAttribute("id", new_quant_name);
				new_input.setAttribute("size", "5");
				new_input.setAttribute("maxlength", "5");
				new_input.setAttribute("value", prodquant);
				new_input.setAttribute("onKeyPress", "return only_num(event);");
				new_input.setAttribute("onblur", "Eval_values(this.value, 'Prod_quantity');");
				//new_input.onChange("return only_num(event);");
				new_cell.appendChild(new_input);	
				//Remove this request section
				new_cell = document.createElement("td");
				new_row.appendChild(new_cell);
			
				var remove_div = document.createElement("div");
				remove_div.setAttribute("id", "rlink" + new_row_num);
				new_cell.appendChild(remove_div);
				
					var remove_link = document.createElement("a");
					remove_link.setAttribute("href", "javascript: remove_one_request(" + new_row_num + ");");	
					remove_div.appendChild(remove_link);	
			
					var remove_text = document.createTextNode("<< Remove.");
					remove_link.appendChild(remove_text);
		
			
				}//second if
					}	//second for
	
	}//first if
}//first for
			}//else
	//alert ("prodID: "+prodID+">> x="+x+">>  ");
	x++;
}


}








function createRow(prevRow) {
	//alert (prevRow);
	//get the tbody node
	var table_element = document.getElementById("formTable");
	var tbody_element = table_element.lastChild;

	//increment the row number
	var new_row_num = prevRow++;
	//alert (new_row_num);
	//assemble the names of the new objects	
	var new_row_name = "row" + new_row_num;
	var new_prod_name = "prodname" + new_row_num;
	var new_quant_name = "quant" + new_row_num;
//alert (new_row_name);
//alert (new_prod_name);
//alert (new_quant_name);
	//get last row
	var add_prod_row = table_element.lastChild.lastChild;

	//create a new row
	var new_row = document.createElement("tr");
	new_row.setAttribute("id", new_row_name);
	
	//insert new row before last row
	table_element.lastChild.insertBefore(new_row, add_prod_row);
	
	/* the rest of this function assembles and adds
		the new row's elements

	*/
	var new_cell = document.createElement("td");
	new_row.appendChild(new_cell);
	
    

	var new_select = document.createElement("select");
	new_select.setAttribute("name", new_prod_name);	
	new_select.setAttribute("id", new_prod_name);
	new_select.setAttribute("onChange", "change_thumb(this);");
	new_select.setAttribute("onblur", "Eval_values(this.value, 'Prod_name');");
	new_cell.appendChild(new_select);


	//add option for "Select a product"
	var new_option = document.createElement("option");
	new_option.setAttribute("value", 1);		
	new_select.appendChild(new_option);
//alert ("here the products get created");
	var new_text = document.createTextNode("List of Materials");
	new_option.appendChild(new_text);

	//for each product, create an option element

	for (rw_num in table) {
		var new_option = document.createElement("option");
		new_option.setAttribute("value", table[rw_num][0]);		
		new_select.appendChild(new_option);

		var new_text = document.createTextNode(table[rw_num][1]);
		new_option.appendChild(new_text);

	}
	
	if (document.URL.indexOf("_special") == -1) {
		//add special option for the superclass "Postcards"
		var new_option = document.createElement("option");
		new_option.setAttribute("value", 67);		
		new_select.appendChild(new_option);
	
		var new_text = document.createTextNode("Postcards");
		new_option.appendChild(new_text);	
	}
	
	//Quantity Section
	new_cell = document.createElement("td");
	new_row.appendChild(new_cell);
	
	var new_input = document.createElement("input");
	new_input.setAttribute("type", "text");	
	new_input.setAttribute("name", new_quant_name);
	new_input.setAttribute("id", new_quant_name);
	new_input.setAttribute("size", "5");
	new_input.setAttribute("maxlength", "5");
	new_input.setAttribute("value", "0");
	new_input.setAttribute("onKeyPress", "return only_num(event);");
	new_input.setAttribute("onblur", "Eval_values(this.value, 'Prod_quantity');");
	//new_input.onChange("return only_num(event);");
	new_cell.appendChild(new_input);	

	//Remove this request section
	new_cell = document.createElement("td");
	new_row.appendChild(new_cell);

	var remove_div = document.createElement("div");
	remove_div.setAttribute("id", "rlink" + new_row_num);
	new_cell.appendChild(remove_div);

	//remove link is only for the last item
		
	if (new_row_num > 0) {

		var remove_link = document.createElement("a");
		remove_link.setAttribute("href", "javascript: remove_request(" + new_row_num + ");");	
		remove_div.appendChild(remove_link);	

		var remove_text = document.createTextNode("<< Remove.");
		remove_link.appendChild(remove_text);
		
		//hide the previous link, if it existed
		var prev_rlink_id = "rlink" + (new_row_num - 1);
		var prev_rlink = document.getElementById(prev_rlink_id);
		if (prev_rlink.hasChildNodes() == true) {
			prev_rlink.removeChild(prev_rlink.firstChild);
		}
		
	}

	return prevRow;
}

function add_request(rowNum) {
//alert (rowNum);
		var newRow = createRow(rowNum);
		//update the parameter of the add link to be the last row number
		document.getElementById("reqlink").setAttribute("href", "javascript: add_request(" + newRow + ");");
		document.getElementById("add_prod").write(newRow) ;
		//alert (newRow);
}

function add_perm(obj) {
	var new_input = document.createElement("input");
	new_input.setAttribute("name", "location[]");
	new_input.setAttribute("type", "text");
	new_input.setAttribute("size", "30");
	new_input.setAttribute("value", "");
	new_input.setAttribute("maxlength", "200");
	
	var par = obj.parentNode;
	par.insertBefore(new_input, obj.previousSibling);
}

function add_copresenter(obj) {

	var frag = document.createDocumentFragment();
	var tr = document.createElement("tr");
	frag.appendChild(tr);
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	var new_text = document.createTextNode("Co-Presenter Name: ");
	td.appendChild(new_text);

	var td = document.createElement("td");
	tr.appendChild(td);
	
	var new_input = document.createElement("input");
	new_input.setAttribute("name", "copresenters[]");
	new_input.setAttribute("type", "text");
	new_input.setAttribute("size", "40");
	new_input.setAttribute("value", "");	
	td.appendChild(new_input);
	
	var tr = document.createElement("tr");
	frag.appendChild(tr);
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	var new_text = document.createTextNode("Co-Presenter Email: ");
	td.appendChild(new_text);

	var td = document.createElement("td");
	tr.appendChild(td);
	
	var new_input = document.createElement("input");
	new_input.setAttribute("name", "copresenter_emails[]");
	new_input.setAttribute("type", "text");
	new_input.setAttribute("size", "40");
	new_input.setAttribute("value", "");	
	td.appendChild(new_input);
	
	var par = obj.parentNode.parentNode.parentNode;
	
	par.insertBefore(frag, obj.parentNode.parentNode);

}

function remove_one_request(rowNum) {
	//remove this row
	var table_element = document.getElementById("formTable");
	var tbody_element = table_element.lastChild;	
	tbody_element.removeChild(document.getElementById("row" + rowNum));	
	
	//add remove_link to previous row
	
	if (rowNum > 1) {

		//update the row number
		rowNum--;

		var remove_div = document.getElementById("rlink" + rowNum);
		var remove_link = document.createElement("a");
		remove_link.setAttribute("href", "javascript: remove_request(" + rowNum + ");");	
		remove_div.appendChild(remove_link);	
//
//		var remove_text = document.createTextNode("<<Remove");
//		remove_link.appendChild(remove_text);
//		
//		//hide the previous link, if it existed
//		var prev_rlink_id = "rlink" + (rowNum - 1);
//		var prev_rlink = document.getElementById(prev_rlink_id);
//		if (prev_rlink.hasChildNodes() == true) {
//			prev_rlink.removeChild(prev_rlink.firstChild);
//	}
		
	}

	//update the parameter of the add link to be the last row number
	document.getElementById("reqlink").setAttribute("href", "javascript: add_request(" + rowNum + ");");

}
function remove_request(rowNum) {
	//remove this row
	var table_element = document.getElementById("formTable");
	var tbody_element = table_element.lastChild;	
	tbody_element.removeChild(document.getElementById("row" + rowNum));	
	
	//add remove_link to previous row
	
	if (rowNum > 1) {

		//update the row number
		rowNum--;

		var remove_div = document.getElementById("rlink" + rowNum);
		var remove_link = document.createElement("a");
		remove_link.setAttribute("href", "javascript: remove_request(" + rowNum + ");");	
		remove_div.appendChild(remove_link);	

		var remove_text = document.createTextNode("<<Remove");
		remove_link.appendChild(remove_text);
		
		//hide the previous link, if it existed
		var prev_rlink_id = "rlink" + (rowNum - 1);
		var prev_rlink = document.getElementById(prev_rlink_id);
		if (prev_rlink.hasChildNodes() == true) {
			prev_rlink.removeChild(prev_rlink.firstChild);
		}
		
	}

	//update the parameter of the add link to be the last row number
	document.getElementById("reqlink").setAttribute("href", "javascript: add_request(" + rowNum + ");");

}

function change_thumb(select_obj) {
	//alert("change_thumb fired");
	//alert(select_obj.options[select_obj.selectedIndex].value);
	var thm_div = document.getElementById("thm_div");
	var thumbnail = document.getElementById("thumbnail");
	var prod_name_div = document.getElementById("prod_name_div");
	var prod_category_div = document.getElementById("prod_category_div");
	var more_link = document.getElementById("more_link");
	var close_field_div = document.getElementById("close_field_div");
	
	
	
	
	for (var i = 0; i < table.length; i++) {
		if (select_obj.options[select_obj.selectedIndex].value == table[i][0]) {
			//alert(table[i][0]);	
			var new_close_popup = document.createElement("input");
			new_close_popup.setAttribute("name", 'close');
			new_close_popup.setAttribute("type", 'image');
			new_close_popup.setAttribute("width", '10');
			new_close_popup.setAttribute("height", '10');
			new_close_popup.setAttribute("src", "../mess/images/prod_thumbs/close_01.png");	
			new_close_popup.setAttribute("onClick", "close_popup();");
			close_field_div.appendChild(new_close_popup); 
			var new_name = document.createTextNode("Product Name: " + table[i][1]);
			var new_category = document.createTextNode("Product Type: " + table[i][2]);
			
			
			var old_name = prod_name_div.firstChild;
			var old_category = prod_category_div.firstChild;
			var old_close_popup = close_field_div.firstChild;
			
			
			prod_name_div.replaceChild(new_name, old_name);
			prod_category_div.replaceChild(new_category, old_category);
			
			close_field_div.replaceChild(new_close_popup, old_close_popup);
			if (document.URL.indexOf("_special") == -1) {
				more_link.setAttribute("href", "prod_descriptions.html#" + table[i][0]);
				
			} else {
				
				more_link.setAttribute("href", "prod_special_descriptions.html#" + table[i][0]);
			}
			
			thumbnail.setAttribute("src", table[i][3]);
			//close_popup.setAttribute("src", "/mess/images/prod_thumbs/close_01.png");
			//alert(table[i][3]);	
			thm_div.style.setProperty("visibility", "visible", "important");
			break;		
		} else {
			//close_field_div.appendChild(); 
			thm_div.style.setProperty("visibility", "hidden", "important");		
		}
	}
}
function close_popup() {

	var thm_div = document.getElementById("thm_div");
	
		
			thm_div.style.setProperty("visibility", "hidden", "important");		
		
	
}

function only_num(e) {
	//alert(e.which);
	if (navigator.appName=="Netscape") {
		if (((e.which < 48) || (e.which > 57)) && (e.which != 8) && (e.which != 0)) {
			return false;
		}
	} else {
		if (((e.keyCode < 48) || (e.keyCode > 57)) && (e.keyCode != 8) && (e.keyCode != 0)) {
			return false;
		}
	}
}

function select_item(select_object, select_value){
 //alert(select_object);
  for (var i = 0; i < select_object.length; i++) {
   //alert("i = " + i + "\n select_value = " + select_value + "\n select_object.options[i].value = " + select_object.options[i].value);
   if (select_value == select_object.options[i].value) {
    select_object.selectedIndex = select_object.options[i].index;
   }
  }
}