// JavaScript Document
//====================================================================================================
//	File Name		:	twoweek.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Client side validation in JavaScript.

//
//====================================================================================================

//====================================================================================================
//	Function Name	:	Form_Submit()
//	Purpose			:	This function will executed when user submits a form. It checks validity of
//						every field in the form.
//
//	Parameters		:	frm  - form name
//	Return			:	true or false

//----------------------------------------------------------------------------------------------------

function Check_ava(day,month,year,day_out,month_out,year_out,cday11,cmonth11,cyear11,Check_Var,Wrong_Date)//,hotel_msg,room_msg)
{
	
	with(document.frmweek)
	{
		
		if(day<10)
		{
			day="0"+day;
		}
		if(day_out<10)
		{
			day_out="0"+day_out;
		}
		if(cday1<10)
		{
			cday1="0"+cday1;
		}
		checkin_date=day+"-"+month+"-"+year;
		checkout_date=day_out+"-"+month_out+"-"+year_out;
		select_date=cday11+"-"+cmonth11+"-"+cyear11;//cday1+"-"+cmonth1+"-"+cyear1;
		
		dt1=getDateObject(checkin_date,"-");
		dt2=getDateObject(checkout_date,"-");
		dt3=getDateObject(select_date,"-");
		/*alert(checkin_date);
		alert(checkout_date);
		alert(select_date);*/
		//alert(dt1==dt2);
		var one_day=1000*60*60*24;

		//alert("value : "  +Check_Var);
		var diff=Math.ceil((dt2.getTime()-dt1.getTime())/(one_day));
		//alert(checkin_date);
		//alert(checkout_date);
		//alert('date sel '+select_date);
		/*alert("date : " + dt1);
		alert("date : " + dt2);
		alert("date : " + dt3);*/
		if(noperson.value!='')
		{
			if(!allDigits(noperson.value))
				{
					alert(L_no_of_adult_valid.value);
					noperson.focus();
					noperson.select();
					return false;
				}
		}
		
		if(roomno.value!='')
		{
			if(!allDigits(roomno.value))
				{
					alert(L_no_of_room_valid.value);
					roomno.focus();
					roomno.select();
					return false;
				}
		}
		
		if(dt1>dt2)
		{		
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			//alert("greater");
			return false;
		}
		else if(checkin_date==checkout_date)
		{	
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			//alert("equal");
			return false;
		}
		/*else if(diff > 14)
		{		
			//alert(Wrong_Date_Range);
			alert(Wrong_Date);
			return false;
		}*/
		else if(dt1<dt3)
		{
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		
		else
		{	
			Submit.value = "Check Availability";
			flag=Show_Room(day+'-'+month+"-"+year,day_out+"-"+month_out+"-"+year_out,Check_Var,Wrong_Date);
		}
	}
}



function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting 
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;

	//extract day portion
	curPos=dateString.indexOf(sepChar);
	cDate=dateString.substring(0,curPos);
	
	//extract month portion				
	endPos=dateString.indexOf(sepChar,curPos+1);			cMonth=dateString.substring(curPos+1,endPos);

	//extract year portion				
	curPos=endPos;
	endPos=curPos+5;			
	cYear=curValue.substring(curPos+1,endPos);
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth,cDate);	
	return dtObject;
}

