//alert("Product_input.js V10.05.14");

 function Attr(type,code,name,show_color,womens_name) {
   this.type=type;
   this.code=code;
   this.name=name;
   this.show_color=show_color;
   this.womens_name=womens_name;
 }

   

    
 function commaIf(t, suffix) {
   return t==''?'':', '+t+' '+suffix;
 }
 /* waht abug */

 function Variety(pNbr,size,color,buttons,trim,price) {
     this.pNbr=pNbr;
     this.size=size;
     this.color=color;
     this.buttons=buttons;
     this.trim=trim;
     this.price=price;

     this.putResult =function(product,target) {
         var te = document.getElementById("result_any");
         var say_buy=false;
         if(!te) {
             te = document.getElementById(target);
             var tt = document.getElementById("title_"+target);
             if(tt)tt.style.display="";
             say_buy=true;
         }
         var div = document.createElement("TR");
         var td=document.createElement("TD");
         td.className='product_name';
         td.appendChild(document.createTextNode(product.name));
         div.appendChild(td);
         td=document.createElement("TD");
         td.className='product_size';
         td.appendChild(document.createTextNode(prodattrs[this.size].name));
         div.appendChild(td);
         td=document.createElement("TD");
         td.className='product_variety';
         td.appendChild(document.createTextNode(prodattrs[this.color].name+commaIf(prodattrs[this.buttons].name,'buttons')
                   +commaIf(prodattrs[this.trim].name,'piping')));
         div.appendChild(td);

         td=document.createElement("TD");
         td.className='product_quantity';
         if(this.price==null)this.price=product.price;
         td.innerHTML="<input size=3 onkeyup='variety_calc(this,"+(100*this.price)+");'name="+this.pNbr+" id="+this.pNbr+" value=''>";
         div.appendChild(td);
         //<td class="product_price">$79.00</td><td class="product_price"><script>product_total('P1','$79.00');</script><div id="P1_total">$79.00</div></td>
         td=document.createElement("TD");
         td.className='product_price';
         td.innerHTML="$"+this.price;
         div.appendChild(td);
         td=document.createElement("TD");
         td.className='product_total';
         td.innerHTML="<div id='"+this.pNbr+"_total'>$00.00</div>";
         div.appendChild(td);
         td=document.createElement("TD");
         if(say_buy) {
             td.className='buy_now';
             td.innerHTML="<input id='buy_"+this.pNbr+"' class='buy_now_off' type='submit' value='Buy Now'/>";
             div.appendChild(td);
         }

         //+" Quantity:"));
         //var input=document.createElement("INPUT");
         //input.name=this.pNbr;
         //input.id=this.pNbr;
         //input.value="";
         //div.appendChild(input);
         //div.innerHTML="<td>"+name+"</td><td>"+this.size+"</td><td>"+this.color+", "+this.buttons+", "+this.trim
         //+"</td><td>Quantity:<input size=3 name="+this.pNbr+" id="+this.pNbr+" value=''></td>";
         te.parentNode.insertBefore(div,te);
     }

 }
 function variety_calc(field,price_in_cents) {
     calc(field,price_in_cents);
     //alert("calc"+typeof field.value+"="+field.value+", "+field.name);
     var e = document.getElementById('buy_'+field.name);
     if(e)e.className=field.value>0?'buy_now':'buy_now_off';
 }
     

 function show_chosen(name) {

 }
  function choose_product(value) {
      var target=document.getElementById('select_any');
      if(value==0)
          target.innerHTML='';
      else
          target.innerHTML=
          '<table width="100%"><tr><td rowspan="2"><img src="/images/'+products[value].icon+'"></td><td class="product_description" >'
          +products[value].description
          +'</td></tr><tr><td class="variety_selector">'
          +new Variety_selector(products[value].name).getSelector()
          +"</td></tr></table>";
  }
  function product_selector(site) {
      //Choose "+site+" to add to your order
       var t="<select onchange='choose_product(this.value);'><option value='0'>Continue Shopping</option>";
          for(i=1;i<products.length;i++) {
              t+='<option value="'+i+'">'+products[i].name+"</option>\n";
          }
          t+="</select>";
          //alert('getSelector='+t);
          document.write(t);
  }
     

     function Product(prodID,name,price,size_count,size_select_html,color_count,color_select_html,buttons_count,buttons_select_html,trim_count,trim_select_html,varieties
       ,icon,description) {
         this.prodID=prodID;
         this.name=name;
         this.price=price;
         this.size_select_html=size_select_html;
         this.color_select_html=color_select_html;
         this.buttons_select_html=buttons_select_html;
         this.trim_select_html=trim_select_html;
         this.size_count=size_count;
         this.color_count=color_count;
         this.buttons_count=buttons_count;
         this.trim_count=trim_count;
         this.varieties=varieties;
         this.chosen_size=null;
         this.chosen_color=null;
         this.chosen_buttons=null;
         this.chosen_trim=null;
         this.icon=icon;
         this.description=description;
         this.chosen = function(type,value) {
             if(value==0)value=null;
             if(value<0) {
                 this.chosen_size=null;
                 this.chosen_color=null;
                 this.chosen_buttons=null;
                 this.chosen_trim=null;
                 value=-value;
             }
             if(type=='size') this.chosen_size=value;
             else if(type=='color') this.chosen_color=value;
             else if(type=='buttons') this.chosen_buttons=value;
             else if(type=='trim') this.chosen_trim=value;
             recolor_sketch("recolor_"+this.id,this.chosen_color,this.chosen_trim,this.chosen_buttons);
         }
         this.show_result = function() {


             for(i=0;i<this.varieties.length;i++) {
                 var v=this.varieties[i];


                 if(
                 (this.size_count<2 || this.chosen_size==v.size)
                 && (this.color_count<2 || this.chosen_color==v.color)
                 && (this.buttons_count<2 || this.chosen_buttons==v.buttons)
                 && (this.trim_count<2 || this.chosen_trim==v.trim)
                 ) {
                         var e = document.getElementById(v.pNbr);
                         if(!e) {
                           v.putResult(this,"result_"+this.id);
                           e = document.getElementById(v.pNbr);
                         }
                         e.focus();
                         //alert("found");
                         return true;
                 }
             }
             return false;
         }
         this.choose = function(type,value) {
             this.chosen(type,value);
             if(this.show_result()) return;
                          // revise choices
             if(this.chosen_size==null)this.revise_selector('size');
             if(this.chosen_color==null)this.revise_selector('color');
             if(this.chosen_buttons==null)this.revise_selector('buttons');
             if(this.chosen_trim==null)this.revise_selector('trim');
             this.show_result();
         }
         this.revise_selector = function(type) {
             var e=document.getElementById('select_'+type+'_'+this.id);
             if(e){
                 e.innerHTML=this.getSelector(type);
                 //alert("Revise "+type+"="+this.getSelector(type));
             }
         }
         this.meDot = function() {
             if(!(this.myNumber)) {
                for(i=1;i<products.length;i++) {
                    if(this==products[i])break;
                }
                this.myNumber=i;
             }
             return "products["+this.myNumber+"].";
         };

         this.hasAttr = function(n) {
             var ex=null;
             for(j=0;j<this.varieties.length;j++) {
                 var v=this.varieties[j];
                 if( v.size==n || v.color==n || v.buttons==n || v.trim==n  ) {
                     if(this.size_count>1 && this.chosen_size!=null && v.size!=this.chosen_size)
                         ex=" (not available in "+prodattrs[this.chosen_size].name+")";
                     else if(this.color_count>1 &&this.chosen_color!=null && v.color!=this.chosen_color)
                         ex=" (not available in "+prodattrs[this.chosen_color].name+")";
                     else if(this.buttons_count>1 &&this.chosen_buttons!=null && v.buttons!=this.chosen_buttons)
                         ex=" (not available in "+prodattrs[this.chosen_buttons].name+" buttons)";
                     else if(this.trim_count>1 &&this.chosen_trim!=null && v.trim!=this.chosen_trim)
                         ex=" (not available in "+prodattrs[this.chosen_trim].name+" piping)";
                     else
                        return '';
                 }
             }
             return ex;
         }
         this.getSelector = function(type) {
             var type_name= type=='trim'?'piping':type;
              var t='';
              var first="<option SELECTED value='0'>Choose "+type_name+"</option>";
              var count=0;
              var only='';
              var seenChosen=false;
              for(i=1;i<prodattrs.length;i++) {
                  if(prodattrs[i].type==type) {
                      var has=this.hasAttr(i);
                      if(has=='') {
                        count++;
                        var isChosen=(this.chosen_size==i || this.chosen_color==i || this.chosen_buttons==i || this.chosen_trim==i);
                        if(isChosen)seenChosen=true;
                        only=i;
                        if(count==2 && !seenChosen)t=t.replace("SELECTED","");
                        t+='<option class="attribute_value" '+((count==1 || isChosen)?"SELECTED":"");
                        if(prodattrs[i].show_color)t+=' style="background:'+prodattrs[i].show_color+'" ';
                        t+=' value="'+i+'">';
                        if(this.prodID.substring(0,1)=='W' && prodattrs[i].womens_name!=null)
                            t+=prodattrs[i].womens_name;
                        else
                            t+=prodattrs[i].name;


                        t+="</option>\n";

                      } else if(has!=null)
                          t+='<option class="attribute_value_off"  value="'+(-i)+'">'+prodattrs[i].name+has+"</option>\n";
                      //t+='<option value="'+i+'">'+prodattrs[i].name+"</option>\n";
                  }
              }

              if(count==1)  this.chosen(type,only);
              if(count==1 ||seenChosen) first=first.replace("SELECTED","");
              

              //alert('getSelector='+t);
              return "<select class='choose_attribute' onchange='"+this.meDot()+"choose("+'"'+type+'"'+",this.value);'>"+first+t+"</select>";
         }
     }
     

     function setQuantity(pNbr,value,price_in_cents) {
         var e=document.getElementById(pNbr);
         //alert("setQ "+pNbr+" "+value);
         if(!e) {
             for(pi=1;pi<products.length;pi++) {
                 var vs=products[pi].varieties;
                 for(i=0;i<vs.length;i++) {
                     if(vs[i].pNbr==pNbr) {
                         vs[i].putResult(products[pi],"result_any");
                         e=document.getElementById(pNbr);
                         if(e)break;
                         alert("failed to setQuantity");
                     }
                 }
                 if(e)break;
             }
         }
         e.value=value;
         calc(e,price_in_cents);
     }
     function find_product(name) {
         for(i=1;i<products.length;i++) {
             var p = products[i];
             if(p.name==name) {
                 p.id='product_'+i;
                 return p;
             }
         }
         alert("unknown product '"+name+"'");
         return p;
     }
     function Variety_selector(name) {
         var p= name==''?null:find_product(name);
         this.product=p;
         this.getSelector = function() {
             p=this.product;
             p.chosen_size=null;
             p.chosen_trim=null;
             p.chosen_buttons=null;
             p.chosen_color=null;
             var generic = name.indexOf("Scrubs")>-1?"Scrubs":"Sleepwear";
             return p==null?"":
             ("<br><strong>Available in "+(p.size_count==1?p.size_select_html+" size":p.size_count+" sizes")
             +" in "+(p.color_count==1?p.color_select_html:p.color_count+" colors")
             +(p.buttons_count==0?"":" with "+(p.buttons_count==1?p.buttons_select_html:" your choice of buttons"))
             +(p.trim_count==0?"":" with "+(p.trim_count==1?p.trim_select_html:" and piping!"))
             +" $"+p.price
             +"</strong>"
             +(p.prodID=='WPSL' && null==document.getElementById("result_any")?
               "<br>Select your choice of colors using our <a href='/about/Design Your Own/Preview'>Designer</a>"
             : "<div id='"+p.id+"'><table><tr><td><table>"
                +(p.color_count<2?"":"<tr><td>Choose color:</td><td id='select_color_"+p.id+"'>"+p.getSelector('color')+"</td></tr>")
                +(p.buttons_count<2?"":"<tr><td>Choose buttons:</td><td id='select_buttons_"+p.id+"'>"+p.getSelector('buttons')+"</td></tr>")
                +(p.trim_count<2?"":"<tr><td>Choose piping:</td><td id='select_trim_"+p.id+"'>"+p.getSelector('trim')+"</td></tr>")
                +(p.size_count<2?"":"<tr><td>Choose size:</td><td id='select_size_"+p.id+"'>"+p.getSelector('size')+"</td></tr>")
               +"</table></td><td>"
               +recolor_template("recolor_"+p.id)
               +"</td></table></div>"
            )
               +"<table  width='95%' ><tr id='title_result_"+p.id+"' style='display:none'><th>"+generic+"</th><th>Size</th><th>Color</th><th>Quantity</th><th>Price</th><th>Total</th</tr><tr id='result_"+p.id+"'></tr></table>"
          );
         }
     }
     function new_variety_selector(name) {
        document.write(new Variety_selector(name).getSelector());
     }
    function recolor_sketch(prefix,color,trim,buttons) {
        var d = document.getElementById(prefix+"_div");
        if(d) {
            if(buttons){
                var img = document.getElementById(prefix+"_img");
                img.src ="/images/button_"+prodattrs[buttons].show_color.substring(1)+".png";
                img.style.display='inline';

            }
            if(trim)d.style.borderColor=prodattrs[trim].show_color;
            if(color){
                d.style.backgroundColor=prodattrs[color].show_color;
                if(!trim)
                    d.style.borderColor=d.style.backgroundColor;
            }
            if(trim || buttons || color) {
                //alert("debug: "+typeof d)
                d.style.display='inline';
            }
        }
    }
    function recolor_template(prefix) {
        return '<div id="'+prefix+'_div" style="width:.4in;height:.4in;background:white;border: thick solid black; border-left-width:0; border-top-width:0;display:none">'
              +'<image id="'+prefix+'_img" style ="margin:.1in .2in .2in .10in;display:none" ></div>';
          //xsrc="/images/button_000000.png"
    }


