///<summary>
/// Copyright 2007, Iron Mountain, Inc. All rights reserved. This computer program is the property of Iron Mountain Inc.,
/// and its licensors and contains their confidential trade secrets. Use, examination, copying, transfer and 
/// disclosure to others, in whole or in part, are prohibited except with the express prior written consent of Iron Mountain, Inc. 
///</summary>

//This variable is used to check the browser type
var bIsIE;
//Main solution xml document
var xSolutionXml;
//This array holds current questions and answers based on the level executed by the user
var aQuestionAnswerList = new Array();
//This array holds solutions based on answers user provided.
var aSolutions = new Array();
//Level stack is used to store each level information. It helps to navigate next/back buttons.  
var oLevelStack = new levelStack();

var bIsValidate = true;
var sNextLevelId = "";
var sLevelIn = "";
var sContactTechInclude = "";

//Loads main function. 
main()

//This is the main function which loads xml document, check browser, etc. 
function main()
{
   isInternetExplore();
   loadXml();   
}

//This function is invoked when next button is clicked.
function next()
{  
   var sXpathLevelNode = "";
   var xLevelNode;
   var xAskQuestionNodes;
   bIsValidate = true;
   sNextLevelId = "";
   sLevelIn = "";
   
   try
   {
     if(oLevelStack != null)
     {
       if(oLevelStack.levels.length == 0)
       {
           //Assume that levels in XML file is starting from 1.
           sLevelIn = 1;           
       }
       else
          {
            //Get the last level from the level stack
            var oLevel = oLevelStack.pop();
            /*To process the current level question/answers, level id is required.
            Last element in the oLevelStack holds previius level question and answers
            but not the current level. 
            Last element object has sNextLevelId property and it always has a value if 
            the current level is a question level. */
            sLevelIn = oLevel.sNextLevelId;
            //Push the last element again to the stack because it was taken out above
            oLevelStack.push(oLevel);
          }
       //Xpath to accss the current level node.
       sXpathLevelNode = "drw/drwlogic/level[@levelid='" + sLevelIn + "']";
       
       //Get the question id list for the level and 
       //Process the ansewers for the questions on the level
       
       //Get the current level node
       xLevelNode = selectSingleNode(xSolutionXml, sXpathLevelNode); 
       if(xLevelNode != null)
       {
         //Solutions/Questions of current level need to be removed 
         //because same level can be considered with different answers
         removeSolutions(sLevelIn);
         removeQuestions(sLevelIn);
         
         xAskQuestionNodes = xLevelNode.getElementsByTagName("askquestion");        
         
         //Define a array to hold the questions for a level         
         var questionLevelArray = new Array(xAskQuestionNodes.length);      
         
         //Fill global question/answer array
         for(var i = 0; i < xAskQuestionNodes.length; i++)
		 {
		   //Get the question id from the current question node
		   var sQuestionId = getAttributeValue(xAskQuestionNodes[i], 0);
		   //Xpath for the current question
           var sXpathQuestion = "drw/questions/question[@questionid='" + sQuestionId + "']";
           //Get the current xml node
           var xCurrentQuestion = selectSingleNode(xSolutionXml, sXpathQuestion);
           
           questionLevelArray[i] = new Object();
           questionLevelArray[i].sQuestionId = sQuestionId;
            
           switch (getAttributeValue(xCurrentQuestion, 1))
           {
             case "1" :
                //Get the answer array form the perticuler question
                var aAnswers = getAnswerCheckBoxType(sQuestionId, sLevelIn);
                if(aAnswers.length > 0)
                {
                  //Answers has been provided.
                  questionLevelArray[i].aAnswerCollection = aAnswers;
                }
                else
                  {
                    //Answers has not been provided.
                    bIsValidate = false;                    
                  }
             break;
             case "2" :
                //Get the answer array form the perticuler question
                var aAnswers = getAnswerDropDownType(sQuestionId, sLevelIn);
                if(aAnswers.length > 0)
                {
                  //Answers has been provided.
                  questionLevelArray[i].aAnswerCollection = aAnswers;
                }
                else
                  {
                    //Answers has not been provided.
                    bIsValidate = false;                    
                  }
             break;
             case "3" :
                //Get the answer array form the perticuler question
                var aAnswers = getAnswerRadioButtonType(sQuestionId, sLevelIn);
                if(aAnswers.length > 0)
                {
                  //Answers has been provided.
                  questionLevelArray[i].aAnswerCollection = aAnswers;
                }
                else
                  {
                    //Answers has not been provided.
                    bIsValidate = false;                    
                  }
             break;
           } 
           //TODO: if bIsValidate is false stop iterating                      
		 }
		 
		 //If user answers provided then process the next level using the answers 
		 //else message is displayed to the use in above (getAnswer)methods.
		 if(bIsValidate)
		 {
		   //Get the "outcome" node of the current level
		   var xOutCome = xLevelNode.getElementsByTagName("outcome");
		   //Next level
		   //Assume that <level> node will have only one outcome.
		   var xNexLevelNodeList = xOutCome[0].getElementsByTagName("nextlevel");
		   
		   //If the next level node list is not null then find the next level id
		   if(xNexLevelNodeList != null)
		   { 
		     sNextLevelId = findNextLevel(xNexLevelNodeList);
		     //If xNexLevelNodeList is not null then sNextLevelId can't be null or empty. 
		     //If it happens then it's an xml well-formedness issue   	    
		   }		   
		     
		   //Next solution
		   var xNextSolutionNodeList = xOutCome[0].getElementsByTagName("nextsolution");
		   //Fill solution by evaluating the expression
		   fillSolutions(xNextSolutionNodeList, sLevelIn);
		   
		   if(sNextLevelId != "")
		   {		     
		     //push the level questions into the stack. At any point, last display level 
		     //is not saved into the stack. 
		     var oCurrentLevel = new Object();
		     oCurrentLevel.sLevelId = sLevelIn;
		     oCurrentLevel.sNextLevelId = sNextLevelId;
		     oCurrentLevel.aQuestions = questionLevelArray;
    	   
		     oLevelStack.push(oCurrentLevel);
		     
		     //Display next level questions
		     processNextLevelQuestions(sNextLevelId);	     	     
		   }		   
		 }
       }             
     }  
   }
   catch (excp)
   {
     alert(excp);
   }
}

