﻿

// After the page has loaded ...
$(document).ready(function()
{
	// Need to use this very awkward check to see what page we're on
	var isThisProductMigrationPage = $('h3 span:contains("Product Migration Search")');
	var isThisWhereToBuy = $('h3 span:contains("Where to Buy")');
	var isThisDatasheetsList = $('h3 span:contains("Datasheets List")');
	var isThisCompetitorXReference = $('h3 span:contains("Competitor Cross Reference")');
	var isThisProductFinderInstructions = $('h3 span:contains("Product Finder Instructions")');

	if(isThisProductMigrationPage.length > 0)
	{
		// If we're on the right page, put the search box in a bordered box
		// The search box is the third (eq counts from zero) ms-WPBody div;		
		$('div.ms-WPBody:eq(2)').each(function(i)
		{
			var contentToWrap = $(this);

			// Remove the second row of "buttons"
			contentToWrap.find('table:eq(1)').remove();

			createBorderedBox(contentToWrap);
			
			// Format the input field label
			var inputFieldLabel = contentToWrap.find('span:first');
			inputFieldLabel.css('font-size','13px');
			inputFieldLabel.css('float','left');
			inputFieldLabel.css('margin-right','12px');
			inputFieldLabel.css('line-height','24px');

			// Format the input field
			var inputField = contentToWrap.find('input');
			inputField.addClass('big-search-box');
			inputField.css('float','left');
			inputField.css('width','446px');
			
			// Add the search button to the box
			var buttonHTML = '<p><input type="image" style="height: 25px; width: 105px; border-width: 0px;" src="/Scripts/styles/images/button_search.gif" onclick="javascript:kwiz_ddf_SubmitFormPost();" /></p><p class="search_tips">Enter all or part of the product number. Use the asterisk * character (shift+8) for wildcard searches, then click "Search OPN."</p>';
			inputField.parent('td:first').append(buttonHTML);
		});

		// Since we're on the right page ...
		// ... put the search results in a bordered box
		// The search results are somehow in the 9th row
		// This is extremely ugly code and likely to break at some point, but I've
		//	tried everything I can think of and this is the only thing I could get to work
		createBorderedBox($("div.content table:eq(8)"));
	}
	
	// If we're on the Where To Buy page
	if(isThisWhereToBuy.length > 0)
	{
		// If we're on the right page, put the search box in a bordered box
		// The search box is the second (eq counts from zero) ms-WPBody div;		
		$('div.ms-WPBody:eq(1)').each(function(i)
		{
			var contentToWrap = $(this);

			// Remove all the small transparent images. The easiest way I could
			//	determine which ones I wanted was to find all the images that
			//	didn't have a class
			contentToWrap.find('img').each(function()
			{
				// I know there should be a cleaner way to do this but other things I tried failed
				if(!($(this).attr('class') == 'ComboBoxImage_Default'))
				{
					$(this).remove();
				}
			});
			
			// Add some spacing between the pull-downs
			contentToWrap.find('td').css('padding-right','4px');

			createBorderedBox(contentToWrap);
			
			// Add the search button to the box
			var buttonHTML = '<tr><td><a target="_self" onclick="javascript:kwiz_ddf_ClearFilter(\'WPQ4\');" href="javascript:void(0)" class="ms-addnew"><img style="margin-top: 8px; height: 25px; float: left; margin-right: 12px; width: 105px; border-width: 0px;" src="/SiteCollectionImages/button_clear.gif" /></a><input type="image" style="margin-top: 8px; height: 25px; width: 105px; border-width: 0px;" src="/Scripts/styles/images/button_search.gif" onclick="javascript:kwiz_ddf_SubmitFormPost();" /></td></tr>';
			contentToWrap.find('table:eq(5)').html(buttonHTML);
			
			// Need to add a header above the bordered box we just created
			$(this).closest('table.bordered-box').parent('td').prepend('<h1 class="main_heading" style="margin-bottom: 12px;">Find Sales Representatives and Distributors</h1>');									
		});

		// Since we're on the right page ...
		// ... put the search results in a bordered box
		// The search results are somehow in the 12th row
		// This is extremely ugly code and likely to break at some point, but I've
		//	tried everything I can think of and this is the only thing I could get to work
		createBorderedBox($("div.content table:eq(12)"));

		// Add a header to the search results box we just created
		$('div.content table:eq(12)').parent().prepend('<h1 class="main_heading" style="margin-bottom: 12px;">Sales Representatives and Distributors</h1>');
	}
	

// If we're on the Datasheets List page
	if(isThisDatasheetsList.length > 0)
	{
		// If we're on the right page, put the search box in a bordered box
		// The search box is the sencond (eq counts from zero) ms-WPBody div;		
		$('div.ms-WPBody:eq(1)').each(function(i)
		{
			var contentToWrap = $(this);

			// Remove all the small transparent images. The easiest way I could
			//	determine which ones I wanted was to find all the images that
			//	didn't have a class
			contentToWrap.find('img').each(function()
			{
				// I know there should be a cleaner way to do this but other things I tried failed
				if(!($(this).attr('class') == 'ComboBoxImage_Default'))
				{
					$(this).remove();
				}
			});
			
			// Add some spacing between the pull-downs
			contentToWrap.find('td').css('padding-right','4px');

			createBorderedBox(contentToWrap);
			
			// Add the search button to the box
			var buttonHTML = '<tr><td><a target="_self" onclick="javascript:kwiz_ddf_ClearFilter(\'WPQ6\');" href="javascript:void(0)" class="ms-addnew"><img style="margin-top: 8px; height: 25px; float: left; margin-right: 12px; width: 105px; border-width: 0px;" src="/SiteCollectionImages/button_clear.gif" /></a><input type="image" style="margin-top: 8px; height: 25px; width: 105px; border-width: 0px;" src="/Scripts/styles/images/button_search.gif" onclick="javascript:kwiz_ddf_SubmitFormPost();" /></td></tr>';
			contentToWrap.find('table:eq(6)').html(buttonHTML);
			});

		
	}
	
	
	
// If we're on the Product Finder Page
if(isThisProductFinderInstructions.length > 0)
	{
		// If we're on the right page, put the search box in a bordered box
		// The search box is the second (eq counts from zero) ms-WPBody div;		
		$('div.ms-WPBody:eq(2)').each(function(i)
		{
			var contentToWrap = $(this);

			// Remove all the small transparent images. The easiest way I could
			//	determine which ones I wanted was to find all the images that
			//	didn't have a class
			contentToWrap.find('img').each(function()
			{
				// I know there should be a cleaner way to do this but other things I tried failed
				if(!($(this).attr('class') == 'ComboBoxImage_Default'))
				{
					$(this).remove();
				}
			});
			
			// Add some spacing between the pull-downs
			contentToWrap.find('td').css('padding-right','4px');

			createBorderedBox(contentToWrap);
			
			// Add the search button to the box
			var buttonHTML = '<tr><td><a target="_self" onclick="javascript:kwiz_ddf_ClearFilter(\'WPQ4\');" href="javascript:void(0)" class="ms-addnew"><img style="margin-top: 8px; height: 25px; float: left; margin-right: 12px; width: 105px; border-width: 0px;" src="/SiteCollectionImages/button_clear.gif" /></a><input type="image" style="margin-top: 8px; height: 25px; width: 105px; border-width: 0px;" src="/Scripts/styles/images/button_search.gif" onclick="javascript:kwiz_ddf_SubmitFormPost();" /></td></tr>';
			contentToWrap.find('table:eq(7)').html(buttonHTML);
			
			// Need to add a header above the bordered box we just created
			//$(this).closest('table.bordered-box').parent('td').prepend('<h1 class="main_heading" style="margin-bottom: 12px;">Find Sales Representatives and Distributors</h1>');									
		});

		// Since we're on the right page ...
		// ... put the search results in a bordered box
		// The search results are somehow in the 12th row
		// This is extremely ugly code and likely to break at some point, but I've
		//	tried everything I can think of and this is the only thing I could get to work
		createBorderedBox($("div.content table:eq(6)"));

	}



// If we're on the Competitor Cross Reference page
	if(isThisCompetitorXReference.length > 0)
	{
		// If we're on the right page, put the search box in a bordered box
		// The search box is the first (eq counts from zero) ms-WPBody div;		
		$('div.ms-WPBody:eq(1)').each(function(i)
		{
			var contentToWrap = $(this);

			// Remove all the small transparent images. The easiest way I could
			//	determine which ones I wanted was to find all the images that
			//	didn't have a class
			contentToWrap.find('img').each(function()
			{
				// I know there should be a cleaner way to do this but other things I tried failed
				if(!($(this).attr('class') == 'ComboBoxImage_Default'))
				{
					$(this).remove();
				}
			});
			
			// Add some spacing between the pull-downs
			contentToWrap.find('td').css('padding-right','4px');

			createBorderedBox(contentToWrap);
			
			// Add the search button to the box
			var buttonHTML = '<tr><td><a target="_self" onclick="javascript:kwiz_ddf_ClearFilter(\'WPQ5\');" href="javascript:void(0)" class="ms-addnew"><img style="margin-top: 8px; height: 25px; float: left; margin-right: 12px; width: 105px; border-width: 0px;" src="/SiteCollectionImages/button_clear.gif" /></a><input type="image" style="margin-top: 8px; height: 25px; width: 105px; border-width: 0px;" src="/Scripts/styles/images/button_search.gif" onclick="javascript:kwiz_ddf_SubmitFormPost();" /></td></tr>';
			contentToWrap.find('table:eq(5)').html(buttonHTML);
			});

		
	}


})

// createBorderedBox puts a table around any content passed to it.
//	The table's classes will cause it to be formatted as a bordered
//	box with rounded corners. The box will expand as the content does
function createBorderedBox(_contentToWrap)
{
		var table = $('<table width="100%" cellspacing="0" cellpadding="0" border="0" class="bordered-box">	\
						<tr valign="top">	\
							<td class="ul"></td>	\
							<td class="top"></td>	\
							<td class="ur"></td>	\
						</tr>	\
						<tr valign="top">	\
							<td class="l"></td>	\
							<td class="middle cross-ref">	\
							</td>	\
							<td class="r"></td>	\
						</tr>	\
						<tr valign="top">	\
							<td class="ll"></td>	\
							<td class="bottom"></td>	\
							<td class="lr"></td>	\
						</tr>	\
					</table>');

		table.insertBefore(_contentToWrap);
		table.find('td.middle').append(_contentToWrap);
}
