//validation function for checking values entered by users

function validate(byval)
{
	var obj = eval("document."+byval);
			
	//checking if from location selected properly
	if(obj.ddlFrom.value=="" || obj.ddlFrom.value=="--------------------------------")
	{
		alert('Please choose from the available locations');
		obj.ddlFrom.focus();
		return false;
	}
	//checking if to location selected properly
	if(obj.ddlTo.value=="" || obj.ddlTo.value=="--------------------------------")
	{
		alert('Please choose from the available locations');
		obj.ddlTo.focus();
		return false;
	}
	//checking if from and to are the same
	if(obj.ddlFrom.value.split(' - ')[1]==obj.ddlTo.value.split(' - ')[1])
	{
		alert('Departure Location and Destination Location cannot be the same');
		obj.ddlTo.focus();
		return false;
	}

	//checking for valid departure date
	if(obj.txtDepDate.value=="dd/mm/yyyy" || obj.txtDepDate.value=="")
	{
		alert('Please select Departure Date');
		obj.txtDepDate.focus();
		return false;
	}
	var dep = obj.txtDepDate.value.split('/');
	
	if(dep.length!=3)
	{
		alert('1');
		alert('Invalid Departure Date');
		obj.txtDepDate.focus();
		return false;
	}
	
	var depDD=dep[0];
	if(depDD.substring(0,1)=="0")//removing leading 0 as causing problem with javascript calendar dates going behind DropDownList
	{
		depDD=depDD.substring(1,2);
	}
	var depMM=dep[1];
	if(depMM.substring(0,1)=="0")
	{
		depMM=depMM.substring(1,2);
	}
	var depYYYY=dep[2];
	
	//validating departure date entered
	if(parseInt(depDD) < 1 || parseInt(depDD) > 31 || parseInt(depMM) < 1 || parseInt(depMM) > 12 || depYYYY.length != 4)
	{
		alert('2');
		alert('Invalid Departure Date');
		obj.txtDepDate.focus();
		return false;
	}
	if(depDD.length!=2)
	{
		depDD = "0" + depDD;
	}
	if(depMM.length!=2)
	{
		depMM = "0" + depMM;
	}
	var depDate = depYYYY + "-" + depMM + "-" + depDD;
	if(depDate.length!=10)
	{
		alert('3');
		alert('Invalid Departure Date');
		obj.txtDepDate.focus();
		return false;
	}
		
		
	if(checkDate(depDD, depMM, depYYYY)==false)
	{
		alert('4');
		alert('Invalid Departure Date');
		obj.txtDepDate.focus();
		return false;
	}

	var today=new Date();
	var todayplus2=new Date(eval(today.getMonth()+1)+"/"+eval(today.getDate()+2)+"/"+today.getFullYear());
	var todayplus1year=new Date(eval(today.getMonth()+1)+"/"+today.getDate()+"/"+eval(today.getFullYear()+1));

	dep_date=new Date(depMM+"/"+depDD+"/"+depYYYY).valueOf()
	
	if(dep_date < todayplus2)
	{				
		alert('Departure Date should be atleast 2 days from current date');
		obj.txtDepDate.focus();
		return false;
	}
	
	if(dep_date > todayplus1year)
	{
		alert('Bookings can only be made for 1 year from today');
		obj.txtDepDate.focus();
		return false;
	}//end of validating departure date entered
	
	//checking for valid return date
	if(obj.rbRoundTrip.checked==true)
	{
		if(obj.txtRetDate.value=="dd/mm/yyyy" || obj.txtRetDate.value=="")
		{
			alert('Please select Return Date');
			return false;
		}
		var ret = obj.txtRetDate.value.split('/');
		
		if(ret.length!=3)
		{
			alert('Invalid Return Date');
			obj.txtRetDate.focus();
			return false;
		}
	
		var retDD=ret[0];
		if(retDD.substring(0,1)=="0")
		{
			retDD=retDD.substring(1,2);
		}		
		var retMM=ret[1];
		if(retMM.substring(0,1)=="0")
		{
			retMM=retMM.substring(1,2);
		}
		
		var retYYYY=ret[2];
		
		//validating return date entered
		if(parseInt(retDD) < 1 || parseInt(retDD) > 31 || parseInt(retMM) < 1 || parseInt(retMM) > 12 || retYYYY.length != 4)
		{
			alert('Invalid Return Date');
			obj.txtRetDate.focus();
			return false;
		}

		if(retDD.length!=2)
		{
			retDD = "0" + retDD;
		}
		if(retMM.length!=2)
		{
			retMM = "0" + retMM;
		}

		var retDate = retYYYY + "-" + retMM + "-" + retDD;
		if(retDate.length!=10)
		{
			alert('Invalid Return Date');
			obj.txtRetDate.focus();
			return false;
		}

		if(checkDate(retDD, retMM, retYYYY)==false)
		{
			alert('Invalid Return Date');
			obj.txtRetDate.focus();
			return false;
		}

		ret_date=new Date(retMM+"/"+retDD+"/"+retYYYY).valueOf()
		
		if(ret_date < dep_date)
		{				
			alert('Return Date cannot be before Departure Date');
			obj.txtRetDate.focus();
			return false;
		}
		
		if(ret_date > todayplus1year)
		{
			alert('Bookings can only be made for 1 year from today');
			obj.txtRetDate.focus();
			return false;
		}//end of validating return date entered
	}//end of if rbRoundTrip.checked ==true	

	//comparing no of adults and infants
	//if(obj.rbDomestic.checked==true)
	//{
		if(obj.ddlAdults.value < obj.ddlInfants.value)
		{
			alert('Number of infants cannot be greater than the number of adults');
			obj.ddlAdults.focus();
			return false;
		}
	//}

	if(byval=="Form_FlightsIndex")
	{
		if(document.getElementById('chkIndian').checked==false)
			alert('We only accept credit cards issued in India at the moment.');
	}

	//checking if current page is index page or flight search page
	//as only these 2 pages have Domestic & International radio buttons
	if(byval=="Form_FlightsIndex" || byval=="Form_FlightSearch")
	{
		if(obj.rbDomestic.checked==true)
		{
			//getting flight urls
			return getFlights(byval);
		}
		else
		{
			return true;
		}
	}//end of if(byval=="Form_FlightsIndex")
	else
	{
		if(obj.hidRoute.value=="Domestic")
		{
			//getting flight urls
			return getFlights(byval);
		}
		else
		{
			return true;
		}
	}//end of else //not index page
}//end of function validate

function checkDate(dd, mm, yyyy)
{
	var returnVal=true;

	if(mm=="01" || mm=="03" || mm=="05" || mm=="07" || mm=="08" || mm=="10" || mm=="12")
	{
		//no validations here as dd < 1 and dd > 31 have already been validated
	}
	else if(mm!="02" && dd=="31")	//checking for 30 day months
	{
		returnVal=false;
	}
	else if(mm=="02")							//checking for February month
	{
		if((isleap(yyyy)==false && parseInt(dd) > 28) || (isleap(yyyy)==true && parseInt(dd) > 29))
		{
			returnVal=false;
		}
	}
	return returnVal;				
}//end of function checkDate

function isleap(yr)
{ 
	if ((parseInt(yr)%4) == 0)
	{ 
		if (parseInt(yr)%100 == 0)
		{ 
			if (parseInt(yr)%400 != 0)
			{ 
				return false; 
            } 
            if (parseInt(yr)%400 == 0)
			{ 
				return true; 
            } 
        } 
        if (parseInt(yr)%100 != 0)
		{ 
			return true; 
        } 
	} 
	if ((parseInt(yr)%4) != 0)
	{ 
		return false; 
    }
}//end of function isleap

function testReset(byval)
{
	alert('Hi');
	return false;
}