//This function is invoked when back button is clicked.
function back()
{
  hideDisplay("solutions");
  enableDisplay("nextButton");
  enableDisplay("questions");
  hideDisplay("lowerbackstartbuttons"); 
  enableDisableSendButton();  
}

//This function is used to reload the application
function startOver()
{
  var xLevels = xSolutionXml.getElementsByTagName("level");
  for(var j = 0; j < xLevels.length; j++)
  {
      var sLevelId = getAttributeValue(xLevels[j], 0);
      //Hide all the levels except level 1. Any product will be start from level 1.
      if( sLevelId > 1)
      {
         //Remove solutions from the solution array
         removeSolutions(sLevelId);
         //Remove questions from the question array
         removeQuestions(sLevelId);
         document.getElementById("level" + sLevelId).innerHTML = ""; 
         hideDisplay("level" + sLevelId); 
      }
  }
  //Reset the level stack
  oLevelStack.levels.length = 0;  
  //Reset first level question answers to default values
  resetFirstLevelQuestions();
  
  enableDisplay("questions");
  enableDisplay("nextButton");
  hideDisplay("solutions");
  hideDisplay("lowerbackstartbuttons");
  enableDisableSendButton();   
}

//This function is used to reset the answers of first level questions
function resetFirstLevelQuestions()
{
    //Xpath to accss the first level node.
    var sXpathFirstLevelNode = "drw/drwlogic/level[@levelid='" + 1 + "']";
       
    //Get the current level node
    var xFirstLevelNode = selectSingleNode(xSolutionXml, sXpathFirstLevelNode); 
    
    if(xFirstLevelNode != null)
    {
      var xAskQuestionNodes = xFirstLevelNode.getElementsByTagName("askquestion");        
         
      //Iterate through xAskQuestionNodes to find out each question type
      for(var i = 0; i < xAskQuestionNodes.length; i++)
	  {
		 //Get the question id from the current question node
		 var sQuestionId = getAttributeValue(xAskQuestionNodes[i], 0);
		 //Xpath for the current question
         var sXpathQuestion = "drw/questions/question[@questionid='" + sQuestionId + "']";
         //Get the current xml node
         var xCurrentQuestion = selectSingleNode(xSolutionXml, sXpathQuestion);
           
         switch (getAttributeValue(xCurrentQuestion, 1))
         {
            case "1" :
                //Uncheck checkBox selected 
                var aCheckBoxList = document.getElementsByName(sQuestionId);

                for(var i = 0; i < aCheckBoxList.length; i++) 
                {
                  aCheckBoxList.item(i).checked = false;       
                }
            break;
            case "2" :
                //Reset DropDownType
                //One element can be exist in the array since this is dropdown type 
                var aDropDownList = document.getElementsByName(sQuestionId);
                aDropDownList[0].selectedIndex = "0";                               
            break;
            case "3" :
                //Reset RadioButtonType 
                var aRadioButtonList = document.getElementsByName(sQuestionId);

                for(var i = 0; i < aRadioButtonList.length; i++) 
                {
                  aRadioButtonList.item(i).checked = false;       
                }               
            break;
         }           
	   }
	 }
}

