﻿
var totalPriceOut = 0;
var totalPriceBack = 0;
var globalformat;
var globalcurrency;

/***********************************************/

function SetUniqueRadioButton(nameregex, current) 
{
    re = new RegExp(nameregex);
    for(i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio') {
            if (re.test(elm.id)||re.test(elm.name)) {
                elm.checked = false; 
            }
        }
    }
    current.checked = true; 
}

function GetUniqueRadioButton(nameregex) 
{
    re = new RegExp(nameregex);
    for(i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio') {
            if (re.test(elm.name) && elm.checked) {
                return elm.value;
            }
        }
    } 
    return "";   
}

function BlockRowItem(nameregex, flightRelation, flightDirection) 
{    
     re = new RegExp(nameregex); 
     for(i = 0; i < document.forms[0].elements.length; i++) 
     {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio') 
         {
           if (!re.test(elm.parentNode.parentNode.parentNode.parentNode.id))
           { 
            //   alert(re.test(elm.parentNode.parentNode.parentNode.parentNode.id));
                  elm.parentNode.parentNode.parentNode.parentNode.style.background = "#ffffff";
                  var obj = elm.parentNode.parentNode.parentNode;
                  var strFlight = "";
                  for(x=0; x<obj.childNodes.length; x++)
                  {
                       if (obj.childNodes[x].tagName.toUpperCase()== 'SPAN')
                       {
                          if (obj.childNodes[x].id.indexOf('Flight')>0)
                          {
                          //alert(obj.childNodes[x].innerText);
                          strFlight = obj.childNodes[x].innerText;
                          if (strFlight.length>3)
                            strFlight = strFlight.substring(2,strFlight.length);
                          }
                       }
                       if(flightRelation.indexOf(strFlight)==-1)
                       {
                         //alert( "flightRelation :"+flightRelation + "\n" + "strFlight:" + strFlight);
                         elm.parentNode.parentNode.parentNode.parentNode.style.background = "#999999"; 
                         //alert(elm.parentNode.parentNode.parentNode.parentNode.outerHTML);
                         if (obj.childNodes[x].id.indexOf('Price')>0)
                         {
                          if (obj.childNodes[x].childNodes[0].tagName != null)
                             {     
                                 if (obj.childNodes[x].childNodes[0].childNodes[0].tagName.toUpperCase() == 'INPUT')
                                    {
                                      //alert(strFlight);// +"\n"+ obj.childNodes[x].childNodes[0].tagName.toUpperCase());
                                       obj.childNodes[x].childNodes[0].childNodes[0].disabled = true;
                                       
                                       if (obj.childNodes[x].childNodes[0].childNodes[0].checked)
                                       {
                                         obj.childNodes[x].childNodes[0].childNodes[0].checked = false;
                                         
                                         switch (flightDirection)
                                           {
                                                case "Depart":
                                                totalPriceBack = 0;
                                                SummaryPriceOut(totalPriceOut,globalformat,globalcurrency)
                                                break;
                                                case "Return":
                                                totalPriceOut = 0;
                                                SummaryPriceBack(totalPriceBack,globalformat,globalcurrency)
                                                break;
                                           }
                                        
                                       }
                                    }      
                              }  
                          }                          
                       }
                       else
                       {
                         if (elm.parentNode.parentNode.parentNode.childNodes[x].id.indexOf('Price')>0)
                           {
                             if (elm.parentNode.parentNode.parentNode.childNodes[x].childNodes[0].childNodes[0].tagName.toUpperCase() == 'INPUT')
                             {
                                elm.parentNode.parentNode.parentNode.childNodes[x].childNodes[0].childNodes[0].disabled = false;
                             }
                           }
                       }
                  }
          }
       }     
  }
}


