// Copyright 2004 - HOCO Enterprises, LLC 
// Beyer for HOCO (2005) - Convert to server-based system 
//
//     item = A string containing the number that uniquely identifies the item.
//     cart = Cart Number user selected  
// This function adds an item to the user's shopping cart.

function add_to_cart(item, descnum) {
//Remove path name (needed for selected items now) 
    ilength = item.length;
    if (ilength > 44){
       itail = ilength - 44; 
       item = item.substr(itail,44);
    } 

    if (item.substr(27,7) == 'red_grn' 
     || item.substr(27,7) == 'blk_blk'
     || item.substr(27,7) == 'wht_kki'){
      alert('Sorry, that item is not available');
      return  
    }   
//document.forms[0].sel_item.value = document.images[hats[0].substr(20,6)].src;
//alert ('item = ' + item);
document.forms[0].sel_item.value = item.substr(13,21);
//document.forms[0].sel_item.value = document.images["gmc001"].src; //gets full image path
document.forms[0].prevscrn.value = save_store_page;
document.forms[0].short_desc.value = descnum;

if (item.substr(13,6) == 'stf003'){
   if (document.forms[0].front_designs1.selectedIndex == 0){alert('Please select a design from list.'); document.forms[0].front_designs1.focus; return;}
}
if (item.substr(13,6) == 'stf004'){
   if (document.forms[0].front_designs2.selectedIndex == 0){alert('Please select a design from list.'); document.forms[0].front_designs2.focus; return;}
}

if (CartWindow.location){
  CartWindow.close();
  document.forms[0].cart_open_status.value = 'not_open'; 
}

//Submit Form if it passes the edits...
  document.forms.hat_display.submit();  
}

