// JavaScript Document wizard.js

var wizard = new Object();

wizard.current_page = '';
wizard.pages = new Array('page_customer', 'page_companies', 'page_products', 'page_timeframes', 
  'page_finish', 'page_send_data');
wizard.position = 0;
wizard.customer_info = new Array();
  wizard.customer_info['name'] = '';
  wizard.customer_info['title'] = '';
  wizard.customer_info['company'] = '';
  wizard.customer_info['address_1'] = '';
  wizard.customer_info['address_2'] = '';
  wizard.customer_info['city'] = '';
  wizard.customer_info['state'] = '';
  wizard.customer_info['zip'] = '';
  wizard.customer_info['email_address'] = '';
  wizard.customer_info['phone'] = '';
  wizard.customer_info['fax'] = '';
  wizard.customer_info['pref'] = '';
wizard.vendors = new Array();
wizard.products = new Array();
wizard.timeframe = 0;
wizard.drawing = 1;

//buttons
wizard.next_text = 'Next >>';
wizard.back_text = '<< Back';
wizard.back_width = '';

wizard.navigation = function(theval)
{
  /* if at the customer info screen check for comm preference and then check
  *  for applicable info
  *  
  */
  if (wizard.position == 0)
  {
    var thecheck_method = wizard.check_method_and_info();
    if (thecheck_method != true)
    {
      wizard.notify_user(thecheck_method);
      return;
    }  
  }      
   
  wizard.store_information(wizard.position);
   
  wizard.position = wizard.position + theval;
  wizard.setup_buttons(wizard.position);
  var page_name = wizard.pages[wizard.position];  
  document.getElementById('form_main').innerHTML = frm[page_name]; 
  
  
  //this is for where you are going
  switch(wizard.position)
  {
    case 0:
      setup_states_list();
      setup_pref_list();  
      break;
    case 1:
      document.getElementById('vendors').innerHTML = vendors_list;
      break;
    case 2:
      document.getElementById('products').innerHTML = products_list;
      break;
    case 3:
      document.getElementById('time_frames').innerHTML = time_frames_list;
      break;
    case wizard.pages.length -1:
      wizard.make_hidden_inputs();      
      document.forms[0].method = 'POST';
      document.forms[0].action = 'insert.php?form=vendor_info_request';
      document.forms[0].submit();
      return;
  }//end switch
  
  wizard.fill_out_form(wizard.position);
    
  if ((theval == 1) && (wizard.position == (wizard.pages.length-2)))
  {
    wizard.show_information();    
  }
  
}// end .navigation

wizard.check_method_and_info = function()
{
  if (document.forms[0].elements['name'].value == '')
  {
    return 'name';
  }
  
  var prefval = document.forms[0].elements['pref'].value;
  //alert(prefval + ': ' + document.forms[0].elements['email_address'].value);
  if (prefval == '')
  {    
    return 'pref';
  } else {
    switch(prefval)
    {
      case '1':
        if(document.forms[0].elements['email_address'].value == '')
        {        
          return 'email_address';   
        }
        break;
      case '2':
        if(document.forms[0].elements['fax'].value == '')
        {        
          return 'fax';   
        }
        break;
      case '3':
        if(document.forms[0].elements['address_1'].value == '')
        {        
          return 'address_1';   
        } else if (document.forms[0].elements['city'].value == ''){
          return 'city';
        } else if (document.forms[0].elements['state'].value == '0'){
          return 'state';
        } else if (document.forms[0].elements['zip'].value == ''){
          return 'zip';
        }
        break;
      case '2':
        if(document.forms[0].elements['fax'].value == '')
        {        
          return 'fax';   
        }
        break;
    }// end switch
  }  
  return true;
}// end .check_method_info

wizard.notify_user = function(thefield)
{
  switch(thefield)
  {
    case 'name':
      alert('Please tell us your name');
      break;
    case 'pref':
      alert('How may we contact you?');      
      break;
    case 'email_address':
      alert('Please fill out your email address');
      break;
    case 'phone':
      alert('Please fill out your phone number');
      break;
    case 'fax':
      alert('We will need your fax number')
      break;
    case 'address_1':
      alert('We will need an address')
      break;
    case 'city':
      alert('Please fill out the city')
      break;
    case 'state':
      alert('Please select your state')
      break;
    case 'zip':
      alert('Please fill out the zip')
      break;
  }
  document.getElementById(thefield).focus();
}//end .notify_user

