﻿window.onload = function() {
    createMenu();  
    
    var url = window.location.pathname;
    var filename = url.substring(url.lastIndexOf('/')+1);
    
    if (filename == "request-quote.php"){
        document.getElementById("submitButton").onclick = function() { validateEntryFull("entryFormFull"); }
        
        
    }
    if ((filename == "index.html") || (filename == "")) {
       document.getElementById("submit-continue").onclick = function() { validateEntry("entryForm"); }
    
        
        
    }

      
}

var descriptionRows=1;

function checkEnter(e){ //e is event object passed from function invocation
    var characterCode 

    if(e && e.which){ //if which property of event object is supported (NN4)
    e = e
    characterCode = e.which //character code is contained in NN4's which property
    }
    else{
    e = event
    characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        document.getElementById("furniture"+descriptionRows).removeAttribute("onkeypress")
        descriptionRows = descriptionRows + 1;
        var el = document.createElement("div");
        el.innerHTML = "<input name='quantity" + descriptionRows + "' type='text' maxlength='3' size='3' class='short' id='quantity" + descriptionRows + "'  />&nbsp;&nbsp;&nbsp;<input onkeypress='checkEnter(event)' name='furniture" + descriptionRows + "' type='text' maxlength='400' class='full' id='furniture" + descriptionRows + "'  />"  ;
        document.getElementById("detail").appendChild(el) ;
        document.getElementById("quantity" + descriptionRows).focus();
        document.getElementById("furnitureCount").value = descriptionRows;


        return false
    }
    else{
    return true
    }

}

