$(document).ready(function() {
	if (navigator.appVersion.indexOf('MSIE') > -1)
	{
		// Ajax messages
		$('#msg').slideDown(300);
		
		$('#closeMsg').click(function() {
			$('#msg').slideToggle(400);
		});
	}
	else
	{
		// Ajax messages
		$('#msg').slideDown('slow');
		
		$('#closeMsg').click(function() {
			$('#msg').slideToggle('slow');
		});	
	}
	
	
	// Form submits
	$('#loginButton').click(function() {
		$('#loginButton').attr('disabled','disabled');
		$('#login').submit();
	});
	$('#resetpwSubmit').click(function() {
		$('#resetpwSubmit').attr('disabled','disabled');
		$('#resetpw').submit();
	});
	$('#profileSubmit').click(function() {
		$('#profileSubmit').attr('disabled','disabled');
		$('#profile').submit();
	});
	$('#passwordSubmit').click(function() {
		$('#passwordSubmit').attr('disabled','disabled');
		$('#password').submit();
	});	
	
	// Disable submit buttons after 1st click
	$('form.disableOnSumbit').submit(function(){
	    // On submit disable its submit button
	    $('input[type=submit]').attr('disabled', 'disabled');
	});	
	
	// Cancel Registration
	$('#cancelRegistration').click(function() {
		if (!confirm('Are you sure you would like to delete your registration?  This cannot be undone.'))
			return false;
		else
			return true;
	});
	
	// Add tracking to download links and links to other websites
	$('.tracking').click(function() {
		var orig_page = location.href.replace(new RegExp( "http://plxtech.com/", "i" ), "").replace(new RegExp( "http://www.plxtech.com/", "i" ), "").replace(new RegExp( "http://www1.plxtech.com/", "i" ), "").replace(new RegExp( "http://www2.plxtech.com/", "i" ), "").replace(new RegExp( "http://www3.plxtech.com/", "i" ), "").replace(new RegExp( "http://plx.local/", "i" ), "");
		
		if ($(this).attr('rel') != null && $(this).attr('rel') != "")
		{			
			urchinTracker(orig_page + " - " + $(this).attr('rel'));
		}
		else if ($(this).attr('title') != null && $(this).attr('title') != "")
		{
			urchinTracker(orig_page + " - " + $(this).attr('title'));
		}
		else
		{
			urchinTracker(orig_page + " - " + $(this).attr('href'));
		}
	});	
	
	// Sample Request form fields
	$('#newShipping').click(function() {
		if ($(this).attr('checked') == true)
		{
			$('#SampleRequestShippingContactName').removeAttr('disabled');
			$('#SampleRequestShippingAddress1').removeAttr('disabled');
			$('#SampleRequestShippingAddress2').removeAttr('disabled');
			$('#SampleRequestShippingCity').removeAttr('disabled');
			$('#StateCode').removeAttr('disabled');
			$('#CountryCode').removeAttr('disabled');
			$('#SampleRequestShippingZip').removeAttr('disabled');
		}
		else
		{
			$('#SampleRequestShippingContactName').attr('disabled', 'disabled');
			$('#SampleRequestShippingAddress1').attr('disabled', 'disabled');
			$('#SampleRequestShippingAddress2').attr('disabled', 'disabled');
			$('#SampleRequestShippingCity').attr('disabled', 'disabled');
			$('#StateCode').attr('disabled', 'disabled');
			$('#CountryCode').attr('disabled', 'disabled');
			$('#SampleRequestShippingZip').attr('disabled', 'disabled');
		}
	});
	
	
	// Delete items from SR list
	$('.srDel').click(function() {
		if (confirm('Are you sure you\'d like to remove this item?'))
		{
			var part = $(this).attr('id').replace('del_', '');
			
			$.get('/support/removeSampleRequest/' + part, function(data) {
				if (data == 'Success')
				{
					window.location.reload();
				}
				else
				{
					alert('Sorry, we are having technical difficulties.  Please try again later.');
				}
			});
		}
	});
	
});

function enableSRStuff()
{
	// Accordion
	$('#accordion').accordion({autoHeight: false, collapsible: true, active: false});
	$('#accordion').accordion('activate', 0);
	
	// Datepickers
	$("#datepicker_samples").datepicker({ dateFormat: 'mm-dd-yy', changeMonth: true, changeYear: true});
	$("#datepicker_prototype").datepicker({ dateFormat: 'mm-dd-yy', changeMonth: true, changeYear: true});
	$("#datepicker_production").datepicker({ dateFormat: 'mm-dd-yy', changeMonth: true, changeYear: true});
}