/**************************************************************************************** 
 * Function Name :- redirect_search()
 * Description   :- Turns off all buttons in html class
 * parameter     :- none
 * Return        :- void
 *****************************************************************************************/
redirect_search = function()
{
	/*var webroot = '/';
	var search_collection = 'all';
	var search_size = 'all';
	var search_price = 'all';
	var search_style = 'precast';
	*/
	//if()
	
	url = webroot+'fireplace-mantels/:style/:collection/size-:size/price-:price';
	price      = 'all';
	size       = 'all';
	style      = 'all';
	collection = 'all';
	availablewallspace = 'all';
	fireboxsize = 'all';
	
	// Find All Search Buttons
	selected_images = document.getElementsByClassName("search_btn");
	
	// Loop through buttons to see if they are on. Assign buttons that are on, to build new url.
	for(i=0;i<selected_images.length;i++)
	{
		if(selected_images[i].src.match(/_on/))
		{
			class_name = selected_images[i].className.replace("search_btn ","").replace("_btn","");
			value      = selected_images[i].src.split("search_wizard/")[1].replace(".jpg","").replace("_on","");
			if(value.match(/price/))
			{
				price = value.replace("price_","");
			}
			else if(value.match(/size/))
			{
				size = value.replace("size_","");
			}
			else if(value.match(/style/))
			{
				style = value.replace("style_","");
			}
		}
	}
	
	try
	{
		// If search_collection set in default layout then override collection value from all to real value.
		collection = search_collection;
	}
	catch(error){ /* Do Nothing */ }
	
	try
	{
		// This checks to see if the user has selected a different style ie(going from precast to wood).  This resets the collection parameter to all
		if(style!=search_style)
		{
			collection = 'all';
		}
	}
	catch(error){ /* Do Nothing */ }
	
	if(collection=='all')
	{
		url.replace("/:style","");
	}

	
	// Build New URL to redirect to
	url = url.replace(":style",style).replace(":size",size).replace(":price",price).replace(":collection",collection);
	
	if(style!='all')
	{
		url = url.replace('style-','');
	}
	
	url = url.replace("/size-all","").replace("/price-all","");
	url = url.replace("size-small","IISML");
	url = url.replace("all","");
	url = url.replace("IISML","size-small");
	url = url.replace(/\/\//g,"/");
	url = url.replace(/\/all/g,"");
	
	// Need to Put Url in
	/*var search_collection = 'all';
	var search_size = 'all';
	var search_price = 'all';
	var search_style = 'wood';
	*/
	
	if(collection!="all")
	{
		//alert("collection");
		if(url.match(search_style))
		{
			//alert("match 1");
			//url.replace("/"+search_style+"/", "/"+search_style+"/"+search_collection+"/")
		}
		else if(!url.match(search_style))
		{
			//alert("match 2");
			//url.replace("/fireplace-mantels/", "/fireplace-mantels/"+search_collection+"/")
		}
	}
	
	temp_url = url;
	last_character = temp_url[temp_url.length-1];
	if(last_character!='/')
	{
		url = url + '/';
	}
	
	if(size=='custom')
	{
		try
		{
			if(document.getElementById('available_wall_space').value=='' || document.getElementById('available_wall_space').value=='Avail. Wallspace')
			{
				alert("Invalid Available Wallspace");
				document.getElementById('available_wall_space').focus();
				return false;
			}
			
			
			if(document.getElementById('firebox_size').value=='' || document.getElementById('firebox_size').value=='Firebox Size')
			{
				alert("Invalid Firebox Size");
				document.getElementById('firebox_size').focus();
				return false;
			}
			url += 'wallspace-'+document.getElementById('available_wall_space').value+'/';
			url += 'firebox-'+document.getElementById('firebox_size').value+'/';
		}
		catch(e){}
	}
	
	
	
	// Only Redirect if user is not on current URL already
	if(url!=window.location.href)
	{
		window.location = url;
	}
}

function clearText(thefield, default_text)
{
	if (default_text==thefield.value)
	{
		thefield.value = ""
	}
	else if (thefield.value=='')
	{
		thefield.value = default_text;
	}

}

function isNumberKey(evt)
 {
 	try
 	{
 	var charCode = (evt.which) ? evt.which : event.keyCode
 	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode !== 46)
 	   return false;

 	return true;
 	}catch(e){}
 }
     

