//Create MIL namespace, if not already created
if (typeof(MIL)=="undefined") var MIL = {};
MIL.initialize = function(){
    document.getElementById("comments").onfocus = function(){
        if (this.value == MIL.localization.comments_default) {
            this.value = "";
        };
    };
    MIL.localization.initialize();
    MIL.contact.initialize();
    
    document.getElementById("formContactDetails").onsubmit = MIL.contact.validateContactLookup;
    
    document.getElementById("reset").onclick = function(){
        MIL.contact.resetUserValidation();
        document.getElementById("formUserDetails").reset();
        return false;
    }
    
    document.getElementById("formUserDetails").onsubmit = MIL.contact.validateUserDetails;
    
    //Set mouse events on rollover images
    //Country Submit
    document.getElementById("submitCountry").onmouseover = function(){
      MIL.replaceImg(this, MIL.localization.imgCountrySubmitOver)
    };
    document.getElementById("submitCountry").onmouseout = function(){
      MIL.replaceImg(this, MIL.localization.imgCountrySubmitOut)
    };
    //Directions (first check direction image element is available)
    if (document.getElementById("directions") != null) {
      document.getElementById("directions").onmouseover = function(){
        MIL.replaceImg(this, MIL.localization.imgDirectionsOver)
      };
      document.getElementById("directions").onmouseout = function(){
        MIL.replaceImg(this, MIL.localization.imgDirectionsOut)
      };
    }
    
    //Reset
    document.getElementById("resetButton").onmouseover = function(){
      MIL.replaceImg(this, MIL.localization.imgResetOver)
    };
    document.getElementById("resetButton").onmouseout = function(){
      MIL.replaceImg(this, MIL.localization.imgResetOut)
    };
    //User Details Submit
    document.getElementById("submitUserDetails").onmouseover = function(){
      MIL.replaceImg(this, MIL.localization.imgSubmitUserOver)
    };
    document.getElementById("submitUserDetails").onmouseout = function(){
      MIL.replaceImg(this, MIL.localization.imgSubmitUserOut)
    };
};
MIL.contact = {}; 
MIL.contact.initialize = function(){
  //define input page elements
  MIL.contact.requireLeft = document.getElementById("required_left");
  MIL.contact.requireRight = document.getElementById("required_right");
  MIL.contact.firstname = document.getElementById("firstname");
  MIL.contact.lastname = document.getElementById("lastname");
  MIL.contact.email = document.getElementById("email");
  MIL.contact.telephone = document.getElementById("telephone");
  MIL.contact.company = document.getElementById("company");
  MIL.contact.country = document.getElementById("usrCountry");
  MIL.contact.productArea = document.getElementById("contactProductArea");
  MIL.contact.reason = document.getElementById("contactReason");
  MIL.contact.product = document.getElementById("usrProduct");
  MIL.contact.comments = document.getElementById("comments");
  MIL.contact.distCountry = document.getElementById("country");
  MIL.contact.distService = document.getElementById("product");
};
MIL.contact.addError = function(errorElement, errorText){
    if (errorElement.innerHTML == "") {
        errorElement.innerHTML = errorText;
        errorElement.style.display = "block";
    }
    else {
        errorElement.innerHTML = errorElement.innerHTML + "<br>" + errorText;
    }
};
MIL.contact.resetUserValidation = function(){
    MIL.contact.requireRight.innerHTML = "";
    MIL.contact.requireRight.style.display = "none"
    MIL.contact.firstname.style.border = "1px solid black";
    MIL.contact.lastname.style.border = "1px solid black";
    MIL.contact.company.style.border = "1px solid black";
    MIL.contact.email.style.border = "1px solid black";
    MIL.contact.telephone.style.border = "1px solid black";
    MIL.contact.country.style.border = "1px solid black";
    MIL.contact.reason.style.border = "1px solid black";
    MIL.contact.productArea.style.border = "1px solid black";
    MIL.contact.comments.style.border = "1px solid black";
    MIL.contact.product.style.border = "1px solid black";
}
MIL.contact.validateUserDetails = function(){
    var valid = true;
    //clear any existing error messages
    MIL.contact.resetUserValidation();
    
    //validate First Name
    if (MIL.contact.firstname.value == "") {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_firstname);
        MIL.contact.firstname.style.border = "1px solid red";
        valid = false
    };
    //validate Last Name
    if (MIL.contact.lastname.value == "") {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_lastname);
        MIL.contact.lastname.style.border = "1px solid red";
        valid = false
    };
    //validate Company
    if (MIL.contact.company.value == "") {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_company);
        MIL.contact.company.style.border = "1px solid red";
        valid = false
    };
    //validate Email
    if (MIL.contact.email.value == "") {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_email);
        MIL.contact.email.style.border = "1px solid red";
        valid = false
    }
    else {
        if (!MIL.validation.email(MIL.contact.email.name)) {
            MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_emailInvalid)
            MIL.contact.email.style.border = "1px solid red";
            valid = false
        }
    };
    //validate Telephone
    if (MIL.contact.telephone.value == "") {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_telephone);
        MIL.contact.telephone.style.border = "1px solid red";
        valid = false
    };
    //validate Country
    if (MIL.contact.country.selectedIndex == 0) {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_country);
        MIL.contact.country.style.border = "1px solid red";
        valid = false
    };
    //validate Product Area, if Reason for Contact either "Technical Support" or "Learn More"
    if (MIL.contact.reason.options[MIL.contact.reason.selectedIndex].text == MIL.localization.reason_technical | 
        MIL.contact.reason.options[MIL.contact.reason.selectedIndex].text == MIL.localization.reason_learnMore ) {
        if (MIL.contact.productArea.selectedIndex == 0 ) { 
            MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_productArea);
            MIL.contact.productArea.style.border = "1px solid red";
            valid = false
        };
    };
    //validate Reason for Contact
    if (MIL.contact.reason.selectedIndex == 0) {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_contactReason);
        MIL.contact.reason.style.border = "1px solid red";
        valid = false
    };
    //validate Product, if Request a Quote selected
    if (MIL.contact.reason.options[MIL.contact.reason.selectedIndex].text == MIL.localization.reason_raq) {
        if (MIL.contact.product.selectedIndex == 0 ) { 
            MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_product);
            MIL.contact.product.style.border = "1px solid red";
            valid = false
        };
    };
    //validate Comments
    if (MIL.contact.comments.value == "" | MIL.contact.comments.value == MIL.localization.comments_default) {
        MIL.contact.addError(MIL.contact.requireRight, MIL.localization.req_comments);
        MIL.contact.comments.style.border = "1px solid red";
        valid = false
    };
    
    return valid;
};
MIL.contact.resetContactLookup = function() {
    MIL.contact.requireLeft.innerHTML = "";
    MIL.contact.requireLeft.style.display = "none";
    MIL.contact.distCountry.style.border = "";
    MIL.contact.distService.style.border = "";
}
MIL.contact.validateContactLookup = function(){
    var valid = true
    //Clear existing error messages
    MIL.contact.resetContactLookup();
    
    //validate Country
    if (MIL.contact.distCountry.selectedIndex == 0) {
        MIL.contact.addError(MIL.contact.requireLeft, MIL.localization.req_country);
        MIL.contact.distCountry.style.border = "1px solid red";
        valid = false;
    };
    //validate Service
    if (MIL.contact.distService.selectedIndex == 0) {
        MIL.contact.addError(MIL.contact.requireLeft, MIL.localization.req_service);
        MIL.contact.distService.style.border = "1px solid red";
        valid = false;
    };
    return valid;
};

