
//debugger;
//debugger;

//this function is for syncronising the dates of check in and check out according to the number of nights selected
function SyncDates(sId, newDate)
{
	//by Yonatan - To catch : active was not defined
	
	if (typeof(active)=="undefined" )
		return;
	

	//debugger
	var numChanged   = (sId == null);
	var firstValDate =  ( newDate != null );	
	var strCCtrlChildNameDay     = 'ddlDay';
	var strCCtrlChildNameMon     = 'ddlMonth';	
	var strCnumNightFullName     = active.strNumOfNights;//'ddlNumOfNights'
	
	var strCCtrlParentNameIn = active.strCCtrlParentNameIn; //'@DPCheckIn';  //dpCheckIn
	var strCCtrlFullNameInDay    = strCCtrlParentNameIn + '_' + strCCtrlChildNameDay;     //value will be (dpCheckIn + ddlDay)
	var strCCtrlFullNameInMon    = strCCtrlParentNameIn + '_' + strCCtrlChildNameMon;	  //value will be (dpCheckIn + ddlMonth)	
	var strCParentCtrlFullNameIn = strCCtrlParentNameIn;		//value will be (dpCheckIn)
	
			
	strCCtrlParentNameOut = active.strCCtrlParentNameOut; //'@DPCheckOut';   //dpCheckOut	
	var strCCtrlFullNameOutDay    = strCCtrlParentNameOut + '_' + strCCtrlChildNameDay;
	var strCCtrlFullNameOutMon    = strCCtrlParentNameOut + '_' + strCCtrlChildNameMon;	
	var strCParentCtrlFullNameOut = strCCtrlParentNameOut ;     //value will be (dpCheckOut)
	
	//get today date
	var today = new Date(dtCurrYear, dtCurrMonth - 1, dtCurrDay, 0, 0, 0, 0);
	
	/* the following lines can be ignored right now as they relate to flights module*/
	//by gurdev - To catch : "One Way" or "Multi" in Flight SpeedBook which doesn't have Return Date
	try
	{
		if( !((document.getElementById(strCCtrlFullNameInDay).selectedIndex != 0 &&
			document.getElementById(strCCtrlFullNameInMon).selectedIndex != 0) || 
			(document.getElementById(strCCtrlFullNameOutDay).selectedIndex != 0 &&
			document.getElementById(strCCtrlFullNameOutMon).selectedIndex != 0)) )
		{
			return;
		}
	}
	catch(e)
	{
		return;
	}
		
	if( document.getElementById(strCCtrlFullNameInDay).selectedIndex == 0 ||
		document.getElementById(strCCtrlFullNameInMon).selectedIndex == 0) //dpCheckIn + ddlDay || dpCheckIn + ddlMonth
		{
			sId = strCParentCtrlFullNameOut;   //dpCheckOut			
			newDate = new Date(dtCurrYear, document.getElementById(strCCtrlFullNameOutMon).selectedIndex - 1 ,document.getElementById(strCCtrlFullNameOutDay).selectedIndex ,0,0,0,0);			
		}
	if( sId != null && ( document.getElementById(strCCtrlFullNameOutDay)== null || document.getElementById(strCCtrlFullNameOutMon) == null ||document.getElementById(strCCtrlFullNameOutDay).selectedIndex == 0 ||
		document.getElementById(strCCtrlFullNameOutMon).selectedIndex == 0 ))
		{
			sId = strCParentCtrlFullNameIn;			
			newDate = new Date(dtCurrYear, document.getElementById(strCCtrlFullNameInMon).selectedIndex - 1 ,document.getElementById(strCCtrlFullNameInDay).selectedIndex ,0,0,0,0);		
		}
		
	if( numChanged )
	{
		// num of nights changed
		sId = strCParentCtrlFullNameIn;
		var thisDay  = document.getElementById(sId+"_ddlDay");
		var thisMonth = document.getElementById(sId+"_ddlMonth");
		
		var newDate = new Date(dtCurrYear, thisMonth.selectedIndex - 1 ,thisDay.selectedIndex,0,0,0,0);
		
		if(newDate < today)			
			newDate = new Date(dtCurrYear + 1, thisMonth.selectedIndex - 1 ,thisDay.selectedIndex,0,0,0,0);
	}

	if( numChanged && firstValDate )					//take note of this
	{
		sId = strCParentCtrlFullNameOut;
		var outDay = document.getElementById(sId+"_ddlDay");
		var outMonth  = document.getElementById(sId+"_ddlMonth");
		
		var outDate = new Date(dtCurrYear, outMonth.selectedIndex - 1 ,outDay.selectedIndex,0,0,0,0);
		var now = new Date(dtCurrYear, dtCurrMonth - 1, dtCurrDay, 0, 0, 0, 0);
		
		if( outDate < now)
			outDate = new Date(dtCurrYear + 1, outMonth.selectedIndex - 1 ,outDay.selectedIndex,0,0,0,0);
		document.getElementById(strCnumNightFullName).selectedIndex = ( outDate - newDate)/(24*60*60*1000) - 1 ;
		return;
	}
	else if(sId.indexOf( strCCtrlParentNameIn )!= -1)
	{
		if (typeof(Hotels) != "undefined" && active == Hotels)
			syncId = sId.replace(strCCtrlParentNameIn, strCCtrlParentNameOut);
		else // packages
			syncId = strCParentCtrlFullNameOut;
		dir = 1;
	}
	else
	{
		if (typeof(Hotels) != "undefined" && active == Hotels)
			syncId = sId.replace(strCCtrlParentNameOut, strCCtrlParentNameIn);	
		else // packages
			syncId = strCParentCtrlFullNameIn;
		dir = -1;
	}
	if( !numChanged && dir == -1 )
	{
		if (typeof(Hotels) != "undefined" && active == Hotels) {
			//keep the original day and month
			var originalDay = document.getElementById(syncId+"_ddlDay").selectedIndex;
			var originalMonth = document.getElementById(syncId+"_ddlMonth").selectedIndex - 1;
			var originalYear = dtCurrYear;
			
			var tmpOrgYear = new Date(originalYear , 
									originalMonth ,
									originalDay );
									
			if( tmpOrgYear < today )
			{
				originalYear += 1;
			}
			
			var baseDate = new Date(originalYear , 
									originalMonth ,
									originalDay ,
									0,0,0,0);
			
			document.getElementById(strCnumNightFullName).selectedIndex = (newDate - baseDate)/(24*60*60*1000) -1 ;
			return;
		}
		else  { // packages			
			var baseDate = new Date(dtCurrYear, document.getElementById(syncId+"_ddlMonth").selectedIndex - 1 ,document.getElementById(syncId+"_ddlDay").selectedIndex,0,0,0,0);
			if( baseDate < today)
				baseDate.setFullYear( baseDate.getFullYear()+1);
			document.getElementById(strCnumNightFullName).selectedIndex = (newDate - baseDate)/(24*60*60*1000) - 1 ;
			return;
		}
	}
		
		
	var deltaDays = document.getElementById(strCnumNightFullName).value ;
	
	if( deltaDays != "" )
	{	

		try
		{
			//var SyncDate = new Date(newDate.getTime() + dir*(deltaDays * 24*60*60*1000));
			var SyncDate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate() + dir*deltaDays);
			//THIS IS A WALKAROUND - REMOVE ONLY IF YOU HAVE OTHER SOLUTION
			//in netscape if you run first the "SyncDayByMonth" function
			//and than set the SelectedIndex, it doesn't set it.
			//
			//Therfore - First set the SelectedIndex, than run "SyncDayByMonth" (which remembers the selected day)
			//and for that - make sure you have 31 days in the ddl before setting the selected index.
			document.getElementById(syncId+"_ddlDay").options[29] = new Option( 29, 29 ); 
			document.getElementById(syncId+"_ddlDay").options[30] = new Option( 30, 30 );
			document.getElementById(syncId+"_ddlDay").options[31] = new Option( 31, 31 );
			document.getElementById(syncId+"_ddlDay").selectedIndex = SyncDate.getDate();
			SyncDayByMonth( document.getElementById(syncId+"_ddlDay") , SyncDate );
			document.getElementById(syncId+"_ddlMonth").selectedIndex = SyncDate.getMonth()+1;
		}
		catch(e)
		{}
	}
	else
	{
		if( dir == -1 )
		{
			newDate = new Date(dtCurrYear, document.getElementById(strCCtrlFullNameInMon).selectedIndex - 1 ,document.getElementById(strCCtrlFullNameInDay).selectedIndex ,0,0,0,0);
			if( newDate < today )
				newDate.setFullYear( newDate.getFullYear()+1);
		}
		sId = strCParentCtrlFullNameOut;
		var outDay = document.getElementById(sId+"_ddlDay");
		var outMonth  = document.getElementById(sId+"_ddlMonth");
		
		var outDate = new Date(dtCurrYear, outMonth.selectedIndex - 1 ,outDay.selectedIndex,0,0,0,0);
		
		if( outDate < today )
			outDate.setFullYear( outDate.getFullYear()+1);
		document.getElementById(strCnumNightFullName).selectedIndex = ( outDate - newDate)/(24*60*60*1000) - 1 ;
		return;
	}
}
