$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog").dialog("destroy");
	
	var fname = $("#fname"),lname = $("#lname"),company = $("#company"),city = $("#city"),state = $("#state"),phone = $("#phone");
	var allFields = $([]).add(fname).add(lname).add(company).add(city).add(state).add(phone);
	var tips = $(".validateTips");
	
	// This mask comes from jquery.maskedinput-1.2.2.min.js which is loaded in the header file
	// Source site: http://digitalbush.com/projects/masked-input-plugin/
	$("#phone").mask("(999) 999-9999");
	
	
	function updateTips(t) 
	{
		tips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}
	
	function checkLength(o,n,min,max) 
	{
		if ( o.val().length > max || o.val().length < min ) 
		{
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}
	function validateState(o,n){
		//if it's NOT valid
		if(o.val() == -1){
			o.addClass("ui-state-error");
			updateTips("Please select a state/provence");
			return false;
		}
		else{
			return true;
		}
	}
	
	function checkRegexp(o,regexp,n) 
	{
		if ( !( regexp.test( o.val() ) ) ) 
		{
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	$("#dialog-form").dialog({
		autoOpen: false,
		height: 440,
		width: 350,
		modal: true,
		resizable: false,
		buttons: {
			'Download': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');
	
				bValid = bValid && checkLength(fname,"First Name",3,25);
				bValid = bValid && checkLength(lname,"Last Name",3,25);
				bValid = bValid && checkLength(company,"Company",3,75);
				bValid = bValid && checkLength(city,"City",3,25);
				bValid = bValid && validateState(state,"State");
				
				bValid = bValid && checkRegexp(phone,/^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/,"Telephone must be in the format of (999) 999-9999.");
				
				if (bValid)
				{
					postUserData();
					$(this).dialog('close');
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	}); 
});
	
function showDialog()
{
	if($("#returnUser").val() == "FALSE" && $("#userId").val() == 0)
	{
		$('#dialog-form').dialog('open');
	} else {
		postUserData();
	}
}

function postUserData()
{		
	$.post("/catalog/submitCadSignupForm",
			{'fname':$("#fname").val(),
			 'lname':$("#lname").val(),
			 'company':$("#company").val(),
			 'city':$("#city").val(),
			 'state':$("#state").val(),
			 'phone':$("#phone").val(),
			 'cadPartNumber':$("#cadPartNumber").val(),
			 'userId':$("#userId").val()},
			function(data)
			{
				if (data != "FALSE")
				{
					// Download the CAD file after insertion of user information
					window.open(data, "Download");								
				} else {
					$("#message").html("<strong>Unable to download " + $("#partNumber").val() + "</strong><ul>There was an error attempting to download the requested drawings.  Please contact Lyn-Tron's IT staff if the problem persists</ul>");
					$("#message").slideDown('slow');
				}
			}
		);
}
