// JavaScript Document
function submitForm(form)
	{
					
		var req = false;
		var dateError = false;
		var errorMessage = "";
		
		//Check fields
		var required = $$('form#'+form.id+' .required');
		required.each(function(element) {
					     
				$(element).setStyle("background-color", "#F3F3F3");
				if($(element).value == "")
				{
					$(element).setStyle("background-color", "#FFBFCC");
					req = true;
				}
			});
		
		//Check date fields
		if($$('form#'+form.id+' .date'))
		{
			var dates = $$('form#'+form.id+' .date');
			dates.each(function(element) {
						     
					if($(element).value != "" && checkDate($(element)) == false)
					{
						dateError = true;
					}
					else
					{
						dateError = false;
					}
				});
		}
		
		//Error messaeges
		if(req == true)
			{
				errorMessage = "Please complete the required fields highlighted in red";
				if(dateError == true)
				{
					errorMessage += " and make sure your date(s) are in the correct format";
				}
				alert(errorMessage);
				return false;
			}
		else if(req == false && dateError == true)
			{
				alert("Please make sure your date(s) are in the correct format");
			}
		else if(req == false)
			{
				return true;
			}
	}

function getSubCats(level,cat_id)
	{
		//get all sub cats
		var subcats = $$('select.subcat');
		subcats.each(function(element){
				
				el_level = element.id.split("_")[1];
				if(parseInt(el_level) > parseInt(level))
				{
					$('catlevel_'+el_level).destroy();
				}
				
			});
		
		if(!$('catlevel_'+level))
		{		
			//create new div element
			var div = new Element('div', {'id': 'catlevel_'+level, 'class':'subcat'});
			div.injectBottom($('categories'));
		}
		
		//get subcats drop down list
		var url = "get_sub_cat.asp";
		var data = "cat_id="+cat_id+"&level="+level;

		var req = new Request.HTML({url:url, method:'get', 
			onSuccess: function(html) {
				$('catlevel_'+level).empty();
				$('catlevel_'+level).adopt(html);
				
				//add onchange category
				if($('cat_'+level))
				{
					$('cat_'+level).addEvent('change', function(element){
							getSubCats(level+1,this.options[this.selectedIndex].value);
						});
				}
				
				//update category hierarchy 
				var cat = "";
				var count = 0; 
 				$('breadcrumb').setAttribute('value',"");
				
				var catstring = $$('select.catstring');
				catstring.each(function(element){
										
						id = element.id.split("_")[1];
						if(id == "cat")
						{
							id = 1;
						}
						if(parseInt(id) <= level)
						{				
							var value = element.options[element.selectedIndex].text;
							if(value != "" && value != "Select Sub Category")
							{
								if(count ==0)
								{
									cat += value;
								}
								else
								{
									cat += " > " + value;
								}
							}
						}
						
						count++;
				});
				
				$('breadcrumb').set('text', cat);
				
			},
	
			onFailure: function() {
				$('catlevel_'+level).set('text', 'The request failed.');
			}
		});
		req.send(data);
			
	
		//update products
		var url2 = "get_products.asp";
		var products = new Request.HTML({url:url2, method:'get', 
			onRequest: function() {
				$('products_content').empty();
				$('products_content').addClass('loading');
			},
			onSuccess: function(html) {
				$('products_content').removeClass('loading');
				$('products_content').adopt(html);
				
				if($('viewproducts'))
				{
					$('viewproducts').addEvent('click', function(element){
							viewProducts(cat_id);
						});
				}
				
				if($$('span.add_product'))
				{
					$$('span.add_product').each(function(element){
									
						element.addEvent('click', function(){
									addToFaves(element.id);					   
									});
													
					});
				}
				if($$('span.remove_product'))
				{
					$$('span.remove_product').each(function(element){
									
						element.addEvent('click', function(){
									removeFromFaves(element.id);					   
									});
													
					});
				}
				
				var links = $$("a").filter(function(el) {
					return el.rel && el.rel.test(/^lightbox/i);
				});
				$$(links).slimbox({/* Put custom options here */}, null, function(el) {
					return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
				});
				
				
			},
	
			onFailure: function() {
				$('products_content').set('text', 'The request failed.');
			}
		});
		products.send(data);
	}
	