function BlockRowMapping(nameregex, flightRelation, flightDirection)
{
   //alert(flightRelation + "----------" + flightDirection);
    var flightOfTarget = (flightDirection=='Outbound')?'Inbound':'Outbound';
    var obj = document.getElementById('GridFareDisplay' + flightOfTarget).childNodes[0];
    var strFlight = "";
    for(x=0; x<obj.childNodes.length; x++)
    {
    if (obj.childNodes[x].tagName != '' && obj.childNodes[x].tagName != null && obj.childNodes[x].tagName.toUpperCase()== 'DIV')
      {
         if (obj.childNodes[x].id != '')
         {
             if (obj.childNodes[x].id != null && obj.childNodes[x].id.indexOf('GridItemRow')>-1)
               {
                 obj.childNodes[x].style.background = "#ffffff";
                 for(y=0; y<obj.childNodes[x].childNodes.length; y++)
                 {
                   // var objItem = obj.childNodes[x];
                     if (obj.childNodes[x].childNodes[y].tagName != null && obj.childNodes[x].childNodes[y].tagName.toUpperCase()== 'SPAN')
                     {           
                           for(z=0; z<obj.childNodes[x].childNodes[y].childNodes.length; z++)
                           {
                             if (obj.childNodes[x].childNodes[y].childNodes[z].tagName != null && obj.childNodes[x].childNodes[y].childNodes[z].tagName.toUpperCase()== 'SPAN')
                             {
                                if (obj.childNodes[x].childNodes[y].childNodes[z].id!=null && obj.childNodes[x].childNodes[y].childNodes[z].id.indexOf('Flight')>-1)
                                   {
                                       var objItemRow = obj.childNodes[x].childNodes[y].childNodes[z];
                                       var strFlight;
                                       if (objItemRow.innerText!= undefined)
                                          strFlight = objItemRow.innerText;
                                       else
                                          strFlight = objItemRow.innerHTML;
                                          
                                       if (strFlight.length>3)
                                        strFlight = strFlight.substring(2,strFlight.length);
                                        if(flightRelation.indexOf(strFlight)==-1)
                                        {
                                          objItemRow.parentNode.parentNode.style.background = "#999999";
                                          for(i=0; i<objItemRow.parentNode.childNodes.length; i++)
                                          {
                                            //alert(objItemRow.parentNode.childNodes[i].outerHTML);
                                                if (objItemRow.parentNode.childNodes[i].id.indexOf('Price')>-1)
                                                 {  
//                                                     for(n=0; n<objItemRow.parentNode.childNodes[i].childNodes.length; n++)
//                                                     {
                                                         if (objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].tagName != null && objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].tagName.toUpperCase() == 'INPUT')
                                                            {
                                                                 objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].disabled = true;
                                                                 if (objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].checked)
                                                                  {
                                                                     objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].checked = false;
                                                                  }
                                                            }
//                                                     }
                                                  }
                                          }
                                 
                                        }
                                        else
                                        {
                                           objItemRow.parentNode.parentNode.style.background = "#ffffff";
                                              for(i=0; i<objItemRow.parentNode.childNodes.length; i++)
                                              {
                                                    if (objItemRow.parentNode.childNodes[i].id.indexOf('Price')>-1)
                                                     {  
                                                             if (objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].tagName != null && objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].tagName.toUpperCase() == 'INPUT')
                                                                {
                                                                     objItemRow.parentNode.childNodes[i].childNodes[0].childNodes[0].disabled = false;
                                                                }
                                                      }
                                              }
                                        }
                                   }
                             }
                           }
                      }
                  }
               }
        }  
      }       
   }
   
}


function select_deselectAll(chkVal, idVal) 
{ 
    var frm = document.forms[0];
    for (i=0; i<frm.length; i++) 
    {
        if (idVal.indexOf ('checkAll') != -1) 
        {
            if(chkVal) 
            {
               frm.elements[i].checked = true;
            }else 
            {
               frm.elements[i].checked = false;
            }
       } 
   
    }
}

function select_deselectdehead(PassengercheckAllid) 
{ 
    document.getElementById('DivCheckBoxPassenger1').childNodes[0].checked = false;
}

/****************************************************/

function SummaryPriceOut(price, format, currency, select_keyflight, totalPrice)
{
    //alert(price + "\n" + format + "\n" + currency + "\n" + select_keyflight);
    globalformat = format;
    globalcurrency = currency;
    totalPriceOut = price;

    var obj = document.getElementById('divKeyFlight');
                for(x=0; x<obj.childNodes.length; x++)
                  { 
                       if (obj.childNodes[x]!= undefined)
                       {
                           if (obj.childNodes[x].tagName != undefined)
                           {
                              if (obj.childNodes[x].tagName.toUpperCase() == 'INPUT')
                               {
                                     if (obj.childNodes[x].id.indexOf('Out')>0)
                                      {
                                         obj.childNodes[x].value = select_keyflight;
                                      }
                                }  
                           }
                       } 
                   }    
    
    
    //document.getElementById('divKeyFlight').childNodes[0].value = select_keyflight;  
    if(price != -1 && price != -2){
     if (document.getElementById('divPriceTotalPrice') != null)
     {
      if (document.getElementById('divPriceTotalPrice').childNodes[0].innerText != undefined)
        document.getElementById('divPriceTotalPrice').childNodes[0].innerText = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack));//format.replace("[PRICE]", formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack))).replace("[CURRENCY]", currency);
      else
        document.getElementById('divPriceTotalPrice').childNodes[0].innerHTML = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack));format.replace("[PRICE]", formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack))).replace("[CURRENCY]", currency);
     
     }
    }
    else if(price == -2){
       if (document.getElementById('divPriceTotalPrice').childNodes[0].innerText != undefined)
            document.getElementById('divPriceTotalPrice').childNodes[0].innerText = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(0));//format.replace("[PRICE]", formatCurrency(Math.floor(0))).replace("[CURRENCY]", currency);
       else
            document.getElementById('divPriceTotalPrice').childNodes[0].innerHTML = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(0));//format.replace("[PRICE]", formatCurrency(Math.floor(0))).replace("[CURRENCY]", currency);
   }
    
 }

