/*	
	
	Early Childhood Mall
	Author: Denise Farmer
	Date: 	12/8/09
	
	Filename:		project.js
	Supporting files: account.html, boyshoesinfant0-1.html, boyshoesinfant2.html, cart.html, childrensshoesindex.html, confirm.html, contact.html, index.html, projectstyles.css, shipping.html, thankyou.html
	
	Function List:
	makeCookie()
		Creates two cookies that hold the user's first and last names.
	findCookie()
		Extracts values from the cookies that have been stored by the website.
	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.
	delProd()
		Deletes the cookie holding information about an item that has been placed in
		the shopping cart.
	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.	
	checkAccountForm()
		Verifies all fields in the account.html page are completed and then
		sends the user to the makeCookie() function.	
	checkContactForm()
		Verifies all fields in the contact.html page are completed.
	placeOrder()
		Verifies that a numeric value has been placed in a quantity field in the
		boyshoesinfant0-1.html and boyshoesinfant2.html pages. The function then passes the order        information to the function cookieOrder().		
	showOrder()
		Extracts the items held in cookies and displays their values in a
		dynamically created table.	
	confirmCartForm()
		Verifies all field in the cart.html form are completed and extracts all cookies that hold        ordering and payment information and displays this data in a form.
			
*/

/* Functions for Cookies */

// creates a variable that stores a date and time two weeks in the future; this variable is then used as the expires property for each cookie in the makeCookie function
var now = new Date();
var expdate = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);

function makeCookie(fn,ln,user,pswd) {
	// creates first name cookie
	document.cookie = "fn = "+fn+"; expires="+expdate.toGMTString()
	// creates last name cookie
	document.cookie = "ln = "+ln+"; expires="+expdate.toGMTString()
	// creates username cookie
	document.cookie = "user = "+user+"; expires="+expdate.toGMTString()
	// creates password cookie
	document.cookie = "pswd = "+pswd+"; expires="+expdate.toGMTString()
	// opens site's home page which will be personalized with the first nane and last name data
	window.location = "index.html";
}

// function findCookie extracts values from the cookies that have been stored by the the web site; the parameter is a cookie; the function uses the IndexOf() method to extract the position value of the first character of the cookie's value, storing it in the lastVal variable; it then uses these two as parameters for the substring method to extract the cookie's values as a text string, assigning it to the cookie variable

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;
}

// this code uses the findCookie function to assign the values of the cookies you'll be using to variables, which can then be used to personalize the website

fn = findCookie("fn");
ln = findCookie("ln");
user = findCookie("user");
pswd = findCookie("pswd");

// function makeOrderCookie builds a single cookie whose value holds the product number, product name, product price, and quantity ordered

function makeOrderCookie(n,id,prod,pr,q) {
	document.cookie = "myitem"+n+" = "+id+","+prod+","+pr+","+q+";expires = "+expdate.toGMTString();
	alert("You have added "+q+" of the "+prod+" to your shopping cart.");
}

// function delProd is used to delete an item from the shopping cart; it expires the cookie that called it and reloads the web page in the browser, updating the display of the cart contents

function delProd(prod) {
	document.cookie = "myitem"+prod+"=;expires=Thu, 01-Jan-70 00:00: 01 GMT;";
    location.reload();
}

// function makeFormCookie creates one cookie for each input field in the form; the first two lines create cookies for the first name and last name in case user has not created an account on the account page; next it creates an array with the id "form" containing the values in the remaining form fields; it concludes by creating a cookie for each of the values in the array, including the expiration date configured earlier

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();
	}
}

// these variables are used to extract the form values from the cookies created by the makeFormCookie function
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 will pass an expiration date of thu 01-Jan-70 to each variable, and replace the expiration date of each cookie with this variable, expiring each cookie instantly; this function is called after the confirmation page opens - as the page is loading the data the cookies will deleted except for the user's first name, last name, username and password; to call this function you will need to place an body onload event handler on the confirmation page

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+";";
   }
}

