
var validation = {
    addCall : function () {
        $('form').submit(validation.listener);
        validation.addListener();
    },
    addListener : function () {
        lmbValidator.addErrorListener("lmbValidationErrorDisplayer", lmbValidationErrorDisplayer.showFirstError.render);
    },
    listener : function (event) {
        if (typeof event == "undefined") {
            event = window.event;
        }

        if (!lmbValidator.validateDirectoryForm(document.forms[0])) {
            utility.suppressEvent(event);
            return false;
        }
    }
}

var pageInit = {
    attachAutoTab : function () {
        $(':input.areaCode, :input.phoneExchange, :input.prefix').each(
            function() {
                $(this).keyup(utility.autoTab);
            });
    }
}

function disableAllSubmitButtons() {
  var allInputs = document.getElementsByTagName("input");
  for(var i = 0; i < allInputs.length; i++) {
    if(allInputs.item(i).type.toLowerCase() == "submit") {
      lmbUtil.addEventListener(allInputs.item(i), "click", function() {
        this.disabled = true;
      });
    }
  }
}

lmbValidationErrorDisplayer.showFirstError = {
    radioAndCheckboxDecoratorClass: 'errorBorderCheckbox',
    decoratorClass: 'errorBorder',
    messageClass: 'errorMessage',
    render: function(error){
        if (error.first){
            var selectedErrorStyle;
            if((error.errorField.type == "checkbox") || (error.errorField.type == "radio")){
                selectedErrorStyle =  lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass;
            } else {
                selectedErrorStyle =  lmbValidationErrorDisplayer.showFirstError.decoratorClass;
            }

            /*this is a hack around the limitation of the NPA NXX validator.  changing the NPA NXX message
            * will break this hack.*/
            if(error.errorMessage == "Please enter a valid Daytime Phone.") {
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phoneAreaDay'), selectedErrorStyle);
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phonePrefixDay'), selectedErrorStyle);
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phoneStationDay'), selectedErrorStyle);
                    // Hack to focus on UOP/Devry daytime phone field
                	try {
                		$('input#daytime_phone_feed').focus(); 
                		}
                	catch(ex) {
                		}
            } else if (error.errorMessage == "Please enter a valid Evening Phone.") {
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phoneAreaEvening'), selectedErrorStyle);
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phonePrefixEvening'), selectedErrorStyle);
                lmbValidationErrorDisplayer.showFirstError.decorate(document.getElementById('phoneStationEvening'), selectedErrorStyle);
                    // Hack to focus on UOP/Devry eveningtime phone field
                	try {
                		$('input#eveningtime_phone_feed').focus(); 
                		}
                	catch(ex) {
                		}
            } else {
                lmbValidationErrorDisplayer.showFirstError.decorate(error.errorField, selectedErrorStyle);
            }

            lmbValidationErrorDisplayer.showFirstError.writeErrorMessage(error.errorField, error.errorMessage, lmbValidationErrorDisplayer.showFirstError.messageClass);

            if(error.errorMessage == "Please enter a valid Daytime Phone.") {
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phoneAreaDay'));
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phonePrefixDay'));
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phoneStationDay'));
            } else if (error.errorMessage == "Please enter a valid Evening Phone.") {
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phoneAreaEvening'));
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phonePrefixEvening'));
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(document.getElementById('phoneStationEvening'));
            } else {
                lmbValidationErrorDisplayer.showFirstError.setStyleAndErrorReset(error.errorField);
            }


            lmbValidationErrorDisplayer.setFocus(error.errorFocusField);
            lmbValidationErrorDisplayer.enableSubmitButton();
        }
    },
    decorate: function(field, styleClass){
        $(field).addClass(styleClass);
    },
    writeErrorMessage: function(field, message, styleClass){
        switch(field.type){
            case "password":
            case "textarea":
                $('p#errorMessage').remove();
                $(field).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                break;
            case "select-one":
            case "select-multiple":
                $('p#errorMessage').remove();
                $(field).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                break;
            case "text":
            case "radio":
                $('p#errorMessage').remove();
                var pelement = $(field)[0].parentNode;
                if(pelement
                        && pelement.getElementsByTagName("input")
                        && pelement.getElementsByTagName("input").length > 1) {
                    $(pelement).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                } else {
                    $(field).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                }
                break;
            case "checkbox":
                $('p#errorMessage').remove();
                if($(field)[0].parentNode) {
                    $($(field)[0].parentNode).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                } else {
                    $(field).after('<p id="errorMessage" class="'+styleClass+'">'+message+'</p>');
                }
                break;
        }
    },
    setStyleAndErrorReset: function(field){
        switch(field.type){
            case "text":
            case "password":
            case "textarea":

                if(field.id == "phoneAreaDay" || field.id == "phonePrefixDay" || field.id == "phoneStationDay" ||
                        field.id == "phoneAreaEvening" || field.id == "phonePrefixEvening" || field.id == "phoneStationEvening") {
                    $($(field)[0].parentNode).children('input').each(function() {
                        $(this).click(function(){
                            $('input[type="text"]').each(function() {
                                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.decoratorClass);
                                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass);
                            });

                            $('p#errorMessage').remove();
                        });
                    });
                } else {
                    $(field).change(function(){
                        $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.decoratorClass);
                        $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass);
                        $('p#errorMessage').remove();
                    });
                }

                break;
            case "select-one":
            case "select-multiple":
                $(field).change(function(){
                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.decoratorClass);
                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass);
                    $('p#errorMessage').remove();
                });
                break;
            case "radio":
                $($(field)[0].parentNode).children('input').each(function() {
                    $(this).click(function(){
                        $('input[type="radio"]').each(function() {
                                $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.decoratorClass);
                                $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass);
                        });

                        $('p#errorMessage').remove();
                    });
                });

                break;
            case "checkbox":
                $(field).click(function(){
                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.decoratorClass);
                    $(this).removeClass(lmbValidationErrorDisplayer.showFirstError.radioAndCheckboxDecoratorClass);
                    $('p#errorMessage').remove();
                });
                break;
        }
    }
};
var browserOrigin = function(){
	var browser;
	$.each($.browser, function(i, val) {
		if(val==true){
			browser=i;
		}
	});
	return browser;
};

$(function() {
    validation.addCall();
    pageInit.attachAutoTab();
    var browser=browserOrigin();
    if(browser!='safari'){
    	disableAllSubmitButtons();
    }
});
