/*
this file contains the functions necessary for configuring the print - KJP

Created March 8, 2008
*/

itemArray = new Array();


function matted(productid,category,size,price){

	document.addCartItem.elements['colorMatted'].checked=true;

	//before we add check to see if they already selected it
	arrayLength = itemArray.length;
	var itemFound = "n";
	for (ew=0;ew<arrayLength;ew++){
		if (itemArray[ew][0]==productid){
			itemFound = "y";
		}	
	}
	
	if (itemFound == "n"){
		var arrayLength = itemArray.length;
		itemArray[arrayLength] = new Array(productid,category,size,price,'Dove-Gray');

		addClearMattedLink(size);
		pagePrice();
	} else {
		alert("Item has already been selected");	
	}
}

function unmatted(productid,category,size,price){

	//before we add check to see if they already selected it
	arrayLength = itemArray.length;
	var itemFound = "n";
	for (ew=0;ew<arrayLength;ew++){
		if (itemArray[ew][0]==productid){
			itemFound = "y";
		}	
	}

	if (itemFound == "n"){
		var arrayLength = itemArray.length;
		itemArray[arrayLength] = new Array(productid,category,size,price,'');
	
		addClearUnmattedLink(size);
		pagePrice();
	} else {
		alert("Item has already been selected");	
	}
	
}

function clothBag(productid,category,size,price){

	//before we add check to see if they already selected it
	arrayLength = itemArray.length;
	var itemFound = "n";
	for (ew=0;ew<arrayLength;ew++){
		if (itemArray[ew][0]==productid){
			itemFound = "y";
		}	
	}

	if (itemFound == "n"){
		document.addCartItem.elements['colorCloth Bag'].checked=true;
		var arrayLength = itemArray.length;
		itemArray[arrayLength] = new Array(productid,category,size,price,'Cream');
	
		addClearClothBagLink(size);
		pagePrice();
	} else {
		alert("Item has already been selected");	
	}
	
}

function tile(productid,category,size,price){

	//before we add check to see if they already selected it
	arrayLength = itemArray.length;
	var itemFound = "n";
	for (ew=0;ew<arrayLength;ew++){
		if (itemArray[ew][0]==productid){
			itemFound = "y";
		}	
	}

	if (itemFound == "n"){
		var arrayLength = itemArray.length;
		itemArray[arrayLength] = new Array(productid,category,size,price,'');
	
		addClearTileLink(size);
		pagePrice();
	} else {
		alert("Item has already been selected");	
	}
	
}

function greetingCard(productid,category,size,price){

	//before we add check to see if they already selected it
	arrayLength = itemArray.length;
	var itemFound = "n";
	for (ew=0;ew<arrayLength;ew++){
		if (itemArray[ew][0]==productid){
			itemFound = "y";
		}	
	}

	if (itemFound == "n"){
		var arrayLength = itemArray.length;
		itemArray[arrayLength] = new Array(productid,category,size,price,'');
	
		addClearGreetingCardLink(size);
		pagePrice();
	} else {
		alert("Item has already been selected");	
	}
	
}

function pagePrice(){
	//go through the itemArray to add up the prices
	var priceTotal = "0.00";
	for (re=0;re<itemArray.length;re++){
		arrayPrice = "0.00";
		try {
			arrayPrice = parseFloat(itemArray[re][3]);
		} catch (r){}
		priceTotal = parseFloat(priceTotal) + arrayPrice;
	}
	if (priceTotal == "0.00") {
		document.getElementById('price').innerHTML = "$"+priceTotal;
	} else {
		document.getElementById('price').innerHTML = "$"+priceTotal+".00";
	}
}

function clearMatted(desc){

	//loop through the matted radio button array to 'unselect' each one
	try {
		document.addCartItem.elements['Matted'].checked=false;	
	} catch (e){}	
	
	removeLink("Clear Matted", "Matted", desc);	
	
	//clear any selected colors
	clearMattedColor();

}

function clearMattedColor(){
	//loop through the colorMatting radio button array to 'unselect' each one

	try {
		var colorMattingArray = document.addCartItem.elements['colorMatted'];
		for (var te=0;te<colorMattingArray.length;te++){
			colorMattingArray[te].checked=false;	
		}
		document.addCartItem.elements['colorMatted'].checked=false;
	} catch (e){}	
	
}

function clearUnmatted(desc){
	//'unselect' the unmatted radio button
	try {
		document.addCartItem.elements['Unmatted'].checked=false;	
	} catch (e){}

	removeLink("Clear Unmatted", "Unmatted", desc);	
}


function clearClothBag(desc){
	//loop through the matting radio button array to 'unselect' each one
	try {
		document.addCartItem.elements['Cloth Bag'].checked=false;
	} catch (e){}	
	
	removeLink("Clear Cloth Bag", "Cloth Bag", desc);	

	//clear any selected colors
	clearClothBagColor();
}

function clearClothBagColor(){
	//loop through the colorMatting radio button array to 'unselect' each one
	try {
		var colorMattingArray = document.addCartItem.elements['colorCloth Bag'];
		for (var te=0;te<colorMattingArray.length;te++){
			colorMattingArray[te].checked=false;	
		}
		document.addCartItem.elements['colorCloth Bag'].checked=false;
	} catch (e){}		
}

function clearTile(desc){
	//loop through the tile radio button array to 'unselect' each one
	try {
		var tileArray = document.addCartItem.elements['Tile'];
		for (var te=0;te<tileArray.length;te++){
			tileArray[te].checked=false;	
		}
	} catch (e){}	
	
	removeLink("Clear Tile", "Tile", desc);
}


