var maxpeople = 7;
var maxnights = 7;
var daystoadd = 1;
var peopledefault = 1;
var nightdefault = 1;
var groupname;
var istest = false;
var template = 'b1';


function testit()
{
	alert('test');
}

function loadengine(name)
{
	//load the engine elements
	loadengineelements();
	
	groupname = name;

	groupname = groupname.replace(' ', '');
	
	var curdate = new Date();
	curdate.setDate(curdate.getDate() + daystoadd);
	
	var daynum = curdate.getDate();
	var monthnum = curdate.getMonth();
	var yearnum = curdate.getFullYear();
	
	loaddateddl(yearnum);
	loadotherddl();
	
	var dayddl = document.getElementById("selectHtlDay");
	var monthddl = document.getElementById("selectHtlMonth");
	var yearddl = document.getElementById("selectHtlYear");
	
	dayddl.selectedIndex = (daynum-1);
	monthddl.selectedIndex = (monthnum);
	
	for( j=0; j<yearddl.options.length; j++)
	{
		
		if(eval(yearddl.options[j].value)==eval(yearnum))
		{
			yearddl.selectedIndex=j;
		}
	}
	
	document.getElementById("selectHtlPeople").selectedIndex=(peopledefault-1);
	document.getElementById("selectHtlNights").selectedIndex=(nightdefault-1);
	
	loadcountryDropDownListjs();
}

function loadenginewithtemplate(templatearg)
{
	//load the engine elements
	loadengineelements();
	
	groupname = templatearg;
	template = templatearg;
	
	var curdate = new Date();
	curdate.setDate(curdate.getDate() + daystoadd);
	
	var daynum = curdate.getDate();
	var monthnum = curdate.getMonth();
	var yearnum = curdate.getFullYear();
	
	loaddateddl(yearnum);
	loadotherddl();
	
	var dayddl = document.getElementById("selectHtlDay");
	var monthddl = document.getElementById("selectHtlMonth");
	var yearddl = document.getElementById("selectHtlYear");
	
	dayddl.selectedIndex = (daynum-1);
	monthddl.selectedIndex = (monthnum);
	
	for( j=0; j<yearddl.options.length; j++)
	{
		if(eval(yearddl.options[j].value)==eval(yearnum))
		{
			yearddl.selectedIndex=j;
		}
	}
	
	document.getElementById("selectHtlPeople").selectedIndex=(peopledefault-1);
	document.getElementById("selectHtlNights").selectedIndex=(nightdefault-1);
	
	loadcountryDropDownListjs();
}


function loadcountryDropDownListjs()
{
	//load countrys
	var countryddl = document.getElementById("selectHtlCountry"); 

	var countrydata = getcountrynamearr();
	var countryiddata = getcountryidarr();
	
	document.getElementById("selectHtlLocation").options[0] = new Option('Select a Location', -1);
	
	if(countrydata.length > 1)
	{
		countryddl.options[0] = new Option('Select a Country', -1);	
		
	
		for (var i = 0; i < countrydata.length; i++) 
		{
			countryddl.options[(i+1)] = new Option(countrydata[i], countryiddata[i]);	
		}
	}
	else if(countrydata.length == 1)
	{
		countryddl.options[0] = new Option(countrydata[0], countryiddata[0]);	
		htlcountryddlchanged();
	}
	else
	{
		alert('Error loading engine.');
	}
}

function htlcountryddlchanged()
{ 
	var countryddl = document.getElementById("selectHtlCountry");
	var locationddl = document.getElementById("selectHtlLocation"); 
	locationddl.length = 1;
	
	if(countryddl.options.length > 1)
	{
		if(countryddl.selectedIndex > 0)
		{
			var locationnamesdata = getlocationnamearr(countryddl.selectedIndex);
			var locationiddata = getlocationidarr(countryddl.selectedIndex);
	
			for (var i = 0; i < locationnamesdata.length; i++) 
			{
				locationddl.options[(i+1)] = new Option(locationnamesdata[i], locationiddata[i]);	
			}
		}
	}
	else
	{
		var locationnamesdata = getlocationnamearr(1);
		var locationiddata = getlocationidarr(1);
	
		for (var i = 0; i < locationnamesdata.length; i++) 
		{
			locationddl.options[(i+1)] = new Option(locationnamesdata[i], locationiddata[i]);	
		}
	}
}