function add_jkt_to_cart(str, front, back, descnum) {
   t_descnum = descnum;
//   alert('front.substr(0,3) = ' + front.substr(0,3));
   findjkt = front.indexOf(str);
   if (str == 'tsw'){findjkt = front.indexOf('tsh')}
   finddnm = front.indexOf('dnm');
   if (finddnm > 0){findjkt = finddnm} 
   front = front.substring(findjkt,findjkt+24);
   if (str == 'jkt'){
      findjkt = back.indexOf(str);
      back = back.substring(findjkt,findjkt+24);
   }
   if (front.substr(0,20) == 'jkt001_gto001_ft_blk' || back.substr(0,20) == 'jkt001_gto001_bk_blk'    
    || front.substr(0,20) == 'jkt002_gto001_ft_blk' || back.substr(0,20) == 'jkt002_gto001_bk_blk'    
    || front.substr(0,24) == 'jkt003_gto001_ft_blk_thu' || back.substr(0,24) == 'jkt003_gto001_bk_blk_thu'
    || front.substr(0,24) == 'jkt003_gto001_ft_red_blu' || back.substr(0,24) == 'jkt003_gto001_bk_red_blu'
    || front.substr(0,24) == 'jkt003_gmc001_ft_mid_red' || back.substr(0,24) == 'jkt003_gmc001_bk_mid_red'
    || front.substr(0,24) == 'jkt003_pnt002_ft_mid_red' || back.substr(0,24) == 'jkt003_pnt002_bk_mid_red'
    || front.substr(0,24) == 'jkt005_gmc001_ft_mid_red' || back.substr(0,24) == 'jkt005_gmc001_bk_mid_red'
    || front.substr(0,24) == 'jkt005_pnt002_ft_mid_red' || back.substr(0,24) == 'jkt005_pnt002_bk_mid_red'
    || front.substr(0,24) == 'jkt006_roygto_ft_mid_red' || back.substr(0,24) == 'jkt006_roygto_bk_mid_red'
    || front.substr(0,24) == 'jkt006_roygto_ft_mid_nvy' || back.substr(0,24) == 'jkt006_roygto_bk_mid_nvy'
    || front.substr(0,24) == 'jkt006_poci00_ft_mid_blu' || back.substr(0,24) == 'jkt006_poci00_bk_mid_blu'
    || front.substr(17,7) == 'red_red' || back.substr(17,7) == 'red_red'
    || front.substr(17,7) == 'red_bur' || back.substr(17,7) == 'red_bur'
    || front.substr(17,7) == 'blk_blk' || back.substr(17,7) == 'blk_blk'
    || front.substr(17,7) == 'wht_wht' || back.substr(17,7) == 'wht_wht'
    || front.substr(7,24) == 'gmc001_ft_mid_red' || front.substr(7,24) == 'gmc001_lg_mid_red'
    || front.substr(7,24) == 'pnt002_ft_mid_red' || front.substr(7,24) == 'pnt002_lg_mid_red'
    || front.substr(7,24) == 'pnt001_ft_mid_gry' || front.substr(7,24) == 'pnt001_lg_mid_gry'
    || front.substr(7,24) == 'gto001_ft_wht_gry' || front.substr(7,24) == 'gto001_lg_wht_gry'
    || front.substr(0,24) == 'swt001_poci00_ft_mid_blu' || front.substr(7,24) == 'poci00_lg_mid_blu'
    || front.substr(0,24) == 'swt002_poci00_ft_mid_blu'
    || front.substr(0,24) == 'spt002_poci00_ft_mid_blu' || front.substr(0,24) == 'spt001_poci00_ft_mid_blu'
    || front.substr(7,24) == 'roygto_ft_mid_bur' || front.substr(7,24) == 'roygto_lg_mid_bur')
   {
     alert('Sorry, that item is not available');
     return  
   }

//alert('front = ' + front);
//alert('front.substr(14,3) = ' + front.substr(14,3));
//alert('back = ' + back);
//alert('back.substr(7,6) = ' + back.substr(7,6));

   get_color_size(front);
   if (str == 'jkt'){
      if (save_store_page.substr(0,4) == 'club' && descnum > 3){t_descnum = descnum - 1}
      if (document.forms[0].jkt_size[t_descnum].value == "none"){alert('Please select size of jacket.'); document.forms[0].jkt_size[t_descnum].focus; return;}

//alert('doc selected value = ' + document.forms[0].front_designs1.options[document.forms[0].front_designs1.selectedIndex].text);
//alert('doc selected index = ' + document.forms[0].front_designs1.selectedIndex);
      if (descnum == 0){
         if (document.forms[0].front_designs1.selectedIndex < 0 && document.forms[0].back_designs1.selectedIndex < 0 && front.substr(0,6) == 'jkt001'){alert('Please select price from list.'); document.forms[0].front_designs1.focus; return;}
         if ((document.forms[0].front_designs1.selectedIndex == 0 || document.forms[0].back_designs1.selectedIndex == 0) && front.substr(0,6) == 'jkt001'){alert('Please select a design from list.'); document.forms[0].front_designs1.focus; return;}
      }

      if (descnum == 1){
         if (document.forms[0].front_designs2.selectedIndex < 0 && document.forms[0].back_designs2.selectedIndex < 0 && front.substr(0,6) == 'jkt006'){alert('Please select price from list.'); document.forms[0].front_designs2.focus; return;}
         if ((document.forms[0].front_designs2.selectedIndex == 0 || document.forms[0].back_designs2.selectedIndex == 0) && front.substr(0,6) == 'jkt006'){alert('Please select a design from list.'); document.forms[0].front_designs2.focus; return;}
      }
      if (descnum == 2){
         if (document.forms[0].front_designs3.selectedIndex < 0 && document.forms[0].back_designs3.selectedIndex < 0 && front.substr(0,6) == 'jkt003'){alert('Please select price from list.'); document.forms[0].front_designs3.focus; return;}
         if ((document.forms[0].front_designs3.selectedIndex == 0 || document.forms[0].back_designs3.selectedIndex == 0) && front.substr(0,6) == 'jkt003'){alert('Please select a design from list.'); document.forms[0].front_designs3.focus; return;}
      }
      if (descnum == 3){
         if (document.forms[0].front_designs4.selectedIndex < 0 && document.forms[0].back_designs4.selectedIndex < 0 && front.substr(0,6) == 'jkt004'){alert('Please select price from list.'); document.forms[0].front_designs4.focus; return;}
         if ((document.forms[0].front_designs4.selectedIndex == 0 || document.forms[0].back_designs4.selectedIndex == 0) && front.substr(0,6) == 'jkt004'){alert('Please select a design from list.'); document.forms[0].front_designs4.focus; return;}
      }
      if (descnum == 4){
         if (document.forms[0].front_designs5.selectedIndex < 0 && document.forms[0].back_designs5.selectedIndex < 0 && front.substr(0,6) == 'jkt005'){alert('Please select price from list.'); document.forms[0].front_designs5.focus; return;}
         if ((document.forms[0].front_designs5.selectedIndex == 0 || document.forms[0].back_designs5.selectedIndex == 0) && front.substr(0,6) == 'jkt005'){alert('Please select a design from list.'); document.forms[0].front_designs5.focus; return;}
      }
   }

   if (str == 'spt' || str == 'rac' || str == 'msp' || str == 'tsh'){
      if (document.forms[0].jkt_size[descnum].value == "none"){alert('Please select size of shirt.'); document.forms[0].jkt_size[descnum].focus; return;}
      if (descnum == 0){
         if (document.forms[0].front_designs1.value == "none" && (front.substr(0,6) == 'spt002' || front.substr(0,3) == 'tsh')){alert('Please select a design from list.'); document.forms[0].front_designs1.focus; return;}
      }
      if (descnum == 1){
         if (document.forms[0].front_designs2.value == "none" && (front.substr(0,6) == 'spt001' || front.substr(0,3) == 'tsh')){alert('Please select a design from list.'); document.forms[0].front_designs2.focus; return;}   
      }
      if (descnum == 2 && str != 'tsh'){
         if (document.forms[0].front_designs3.value == "none" && front.substr(0,6) == 'rac001'){alert('Please select a design from list.'); document.forms[0].front_designs3.focus; return;}
      }
      if (descnum == 3 && str != 'tsh'){
         if (document.forms[0].front_designs4.value == "none" && front.substr(0,6) == 'msp002'){alert('Please select a design from list.'); document.forms[0].front_designs4.focus; return;}
      } 
      if (descnum == 4 && str != 'tsh'){
         if (document.forms[0].front_designs5.value == "none" && front.substr(0,6) == 'msp001'){alert('Please select a design from list.'); document.forms[0].front_designs5.focus; return;}
      }
   }
   if (str == 'swt' || str == 'msw'){
      if (document.forms[0].jkt_size[descnum].value == "none"){alert('Please select size of sweatshirt.'); document.forms[0].jkt_size[descnum].focus; return;}
      if (descnum == 0){
         if (document.forms[0].front_designs1.selectedIndex < 0 && document.forms[0].back_designs1.selectedIndex < 0 && front.substr(0,6) == 'swt002'){alert('Please select price from list.'); document.forms[0].front_designs1.focus; return;}
         if ((document.forms[0].front_designs1.selectedIndex == 0 || document.forms[0].back_designs1.selectedIndex == 0) && front.substr(0,6) == 'swt002'){alert('Please select a design from list.'); document.forms[0].front_designs1.focus; return;}
      }
      if (descnum == 1){
         if (document.forms[0].front_designs2.selectedIndex < 0 && document.forms[0].back_designs2.selectedIndex < 0  && front.substr(0,6) == 'swt001'){alert('Please select price from list.'); document.forms[0].front_designs2.focus; return;}
         if ((document.forms[0].front_designs2.selectedIndex == 0 || document.forms[0].back_designs2.selectedIndex == 0) && front.substr(0,6) == 'swt001'){alert('Please select a design from list.'); document.forms[0].front_designs2.focus; return;}
      }
      if (descnum == 2){
         if (document.forms[0].front_designs3.selectedIndex < 0 && document.forms[0].back_designs3.selectedIndex < 0  && front.substr(0,6) == 'msw002'){alert('Please select price from list.'); document.forms[0].front_designs3.focus; return;}
	 if ((document.forms[0].front_designs3.value == "none") && front.substr(0,6) == 'msw002'){alert('Please select a design from list.'); document.forms[0].front_designs3.focus; return;}
      }
      if (descnum == 3){
         if (document.forms[0].front_designs4.selectedIndex < 0 && document.forms[0].back_designs4.selectedIndex < 0 && front.substr(0,6) == 'msw001'){alert('Please select price from list.'); document.forms[0].front_designs4.focus; return;}
         if ((document.forms[0].front_designs4.value == "none") && front.substr(0,6) == 'msw001'){alert('Please select a design from list.'); document.forms[0].front_designs4.focus; return;}
      }
   }

   if (front.substr(7,6) == 'bldcar' || back.substr(7,6) == 'bldcar'){
      if ((document.forms[0].caryear.value == '' || document.forms[0].model.value == ''
       || document.forms[0].carcolor.value == '' 
       || document.forms[0].intcolor.value == '') || ((document.forms[0].style[0].checked == true || document.forms[0].style[2].checked == true) && document.forms[0].topcolor.value == ''))
      {
//       alert("Interior color = " + document.forms[0].intcolor.value); 
       if (document.forms[0].style[1].checked == true && document.forms[0].topcolor.value == '')  
         {alert_info = '(Year, Model, Style, Car Color & Interior Color)'}
       else
         {alert_info = '(Year, Model, Style, Car Color, Interior Color & Top Color)'}
       document.forms[0].caryear.focus();
       alert("Please enter all car information.\n" + alert_info);
       return  
      }
      if (document.forms[0].style[0].checked == false && document.forms[0].style[1].checked == false && document.forms[0].style[2].checked == false){
        document.forms[0].style[0].focus();
        alert("Please click on style.");
        return
      }   
   } 

   gsize = document.forms[0].jkt_size[t_descnum].value;
   gcolor0 = c1; 
   c3 = c2;
   get_color_size(back); 
   gname = 'none';
   if (str == 'spt' || str == 'rac' || str == 'jkt'|| str == 'swt'){
      if (document.forms[0].jkt_name[t_descnum].value != ''){gname = document.forms[0].jkt_name[t_descnum].value}
   }
   gcolor = c2 + '*' + gcolor0 + '*' + c1;

   //Below handles T-Shirts with Sleeve Designs
   if (str == 'tsh'){
      if (front.substr(7,4) == 'poci' && descnum < 2){gcolor0 = 'Red'}
      if (front.substr(7,6) == 'roygto' && descnum < 2){gcolor0 = 'Blue'}
   // Below handles Grampa's and DAD's T-shirts
      flength = front.length;
      fleft = front.length - 6;
      if (front.substr(14,3) == 'gpa'){front = front.substr(0,3) + 'GPA' + front.substr(6,fleft)}
      if (front.substr(14,3) == 'dad'){front = front.substr(0,3) + 'DAD' + front.substr(6,fleft)}
   }

   //Below handles T-Shirts without Sleeve Designs
   if (str == 'tsw'){
      if (document.forms[0].jkt_size[descnum].value == "none"){alert('Please select size of shirt.'); document.forms[0].jkt_size[descnum].focus; return;}
   }
   if (str == 'jkt'){
      gitem = front.substr(0,13) + 'F-' +  back.substr(7,6) + 'B';
   }
   else {
         gitem = front.substr(0,13);
         gcolor = c3 + '*' + gcolor0;
        }  

   // On Jacket Orders, 'mid' = not selected (front or back)
   if (front.substr(10,3) == "mid" && str == 'jkt'){
      gitem = back.substr(0,13) + 'B';
      gcolor = c2 + '*' + c1;
   }      

   if (back.substr(10,3) == "mid" && str == 'jkt'){
      gitem = front.substr(0,13) + 'F';
      gcolor = c3 + '*' + gcolor0;
   }

   //Below handles Sweatshirts
   if (str == 'swt'){
      gcolor = c3 + '*' + gcolor0;
      if (front.substr(14,2) == "lg"){
         gitem = front.substr(0,13) + 'L'
      }
      else{
           gitem = front.substr(0,13) + 'S'
          }
   }
   //Below handles Denim Shirts
   if (finddnm > 0){descnum = 5} 

   document.forms[0].sel_item.value = gitem;
   document.forms[0].prevscrn.value = save_store_page;
   document.forms[0].short_desc.value = descnum;
   document.forms[0].size.value = gsize;
   document.forms[0].name.value = gname;
   document.forms[0].color.value = gcolor;

if (CartWindow.location){
  CartWindow.close();
  document.forms[0].cart_open_status.value = 'not_open'; 
}

//Assemble Car info 
   if (save_store_page.substr(0,4) != 'club' && str != 'tsw'){ 
      save_car();
   }

//Submit Form...
   if (str == 'tsw'){
      document.forms[0].price.value = tshirtw_price[descnum];
      document.forms.tshirt_display.submit();
   } 
   if (str == 'tsh'){
      document.forms[0].price.value = tshirt_price[descnum];
      document.forms.tshirt_display.submit();
   } 
   if (str == 'spt' || str == 'rac' || str == 'msp'){
      document.forms[0].price.value = shirt_price[descnum];
      document.forms.shirt_display.submit();
   } 
   if (str == 'swt' || str == 'msw'){
      document.forms[0].price.value = swt_price[descnum];
      document.forms.sweatshirt_display.submit();
   } 
   if (str == 'jkt'){
      document.forms[0].price.value = jkt_price[descnum];
      if (front.substr(0,6) == 'jkt006'){document.forms[0].price.value = jkt_price[5]}
      document.forms.jacket_display.submit();
   } 
}

