/*	
	New Perspectives on JavaScript
	Tutorial 9
	Tutorial Case
	
	Patti's Potpourri
	Name: Georgia Wright
   	CIS 377 W1
   	Filename:		patti.js
   	Assignment6 FINAL
   	Date: March 12, 2007  	
	
	
	Supporting files:	patti_cart.htm, patti_checkout.htm, patti_confirm.htm,
				patti_home.htm,patti_account.htm, patti_trinkets.htm
	Function List:
	checkForm()
		Verifies all fields in the patti_account.htm page are completed and then then
		sends the user to the makeCookie() function.
	makeCookie()
		Creates two cookies that hold the user's first and last names.
	getCookie()
		Used to extract the value of a cookie based on its name.
	checkcookie()
		Checks for cookies when the user tries to access the patti_cart.htm page or
		the patti_trinkets.htm page. Redirects the user to the patti_account.htm page
		if there are no cookies.
	placeOrder()
		Verifies that a numeric value has been placed in a quantity field in the
		patti_trinkets.htm page. The function then passes the order information to the
		function cookieOrder().
	makeOrderCookie()
		This function takes the values from the placeOrder() function and makes cookies
		that remember information about a particular product purchased. It then informs
		the user via an alert box of the purchase added to the shopping cart.
	showOrder()
		Extracts the items held in cookies and displays their values in a
		dynamically created table.
	delProd()
		Deletes the cookie holding information about an item that has been placed in
		the shopping cart.
	confirmForm()
		Extracts all cookeis that hold ordering and payment information and
		displays this data in a form.
	makeFormCookie()
		Takes the values from the confirmForm() function and creates cookiees that
		remembers the user's mailing information.
	zapOrder()
		Clears all cookies including the cookies that hold the user's first and last
		name.
		
		
*/

var now = new Date;
var expdate = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);

function makeCookie(fn,ln,user,pswd) {
	document.cookie = "fn = "+fn+";expires="+expdate.toGMTString();
	document.cookie = "ln = "+ln+";expires="+expdate.toGMTString();
	document.cookie = "user = "+user+";expires="+expdate.toGMTString();
	document.cookie = "pswd = "+pswd+";expires="+expdate.toGMTString();

	
	window.location = "patti_home.htm";
}

function findCookie(val) {
	var cookie = null;
	var findVal = val + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		var start = dc.indexOf(findVal);
		if (start >=0) {
			start += findVal.length;
			lastVal = dc.indexOf(";", start);
			if (lastVal == -1) {
				lastVal = dc.length;
			}
			cookie = (dc.substring(start, lastVal));
		} else {
			return cookie;
		}
	}
		return cookie;
}



fn = findCookie("fn");
ln = findCookie("ln");
user = findCookie("user");
pswd = findCookie("pswd");

function makeOrderCookie(n,id,prod,pr,q){
	document.cookie = "myitem"+n+" = "+id+","+prod+","+pr+","+q+";expires = "+expdate.toGMTString() +";path /";
	alert("You have added "+q+" of the "+prod+" to your shopping cart.");
}

function delProd(prod) {
	document.cookie = "myitem"+prod+" = ;expires = Thu, 01-Jan-70 00:00 01 GMT;";
		location.reload();
}

function makeFormCookie(fn,ln,c0,c1,c2,c3,c4,c5,c6,c7) {
	document.cookie = "fn = "+fn+"; expires = "+expdate.toGMTString();
	document.cookie = "ln = "+ln+"; expires = "+expdate.toGMTString();
	var form = new Array();
	form[0] = c0;
	form[1] = c1;
	form[2] = c2;
	form[3] = c3;
	form[4] = c4;
	form[5] = c5;
	form[6] = c6;
	form[7] = c7;
	for(i=0;i<9;i++) {
		document.cookie = "c"+[i]+" = "+form[i]+"; expires = "+expdate.toGMTString();
	}
}

c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");
c4 = findCookie("c4");
c5 = findCookie("c5");
c6 = findCookie("c6");
c7 = findCookie("c7");

function zapOrder() {
	deldate = "Thu, 01-Jan-70 00:00:01 GMT";
		for (i=0;i<=4;i++) {
			document.cookie = "myitem"+[i]+" = ; expires = "+deldate+";";
		}
		for (i-0;i<=10;i++) {
			document.cookie = "c"+[i]+" = ; expires = "+deldate+";";
		}
}
/* Helper Functions */