function createMenu() {
    
    var section = document.getElementsByTagName("body").item(0).getAttribute("id");
    document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
    
    document.getElementById("menu-home").onmouseover = function() {
        document.getElementById("menu-home").src = "images/menu-home-on.gif";
        document.getElementById("menu-home").onmouseout = function() {
           document.getElementById("menu-home").src = "images/menu-home.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    document.getElementById("menu-local").onmouseover = function() {
        document.getElementById("menu-local").src = "images/menu-local-on.gif";
        document.getElementById("menu-local").onmouseout = function() {
           document.getElementById("menu-local").src = "images/menu-local.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    document.getElementById("menu-long").onmouseover = function() {
        document.getElementById("menu-long").src = "images/menu-long-on.gif";
        document.getElementById("menu-long").onmouseout = function() {
           document.getElementById("menu-long").src = "images/menu-long.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    document.getElementById("menu-testimonials").onmouseover = function() {
        document.getElementById("menu-testimonials").src = "images/menu-testimonials-on.gif";
        document.getElementById("menu-testimonials").onmouseout = function() {
           document.getElementById("menu-testimonials").src = "images/menu-testimonials.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    document.getElementById("menu-tips").onmouseover = function() {
        document.getElementById("menu-tips").src = "images/menu-tips-on.gif";
        document.getElementById("menu-tips").onmouseout = function() {
           document.getElementById("menu-tips").src = "images/menu-tips.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    document.getElementById("menu-contact").onmouseover = function() {
        document.getElementById("menu-contact").src = "images/menu-contact-on.gif";
        document.getElementById("menu-contact").onmouseout = function() {
           document.getElementById("menu-contact").src = "images/menu-contact.gif"; 
           document.getElementById("menu-" + section).src = "images/menu-" + section + "-on.gif";
        }
    }
    
}


function validateEntry(formId) {
    var valid = 0;
    var movingDate = document.getElementById("moving_date").value;
    var fromZip = document.getElementById("moving_from_zip").value;
    var toZip = document.getElementById("moving_to_zip").value;
    
        valid = validateZip(fromZip) + validateZip(toZip) + isFullDate(movingDate);
        
        
        if (valid > 0) { 
            document.getElementById("errormsg").style.display = "block";
            showError(validateZip(fromZip),"moving_from_zip");
            showError(validateZip(toZip),"moving_to_zip");
            showError(isFullDate(movingDate),"moving_date");
        }
        else {
            document.getElementById(formId).submit();
        }
        
    
}

function validateEntryFull(formId) {
    var valid = 0;
    var ValidQuantity = 0;
    var movingDate = document.getElementById("moving_date").value;
    var fromZip = document.getElementById("moving_from_zip").value;
    var toZip = document.getElementById("moving_to_zip").value;
    var fullname = document.getElementById("fullname").value;
    var email = document.getElementById("email").value;
    
        valid = validateZip(fromZip) + validateZip(toZip) + isFullDate(movingDate) + validateName(fullname) + validateEmail(email);
        
        
        //check for valid quantity
        for (var ct = 1;ct <= descriptionRows; ct++){
            ValidQuantity = ValidQuantity + validateQuantity(document.getElementById("quantity" + ct).value);
        }
        
        
        valid = valid + ValidQuantity;
        if (valid > 0) { 
            document.getElementById("errormsg2").style.display = "block";
            showErrorForm(validateZip(fromZip),"moving_from_zip");
            showErrorForm(validateZip(toZip),"moving_to_zip");
            showErrorForm(isFullDate(movingDate),"moving_date");
            showErrorForm(validateName(fullname),"fullname");
            showErrorForm(validateEmail(email),"email");
            for (var ct = 1;ct <= descriptionRows; ct++){
                showErrorForm(validateQuantity(document.getElementById("quantity" + ct).value),"quantity" + ct);
            }
        }
        else {
            document.getElementById("entryFormFull").submit();
        }
        
    
}

function validateUpdate() {
    var valid = 0;
    
    var getEmail = document.getElementById("email").value;
    var getMobile1 = document.getElementById("mobile1").value;
    var getMobile2 = document.getElementById("mobile2").value;
    var getMobile3 = document.getElementById("mobile3").value;
        valid = validateEmail(getEmail,"error-email") + 
        validateMobile(getMobile1,getMobile2,getMobile3,"error-mobile");
        if (valid > 0) { return false; }
        else { document.getElementById("updates").submit(); return true;}
        
    
}

function validateMobile(mobileValue1,mobileValue2,mobileValue3,mobileError,size) {
         var mobileValue = mobileValue1 + mobileValue2 + mobileValue3;
         if (isInteger(mobileValue) && (mobileValue.length == 10)) {
            document.getElementById(mobileError).style.visibility = "hidden";
            return 0;
         } 
         else {
            document.getElementById(mobileError).style.visibility = "visible";
            return 1;
        }
}

function validateZip(zip) {
         if (isInteger(zip) && (zip.length == 5)) {
            return 0;
         } 
         else {
            return 1;
        }   

}


function validateName(nameValue, nameError){      
   
    if (isEmpty(nameValue.trim())) { 
        return 1;
        }
    else {
        return 0;
    }

}



function validateEmail(emailValue, emailError){      
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if  (!(emailPattern.test(emailValue))) { 
        return 1;
        }
    else {
        return 0;
    }

}

function validateQuantity(quantity) {
         if (isInteger(quantity)) {
            return 0;
         } 
         else {
            return 1;
        }   

}

function isInteger (s)
   {
      var i;
      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }


function isFullDate(dateValue) {
    var isDate = /(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d/;
    if (isDate.test(dateValue)) {
        return 0;
    }
    else {
        return 1;
    }
}

//trim
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}


function showError(isError,errorId) {
    document.getElementById(errorId).style.backgroundImage = "url(images/field-home.gif)";
    if (isError) {
        document.getElementById(errorId).style.backgroundImage = "url(images/field-home-error.gif)";
    }
}

function showErrorForm(isError,errorId) {
    document.getElementById(errorId).style.backgroundColor = "#f8e8a6";
    if (isError) {
        document.getElementById(errorId).style.backgroundColor = "#f8baa6";
    }
}



