 $(document).ready(function() {
	   
	     var options = {
                 success:  showResponse,  // post-submit callback
                 url:  "contact.php"
                },
			  name = $("#name"),
			  email = $("#email"),
			  city = $("#city"),
			  state = $("#state"),
			  country = $("#country"),
			  allFields = $([]).add(name).add(email).add(city).add(state).add(country),
			  tips = $("#validateTips");
				
	   //for loading the form dialog box
	   $("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 500,
			modal: true
		});
		
		$('#download').click(function() {
			$('#dialog').dialog('open');
		})
		
        $('input#submit').click(function() {
				       //validate
					   var bValid = true;
					   allFields.removeClass('ui-state-error');
					   
					   bValid = bValid && checkLength(name,"Name");
					   bValid = bValid && checkLength(email,"Email");
                       bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. email@something.com");
                          bValid = bValid && checkLength(city,"City");
						  bValid = bValid && checkLength(state,"State");
						  bValid = bValid && checkLength(country,"Country");
					   

					   if(bValid) {
				       $('#downloadForm').ajaxSubmit(options);
		                    $('#downloadContainer').empty();
		                    $('#downloadContainer').html("<img src='img/loading-animation.GIF' alt='loading' width='42' height='42'/>");
					   }
	               // !!! Important !!!
	               // always return false to prevent standard browser submit and page navigation
				   return false;
			});
			
	      $('#download').hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
		
		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n) {

			if ( o.val().length ==0 ) {
				o.addClass('ui-state-error');
				updateTips(  n + " is required.");
				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;
			}

		}


		
	});//EOF
	
	 function showResponse(responseText,statusText)  {
        // $('#downloadContainer').empty();
         $('#downloadContainer').html(responseText);
		 setTimeout( "timedRedirect()", 3000 );
	  }
	

	function timedRedirect()
	{
	    location.href="docs/AUTISM PARADIGM SHIFT- 100101.pdf";
	}

	