// JavaScript Document

function isLeapYear(dTest) {
	var y = dTest.getYear();
	var bReturn = false;

	if(y % 4 == 0) {
		if(y % 100 != 0) {
			bReturn = true;
		} else {
			if (y % 400 == 0) {
				bReturn = true;
			}
		}
	}
	return bReturn;
}


function trim (strVar) { 
	if(strVar.length >0)
	{
		while(strVar.charAt(0)==" ")			//remove left spaces
			strVar=strVar.substring(1,strVar.length);
		while(strVar.charAt(strVar.length-1)==" ")			//remove right spaces
			strVar=strVar.substring(0,strVar.length-1);
	}
	return strVar;
}

function checkEmail(email) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email))
	{
		return (true);
	}
	return false;
}


expires = new Date();
expires.setTime (expires.getTime() + 24 * 60 * 60 * 150 * 1000);


function setCookie(name, value){
	if (value.length > 0)document.cookie = name + "=" + escape(value)+ ";"+"expires=" + expires.toGMTString()+";"
}

function check(thisForm)
{	

	var arrival_date= thisForm.arrival_date.value;
	var depart_date	= thisForm.depart_date.value;
	var No_of_Adults = trim(thisForm.No_of_Adults.value);

	var Room_Single = trim(thisForm.Room_Single.value);
	var Room_Double = trim(thisForm.Room_Double.value);
	var Room_Triple = trim(thisForm.Room_Triple.value);
	var name		= trim(thisForm.sender_name.value);
	var email		= trim(thisForm.email.value);
	var country		= trim(thisForm.country.value);

	var dd1 = arrival_date.substring(0,2)
	var mm1 = arrival_date.substring(3,5)
	var yy1 = arrival_date.substring(6,10)

	var dd2 = depart_date.substring(0,2)
	var mm2 = depart_date.substring(3,5)
	var yy2 = depart_date.substring(6,10)

	if(arrival_date.length==0)
	{
		alert("Please enter Checkin Date!")
		thisForm.arrival_date.focus();
		return false;
	}
	if(depart_date.length==0)
	{
		alert("Please enter Checkout Date!")
		thisForm.depart_date.focus();
		return false;		
	}
	if(!dateDiff(dd1, mm1, yy1, dd2, mm2, yy2))
	{
		alert("Please recheck Start and End date!");
		thisForm.arrival_date.focus();
		return false;
	}
	if(No_of_Adults.length==0)
	{
		alert("Please enter No. of Adults!")
		thisForm.No_of_Adults.focus();
		return false;
	}
	if(isNaN(No_of_Adults))
	{
		alert("Please enter valid value in No. of Adults!");
		thisForm.No_of_Adults.select();
		return false;
	}

	var No_of_Children = thisForm.No_of_Children.value;
	if(No_of_Children.length>0)
	{
		for(i=0;i<No_of_Children;i++)
		{
			var ag = thisForm['age['+i+']'];
			if(ag.value.length==0)
			{
				alert("Please enter age of Child - "+(i+1));
				thisForm['age['+i+']'].focus();
				return false;
			}
			if(isNaN(ag.value))
			{
				alert("Please enter age only numeric");
				thisForm['age['+i+']'].select();
				return false;
			}
		}
	}

	if (name.length==0)
	{
		alert("Please enter your name!")
		thisForm.sender_name.focus();
		return false;	
	}

	if (email.length==0)
	{
		alert("Please enter your email address!")
		thisForm.email.focus();
		return false;	
	}
	if(checkEmail(email) == false)
	{
		alert("Invalid email! Please re-enter.");
		thisForm.email.select();
		return false;
	}
	if(thisForm.nationality[0].checked==false && thisForm.nationality[1].checked==false)
	{
		alert("Please select Nationality!");
		thisForm.nationality[0].focus();
		return false;			
		}
	if (country.length==0)
	{
		alert("Please select your Country!");
		thisForm.country.focus();
		return false;
	}

var S_fax= trim(thisForm.S_fax.value);
var S_streetaddress= trim(thisForm.S_streetaddress.value);
var S_city= trim(thisForm.S_city.value);
var S_pin= trim(thisForm.S_pin.value);
var cvalue=name+'|'+email+'|'+S_fax+'|'+S_streetaddress+'|'+S_city+'|'+S_pin+'|'+country+'|'+arrival_date+'|'+depart_date;
var cname="newSavion" ;
setCookie(cname,cvalue)	;

return true;
}