wizard.setup_buttons = function(theval)
{
  if(theval == wizard.pages.length-1)
  {
    //alert('this is the end');
    document.getElementById('btn_next').disabled = true;
  } else if (theval == wizard.pages.length-2) {
    document.getElementById('btn_next').value = 'Save';
    //store original values
    wizard.back_text = document.getElementById('btn_back').value;
    wizard.back_width = document.getElementById('btn_back').style.width
    //now, change the values
    document.getElementById('btn_back').style.width = '150px';
    document.getElementById('btn_back').value = 'Go Back and Edit';    
  } else if (theval < wizard.pages.length-2) {
    document.getElementById('btn_next').disabled = false;
    document.getElementById('btn_next').value = wizard.next_text;
    document.getElementById('btn_back').style.width = wizard.back_width;
    document.getElementById('btn_back').value = wizard.back_text;
  } 
  if (theval == 0) {
    //alert('you are at the beginning');
    document.getElementById('btn_back').disabled = true;
  } else if (theval > 0) {
    document.getElementById('btn_back').disabled = false;
  }
}// end .setup_buttons

wizard.store_information = function(pos)
{
  // 'pos' is the wizard.position, or the current page
  
  switch(pos)
  {
    case 0:      
      for(i=0; i<document.forms[0].elements.length; i++)
      {
        //alert(document.forms[0].elements[i].id);
        theid = document.forms[0].elements[i].id;
        wizard.customer_info[theid] = document.forms[0].elements[i].value;        
      }      
      break;
    case 1:
      wizard.vendors = wizard.get_selected_items('vendors_list').split(',');            
      //alert(wizard.vendors.toString());
      break;
    case 2:
      wizard.products = wizard.get_selected_items('products_list').split(',');
      //alert(wizard.products.toString());
      break;
    case 3:
      wizard.timeframe = wizard.get_selected_items('time_frames_list');      
      break;
  }//end switch pos
  
}//end .store_information

wizard.get_selected_items = function (elem)
{
  var selObj = document.getElementById(elem);
  var i;
  var count = 0;
  var return_string = '';
  var temp_array = new Array();
  for (i=0; i<selObj.options.length; i++) 
  {
    if (selObj.options[i].selected) 
    {
      temp_array[count] = selObj.options[i].value;      
      //temp_array[count] = selObj.options[i].innerHTML;
      count++;
    }
  }// end for
  return_string = temp_array.toString();
  return return_string;  
}// .get_selected_items

wizard.highlight_selected_items = function (elem, items)
{  
  var selObj = document.getElementById(elem);
  var i;
  var count = 0;
  var return_string = '';
  var temp_array = new Array();
  var arr_items = items.split(',');
  for (i=0; i<selObj.options.length; i++) 
  {
    for (j=0; j<arr_items.length; j++)
    {      
      if(selObj.options[i].value == arr_items[j]) selObj.options[i].selected = 'selected';
    }
  }
}// .highlight_selected_items

