function ProductPricing(reftype,refid,quoteDest,form){
  this.content=ce('div'); 
  this.editor=new HTMLParse(this.content);
  this.refType=reftype;
  this.refID=refid;  
  this.selectedOptions= new Array();
  this.priceFormList= new Array();
  this.data=null;
  this.openedOptions = new Array();
  this.quoteDest=quoteDest;
  this.res=null;
  this.quoteForm=form;
    var instance=this;
    ajaxCallFunction("ajax.iml?mdl=pricing/product.aj&Ref_Type="+this.refType+"&Ref_ID="+this.refID+"&"+Date(),function(sc,st,rt,rxml){
        instance.res=new HTMLParse(getTagValue(rxml,'resource'));      
        instance.processXML(rxml.getElementsByTagName('product')[0]);
        if(instance.selectedForm!=null){  instance.selectedForm(); }
      });

  this.selectedForm=null;
  var instance=this;
  this.menu = new Array();
  this.menu[this.menu.length]= new popOutMenuItem("Summary",this.content,function(){instance.displayCategoryTree();});
  this.menu[this.menu.length]= new popOutMenuItem("Base Pricing",this.content,function(){instance.baseForm();});
  this.menu[this.menu.length]= new popOutMenuItem("Option Pricing",this.content,function(){instance.selectedOptions=new Array();instance.simpleForm();});
  this.menu[this.menu.length]= new popOutMenuItem("Help",this.content,function(){instance.help();});
}

ProductPricing.prototype.processXML=function(xml){  
  this.res.editor[0].pricingForm[0].refID[0].node.value=this.refID;
  this.res.editor[0].pricingForm[0].refType[0].node.value=this.refType;        

  var x, y, z;
  var i, j, k;
  var rowIndex;
  this.quoteDest.innerHTML="";
  this.quote=new HTMLParse(this.quoteDest);
  this.quote.appendChild(this.res.quote[0].node.childNodes[0],true);  
  this.data=new Object();
  this.data.minQty=getTagValue(xml,'min_qty')
  if(getTagValue(xml,'pricingmethod')=="marginal")  this.data.marginal=true; else this.data.marginal=false;
  this.data.options=new Array();  
  x=xml.getElementsByTagName('option');
  for(i=0;i<x.length;i++){
    this.data.options[i]=new Object();
    this.data.options[i].id=x[i].getAttribute('id');
    this.data.options[i].question=getTagValue(x[i],'question');
    this.data.options[i].choices=new Array();
    y=x[i].getElementsByTagName('choice');
    for(j=0;j<y.length;j++){
      this.data.options[i].choices[j]=new Object();
      this.data.options[i].choices[j].id=y[j].getAttribute('id');
      this.data.options[i].choices[j].value=y[j].firstChild.nodeValue;      
      if(this.data.options[i].choices[j].id==null)this.data.options[i].choices[j].id=this.data.options[i].choices[j].value;
    }
  }
  this.data.quantity=this.quote.breakdown[0].quantity[0].node;
  this.data.total=this.quote.total[0].node;
  this.data.totalEach=this.quote.totalEach[0].node;

  this.data.pricing=new Array();
  this.data.categories=new Array();
  x=xml.getElementsByTagName('category');
  for(i=0;i<x.length;i++){
    this.data.categories[i]=new Object();
    this.data.categories[i].id=x[i].getAttribute('id');
    this.data.categories[i].cumulativeTotal=0;
    this.data.categories[i].title=getTagValue(x[i],'title');
    this.data.categories[i].type=getTagValue(x[i],'type'); // "header" or "sub group"
    this.data.categories[i].pricing=new Array();    
    
    y=x[i].getElementsByTagName('pricing');
    if(y.length>0){
      this.quote.breakdown[0].appendObject(this.res.quoteline[(this.data.categories[i].type=="Header"?0:1)],true);
      rowIndex=this.quote.breakdown[0].quoteline.length-1;
      if(this.data.categories[i].type=="Header")  this.quote.breakdown[0].quoteline[rowIndex].title[0].node.innerHTML=this.data.categories[i].title;
      else this.quote.breakdown[0].quoteline[rowIndex].title[0].node.innerHTML="+ "+this.data.categories[i].title;
      this.data.categories[i].catTotal=this.quote.breakdown[0].quoteline[rowIndex].value[0].node;
      this.data.categories[i].catTotal.name=this.data.categories[i].title.replace(' ','_');
      this.data.categories[i].catTotal.value=0;
    }else{
      this.data.categories[i].catTotal=new Object();
    }
    for(j=0;j<y.length;j++){
      this.data.categories[i].pricing[j]=new Object();
      this.data.categories[i].pricing[j].id=y[j].getAttribute('id');
      this.data.categories[i].pricing[j].category=this.data.categories[i].title;
	  this.data.categories[i].pricing[j].minOrderTotal=Number(getTagValue(y[j],'min_order_total'));
      this.data.categories[i].pricing[j].setupCharge=Number(getTagValue(y[j],'setup_charge'));
      this.data.categories[i].pricing[j].setupPercent=Number(getTagValue(y[j],'setup_percent'));      
      this.data.categories[i].pricing[j].subPricePerItem=Number(getTagValue(y[j],'sub_price_per_item'));      
      this.data.categories[i].pricing[j].options=new Array();
      z=y[j].getElementsByTagName('option_choice');
      for(k=0;k<z.length;k++){
        this.data.categories[i].pricing[j].options[k]=new Object();
        this.data.categories[i].pricing[j].options[k].id=z[k].getAttribute('option_id');
        this.data.categories[i].pricing[j].options[k].choiceId=z[k].getAttribute('choiceid');
        if(this.data.categories[i].pricing[j].options[k].choiceId==null)
           this.data.categories[i].pricing[j].options[k].choiceId=z[k].firstChild.nodeValue;
      }    
      this.data.categories[i].pricing[j].breaks=new Array();
      z=y[j].getElementsByTagName('break');
      for(k=0;k<z.length;k++){
        this.data.categories[i].pricing[j].breaks[k]=new Object();
        this.data.categories[i].pricing[j].breaks[k].qty=Number(z[k].getAttribute('qty'));
        this.data.categories[i].pricing[j].breaks[k].value=Number(z[k].firstChild.nodeValue);    
      }
      this.data.pricing[this.data.pricing.length]=this.data.categories[i].pricing[j];
    }
  }
  if(this.quoteForm) this.recalculateForm();
}

