function enable_remove_request ( remove_button ) {
	remove_button.click( function(){
		$(this).parent().remove();
		var last_number = $('.form-container').size() - 1;
		$('.form-container').removeClass('no-bottom');
		if ( last_number > 1 ) {
			$('.form-container:lt('+(last_number-1)+')').addClass('no-bottom');
		} else {
			$('.remove-request').remove();
		}
	});
}
	
function enable_add_another ( add_button ) {
	add_button.click( function(){
		$('.form-container').removeClass('no-bottom');
		var next_number = $('.form-container').size();
		
		if ( next_number > 1 ) {
			$('.form-container:lt('+(next_number - 1)+')').each( function(){
				var current_container = $(this);
				current_container.addClass('no-bottom');
				if ( current_container.children('.remove-request').size() == 0 ) {
					var this_number = current_container.find("input[id^='quantity']").attr('id');
					this_number = parseInt( this_number.replace(/quantity-/,'') );
					current_container.prepend('<img id="remove-request-'+this_number+'" class="remove-request" src="/images/remove-request.png" />');
					enable_remove_request( current_container.children('.remove-request') );
				}
			});
		}
		var last_form = $('.form-container:eq('+(next_number - 2)+')');
		var new_number = last_form.find("input[id^='quantity']").attr('id');
		new_number = parseInt( new_number.replace(/quantity-/,'') ) + 1;
		last_form.after('<div class="form-container"></div>');
		var new_form = last_form.next();
		new_form.append('<img id="remove-request-'+new_number+'" class="remove-request" src="/images/remove-request.png" />');
		new_form.append('<div class="left-form"></div>');
		new_form.children('.left-form').append('<label for="part-number-'+new_number+'">Lumex Part Number</label><div class="text-field"><input name="part-number[]" type="text" id="part-number-'+new_number+'" /></div><span class="hint">get a part number by navigating through our <a href="/mn/products">products section</a></span>');
		new_form.children('.left-form').append('<label for="target-cost-'+new_number+'">Target Cost</label><div class="text-field"><input name="target-cost[]" type="text" id="target-cost-'+new_number+'" /></div>');
		new_form.children('.left-form').append('<label for="quantity-'+new_number+'">Quantity</label><div class="text-field"><input name="quantity[]" type="text" id="quantity-'+new_number+'" /></div>');
		new_form.children('.left-form').append('<label for="estimated-usage-'+new_number+'">Estimated Annual Usage</label><div class="text-field"><input name="estimated-usage[]" type="text" id="estimated-usage-'+new_number+'" /></div>');
		new_form.append('<div class="right-form"></div>');
		new_form.children('.right-form').append('<label for="application-'+new_number+'">Application</label><div class="text-area"><textarea name="application[]" id="application-'+new_number+'"></textarea></div>');
		new_form.children('.right-form').append('<label for="target-date-'+new_number+'">Target Production Date</label><div class="text-field"><input name="target-date[]" type="text" id="target-date-'+new_number+'" /></div><span class="hint">ex. mm.dd.yy</span>');
		new_form.append('<p class="last"></p>');
		enable_remove_request( new_form.children('.remove-request') );
	});
}

