function show(div){
	if($('#'+div).length > 0)
		$('#'+div).css('display', 'block');
}
function hide(div){
	if($('#'+div).length > 0)
		$('#'+div).css('display', 'none');
}

function checkPhoneNumber(box){
	var val = box.value;
	var v = "";
	var valid = "0 1 2 3 4 5 6 7 8 9 - + ( )";
	var bits = valid.split(" ");
	for(var i=0; i<val.length; i++){
		if(bits.indexOf(val.charAt(i))!=-1)
			v+= val.charAt(i);
	}
	box.value = v;
}

function formatPhoneNumber(box){	
	var val = box.value;
	//now add spaces to make it easier to read
	var len = val.length;
	
	var t = "";
	if(len > 7)
		t += val.substring(0,len-7)+" ";
	if(len>=7)
		t += val.substring(len-7,len-4)+" "+val.substring(len-4,len);
	
	box.value = t;
}

//================================================
//general functions
//================================================
function validateEmail(email){
	var atpos = email.indexOf("@");
	var domain = email.substring(atpos,email.length);
	if(!(atpos > 0) || (domain.indexOf(".") == -1) || (domain.indexOf(".") == (domain.length-1)) || email.indexOf(" ")!=-1){
		return false;
	} else {
		return true;
	}
}

function displayMessage(message,colour){
	if(colour==null){
		colour = "red";
	}
	$('#message_space').html("<div id='message'><div id='message_div'>"+message+"</div></div>");
	$('#message_div').css('backgroundColor', colour);
	$('#message_space').css('height', "auto");
}

function clearMessage(){
	$('#message_space').html("");
	$('#message_space').css('height', "0");
}

function checkEnquiry(){
	if($('#name').val() == ""){ 
		displayMessage("Please fill in your name in the box below");
		gotoField('name');
		return false;
	} else if(!validateEmail($('#email').val())){
		displayMessage("Please enter a valid email address i.e name@emailprovider.com");
		gotoField('email');
		return false;
	} else if($('#tel').val()==""){
		displayMessage("Please fill in your phone number in the box below");
		gotoField('tel');
		return false;
	} else if($('#enquiry').val()==""){
		displayMessage("Please enter your enquiry in the box below");
		gotoField('enquiry');
		return false;
	} else {
		return true;
	}
}

function gotoField(div){
	if($('#'+div).length > 0)
		$('#'+div).focus();	
}

function over(div){
	$('#'+div+"_button").attr('src', "graphics/"+div+"_button_over.jpg");	
}
function out(div){
	$('#'+div+"_button").attr('src', "graphics/"+div+"_button.jpg");	
}

function loadPage(url,waitdiv,loaddiv,postvars){
	if(waitdiv==null)
		waitdiv = 'content_div';
	if(loaddiv==null || !$(loaddiv))
		loaddiv = waitdiv;
		
	if(waitdiv != "")
		$('#'+waitdiv).html('<p align="center"><span style="display:block; height:100%; vertical-align:middle;">Please wait:<br /><img src="/graphics/connecting.gif"></span></p>');
	$('#'+loaddiv).load(url);
}


function newVerify(){
	loadPage('core_verify.php', 'verify_div');	
}

function loadTabs(){
	var img = new Image();
	var tabs = new Array("home","tours","courses","hotels","sights","transport","enquiry");
	for(i=0; i<tabs.length; i++)
		img.src = "/graphics_new/tabs/"+tabs[i]+"_selected.gif";
}

function highlightTab(tab){
	$('#'+tab).attr('src', "/graphics_new/tabs/"+tab+"_selected.gif");
}
function unhighlightTab(tab){
	$('#'+tab).attr('src', "/graphics_new/tabs/"+tab+".gif");
}