ProductPricing.prototype.open=function(evt){
  var evt1=new Evt(evt);
  this.selectedForm=this.baseForm;
  myPopOut.openMenu(evt,this.menu,400);
}


ProductPricing.prototype.loading=function(){
  for(var i=0; i<this.priceFormList.length; i++){
    if(this.priceFormList[i].parent){
      this.priceFormList[i].parent.removeChild(this.priceFormList[i]);      
    }      
  }
  this.priceFormList= new Array();
  this.content.innerHTML="";  this.editor=new HTMLParse(this.content); 
  this.editor.appendObject(this.res.editor[0].loading[0],true);
}

ProductPricing.prototype.getOptionByID=function(id){
  for(var i=0;i<this.data.options.length;i++){
    if(this.data.options[i].id==id) return this.data.options[i];
  }
  return null;  
}
ProductPricing.prototype.getChoiceByID=function(id){
  for(var i=0;i<this.data.options.length;i++){
    for(var j=0; j<this.data.options[i].choices.length; j++){
       if(this.data.options[i].choices[j].id==id) return this.data.options[i].choices[j];
    }
  }
  return null;  
}

ProductPricing.prototype.findPricingByOptions=function(options){  
  var result=null;  
  var i, j, k,m;
  var optionOk, optionFound, pricingOk;
  for(i=0; i<this.data.categories.length; i++){
   for(j=0; j<this.data.categories[i].pricing.length; j++){
    pricingOk=true;   
    if(this.data.categories[i].pricing[j].options.length!=options.length)  pricingOk=false;
    else{
      for(k=0; k<this.data.categories[i].pricing[j].options.length&&pricingOk; k++){
        optionOk=true;
        optionFound=false;
        for(m=0; m<options.length&&!optionFound&&optionOk; m++){
          if(options[m].id==this.data.categories[i].pricing[j].options[k].id){
            optionFound=true;
            if(options[m].choiceId!=this.data.categories[i].pricing[j].options[k].choiceId)  optionOk=false;
            break
          }          
        }
        if(!optionFound||!optionOk){ pricingOk=false; break; }
      }
    }
    if(pricingOk) return this.data.categories[i].pricing[j];
   }
  }
  return null;
}

