//-------------------------------------------------- MVC Model -------------------------------------------
function TopOfferModel(aDom)
{
  this.reader=new ReaderFactory().createXmlReader(aDom);
  this.getTopOfferNodes=getTopOfferNodes;
  this.getTopOfferDestName=getTopOfferDestName;
  this.getHighestPrice=getHighestPrice;
  this.topOfferNodes=null;
  this.calculateOptimumNode=calculateOptimumNode;
  this.getTopOfferNode=getTopOfferNode;
  this.getTopListLength=getTopListLength;
  this.getOptimumOfferId=getOptimumOfferId;
  this.optimumNodes=new Array();
  this.getOptimumPriceText=getOptimumPriceText;
  this.getOfferId=getNormalOfferId;
  this.getPriceText=getNormalPriceText;
  this.initTopOfferModel=initTopOfferModel;
  this.calculateMaxOfferPrice=calculateMaxOfferPrice;
  this.highestPrice=0;
  this.initTopOfferModel();
}



function initTopOfferModel()
{
  var theNodes=this.getTopOfferNodes();
  if(theNodes != null)
  {
    var i;
    var theNode;
    var thePrice=0;
    for (i=0;i<theNodes.length;i++)
    {
      theNode = theNodes[i];
      thePrice = this.calculateMaxOfferPrice(theNode);
      if(thePrice>this.highestPrice)
      {
        this.highestPrice=thePrice;
      }
    }
  }
  //alert("highest price:"+this.highestPrice);
}

function calculateMaxOfferPrice(aNode)
{
  //alert("calculate max offer Price:"+aNode.nodeName);
  //alert(this.reader.printNodeList(aNode));
  var theNode = aNode; 
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  
  theNodeValue[0] = theReader.getText(theReader.getNode(theNodes[0],'sevendays.price'));
  theNodeValue[1] = theReader.getText(theReader.getNode(theNodes[1],'thirtydays.price'));
  theNodeValue[2] = theReader.getText(theReader.getNode(theNodes[2],'ninetydays.price'));
  var i=0;
  var theNo=0;
  var theValue = theNodeValue[0];
  //alert("theNodeValue:"+theValue);
  for (i=1;i<3;i++)
  {
    if(theNodeValue[i] > theNodeValue[i-1])
    {
      theNo=i;
      theValue=theNodeValue[i];
    }
  }
  return theValue;
}

function getNormalPriceText(aTopNum, aSub)
{
  var theNode = this.getTopOfferNode(aTopNum);
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  
  theNodeValue[0] = theReader.getText(theReader.getNode(theNodes[0],'sevendays.price'));
  theNodeValue[1] = theReader.getText(theReader.getNode(theNodes[1],'thirtydays.price'));
  theNodeValue[2] = theReader.getText(theReader.getNode(theNodes[2],'ninetydays.price'));
  
  return theNodeValue[aSub];
}


function getNormalOfferId(aTopNum, aSub)
{
  var theNode = this.getTopOfferNode(aTopNum);
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  
  theNodeValue[0] = theReader.getText(theReader.getNode(theNodes[0],'sevendays.offerid'));
  theNodeValue[1] = theReader.getText(theReader.getNode(theNodes[1],'thirtydays.offerid'));
  theNodeValue[2] = theReader.getText(theReader.getNode(theNodes[2],'ninetydays.offerid'));
  
  return theNodeValue[aSub];
}

function getTopListLength()
{
  if (this.getTopOfferNodes() != null)
    return this.getTopOfferNodes().length;
  else
    return 0;
}

function getTopOfferNode(aNumber)
{
  return this.getTopOfferNodes()[aNumber];
}

function getTopOfferNodes()
{
  if (this.topOfferNodes == null)
  {
    this.topOfferNodes = this.reader.getNodesByPath('flightRequestResponse.topoffer');
  }
  return this.topOfferNodes;
}


function getTopOfferDestName(anIndex)
{
  var theNodes = this.getTopOfferNodes();
  return this.reader.getText(this.reader.getNode(theNodes[i],'topoffer.ninetydays.from'));
}

function getHighestPrice()
{
  //return this.reader.getText(this.reader.getFirstNodeByPath('flightRequestResponse.listStatistics.highestPrice'));
  return this.highestPrice;
}