function loadengineelements()
{
	var countryddl = document.createElement("select");
	countryddl.id = "selectHtlCountry";
	countryddl.name = "selectHtlCountry";
	countryddl.onchange = htlcountryddlchanged;
	
	var locationddl = document.createElement("select");
	locationddl.id = "selectHtlLocation";
	locationddl.name = "selectHtlLocation";
	
	//dates
	var monthddl = document.createElement("select");
	monthddl.id = "selectHtlMonth";
	monthddl.name = "selectHtlMonth";
	
	var dayddl = document.createElement("select");
	dayddl.id = "selectHtlDay";
	dayddl.name = "selectHtlDay";
	
	var yearddl = document.createElement("select");
	yearddl.id = "selectHtlYear";
	yearddl.name = "selectHtlYear";
	
	var peopleddl = document.createElement("select");
	peopleddl.id = "selectHtlPeople";
	peopleddl.name = "selectHtlPeople";
	
	//night ddl
	var nightddl = document.createElement("select");
	nightddl.id = "selectHtlNights";
	nightddl.name = "selectHtlNights";
	
	//button
	var searchbtn = document.createElement("input");
	searchbtn.type = "button";
	searchbtn.value = "Search";
	searchbtn.id = "searchHtlButton";
	searchbtn.name = "searchHtlButton";
	searchbtn.onclick = gotohtlbooking;
	
	//travelfat link
	var linkdiv = document.createElement("div");
	
	var linkhtml = '<div id="linkdiv"><a href="http://www.travelfat.com" style="FONT-SIZE: 7pt; FONT-FAMILY: Verdana, Arial; TEXT-DECORATION: none; color: #000000;">powered by </a>';
	
	if(istest)
	{
		linkhtml = linkhtml + '<a href="http://www.travelfat.com" style="FONT-WEIGHT: bold; FONT-SIZE: 8pt; FONT-FAMILY: Verdana, Arial; TEXT-DECORATION: none; color: #000000;">';
		linkhtml = linkhtml + '<img src="http://localhost/radicaltravel/b1/assets/searchengine/logo.png" style="Border: none;" alt="TravelFAT"/></a></div>';
	}
	else
	{
		linkhtml = linkhtml + '<a href="http://www.travelfat.com" style="FONT-WEIGHT: bold; FONT-SIZE: 8pt; FONT-FAMILY: Verdana, Arial; TEXT-DECORATION: none; color: #000000;">';
		linkhtml = linkhtml + '<img src="http://securebookings.travelfat.com/b1/assets/searchengine/logo.png" style="Border: none;" alt="TravelFAT"/></a></div>';
	}
	
	linkdiv.innerHTML = linkhtml;

    var para = document.getElementById("tfatengineportal");
    
    var topdiv = document.createElement("div");
	topdiv.id = "topdiv";
	
	//Country div------------------------------------
	
	var countrydiv = document.createElement("div");
	countrydiv.id = "countrydiv";
	
	var spncountry = document.createElement("span");
	spncountry.id = "spncountry";
	spncountry.innerHTML = "Country";
	countrydiv.appendChild(spncountry);
	
	var countryddldiv = document.createElement("div");
	countryddldiv.id = "countryddldiv";
	countryddldiv.appendChild(countryddl);
	
	countrydiv.appendChild(countryddldiv);
	topdiv.appendChild(countrydiv);
	
	
	//Location div------------------------------------
	
	var locationdiv = document.createElement("div");
	locationdiv.id = "locationdiv";
	
	var spnlocation = document.createElement("span");
	spnlocation.id = "spnlocation";
	spnlocation.innerHTML = "Location";
	locationdiv.appendChild(spnlocation);
	
	var locationddldiv = document.createElement("div");
	locationddldiv.id = "locationddldiv";
	locationddldiv.appendChild(locationddl);
	
	locationdiv.appendChild(locationddldiv);
	topdiv.appendChild(locationdiv);
	
	
	//Arrival Date div------------------------------------
	
	var datediv = document.createElement("div");
	datediv.id = "datediv";
	
	var spndate = document.createElement("span");
	spndate.id = "spndate";
	spndate.innerHTML = "Arrival Date";
	datediv.appendChild(spndate);
	
	var dateddldiv = document.createElement("div");
	dateddldiv.id = "dateddldiv";
	dateddldiv.appendChild(monthddl);
	dateddldiv.appendChild(dayddl);
	dateddldiv.appendChild(yearddl);
	datediv.appendChild(dateddldiv);
	
	topdiv.appendChild(datediv);
	
	
	//People div------------------------------------
	
	var peoplediv = document.createElement("div");
	peoplediv.id = "peoplediv";
	
	var spnpeople = document.createElement("span");
	spnpeople.id = "spnpeople";
	spnpeople.innerHTML = "People";
	peoplediv.appendChild(spnpeople);
	
	var peopleddldiv = document.createElement("div");
	peopleddldiv.id = "peopleddldiv";
	peopleddldiv.appendChild(peopleddl);
	
	peoplediv.appendChild(peopleddldiv);
	topdiv.appendChild(peoplediv);
	
	
	//Nights div------------------------------------
	
	var nightdiv = document.createElement("div");
	nightdiv.id = "nightdiv";
	var spnnight = document.createElement("span");
	spnnight.id = "spnnight";
	spnnight.innerHTML = "Nights";
	nightdiv.appendChild(spnnight);
	
	var nightddldiv = document.createElement("div");
	nightddldiv.id = "nightddldiv";
	nightddldiv.appendChild(nightddl);
	
	nightdiv.appendChild(nightddldiv);
	topdiv.appendChild(nightdiv);
	
	
	var buttondiv = document.createElement("div");
	buttondiv.id = "buttondiv";
	buttondiv.appendChild(searchbtn);
	topdiv.appendChild(buttondiv);
	
	para.appendChild(topdiv);
	
	//para.appendChild(linkdiv);

}