ProductPricing.prototype.recalculateForm=function(form){
  if(!form) form=this.quoteForm;
  var options=new Array();
  var qty=0;
  var index=0;
  var i;
  var inputs=form.getElementsByTagName('input');
  for(i=0; i<inputs.length; i++){
    if(inputs[i].getAttribute('optionid')>0&&inputs[i].checked){
      options[index]=new Object();
      options[index].id=inputs[i].getAttribute('optionid');
      options[index].choiceId=inputs[i].getAttribute('choiceid');    
      index++;
    }else if(inputs[i].getAttribute('isQty')>0){
      qty=inputs[i].value;
    }else if(inputs[i].getAttribute('optionid')>0&&inputs[i].type.toLowerCase()=="text"){
      options[index]=new Object();
      options[index].id=inputs[i].getAttribute('optionid');
      options[index].choiceId=inputs[i].value;    
      index++;	
	}
  }
  inputs=form.getElementsByTagName('select');
  for(i=0; i<inputs.length; i++){
    if(inputs[i].getAttribute('optionid')>0){
      options[index]=new Object();
      options[index].id=inputs[i].getAttribute('optionid');
      options[index].choiceId=inputs[i].options[inputs[i].selectedIndex].getAttribute('choiceid');    
      index++;
    }else if(inputs[i].getAttribute('isQty')>0){
      qty=inputs[i].options[inputs[i].selectedIndex].value;
    }
  }    
  this.calculatePricing(qty,options);
}

/*************  Base Pricing ***********/
ProductPricing.prototype.baseForm=function(){
  this.loading();
  this.selectedForm=this.baseForm;
  if(this.data!=null){
    var options= new Array();
    this.content.innerHTML="";  this.editor=new HTMLParse(this.content); 
    this.editor.appendObject(this.res.editor[0].maindiv[0],true);
    this.showPricingForm(options,this.editor.maindiv[0].node);
    myPopOut.resize();
  }
}

/*************  Simple Options ***********/
ProductPricing.prototype.simpleForm=function(){
  this.loading();
  this.selectedForm=this.simpleForm;
  if(this.data!=null){  
    this.showOptionList();
  }
}

