// 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 Form_Submit(frm)
{
	
	with(frm)
	{	
				
		if(!IsEmpty(fname,F_Name_Empty))
		{
			return false;
		}
		if(!IsEmpty(lname,L_Name_Empty))
		{
			return false;
		}
		if(!IsEmpty(address,Address_Empty))
		{
			return false;
		}
		if(!IsEmpty(city,City_Empty))
		{
			return false;
		}
		if(!IsEmpty(state,State_Empty))
		{
			return false;
		}
		if(!IsEmpty(country,Country_Empty))
		{
			return false;
		}
		if(!IsEmpty(email,Email_Empty))
		{
			return false;
		}
		if(!IsEmail(email,Valid_Email))
		{
			return false;
		}
		if(!IsEmpty(confirm_email,CEmail_Empty))
		{
			return false;
		}
		if(!IsEmail(confirm_email,CValid_Email))
		{
			return false;
		}
		if(email.value != confirm_email.value)
		{
		  alert(Match_Email);
		  confirm_email.focus();
		  return false;
		}		 	
		if(!IsValidInteger(day_phone,Phone_Empty))
		{
			return false;
		}
		if(!frm.condition.checked)
		{
			alert("You must confirm that you have read and agree to Terms & condition and Privacy Policy");
			return false;
		}
	return true;
	}
}
//====================================================================================================
//	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(day,month,year,day_out,month_out,year_out)
{
	
	with(document.frmweek)
	{
		if(day<10)
		{
			day="0"+day;
		}
		if(day_out<10)
		{
			day_out="0"+day_out;
		}
		checkin_date=day+"-"+month+"-"+year;
		checkout_date=day_out+"-"+month_out+"-"+year_out;
		dt1=getDateObject(checkin_date,"-");
		dt2=getDateObject(checkout_date,"-");
		/*alert(checkin_date);
		alert(checkout_date);
		alert(dt1==dt2);*/
		var one_day=1000*60*60*24;


		var diff=Math.ceil((dt2.getTime()-dt1.getTime())/(one_day));
		if(dt1>dt2)
		{		
			alert(L_Proper_Arrival_Date);
			return false;
		}
		else if(checkin_date==checkout_date)
		{	
			alert(L_Proper_Arrival_Date);
			return false;
		}
		/*else if(diff > 14)
		{		
			alert(Wrong_Date_Range);
			return false;
		}*/
		else
		{	
			Submit.value = "Check Availability";
			flag=Show_Room(day+'-'+month+"-"+year);
		}
	}
}

function adddate(day,month,year,frm,cval2)
{
	with(frm)
	{
	
	day   = parseInt(day) + 1;
	var monthc=month;
	
	if(cval2=='M' || cval2=='Y' || cval2=='D')
		month = monthc-1;
	else	
		month = monthc-1;
	
	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;
    day   = newDate.getDate();
    formattedDate = month + '/' + day + '/' + year;
	
	daylist_out.value=day;
	monthlist_out.value=month;
	yearlist_out.value=year;
	
	}
	
}

function Show_Room(Adate)
{
	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,"-");
		if(dt1<dt2)
		{		
			alert(L_Proper_Arrival_Date);
			return false;
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}
}
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 Change_Month(Month,flag)
{ 
	 with(document.frmweek)
	 {
		checkin_date=daylist.value+"-"+monthlist.value+"-"+yearlist.value;
		checkout_date=daylist_out.value+"-"+monthlist_out.value+"-"+yearlist_out.value;
		dt1=getDateObject(checkin_date,"-");
		dt2=getDateObject(checkout_date,"-");
		
		if(dt1>dt2)
		{
			alert(L_Proper_Arrival_Date);
			monthlist_out.value=monthlist.value;
			yearlist_out.value=yearlist.value;
			daylist_out.value=daylist.value;
		}
		/*if(flag=='I')
		{
			var m=parseInt(Month)+1;
			if(m<10)
				m="0"+m;
			if(m<=12)
			{
			 monthlist_out.value=m;
			 yearlist_out.value=parseInt(yearlist.value);
			}
			else
			{
				monthlist_out.value='01';
				yearlist_out.value=parseInt(yearlist.value)+1;
			}
		}
		else
		{
			var m=parseInt(Month)-1;
			if(m<10)
				m="0"+m;
			if(m>0)
			{
			 monthlist.value=m;
			 yearlist.value=parseInt(yearlist_out.value);
			}
			else
			{
				monthlist.value='01';
				yearlist.value=parseInt(yearlist_out.value)-1;
			}	
		}*/
		 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;
}
//checking for other charges fields
function Submit_Form()
{
	with(document.frmothercharge)
	{	
		var roominventory = parseInt(room_inventory.value);
		var roomrequest = parseInt(no_of_room.value);
		
		if(!IsEmpty(no_of_room,L_Empty_Room))
		{
			return false;
		}

		if(!IsValidInteger(no_of_room,L_Valid_Room))
		{
			
			return false;
		}

		if(no_of_room.value<=0)
		{
			alert(L_Zero_Room);
			no_of_room.focus();
			return false;
		}
		if(roominventory < roomrequest)
		{
			//alert(JM_Max_room+" "+room_inventory.value+" "+JM_Max_room_Avai);
			//"JM_Max_room"	=> "Maximum",
			//"JM_Max_room_Avai"	=>" Room Available."
			alert("Maximum"+" "+room_inventory.value+" "+"Room Available.");
			no_of_room.focus();
			return false;
		}
		if(!IsEmpty(extra_adult,L_Valid_Adult))
		{
			return false;
		}
		if(!IsValidInteger(extra_adult,L_Valid_Adult))
		{
			return false;
		}
		if(extra_adult.value<=0)
		{
			alert(L_Valid_Adult);
			extra_adult.focus();
			return false;
		}
		if(!IsEmpty(extra_child,L_Valid_Child))
		{
			return false;
		}
		if(!IsValidInteger(extra_child,L_Valid_Child))
		{
			return false;
		}
		var c=0;
		if(ChargeId.value!='')
		{
			var data=ChargeId.value;
			var arrdata=data.split(",");
			var i;
			var regex = /^[0-9]*$/;
			//alert(data);
			for(i=0; i< arrdata.length-1 ;i++)
				{
					var t="c_"+ arrdata[i];
					var a=eval(t + ".value");
					if(a!='')
					{
						if(!regex.test(a))
						{
							alert(L_Valid_Charge);
							eval(t+".focus()");
							eval(t+".select()");
							c++;
							return false;
							
						}
					}//if
				}
				if(c!=0)
					return false;
		}
	return true;
	}
}
function Privacy_Click(page_Id)
{
	with(document.frmtwoweek)
	{
		var hotel_id1=hotel_id.value;
		popupWindowURL("document.php?page_id="+page_Id+"&hotel_id="+hotel_id1, 'PageContent', 650, 500, false, false, true);
	}
}
function Terms_Click(page_Id)
{
	with(document.frmtwoweek)
	{
		var hotel_id1=hotel_id.value;
		popupWindowURL("document.php?page_id="+page_Id+"&hotel_id="+hotel_id1, 'PageContent', 650, 500, false, false, true);
	}
}
function View_Gallary(Room_Id,Total)
{
	with(document.frmroom)
	{
		popupWindowURL("gallary.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 850, 760, false, false, true);
	}
}
function Next_Click()
{
		with(document.frmview)
		{
			action.value="Next";
			submit();
		}
}
function Previous_Click()
{
		with(document.frmview)
		{
			action.value="Previous";
			submit();
		}
}
