﻿var frmNewsLeter;


$(document).ready(function() {


    $("#ncSearchBtn").click(function() {
        $("#ncSearchForm").submit();
    });


    frmNewsLeter = $('#formnewsletter').validate({
        rules: {
            txtemail: { required: true, email: true }
        },
        messages: {
            txtemail: { required: 'Email is required.', email: 'Email is invalid.' }
        },
        submitHandler: function(form) {
        }
    });


    $('#newsletter_btn').click(function(event) {

        if (frmNewsLeter.form()) {
           
            var lastUrl = "../../handlers/newsletter.ashx?txtemail=" + $("#txtemail").val();


            try {


                $.getJSON(lastUrl, function(data) {
                    alert("You have been added to our newsletter list, thank you!");
                  

                });

            }
            catch (err) {
                alert(err.message);
            }



        }
        event.preventDefault();
    });

	$('.popImgLink').fancybox();
	

	
	if($('#tabContents').length>0){
		
		
		var sTourSpot='overview';
		
		try
		{
			if (document.location.hash.replace(/^#/, '') != '')
		{
			sTourSpot=document.location.hash.replace(/^#/, ''); 
		}
		}
			catch(err)
		{ }


		showTourTab('#tab_' + sTourSpot);
		
		$('#tabs ul.naviTabs li').click(function(event) {									 
			showTourTab($(this).find('a').attr('href'),false);
			event.preventDefault();
		});		

		$('#tour-nav-btns #btnNext').click(function(event) {
			var sShowTab = '';
			if($('#tab_more_contents').is(':visible')) {
				sShowTab='#tab_overview';
			} else if($('#tab_warehouse_contents').is(':visible')) {
				sShowTab='#tab_more';
			} else if($('#tab_production_contents').is(':visible')) {
				sShowTab='#tab_warehouse';
			} else if($('#tab_purchasing_contents').is(':visible')) {
				sShowTab='#tab_production';
			} else if($('#tab_sales_contents').is(':visible')) {
				sShowTab='#tab_purchasing';
			} else if($('#tab_overview_contents').is(':visible')) {
				sShowTab='#tab_sales';
			}
			
			showTourTab(sShowTab,true);
			event.preventDefault();
		});
		
		$('#tour-nav-btns #btnPrev').click(function(event) {
			var sShowTab = '';
			if($('#tab_more_contents').is(':visible')) {
				sShowTab='#tab_warehouse';
			} else if($('#tab_warehouse_contents').is(':visible')) {
				sShowTab='#tab_production';
			} else if($('#tab_production_contents').is(':visible')) {
				sShowTab='#tab_purchasing';
			} else if($('#tab_purchasing_contents').is(':visible')) {
				sShowTab='#tab_sales';
			} else if($('#tab_sales_contents').is(':visible')) {
				sShowTab='#tab_overview';
			} else if($('#tab_overview_contents').is(':visible')) {
				sShowTab='#tab_more';
			}
			
			showTourTab(sShowTab,true);
			event.preventDefault();
			
		});	
		
	}




	
		
		
	$('.inputDefVal').focus(function(event) {
		if ($(this).val()==$(this).attr('defVal')) {
			$(this).val('');
		}
	});

	$('.inputDefVal').blur(function(event) {
		if ($(this).val()=='') {
			$(this).val($(this).attr('defVal'));
		}							 
	});
	
	
	if($('#homeMidContainer').length>0) {
		$('#nextTest').click(function(event) {			  
			var el = $('#homeTest .custTestSlide:visible')[0]
			$(el).hide('slide');
			$(el).next().show('slide');
			$('#prevTest').show();
			if ($(el).next()[0] == $('#homeTest .custTestSlide:last')[0]) {
				$('#nextTest').hide();
			}					  
			event.preventDefault();
		});
	
		$('#prevTest').click(function(event) {
			var el = $('#homeTest .custTestSlide:visible')[0]
			$(el).hide('slide');
			$(el).prev().show('slide');
			$('#nextTest').show();
			if ($(el).prev()[0] == $('#homeTest .custTestSlide:first')[0]) {
				$('#prevTest').hide();
			}					  
			event.preventDefault();
		});
		
		
		$('#aoVideoTourImg').click(function(event) {
			$(this).hide();
			$('#aoVideoTourIframe').show();
			event.preventDefault();
		});
	}
	
	

});



function showTourTab(sTabName, scrollToTop) {
	
	$('#tabContents div.single_content').not(sTabName + '_contents').hide();
	$(sTabName + '_contents').show();
	$('.naviTabs li').not(sTabName).removeClass('active');
	$(sTabName).addClass('active');
	if(scrollToTop) {
		window.scrollTo(0, 0);
	}
	
}


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function querystring(key) {
    var re = new RegExp('(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi');
    var r = [], m;
    while ((m = re.exec(document.location.search)) != null) r.push(m[1]);
    return r;
}