/* functions for form validation and cart calculations & display */

function checkAccountForm(){
	var fn = document.account.fn.value;
	var ln = document.account.ln.value;
	var user = document.account.user.value;
	var pswd = document.account.pswd.value;
	var pswd2 = document.account.pswd2.value;

	if (fn == ""){
			alert("You must enter a first name.");
			return false;
		}
        else if (ln == "") {

			alert("You must enter a last name.");
			return false;
                }
        else if (user == "") {

			alert("You must enter a user name.");
			return false;
                }
        else if (pswd == "") {

			alert("You must enter a password.");
			return false;
                }
        else if (pswd2 == "") {

			alert("You must confirm your password.");
			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.qty0.value = "";
		}
	else if(n == 1)
		{
			document.order1.qty1.value = "";
		}
	else if(n == 2)
		{
			document.order2.qty2.value = "";
		}
	else
		{
			document.order3.qty3.value = "";
		}
}

function checkContactForm(){
	var fullname = document.contact.fullname.value;
	var email = document.contact.email.value;
	var message = document.contact.message.value;

	if (fullname == ""){
			alert("Please enter your full name.");
			return false;
		}
        else if (email == "") {

			alert("Please enter your email address.");
			return false;
                }
        else if (message == "") {

			alert("Please enter your message.");
			return false;
                }
}

function placeOrder(id,prod,pr,n){
	switch(n)
		{
			case 0:
				q = document.order0.qty0.value;
				break;
			case 1:
				q = document.order1.qty1.value;
				break;
			case 2:
				q = document.order2.qty2.value;
				break;
			case 3:
				q = document.order3.qty3.value;
				break;
			case 4:
				q = document.order4.qty4.value;
				break;
			case 5:
				q = document.order5.qty5.value;
				break;
			case 6:
				q = document.order6.qty6.value;
				break;
			case 7:
				q = document.order7.qty7.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");	
	prod[4] = findCookie("myitem4");	
	prod[5] = findCookie("myitem5");	
	prod[6] = findCookie("myitem6");	
	prod[7] = findCookie("myitem7");	
	prod[8] = findCookie("myitem8");

	if((prod[0] == null)&&(prod[1] == null)&&(prod[2] == null)&&(prod[3] == null)&&(prod[4] == null)&&(prod[5] == null)&&(prod[5] == null)&&(prod[7] == null)&&(prod[8] == null))
		{
			document.write("<tr><td colspan='5' align='center'>");
			document.write("Your cart is empty.");
			document.write("</td></tr>");
		}
	for(i=0;i<=prod.length;i++)
		{
		if((prod[i] != null)&&(prod[i] != ""))
			{
			document.write("<tr><td>");
			start =  prod[i].substring(0,6);
			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>");

			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>");

			quant = parseInt(prod[i].substring(prod[i].length-1,prod[i].length));
			document.write(quant+"</td><td>");

			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='center'>The total cost of your order is <b>$"+subTot.toFixed(2)+"</b></td></tr>");
}

function confirmCartForm() {
	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 == "") {
			alert("You must enter a first name.");
			return false;
		}
         else if (ln == "") {
			alert("You must enter a last name.");
			return false;
		}
         else if (c0 == "") {
			alert("You must enter a street address.");
			return false;
		}
         else if (c1 == "") {
			alert("You must enter a city.");
			return false;
		}
          if (c2 == "") {
			alert("You must enter a state.");
			return false;
		}
         else if (c3 == "") {
			alert("You must enter a zip code.");
			return false;
		}
         else if (c4 == "") {
			alert("You must choose a type of credit card.");
			return false;
		}
         else if (c5 == "") {
			alert("You must enter a valid credit card number.");
			return false;
		}
        else if (c6 == "") {
			alert("You must enter the month your credit card expires.");
			return false;
		}
         else if (c7 == "") {
			alert("You must enter the year your credit card expires.");
			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 = "";
	}

