/* For calling messages from resource files. START */
var ResourceManager = new RM();
function RM()
{
    this.list = new Array();
};

// To add key values in list array.
RM.prototype.AddString = function(key, value)
{
    this.list[key] = value;
    //alert(this.list[key]);
};

// To get key values from list array.
RM.prototype.GetString = function(key)
{
    var result = this.list[key];  
    for (var i = 1; i < arguments.length; ++i)
    {
        result = result.replace("{" + (i-1) + "}", arguments[i]);
    }
    return result;
};
/* For calling messages from resource files. END */

///this function is used to increment the date of y object
function DateIncrement(x,y,increment)
{ 
	var arrDate=x.value.split("/")
	var sDate=arrDate[1]+"/"+arrDate[0]+"/"+arrDate[2]
	var incrementday=new Date(sDate).valueOf()+86400000*increment
	incrementday=new Date(incrementday)
	var yvalue=(incrementday.getDate()+"/"+(incrementday.getMonth()+1)+"/"+incrementday.getFullYear())
	var flag = DateCheck(yvalue,0)
	if (flag)
	{
		y.value = flag;
	}
}

//function VerifyDate(year,month,day)
function VerifyDate(day,year_month)
{
var year=year_month.split('-')[0];
var month=year_month.split('-')[1]; 
timeA = new Date(year,month,1);
timeDifference = timeA - 86400000;
timeB = new Date(timeDifference);
var daysInMonth = timeB.getDate();
if (day > daysInMonth)
{

      CtrlKeyId = 'LeftPanelJS_alertForInvalidDate';
    	Message = ResourceManager.GetString(CtrlKeyId);
    	if(Message == null || Message == "undefined")
    	        Message = "Please enter valid date.";
       alert(Message);   
//alert('Invalid date.');
return false;
}

var systemdate=new Date()
systemdate.setDate(systemdate.getDate() + 1);
systemdate=((systemdate.getMonth()+1)+"/"+systemdate.getDate()+"/"+systemdate.getFullYear())
var systemcurrdate=new Date(systemdate).valueOf();

var sDDate=month+"/"+day+"/"+year;

var chkin=new Date(sDDate).valueOf();

var difference =((chkin-systemcurrdate)/86400000);
if (difference< 0)
{
        CtrlKeyId = 'LeftPanelJS_alertForDepartureDate';
    	Message = ResourceManager.GetString(CtrlKeyId);
    	if(Message == null || Message == "undefined")
    	        Message = "Departure Date can not be less than or equal to today's date.";
       alert(Message);   
    //alert(ResourceManager.GetString('LeftPanelJS_alertForDepartureDate'));
    //alert("Departure Date can not be less than or equal to today's date.");
    return false;
}

return true;
}


///This function is used to check and modify the dade format
function DateCheck(sDate,minperiod){
	var iDateLength
	var iDay
	var sMonth
	var iYear
	var iFirst
	var iSecond
	var bLeapYear
	var sMessage
	sMessage = "Enter a valid Date in dd/mm/yyyy format."
	iDateLength=sDate.length
	
	if (iDateLength < 5) 
		{
		alert (sMessage)
		return false;
		}
	else 
		{
		iFirst=sDate.indexOf("/")
		if (iFirst=="1")
			{
			iDay="0" + sDate.substring(0,1)
			sDate = sDate.substring(2,iDateLength)
		   	}
		else if (iFirst=="2")
			{
			iDay=sDate.substring(0,2)	
			sDate = sDate.substring(3,iDateLength)
			}
		else
			{
			alert (sMessage)
			return false;
			}
		if (iDay=="00")
			{
			alert (sMessage)
			return false;
			}
		if (isNaN(iDay))
			{
			alert(sMessage)
			return false;
			}
		iSecond=sDate.indexOf("/")
		iDateLength=sDate.length
		if (iSecond=="1")
			{
			smonth="0" + sDate.substring(0,1)
			iYear = sDate.substring(2,iDateLength)
		   	}
		else if (iSecond=="2")
			{
			smonth=sDate.substring(0,2)	
			iYear = sDate.substring(3,iDateLength)
			}
		else
			{
			alert(sMessage)
			return false;
			}
		if (smonth=="00")
			{
			alert(sMessage)
			return false;
			}
		if (isNaN(smonth))
			{
			alert(sMessage)
			return false;
			}
		
		if(iYear.length =="1") 
			{
			iYear="200"+iYear
			}
		
		else if(iYear.length =="2") 
			{
			iYear="20"+iYear
			}
		else if (iYear.length=="4")
			{
			iYear= iYear
			}
		else if (iYear.length > "4")
			{
			alert(sMessage)
			return false;
			}
		else if (iYear.length == "0")
			{
			alert(sMessage)
			return false;
			}
		else
			{
			alert(sMessage)
			return false;
			}  	
		if (isNaN(iYear))
			{
			alert(sMessage)
			return false;
			}
		}
		var timeA = new Date(iYear,smonth-1,iDay);
		if(!((timeA.getDate()==iDay) && (timeA.getMonth()==(smonth-1))))
		{
			alert(sMessage)
			return false;
		}
		if (isNaN(minperiod))
			minperiod=0;

		var sysdate=new Date()
		sysdate=((sysdate.getMonth()+1)+"/"+sysdate.getDate()+"/"+sysdate.getFullYear())
		var syscurrdate=new Date(sysdate).valueOf() 
		var minbookperiod=new Date(sysdate).valueOf()+86400000*minperiod

		//Validating Today's date and date diff
		var sDate=smonth+"/"+iDay+"/"+iYear
		var ckin=new Date(sDate).valueOf()
		var diff =((ckin-syscurrdate)/86400000)	
		if (diff < 0)
		{
			alert("Date can not be less than today's date.")
			return false;
		}
		if (diff > 331)
		{
			alert("Date should not exceed 331 days from today's date.")
			return false;
		}
		//validating date and and the min booking period
		
		//alert(minperiod);
		
		diff =((ckin-minbookperiod)/86400000)	
		if (diff < 0)
		{
			alert("A booking must be made a minimum of " + minperiod + " days in advance.")
			return false;
		}
		
	// Check End
	sDate=iDay+'/'+smonth+'/'+iYear
	return sDate;
}