$(document).ready(function(){
	$('body.request-sample-page a').click(function(e){
		if (!$('body').hasClass('contact-us'))
		{
		if ($('input[type="text"][name!="keywords"]').val()!='')
			if (!confirm("You are leaving this page. Any products you've selected by clicking 'Request Sample' elsewhere on the site will be retained. Any text you've entered will be lost."))
				e.preventDefault();
		}
	})
	
	$('#locate_country').change(function(e){
		//alert($('#locate_country option:selected').val());
		window.location.href = "?country="+$('#locate_country option:selected').val();
	})

	$('#search').submit(function(e){
		if ($('#search-box').val().length <= 1) {
			e.preventDefault();
			confirm("Please enter at least 2 characters for your search term.");
		}
	})

	$('#search>div').prepend('<ul><li class="keyword"><a rel="/mn/search" href="javascript:;">关键字</a></li><li class="stock"><a rel="/mn/stock-check" href="javascript:;">股票查询</a></li><li class="part"><a rel="/mn/cross-ref" href="javascript:;">公共检索</a></li></ul>');
	pathArray = window.location.pathname.split( '/' );
	segment = pathArray[1];
	switch(segment)
	{
	case 'search':
		$('#search>div>ul>li.keyword>a').toggleClass('active');
		$('#search').attr('action',$('#search>div>ul>li.keyword>a.').attr('rel'));
		break;
	case 'stock-check':
		$('#search>div>ul>li.stock>a').toggleClass('active');
		$('#search').attr('action',$('#search>div>ul>li.stock>a').attr('rel'));
		break;
	case 'cross-ref':
		$('#search>div>ul>li.part>a').toggleClass('active');
		$('#search').attr('action',$('#search>div>ul>li.part>a').attr('rel'));
		break;
	default:
		$('#search>div>ul>li.keyword>a').toggleClass('active');
	}

	$('#search ul a').click(function(){
		var clicked = this;
		$(this).parent().parent().children('li').children('a').removeClass('active');
		$(clicked).addClass('active');
		$('#search').attr('action',$(clicked).attr('rel'));
	});
	$('#search-box').focus( function(){
		if ( $(this).attr('value') == '输入关键字' ) {
			$(this).attr({'value':''});
		}
	});
	$('#search-box').blur( function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr({'value':'输入关键字'});
		}
	});
	$('#locate_zip').focus( function(){
		if ( $(this).attr('value') == 'enter zip' ) {
			$(this).attr({'value':''});
		}
	});
	$('#locate_zip').blur( function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr({'value':'enter zip'});
		}
	});
	$('#newsletter-name').focus( function(){
		if ( $(this).attr('value') == 'name' ) {
			$(this).attr({'value':''});
		}
	});
	$('#newsletter-name').blur( function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr({'value':'name'});
		}
	});
	$('#newsletter-email').focus( function(){
		if ( $(this).attr('value') == 'email address' ) {
			$(this).attr({'value':''});
		}
	});
	$('#location-zip').blur( function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr({'value':'enter zip'});
		}
	});
	$('#location-zip').focus( function(){
		if ( $(this).attr('value') == 'enter zip' ) {
			$(this).attr({'value':''});
		}
	});
	$('#newsletter-email').blur( function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr({'value':'email address'});
		}
	});
	$('#comment').focus( function(){
		if ( $(this).val() == 'comment' ) {
			$(this).val('');
		}
	});
	$('#comment').blur( function(){
		if ( $(this).val() == '' ) {
			$(this).val('comment');
		}
	});
	/*$('.large-text-area textarea').blur(function(){
		if ( $(this).text() == '' ) {
			$(this).text('enter your comment');
		}
	});*/
	$('.large-text-area textarea').focus(function(){
		if ( $(this).text() == 'enter your comment' ) {
			$(this).text('');
		}
	});
	$('#add-another').each( function(){
		enable_add_another( $(this) );
	});
	
	if ($.browser.msie && $.browser.version.substr(0,1)==6)
	{
		
	}
	else
	{
		$('#nav_sub li a').mouseover( function(){
			$prev = $(this).parent().prev('li:not(.here)');
			$prev.children('a').css('border-bottom','0px');
			$(this).css('border-bottom','0px');
		});
		$('#nav_sub li a').mouseout( function(){
			$prev = $(this).parent().prev('li:not(.here)');
			$prev.children('a').css('border-bottom','1px solid #e2dbc7');
			$(this).parent('li:not(.here)').children().css('border-bottom','1px solid #e2dbc7');
		});
		$('#main .section .tech-results li a').mouseover( function(){
			$prev = $(this).parent().prev('li');
			$prev.children('a').css('border-bottom','0px');
			$prev.children('a').css('margin-bottom','1px');
			$(this).parent().parent().next('div.results-link').css('padding-bottom','1px');
			$(this).parent().parent().next('div.results-link').css('border-bottom','0px');
		});
		$('#main .section .tech-results li a').mouseout( function(){
			$prev = $(this).parent().prev('li');
			$prev.children('a').css('border-bottom','1px solid #e2dbc7');
			$prev.children('a').css('margin-bottom','0px');
			$(this).parent().parent().next('div.results-link').css('padding-bottom','0px');
			$(this).parent().parent().next('div.results-link').css('border-bottom','1px solid #e2dbc7');
		});	
	}
	$('#nav_sub li.here a').parent().prev('li:not(.here)').children('a').css('border-bottom','0px');
});