wizard.show_information = function()
{
  //wizard.check_info('customer_info');
  var tmp = '';
  //go through the customer array and show the values entered by the user
  for(key in wizard.customer_info)
  {
    switch (key)
    {
      case 'state':
        var arr_states = states.split(',');      
        var thestate = '';
        if (wizard.customer_info[key] > 0)
        {
          thestate = arr_states[parseInt(wizard.customer_info[key]) -1]; 
        } else {
          thestate = '';
        }      
        tmp += '<b>' + fix_words(key) + ':</b> ' + thestate + '<br />';
        break;
      case 'pref':
        var arr_prefs = pref.split(',');      
        var thepref = '';
        if (wizard.customer_info[key] > 0)
        {
          thepref = arr_prefs[parseInt(wizard.customer_info[key]) -1]; 
        } else {
          thepref = '';
        }      
        tmp += '<b>' + fix_words(key) + ':</b> ' + thepref + '<br />';
        break;
      default:
        var theval = wizard.customer_info[key];
        var keyname = fix_words(key);
        tmp += '<b>' + keyname + ':</b> ' + theval + '<br />';
        break;      
    }// end switch
        
  }// end for 
  tmp += '<br />';
  
  // go through the Companies/Vendors array
  tmp += 'Companies You Selected: ';
  if (wizard.vendors[0])
  {
    //var arr_vendors = vendors_obj;//.split(',');
    for(i=0; i<wizard.vendors.length; i++)
    {
      
      /*
      var num = parseInt(wizard.vendors[i]) -1;
      tmp += arr_vendors[num];
      if(i<wizard.vendors.length -1) tmp += ', ';
      
      */
      for(var j=0; j<vendors_obj.length; j++)
      {
        if (wizard.vendors[i] == vendors_obj[j].id)
        {
          tmp += vendors_obj[j].name;
        }
      }
      if(i<wizard.vendors.length -1) tmp += ', ';
      
    }
    
    
  } else {
    tmp += 'No companies selected';
  }    
  
  // go through the products array
  tmp += '<br />Products: ';
  if(wizard.products[0])
  {    
    var arr_products = products.split(',');
    for (i=0; i<wizard.products.length; i++)
    {
      var num = parseInt(wizard.products[i]) -1;
      tmp += arr_products[num];
      if(i<wizard.products.length -1) tmp += ', ';
    }
    
  } else {
    tmp += 'No products selected';
  }
  
  // show the Timeframe selected
   
  tmp += '<br />I am thinking of purchasing: ';
  
  var arr_timeframes = timeframes.split(',');
  var num = parseInt(wizard.timeframe) - 1;  
  tmp += arr_timeframes[num]; 
      
  document.getElementById('form_values').innerHTML =  tmp;
  
}// end .show_information


wizard.make_hidden_inputs = function()
{   
  for(key in wizard.customer_info)
  {
    if (wizard.customer_info[key])
    {
      wizard.add_form_fields('hidden', key, wizard.customer_info[key]);
    }    
  }
  
  wizard.add_form_fields('hidden', 'vendors', wizard.vendors);
  wizard.add_form_fields('hidden', 'products', wizard.products);
  wizard.add_form_fields('hidden', 'timeframe', wizard.timeframe.toString());
  wizard.add_form_fields('hidden', 'drawing', wizard.drawing? 1 : 0);
  
}//end .make_hidden_inputs

wizard.add_form_fields = function(fieldType, fieldName, fieldValue)
{  
  var theform = document.forms[0];
  var input = document.createElement('input');  
  input.setAttribute('type', fieldType);
  input.setAttribute('name', fieldName);
  input.setAttribute('id', fieldName);
  input.setAttribute('value', fieldValue);
  theform.appendChild(input);
}// end .add form fields

wizard.fill_out_form = function(theid)
{
  switch(theid)
  {
    case 0:
      for(key in wizard.customer_info)
      {
        if (key == 'pref')
        {
          wizard.highlight_selected_items('pref', wizard.customer_info['pref']);
        } else {
          document.forms[0].elements[key].value = wizard.customer_info[key];
        }       
      }      
      break;
    case 1:
      if (wizard.vendors[0])
      {
        wizard.highlight_selected_items('vendors_list', wizard.vendors.toString());
      }
      break;
    case 2:
      if (wizard.products[0])
      {
        wizard.highlight_selected_items('products_list', wizard.products.toString());
      }
      break;
    case 3:
      wizard.highlight_selected_items('time_frames_list', wizard.timeframe);
      break;
  }//end switch
  
}//end .fill out form

wizard.check_info = function(thekey)
{
  for(key in wizard[thekey])
  {
    alert(wizard[thekey][key]);
  }
}

wizard.drawing_change = function()
{
  wizard.drawing = document.getElementById('drawing').checked? 1: 0;
}

wizard.test = function()
{
  alert('this is a test');
}// end .test

