function confirmFields(formName) {
	var errorMessage = "";

	for (var i = 0; i < document.forms[formName].elements.length; i++) {
	if (document.forms[formName].elements[i].value == "" && document.forms[formName].elements[i].attributes["required"] != null )
      errorMessage += "*   " + document.forms[formName].elements[i].attributes["required"].value + "\n";
	}

	if (errorMessage != "") {
		alert ("Please fill in the following fields:\n\n" + errorMessage);
		return false;
	} else return true;
}

function toggle(id)
{
	var item = document.getElementById(id);
	
	if (item.style.display == 'none')
		item.style.display = 'block';
	else
		item.style.display = 'none';
}

function getExpiryDate(nodays) {
  var UTCstring;
  Today = new Date();
  nomilli = Date.parse(Today);
  Today.setTime(nomilli + nodays * 24 * 60 * 60 * 1000);
  UTCstring = Today.toUTCString();
  return UTCstring;
}

function getCookie(cookieName) {
  var cookieString = "" + document.cookie;
  var index1 = cookieString.indexOf(cookieName);
  if (index1 == -1 || cookieName == "") return "";
  var index2 = cookieString.indexOf(';', index1);
  if (index2 == -1) index2 = cookieString.length; 
  return unescape(cookieString.substring(index1 + cookieName.length + 1, index2));
}

function setCookie(name, value, duration) {
  cookieString = name + "=" + escape(value) + ";EXPIRES=" + getExpiryDate(duration);
  document.cookie = cookieString;
  if(!getCookie(name))
    return false;
  else
    return true;
}

function toggleNav(id) {
  ul = "ul_" + id;
  ulElement = document.getElementById(ul);
  if (ulElement) {
    if (ulElement.className == 'closed') {
      ulElement.className = "open";
      setCookie("ul_" + id, 'open', 20);
    }
    else {
      ulElement.className = "closed";
      setCookie("ul_" + id, 'closed', 20);
    }
  }
}

function checkStatus(id) {
  ulElement = document.getElementById(id);
  if (getCookie(id) == "open")
    ulElement.className = "open";
}

function validateEmail(field)
{
	var emailAddr = document.getElementById(field).value;

	if (isValidEmail(emailAddr))
		return true;
	else
	{
		alert('Email address is not valid, please fix.');
		return false;
	}
}

function isValidEmail(email)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
		return true;
	else
		return false;
}

function showHide(divName, watcherName, id) {
  divName = document.getElementById(divName+id);
  watcherName = document.getElementById(watcherName+id);
  if (divName.style.display == 'none') {
    divName.style.display = 'block';
    watcherName.value = 'block';
  } else {
    divName.style.display = 'none';
    watcherName.value = 'none';
  }
}
  
  
function validateUserInfo()
{
	if (validateEmail('email') && validatePasswords('password', 'password2'))
		return confirmFields('editUserForm');
	else
		return false;
}

function validatePasswords(pass1, pass2)
{
	if (document.getElementById(pass1).value == document.getElementById(pass2).value)
		return true;
	else
	{
		alert('Your passwords do not match!');
		return false;
	}
}


function showImage() {
  var bigImages = new Array();
  
  bigImages[0] = 'images/big-image-1.jpg';
  bigImages[1] = 'images/big-image-2.jpg';
  bigImages[2] = 'images/big-image-3.jpg';
  bigImages[3] = 'images/big-image-4.jpg';
  
  var whichBigImage = Math.round(Math.random() * (bigImages.length - 1));
 
  document.writeln('<img src="'+bigImages[whichBigImage]+'" />');
}

function clearForm(formName)
{
	for (var i = 0; i < document.forms[formName].elements.length; i++) {
		document.forms[formName].elements[i].value = "";
	}
}

function togglePrice(id)
{
	dropDown = document.getElementById(id);
	currentAttr = dropDown.options[dropDown.selectedIndex].value;
	
	divs = document.getElementsByTagName('div');
	inputs = document.getElementsByTagName('input');
	
	for (i = 0; i < inputs.length; i++)
	{
		if (inputs[i].type == 'radio')
		{
				inputs[i].checked = false;
		}
	}
	
	for (i = 0; i < divs.length; i++)
	{
			if (divs[i].attributes['productAttr'] != null)
			{
					divs[i].style.display = 'none';
					
					if (divs[i].attributes['productAttr'].value == ('attr-' + currentAttr) || divs[i].attributes['productAttr'].value == ('price-' + currentAttr))
						divs[i].style.display = 'block';		
			}
	}
}

function validateProductAdd()
{
	inputs = document.getElementsByTagName('input');
	atleastOne = false;

	for (i = 0; i < inputs.length; i++)
	{
		if (inputs[i].type == 'radio')
		{	
				atleastOne = true;
				if (inputs[i].checked)
					return true;
		}
	}	
	
	if (!atleastOne)
		return true;
	
	alert("You must select an option before adding the product to your cart.");	
	return false
}

function toggleBillingInformation()
{
	billingInfo = document.getElementById('billing-table');
	
	if (billingInfo.style.display == "none")
		billingInfo.style.display = "block";
	else
		billingInfo.style.display = "none";
}

function confirmOrderFields(formName) {
	var errorMessage = "";
	same = document.getElementById('billingSameAsShipping');

	for (var i = 0; i < document.forms[formName].elements.length; i++) {
	if (document.forms[formName].elements[i].value == "" && document.forms[formName].elements[i].attributes["required"] != null )
			
			if (same.checked == true)
			{
				if (document.forms[formName].elements[i].name.indexOf('billing') != 0 && same.checked == true)		
					errorMessage += "*   " + document.forms[formName].elements[i].attributes["required"].value + "\n";
			}
			else
				errorMessage += "*   " + document.forms[formName].elements[i].attributes["required"].value + "\n";
		
      	
	}

	if (errorMessage != "") {
		alert ("Please fill in the following fields:\n\n" + errorMessage);
		return false;
	} else return true;
}
