// jshop.js JavaScript Development by JavaScriptDesign.com

// ****Begin code section for catalog display****

// **Establish array variables. These must match the**
// **catalog array constructionin each page.**


var catNo = new Array(); // codigo (ou numero) do item - ex.: 001,peq001
var itemName = new Array(); // O nome do item
var itemDesc = new Array(); // A descrica do item
var price = new Array(); // O preco do item
var notes = new Array(); // Notas sobre o item
var ordQty = new Array(); // Vetor para armazenar a quantidade a ser comprada

// **Object constructor to build the catalog array.**
// **Catalog entries are in an array on each page.**

function loadCatalog(num,iNum,iName,iDesc,cost,info) {
 catNo[num] = iNum;
 itemName[num] = iName;
 itemDesc[num] = iDesc;
 price[num] = cost;
 notes[num] = info;
}

// **Function to write the content for each table row.**
// **This reads the array data and populates a table **
// **for catalog display.

function writeTableRow(i) {
document.write('<table cellpadding="1" cellspacing="1" bgcolor="#ffffff">'
  + '<td rowspan="4" bordercolor="#ffffff" bgcolor="#ffffff" align=center>' + itemName[i] + '</td>'
  + '<td bgcolor="#ffffff" width="160" align=center><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>' + itemDesc[i] + '</b></font></td></tr>'
  + '<td bgcolor="#ffffff" width="160" align=center><font face="Arial, Helvetica, sans-serif" size="2">Valor: ' + notes[i] + '</font></td>'
  + '<tr><td bgcolor="#ffffff" width="160" align=center><font face="Arial, Helvetica, sans-serif" size="2">Quantidade: <input type="text" size="3" value="" name="q'+i+'"></font>'
  + '<tr><td bgcolor="#ffffff" width="160" height="28" align=center><a href="javascript:addItem(' + i + ')"><img src="scart.gif" border="0" '
  + 'alt="Click aqui para adicionar este item em seu carrinho de compras"></a></td></tr>');
  }

// **Function to write the catalog display table to **
// **the current document.**

function writeTable() {
 document.write('<form name="form1"><table border=" " width="450" cellspacing="0" cellpadding="0" border="1" align="center">');
 for (i=0; i < catNo.length; i++)
  writeTableRow(i);
 document.write('</table></form>');
}

// ****End code section for writing catalog content.****


// ****Begin shared cookie functions****

// **Global expdate variable for cookies**
// **Cookie is set to expire in 24 hours**

var expdate = new Date()
expdate.setTime (expdate.getTime() + (1 * 24 * 60 * 60 * 1000))

// **Read cookie data.**

function getCookieData(name) {
	var label = name + "="
	var labelLen = label.length
	var cLen = document.cookie.length
	var i = 0
	while (i < cLen) {
		var j = i + labelLen
		if (document.cookie.substring(i,j) == label) {
			var cEnd = document.cookie.indexOf(";",j)
			if (cEnd == -1) {
				cEnd = document.cookie.length
			}
			return unescape(document.cookie.substring(j,cEnd))
		}
		i++
	}
	return ""
}

// **Write cookie data**

function setCookieData(name,value,expires) {
        counter ++
	document.cookie = name + "=" + counter + "@" + value + "; expires=" + expires
}

// **Kill cookie function. When the order is submitted**
// **the cookie is killed via an event handler call.**

function killCookie(name) {
 if (getCookieData(name)) {
 document.cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT";
 cookData = ""
 counter = 0
// history.go(0)
 }
}

// ****End shared cookie functions****


// ****Begin code section to update the cookie to ****
// ****add items to the shopping cart cookie 'Scart'.****

// **Global variables.**

var counter = 0
var cookData = ""

// **Extract current value of cookie when page loads **
// **and store the values in the global variables.**

if (getCookieData("Scart")) {
 orderString = getCookieData("Scart")
 cLen = orderString.length
 countEnd = orderString.indexOf("@")
 pointer = countEnd + 1
 counter = orderString.substring(0,countEnd)
 cookData = orderString.substring(pointer,cLen)
}

// **Function to add an item to the shopping cart cookie.**

function addItem(num) {
 a = eval("catNo[num]")
 ordQty[num]=eval("document.form1.q"+num+".value");
 b = eval("ordQty[num]")
 if (ordQty[num] == 0) {
  alert("Por favor, entre com a quantidade");
  return;
 }
 for (var i = 0; i < b.length; i++) {
  var oneChar = b.substring(i, i + 1)
  if (oneChar < "0" || oneChar > "9") {
   alert("Por favor, digite apenas numeros.")
   eval("document.form1.q"+num+".value = ''")
   return;
  }
 }
// return true;
 if (confirm("Adiciona o Item " + a + " para suas compras?")) {
 addCart = '' + a + '`' + ordQty[num] + '*' + itemName[num] + ' ' + itemDesc[num] + '~' + price[num] + '^'
 cookData += eval("addCart")
 setCookieData("Scart", cookData, expdate.toGMTString())
 }
}

// ****End code section for updating the cookie.****

// ****Function to reset the form elements everytime the page loads
// ****Remove th onLoad() event handler from the catalog body tags
// ****to eliminate this feature.

function clearQtys() {
 document.form1.reset()
}

// ****Debugging function to display cookie contents.****

function showMe() {
 display = getCookieData("Scart");
 alert(display)
}