submit_custom = function()
{	
	if(document.getElementById('available_wall_space').value=='' || document.getElementById('available_wall_space').value=='Avail. Wallspace')
	{
		alert("Invalid Available Wallspace");
		document.getElementById('available_wall_space').focus();
		return false;
	}
	
	
	if(document.getElementById('firebox_size').value=='' || document.getElementById('firebox_size').value=='Firebox Size')
	{
		alert("Invalid Firebox Size");
		document.getElementById('firebox_size').focus();
		return false;
	} 
	
	// Continue
	redirect_search();
}


/**************************************************************************************** 
 * Function Name :- unselect_images(field)
 * Description   :- Turns off all buttons in class
 * parameter     :- field - // Pass in the id of button without "_btn". e.g. "price_high"
 * Return        :- void
 *****************************************************************************************/
unselect_images = function(field)
{
	// Determine Which type of button we are working with (style, size, price).
	prefix = field.split("_")[0];
	
	// Find Buttons matching the type.
	selected_images = document.getElementsByClassName(prefix+"_btn");
	
	// Loop through buttons and turn them all off.
	for(i=0;i<selected_images.length;i++)
	{
		if(selected_images[i].src.match(/_on/))
		{
			selected_images[i].src = selected_images[i].src.replace(/_on/,"");
		}
	}
	
}


/**************************************************************************************** 
 * Function Name :- toggle_search(field)
 * Description   :- Toggles on/off state of buttons
 * parameter     :- field - // Pass in the id of button without "_btn". e.g. "price_high"
 * Return        :- void
 *****************************************************************************************/
toggle_search = function(field)
{
	
	btn_element = document.getElementById(field+"_btn");
	
	if(btn_element.src.match(/_on/))
	{
		// Turn off currently clicked image.
		btn_element.src = btn_element.src.replace(/_on/,"");
	}
	else
	{
		// Turn off all images for this type.
		unselect_images(field); 
		
		// Turn On currently clicked image.
		btn_element.src = btn_element.src.replace(/.jpg/,"_on.jpg");
	}
	
	// Perform URL Redirection for new search settings.
	redirect_search();
}


/**************************************************************************************** 
 * Function Name :- initialize_search_wizard()
 * Description   :- Initializes all onclick events for search_wizard buttons
 * parameter     :- none
 * Return        :- void
 *****************************************************************************************/
initialize_search_wizard = function()
{
	try
	{
	// Define HTML Button Objects
	var price_high = $("price_high_btn");
	var price_low = $("price_low_btn");
	var price_mid = $("price_mid_btn");
	var size_large = $("size_large_btn");
	var size_medium = $("size_medium_btn");
	var size_small = $("size_small_btn");
	var size_custom = $("size_custom_btn");
	var style_precast = $("style_precast_btn");
	var style_wood = $("style_wood_btn");
	
	// Define All Onclick Events for Buttons
	price_high.onclick = function()
	{
		toggle_search("price_high");
	}
	price_low.onclick = function()
	{
		toggle_search("price_low");
	}
	
	price_mid.onclick = function()
	{
		toggle_search("price_mid");
	}
	
	size_large.onclick = function()
	{
		toggle_search("size_large");
	}
	
	size_medium.onclick = function()
	{
		toggle_search("size_medium");
	}
	
	size_small.onclick = function()
	{
		toggle_search("size_small");
	}
	
	size_custom.onclick = function()
	{
		toggle_search("size_custom");
	}
	
	style_precast.onclick = function()
	{
		toggle_search("style_precast");
	}
	
	style_wood.onclick = function()
	{
		toggle_search("style_wood");
	}
	
	}
	catch(error)
	{
		// do nothing
	}
}