var prevTourSummary = -1;
function showTourSummary(number){
	if($('#summarylist'+prevTourSummary).length > 0)
		$('#summarylist'+prevTourSummary).removeClass();
	if($('#summary'+prevTourSummary).length > 0)
		$('#summary'+prevTourSummary).css('display', "none");
	
	$('#summarylist'+number).addClass('current');
	
	if($('#summary'+number).length > 0)
		$('#summary'+number).css('display', "block");
	
	prevTourSummary = number;
}

var prevList = "_";

function showArea(area){
	if($('#list'+prevList).length > 0)
		$('#list'+prevList).removeClass("current");
	if($('#area'+prevList).length > 0)
		$('#area'+prevList).css('display', "none");
	$('#list'+area).addClass('current');
	$('#area'+area).css('display', "block");

	var temp = $("#area"+area+" .thumb");
	
	$.each(temp, function(){
		if(this.title != "")
			this.src = this.title;
	});
	
	prevList = area;
}

function showCourseDetails(course_id){
	var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null){
		//IF IT'S IE, THE BOX NEEDS TO BE BIGGER
		displayModalBox('/content/core_course_details.php?ID='+course_id,'800','395');
	} else {
		//OTHER BROWSERS
		displayModalBox('/content/core_course_details.php?ID='+course_id,'800','370');
	}
}

function showHotelDetails(hotel_id){
	var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null){
		//IF IT'S IE, THE BOX NEEDS TO BE BIGGER
		displayModalBox('/content/core_hotel_details.php?ID='+hotel_id,'800','395');
	} else {
		//OTHER BROWSERS
		displayModalBox('/content/core_hotel_details.php?ID='+hotel_id,'800','370');
	}
}

function showSightDetails(sight_id){
	var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null){
		//IF IT'S IE, THE BOX NEEDS TO BE BIGGER
		displayModalBox('/content/core_sight_details.php?ID='+sight_id,'800','395');
	} else {
		//OTHER BROWSERS
		displayModalBox('/content/core_sight_details.php?ID='+sight_id,'800','370');
	}
}

function showTourSummary(tour_id){
	var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null){
		//IF IT'S IE, THE BOX NEEDS TO BE BIGGER
		displayModalBox('/content/core_tour_summary.php?ID='+tour_id,'800','300');
	} else {
		//OTHER BROWSERS
		displayModalBox('/content/core_tour_summary.php?ID='+tour_id,'800','300');
	}
}

function calculatePrice(currency){
	loadPage('/content/core_updatePrice.php?currency='+currency,'', 'price_div');
}

function toCurrency(val){
	bits = String(val).split(".");
	str = bits[0]+".";
	if(bits.length == 1)
		str += "00";
	else if(String(bits[1]).length >= 2)
		str += String(bits[1]).substr(0,2);
	else
		str += bits[1]+"0";
	return str;
}

function showVid(label, video){
	if($('#'+prevList).length > 0)
		$('#'+prevList).removeClass();
	
	$('#list'+label).addClass("current");
	prevList = 'list'+label;
	
	$('#video_div').css('display', "block");
	
	includeSWF('video_div','http://www.youtube.com/v/'+video+'&hl=en&fs=1&rel=0&color1=0x817b4e&color2=0xb1c470&border=0', '500', '375');
}

function highlight(div){
	if($('#'+div).length > 0)
		$('#'+div).addClass('current');
}

function unhighlight(div){
	if(div != prevList)
		$('#'+div).removeClass();
}

function newEnquiry(type){
	switch(type){
		case "general":
		case "customise_step1":
			$('#enquiry_buttons').css('display', 'none');
			loadPage('content/core_enquiry_'+type+'.php	','enquiry_content');
			break;
		case "customise_step2":
			$('enquiry_buttons_2').css('display', 'none');
			loadPage('content/core_enquiry_'+type+'.php	','CoursesAndHotels');
			break;
	}
}

var customPackageItems = new Array();

function createMarker(lat, lng, map, icon) {
	var marker;
	var myLatlng = new google.maps.LatLng(lat, lng);
	
	if(icon != undefined){
		marker = new google.maps.Marker({
			position: myLatlng, 
			map: map,
			title:"",
			icon: icon
		});
	} else {
		marker = new google.maps.Marker({
			position: myLatlng, 
			map: map,
			title:""
		});
	}
	return marker;
}