function calculateOptimumNode(aTopOfferNumber)
{
  if(this.optimumNodes.length>aTopOfferNumber && this.optimumNodes[aTopOfferNumber]!= null)
  {
    return this.optimumNodes[aTopOfferNumber];
  }
  
  var theNode = this.getTopOfferNode(aTopOfferNumber);
  
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  
  theNodeValue[0] = theReader.getText(theReader.getNode(theNodes[0],'sevendays.price'));
  theNodeValue[1] = theReader.getText(theReader.getNode(theNodes[1],'thirtydays.price'));
  theNodeValue[2] = theReader.getText(theReader.getNode(theNodes[2],'ninetydays.price'));
  var i=0;
  var theNo=0;
  for (i=1;i<3;i++)
  {
    if(theNodeValue[i] < theNodeValue[i-1])
    {
      theNo=i;
    }
  }
  
  this.optimumNodes[aTopOfferNumber] = theNodes[theNo];
  
  return theNodes[theNo];
}

function getOptimumOfferId(aNumber)
{
  var theOptimumNode = this.calculateOptimumNode(aNumber);
  var theOfferNode = this.reader.getNode(theOptimumNode,'node.offerId')
  //7alert("The offer id node:"+theOfferNode.nodeName);
  var theId = this.reader.getText(theOfferNode);
  return theId;
}

function getOptimumPriceText(aNumber)
{
  var theOptimumNode = this.calculateOptimumNode(aNumber);
  
  return this.reader.getText(this.reader.getNode(theOptimumNode,'node.price'));
}
// JavaScript Document
//------------------------------------------ Multi Offer Model --------------------------------
function MultiOfferModel(aDom)
{
  this.reader=new ReaderFactory().createXmlReader(aDom);
  this.getTopOfferNodes=getMultiTopOfferNodes;
  this.getTopOfferDestName=getMultiTopOfferDestName;
  this.getHighestPrice=getMultiHighestPrice;
  this.topOfferNodes=null;
  this.calculateOptimumNode=calculateMultiOptimumNode;
  this.getTopOfferNode=getMultiTopOfferNode;
  this.getTopListLength=getMultiTopListLength;
  this.getOptimumOfferId=getMultiOptimumOfferId;
  this.optimumNodes=new Array();
  this.getOptimumPriceText=getMultiOptimumPriceText;
  this.getOfferId=getMultiNormalOfferId;
  
  this.getPriceText=getMultiNormalPriceText;
  
  this.getOfferPrice=getOfferPriceText;
  this.initTopOfferModel=initTopOfferModel;
  this.calculateMaxOfferPrice=calculateMultiMaxOfferPrice;
  this.highestPrice=0;
  this.offerNodes=null;
  this.getAllOfferNodes=getMultiAllOfferNodes;

  this.initTopOfferModel();
}

function getMultiAllOfferNodes()
{
 if (this.offerNodes==null)
 {
  var theRoot=this.reader.getFirstNodeByPath("flightRequestResponse");
  var theOfferNodes=this.reader.getChildNodes(theRoot, "offer" );
  if (theOfferNodes != null)
  {
    var i;
    var theArray= new Array();
    for (i=0;i<theOfferNodes.length;i++)
    { 
      var theOfferId = this.reader.getNodeAttributeValue(theOfferNodes[i],"offerid");
      theArray[theOfferId]=theOfferNodes[i];
    }
    this.offerNodes=theArray;
    alert("All Offer nodes="+this.offerNodes.length);
  }
 }
 return this.offerNodes;
}

function getMultiNormalPriceText(anOfferId)
{
  var thePriceNode = this.reader.getChildNode(this.getAllOfferNodes()[anOfferId],"price");
  return this.reader.getText(thePriceNode);
}

function getOfferPriceText(anOfferId)
{
  var thePriceNode = this.reader.getChildNode(this.getAllOfferNodes()[anOfferId],"price");
  return this.reader.getText(thePriceNode);
}


function calculateMultiMaxOfferPrice(aNode)
{
  //alert("calculate max offer Price:"+aNode.nodeName);
  //alert(this.reader.printNodeList(aNode));
  var theNode = aNode; 
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  var theIds = new Array();
  theIds[0]=theReader.getNodeAttributeValue(theNodes[0],"offerid");
  theIds[1]=theReader.getNodeAttributeValue(theNodes[1],"offerid");
  theIds[2]=theReader.getNodeAttributeValue(theNodes[2],"offerid");
  
  
  theNodeValue[0] = this.getOfferPrice(theIds[0]);
  theNodeValue[1] = this.getOfferPrice(theIds[1]);
  theNodeValue[2] = this.getOfferPrice(theIds[2]);
  var i=0;
  var theNo=0;
  var theValue = theNodeValue[0];
  //alert("theNodeValue:"+theValue);
  for (i=1;i<3;i++)
  {
    if(theNodeValue[i] > theNodeValue[i-1])
    {
      theNo=i;
      theValue=theNodeValue[i];
    }
  }
  return theValue;
}