ProductPricing.prototype.showOptionList=function(){
    var instance=this;
    var row=null; var question=null; var choice=null; 
    this.content.innerHTML="";  this.editor=new HTMLParse(this.content); 
    this.editor.appendObject(this.res.editor[0].maindiv[0],true);

    var priceOptions= new Array();
    var div,div2,div3,i,j,choices,a,span;
    if(this.selectedOptions.length>0){
      this.editor.maindiv[0].appendObject(this.res.editor[0].advancedheader[0],true);
    }
    for(i=0; i<this.selectedOptions.length; i++){
      priceOptions[i]=this.selectedOptions[i];
      if(this.getChoiceByID(this.selectedOptions[i].choiceId)){
        row=new HTMLParse(this.res.editor[0].advancedOption[0].node,true)
        row.question[0].node.innerHTML=this.getOptionByID(this.selectedOptions[i].id).question;
        row.choice[0].node.innerHTML=this.getChoiceByID(this.selectedOptions[i].choiceId).value;
        this.editor.maindiv[0].advancedheader[0].optionList[0].appendObject(row);
      }
    }


    var poIndex=priceOptions.length;
    priceOptions[poIndex]=new Object();
    for(i=0; i<this.data.options.length; i++){
      priceOptions[poIndex].id=this.data.options[i].id;
      question=new HTMLParse(this.res.editor[0].questionHeader[0].node,true);
      question.question[0].node.innerHTML=this.data.options[i].question; 
      question.showHide[0].node.setAttribute('optionIndex',i);
      if(this.openedOptions[i]){
       question.showHide[0].node.onclick=function(){instance.hideSimplePricingChoices(this);};  question.showHide[0].node.innerHTML="Hide Choices";
      }else{
       question.showHide[0].node.onclick=function(){instance.showSimplePricingChoices(this);};  question.showHide[0].node.innerHTML="Show Choices";
      }
      this.editor.maindiv[0].appendObject(question);
      question=new HTMLParse(this.res.editor[0].choiceList[0].node,true);
      if(this.openedOptions[i]) question.node.style["display"]="";  question.node.id="SIMPLE_PRICING_"+i; 
      for(j=0;j<this.data.options[i].choices.length; j++){
        priceOptions[poIndex].choiceId=this.data.options[i].choices[j].id;
        choice=new HTMLParse(this.res.editor[0].choiceDiv[0].node,true);
        choice.choice[0].node.innerHTML=this.data.options[i].choices[j].value;
        choice.edit[0].node.onclick=function(){instance.editSimplePricing(this);};
        choice.edit[0].node.setAttribute('optionIndex',i); choice.edit[0].node.setAttribute('choiceIndex',j);
        choice.advanced[0].node.onclick=function(){instance.editConditionalPricing(this);};
        choice.advanced[0].node.setAttribute('optionID',priceOptions[poIndex].id); 
        choice.advanced[0].node.setAttribute('choiceID',priceOptions[poIndex].choiceId);
        choice.pricing[0].node.id="SIMPLE_PRICING_"+i+"_"+j; 
        this.showPricing(priceOptions,choice.pricing[0].node);
        question.appendObject(choice);
      }
      this.editor.maindiv[0].appendObject(question);
    }
    myPopOut.resize();
}
ProductPricing.prototype.editConditionalPricing=function(a){
  var i=this.selectedOptions.length;
  this.selectedOptions[i]=new Object();
  this.selectedOptions[i].id=a.getAttribute('optionID');
  this.selectedOptions[i].choiceId=a.getAttribute('choiceID');
  this.showOptionList();
}

ProductPricing.prototype.editSimplePricing=function(a){
  var i=a.getAttribute('optionIndex'); j=a.getAttribute('choiceIndex');
  var dest=el("SIMPLE_PRICING_"+i+"_"+j);
  var priceOptions= new Array();
  for(var x=0; x<this.selectedOptions.length; x++){
    priceOptions[x]=this.selectedOptions[x];
  }
  var poIndex=priceOptions.length;
  priceOptions[poIndex]=new Object();
  priceOptions[poIndex].id=this.data.options[i].id;
  priceOptions[poIndex].choiceId=this.data.options[i].choices[j].id;
  this.showPricingForm(priceOptions,dest);
  var instance=this;
  a.onclick=function(){instance.closeSimplePricing(this);}; a.innerHTML="Close";
  myPopOut.resize();
}

ProductPricing.prototype.closeSimplePricing=function(a){
  var i=a.getAttribute('optionIndex'); j=a.getAttribute('choiceIndex');
  var dest=el("SIMPLE_PRICING_"+i+"_"+j);
  var priceOptions= new Array();
  for(var x=0; x<this.selectedOptions.length; x++){
    priceOptions[x]=this.selectedOptions[x];
  }
  var poIndex=priceOptions.length;
  priceOptions[poIndex]=new Object();
  priceOptions[poIndex].id=this.data.options[i].id;
  priceOptions[poIndex].choiceId=this.data.options[i].choices[j].id;
  this.showPricing(priceOptions,dest);
  var instance=this;
  a.onclick=function(){instance.editSimplePricing(this);};  a.innerHTML="Edit Pricing";
  myPopOut.resize();  
}

ProductPricing.prototype.showSimplePricingChoices=function(a){
  var i=a.getAttribute('optionIndex');
  el("SIMPLE_PRICING_"+i).style["display"]="";  
  var instance=this;
  this.openedOptions[i]=true;
  a.onclick=function(){instance.hideSimplePricingChoices(this);};  a.innerHTML="Hide Choices";
  myPopOut.resize();  
}
ProductPricing.prototype.hideSimplePricingChoices=function(a){
  var i=a.getAttribute('optionIndex');
  el("SIMPLE_PRICING_"+i).style["display"]="none";  
  var instance=this;
  this.openedOptions[i]=false;
  a.onclick=function(){instance.showSimplePricingChoices(this);};  a.innerHTML="Show Choices";
  myPopOut.resize();
}