function toggleCourse(course_id, lat, lng){
	if($('#course'+course_id).attr("checked")){
		//ADD
		customPackageItems['course'+course_id] = createMarker(lat,lng, sideMap, '/graphics_new/golf.png');
		$("#dummy_ajax").load("addCourse"+course_id+"ToMyTour.html");
	} else {
		//REMOVE
		var marker = customPackageItems['course'+course_id];
		if(marker != null)
			marker.setVisible(false);
		$("#dummy_ajax").load("removeCourse"+course_id+"FromMyTour.html");
	}
}

function checkHotel(hotel_id, lat, lng){
	if($('#hotel'+hotel_id).val() != "NULL"){
		//ADD
		customPackageItems['hotel'+hotel_id] = createMarker(lat,lng, sideMap, '/graphics_new/hotel.png');
		$("#dummy_ajax").load("addHotel"+hotel_id+"ToMyTour.html");
	} else {
		//REMOVE
		var marker = customPackageItems['hotel'+hotel_id];
		if(marker != null)
			marker.setVisible(false);
		$("#dummy_ajax").load("removeHotel"+hotel_id+"FromMyTour.html");
	}
}

function checkFlight(){
	if($('#flights').attr('checked')){
		$('#flight_from').css('display', "block");
		$('#flight_from_label').css('display', "block");
	} else {
		$('#flight_from').css('display', "none");
		$('#flight_from_label').css('display', "none");
	}
}

function checkVerify(){
	var errors = "";
	
	if($("#enquiry_name").val() == ""){
		errors += "\nName";
		$("#enquiry_name").focus();
	}
	
	if($("#enquiry_email").val() == ""){
		if(errors == "")
			$("#enquiry_email").focus();
		
		errors += "\nEmail address";	
	}
	
	if($("#enquiry_tel").val() == ""){
		if(errors == "")
			$("#enquiry_tel").focus();
	
		errors += "\nA contact phone number";
	}
	
	if($("#enquiry_text").length != 0 && $("#enquiry_text").val() == ""){
		if(errors == "")
			$("#enquiry_text").focus();
		
		errors += "\nYour enquiry";
	}
	
	if(errors == "")
		loadPage('core_verify_check.php?code='+$('#verify').val(),'','verify_div');	
	else
		alert("The following information is missing:\n"+errors);
}

function displayModalBox(url, w, h, title){
	h = parseInt(h)+105;
	w = parseInt(w)+20;
	
	x = Math.floor($(window).width() / 2) - (w/2);
	y = Math.floor($(window).height() / 2) - (h/2);
	
	if(y < 10){
		y = 10;
	}
	
	if(y + h > $(window).height()){
		h = $(window).height() - (y + 10);
	}
	
	x = Math.floor(x);
	y = Math.floor(y);
	w = Math.floor(w);
	h = Math.floor(h);
	
	$("#modal_dialogue").html("");
	$("#modal_dialogue").load(url);
	$("#modal_dialogue").dialog({ 
	  modal:true,
	  draggable:false,
	  title:title,
	  width:w,
	  height:h,
	  resizable:false,
	  position:[x, y],
	  buttons: { 
		"Close": function() { $(this).dialog("close"); $(this).html(""); } 
	  }
	}); 
}

function displayImageBox(image, w, h){
	h = parseInt(h)+105;
	w = parseInt(w)+20;
	
	x = Math.floor($(window).width() / 2) - (w/2);
	y = Math.floor($(window).height() / 2) - (h/2);
	
	if(y < 10){
		y = 10;
	}
	
	if(y + h > $(window).height()){
		ratio = w / h;
		h = $(window).height() - (y + 10);
		newW = h * ratio;
		
		diff = (w - newW) / 2;
		x += diff;
		
		w = newW;
	}
	
	x = Math.floor(x);
	y = Math.floor(y);
	w = Math.floor(w);
	h = Math.floor(h);
	
	$("#image_dialogue").html("");
	$("#image_dialogue").load("/content/core_show_image.php?url="+image);
	$("#image_dialogue").dialog({ 
	  modal:true,
	  draggable:false,
	  width:w,
	  height:h,
	  resizable:false,
	  position:[x, y],
	  buttons: { 
		"Close": function() { $(this).dialog("close"); $(this).html(""); } 
	  }
	}); 
	
	//$("#image_dialogue").animate({width:w, height:h}, 500);
}

