// JavaScript Document

/**********************************************************************
 *                            validateHeaderSearch                    *
 *                                                                    *
 * Verify that a search term has been entered in the header search    *
 * box. If no search term was entered, bring user to the advanced     *
 * search page.                                                       *
 *                                                                    *
 * Arguments:                                                         *
 *    searchForm: The header search form                              *  
 * *******************************************************************/
 
 function validateHeaderSearch(searchForm) 
 {
    if (searchForm.keyword.value == "What are you looking for?" || 
        searchForm.keyword.value == "")
    {
       //window.location.href = "/servlet/Catalog";
       window.location.href = 'http://www.emeraldcityfabrics.com/servlet/Catalog';
       return false;
    }
    else
    {
       return true;
    }
 }
 
 
/**********************************************************************
 *                            clearSearchField                        *
 *                                                                    *
 * Clear the value in the header search box.                          *
 *                                                                    *
 * Arguments:                                                         *
 *    keywordInput: The search form keyword input box.                *  
 * *******************************************************************/ 
 
 function clearSearchField(keywordInput)
 {
    keywordInput.value = "";
 }                                                               


/**********************************************************************
 *                            validateAdvancedSearch                  *
 *                                                                    *
 * Verify that a search term has been entered in the advanced search  *
 * box or a category has been selected.                               *
 *                                                                    *
 * Arguments:                                                         *
 *    searchForm: The advanced search form                            *  
 * *******************************************************************/
 
 function validateAdvancedSearch(searchForm) 
 {
    if ( (searchForm.keyword.value ==  "") && (searchForm.category.value == "all") )
    {
       if( searchForm.elements["$catalog.product"].value != "" || searchForm.elements["oem"].value != "")
       {
          var error = document.getElementById('errorMessage');
          error.style.display = 'none';
          return true;
       }
       else
       {
          var error = document.getElementById('errorMessage');
          error.style.display = 'block';
          error.style.color = 'red';
          error.style.marginTop = '5px';
          return false;
       }
    }
    else
    {
       var error = document.getElementById('errorMessage');
       error.style.display = 'none';
       return true;
    }
 }

/**********************************************************************
 *                            validateQuantity                        *
 *                                                                    *
 * Verify that a valid quantity value has been entered on the product *
 * detail or product list pages.                                      *
 *                                                                    *
 * Arguments:                                                         *
 *    cartAddForm:     The CartAddForm                                * 
 *    productId:       The id of the product                          *  
 *    productQuantity: The quantity available                         * 
 * *******************************************************************/
 
 function validateQuantity(cartAddForm, productQuantity, productId) 
      {
         var error1 = document.getElementById('errorMessageFY' + productId);
         var error2 = document.getElementById('errorMessageQE' + productId);
 
         if( cartAddForm.qty.value.substring(cartAddForm.qty.value.length - 1, cartAddForm.qty.value.length) == "." )
         {
            var noDecimal = cartAddForm.qty.value.substring(0, cartAddForm.qty.value.length - 1);
            cartAddForm.qty.value = noDecimal;
         }

         if( cartAddForm.qty.value.substring(cartAddForm.qty.value.length - 2, cartAddForm.qty.value.length) == ".0" )
         {
            var truncated = cartAddForm.qty.value.substring(0, cartAddForm.qty.value.length - 2);
            cartAddForm.qty.value = truncated;
         }

         if(cartAddForm.qty.value % 1 == 0 && cartAddForm.qty.value <= productQuantity )
         {
            error1.style.display = 'none';
            error2.style.display = 'none';
            return true;
         }
         else if ( cartAddForm.qty.value > productQuantity )
         { 
            error2.style.display = 'block';
            error2.style.color = 'red';
            error2.style.marginTop = '5px';
            error1.style.display = 'none';
            return false;
         }
         else
         {
            error1.style.display = 'block';
            error1.style.color = 'red';
            error1.style.marginTop = '5px';
            error2.style.display = 'none';
            return false;
         }
      } 
      
/**********************************************************************
 *                            validateForm                            *
 *                                                                    *
 * Validate form fields.                                              *
 *                                                                    *
 * Arguments:                                                         *
 *    form:     The form to validate                                  * 
 * *******************************************************************/     
 
 function validateForm(form)
 {
    if(validatePassword(form))
    {
       return validateEmail(form)
    }
    else
    {
       return false;
    }
 }  
      


