// JavaScript Document


function setManekinCookie(id) {
	document.cookie=("icon="+id);
	//alert("id = " + id);
}

function setIcon(id)
{
	document.images['man0'].src="images/spacer.gif"
	document.images['man1'].src="images/spacer.gif"
	document.images['man2'].src="images/spacer.gif"
	document.images['man3'].src="images/spacer.gif"
	document.images['man4'].src="images/spacer.gif"
	document.images['man5'].src="images/spacer.gif"
	if(id)
	{
		var icon = "man"+id;
		//alert("icon = " + icon);
		document.images[icon].src="images/manekin_ico.gif"
	}
}

function setCategory(catID)
{
	document.cookie = "catID="+catID;
	//alert(document.cookie)
}


function setProdID(prodID)
{
	document.cookie = "prodID="+prodID;
	//alert(document.cookie)
}




function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) 
				return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
function addProductToCart(prodID)
{
	if (readCookie(prodID))
	{
		var qty = readCookie(prodID);
		//document.write("qty = "+ qty)
		qty = parseInt(qty) + 1;
		document.cookie = prodID+"="+qty+";";
	}
	else
	{
		document.cookie = prodID+"="+1+";";
	}
}

*/

function addProductToCart(prodID)
{
	var found = false; 
	if (readCookie('PL'))
	{
		var temp = readCookie('PL');
		var ca = temp;
		ca = ca.split("|");
		for (i=0; i<ca.length; i++)  //Update qty
		{
			ca[i] = ca[i].split(":");
			if(ca[i][0] == prodID)
			{
				ca[i][1] = parseInt(ca[i][1]) + 1;
				found = true;
			}
		}
		var pl = "";
		if(found == false)
		{
			pl = temp + "|"+prodID+":1";
		}
		else //New item added
		{
			for(i=0; i<ca.length; i++)
			{
				pl += "|"+ca[i][0]+":"+ca[i][1];
			}
			pl = pl.substring(1, pl.length);
		}
		//Create cookie products list
		document.cookie = "PL="+pl;
	}
	
	else //First Item added to the list
	{
		document.cookie = "PL="+prodID+":1";
	}
}


function validateInput(fName, fValue)
{
	var pattern;
	if(fName == "phone")
	{
		pattern = /^\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4}$/i;
	}
	if(fName=="first name" || fName=="last name"|| fName=="country" || fName=="city")
	{
		pattern = /^[a-z,A-Z -\.]{1,}$/i; 
	}
	// PHONE
	if(fName == "phone")
	{
		pattern = /^\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4}$/i; 
	}
	// ZIP CODE
	if(fName == "zipcode")
	{
		pattern = /^\w\d\w(\s|-)?\d\w\d$/i; 
	}
	// EMAIL
	if(fName == "email")
	{
		pattern = /^[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+$/i; 
	}
	// CREDIT CARD
	if(fName == "credit card")
	{
		pattern = /^[0-9]{16}$/i; 
	}
	
	if(pattern.test(fValue) == false)
	{
		alert(fName+" is invalid!");
		//document.getElementByTagName(fName).focus();
		//document.getElementById(fName).focus();
		document.forms.input[fName].focus();
	}
}