function showImage(url, w, h, title){
	h = parseInt(h)+105;
	w = parseInt(w)+20;
	
	x = Math.floor($(window).width() / 2) - (w/2);
	y = Math.floor($(window).height() / 2) - (h/2);
	
	if(y < 10){
		y = 10;
	}
	
	if(y + h > $(window).height()){
		//ratio = w / h;
		h = $(window).height() - (y + 10);
		//w = h * ratio;
	}
	
	x = Math.floor(x);
	y = Math.floor(y);
	w = Math.floor(w);
	h = Math.floor(h);
	
	$("#image_dialogue").dialog({ 
	  modal:true,
	  title:title,
	  resizable:false,
	  position:[x, y],
   	  open: function(event, ui) { $('#image_dialogue').html("<div style='text-align:center;'><img height='100%' src='"+url+"' /></div>"); },
	  buttons: { 
		"Close": function() { $(this).dialog("close"); $(this).html(""); } 
	  }
	});
	
	$("#image_dialogue").animate({width:w, height:h}, 500);
}
 
function closeDialogueBox(){
	$("#modal_dialogue").dialog('close');
}

function addCourseToMyTour(course_id, lat, lng){
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Remove From My Tour": function() { removeCourseFromMyTour(course_id, lat, lng) } } );
	
	if($("#course"+course_id).length > 0){
		$("#course"+course_id).attr('checked','checked');
		toggleCourse(course_id, lat, lng);
	} else {
		$("#dummy_ajax").load("addCourse"+course_id+"ToMyTour.html");
	}
}

function addHotelToMyTour(hotel_id, lat, lng){
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Remove From My Tour": function() { removeHotelFromMyTour(hotel_id, lat, lng) } } );
	
	if($("#hotel"+hotel_id).length > 0){
		$("#hotel"+hotel_id).val("1 night");
		checkHotel(hotel_id, lat, lng);
	} else {
		$("#dummy_ajax").load("addHotel"+hotel_id+"ToMyTour.html");
	}
}

function addSightToMyTour(sight_id, lat, lng){
	$("#dummy_ajax").load("addSight"+sight_id+"ToMyTour.html");
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Remove From My Tour": function() { removeSightFromMyTour(sight_id, lat, lng) } } );
}


function removeCourseFromMyTour(course_id, lat, lng){
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Add To My Tour": function() { addCourseToMyTour(course_id, lat, lng) } } );
	
	if($("#course"+course_id).length > 0){
		$("#course"+course_id).removeAttr('checked');
		toggleCourse(course_id, lat, lng);
	} else {
		$("#dummy_ajax").load("removeCourse"+course_id+"FromMyTour.html");
	}
}

function removeHotelFromMyTour(hotel_id, lat, lng){
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Add To My Tour": function() { addHotelToMyTour(hotel_id, lat, lng) } } );
	
	if($("#hotel"+hotel_id).length > 0){
		$("#hotel"+hotel_id).val("NULL");
		checkHotel(hotel_id, lat, lng);
	} else {
		$("#dummy_ajax").load("removeHotel"+hotel_id+"FromMyTour.html");
	}
}

function removeSightFromMyTour(sight_id, lat, lng){
	$("#dummy_ajax").load("removeSight"+sight_id+"FromMyTour.html");
	$("#modal_dialogue").dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); }, "Add To My Tour": function() { addSightToMyTour(sight_id, lat, lng) } } );
}