/**********************************************************************
 *                            validatePassword                        *
 *                                                                    *
 * Verify that password and confirmation password are the same.       *
 *                                                                    *
 * Arguments:                                                         *
 *    form:     The form to validate                                  * 
 * *******************************************************************/
 
 function validatePassword(form)
 {
    var password = form.elements["$customer.password"].value;
    var confirmPassword = form.elements["confirm"].value;
    var errorMsg = document.getElementById("passwordError");
    
    if( password != confirmPassword)
    {
      errorMsg.style.display = 'block';
      errorMsg.style.color = 'red';
      return false;
    }
    else
    {
       errorMsg.style.display = 'none';
       return true;    
    }
    
 }
 

/**********************************************************************
 *                            validateEmail                           *
 *                                                                    *
 * Verify that entered email is of the correct format and that email  *
 * confirmation matches email.                                        *
 *                                                                    *
 * Arguments:                                                         *
 *    form:     The form to validate                                  * 
 * *******************************************************************/
 
 function validateEmail(form)
 {
    var emailElement;
    
    if (form.name == 'register')
    {
       emailElement = form.elements["$customer.email"];
    }
    else if (form.name = 'lead')
    {
       emailElement = form.elements["$lead.email"];
    }
    else if (form.name = 'emailfriend')
    {
       emailElement = form.elements["$email.senderEmail"];
       var emailFriendElement = form.elements["$email.recipientEmail"];
       if (emailElement != null && emailFriendElement != null)
       {
          var email = emailElement.value.replace(/^\s+|\s+$/g, '');
          var friendEmail = emailFriendElement.value.replace(/^\s+|\s+$/g, '');
          
          var pattern = "^\\s*\\w+@\\w+\\.\\w+\\s*$";
          
          if(email == "")
          {
          
          }
          else if(friendEmail == "")
          {
          
          }
          else if(email.search(pattern) == -1)
          {
          
          }
          
          
       }
       
    }
    
    var confirmEmailElement = form.elements["confirmEmail"];
    
    if (emailElement != null && confirmEmailElement != null)
    {
       var email = emailElement.value.replace(/^\s+|\s+$/g, '');
       
       var confirmEmail = confirmEmailElement.value.replace(/^\s+|\s+$/g, '');
       
       //var pattern = "^\\s*\\w+@\\w+\\.\\w+\\s*$";
       var pattern = "^\\s*\\w+@\\w+\\s*$";
       
       if(confirmEmail == "" )
       {
          var errorMsg = document.getElementById("emailError");
          errorMsg.innerHTML = "&nbsp;&nbsp;Please confirm email address.";
          errorMsg.style.display = 'block';
          errorMsg.style.color = 'red';
          return false;
       }
       
       if(email.search(pattern) == -1)
       {
          //var errorMsg = document.getElementById("emailError");
          //errorMsg.innerHTML = "&nbsp;&nbsp;Invalid email address.";
          //errorMsg.style.display = 'block';
          //errorMsg.style.color = 'red';
          //return false;    
          return true;      
       }
    
       if( email == confirmEmail )
       {
          emailElement.value = email;
          confirmEmailElement.value = confirmEmail;
          return true;
       }
       else
       {
          var errorMsg = document.getElementById("emailError");
          errorMsg.innerHTML = "&nbsp;&nbsp;Emails do not match.";
          errorMsg.style.display = 'block';
          errorMsg.style.color = 'red';
          return false;
       }      
    }
    else
    {
       //form doesn't contain items that need validation
       return true;
    }

 }

 /*********************************************************************
 *                            writePreorderCookie                     *
 *                                                                    *
 * Write the product number to a cookie called preorder. Cookie is    *
 * used to send customers back to preorder page when they choose to   *
 * log in or register before completing the preorder form.            * 
 *                                                                    *
 * Arguments:                                                         *
 *    productNumber: the product number of the preorder product       * 
 * *******************************************************************/
 
function writePreorderCookie(productNumber)
{
   document.cookie = "lead=" + productNumber + "; max-age=600";
}

 /*********************************************************************
 *                            deletePreorderCookie                    *
 *                                                                    *
 * Delete the cookie called preorder. Cookie is deleted after         *
 * customer completes a preorder form to avoid incorrect redirect to  *
 * preorder form after logging in or registering.                     * 
 *                                                                    *
 * *******************************************************************/
 
 function deletePreorderCookie()
 {
    document.cookie = "lead=; max-age=0;"
 }
 

