// JScript File
/************************************* Flight Search Specific Scripts Starts ****************************************/
var strDepCity
var strArrCity
function fnSearch(channelName, wsPrifix, Src) 
{
    if(!CityValidation(wsPrifix))return false;
	  var numAdult
	  var numChild
	  var numInfant
	  var total
  if (wsPrifix == "") 
  {
    if (document.getElementById("ctl00_contentMain_ctl00_hdnFlightMode").value == "D") 
    {
      numAdult = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLDAdults").value);
      numChild = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLDChildren").value);
      numInfant = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLDInfants").value);
      if (numInfant > numAdult) 
      {
        alert("Infants must be less than or equal to Adults.");//Infants must be less than or equal to Adults.
        return false;
      }
      total = numAdult + numChild + numInfant;
    }
    else 
    {
      numAdult = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLAdults").value);
      numChild = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLChildren").value);
      numInfant = parseFloat(document.getElementById("ctl00_contentMain_ctl00_numFLInfants").value);
      if (numInfant > numAdult) 
      {
        alert("Infants must be less than or equal to Adults.");//Infants must be less than or equal to Adults.
        return false;
      }
      total = numAdult + numChild + numInfant;
    }
    var journeyType = (document.getElementById("ctl00_contentMain_ctl00_hdnFlightMode")).value;
	var flexiCheck, departDate, returnDate;
	if (journeyType == "D")
	{
		if(document.getElementById("ctl00_contentMain_ctl00_optFLDJourneyReturn").checked)
		{
			flexiCheck = document.getElementById("ctl00_contentMain_ctl00_FlexidateDomchk"); 
			departDate = document.getElementById("ctl00_contentMain_ctl00_txtFLDDepDate").value;
			returnDate = document.getElementById("ctl00_contentMain_ctl00_txtFLDRetDate").value;
		}	
	}
	else if (journeyType == "I")
	{
		if (ObjectExists(document.getElementById("ctl00_contentMain_ctl00_optFLJourneyReturn")) && document.getElementById("ctl00_contentMain_ctl00_optFLJourneyReturn").checked)
		{
			flexiCheck = document.getElementById("ctl00_contentMain_ctl00_FlexidateIntchk");
			departDate = document.getElementById("ctl00_contentMain_ctl00_txtFLDepDate").value;
			returnDate = document.getElementById("ctl00_contentMain_ctl00_txtFLRetDate").value;
		}
	}
	
    if(ObjectExists(flexiCheck) && flexiCheck.checked)
	{
		if (!ValidateFlexiDates(departDate, returnDate))
			return false;
	}

 }
             
    else 
   {
    if (wsPrifix == "MI") 
    {
    if(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_hdnFlightMode"))
    {
      if (document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_hdnFlightMode").value == "D") 
      {
        numAdult = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLDAdults").value);
        numChild = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLDChildren").value);
        numInfant = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLDInfants").value);
        if (numInfant > numAdult) 
        {
          alert("Infants must be less than or equal to Adults."); //Infants must be less than or equal to Adults.
          return false;
        }
        
        total = numAdult + numChild + numInfant;
      }
      else 
      {
        numAdult = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLAdults").value);
        numChild = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLChildren").value);
        numInfant = parseFloat(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_numFLInfants").value);
        if (numInfant > numAdult) 
        {
          alert("Infants must be less than or equal to Adults."); //Infants must be less than or equal to Adults.
          return false;
        }
        total = numAdult + numChild + numInfant;
          }
	}
}
     else 
    {
      if (document.getElementById("ctl00_contentMain_ModifyFlightSearch1_hdnFlightMode").value == "D") 
      {
        numAdult = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLDAdults").value);
        numChild = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLDChildren").value);
        numInfant = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLDInfants").value);
        if (numInfant > numAdult) 
        {
          alert("Infants must be less than or equal to Adults."); //Infants must be less than or equal to Adults.
          return false;
        }
        total = numAdult + numChild + numInfant;
      }
      else 
      {
        numAdult = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLAdults").value);
        numChild = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLChildren").value);
        numInfant = parseFloat(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_numFLInfants").value);
        if (numInfant > numAdult) 
            {
               alert("Infants must be less than or equal to Adults."); //Infants must be less than or equal to Adults.
                return false;
             }
           total = numAdult + numChild + numInfant;
       }
    }
      var flexiCheck, departDate, returnDate,journeyType;
	if(ObjectExists(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_hdnFlightMode")))
	{
		journeyType = (document.getElementById("ctl00_contentMain_ModifyFlightSearch1_hdnFlightMode")).value;
	}
	 else
	 {
		 journeyType = (document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_hdnFlightMode")).value;
	 }
		if (journeyType == "D")
		{
			if(document.getElementById("ctl00_contentMain_ModifyFlightSearch1_optFLDJourneyReturn").checked)
			{
				flexiCheck = document.getElementById("ctl00_contentMain_ModifyFlightSearch1_FlexidateDomchk"); 
				departDate = document.getElementById("ctl00_contentMain_ModifyFlightSearch1_txtFLDDepDate").value;
				returnDate = document.getElementById("ctl00_contentMain_ModifyFlightSearch1_txtFLDRetDate").value;
			}	
		}
		else if (journeyType == "I")
		{
			if (document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_optFLJourneyReturn").checked)
			{
				flexiCheck = document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_FlexidateIntchk");
				departDate = document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_txtFLDepDate").value;
				returnDate = document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_txtFLRetDate").value;
			}
		}
			
			if(ObjectExists(flexiCheck) && flexiCheck.checked)
			{
				if (!ValidateFlexiDates(departDate, returnDate))
					return false;

			}
   }
  

  if (total > 9) 
  {
    alert("Total number of Passengers cannot exceed 9.");//Total number of Passengers cannot exceed 9.
    return false;
  }
  try 
  {
    var target = "";
    switch (channelName) 
    {
    case "online-dc":
    case "online-sa":
      target = "_parent";
      break;
    case "bo-dc":
    case "bo-sa":
     // target = "frm2";
      target = "main";
      break;
    case "subagent":
      target = "login";
      break;
    }
 
     if (target == "login") 
    {
         document.aspnetForm.target = "main";
    }
    else
    {
         document.aspnetForm.target = target;
    }
    return true;
  }
  catch(ex) 
  {
    throw (ex);
  }
}

function CityValidation(wsPrifix)
{
    var DomDepartCity;
    var DomArrivalCity;
    var IntDepartCity;
    var IntArrivalCity;
    var JourneyType;
    if(wsPrifix=="MD")
    {
        JourneyType=document.getElementById("ctl00_contentMain_ModifyFlightSearch1_hdnFlightMode").value;
    }
    if(wsPrifix=="MI")
    {
        JourneyType=document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_hdnFlightMode").value;
    }
    if(wsPrifix=="")
    {
       JourneyType=document.getElementById("ctl00_contentMain_ctl00_hdnFlightMode").value;
    }
    
    
    if(JourneyType=="D")
    {
        if(wsPrifix=="MD")
        {
            DomDepartCity=document.getElementById("ctl00_contentMain_ModifyFlightSearch1_ddlFLDepart").value;
            DomArrivalCity=document.getElementById("ctl00_contentMain_ModifyFlightSearch1_ddlFLArrival").value;
            if(DomDepartCity==DomArrivalCity)
            {
                alert("~Translate~(Cities_are_same)");
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            DomDepartCity=document.getElementById("ctl00_contentMain_ctl00_ddlFLDepart").value;
            DomArrivalCity=document.getElementById("ctl00_contentMain_ctl00_ddlFLArrival").value;
            if(DomDepartCity==DomArrivalCity)
            {
                alert("~Translate~(Cities_are_same)");
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    if(JourneyType=="I")
    {
        if(wsPrifix=="MI")
        {
            if(document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_ddlFLIntDepart") != null)
            {
                IntDepartCity=document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_ddlFLIntDepart").value;
                IntArrivalCity=document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_txtFLArrival").value;
                var regx=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr=regx.exec(IntArrivalCity);
                if(IntDepartCity==substr[0])
                {
                    alert("~Translate~(Cities_are_same)");
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                IntDepartCity=document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_txtFLArrival").value;
                var regx=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr=regx.exec(IntDepartCity);
                IntArrivalCity=document.getElementById("ctl00_contentMain_ModifyIntFlightSearch1_txtFLDepart").value;
                var regx1=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr2=regx1.exec(IntArrivalCity);
                if(substr[0]==substr2[0])
                {
                    alert("~Translate~(Cities_are_same)");
                    return false;
                }
                else
                {
                    return true;
                }
            
            }
        }
        else
        {
            if(document.getElementById("ctl00_contentMain_ctl00_ddlFLIntDepart") != null)
            {
                IntDepartCity=document.getElementById("ctl00_contentMain_ctl00_ddlFLIntDepart").value;
                IntArrivalCity=document.getElementById("ctl00_contentMain_ctl00_txtFLArrival").value;
                var regx=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr=regx.exec(IntArrivalCity);
                if(IntDepartCity==substr[0])
                {
                    alert("~Translate~(Cities_are_same)");
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                IntDepartCity=document.getElementById("ctl00_contentMain_ctl00_txtFLDepart").value;
                var regx=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr=regx.exec(IntDepartCity);
                IntArrivalCity=document.getElementById("ctl00_contentMain_ctl00_txtFLArrival").value;
                var regx1=new RegExp("([A-Z][A-Z][A-Z])","g");
                var substr2=regx1.exec(IntArrivalCity);
                if(substr[0]==substr2[0])
                {
                    alert("~Translate~(Cities_are_same)");
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
 }
 
 //added by harish to validate fare rule on miniRule header
 function checkrules()
    { 
    var sreturn=true; 
        if(document.getElementById("chkValRules") != null)
        {
            if(document.getElementById("chkValRules").checked != true)
            {
                alert("Please go through the airline rules and accept them before continuing further !!");
               sreturn= false;
               return false;
            }
            else
            {sreturn= true;}
        }
        
         if(document.getElementById("ctl00_contentMain_chkTermsHHL") != null)
        {
            if(document.getElementById("ctl00_contentMain_chkTermsHHL").checked != true)
            {
                alert("Please go through the Hotel Policies and accept them before continuing further !!");
               sreturn= false;
               return false;
            }
            else
            {sreturn= true;}
        }
        
        
        if (document.getElementById("hdnaddcount") != null && document.getElementById("hdnsightcount") != null)
        {  
            if (document.getElementById("hdnaddcount").value != "" && document.getElementById("hdnsightcount").value != "")
            {
                    var addcount=document.getElementById("hdnaddcount").value;
                    var count=document.getElementById("hdnsightcount").value;
                    if (fnvalidatetxtAddition(addcount,count)==true)
                    {sreturn= true;}
                    else
                    {   
                        sreturn= false;
                        return false;
                    }
            }
        }
      return sreturn;
        
   }

 
 function BLoadFlightResult(FlightType)
{
   //debugger
   if(FlightType=="multicity")
   {
        window.location.href = 'FlightResultNew.aspx?AIRBackButton=True';
   }
   else if(FlightType=="FlexiFlight")
   {
        window.location.href = 'FlexiFlightResult.aspx?AIRBackButton=True';
   }
   else
   {
        window.location.href = 'FlightResult.aspx?AIRBackButton=True'
   }
}
 
function LoadFlightResult(filename)
{
   //debugger
    var iHotel = document.getElementById("ctl00_contentMain_Basket1_hdnHotelindex").value
    window.location.href = 'FlightResult.aspx?DynamicHit=CAFR&Dynamic='+iHotel;
}
 
/******************************** Flight Search Specific Scripts Ends ***************************/

function ValidateFlexiDates(departDate, arrivalDate)
{
	var du = new DateUtils();
	var date1= du.convertStringToDate(departDate, "-");
	var date2= du.convertStringToDate(arrivalDate, "-");
	var diff = date2 - date1;
	var days = diff / (1000*60*60*24);
	if(days < 6)
	{
		alert("The minimum difference between the dates should be 6 days.If you want to search less than 6 days please uncheck the My dates are flexible.");
		return false;
	}
	else
		return true;
}

function addCommas(nStr)
{
    //Get Display format for hidden filed.
    var decSeparator = document.getElementById("ctl00_hdnDecSeparator").value;
    var numFormat = "";
    if(decSeparator == ",")
        numFormat = ".";
    else
        numFormat = ",";
    
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? decSeparator + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
    {
        x1 = x1.replace(rgx, '$1' + numFormat + '$2');
    }
    return x1 + x2;
}

function replaceCommas(inputString)
{
	//Get Display format for hidden field.
	var decSeparator = document.getElementById("ctl00_hdnDecSeparator").value;

	if(decSeparator == ",")
	{
		inputString = inputString.replace(".", "");
		inputString = inputString.replace(",", ".");
	}
	else
	{
		inputString = inputString.replace(",", "");
	}

	return inputString;
}