function get_color_size(item) {

//c1 = Thread, C2 = Garment 
    c1 = 'Std';
    c2 = 'Std';
//alert('item.substr(21,3) = ' + item.substr(21,3));
    if (item.substr(17,3) == "wht"){c1 = 'White'}
    if (item.substr(17,3) == "red"){c1 = 'Red'}               
    if (item.substr(17,3) == "blk"){c1 = 'Black'}
    if (item.substr(17,3) == "wbk"){c1 = 'White-Blk'}
    if (item.substr(17,3) == "bwh"){c1 = 'Black-Wht'}
    if (item.substr(21,3) == "wht"){c2 = 'White'}
    if (item.substr(21,3) == "blk"){c2 = 'Black'}
    if (item.substr(21,3) == "blu"){c2 = 'Blue'}
    if (item.substr(21,3) == "kki"){c2 = 'Khaki'}
    if (item.substr(21,3) == "grn"){c2 = 'Green'}
    if (item.substr(21,3) == "nvy"){c2 = 'Navy'}
    if (item.substr(21,3) == "ppl"){c2 = 'Purple'}
    if (item.substr(21,3) == "red"){c2 = 'Red'}
    if (item.substr(21,3) == "org"){c2 = 'Orange'}
    if (item.substr(21,3) == "gry"){c2 = 'Grey'}
    if (item.substr(21,3) == "sgr"){c2 = 'Sports-Grey'}
    if (item.substr(21,3) == "bur"){c2 = 'Burgandy'}
    if (item.substr(21,3) == "ldn"){c2 = 'Light-Denim'}  
// Below are for POCI#5

// Below are for T-Shirts
    if (item.substr(14,3) == "wht"){c2 = 'White'}
    if (item.substr(14,3) == "gry"){c2 = 'Sports-Grey'}
    if (item.substr(14,3) == "blk"){c2 = 'Black'}
    if (item.substr(14,3) == "bur"){c2 = 'Burgandy'}
    if (item.substr(14,3) == "tye"){c2 = 'Blue/Tye-Dye'}
}

