var blnDebug = false;

$(function() {

		function randomString(stringLength) {
				var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
				var returnString = '';
				for (var i = 0; i < stringLength; i++) {
						var postition = Math.floor(Math.random() * chars.length);
						returnString += chars.substring(postition, postition + 1);
				}
				return returnString;
		}

		function addErrorClass(objFormControl) {
				objFormControl.addClass("ui-state-error");          
		}
		
		function removeErrorClass(objFormControl) {
				objFormControl.removeClass("ui-state-error");
		}
		
		function checkLength(objFormControl, strName, strWarningMessage) {
                if (blnDebug){alert("checkLength start");}

                if (objFormControl.val().length == 0) {
                        addErrorClass(objFormControl);
                        strWarningMessage = strWarningMessage + strName + "~";
                }
                
                return strWarningMessage;
        }

        function processWarningMessage(strWarningMessage) {
                if (blnDebug){alert("processWarningMessage start");}

                if (strWarningMessage != "") {
                        strWarningMessage = strWarningMessage.substring(0, strWarningMessage.length - 1);
                        var intPos = strWarningMessage.lastIndexOf("~");
                        if (intPos != -1) {
                                strWarningMessage = strWarningMessage.slice(0, intPos) + " and " + strWarningMessage.slice(intPos + 1);
                                strWarningMessage = strWarningMessage.replace(/~/g,", ");
                        }
                }

                return strWarningMessage;
        }
		
        $("#dialog-form").dialog({
                autoOpen: false,
                height: 450,
                width: 350,
                modal: true
        });

        $("#submitrequest")
                .button()
                .click(function() {
                        if (blnDebug){alert("#submitrequest click start");}
						
                        var strWarningMessage = "";
						
						removeErrorClass($("#security"));
						removeErrorClass($("#name"));
						removeErrorClass($("#email"));
												
						if ($("#security-span").text() == jQuery.trim($("#security").val())) {
							strWarningMessage = checkLength($("#name"), "name", strWarningMessage);
							strWarningMessage = checkLength($("#email"), "email address", strWarningMessage);
							if (strWarningMessage != "") { strWarningMessage = "Please complete sections: " + processWarningMessage(strWarningMessage) + "."; }
						} else {
							strWarningMessage = "Please enter the security code.";
							addErrorClass($("#security"), true);
						}

                        if (strWarningMessage == "") {
                                $("#bookletrequestform").submit();
                        } else {
                                $("#bookletwarningmessage").text(strWarningMessage);
                                alert(strWarningMessage);
                        }

                });

        $("#cancelrequest")
                .button()
                .click(function() {
                        $("#dialog-form").dialog("close");
                });

        $("#createrequest")
                .click(function() {
						$("#security-span").text(randomString(6));
                        $("#dialog-form").dialog("open");
                });

});