function checkForm(){
	var fn = document.login2.fn.value;
	var ln = document.login2.ln.value;
	var user = document.login2.user.value;
	var pswd = document.login2.pswd.value;
	var pswd2 = document.login2.pswd2.value;
	if((fn == "")||(ln == "")||(user == "")||(pswd == "")||(pswd2 == ""))
		{
			alert("Please fill out all input fields on this form.");
			return false;
		}
	else if(pswd != pswd2)
		{
			alert("Your passwords did not match. Please try again.");
			return false;
		}
	else
		{
			makeCookie(fn,ln,user,pswd);
		}

//}

if(fn == null)
	{
		fn = "";
	}
if(ln == null)
	{
		ln = "";
	}
if(user == null)
	{
		user = "";
	}
}
	
function clearValue(n){
	if(n == 0)
		{
			document.order0.quantity0.value = "";
		}
	else if(n == 1)
		{
			document.order1.quantity1.value = "";
		}
	else if(n == 2)
		{
			document.order2.quantity2.value = "";
		}
	else
		{
			document.order3.quantity3.value = "";
		}
}

function placeOrder(id,prod,pr,n){
	switch(n)
		{
			case 0:
				q = document.order0.quantity0.value;
				break;
			case 1:
				q = document.order1.quantity1.value;
				break;
			case 2:
				q = document.order2.quantity2.value;
				break;
			case 3:
				q = document.order3.quantity3.value;
		}
	if(q == 0)
		{
			alert("Please enter a quantity greater than zero.");
		}
makeOrderCookie(n,id,prod,pr,q);
}

function showOrder(){
	var subTot = 0;
	var prod = new Array();
	prod[0] = findCookie("myitem0");
	prod[1] = findCookie("myitem1");
	prod[2] = findCookie("myitem2");
	prod[3] = findCookie("myitem3");
	var j = 0;
	if((prod[0] == null)&&(prod[1] == null)&&(prod[2] == null)&&(prod[3] == null))
		{
			document.write("<tr><td>");
			document.write("Your cart is empty.");
			document.write("</td></tr>");
		}
	for(i=0;i<=prod.length;i++)
		{
		if((prod[i] != null)&&(prod[i] != ""))
			{
			var j = j+1;
			document.write("<tr><td>");
			start =  prod[i].substring(0,4);
			document.write(start+"</td><td>");

			name = prod[i].indexOf(",")+1;
			name2 = prod[i].indexOf(",",name);
			prod_name = prod[i].substring(name,name2);
			document.write(prod_name+"</td><td class=right'>");
			document.write("<input type='hidden' name='item_name_"+[j]+"' value='"+prod_name+"'>");
			price = prod[i].indexOf(",",name2)+1;
			price2 = prod[i].indexOf(",",price);
			prod_price = parseFloat(prod[i].substring(price,price2));
			price = prod_price.toFixed(2);
			document.write("$"+price+"</td><td class='center'>");
			document.write("<input type='hidden' name='amount_"+[j]+"' value='"+price+"'>");		
			
			quant = parseInt(prod[i].substring(prod[i].length-1,prod[i].length));
			document.write(quant+"</td><td class='right'>");
			document.write("<input type='hidden' name='quantity_"+[j]+"' value='"+quant+"'>");
			cost_raw = quant*prod_price;
			cost_tot = cost_raw;
			subTot += cost_tot;
			cost = cost_raw.toFixed(2);
			document.write("$"+cost+"</td><td>");
			document.write("<a href=# onclick = 'delProd("+[i]+")'>Remove Item</a>");
			document.write("</td><td>");
			}
	  	}
	document.write("<tr><td colspan = '5' align = 'right'>The total cost of your order is <b>$"+subTot.toFixed(2)+"</b></td></tr>");
}

function confirmForm(){
	fn = document.order.fn.value;
	ln = document.order.ln.value;
	c0 = document.order.add.value;
	c1 = document.order.city.value;
	c2 = document.order.st.value;
	c3 = document.order.zip.value;
	c = -1
          for(i = 0; i<document.order.pay.length; i++)
		{  
          	if(document.order.pay[i].checked)
		  	{
          			c = i; 
		  		c4 = document.order.pay[i].value;
          		}
		}
		  
	c5 = document.order.card.value;
	c6 = document.order.month.value;
	c7 = document.order.year.value;
	  if((fn == "")||(ln == "")||(c0 == "")||(c1 == "")||(c2 == "")||
		 (c3 == "")||(c4 == "")||(c5 == "")||(c6 == "")||(c7 == ""))
		{
			alert("Please complete all parts of this form.");
			return false;
		}
	makeFormCookie(fn,ln,c0,c1,c2,c3,c4,c5,c6,c7);
}

if(c0 == null)
	{
		c0 = "";
	}
if(c1 == null)
	{
		c1 = "";
	}
if(c2 == null)
	{
		c2 = "";
	}
if(c3 == null)
	{
		c3 = "";
	}
if(c4 == null)
	{
		c4 = "";
	}
if(c5 == null)
	{
		c5 = "";
	}
if(c6 == null)
	{
		c6 = "";
	}
if(c7 == null)
	{
		c7 = "";
	}