/******************  Summary  ***********************/
ProductPricing.prototype.displayCategoryTree=function(){
  this.selectedForm=this.displayCategoryTree;
  if(!this.data){
    return;
  }
  var category,row, i,j,k,x;
  var pricingOptions;
  var optionTitle, choiceTitle, oldOptionTitle;
  var instance=this;
    this.content.innerHTML="";  this.editor=new HTMLParse(this.content); 
    this.editor.appendObject(this.res.editor[0].maindiv[0],true);

  if(this.data.pricing.length==0){
    this.editor.maindiv[0].node.innerHTML="No pricing information available.";
    return;
  }  
 
  
  for(i=0; i<this.data.categories.length; i++){
   if(this.data.categories[i].pricing.length>0){
    category=new HTMLParse(this.res.editor[0].summaryCategory[0].node,true); category.node.innerHTML=this.data.categories[i].title;
    for(j=0;j<this.data.categories[i].pricing.length; j++){      
          optionTitle="";
          for(k=0;k<this.data.categories[i].pricing[j].options.length;k++){
            if(optionTitle!="") optionTitle=optionTitle+" / ";
            if(this.getOptionByID(this.data.categories[i].pricing[j].options[k].id)){
              optionTitle=optionTitle+this.getOptionByID(this.data.categories[i].pricing[j].options[k].id).question;
            }
          }
          if(optionTitle!=oldOptionTitle){
            row=new HTMLParse(this.res.editor[0].summaryQuestion[0].node,true); row.node.innerHTML=optionTitle;
            category.appendObject(row);
            oldOptionTitle=optionTitle;
          }
      choiceTitle="";
      pricingOptions=new Array();
      for(k=0;k<this.data.categories[i].pricing[j].options.length;k++){         
        pricingOptions[k]=new Object();
        pricingOptions[k].id=this.data.categories[i].pricing[j].options[k].id;
        pricingOptions[k].choiceId=this.data.categories[i].pricing[j].options[k].choiceId;
        if(choiceTitle!="") choiceTitle=choiceTitle+" / ";
        if(this.getChoiceByID(this.data.categories[i].pricing[j].options[k].choiceId)){
          choiceTitle=choiceTitle+this.getChoiceByID(this.data.categories[i].pricing[j].options[k].choiceId).value;
        }
      }
      row=new HTMLParse(this.res.editor[0].summaryChoice[0].node,true);
      row.title[0].node.innerHTML=choiceTitle;
      row.edit[0].node.onclick=function(){instance.editSummaryPricing(this);};
      row.edit[0].node.setAttribute('catIndex',i);      
      row.edit[0].node.setAttribute('pricingIndex',j);
      row.deleteLink[0].node.onclick=function(){instance.deletePricing(this);}; 
      row.deleteLink[0].node.setAttribute('pricingID',this.data.categories[i].pricing[j].id);      
      row.pricingGrid[0].node.id="SUMMARY_PRICING_"+i+"_"+j;
      this.showPricing(pricingOptions,row.pricingGrid[0].node,true);  
      category.appendObject(row);
    }
    this.editor.maindiv[0].appendObject(category);
   }  
  }      
  myPopOut.resize();
}

ProductPricing.prototype.editSummaryPricing=function(a){
  var i=a.getAttribute('catIndex'); var j=a.getAttribute('pricingIndex');
  var dest=el("SUMMARY_PRICING_"+i+"_"+j);
  var pricingOptions=new Array();
  for(var x=0;x<this.data.categories[i].pricing[j].options.length;x++){         
    pricingOptions[x]=new Object();
    pricingOptions[x].id=this.data.categories[i].pricing[j].options[x].id;
    pricingOptions[x].choiceId=this.data.categories[i].pricing[j].options[x].choiceId;
  }
  this.showPricingForm(pricingOptions,dest);
  var instance=this;
  a.onclick=function(){instance.closeSummaryPricing(this);}; a.innerHTML="Close";
  myPopOut.resize();
}