// function recalculate_order()
//
// This function recalculates the shopping cart totals.

function recalculate_order() {

    newqty = '';
    // Loop through the fields in the shopping cart form
    for (var counter = 0; counter < document.shopping_cart.length; counter++) {

        // Only work with the "Quantity" text fields
        if (document.shopping_cart[counter].type == "text") {
      
            // Is the value empty or negative?
            current_quantity = document.shopping_cart[counter].value
            if (its_empty(current_quantity) || parseInt(current_quantity) < 0) {
                
                // If so, warn the user, set the focus(), and return
                alert("Please enter a quantity greater than or equal to 0.")
                document.shopping_cart[counter].focus()
                return
            }
            
            // Is the value a floating-point number?
            if (its_floating_point(current_quantity)) {
                
                // If so, warn the user and return
                alert("Please enter an integer quantity.")
                document.shopping_cart[counter].focus()
                return
            }
            // Is the value all digits?
            if (!its_got_characters(current_quantity)) {
                
                // If there are characters, warn the user and return
                alert("Please enter a number.")
                document.shopping_cart[counter].focus()
                return
            }
        newqty += document.shopping_cart[counter].value + ' ';
        }
    }


// save to hidden fields
document.forms[0].nqty.value = newqty;

//Submit Form
  document.forms.shopping_cart.submit();         

}

// function remove_from_cart(record_index)
//
// This function removes the item.

function remove_from_cart(row) {

document.forms[0].del_row.value = row;
recalculate_order();

}

function cart_close(close_switch) {
//   if (!CartWindow == ''){
//     CartWindow = '';
//   }  
//   alert('cart_open_status = ' + document.forms[0].cart_open_status.value);
 
//    if (!CartWindow.closed && CartWindow.location){
   if (CartWindow.location){
      //    CartWindow.focus();
      CartWindow.close();
      document.forms[0].cart_open_status.value = 'not_open'; 
   }
   else {
      if (document.forms[0].cart_open_status.value == "refreshed"){
        alert('Please close View Cart Window before continuing');
        close_switch = '0';
      }
   }
   if (close_switch == '1'){
      document.forms.tshirt_display.submit();
   } 
   if (close_switch == '3'){
      document.forms.hat_display.submit();
   } 
   if (close_switch == '4'){
      document.forms.jacket_display.submit();
   } 
   if (close_switch == '5'){
      document.forms.shirt_display.submit();
   } 
   if (close_switch == '6'){
      document.forms.sweatshirt_display.submit();
   }
}
// End