function clearGreetingCard(desc){
	//'unselect' the greeting card radio button
	try {
		document.addCartItem.elements['Greeting Card'].checked=false;	
	} catch (e){}	
	
	removeLink("Clear Greeting Card", "Greeting Card", desc);
	
}



function addClearMattedLink(desc){
	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;

	trRow = document.createElement("tr");
	trRow.setAttribute("class","ltcolor");
	trRow.setAttribute("className","ltcolor");

	tdCellLink = document.createElement("a");
	tdCellLink.setAttribute("href","javascript:clearMatted('"+desc+"');");
	tdCellName = document.createTextNode("Clear Matted ["+desc+"]");
	tdCellLink.appendChild(tdCellName);

	tdCell = document.createElement("td");
	tdCell.setAttribute("align","center");
	tdCell.appendChild(tdCellLink);
	trRow.appendChild(tdCell);

	insertAfterRow = tablBody.getElementsByTagName("tr")[2];
	tablBody.insertBefore(trRow,insertAfterRow);

}

function addClearUnmattedLink(desc){
	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;

	trRow = document.createElement("tr");
	trRow.setAttribute("class","ltcolor");
	trRow.setAttribute("className","ltcolor");

	tdCellLink = document.createElement("a");
	tdCellLink.setAttribute("href","javascript:clearUnmatted('"+desc+"');");
	tdCellName = document.createTextNode("Clear Unmatted ["+desc+"]");
	tdCellLink.appendChild(tdCellName);

	tdCell = document.createElement("td");
	tdCell.setAttribute("align","center");
	tdCell.appendChild(tdCellLink);
	trRow.appendChild(tdCell);

	insertAfterRow = tablBody.getElementsByTagName("tr")[2];
	tablBody.insertBefore(trRow,insertAfterRow);

}

function addClearClothBagLink(desc){
	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;

	trRow = document.createElement("tr");
	trRow.setAttribute("class","ltcolor");
	trRow.setAttribute("className","ltcolor");

	tdCellLink = document.createElement("a");
	tdCellLink.setAttribute("href","javascript:clearClothBag('"+desc+"');");
	tdCellName = document.createTextNode("Clear Cloth Bag ["+desc+"]");
	tdCellLink.appendChild(tdCellName);

	tdCell = document.createElement("td");
	tdCell.setAttribute("align","center");
	tdCell.appendChild(tdCellLink);
	trRow.appendChild(tdCell);

	insertAfterRow = tablBody.getElementsByTagName("tr")[2];
	tablBody.insertBefore(trRow,insertAfterRow);

}

function addClearTileLink(desc){
	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;

	trRow = document.createElement("tr");
	trRow.setAttribute("class","ltcolor");
	trRow.setAttribute("className","ltcolor");

	tdCellLink = document.createElement("a");
	tdCellLink.setAttribute("href","javascript:clearTile('"+desc+"');");
	tdCellName = document.createTextNode("Clear Tile ["+desc+"]");
	tdCellLink.appendChild(tdCellName);

	tdCell = document.createElement("td");
	tdCell.setAttribute("align","center");
	tdCell.appendChild(tdCellLink);
	trRow.appendChild(tdCell);

	insertAfterRow = tablBody.getElementsByTagName("tr")[2];
	tablBody.insertBefore(trRow,insertAfterRow);

}

function addClearGreetingCardLink(desc){
	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;

	trRow = document.createElement("tr");
	trRow.setAttribute("class","ltcolor");
	trRow.setAttribute("className","ltcolor");

	tdCellLink = document.createElement("a");
	tdCellLink.setAttribute("href","javascript:clearGreetingCard('"+desc+"');");
	tdCellName = document.createTextNode("Clear Greeting Card ["+desc+"]");
	tdCellLink.appendChild(tdCellName);

	tdCell = document.createElement("td");
	tdCell.setAttribute("align","center");
	tdCell.appendChild(tdCellLink);
	trRow.appendChild(tdCell);

	insertAfterRow = tablBody.getElementsByTagName("tr")[2];
	tablBody.insertBefore(trRow,insertAfterRow);

}


function removeLink(type,arrayName, desc){
	//remove link; find the right line

	compareString = type+" ["+desc+"]";

	tablBody = document.getElementById('mattingWindow').getElementsByTagName('tbody')[0];
	tablRowCount = tablBody.rows.length;
	for (sa=0;sa<tablRowCount;sa++){
		try {
			if (tablBody.rows[sa].cells[0].getElementsByTagName("a")[0].innerHTML == compareString){
				//found the right one
				tablBody.removeChild(tablBody.rows[sa]);
			}
		} catch (e){}
	}	
	
	
	//remove from itemArray
	arrayLength = itemArray.length;
	for (ew=0;ew<arrayLength;ew++){
		var arrayNam = "";
		try {
			arrayNam = itemArray[ew][1];
		} catch (w){}
		
		if (arrayNam==arrayName){
			//remove this line from the array
			itemArray.splice(ew,1);
		}
	}
	//refigure the page total
	pagePrice();
}

function submitForm(){

	arrayLength = itemArray.length;
	var items = "";
	var colors = "";
	
	for (ew=0;ew<arrayLength;ew++){
		try {
			if (ew==0){
				items = itemArray[ew][0];
				colors = itemArray[ew][4];
			} else {
				items = items + "," + itemArray[ew][0];
				colors = colors + "," + itemArray[ew][4];
			}
		} catch (w){}
	}

	document.addCartItem.type.value = items;
	document.addCartItem.specs.value = colors;
	
	document.addCartItem.submit();
}