//This method is udes when user changes the answerof a question which was answered previously.
function updateUI(sLevel)
{ 
  var xLevels = xSolutionXml.getElementsByTagName("level");
  var iStackLength = oLevelStack.levels.length;
  
  if(sLevel != "")
  { 
    if(iStackLength > 0)
    {
      var oLastLevel = oLevelStack.pop();
      oLevelStack.push(oLastLevel);
     
      if(oLastLevel.sLevelId >= sLevel )
      {    
        //Iterate through the level ids in the xml. If the level id of an array element 
        //is greater than the given level id then set empty string for the innerHTML property.  
        //Purpose of using for loop is remove elements from oLevelStack    
        for(var i = 0; i < iStackLength; i++)
        {                  
           var oCurretntLevel = oLevelStack.pop();
           //Remove solutions from the solution array
           removeSolutions(oCurretntLevel.sLevelId);
           //Remove questions from the question array
           removeQuestions(oCurretntLevel.sLevelId);
            
           if(oCurretntLevel.sLevelId > sLevel)
           {
             document.getElementById("level" + oCurretntLevel.sLevelId).innerHTML = ""; 
           }
           else
              {
                break;
              }
        } 
        
        //Purpose of using for loop is reset levels that are not countered in oLevelStack
        for(var j = 0; j < xLevels.length; j++)
        {
           var sLevelId = getAttributeValue(xLevels[j], 0);
           if( sLevelId > sLevel)
           {
             //Remove solutions from the solution array
             removeSolutions(sLevelId);
             //Remove questions from the question array
             removeQuestions(sLevelId);
             document.getElementById("level" + sLevelId).innerHTML = "";  
           }
        }        
      }                  
    }
    next();
    enableDisableSendButton();
  }  
}

//This function checks whether the loaded browser is IE or not.
function isInternetExplore()
{
  var sBrowserName = navigator.appName; 
   
  if (sBrowserName == "Microsoft Internet Explorer")
  {
    bIsIE = true;
  }
}

