function setActive() {
  aObj = document.getElementById('navtd').getElementsByTagName('a');
  for(i=0;i<aObj.length;i++) {
    if(document.location.href.indexOf(aObj[i].href)>=0) {
      aObj[i].className='active';
    }
  }
}

function setActiveSub() {
  mainObj = document.getElementById('storelink');
  mainObj.style.color = '#9C1617';
  aObj = document.getElementById('subnavtd').getElementsByTagName('a');
  for(i=0;i<aObj.length;i++) {
    if(document.location.href.indexOf(aObj[i].href)>=0) {
      aObj[i].className='active';
    }
  }
}

function setActiveType() {
  aObj = document.getElementById('type').getElementsByTagName('a');
  for(i=0;i<aObj.length;i++) {
    if(document.location.href.indexOf(aObj[i].href)>=0) {
      aObj[i].className='active';
    }
  }
}

function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { 
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validate(theForm) 
{
   var field = theForm.keywords; 
        if (field.value.length == 0) 
        { 
		field.style.background = "#ffff99";
                alert("The search box is empty.\n\nPlease fill in the search box and submit the form again.");
                return false; 
        } 
} 

function validateAdmin() 
{
   var field = document.getElementById("searchval"); 
        if (field.value.length == 0) 
        { 
		field.style.background = "#ffff99";
                alert("The search box is empty.\n\nPlease fill in the search box and submit the form again.");
                return false; 
        }
	else
	{
		ajaxFunction();
	} 
} 

function CheckFormValues(theForm)
{
        var elem = theForm.elements;
        for(var i = 0; i < elem.length; i++)
        {
            if (elem[i].name != "featured_desc" && elem[i].name != "large_image2" && elem[i].name != "large_image3" && elem[i].name != "colors")
            {
				if (elem[i].value == "")
				{
                	alert("Please fill in all empty fields, select dropdown values and try again.");
                	return false;
					break;
				}
            }
        } 
}

function CheckFormValuesEdit(theForm)
{
	var elem_array = new Array();
	elem_array[0] = theForm.name;
	elem_array[1] = theForm.description;
	elem_array[2] = theForm.price;
	elem_array[3] = theForm.featured;

        for(var i = 0; i < elem_array.length; i++)
        {
		if (elem_array[i].value == "")
		{
                	alert("Please fill in all empty fields, select dropdown values and try again.");
                	return false;
			break;
		}
        }  
}

function IsZip(theForm)
{
	var field = theForm.zipcode;

	if (field.value.length == 0) 
        { 
		field.style.background = "#ffff99";
                alert("The zip code field is empty.\n\nPlease type in a valid 5 digit zip code and try again.");
                return false; 
        }
	else
	{
		if (field.value%1==0 && field.value != 0 && field.value.indexOf("-") < 0 && field.value.indexOf(".") < 0)
		{
			return true;
		}
		else
		{
			field.style.background = "#ffff99";
                	alert("The zip code you entered is invalid.\n\nPlease type in a valid 5 digit zip code and try again.");
			return false;
		}
	} 
	
}


function IsInteger(theForm)
{
	var field = theForm.quantity;

	if (field.value.length == 0) 
        { 
		field.style.background = "#ffff99";
                alert("The quantity field is empty.\n\nPlease type in a quantity (1 - 99) and try clicking the buy button again.");
                return false; 
        }
	else
	{
		if (field.value%1==0 && field.value != 0 && field.value.indexOf("-") < 0 && field.value.indexOf(".") < 0)
		{
			return true;
		}
		else
		{
			field.style.background = "#ffff99";
                	alert("The quantity field has an invalid quantity.\n\nPlease type in a valid quantity (1 - 99) and try clicking the buy button again.");
			return false;
		}
	} 
	
}

function editProduct(id)
{
	var LeftPosition = (screen.width) ? (screen.width-630)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-600)/2 : 0;
   	window.open("editProduct.php?id=" + id + "","" + id + "","width=630,height=600,left=" + LeftPosition + ",top=" + TopPosition + ",scrollbar=yes,resizable=yes,location=no,status=no,menubar=no,toolbar=no");	
}

function addProduct()
{
	var LeftPosition = (screen.width) ? (screen.width-630)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-500)/2 : 0;
	window.open("addProduct.php","Add","width=630,height=500,left=" + LeftPosition + ",top=" + TopPosition + ",scrollbar=yes,resizable=yes,location=no,status=no,menubar=no,toolbar=no");
}

function delProduct(id)
{
	var LeftPosition = (screen.width) ? (screen.width-630)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-500)/2 : 0;
	window.open("delProduct.php?id=" + id + "","" + id + "","width=630,height=500,left=" + LeftPosition + ",top=" + TopPosition + ",scrollbar=yes,resizable=yes,location=no,status=no,menubar=no,toolbar=no");
}

function addTypes()
{
	var str = "";
	var type_array = document.coffeeform.type;
	
	for (var i=0; i < type_array.length; i++)
	{
		if (type_array[i].checked)
		{
			str = str + type_array[i].value + ", ";
  		}
	}

	var type_value = str.substring(0, str.length-2);

	coffeeform.type_value.value = type_value;
}
