//events to be bound when the page loads
var fail=true;
$(document).ready(function(){
	var selected, idNum; 
	var userName;
	var passcode;
	var gender=0;
	$(".menu").click(function(){
	dispSelectAirport();
	dispSelectHotel();
	window.location='index.php';
	});
	$(".selectAirport").toggle(dispChooseAirport, dispSelectHotel);
	$(".selectHotel").toggle(dispChooseHotel, dispSelectAirport);
	$('h3').click(slideUp);
	$('#logo').mouseover(function(){
		$("#logo").css('background-image','url(/images/logoOn.png)');
	});
	$('#logo').mouseout(function(){
		$("#logo").css('background-image','url(/images/logoOff.png)');
	});
})

//ajax call to retrieve a list of locations
function getLocation(type,location, id) {
	var loc;
	if(location=='0'){
		loc='country';
	}
	else if(location=='1'){
		loc='state';
	}
	else if(location=='2'){
		loc='city';
	}
	else if(location=='3'){
		loc='location';
	}
	$(selected + loc ).html('Loading...');	
	$.ajax({
		url: 'include/getLoc.php',
		type: 'GET',
		dataType: 'html',
		data: "type="+type+"&l="+location+"&i="+id,
		success: function(html){
			//alert(selected+loc);
			$(selected + loc ).html(html);	
			$(selected + loc +' a').bind("click", getLoc)
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
	});
}


//populates the next set of locations
function getLoc(){
	$(this).parent().parent().next().next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
	var string=$(this).parent().attr("id");
	var name=$(this).html();
	var brokenstring=string.split("_"); 
	var type=brokenstring[0]
	var location=brokenstring[1];
	var id=brokenstring[2];
	if(location=='1'){
		$('.country').html(name);
	}
	if(location=='2'){
		$('.state').html(name);
	}
	if(location=='3'){
		$('.city').html(name);
	}
	if(location=='4'){
		idNum=id;
		showUser();
	}
	else{
		getLocation(type,location, id);
	}
	return false;
}

//function to initiate the chat application
function showUser(){
	var tmp;
	var tmp2;
	if(selected=='#a'){
		tmp='.chooseAirport';
		tmp2='.airportTop';
	}
	else{
		tmp='.chooseHotel';
		tmp2='.hotelTop';
	}
	$(tmp).find(".whiteStuff").fadeOut("normal",function(){
	$(tmp).find(".whiteName").fadeIn();
	});

	return false;
}


function startHotelChat(){
	$(".homeDisclaimer").fadeOut();
	var anticache=new Date().getTime();
	passcode=$('#hotelpassCode').attr("value");
	userName=$('#hotelUserName').attr("value");
	gender=$('.select:checked').val();
	if(userName != undefined){
		$.ajax({
			url: 'include/checkHotel.php',
			type: 'GET',
			dataType: 'html',
			data: "name="+userName+"&pass="+passcode+"&id="+idNum+"&c="+anticache,
			success: function(html){
				if(html=='1'){
					$("#hotelMessage").slideUp('');
					initChat();
				}
				else if(html=='2'){
					$("#hotelMessage").html("The name you have entered is invalid or already in use");
					$("#hotelMessage").slideDown('slow',function(){
						$(".hotelMessageFill").slideDown('slow');
					});
					userName='';
				}
				else if(html=='3'){
					$("#hotelMessage").html("The hotel Id you entered is invalid");
					$("#hotelMessage").slideDown('slow');
					userName='';
				}
				else if(html=='4'){
					$("#hotelMessage").html("There was an error loggin in, please enter all fields");
					$("#hotelMessage").slideDown('slow');
					userName='';
				}
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	}
	else{
		$("#hotelMessage").html("Please enter all Fields");
		$("#hotelMessage").slideDown('slow');
	}
	return false;
}


// - - - - - - - - - - - - function to check the credentials on the airport chat - - - - - - - - - - - //
function startAirportChat(){
	var anticache=new Date().getTime();
	userName=$('#airportUserName').attr("value");
	gender=$('.select:checked').val();
	if(userName != undefined){
		$.ajax({
			url: 'include/checkAirport.php',
			type: 'GET',
			dataType: 'html',
			data: "name="+userName+"&id="+idNum+"&c="+anticache,
			success: function(html){
				if(html=='1'){
					$("#airportMessage").slideUp('');
					initChat();
				}
				else if(html=='2'){
					$("#airportMessage").html("The name you have entered is invalid or already in use");
					$("#airportMessage").slideDown('slow');
					userName='';
				}
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	}
	else{
		$("#airportMessage").html("Please enter all Fields");
		$("#airportMessage").slideDown('slow');
	}
	return false;
}

function initChat(){
	var anticache=new Date().getTime();
	$.ajax({
		url: 'include/showChat.php',
		type:"GET",
		data:"id="+idNum+"&name="+userName+"&c="+anticache,
		dataType: 'text',
		success: function(html){
			startChat();
			$(".wideStuff").html(html);
			showChat();
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
	});
}

// - - - - - - - - - - - - - Shows the large white background for the chat, called in the initChat function - - - - - - - - - //
function showChat(){
	wait();
	$(".small").fadeOut( "normal" , function(){
		$(".wide").fadeIn();
	});
}


// - - - - - - - - - -walks back up the location list - - - - - - - - - - - - - - //
function slideUp(){
	$(this).next().slideToggle('fast').siblings('div:visible').slideUp('fast');
}

// - - - - - - - - - - - displays the hotel location list - - - - - - - - - - - - - - - - //
function dispChooseHotel(){
	
	$(".selectAirport").fadeOut("fast", function(){
		selected='#hotel';
		$(".chooseHotel").fadeIn();
		
		getLocation('0','0','0');
	});
}

// - - - - - - - - - - - - - - displays the airport location list - - - - - - - - - - -//
function dispChooseAirport(){
	$(".homeDisclaimer").fadeOut();
	$(".selectHotel").fadeOut("fast",function(){
		selected='#a';
		$(".chooseAirport").fadeIn();
		getLocation('1','0','0');
	});
}

// - - - - - - - - - - - - displays the main hotel selection button - - - - - - - - - - - //
function dispSelectHotel(){
	$(".wide").fadeOut("normal", function(){
		$(".chooseAirport").fadeOut("fast", function(){
			$(".selectHotel").fadeIn("fast", function(){
				$(".small").fadeIn();
			})
		})
	})
}

// - - - - - - - - - - - - - - - displays the main airport selection button - - - - - - - - - - - - //
function dispSelectAirport(){
	$(".wide").fadeOut("normal",function(){
		$(".chooseHotel").fadeOut("fast", function(){
			$(".selectAirport").fadeIn("fast",function(){
				$(".small").fadeIn();
			})
		})
	})
}
//function to show and display one of the misc pages from the footer
function showMisc(page){
	$(".wide").fadeOut( "normal" , function(){
		$.ajax({
			url: 'include/showMisc.php',
			type:"GET",
			data:"page="+page,
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html("<div class=\"miscTitle\"></div><div class=\"misc\">"+html+"</div>");
				$(".miscTitle").html(page);
				showChat();
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	});
	return false;
}
function showFAQ(){
	$(".wide").fadeOut( "normal" , function(){
		$.ajax({
			url: 'include/showMisc.php',
			type:"GET",
			data:"action=faq",
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html("<div class=\"miscTitle\">FAQ</div>"+html);
				$(".question").bind("click", function(){
					$(this).next().slideToggle('fast');
				})
				showChat();
			},
			error: function(request,status,error){
				//alert('Error in showFAQ! status: '+status+" error: "+error+" request: "+request);
			}
		});
	});
	return false;
}

// - - - - - -  - - - - - Displays the "add your hotel" form - - - - - - - - - - - - //
function showAdd(){
	$(".small").fadeOut();
	$(".wide").fadeOut( "normal" , function(){
		$.ajax({
			url: 'include/addYourHotel.php',
			type:"GET",
			data: "",
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html(html);
				$(".wide").fadeIn();
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	});
	return false;
}



// - - - - - - - - - - - - handles the "add your hotel" form submission - - - - - - - - - - - - - - //
function handleAdd(){
	if($("#addNameIn").attr("value")==undefined){
		$("#addName").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addCountryIn").attr("value")==undefined){
		$("#addCountry").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addStateIn").attr("value")==undefined){
		$("#addState").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addCityIn").attr("value")==undefined){
		$("#addCity").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addAddressIn").attr("value")==undefined){
		$("#addAddress").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addZipIn").attr("value")==undefined){
		$("#addZip").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addPhoneIn").attr("value")==undefined){
		$("#addPhone").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addContactNameIn").attr("value")==undefined){
		$("#addContactName").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addEmailIn").attr("value")==undefined){
		$("#addEmail").addClass("error");
		$("#errorDiv").html("Sorry, but you must fill in the required fields first!");
	}
	if($("#addNameIn").attr("value")==undefined || $("#addCountryIn").attr("value")==undefined || $("#addStateIn").attr("value")==undefined || $("#addCityIn").attr("value")==undefined || $("#addAddressIn").attr("value")==undefined || $("#addZipIn").attr("value")==undefined || $("#addPhoneIn").attr("value")==undefined || $("#addContactNameIn").attr("value")==undefined || $("#addEmailIn").attr("value")==undefined){
		;;
	}
	else{
		$(".error").html("");
		$.ajax({
			url: 'include/handleForm.php',
			type:"GET",
			data: "type="+$(".hotelType:checked").val()+"&name="+$("#addNameIn").attr("value")+"&id="+$("#addIdIn").attr("value")+"&country="+$("#addCountryIn").attr("value")+"&state="+$("#addStateIn").attr("value")+"&city="+$("#addCityIn").attr("value")+"&address="+$("#addAddressIn").attr("value")+"&zip="+$("#addZipIn").attr("value")+"&phone="+$("#addPhoneIn").attr("value")+"&fax="+$("#addFaxIn").attr("value")+"&website="+$("#addWebsiteIn").attr("value")+"&contact="+$("#addContactNameIn").attr("value")+"&email="+$("#addEmailIn").attr("value"),
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html(html);
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	}
}


// - - - - - -  - - - - - Displays the "add your hotel" form - - - - - - - - - - - - //
function showContact(){
	$(".small").fadeOut();
	$(".wide").fadeOut( "normal" , function(){
		$.ajax({
			url: 'include/contact.php',
			type:"GET",
			data: "",
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html(html);
				$(".wide").fadeIn();
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	});
	return false;
}
// - - - - - - - - - - - - handles the "add your hotel" form submission - - - - - - - - - - - - - - //
function handleContact(){
	if($("#contactNameIn").attr("value")==undefined){
		$("#contactName").addClass("error");
		$("#errorDiv2").html("Sorry, but you must fill in the required fields first!");
	} 
	if($("#contactEmailIn").attr("value")==undefined){
		$("#contactEmail").addClass("error");
		$("#errorDiv2").html("Sorry, but you must fill in the required fields first!");
	} 
	if($("#contactCommentIn").attr("value")==undefined){
		$("#contactComment").addClass("error");
		$("#errorDiv2").html("Sorry, but you must fill in the required fields first!");
	} 
	if($("#contactNameIn").attr("value")==undefined || $("#contactEmailIn").attr("value")==undefined || $("#contactCommentIn").attr("value")==undefined ){
		;;
	}
	else{
		$(".error").html("");
		$.ajax({
			url: 'include/contactSend.php',
			type:"GET",
			data: "name="+$("#contactNameIn").attr("value")+"&email="+$("#contactEmailIn").attr("value")+"&comments="+$("#contactCommentIn").val(),
			dataType: 'html',
			success: function(html){
				$(".wideStuff").html(html);
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}
		});
	}
}


// - - - - - - - - - - - - takes a user back to the list of locations from a chat- - - - - - //
function showLoc(){
	var tmp;
	var tmp2;
	if(selected=='#a'){
		tmp='.chooseAirport';
		tmp2='.airportTop';
	}
	else{
		tmp='.chooseHotel';
		tmp2='.hotelTop';
	}
	$(tmp).find(".whiteName").fadeOut("normal",function(){
	$(tmp).find(".whiteStuff").fadeIn();
	});
	return false;
}

function showChatLoc(){
	var tmp;
	var tmp2;
	if(selected=='#a'){
		tmp='.chooseAirport';
		tmp2='.airportTop';
	}
	else{
		tmp='.chooseHotel';
		tmp2='.hotelTop';
	}
	$(tmp).find(".whiteName").hide();
	$(tmp).find(".whiteStuff").show();
	$('.wide').fadeOut('normal', function(){
		$('.small').fadeIn();
		endChat();
	});
	return false;
}

function getAd() {
	//create a bs variable to make sure IE doesn't cache the page
	var anticache=new Date().getTime();
	$.ajax({
		url: 'include/ads.php',
		type: 'GET',
		dataType: 'html',
		data: 'chache='+anticache,
		success: function(html){
			//replace a div with the returned content
			$(".adZone").fadeOut("normal", function(){
				$(".adZone").html(html);
				setTimeout('$(".adZone").fadeIn(\'normal\');', 1000);		
			});
			
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
	});
	wait();
}
function wait() {
	//waits one minute, then recalls the getAd() function
	wTimer=setTimeout('getAd();', 60000);
}