﻿function processForm(ButtonCommand)
        {
        //display processing logo
        
        toggleLayer('productprocessingimage')
        
        
        var frm  = document.forms['FormBody'];   
        var numElements = frm.elements.length;
        var result = "";
        for (var i=0; i<numElements; i++)
            {
            var theElement = frm.elements[i];
            var elementName = theElement.name;
            var elementType = theElement.type;
            var elementValue = theElement.value;
            
            
            if ("text" == theElement.type || "textarea" == theElement.type)
                {
                    result = result + elementName + '=' + elementValue + '|';
                }
            else if ("checkbox" == theElement.type || "radio" == theElement.type)
                {
                    if (theElement.checked)
                        {
                        result = result + elementName + '=' + elementValue + '|';
                        }
                }
             else if ("hidden" == theElement.type && elementName != '__VIEWSTATE')
                {
                result = result + elementName + '=' + elementValue + '|';
                }
            else if ("select-one" == theElement.type)
                {
                result = result + elementName + '=' + elementValue + '|';                    
                }
            else if ("select-multiple" == theElement.type)
                {
                    result = result + elementName + '=';
                    var len = theElement.options.length;
                    for (var x=0; x<len; x++)
                    {
                      if (theElement.options[x].selected)
                      {
                      result = result + theElement.options[x].value + ',';  
                      }
                      
                   
                    
                    } 
                    if (result.charAt(result.length) == ",")
                        {
                        result = result.substr(0,result.length-1);
                        }                   
                    result = result + '|';
                }

            }
            
            //call the ajax handler for a remote vb procedure
            //different calls are made to have the return function process them differently
            
            if (ButtonCommand == 'GetQuote')
                {
                SitecoreWebsite.SOLI_Interior_Products_SubLayout.ValidateFormData(result, ButtonCommand, processQuote);
                }
            else if (ButtonCommand == 'Calculate')
                {
                SitecoreWebsite.SOLI_Interior_Products_SubLayout.ValidateFormData(result, ButtonCommand, processCalculate);
                }
            else if (ButtonCommand == 'AddToCart')
                {
                SitecoreWebsite.SOLI_Interior_Products_SubLayout.ValidateFormData(result, ButtonCommand, processAddToCart);
                }
            
            
        }


        function processAddToCart(res)
        {
        
         toggleLayer('productprocessingimage')
         
        if (res.error != null)
        {

          document.getElementById('ErrorMessage').innerHTML = "<hr/>Your request was not processed for the following reason:<li>" + res.error.Message + "</li>"
          return;
        }
        else
        {
            if (res.value != null)
               {
               if (res.value.indexOf("Add To Cart") > -1)
                    {window.location = "/MyLyrasis/Shopping Cart.aspx";}
                else
                    {document.getElementById('ErrorMessage').innerHTML = res.value;}               
               }
            else
               {
               document.getElementById('ErrorMessage').innerHTML = "An error has occured. Your request was not processed.";
               }
        }
        }



   



        function processCalculate(res)
        {
        toggleLayer('productprocessingimage')
        if (res.error != null)
        {

          document.getElementById('ErrorMessage').innerHTML = "<hr/>Your request was not processed for the following reason:<li>" + res.error.Message + "</li>"
          return;
        }
        else
        {
            if (res.value != null)
               {   
                           
               if (res.value.indexOf("Price:") != -1)                              
                    {
                    if (document.getElementById('ErrorMessage').innerHTML != "")
                        {
                        document.getElementById('ErrorMessage').innerHTML ="";
                        }
                    
                    document.getElementById('CalculatedAmount').innerHTML = res.value.replace("Price:","");
                    }
               else
                    {
                    document.getElementById('ErrorMessage').innerHTML = res.value;
                    document.getElementById('CalculatedAmount').innerHTML = "$0.00";
                    }
                
                
               }
            else
               {
               document.getElementById('ErrorMessage').innerHTML = "";
               }
        }
        }



        function processQuote(res)
        {
        toggleLayer('productprocessingimage')
        if (res.error != null)
            {

              document.getElementById('ErrorMessage').innerHTML = "<hr/>Your request was not processed for the following reason:<li>" + res.error.Message + "</li>"
              return;
            }
            else
            {
                if (res.value != null)
                   {               
                   if (res.value == "success")
                    {
                    alert("A quote request has been successfully created.\n\nYou will be notified via email when your quote is available or \nyou may check your shopping cart for available quotes.");
                    window.location.reload();
                    }
                   
                   }
                else
                   {
                   document.getElementById('ErrorMessage').innerHTML = "There was an error generating your quote, please contact the helpdesk to obtain your quote.";
                   }
            }
        }

        
var ObjectName;
        
function GetReturnList(ObjectToAddValuesName,SCIDNodeBase,NodeValue)
{
toggleLayer('productprocessingimage');

ObjectName = ObjectToAddValuesName;

SitecoreWebsite.SOLI_Interior_Products_SubLayout.RetrieveNodeValues(SCIDNodeBase, NodeValue, true ,processValues);
  
}



function GetReturnListWithoutBlank(ObjectToAddValuesName,SCIDNodeBase,NodeValue)
{
toggleLayer('productprocessingimage');

ObjectName = ObjectToAddValuesName;

SitecoreWebsite.SOLI_Interior_Products_SubLayout.RetrieveNodeValues(SCIDNodeBase, NodeValue, false ,processValues);
  
}



function processValues(res)
{

 if (res.error != null)
        {
          document.getElementById('ErrorMessage').innerHTML = "<hr/>Your request was not processed for the following reason:<li>" + res.error.Message + "</li>"
          return;
        }
        else
        {
            if (res.value != null)
               {   
                var myArray = res.value.split(",");
                var obj = document.getElementById(ObjectName);
                obj.options.length = 0;
                for (i=0; i<myArray.length; i++) 
                    {
                    obj.options[i] = new Option(myArray[i],myArray[i]);        
                    }
                }
        }    
toggleLayer('productprocessingimage');     
}




