$(document).ready(function(){
    $("body").append(' \
	   <div id="dialog-download" class="popupform" title="download trial" style="display:none"> \
		    <div id="form"> \
			<p id="validateTips"> \
			</p> \
			<form> \
			    <fieldset> \
				<label for="name">name *</label> \
				<input type="text" name="name" id="name" class="ui-widget-content ui-corner-all" /> \
				<label for="company">company</label> \
				<input type="text" name="company" id="company" class="ui-widget-content ui-corner-all" /> \
				<label for="email">email address *</label> \
				<input type="text" name="email" id="email" value="" class="ui-widget-content ui-corner-all" /> \
				<label for="number">contact number</label> \
				<input type="text" name="number" id="number" value="" class="ui-widget-content ui-corner-all" /><span>please send me further news and updates from bemoko&#160;</span> \
				<input type="checkbox" name="info" id="info" checked="off" class="ui-widget-content ui-corner-all" /> \
			    </fieldset> \
			</form> \
		    </div> \
		    <div id="waiting"> \
			<div class="notice">please wait</div> \
			<div class="loading"><span/></div> \
		    </div> \
		    <div id="success"> \
			<div class="notice"> \
			    <p>Thank you, an email with download instructions has been sent to <span id="dynmessage1"/></p> \
			    <p>Please email <a href="mailto:info@bemoko.com?subject=download">info@bemoko.com</a> or <a href="http://www.bemoko.com/i/page/contact-us">contact us</a> if you do not receive it.</p> \
			</div> \
		    </div> \
		    <div id="failure"> \
			<div class="notice"> \
			    <p>We\'re sorry, something\'s prevented us sending you the email.</p> \
			    <p>Please email <a href="mailto:info@bemoko.com?subject=download">info@bemoko.com</a> or <a href="i/page/contact-us">contact us</a> and we\'ll provide you with a download link.</p> \
			</div> \
		    </div> \
		</div> \
	');

    	var name = $("#form #name"), email = $("#form #email"), company = $("#form #company"), number = $("#form #number"), info = $("#form #info");
        var allFields = $([]).add(name).add(email).add(company).add(number).add(info), tips = $("#validateTips");
        
        function updateTips(t){
            tips.text(t).effect("highlight", {}, 1500);
        }
        
        function checkLength(o, n, min, max){
            if (o.val().length > max || o.val().length < min) {
                o.addClass('ui-state-error');
                updateTips("we'd love to know who you are, please tell us your " + n + ".");
                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-download").dialog({
            bgiframe: true,
            autoOpen: false,
            height: "auto",
            minHeight:100,
            width: 360,
            position:"center",
            modal: true,
            open: function(){
                $("#dialog-download").children(":not(#form)").hide();
                $("#dialog-download").children("#form").show();
                $(this).dialog('option', 'position', 'center');//subsequent dialog opening is off-center, probably due to resizing.
                $(this).dialog('option', 'buttons', {
                    'email me the download link': function(){
                        var bValid = true;
                        allFields.removeClass('ui-state-error');
                        
                        bValid = bValid && checkLength(name, "name", 3, 16);
                        bValid = bValid && checkLength(email, "email", 6, 80);
                        
                        // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
                        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. ui@jquery.com");
                        
                        var dialogHandle=$(this);
                        //fade out form
                        $(this).find('#form').fadeOut("fast", function(){
                            //hide buttons
                            dialogHandle.dialog('option', 'buttons', null);
                            //fade in waiting
                            $(this).siblings('#waiting').fadeIn("fast", function(){
                                //do the ajax
                                var postString = 'name=' + name.val() + '&email=' + email.val() + '&number=' + number.val() + '&company=' + company.val() + '&info=' + info.is(':checked');
                                $.ajax({
                                    type: "POST",
                                    //url: "/live/webservices/senddownloademail",
                                    url: "/webservices/senddownloademail",
                                    data: postString,
                                    success: function(){
                                        dialogHandle.find('#waiting').fadeOut("fast", function(){
                                            dialogHandle.dialog('option', 'buttons', {
                                                'OK': function(){
                                                    dialogHandle.dialog('close');
                                                }
                                            });
                                            dialogHandle.find('#success span#dynmessage1').text(email.val());
                                            dialogHandle.find('#success').fadeIn("fast");
                                        });
                                    },
                                    error: function(XMLHttpRequest, textStatus, errorThrown){
                                        dialogHandle.find('#waiting').fadeOut("fast", function(){
                                            dialogHandle.dialog('option', 'buttons', {
                                                'OK': function(){
                                                    dialogHandle.dialog('close');
                                                }
                                            });
                                            dialogHandle.find('#failure').fadeIn("fast");
                                        });
                                    }
                                });
                            });
                        });
                        
                        
                    },
                    cancel: function(){
                        $(this).dialog('close');
                    }
                });
            },
            close: function(){
                allFields.val('').removeClass('ui-state-error');
            }
        });
        
        
        
        $('#button-download').click(function(event){
            event.preventDefault();
            $('#dialog-download').dialog('open');
        });
   });