ProductPricing.prototype.closeSummaryPricing=function(a){
  var i=a.getAttribute('catIndex'); var j=a.getAttribute('pricingIndex');
  var dest=el("SUMMARY_PRICING_"+i+"_"+j);
  var pricingOptions=new Array();
  for(var x=0;x<this.data.categories[i].pricing[j].options.length;x++){         
    pricingOptions[x]=new Object();
    pricingOptions[x].id=this.data.categories[i].pricing[j].options[x].id;
    pricingOptions[x].choiceId=this.data.categories[i].pricing[j].options[x].choiceId;
  }
  this.showPricing(pricingOptions,dest);
  var instance=this;
  a.onclick=function(){instance.editSummaryPricing(this);}; a.innerHTML="Edit";
  myPopOut.resize();
}

/******************  Calculate Price  ***********************/
ProductPricing.prototype.calculatePricing=function(qty,options){
  var subTotal;
  this.data.quantity.value=qty;
  this.data.total.value=0;
  this.data.totalEach.value=0;
  var i, j, k,x,  breaks;
  for(i=0; i<this.data.categories.length; i++){
    if(i>0) this.data.categories[i].cumulativeTotal=this.data.categories[i-1].cumulativeTotal;    
    else this.data.categories[i].cumulativeTotal=0;
    this.data.categories[i].catTotal.value=0;
    for(j=0; j<this.data.categories[i].pricing.length; j++){
      if(this.matchPricingOptions(this.data.categories[i].pricing[j].options,options)){
		  if(this.data.categories[i].pricing[j].minOrderTotal!=null&&this.data.categories[i].pricing[j].minOrderTotal>0) {
		  	  if(this.data.categories[i].cumulativeTotal>this.data.categories[i].pricing[j].minOrderTotal){
				  this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+this.data.categories[i].pricing[j].setupCharge).toFixed(2);
		          this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+this.data.categories[i].pricing[j].setupPercent*this.data.categories[i].cumulativeTotal/100).toFixed(2);
		          if(this.data.marginal)  subTotal=this.marginalPricing(qty,this.data.categories[i].pricing[j]);
		          else subTotal=this.simplePricing(qty,this.data.categories[i].pricing[j]);
		          this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+subTotal).toFixed(2);
			  }
		  }else{
		  	  this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+this.data.categories[i].pricing[j].setupCharge).toFixed(2);
	          this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+this.data.categories[i].pricing[j].setupPercent*this.data.categories[i].cumulativeTotal/100).toFixed(2);
	          if(this.data.marginal)  subTotal=this.marginalPricing(qty,this.data.categories[i].pricing[j]);
	          else subTotal=this.simplePricing(qty,this.data.categories[i].pricing[j]);
	          this.data.categories[i].catTotal.value=(Number(this.data.categories[i].catTotal.value)+subTotal).toFixed(2);
		  }
      }
    }
    this.data.categories[i].cumulativeTotal+=Number(this.data.categories[i].catTotal.value);
    this.data.total.value=this.data.categories[i].cumulativeTotal.toFixed(2);
  }  
  if(qty>0) this.data.totalEach.value=(this.data.total.value/qty).toFixed(3);
  else this.data.totalEach.value=0;
}

ProductPricing.prototype.marginalPricing=function(qty,pricing){
   var total=0;
   var previousBreak=0;
   var previousPrice=pricing.subPricePerItem;
   total=pricing.subPricePerItem*this.data.minQty;
   previousBreak=this.data.minQty;
   for(x=0;x<pricing.breaks.length&&qty-previousBreak>0;x++){
      if(qty>=pricing.breaks[x].qty){
        total+=pricing.breaks[x].value*(pricing.breaks[x].qty-previousBreak);
        previousBreak=pricing.breaks[x].qty
      }else{         
         total+=pricing.breaks[x].value*(qty-previousBreak);
         previousBreak=qty
         break
      }
      previousPrice=pricing.breaks[x].value; 
   }
   total+=previousPrice*(qty-previousBreak);
   return total;
}

ProductPricing.prototype.simplePricing=function(qty,pricing){
   var unitPrice=pricing.subPricePerItem;
   for(x=0;x<pricing.breaks.length;x++){
      if(qty>=pricing.breaks[x].qty) unitPrice=pricing.breaks[x].value; 
   }
   return qty*unitPrice;
}