//This function loads the xml file into a xml document at the client end  
function loadXml()
{
   var xRequester;
   try
   { 
      // Firefox, Opera 8.0+, Safari     
      xRequester = new XMLHttpRequest();
   }
   catch (e)
   {  
      // Internet Explorer 
      try
      {        
         xRequester = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {   
         try
         { 
            xRequester = new ActiveXObject("Microsoft.XMLHTTP"); 
         }
         catch (e)
         {     
            //TODO: Need to be localize if necessary 
            alert("Your browser does not support AJAX!");     
            return false;      
         }   
      } 
    }
  
    xRequester.onreadystatechange = function()
    {
       if(xRequester.readyState == 4)
       { 
          xSolutionXml = xRequester.responseXML; 
       }
    }
    //TODO: Need to be get through a variable
    xRequester.open("GET", "/common/drw/drw.xml", true);
    xRequester.send(null);  
}

//Question and answers level stack 
function levelStack()	//Creating Stack Object
{
   // Create an empty array question levels.
   this.levels = new Array();  //levels array inside stack object
   this.push  = pushdata;      //Call pushdata function on push operation
   this.pop   = popdata;	   //Call popdata function on pop operation    
}

//Push object into level stack
function pushdata(oData)
{
   this.levels.push(oData);
}

//Pop object from the level stack 
function popdata()
{
   return this.levels.pop();			
}

//Enable display of a html control
function enableDisplay(sElementId)
{   
   var oDiv = document.getElementById(sElementId);    
   oDiv.style.display = "inline";
}

//Hide html control
function hideDisplay(sElementId)
{   
   var oDiv = document.getElementById(sElementId);    
   oDiv.style.display= "none";
}

function hideDisplayContactTech(sElementId)
{   
   var oDiv = document.getElementById(sElementId);    
   oDiv.style.display= "none";
   //Reset the empty form
   if(sContactTechInclude != "")
   {
     document.getElementById("contactTechSupport").innerHTML = sContactTechInclude;
   }
}

//Enable/disable send button
function enableDisableSendButton()
{
  if ((aSolutions.length > 0) && (sNextLevelId == "") && (bIsValidate))
  { 
    document.getElementById("send").disabled = false;
  }
  else
     {
       document.getElementById("send").disabled = true;
     }  
}

//This function is used to get selected nodes from a xml and function is 
//supported only for IE and Firefox browsers. 
function getSelectNodes(oXmlDocument, sXmlPath, sTagName)
{
   if(bIsIE)
   {
      return oXmlDocument.selectNodes(sXmlPath);
   }
   else
      {
         //Firefox
         return oXmlDocument.getElementsByTagName(sTagName);          
      }
}

//This function is used to get a selected node from a xml and function is 
//supported only for IE and Firefox browsers.
function selectSingleNode(xXmlDoc, sElementPath)
{ 
  if(bIsIE)
  {
     return xXmlDoc.selectSingleNode(sElementPath);
  }
  else
      { 
         //Firefox 
         if (document.implementation && document.implementation.createDocument)   
         {
            var xNodes = xXmlDoc.evaluate(sElementPath, xXmlDoc, null, XPathResult.ANY_TYPE, null);
            var xResults = xNodes.iterateNext();
            return xResults;  
         }
      } 
}

//This function is used to get element name of a xml element and function is 
//supported only for Firefox browsers.
function getElementValue(oElementNode)
{
   if(bIsIE)
   {
      return oElementNode.text;
   }
   else
   {
      //Firefox
      if (oElementNode.childNodes[0] == null)
      {
			return "";
	  }
	  else
	     {
			return oElementNode.childNodes[0].nodeValue;
	     }
   } 
}

//This function is used to get attibute value of a attribute node and function is 
//supported only for Firefox browsers.
function getAttributeValue(xAttributeNode, sPosition)
{
   if(bIsIE)
   {
      return xAttributeNode.attributes(sPosition).text;
   }
   else
   {
      //Firefox     
      return xAttributeNode.attributes[sPosition].nodeValue;
   }   
}

//This function process the next level questions and build the inner html
function processNextLevelQuestions(sNextLevelId)
{
   var sXPathLevel = sXpathLevelNode = "drw/drwlogic/level[@levelid='" + sNextLevelId + "']";
   var xNextLevelNode = selectSingleNode(xSolutionXml, sXPathLevel);
   var xNextAskQuestionNodes = null;
   var sLevelHtml = "<table width='90%'>";
      
   if(xNextLevelNode != null)
   {
     xNextAskQuestionNodes = xNextLevelNode.getElementsByTagName("askquestion");        
     var nQuestionNum = getLastQuestionNumber();
         
     //Fill global question/answer array
     for(var i = 0; i < xNextAskQuestionNodes.length; i++)
	 {
	   var sQuestionId = getAttributeValue(xNextAskQuestionNodes[i], 0);
	   //Xpath for the current question
       var sXpathQuestion = "drw/questions/question[@questionid='" + sQuestionId + "']";
       //Get the current xml node
       var xCurrentQuestion = selectSingleNode(xSolutionXml, sXpathQuestion);       
       
       nQuestionNum += 1; 
       
       switch (getAttributeValue(xCurrentQuestion, 1))
       {
          case "1" :
            sLevelHtml = sLevelHtml + generateCheckBoxType(xCurrentQuestion, nQuestionNum, sNextLevelId);
          break;
          case "2" :
            sLevelHtml = sLevelHtml + generateDropDownType(xCurrentQuestion, nQuestionNum, sNextLevelId);
          break;
          case "3" :
            sLevelHtml = sLevelHtml + generateRadioButtonType(xCurrentQuestion, nQuestionNum, sNextLevelId);
          break;
       }       
	 }
	 sLevelHtml += "</table>"; 
	 
	 enableDisplay("level" + sNextLevelId);
     document.getElementById("level" + sNextLevelId).innerHTML = sLevelHtml;
   }
}

//This function displays solutions.
function displaySolutions()	
{
  var sSolutionHtml = "<table>";
  
  sSolutionHtml += "<tr><td colspan='3'>&nbsp;</td></tr>";
  //TODO: This text should be retrived from the xml if possible else it should goes to property file.
  //with the new updates on xml, this text may go off.
  sSolutionHtml += "<tr><td>&nbsp;</td><td colspan='2' class=solutionPageTitle>" + document.getElementsByName("solutionPageTitleText").item(0).value + "</td></tr>";
  sSolutionHtml += "<tr><td>&nbsp;</td><td colspan='2' class=solutionPageHeading>Select the method below that best describes your situation.</td></tr>";
  
  for(var i = 0; i < aSolutions.length; i++)
  { 
     var sXPathSolutionHeading = "drw/solutions/solution[@solutionid =" + aSolutions[i].sSolutionId + "]/label";
     var sXPathSolutionDes = "drw/solutions/solution[@solutionid =" + aSolutions[i].sSolutionId + "]/description";
     var sXPathSolutionLink = "drw/solutions/solution[@solutionid =" + aSolutions[i].sSolutionId + "]/link";
	         
     var bOpenNew = getAttributeValue(selectSingleNode(xSolutionXml, sXPathSolutionLink), 0);
     
     if(bOpenNew == "true")
     {     
       sSolutionHtml += "<tr><td>&nbsp;</td><td><table><tr><td class=rightArrow><img src='/common/drw/images/rightarrow.gif'/></td></tr></table></td><td class=solutionTitle><a href='" + getElementValue(selectSingleNode(xSolutionXml, sXPathSolutionLink)) +  "' target='_blank'>" + getElementValue(selectSingleNode(xSolutionXml, sXPathSolutionHeading)) + "</a></td></tr>";
     }
     else
     {
       sSolutionHtml += "<tr><td>&nbsp;</td><td><table><tr><td class=rightArrow><img src='/common/drw/images/rightarrow.gif'/></td></tr></table></td><td class=solutionTitle><a href='" + getElementValue(selectSingleNode(xSolutionXml, sXPathSolutionLink)) +  "'>" + getElementValue(selectSingleNode(xSolutionXml, sXPathSolutionHeading)) + "</a></td></tr>";
     }
     sSolutionHtml += "<tr><td>&nbsp;</td><td></td><td class=solutiondescription>" + getElementValue(selectSingleNode(xSolutionXml, sXPathSolutionDes)) + "</td></tr>";
  }
  
  sSolutionHtml += "</table>";
  hideDisplay("questions");
  hideDisplay("nextButton");
  enableDisplay("solutions");
  enableDisplay("lowerbackstartbuttons");
  document.getElementById("solutions").innerHTML = sSolutionHtml;
}

//This method returns the number of questions displayed in the levels
function getLastQuestionNumber()
{
   var nCount = 0;
   
   for(var i = 0; i < oLevelStack.levels.length; i++)
   {
      var aQues = oLevelStack.levels[i].aQuestions;
      nCount += aQues.length;
   }
   return nCount;
}

//This function remove solution of the last question level
function removeLevelSolutionAndQuestions()
{   
   if(oLevelStack.levels.length > 0)
   {    
     var oLevel = oLevelStack.pop();
     var level = oLevel.sLevelId;            
     removeSolutions(level);
     removeQuestions(level);
   }  
}

//This method is used to remove solutions from a givn level
function removeSolutions(sLevel)
{
  var aTempArray = new Array();
  
  for(var i = 0; i < aSolutions.length; i++)
  {
    if(aSolutions[i].sLevelId != sLevel)
    { 
        aTempArray.push(aSolutions[i]);
    } 
  }
  aSolutions = aTempArray;  
}

//This method is used to remove question from a givn level
function removeQuestions(sLevel)
{
  var aTempArray = new Array();
  
  for(var i = 0; i < aQuestionAnswerList.length; i++)
  {
    if(aQuestionAnswerList[i].sLevelId != sLevel)
    { 
        aTempArray.push(aQuestionAnswerList[i]);
    } 
  }
  aQuestionAnswerList = aTempArray;  
}

//This method is used to get the answers for the checkbox type question
function getAnswerCheckBoxType(sQustionId, sLevel)
{
  var bIsChecked = false;
  var aLevelQuestionSelectList;
  aLevelQuestionSelectList = document.getElementsByName(sQustionId);
  var aQuestionArray = new Array(aLevelQuestionSelectList.length); 
  for(var r = 0; r < aLevelQuestionSelectList.length; r++)
  {
    if(aLevelQuestionSelectList.item(r).checked)
    {
       var bIsQuExist = false;
       bIsChecked = true;
       aQuestionArray[r] = new Object();
       aQuestionArray[r].sLevelId = sLevel;
       aQuestionArray[r].sId = sQustionId;     
       aQuestionArray[r].sValue = aLevelQuestionSelectList.item(r).id;
              
       for(var z = 0; z < aQuestionAnswerList.length; z++)
       {
          if((aQuestionAnswerList[z].sId == aQuestionArray[r].sId) && (aQuestionAnswerList[z].sValue == aQuestionArray[r].sValue))
          {
             bIsQuExist  =  true;
          }
       }
      
       if(!bIsQuExist)
       { 
           //Push question/answer pair into global array
           aQuestionAnswerList.push(aQuestionArray[r]);
       }
    }
  }
  
  if(!bIsChecked)
  {
    aQuestionArray.length = 0;
    //alert(document.getElementsByName("getAnswerCheckBoxError").item(0).value);
  }  
  return aQuestionArray;
}

//This method is used to get the answers for the dropdwon type question
function getAnswerDropDownType(sQustionId, sLevel)
{
  var aLevelQuestionSelectList;
   
   //Here, assume that user may not allowed to  type answers/ select multiple answers/
   //All the question informations will be retrived using select html controls. 
   aLevelQuestionSelectList = document.getElementsByName(sQustionId, sLevel);
   //This array is returned to fill the LevelStack
   var aQuestionArray = new Array(aLevelQuestionSelectList.length); 
   for(var r = 0; r < aLevelQuestionSelectList.length; r++)
   { 
     var box = aLevelQuestionSelectList[r].options;
     var sChosenValue = box[box.selectedIndex].value;
     
     if(sChosenValue != -1)
     {
        var bIsQuExist = false;
        aQuestionArray[r] = new Object();
        aQuestionArray[r].sLevelId = sLevel;
        aQuestionArray[r].sId = aLevelQuestionSelectList[r].name;     
        aQuestionArray[r].sValue = sChosenValue;
        
        for(var z = 0; z < aQuestionAnswerList.length; z++)
        {
          if((aQuestionAnswerList[z].sId == aQuestionArray[r].sId) && (aQuestionAnswerList[z].sValue == aQuestionArray[r].sValue))
          {
             bIsQuExist  =  true;
          }
        }
      
        if(!bIsQuExist)
        { 
           //Push question/answer pair into global array
           aQuestionAnswerList.push(aQuestionArray[r]);
        }
     }
     else
         {
           aQuestionArray.length = 0;
           //alert(document.getElementsByName("getAnswerDropDownError").item(0).value);
         }
   }   
   return aQuestionArray;
}

//This method is used to get the answers for the radiobutton type question
function getAnswerRadioButtonType(sQustionId, sLevel)
{
   var bIsChecked = false;
   var aLevelQuestionSelectList;
   aLevelQuestionSelectList = document.getElementsByName(sQustionId);
   var aQuestionArray = new Array(aLevelQuestionSelectList.length); 
   for(var r = 0; r < aLevelQuestionSelectList.length; r++)
   {
     if(aLevelQuestionSelectList.item(r).checked)
     {
       bIsChecked = true;
       var bIsQuExist = false;
       
       aQuestionArray[r] = new Object();
       aQuestionArray[r].sLevelId = sLevel;
       aQuestionArray[r].sId = sQustionId;     
       aQuestionArray[r].sValue = aLevelQuestionSelectList.item(r).value;
       
       for(var z = 0; z < aQuestionAnswerList.length; z++)
       {
         if((aQuestionAnswerList[z].sId == aQuestionArray[r].sId) && (aQuestionAnswerList[z].sValue == aQuestionArray[r].sValue))
         {
            bIsQuExist  =  true;
         }
       }
      
       if(!bIsQuExist)
       { 
          //Push question/answer pair into global array
          aQuestionAnswerList.push(aQuestionArray[r]);
       }       
     }
   }
  
   if(!bIsChecked)
   {
     aQuestionArray.length = 0;
     //alert(document.getElementsByName("getAnswerRadioButtonError").item(0).value);
   }  
   return aQuestionArray;
}

//This method finds the next level id
function findNextLevel(xNexLevelNodeList)
{
   var sNextLevel = "";
   for(var i = 0; i < xNexLevelNodeList.length; i++)
   {
     var xExpressionNodeList = xNexLevelNodeList[i].getElementsByTagName("expression");
     //Assume that <nextlevel> will have only one expression element at the same level
     //and expression evaluation should direct only a one next level
     if(evaluateExpression(xExpressionNodeList[0]))
     {
       return getAttributeValue(xNexLevelNodeList[i], 0);
     }     
   }
   return sNextLevel;
}

//This method collects the solution for the question level
function fillSolutions(xNextSolutionNodeList, sCurrentLevel)
{
   for(var i = 0; i < xNextSolutionNodeList.length; i++)
   {
     var xExpressionNodeList = xNextSolutionNodeList[i].getElementsByTagName("expression");
     //Assume that <nextsolution> will have only one expression element at the same level
     //and expression evaluation should direct only a one next level
     if(evaluateExpression(xExpressionNodeList[0]))
     {       
       var bIsExist = false;
       var sSolId = getAttributeValue(xNextSolutionNodeList[i], 0);
       
       for(var j = 0; j < aSolutions.length; j++)
       {
          if(aSolutions[j].sSolutionId == sSolId)
          {
             bIsExist  =  true;
          }
       }
      
       if(!bIsExist)
       { 
          var sLevelSolution = new Object();
          sLevelSolution.sLevelId = sCurrentLevel;
          sLevelSolution.sSolutionId = sSolId;
          aSolutions.push(sLevelSolution);        
       }
     }     
   }
}

//Evaluates "expression" element in the xml
function evaluateExpression(xExpressionNode)
{  
  for(var i = 0; i < xExpressionNode.childNodes.length; i++)
  {
      var xCurrentNode = xExpressionNode.childNodes[i];
      if(xCurrentNode.nodeType == 1)
      {
        switch (xCurrentNode.nodeName)
           {
             case "factor" :
                  if(!evaluateFactor(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "and" :
                  if(!evaluateAnd(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "or" :
                  if(!evaluateOr(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "not" :
                  if(!evaluateNot(xCurrentNode))
                  {
                    return false;
                  }
             break;
           }
      }
  }   
  return true;
}

//Evaluates "factor" element in the xml
function evaluateFactor(xFactorNode)
{
  for(var i = 0; i < aQuestionAnswerList.length; i++)
  {
    if(aQuestionAnswerList[i].sId == getAttributeValue(xFactorNode, 0))
    {
      if(aQuestionAnswerList[i].sValue == getAttributeValue(xFactorNode, 1))
      {
        return true;
      }
    }
  }  
  return false;
}

//Evaluates "and" element in the xml
function evaluateAnd(xAndNode)
{  
  for(var i = 0; i < xAndNode.childNodes.length; i++)
  {
    var xCurrentNode = xAndNode.childNodes[i];
      if(xCurrentNode.nodeType == 1)
      {
        switch (xCurrentNode.nodeName)
           {
             case "factor" :
                  if(!evaluateFactor(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "and" :
                  if(!evaluateAnd(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "or" :
                  if(!evaluateOr())
                  {
                    return false;
                  }
             break;
             case "not" :
                  if(!evaluateNot())
                  {
                    return false;
                  }
             break;
           }
      }
  }  
  return true;
}

//Evaluates "or" element in the xml
function evaluateOr(xOrNode)
{  
  for(var i = 0; i < xOrNode.childNodes.length; i++)
  {
    var xCurrentNode = xOrNode.childNodes[i];
      if(xCurrentNode.nodeType == 1)
      {
        switch (xCurrentNode.nodeName)
           {
             case "factor" :
                  if(evaluateFactor(xCurrentNode))
                  {
                    return true;
                  }
             break;
             case "and" :
                  if(evaluateAnd(xCurrentNode))
                  {
                    return true;
                  }
             break;
             case "or" :
                  if(evaluateOr())
                  {
                    return true;
                  }
             break;
             case "not" :
                  if(evaluateNot())
                  {
                    return true;
                  }
             break;
           }
      }
  }
  return false;
}

//Evaluates "not" element in the xml
function evaluateNot(xAndNode)
{  
  for(var i = 0; i < xAndNode.childNodes.length; i++)
  {
    var xCurrentNode = xAndNode.childNodes[i];
      if(xCurrentNode.nodeType == 1)
      {
        switch (xCurrentNode.nodeName)
           {
             case "factor" :
                  if(evaluateFactor(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "and" :
                  if(evaluateAnd(xCurrentNode))
                  {
                    return false;
                  }
             break;
             case "or" :
                  if(evaluateOr())
                  {
                    return false;
                  }
             break;
             case "not" :
                  if(evaluateNot())
                  {
                    return false;
                  }
             break;
           }
      }
  }  
  return true;
}

//This function is used to display contactTechSupport form
function displayContactTechSuport()
{
   enableDisplay("contactTechSupport");
   contactTechSupport.style.top = (parseInt(document.body.clientHeight) - 475)/ 2;
   contactTechSupport.style.left = (parseInt(document.body.clientWidth) - parseInt(contactTechSupport.style.width))/ 2;
   contactTechSupport.style.display = "inline";
   window.scrollTo(0,0);
   //store inner html of the form when form close it will re-set
   sContactTechInclude = document.getElementById("contactTechSupport").innerHTML;
}

//This method builds a string that includes questions and answers user entered in each level.
function buildQuestionAnswerString()
{
   var sQuestionAnswerString = "";
   if(aQuestionAnswerList.length > 0)
   {
      for(var p = 0; p < aQuestionAnswerList.length; p++)
      {        
         var sXpathQuestionText = "drw/questions/question[@questionid='" + aQuestionAnswerList[p].sId + "']/label";
         var sXpathQuestionAnswerText = "drw/questions/question[@questionid='" + aQuestionAnswerList[p].sId + "']/value[@valueid =" + aQuestionAnswerList[p].sValue + "]";
         
         sQuestionAnswerString = sQuestionAnswerString + getElementValue(selectSingleNode(xSolutionXml, sXpathQuestionText)) + ":";
         sQuestionAnswerString = sQuestionAnswerString + getElementValue(selectSingleNode(xSolutionXml, sXpathQuestionAnswerText)) + "::";         
       }
    }
    return sQuestionAnswerString;  
}

//This method relted to loading contactTechSupport div.
function $(v) 
{ 
  return(document.getElementById(v)); 
}

//This method relted to loading contactTechSupport div.
function agent(v) 
{ 
  return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); 
}

//This method relted to loading contactTechSupport div.
function xy(e,v) 
{ 
  return(v?(agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:e.pageX)); 
}

//This method relted to loading contactTechSupport div.
function dragOBJ(d,e) 
{
    function drag(e) 
    {
      if(!stop) 
      {
         d.style.top=(tX=xy(e,1)+oY-eY+'px'); 
         d.style.left=(tY=xy(e)+oX-eX+'px'); 
      } 
    }
    
    var oX=parseInt(d.style.left),oY=parseInt(d.style.top),eX=xy(e),eY=xy(e,1),tX,tY,stop;

    document.onmousemove=drag; 
    document.onmouseup=function(){ 

       stop=1; 
       document.onmousemove=''; 
       document.onmouseup=''; 
     };
}