// *********************************************************    END    *****************************************************
//This function is used to validate the date with minperiod
function IsValidDate(minperiod,x,y,msgtxt,module)
{
	if(arguments.length == 2)
	{
		if (!DateCheck(x.value,minperiod*1))
		{
			x.focus();
			x.select();
			return false;
		}else
		{
			x.value = DateCheck(x.value,0);
		}
	}
	else if(y.value == '')
	{
		if (!DateCheck(x.value,minperiod*1))
		{
			x.focus()
			x.select();
			return false;
		}else
		{
			x.value = DateCheck(x.value,0);
		}
	}
	else
	{
		if (!DateCheck(x.value,minperiod*1))
		{
			x.focus()
			x.select();
			return false;
		}else
		{
			x.value = DateCheck(x.value,0);
		}
		if (!DateCheck(y.value,0))
		{
			y.focus()
			y.select();
			return false;
		}else
		{
			y.value = DateCheck(y.value,0);
		}
		var ret_date=y.value.split("/")
		var dep_date=x.value.split("/")
		var dayDate=ret_date[0];
		
		ret_date=ret_date[1]+"/"+ret_date[0]+"/"+ret_date[2]
		dep_date=dep_date[1]+"/"+dep_date[0]+"/"+dep_date[2]

		diff=(new Date(ret_date).valueOf()-new Date(dep_date).valueOf())/86400000

		if (diff < 0)
		{
			alert(msgtxt);
			y.focus();
			y.select();
			return false;
		}
		if(arguments.length > 4)
		{
			if ((diff == 0)&&(module=='H'))
			{
				if(dayDate!=01)
				{
					alert(msgtxt);
					y.focus();
					y.select();
					return false;
				}
			}
		}
		if (!DateCheck(y.value,minperiod*1))
		{
			y.focus()
			y.select();
			return false;
		}
	}		
	return true;	
}

// The function is used to check the dates in Drop Down Box
function validateDate(frm,chkinM,chkinD,chkinY,PreNextFlag,Message)
{
	ckinm=eval("document."+frm+".elements['"+chkinM+"'].options[document."+frm+".elements['"+chkinM+"'].selectedIndex].value")
	ckind=eval("document."+frm+".elements['"+chkinD+"'].options[document."+frm+".elements['"+chkinD+"'].selectedIndex].value")
	ckiny=eval("document."+frm+".elements['"+chkinY+"'].options[document."+frm+".elements['"+chkinY+"'].selectedIndex].value")
	ckin=ckinm+"/"+ckind+"/"+ckiny

	var systemdate=new Date()
	var sysdate,diff
	systemdate=((systemdate.getMonth()+1)+"/"+systemdate.getDate()+"/"+systemdate.getFullYear())
	sysdate=new Date(systemdate).valueOf()
	ckin=new Date(ckin).valueOf()
	diff =((ckin-sysdate)/86400000)	
	
	if ((diff < 0)&&(PreNextFlag =='P'))
	{
		alert(Message + " can not be less than today's date")
		eval("document."+frm+".elements['"+chkinD+"'].focus()")
		return false;
	}
	if ((diff > 0)&&(PreNextFlag =='N'))
	{
		alert(Message + " can not be more than today's date")
		eval("document."+frm+".elements['"+chkinD+"'].focus()")
		return false;
	}
	return true;
}