ProductPricing.prototype.matchPricingOptions=function(pOptions,options){
  var optionID,choice,found;
  var i,j;
  for(i=0;i<pOptions.length;i++){
    found=false;
    for(j=0; j<options.length; j++){
      if(options[j].id==pOptions[i].id){
        found=true;
        if(options[j].choiceId!=pOptions[i].choiceId) return false;
        break;
      }      
    }  
    if(!found) return false;   
  }
  return true;
}

/******************  Help  ***********************/
ProductPricing.prototype.help=function(){
    var instance=this;
    this.loading();
    ajaxCallFunction("ajax.iml?mdl=pricing/help.mdl&Ref_Type="+this.refType+"&Ref_ID="+this.refID+"&"+Date(),function(sc,st,rt,rxml){
        instance.content.innerHTML=rt;
      }
    );  
}

/******************  General Pricing Forms/Displays ********************/
ProductPricing.prototype.showPricingForm=function(options,dest){
  dest.innerHTML="";
  var pricing=this.findPricingByOptions(options);  
  var category=(pricing!=null)?pricing.category:'';
  var pricingID=(pricing!=null)?pricing.id:"";
  var breaks=(pricing!=null)?pricing.breaks:null;  
  var input, div,block,row, text,i,j,option;
  var qty,priceEach;
  var instance=this;
  var listID=this.priceFormList.length;
  this.priceFormList[listID]=new HTMLParse(this.res.editor[0].pricingForm[0].node,true);
  this.priceFormList[listID].node.name="PRICING_FORM_"+listID; this.priceFormList[listID].node.id="PRICING_FORM_"+listID;
    this.priceFormList[listID].prID[0].node.value=pricingID;
    this.priceFormList[listID].prBreakCount[0].node.value=(breaks?breaks.length:0);
    this.priceFormList[listID].prOptionCount[0].node.value=options.length;
    for(i=0; i<options.length;i++){
      this.priceFormList[listID].appendObject(this.res.editor[0].pricingHelpers[0].advancedOptions[0],true);
        this.priceFormList[listID].advancedOptions[i].advOptionID[0].node.name="PRO_"+i+"_Option_ID";
        this.priceFormList[listID].advancedOptions[i].advOptionID[0].node.value=options[i].id;
        this.priceFormList[listID].advancedOptions[i].advChoice[0].node.name="PRO_"+i+"_Choice";
        this.priceFormList[listID].advancedOptions[i].advChoice[0].node.value=options[i].choiceId;
    }
    
    for(i=0; i<this.data.categories.length;i++){
      if(category==this.data.categories[i].title) j=i;
      option=ce('option'); option.innerHTML=this.data.categories[i].title; option.value=this.data.categories[i].id;
      this.priceFormList[listID].prCategory[0].node.appendChild(option);
    }       
    this.priceFormList[listID].prCategory[0].node.selectedIndex=j;
    this.priceFormList[listID].prSetupCharge[0].node.value=(pricing!=null)?pricing.setupCharge:'';
    this.priceFormList[listID].prSetupPercent[0].node.value=(pricing!=null)?pricing.setupPercent:''; 
    
    if(this.data.marginal){
      this.priceFormList[listID].pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridHeader[1],true);
    }else{    
      this.priceFormList[listID].pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridHeader[0],true);
    }

   this.priceFormList[listID].pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridRow[0],true);
     this.priceFormList[listID].pricingGrid[0].gridRow[0].minQty[0].node.innerHTML=(this.data.minQty>0)?this.data.minQty+" items":"1 item";
     this.priceFormList[listID].pricingGrid[0].gridRow[0].prSubPricePerItem[0].node.value=(pricing!=null)?pricing.subPricePerItem:"";
  if(breaks){
   for(i=0; i<breaks.length; i++){  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(i,breaks[i].qty,breaks[i].value)); }   
  }  
  this.priceFormList[listID].addBreakPoint[0].node.onclick=function(){instance.addPriceBreak(listID);return false;};
  this.priceFormList[listID].prSubmit[0].node.onclick=function(){instance.submitPricing(listID);};
  dest.appendChild(this.priceFormList[listID].node); 
}

