﻿// JScript File

var one_day=1000*60*60*24;
function GetSegmentCount(controlclientid)
{

  var roundtype=document.getElementById(controlclientid+"hdn_roundtype").value;
   switch (roundtype)
    {
        case "1":
            //one way
            return 1;
        break;
        case "2":
       //round trip
          return 2;
        break;
        case "3":
        
       //multi min = 2 , 3 or max= 5
            for (i=3; i<=max_segments; ++i){ 
                if (IsSegmentNull(i,controlclientid)) 
                { 
                     return (i-1);
                }
                else
                {
                    if (i == max_segments)
                    {return i;}
                }
            }
        break;
    } 

}
function IsSegmentNull(index,controlclientid)
{
   var _arrivalDest = document.getElementById(controlclientid + "inputArrivalLoc" + index).value;
   var departuredate = document.getElementById("fdt"+index).value;       
   if (_arrivalDest == "" && departuredate == "")
    {return true;}
    
   return false;
}

function GetDate(myDate){
  var aDate = myDate.split("/");
  if (aDate.length != 3) return;
  day  = aDate[0]-0;
  month = aDate[1]-1;
  year  = aDate[2]-0;

  if(isNaN(year))return;
  if(isNaN(month))return;
  if(isNaN(day))return;
  
  return new Date(year,month,day);
}

function CheckDestinations(controlclientid, segmentcount)
    {
       var _fromDest, _toDest;
       var _segment_loop = segmentcount;
       var roundtype = "2";
       if (segmentcount > 1) { roundtype=document.getElementById(controlclientid+"hdn_roundtype").value; }
       if (roundtype == "2")
       {_segment_loop=1;}
       
            for (i=1; i<=_segment_loop; ++i){ 
       	    _fromDest = document.getElementById(controlclientid + "inputDepartureLoc" + i).value;
  	        _toDest=document.getElementById(controlclientid + "inputArrivalLoc" + i).value;
  	           if  (_fromDest == "" )
  	           {
  	              alert("נא למלא יעד יציאה ")
  	              return false;
  	           }
  	             if  (_toDest == "" )
  	           {
  	              alert("נא למלא יעד חזרה ")
  	              return false;
  	           }
  	           if (_fromDest == _toDest)
  	            {
      	            alert("יעד ההגעה חייב להיות שונה מיעד היציאה")
  	                return false;
  	            }
            }

        return true;
    }
    
    function ShowMoreSegments(linkobject)
    {
       document.getElementById("tdMoresegments").style.display="none";
       for (i=max_segments_display+1; i<=max_segments; ++i){
        document.getElementById("segment" +i).style.display = "" ;
        }  
    }
    
    function SetDate1(controlclientid, segmentcount){
	
	   for (i=1; i<=max_segments; ++i){ 
	        var departuredate = document.getElementById("fdt"+i).value;    
            document.getElementById(controlclientid+"hdn_departuredate"+i).value = departuredate;
        }
	}
	
	function CheckDates(controlclientid, segmentcount)
	{
	   var _fromdateStr = document.getElementById("fdt1").value;
	   if (segmentcount == 1) {
	    if (_fromdateStr == "") {
	        alert("נא לבחור תאריך יציאה");
  	        return false;
	     } else { return true; }
	   }
	     	   
  	   var _todateStr =document.getElementById("fdt2").value;
  	   if (_fromdateStr == "" || _todateStr == "")
  	   {
  	        alert("נא לבחור תאריכי יציאה וחזרה");
  	        return false;
  	   }
  	   
  	   var _fromDate  = parseDate2(_fromdateStr);	   
       var _toDate  = parseDate2(_todateStr);
       
       if (_toDate <= _fromDate)
       {
   	        alert("תאריך החזרה חייב להיות לאחר תאריך היציאה");
  	        return false;
       } 
       return true;
	}
  
  function parseDate2(date)
  {
     var d = new String(date);
     var slash = d.indexOf("/");
     var day = d.substr(0, slash);
     //alert(day);
     d = d.substr(slash+1, d.length-slash-1);
     slash = d.indexOf("/");
     var month = d.substr(0, slash);
     month = parseInt(month, 10) - 1;
     //alert(month);
     d = d.substr(slash+1, d.length-slash-1);
     var year = d;     
     //alert(year);
     var dateObj = new Date();
     dateObj.setFullYear(year, month, day);
    // alert(dateObj);
     return dateObj;
  }
  
  function CheckPassengers(controlclientid) {
    var _adults = parseInt(document.getElementById(controlclientid + "ddlAdults").value) + parseInt(document.getElementById(controlclientid + "ddlSenior").value);
    if (_adults < 1) { alert('נא לבחור לפחות נוסע אחד מעל גיל 18'); return false; }
    else if (_adults + parseInt(document.getElementById(controlclientid + "ddlChild").value) + parseInt(document.getElementById(controlclientid + "ddlInfant").value) > 9) { alert('לא ניתן לבחור יותר מ 9 נוסעים'); return false;}
    return true;        
  }
  