function child_age(age)
{
	a1.style.display = "none";
	a2.style.display = "none";
	a3.style.display = "none";
	a4.style.display = "none";
	a5.style.display = "none";
	a6.style.display = "none";
	if(age>0)
	{
		childAge.style.display = "";
		if(age>0)
			a1.style.display = "";
		if(age>1)
			a2.style.display = "";
		if(age>2)
			a3.style.display = "";
		if(age>3)
			a4.style.display = "";
		if(age>4)
			a5.style.display = "";
		if(age>5)
			a6.style.display = "";
		if(age>6)
			f1.No_of_Children.value = 6;
	}
	else
		childAge.style.display = "none";
}

function getCurrentDate()
{
	currentTime = new Date();
	
	dd=currentTime.getDate();

	mm=currentTime.getMonth();
	yy=currentTime.getYear();
	mm=mm+1;
	if(dd<10) dd="0"+dd;
	if(mm<10) mm="0"+mm;
	currDate=yy+"-"+mm+"-"+dd;

	return currDate;
}
function dateDiff(dd1, mm1, yy1, dd2, mm2, yy2)
{
	var date1=yy1+mm1+dd1;
	var date2=yy2+mm2+dd2;
	if(date1>date2) return false;
	return true;
}

function get(){
if( (cookie = getCookie("newSavion")) > ""){
	
	Values = cookie.split("|");
	if (Values.length >= 6){
		if (document.f1.sender_name.value.length == 0) document.f1.sender_name.value = Values[0];
		if (document.f1.email.value.length == 0) document.f1.email.value = Values[1];
		if (document.f1.S_fax.value.length == 0) document.f1.S_fax.value=Values[2];
		if (document.f1.S_streetaddress.value.length == 0) document.f1.S_streetaddress.value=Values[3];
		if (document.f1.S_city.value.length == 0) document.f1.S_city.value=Values[4];
		if (document.f1.S_pin.value.length == 0) document.f1.S_pin.value=Values[5];
		if (document.f1.country.value.length == 0) document.f1.country.value=Values[6];
		if (document.f1.arrival_date.value.length == 0) document.f1.arrival_date.value=Values[7];
		if (document.f1.depart_date.value.length == 0) document.f1.depart_date.value=Values[8];
	}
}
return true;
}

function getCookie(Name) {
	var search = Name + "="
	if (document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search) 
		if (offset != -1) { // if cookie exists 
			offset += search.length 
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset) 
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
	}
	return "";
}




window.onload=function()
{get();}


$(document).ready(function() {
  dates="";
 var option="";
 var date="";
mmmax='+30Y+1M +10D';


 dates = $('#arrival_date, #depart_date').datepicker({

											
			minDate:0, maxDate:mmmax,
			showOn: 'both',
			buttonImage:'/query/images/calender_img.gif',
			showAnim:'',
			closeText:'Close',
			autoSize:false,
			buttonImageOnly:true,
			changeMonth: true,
			changeYear: true,
			buttonText: 'Calander',
			showButtonPanel: true,
			//showMonthAfterYear:true,
			numberOfMonths: 2,dateFormat: 'dd/mm/yy',
			onSelect: function(selectedDate) { var option = this.id == dates[0].id ? "minDate" : "maxDate"; var instance = $(this).data("datepicker"); var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings); dates.not(this).datepicker("option", option, date); }
		});
	});