ProductPricing.prototype.submitPricing=function(listID){
  var instance=this;
  ajaxFormFunction("PRICING_FORM_"+listID,function(sc,st,rt,rxml){
        instance.loading();
        instance.res=new HTMLParse(getTagValue(rxml,'resource'));              
        instance.processXML(rxml.getElementsByTagName('product')[0]);
        if(instance.selectedForm!=null){  instance.selectedForm(); }
      }
    );  
}

ProductPricing.prototype.showPricing=function(options,dest,summary){
  dest.innerHTML="";
  var destObj=new HTMLParse(this.res.editor[0].pricingDiv[0].node,true);
  var pricing=this.findPricingByOptions(options);  
 if(pricing!=null){
  var category=pricing.category;
       if(category=="")category="Base Pricing";
  var breaks=pricing.breaks;
  var input, div,block,row, text,i,j,option;
  var qty,priceEach;
  var instance=this;
  if(!summary){  
    destObj.nonSummary[0].node.style["display"]=''; destObj.nonSummary[0].category[0].node.innerHTML=category; 
  }
  if(pricing.setupPercent>0) destObj.setup[0].node.innerHTML=pricing.setupPercent+"%"; 
  else destObj.setup[0].node.innerHTML="$"+pricing.setupCharge; 
   if(!summary){
    if(this.data.marginal){
      destObj.pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridHeader[1],true);
    }else{    
      destObj.pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridHeader[0],true);
    }
   }
     destObj.pricingGrid[0].appendObject(this.res.editor[0].pricingHelpers[0].gridRow[2],true);
     destObj.pricingGrid[0].gridRow[0].prbQty[0].node.innerHTML=(this.data.minQty>0)?this.data.minQty+" items":"1 item";
     destObj.pricingGrid[0].gridRow[0].prbSubPricePerItem[0].node.innerHTML=(pricing!=null)?pricing.subPricePerItem:"";
  if(breaks){
   for(i=0; i<breaks.length; i++){ 
     row=new HTMLParse(this.res.editor[0].pricingHelpers[0].gridRow[2].node,true);
     row.prbQty[0].node.innerHTML=breaks[i].qty+" items";
     row.prbSubPricePerItem[0].node.innerHTML=breaks[i].value;
     destObj.pricingGrid[0].appendObject(row);
   }   
  }  
  dest.appendChild(destObj.node); 

 } 
}

ProductPricing.prototype.createQtyBreak=function(id,qty,price){
      var result=new HTMLParse(this.res.editor[0].pricingHelpers[0].gridRow[1].node,true);
      result.prbQty[0].node.name="PRB_"+id+"_Qty"; result.prbQty[0].node.value=qty;
      result.prbSubPricePerItem[0].node.name="PRB_"+id+"_Sub_Price_Per_Item"; result.prbSubPricePerItem[0].node.value=price;
     return result;
}

ProductPricing.prototype.addPriceBreak=function(listID){
  var id=Number(this.priceFormList[listID].prBreakCount[0].node.value);
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'500',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'1000',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'2500',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'5000',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'7500',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'10000',''));
  this.priceFormList[listID].pricingGrid[0].appendObject(this.createQtyBreak(id++,'20000',''));
  this.priceFormList[listID].prBreakCount[0].node.value=id;
  myPopOut.resize();
}

ProductPricing.prototype.deletePricing=function(a){
  var pricingID=a.getAttribute('pricingID');       
  if(pricingID!=""){
    if(confirm('You are about to delete this pricing record.  This action cannot be undone.  Do you want to continue?')){
      var instance=this;
      ajaxCallFunction("ajax.iml?mdl=pricing/delete.aj&Ref_Type="+this.refType+"&Ref_ID="+this.refID+"&PR_ID="+pricingID+"&"+Date(),function(sc,st,rt,rxml){
        instance.res=new HTMLParse(getTagValue(rxml,'resource'));      
        instance.processXML(rxml.getElementsByTagName('product')[0]);
        if(instance.selectedForm!=null){  instance.selectedForm(); }
        }
      );       
    }
  } 
}