function SummaryPriceBack(price, format, currency, select_keyflight, totalPrice)
{
    globalformat = format;
    globalcurrency = currency;
    totalPriceBack = price;
    var obj = document.getElementById('divKeyFlight');
                for(x=0; x<obj.childNodes.length; x++)
                  { 
                       if (obj.childNodes[x]!= undefined)
                       {
                           if (obj.childNodes[x].tagName != undefined)
                           {
                              if (obj.childNodes[x].tagName.toUpperCase() == 'INPUT')
                               {
                                     if (obj.childNodes[x].id.indexOf('Back')>0)
                                      {
                                         obj.childNodes[x].value = select_keyflight;
                                      }
                                }  
                           }
                       } 
                   }
    //document.getElementById('divKeyFlight').childNodes[1].value = select_keyflight;
    if(price != -1 && price != -2){
      if(document.getElementById('divPriceTotalPrice') != null)
      {
      if (document.getElementById('divPriceTotalPrice').childNodes[0].innerText != undefined)
        document.getElementById('divPriceTotalPrice').childNodes[0].innerText = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(totalPriceOut)+ Math.floor(totalPriceBack));//format.replace("[PRICE]", formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack))).replace("[CURRENCY]", currency);
      else
        document.getElementById('divPriceTotalPrice').childNodes[0].innerHTML = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(totalPriceOut)+ Math.floor(totalPriceBack));//format.replace("[PRICE]", formatCurrency(Math.floor(totalPriceOut)+Math.floor(totalPriceBack))).replace("[CURRENCY]", currency);
      }
    }
    else if(price == -2){
       if (document.getElementById('divPriceTotalPrice').childNodes[0].innerText != undefined)
        document.getElementById('divPriceTotalPrice').childNodes[0].innerText = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(0));//format.replace("[PRICE]", formatCurrency(Math.floor(0))).replace("[CURRENCY]", currency);
       else
        document.getElementById('divPriceTotalPrice').childNodes[0].innerHTML = totalPrice + ' ' + currency + ' ' + formatCurrency(Math.floor(0));//format.replace("[PRICE]", formatCurrency(Math.floor(0))).replace("[CURRENCY]", currency);
    }
}

function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}

/***************************************/
/***************************************/
function getIndexByText(id, val) {            
    var ddl = document.getElementById(id);
    for(var i=0; i<ddl.options.length; i++) {
        if (ddl.options[i].innerText.toUpperCase() == val.toUpperCase())
            return i;             
    }
    return 0;            
}

function getIndexValue(id, val) {            
    var ddl = document.getElementById(id);
    for(var i=0; i<ddl.options.length; i++) {
        if (ddl.options[i].value.toUpperCase() == val.toUpperCase())
            return i;             
    }
    return 0;            
}
/***************************************/
/***************************************/


/*******************************************************************************************************/

BIRTSCommon = function() {}
BIRTSCommon.IE7 = 'ie7';
BIRTSCommon.IE = 'ie';
BIRTSCommon.OPERA = 'opera';
BIRTSCommon.SAFARI = 'safari';
BIRTSCommon.GECKO = 'gecko';
/*
 * Returns a string representing the current browser.	
 */
BIRTSCommon.browser = function() {
    var ua = navigator.userAgent.toLowerCase();    
    if (ua.indexOf('opera')!=-1) { // Opera (check first in case of spoof)
     return 'opera';
    } else if (ua.indexOf('msie 7')!=-1) { // IE7
     return 'ie7';
    } else if (ua.indexOf('msie') !=-1) { // IE
     return 'ie';
    } else if (ua.indexOf('safari')!=-1) { // Safari (check before Gecko because it includes "like Gecko")
     return 'safari';
    } else if (ua.indexOf('gecko') != -1) { // Gecko
     return 'gecko';
    } else {
     return false;
    }
}

BIRTSCommon.currenBrower = BIRTSCommon.browser();