function getMultiNormalPriceText(aTopNum, aSub)
{
  var theNode = this.getTopOfferNode(aTopNum);
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');

  var theIds = new Array();
  theIds[0]=theReader.getNodeAttributeValue(theNodes[0],"offerid");
  theIds[1]=theReader.getNodeAttributeValue(theNodes[1],"offerid");
  theIds[2]=theReader.getNodeAttributeValue(theNodes[2],"offerid");
   
  
  theNodeValue[0] = this.getOfferPrice(theIds[0]);
  theNodeValue[1] = this.getOfferPrice(theIds[1]);
  theNodeValue[2] = this.getOfferPrice(theIds[2]);
  
  return theNodeValue[aSub];
}


function getMultiNormalOfferId(aTopNum, aSub)
{
  var theNode = this.getTopOfferNode(aTopNum);
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  var theIds = new Array();
  theIds[0]=theReader.getNodeAttributeValue(theNodes[0],"offerid");
  theIds[1]=theReader.getNodeAttributeValue(theNodes[1],"offerid");
  theIds[2]=theReader.getNodeAttributeValue(theNodes[2],"offerid");
  
  return theIds[aSub];
}


function getMultiTopListLength()
{
  if (this.getTopOfferNodes() != null)
    return this.getTopOfferNodes().length;
  else
    return 0;
}

function getMultiTopOfferNode(aNumber)
{
  return this.getTopOfferNodes()[aNumber];
}


function getMultiTopOfferNodes()
{
  if (this.topOfferNodes == null)
  {
    this.topOfferNodes = this.reader.getNodesByPath('flightRequestResponse.topOfferList');
  }
  return this.topOfferNodes;
}


function getMultiTopOfferDestName(anIndex)
{
  var theNode = this.getTopOfferNodes()[anIndex];
  var theNinetyDaysNode = this.reader.getChildNode(theNode,"ninetydays");
  
  var theOfferId = this.reader.getNodeAttributeValue(theNinetyDaysNode, "offerid");
  var theDestinationName =this.reader.getChildNode(this.getAllOfferNodes()[theOfferId], "to");
  return this.reader.getText(theDestinationName);
}

function getMultiHighestPrice()
{
  //return this.reader.getText(this.reader.getFirstNodeByPath('flightRequestResponse.listStatistics.highestPrice'));
  return this.highestPrice;
}

function calculateMultiOptimumNode(aTopOfferNumber)
{
  if(this.optimumNodes.length>aTopOfferNumber && this.optimumNodes[aTopOfferNumber]!= null)
  {
    return this.optimumNodes[aTopOfferNumber];
  }
  
  var theNode = this.getTopOfferNode(aTopOfferNumber);
  
  var theNodeValue = new Array();
  var theNodes = new Array();
  var theReader= this.reader;
  theNodes[0] = theReader.getNode(theNode,'topoffer.sevendays');
  theNodes[1] = theReader.getNode(theNode,'topoffer.thirtydays');
  theNodes[2] = theReader.getNode(theNode,'topoffer.ninetydays');
   
  var theIds = new Array();
  theIds[0]=theReader.getNodeAttributeValue(theNodes[0],"offerid");
  theIds[1]=theReader.getNodeAttributeValue(theNodes[1],"offerid");
  theIds[2]=theReader.getNodeAttributeValue(theNodes[2],"offerid");
  
  theNodeValue[0] = this.getOfferPrice(theIds[0]);
  theNodeValue[1] = this.getOfferPrice(theIds[1]);
  theNodeValue[2] = this.getOfferPrice(theIds[2]);
  
  var i=0;
  var theNo=0;
  for (i=1;i<3;i++)
  {
    if(theNodeValue[i] < theNodeValue[i-1])
    {
      theNo=i;
    }
  }
  
  this.optimumNodes[aTopOfferNumber] = this.getAllOfferNodes()[theIds[theNo]];
  
  return this.optimumNodes[aTopOfferNumber];
}

function getMultiOptimumOfferId(aNumber)
{
  var theOptimumNode = this.calculateOptimumNode(aNumber);
  var theOfferNode = this.reader.getNodeAttributeValue(theOptimumNode,'offerid')
  //7alert("The offer id node:"+theOfferNode.nodeName);
  return theOfferNode;
}

function getMultiOptimumPriceText(aNumber)
{
  var theOptimumNode = this.calculateOptimumNode(aNumber);
  
  return this.reader.getText(this.reader.getNode(theOptimumNode,'node.price'));
}