function loaddateddl(yearnum)
{
	var monthname = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
	var monthddl = document.getElementById("selectHtlMonth");
	var dayddl = document.getElementById("selectHtlDay");
	var yearddl = document.getElementById("selectHtlYear");
	
	for ( i=0; i<12; i++ ) 
	{
	
		monthddl.options[i] = new Option(monthname[i], (i+1));
	}
	
	for ( d=0; d<31; d++ ) 
	{
		dayddl.options[d] = new Option((d+1), (d+1));
	}
	
	for ( y=0; y<5; y++ ) 
	{
		yearddl.options[y] = new Option((yearnum+y), (yearnum+y));
	}
}

function loadotherddl()
{
	var nightsddl = document.getElementById("selectHtlNights");
	
	for ( n=0; n<maxnights; n++ ) 
	{
		nightsddl.options[n] = new Option((n+1), (n+1));
	}
	
	var peopleddl = document.getElementById("selectHtlPeople");
	
	for ( p=0; p<maxpeople; p++ ) 
	{
		peopleddl.options[p] = new Option((p+1), (p+1));
	}
}


function gotohtlbooking()
{	
	var peopleddl = document.getElementById("selectHtlPeople"); 
	var nightsddl = document.getElementById("selectHtlNights"); 
	
	var countryddl = document.getElementById("selectHtlCountry");
	var locationddl = document.getElementById("selectHtlLocation");

	var people = peopleddl.options[peopleddl.selectedIndex].value;
	var nights = nightsddl.options[nightsddl.selectedIndex].value;
	var countryid = countryddl.options[countryddl.selectedIndex].value;
	var locationid = locationddl.options[locationddl.selectedIndex].value;
	
	var dayddl = document.getElementById("selectHtlDay");
	var monthddl = document.getElementById("selectHtlMonth");
	var yearddl = document.getElementById("selectHtlYear");
		
	var dayval = dayddl.options[dayddl.selectedIndex].value;
	var monthval = monthddl.options[monthddl.selectedIndex].value;
	var yearval = yearddl.options[yearddl.selectedIndex].value
	
	dayval = checknum(dayval);
	monthval = checknum(monthval);

	var searchdate = dayval + '-' + monthval + '-' + yearval;

	if(checkdateisvalid(dayval, monthval, yearval))
	{	
		if(checkargs(countryid, locationid))
		{	
			if(istest)
			{
							
				top.location.href='http://widget.travelonomy.com/js_to_pp.php?PortalName=' + groupname
							+ '&SearchDate=' + searchdate + '&people=' + people + '&nights=' + nights 
							+ '&countryID=' + countryid + '&locationID=' + locationid;
			}
			else
			{		
				top.location.href='http://widget.travelonomy.com/js_to_pp.php?PortalName=' + groupname
							+ '&SearchDate=' + searchdate + '&people=' + people + '&nights=' + nights 
							+ '&countryID=' + countryid + '&locationID=' + locationid;
			}
		}
	}
	else
	{			
		alert('Warning: Date invalid.');
	}
	
}

function checknum(num)
{
	if(num < 10)
	{
		num = '0' + num;

	}
	return num;
}

function checkargs(countryid, locationid)
{

	if(countryid == -1)
	{
		alert('Please select a country.');
		return false;
	}
	else if(locationid == -1)
	{
		alert('Please select a location.');
		return false;
	}
	else
	{
		return true;
	}
}


function checkdateisvalid(d, m, y)
{
	var monthlengths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	var curdate = new Date();
	curdate.setDate(curdate.getDate() + daystoadd);
	var currdaynum = curdate.getDate();
	var currmonthnum = curdate.getMonth();
	var curryearnum = curdate.getFullYear();
	
	currmonthnum = currmonthnum + 1;

	if(y < curryearnum)
	{
		return false;
		
	}		
	else if(y == curryearnum)
	{
		if(m < currmonthnum)
		{
			return false;
		}
		else if(m == currmonthnum)
		{
			if(d < currdaynum)
			{
				return false;
			}			
		}
	}
	
	if(m==2)
	{
		var modulus = y % 4;
		if(modulus==0)
		{						
			if(d > 29)			
			{
				return false;
			}
		}
		else
		{					
			if(d > monthlengths[(m-1)])			
			{
				return false;
			}
		}
	}	
	else		
	{		
		if(d > monthlengths[(m-1)])
		{
			return false;
		}
	}

	return true;
}