function Show_Room(Adate,Ddate,Check_Var,Wrong_Date)
{
	Stamp = new Date();
	with(document.frmweek)
	{
		var cdate="",cyear,cmonth="";
		var fulldate="";
		cmonth=Stamp.getMonth()+1;
		cdate=Stamp.getDate();
		if(cmonth<10)
		{
			cmonth="0"+cmonth;
		}
		if(cdate<10)
		{
			cdate="0"+cdate;
		}
		fulldate=cdate+"-"+cmonth+"-"+Stamp.getFullYear();
		dt1=getDateObject(Adate,"-");
		dt2=getDateObject(fulldate,"-");
		dt3=getDateObject(Ddate,"-");
		if(dt1<dt2)
		{		
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		else if(dt1>dt3)
		{
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}
	

}



function allDigits(str)
{
	
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	var strLen = str.length;
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}

function adddate(day,month,year,frm,cval2)
{
	with(frm)
	{
	//alert("gg");
	/*alert(day);
	alert(month);
	alert(year);*/
	//alert(month.length);
	//alert("month :" +month);
	//month = parseInt(month);
	//alert("before month : " + month);
	day   = parseInt(day) + 1;
	
	var monthc=month;
	/*if(monthc.length==2)
		var val2=monthc.substr(1);
	else
		var val2=month;*/
	
	/*if(dayc.length==2)
		var val2=document.frmweek.cday1.value;
	else
		var val2=dayc.substr(1);*/
									
	/*if(monthc.length==2)
		var val2=monthc;
	else
		var val2=monthc.substr(1);*/
	
	if(cval2=='M' || cval2=='Y' || cval2=='D')
		month = parseInt(monthc)-1;
	else	
		month = parseInt(monthc)-1;
	//alert(val2);
	
	newDate = new Date ( year, month, day );
	year  = newDate.getFullYear();
    
	if(cval2=='M' || cval2=='Y' || cval2=='D')
		month = newDate.getMonth()+1;
	else
		month = newDate.getMonth()+1;
		
    month = (month<10)?'0'+month:month;
	//alert("parse month : " + month);
    day   = newDate.getDate();
    //day   = (day<10)?'0'+day:day;

    formattedDate = month + '/' + day + '/' + year;
	/*alert(day);
	alert(month);
	alert(year);*/
	daylist_out.value=day;
	monthlist_out.value=month;
	yearlist_out.value=year;
	
	
	}
	
}
///==============================================================================


function calendarCallback(date, month, year)
{
	if(month <= 9)
	{ month = '0'+month }
	if(date <= 9)
	{ date = '0'+date }
	//date =  year +'-' + month + '-' + date ;
	//document.frmweek.news_date.value = date;
	//document.frmweek.news_date.focus();
	document.frmweek.yearlist.value=year;
	document.frmweek.monthlist.value=month;
	document.frmweek.daylist.value=date;
}

//====================================================================================================
//	Function Name	:	Add_Click()
//	Purpose			:	This function will executed when user submits a form. It checks validity of 
//						every field in the form.
//----------------------------------------------------------------------------------------------------
function Add_Click()
{
	with(document.frmtwoweek)
	{
		Action.value = "Add";
		submit();
	}
}

function Check_Ava_Display(hotelid2)
{
	
	with(document.frmweek)
	{ 
	
		//alert(hotelid);
		//SubmitCheck.value = "Availability";
		//selected_month.value = monthlist.value;
		popupWindowURL("avaibility.php?Action=Avaibility&hotelid="+hotelid2, 'PageContent', 800, 400, false, false, true);
		//popupWindowURL("avaibility.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 650, 500, false, false, true);
		//submit();
		
	}
}

function Check_ava_month(day,month,year,day_out,month_out,year_out)
{
	
	with(document.frmweek)
	{ 
	
		SubmitCheck.value = "Availability";
		selected_month.value = monthlist.value;
		//popupWindowURL("avaibility.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 650, 500, false, false, true);
		submit();
		
	}
}

function Show_Room_Ava(Adate)
{
	Stamp = new Date();
	with(document.frmweek)
	{
		var cdate="",cyear,cmonth="";
		var fulldate="";
		cmonth=Stamp.getMonth()+1;
		if(cmonth<10)
		{
			cmonth="0"+cmonth;
		}
		
		if(cdate<10)
		{
			cdate="0"+cdate;
		}
		fulldate=Stamp.getFullYear()+"-"+cmonth+"-"+cdate;
		
		if(Adate<fulldate)
		{		
			alert(L_Proper_Arrival_Date);
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}
}



function Change_Month(Month)
{ 
	 with(document.frmweek)
	 {
		 selected_month.value = Month;
		 submit();
	 } 
}
function Change_Year(Year)
{	
	 with(document.frmweek)
	 {
		  selected_year.value = Year;
		  submit();
	 } 
}
function New_Search()
{
	 with(document.frmweek)
	 {
		Action.value='Reset';
		submit();
	 	
	 }
}

function IsValidInteger(fld,msg)
{
	var regex = /^[0-9]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function Next_Click()
{
		with(document.frmview)
		{
			action.value="Next";
			submit();
		}
}
function Previous_Click()
{
		with(document.frmview)
		{
			action.value="Previous";
			submit();
		}

}