function addToFaves(id) 
	{
		var product_id = id.split("_")[1];
		
		//update products
		var data = "id="+product_id;
		var url3 = "add_to_faves.asp";
		var add = new Request.HTML({url:url3, method:'get', 
			onRequest: function() {
				$('add_'+product_id).empty();
				$('add_'+product_id).removeClass('add_product');
				$('add_'+product_id).removeClass('hand');
				$('add_'+product_id).addClass('loading');
			},
			onSuccess: function(html) {
				$('add_'+product_id).removeClass('loading');
				$('add_'+product_id).addClass('product_added');
				$('add_'+product_id).adopt(html);
			},	
			onFailure: function() {
				$('products_content').set('text', 'The request failed.');
			}
		});
		add.send(data);
		
	}
function removeFromFaves(id) 
	{
		var fav_id = id.split("_")[1];
		
		//update products
		var data = "id="+fav_id;
		var url4 = "remove_from_faves.asp";
		var add = new Request.HTML({url:url4, method:'get', 
			onRequest: function() {
				$('remove_'+fav_id).empty();
				$('remove_'+fav_id).removeClass('remove_product');
				$('remove_'+fav_id).removeClass('hand');
				$('remove_'+fav_id).addClass('loading');
			},
			onSuccess: function(html) {
				$('remove_'+fav_id).removeClass('loading');
				$('remove_'+fav_id).addClass('product_removed');
				$('remove_'+fav_id).adopt(html);
			},	
			onFailure: function() {
				$('products_content').set('text', 'The request failed.');
			}
		});
		add.send(data);
		
	}
	
function viewProducts(cat_id)
	{
		var url = "view_products.asp";
		var data = "cat_id="+cat_id;
		
		var products = new Request.HTML({url:url, method:'get', 
			onRequest: function() {
				$('products_content').empty();
				$('products_content').addClass('loading');
			},
			onSuccess: function(html) {
				$('products_content').removeClass('loading');
				$('products_content').adopt(html);
				
				
				if($$('span.add_product'))
				{
					$$('span.add_product').each(function(element){
									
						element.addEvent('click', function(){
									addToFaves(element.id);					   
									});
													
					});
				}
				if($$('span.remove_product'))
				{
					$$('span.remove_product').each(function(element){
									
						element.addEvent('click', function(){
									removeFromFaves(element.id);					   
									});
													
					});
				}
				var links = $$("a").filter(function(el) {
					return el.rel && el.rel.test(/^lightbox/i);
				});
				$$(links).slimbox({/* Put custom options here */}, null, function(el) {
					return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
				});
				
			},
	
			onFailure: function() {
				$('products_content').set('text', 'The request failed.');
			}
		});
		products.send(data);
		
	}
	

//Attach Events
window.addEvent('domready', function() {
			
		if($('top_cat'))
		{
			$('top_cat').selectedIndex = 0;	
		}
		
		if($$('li.expand'))
		{
			var expand = $$('li.expand span');
			expand.each(function(element) {
				
				id = element.parentNode.id+"_sub";
				
				
				window['slideNav_'+id] = new Fx.Slide(id, {duration: 400});
				
				if($('body').getAttribute('class'))
				{
				
					if(element.parentNode.id != $('body').getAttribute('class') )
					{
						window['slideNav_'+id].hide();
					}
					else if(element.parentNode.id == $('body').getAttribute('class'))
					{
						$(element).setStyle('background-image', 'url(images/contract.gif)');
					}
				}
				else
				{
					if(element.parentNode.id != $('body').getAttribute('className') )
					{
						window['slideNav_'+id].hide();
					}
					else if(element.parentNode.id == $('body').getAttribute('className'))
					{
						$(element).setStyle('background-image', 'url(images/contract.gif)');
					}
				}
						   
				element.addEvent('click', function(){
					expandNav(element.parentNode.id);
				});
				
			});
		}
		
	if($$('span.remove_product'))
	{
		$$('span.remove_product').each(function(element){
						
			element.addEvent('click', function(){
						removeFromFaves(element.id);					   
						});
										
		});
	}	
	
}); 

window.addEvent('resize', function() {
	if($$('div.overlay'))
		{
			var overlay = $$('div.overlay');
			overlay.each(function(element) {
				repositionOverlay(element);
			});
		}
});