//--------------------------------//
//    declare global variables    //
//--------------------------------//

var components_ajax_call_file = '_include/components_ajax.php';

var c_search = "";
var c_refine = "";
var ajax_in_use = 0;
var ajax_prev;
var this_rating;
var review_edit_in_progress = 0;
var new_site_rating = 0;
var edit_rating;

// LINKS

function show_links_page(page_no, n_items, cat_id)
{
	$.get(
		components_ajax_call_file,
		{
			op: 'show_links_page' ,
			page_no: page_no ,
			n_items: n_items ,
			cat_id: cat_id
		}, 
		function(data){
			if(data.response == 'success') {
				var page_data = data.page_data;
				$('#link_container').html(page_data);
			} else {
				// error
			}
		},
		'json'
	);
	//
}

// REVIEWS

function show_review_page(page_no, n_items, cat_id)
{
	$.get(
		components_ajax_call_file,
		{
			op: 'show_review_page' ,
			page_no: page_no ,
			n_items: n_items ,
			cat_id: cat_id
		}, 
		function(data){
			if(data.response == 'success') {
				var page_data = data.page_data;
				$('#review_container').html(page_data);
			} else {
				// error
			}
		},
		'json'
	);
	//
}

// COMMENTS

function delete_comment(comment_chk, edit_key)
{
	if(review_edit_in_progress == 1) return;
	review_edit_in_progress = 1;
	
	var msg_result = confirm("Are you sure you want to delete this comment?");
	if(msg_result == false) {
		review_edit_in_progress = 0;
		return;							// user has changed their mind
	}
	
	// use ajax to delete record from database
	
	$.get(
		components_ajax_call_file,
		{
			op: 'deletecomment' ,
			edit_key: edit_key
		}, 
		
		function(data){
			if(data.response == 'success') {
				$('#comment_wrapper_' + comment_chk).hide();		// hide comment
				$('#your_rating').html(data.new_rating_img);

			} else {
				alert("An error occured");
			}
			review_edit_in_progress = 0;
		},
		'json'
	);
	//
}

// set up comment edit

function edit_comment(comment_chk, edit_key)
{
	if(review_edit_in_progress == 1) return;
	review_edit_in_progress = 1;

	// use ajax to delete record from database
	
	$.get(
		components_ajax_call_file,
		{
			op: 'editcomment' ,
			edit_key: edit_key
		}, 
		function(data){
			if(data.response == 'success') {
			
				// build edit box for this comment
				
				var this_rating = data.rating;
				edit_rating = this_rating;
				var str_body = data.body;
				str_body = str_body.replace(/mpound/g, "£");
				
				var textname = comment_chk + "_tb";
				var height = $('#'+comment_chk).height() + 25;
				var str_content = "<textarea class='edit_textarea' style='width:670px;height:" + height + "px;background-color:#F0F0F0;' class='write_review_tb' name='" + textname + "' id='" + textname + "'>" + str_body + "</textarea>";
				
				var star_colour;
				var str_rating = "";
				for(i = 1; i <= 5; i++)
				{
					star_colour = "yellow";
					if(i <= this_rating) star_colour = "red";
					str_rating += "<a href='javascript:comment_edit_rating_star(" + i + ", false);'><img id='comment_edit_star_" + i + "' src='graphics/common/ratingstar" + star_colour + ".jpg'></a>";
				}
				
				$('#'+comment_chk+"_edit").html(str_content);
				$('#comment_edit_rating_'+comment_chk).html(str_rating);
				
				$('#comment_info_'+comment_chk).hide();
				$('#comment_edit_'+comment_chk).hide();
				$('#'+comment_chk).hide();
				
				$('#'+comment_chk+"_edit").show();
				$('#comment_edit_rating_'+comment_chk).show();
				$('#comment_edit_save_'+comment_chk).show();
				
			} else {

				alert("An error occured");
			}
		},
		'json'
	);
	//
}

function cancel_comment_edit(comment_chk)
{
	$('#'+comment_chk+"_edit").hide();
	$('#comment_edit_save_'+comment_chk).hide();
	$('#comment_edit_rating_'+comment_chk).hide();
	$('#'+comment_chk+"_edit").html("");
	
	$('#comment_info_'+comment_chk).show();
	$('#comment_edit_'+comment_chk).show();
	$('#'+comment_chk).show();
	review_edit_in_progress = 0;
}

// create new or save edited comment

function save_comment(div_name, edit_key)
{
	var op;
	if(div_name == 'new_comment') {
		site_rating = new_site_rating;
		op = 'writecomment';
	} else {
		site_rating = edit_rating;
		op = 'saveeditcomment';
	}
	
	if(site_rating < 1 || site_rating == null) {
		alert("You must give a rating by clicking on the yellow stars");
		return;
	}
	if(site_rating > 5) return;

	// get updated text from textarea
	
	var textname = div_name + "_tb";
	var field_text = $('#' + textname).val();
	field_text = field_text.replace(/£/g, 'mpound');
	if(field_text.length < 4) {
		alert("Please write a bit more");
		return;
	}
	
	// show 'wait' div instead
	
	var width = $('#' + div_name).width();
	var height = $('#' + div_name).height();
	var wt = "<div class='wt' style='width:" + width + "px;height:" + height + "px;'></div>";
	$('#' + div_name).html(wt);
	
	// use ajax to save changes
	
	$.get(
		components_ajax_call_file,
		{
			op: op ,
			edit_key: edit_key ,
			review: field_text ,
			rating: site_rating
		}, 
	
		function(data){
			if(data.response == 'success') {
	
				if(div_name == "new_comment") {
				
					// user is adding a new comment
					// remove comment box
					
					var new_comment = "<div style='padding-top:5px;'>";
					new_comment += "<p class='tmtextg'>Thanks for adding your comments!</p>";
					new_comment += "</div>";
					
					$('#new_comment').html(new_comment);
					
					// show new comment
					
					var this_comment = "";
					
					var author_long_id;
					var author_name= data.user_name;
					var comment_date = data.date_words;
					var rating_img = data.comment_rating_img;
					var obj_rating_img = data.obj_rating_img;
					var obj_rating = data.obj_rating;
					var comment_chk = data.chk;
					var body = data.newtext;
					
					$('#your_rating').html(obj_rating_img);
					//$('#overall_rating_value').html(obj_rating);
				
					this_comment += "<div class='comment_wrapper' id='comment_wrapper_" + comment_chk + "'><div id='comment_info_" + comment_chk + "' style='float:left;width:370px;padding-top:7px;'><p class='tmtextg'><a class='tmlinkg' href='users/" + author_long_id + "'>" + author_name + "</a> on " + comment_date + "</b></p>";
					this_comment += "<div id='comment_rating_img_" + comment_chk + "'>" + rating_img + "</div></div><div id='comment_edit_rating_" + comment_chk + "' style='float:left;width:370px;padding-top:7px;display:none;'></div><div id='" + comment_chk + "_edit' class='comment_body' style='display:none;'></div><div id='" + comment_chk + "' class='comment_body'><p class='tmtextgbt'>" + body + "</p></div>";
				
					var comment_edit_key = data.comment_edit_key;
				
					this_comment += "<div id='comment_edit_" + comment_chk + "' style='clear:both;padding-top:4px;padding-bottom:5px;float:right;'>";
					this_comment += "<p class='tmtextg'><a class='tmlinkg' href='javascript:edit_comment(\"" + comment_chk + "\", \"" + comment_edit_key + "\");'>Edit</a>&nbsp;&#183;&nbsp;<a class='tmlinkg' href='#' onclick='javascript:delete_comment(\"" + comment_chk + "\", \"" + comment_edit_key + "\");return false;'>Delete</a></p>";
					this_comment += "</div>";
					this_comment += "<div id='comment_edit_save_" + comment_chk + "' style='clear:both;padding-top:4px;padding-bottom:5px;float:right;display:none;'>";
					this_comment += "<p class='tmtextg'><a class='tmlinkg' href='javascript:cancel_comment_edit(\"" + comment_chk + "\");'>Cancel</a>&nbsp;&#183;&nbsp;<a class='tmlinkg' href='#' onclick='javascript:save_comment(\"" + comment_chk + "\", \"" + comment_edit_key + "\");return false;'>Save</a></p>";
					this_comment += "</div>";
					this_comment += "<div style='width:100%;clear:both;'><hr class='rough' style='margin-bottom:4px;'></div>";
					this_comment += "</div>";
					
					var current_comments = $('#comments_wrapper').html();
					$('#comments_wrapper').html(current_comments + this_comment);
					
				} else {
				
					// user is editing an existing review
					
					var newtext = data.newtext;
					newtext = newtext.replace(/mpound/g, "£");
					$('#' + div_name).html("<p class='tmtextgbt'>" + newtext + "</p>");
					
					var rating_img = data.comment_rating_img;
					$('#comment_rating_img_'+div_name).html(rating_img);
					
					var obj_rating_img = data.obj_rating_img;
					$('#your_rating').html(obj_rating_img);
					
					cancel_comment_edit(div_name);
				}
				
				// need to update stars

				$('#number_reviews').html("<p class='tmtextg' style='display:inline;color:#68594a;'>&nbsp;&nbsp;(" + data.n_reviews + ")</p>");
				$('#site_rating_box').html(data.site_rating_img + "<br><p class='tmtextg'>" + data.new_site_text + "</p>");
				review_edit_in_progress = 0;
				ajax_in_use = 0;
				
			} else {
			
				cancel_edit_review(div_name, edit_key);
				alert("Sorry but there was an error - please reload the page and try again!");
			}
		},
		'json'
	);
	//
}

// click event for review edit stars

function comment_edit_rating_star(star_number) {
	var img_name = "";
	edit_rating = star_number;
	for(i = 1; i <= star_number; i++)
	{
		img_name = "comment_edit_star_" + i;
		$('#' + img_name).attr("src", "graphics/common/ratingstarred.jpg");
	}
	for(i = (star_number + 1); i <= 5; i++)
	{
		img_name = "comment_edit_star_" + i;
		if(i <= 5) {
			$('#' + img_name).attr("src", "graphics/common/ratingstaryellow.jpg");
		}
	}
}

// click event for review stars

function comment_rating_star(star_number) {
	var img_name = "";
	new_site_rating = star_number;
	for(i = 1; i <= star_number; i++)
	{
		img_name = "comment_star_" + i;
		$('#' + img_name).attr("src", "graphics/common/ratingstarred.jpg");
	}
	for(i = (star_number + 1); i <= 5; i++)
	{
		img_name = "comment_star_" + i;
		if(i <= 5) {
			$('#' + img_name).attr("src", "graphics/common/ratingstaryellow.jpg");
		}
	}
}


function show_ad_image(file_name)
{
	document.getElementById('advert_main_image').src = t_base_ref + "images/" + file_name;
}

function open_classified_advert(advert_id, user_id)
{
	var width = document.getElementById('adverts_list').offsetWidth;
	var height = document.getElementById('adverts_list').offsetHeight;
	var wt = "<div class='wt' style='width:" + width + "px;height:" + height + "px;'></div>";
	document.getElementById('advert_item').innerHTML = wt;
	
	document.getElementById('advert_item').style.display = 'block';
	document.getElementById('adverts_list').style.display = 'none';

	// initiate Ajax object
	
	http = new XMLHttpRequest();
	
	// set up event handler for Ajax completion
	
	http.onreadystatechange = function() { 
		if(http.readyState == 4) {
			if(http.status == 200) {
			
				var ctext = http.responseText;
				document.getElementById('advert_item').innerHTML = ctext;
			
			}
		}
	}
	
	// send request for data
		
	var myurl = t_base_ref + "include/components_ajax.php?op=getad&a=" + advert_id + "&u=" + user_id;
   	http.open("GET", myurl, true); 
   	http.send(null); 
}

function hide_classified_advert()
{
	if(document.getElementById('advert_item')) {
		document.getElementById('advert_item').style.display = 'none';
		document.getElementById('adverts_list').style.display = 'block';
		document.getElementById('advert_item').innerHTML = "";
	}
}

function set_search(search_string)
{
	c_search = search_string;
}

function list_search(list_identity, user_id)
{
	c_refine = "";
	var search_text = $('#txt_list_search_'+list_identity).val();
	do_search(list_identity, user_id, search_text);
}

// NEED TO DEAL WITH MULTIPLE SEARCHES ON ONE PAGE

function do_search(list_identity, user_id, search_text)
{
	hide_classified_advert();
	c_search = search_text;

	var div_name = "site_list_inner_" + list_identity;
	
	var width = $('#'+div_name).width();
	var height = $('#'+div_name).height();
	var wt = "<div class='wt' style='width:" + width + "px;height:" + height + "px;'></div>";
	$('#'+div_name).html(wt);
	
	// ajax
	
	$.get(
		components_ajax_call_file,
		{
			op: 'srch' ,
			n: list_identity ,
			u: user_id ,
			s: search_text
		}, 
		
		function(data){
			if(data.response == 'success') {
			
				var n_pages = data.pages;
				var div_content = data.list_content;
				
				$('#'+div_name).html(div_content);
				
				var page_no = 1;
				if(n_pages == 0) n_pages = 1;

				$('#list_status_'+list_identity).html("<p class='tmtextgb' style='padding-top:4px;'>Page " + page_no + " of " + n_pages + "</p>");
				
				// disable search box
				
				var clear_url = "javascript:clear_results(" + user_id + ", \"" + list_identity + "\");";
				document.getElementById("search_box_wrapper_" + list_identity).innerHTML = "<a class='main' href='" + clear_url + "'>Clear results</a>";
				
				// disable/enabled prev link
				
				if(page_no == 1) {		
					$('#list_prev_'+list_identity).html("<p class='tmtextgb'>Previous | </p>");
				} else {
					var prev_page = (page_no - 1);
					$('#list_prev_'+list_identity).html("<p class='tmtextgb'><a class='tmlinkgb' href='javascript:sites_list_move(" + prev_page + ", " + sites_to_list + ", " + n_pages + ", " + user_id + ",\"" + list_identity + "\");'>Previous</a> | </p>");
				}
				
				// disable/enable next link
				
				if(page_no >= n_pages) {	
					$('#list_next_'+list_identity).html("<p class='tmtextgb'>&nbsp;Next</p>");
				} else {
					var next_page = (page_no + 1);
					$('list_next_'+list_identity).html("<p class='tmtextgb'>&nbsp;<a class='tmlinkgb' href='javascript:sites_list_move(" + next_page + ", " + sites_to_list + ", " + n_pages + ", " + user_id + ",\"" + list_identity + "\");'>Next</a></p>");
				}
			
			} else {
			
				// error
			}
		},
		'json'
	);
	//
}

// use search functionality to refine list

function refine_list(list_identity, user_id, refine_sql)
{
	if(refine_sql == "") {
		clear_results(user_id, list_identity);
	} else {
	
		//
		
		var search_text = "";
		if(list_identity == "classified_page_classifieds_to_show") search_text = "WHERE category = " + refine_sql;
		
		if(search_text != "") {
			$('#txt_list_search_'+list_identity).val("");
			c_refine = refine_sql;
			do_search(list_identity, user_id, search_text);	
		} else {
			clear_results(user_id, list_identity);
		}
	}
}

function clear_results(user_id, list_name)
{
	c_search = "";
	c_refine = "";
	sites_list_n_sites(user_id, list_name);
	hide_classified_advert();
}

function sites_list_n_sites(user_id, list_name)
{
	dd_name = "dd_n_sites_" + list_name;
	dd_name = "dd_n_sites_" + list_name;
	var sites_to_show = $('#'+dd_name).val();
	sites_list_move(1, sites_to_show, 3, user_id, list_name);
}

function sites_list_move(page_no, sites_to_list, n_pages, user_id, list_name, filter)
{
	var div_name = "site_list_inner_" + list_name;

	var width = $('#'+div_name).width();
	var height = $('#'+div_name).height();
	var wt = "<div class='wt' style='width:" + width + "px;height:" + height + "px;'></div>";
	$('#'+div_name).html(wt);
	
	if(filter != null) {
		if(list_name == "user_profile_sites_to_show") c_search = "WHERE author = " + filter;
		if(list_name == "user_profile_classifieds_to_show") c_search = "WHERE user_id = " + filter;	
	}
	
	// use ajax to find list data
	
	$.get(
		components_ajax_call_file,
		{
			op: 'sl' ,
			n: list_name ,
			p: page_no ,
			ns: sites_to_list ,
			u: user_id ,
			s: c_search
		}, 
		
		function(data){
			if(data.response == 'success') {
			
				n_pages = data.pages;
				div_content = data.list_content;
				
				$('#'+div_name).html(div_content);
				
				if(n_pages == 0) n_pages = 1;
				$('#list_status_'+list_name).html("<p class='tmtextgb' style='padding-top:4px;'>Page " + page_no + " of " + n_pages + "</p>");
			
				// show search box
			
				if(c_search == "") {
					$('#search_box_wrapper_'+list_name).html("<input type='text' id='txt_list_search_" + list_name + "'>&nbsp;&nbsp;<a class='main' href='javascript:list_search(\"" + list_name + "\", " + user_id + ");'>Search</a>");
				}
				
				// disable/enabled prev link

				if(page_no == 1) {		
					$('#list_prev_'+list_name).html("<p class='tmtextgb'>Previous | </p>");
				} else {
					var prev_page = (page_no - 1);
					$('#list_prev_'+list_name).html("<p class='tmtextgb'><a class='tmlinkgb' href='javascript:sites_list_move(" + prev_page + ", " + sites_to_list + ", " + n_pages + ", " + user_id + ",\"" + list_name + "\");'>Previous</a> | </p");
				}
				
				// disable/enable next link
				
				if(page_no >= n_pages) {	
					$('#list_next_'+list_name).html("<p class='tmtextgb'>&nbsp;Next</p>");
				} else {
					var next_page = (page_no + 1);
					$('#list_next_'+list_name).html("<p class='tmtextgb'>&nbsp;<a class='tmlinkgb' href='javascript:sites_list_move(" + next_page + ", " + sites_to_list + ", " + n_pages + ", " + user_id + ",\"" + list_name + "\");'>Next</a></p>");
				}
				
			} else {
			
				// error?
			}
		},
		'json'
	);
	//
}

//------------------------//
//    Message handling    //
//------------------------//

// show message in popup window

function display_modal_message(send_msg_key, msg_subject, r_name, user_id)
{

	var popup_width = 500;
	var popup_height = 300;

	$('#page_cover').show();					// display semi-transparent page cover
	$('#popup_container').show();
	$('#popup_content').css( { "left": (((parseInt(popup_width) + 20) / 2) * (-1)) + "px", "top": (((parseInt(popup_height) + 20) / 2) * (-1)) + "px" } );
	$('#popup_content').css( { "width": parseInt(popup_width) + "px" } ); //, "height": parseInt(popup_height) + "px" } );

	var msg_box_html = "";
	
	msg_box_html += "<div id='message_box'>";
	
	msg_box_html += "<em class='data'>Send message to " + r_name + "</em><br>";
	msg_box_html += "<div style='width:" + popup_width + "px;height:5px;'></div>";
	
	msg_box_html += "<em class='data'>Subject</em><br>";
	msg_box_html += "<input type='text' id='txt_subject' style='width:350px;' value='" + msg_subject + "' />";
	msg_box_html += "<div style='width:" + popup_width + "px;height:4px;'></div>";
	
	msg_box_html += "<em class='data'>Message</em>";
	msg_box_html += "<textarea id='txt_message' style='width:" + (popup_width - 4) + "px;height:90px;'></textarea>";
	msg_box_html += "<div style='width:" + popup_width + "px;height:3px;'></div>";
	msg_box_html += "<p align='left' style='border:0px;margin:0px;padding:0px;'><a class='sitelink' href='javascript:send_message(\"" + send_msg_key + "\", " + user_id + ");'>Send</a></p>";
	msg_box_html += "</div>";
	
	$('#popup_content').html(msg_box_html);		
}

// hides message popup window

function hide_modal_message()
{
	$('#popup_container').hide();
	$('#page_disable').hide();
	$('#popup_content').html("");
}

// send private message to other user

function send_message(edit_key, user)
{
	if(ajax_in_use == 1) return;
	ajax_in_use = 1;
	ajax_prev = $('#message_box').html();
	
	// edit key will determine sender and recipient, but message must be obtained from page

	var msg_body = $('#txt_message').val();
	var msg_subject = $('#txt_subject').val();

	// ensure body and subject are both present
	
	if(msg_body == "") {
		alert("You must enter a message!");
		return;
	}
	if(msg_subject == "") {
		alert("No message subject entered!");
		return;
	}
	
	// now show ajax wait box
	
	var width = $('#message_box').width();
	var height = $('#message_box').height();
	var wt = "<div class='wt' style='width:" + width + "px;height:" + height + "px;'></div>";
	$('#message_box').html(wt);

	// use ajax to send message
	
	$.get(
		components_ajax_call_file,
		{
			op: 'smsg' ,
			u: user ,
			edit_key: edit_key ,
			body: msg_body ,
			subject: msg_subject
		}, 
		
		function(data){
			if(data.response == 'success') {
			
				// notify user that message has been sent
				
				hide_modal_message();

			} else {
			
				alert("Unable to send message");
				$('#message_box').html(ajax_prev);
				$('#'+msg_body_id).val(msg_body);
			}
			ajax_in_use = 0;
		},
		'json'
	);
	//
}




function show_intro()
{
	show_message(600, 400, "hello");


}

function show_terms()
{
	var msg = "";
	
	msg += "<p class='tmtextgb'>Competition rules</p>";
	msg += "<p class='tmtextg' style='padding-top:18px;'>All wild camping sites added between midnight on 28th November and midnight on 29th February 2012 will count as individual entries.</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>No formal entry required - just add one or more sites by following the link on the front page.</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>Adding a site during the competition period is taken to mean that you accept these terms.</p>";	
	msg += "<p class='tmtextg' style='padding-top:8px;'>Winner to be chosen at random. As multiple sites added will count as a multiple number of entries, adding more sites increases chances of winning. The winner will not therefore be judged on site quality.</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>Winner to be announced no later than 10th February 2012.</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>Sorry, but this competition is for UK members only.</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>CamperStar reserves the right to invalidate any entry (this could be for example if a site is deemed to be not genuine).</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>CamperStar staff and friends are forbidden from entering :(</p>";
	
	msg += "<p class='tmtextg' style='padding-top:8px;'>No crying if you don't win. More competitions will be forthcoming!</p>";
	
	msg += "<p class='tmtextg' style='padding-top:20px;'>Any further questions, <a href='about' class='tmlinkg'>get in touch</a>.</p>";
	
	msg += "<p class='tmtextgb' style='padding-top:20px;'><a class='tmlinkgb' href='#' onclick='show_competition();return false;'>Go back</a></p>";

	show_message(600, 370, msg);
}

function show_competition()
{
	var msg = "";
	
	msg += "<p class='tmtextgb'>Win camping goodies from JustKampers!</p>";
	
	msg += "<div style='width:600px;'><div style='float:left;width:290px;'>";
	msg += "<p class='tmtextg' style='padding-top:12px;'>Here's your chance to win a rad prize from <a target='blank' href='http://www.justkampers.com' class='tmlinkg'>JustKampers</a> for contributing your camping spots! Check out the prizes below, and if you want to win one of these badboys, tell us about somewhere you've camped!</p>";
	
	msg += "<p class='tmtextg' style='padding-top:12px;'>Please do read <a class='tmlinkg' href='#' onclick='show_terms();return false;'>the competition rules and information</a>.</p>";

	msg += "</div><div style='float:right;'>";
	msg += "<img style='padding-top:10px;' src='http://www.camperstar.com/graphics/competitions/justkampersblue.png'>";
	msg += "</div></div>";
	msg += "<div style='clear:both;'>";
	
	msg += "<p class='tmtextgb' style='padding-top:15px;'>How does it work?</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>It's easy! If you're not a member already then sign up (it's free!), and then add a wild camping spot to our ever-growing map. The more sites you add, the more chance you have of winning!</p>";
	
	msg += "<p class='tmtextg' style='padding-top:8px;'><a class='tmlinkg' target='blank' href='http://www.justkampers.com'>JustKampers</a> have provided two prizes - first choice for the winner, naturally. Here's what's up for grabs...</p>";
	
	msg += "</div>";
	
	msg += "<div style='width:600px;clear:both;padding-top:10px;'>";
	
	msg += "<div style='width:250px;float:left;'>";
	msg += "<p class='tmtextgb'>18 piece dining set worth &#163;26.50</p>";
	msg += "<div style='padding-top:10px;padding-bottom:10px;'>";
	msg += "<div style='float:left;width:130px;'>";
	msg += "<img src='http://www.camperstar.com/graphics/competitions/j2_sm.jpg' style='border:solid 6px #FFFFFF;'>";
	msg += "</div><div style='float:right;width:115px;'>";
	
	msg += "<p class='tmtextg'>Featuring Volkswagen Splitscreens, Baywindows, Type 25's and T4's!<br><br><a class='tmlinkg' target='blank' href='http://www.justkampers.com/shop/18-piece-melamine-dining-set-j14054.html'>Find out more...</a></p>";
	
	msg += "</div>";
	msg += "</div>";
	msg += "</div>";
	
	msg += "<div style='width:310px;float:right;'>";
	msg += "<p class='tmtextgb'>Folding aluminium table worth &#163;29</p>"
	msg += "<div style='padding-top:10px;padding-bottom:10px;'>";
	msg += "<div style='float:left;width:170px;'>";
	msg += "<img src='http://www.camperstar.com/graphics/competitions/j1_sm.jpg' style='border:solid 6px #FFFFFF;'>";
	msg += "</div><div style='float:right;width:135px;'>";
	
	msg += "<p class='tmtextg'>Dine outside in style with this lightweight two person roll up table! Packs into a handy carry bag too!<br><br><a class='tmlinkg' target='blank' href='http://www.justkampers.com/shop/just-kampers-2-person-roll-up-folding-aluminium-table-due-late-march-j12869.html'>Find out more...</a></p>";
	
	msg += "</div>";
	msg += "</div>";
	msg += "</div>";
	
	msg += "</div>";
	
	
	msg = "";
	
	msg += "<p class='tmtextgb'>Prizes up for grabs from Vango!</p>";
	
	msg += "<div style='width:600px;'><div style='float:left;width:290px;'>";
	msg += "<p class='tmtextg' style='padding-top:12px;'>It's competition time again! This time we've teamed up with <a target='blank' href='http://www.vango.co.uk' class='tmlinkg'>Vango</a> who've put up a couple of great prizes. Once again all we're asking you to do is tell us about a place or two you've wild camped.</p>";
	
	msg += "<p class='tmtextg' style='padding-top:12px;'>Please do read <a class='tmlinkg' href='#' onclick='show_terms();return false;'>the competition rules and information</a>.</p>";

	msg += "</div><div style='float:right;'>";
	msg += "<img style='padding-top:10px;' src='http://www.camperstar.com/graphics/competitions/vango.png'>";
	msg += "</div></div>";
	msg += "<div style='clear:both;'>";
	
	msg += "<p class='tmtextgb' style='padding-top:15px;'>How does it work?</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>It's easy! If you're not a member already then sign up (it's free!), and then add a wild camping spot to our ever-growing map. The more sites you add, the more chance you have of winning!</p>";
	
	msg += "<p class='tmtextg' style='padding-top:8px;'><a class='tmlinkg' target='blank' href='http://www.vango.co.uk'>Vango</a> have provided two prizes, and the winner has first choice. This is what you can win...</p>";
	
	msg += "</div>";
	
	msg += "<div style='width:600px;clear:both;padding-top:10px;'>";
	
	msg += "<div style='width:250px;float:left;'>";
	msg += "<p class='tmtextgb'>Vango Boulder 35 rucksack &#163;35</p>";
	msg += "<div style='padding-top:10px;padding-bottom:10px;'>";
	msg += "<div style='float:left;width:130px;'>";
	msg += "<img src='http://www.camperstar.com/graphics/competitions/v_boulder.jpg' style='border:solid 6px #FFFFFF;'>";
	msg += "</div><div style='float:right;width:115px;'>";
	
	msg += "<p class='tmtextg'>Modern, streamlined styling fused with an abundance of functional, versatile features.<br><br><a class='tmlinkg' target='blank' href='http://www.vango.co.uk/rucsacs/boulder-35.html'>Find out more...</a></p>";
	
	msg += "</div>";
	msg += "</div>";
	msg += "</div>";
	
	msg += "<div style='width:310px;float:right;'>";
	msg += "<p class='tmtextgb'>Vango Halo 200 tent worth &#163;150</p>"
	msg += "<div style='padding-top:10px;padding-bottom:10px;'>";
	msg += "<div style='float:left;width:170px;'>";
	msg += "<img src='http://www.camperstar.com/graphics/competitions/v_halo.jpg' style='border:solid 6px #FFFFFF;'>";
	msg += "</div><div style='float:right;width:135px;'>";
	
	msg += "<p class='tmtextg'>Includes TBS, Alloy Poles, and a host of other features that you would expect from a Vango Technical tent.<br><br><a class='tmlinkg' target='blank' href='http://www.vango.co.uk/tents/halo-200.html'>Find out more...</a></p>";
	
	msg += "</div>";
	msg += "</div>";
	msg += "</div>";
	
	msg += "</div>";
	
	msg = "";
	
	msg += "<p class='tmtextgb'>Loads of gear to be won from Simply Hike!</p>";
	
	msg += "<div style='width:600px;'><div style='float:left;width:290px;'>";
	msg += "<p class='tmtextg' style='padding-top:12px;'>The amazing people at <a target='blank' href='http://www.simplyhike.co.uk' class='tmlinkg'>Simply Hike</a> have teamed up with us to give you the chance to win some great new camping gear.</p>";
	
	msg += "<p class='tmtextg' style='padding-top:12px;'>Please do read <a class='tmlinkg' href='#' onclick='show_terms();return false;'>the competition rules and information</a>.</p>";

	msg += "</div><div style='float:right;'>";
	msg += "<img style='padding-top:10px;' src='http://www.camperstar.com/graphics/competitions/simply-hike.png'>";
	msg += "</div></div>";
	msg += "<div style='clear:both;'>";
	
	msg += "<p class='tmtextgb' style='padding-top:15px;'>How does it work?</p>";
	msg += "<p class='tmtextg' style='padding-top:8px;'>It's easy! If you're not a member already then sign up (it's free!), and then add a wild camping spot to our ever-growing map. The more sites you add, the more chance you have of winning!</p>";
	
	msg += "<p class='tmtextg' style='padding-top:8px;'><a class='tmlinkg' target='blank' href='http://www.simplyhike.co.uk'>Simply Hike</a> have come up with seven prizes, and the winner has first choice. Here is what you can win...</p>";
	
	msg += "</div>";
	
	msg += "<div style='width:600px;clear:both;padding-top:10px;height:70px;padding-bottom:8px;'>";
	
	msg += "<a href='http://www.simplyhike.co.uk/products/Campingaz/TrekkingKit-8Piece.aspx' target='blank'><img onmouseover='javascript:show_prize_info(1);' style='float:left;' src='graphics/competitions/simply-hike/campingaz-trekking-kit.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/Coleman/LibraConnectX1Tent.aspx' target='blank'><img onmouseover='javascript:show_prize_info(2);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/coleman-libra-connect-x1.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/Vango/Wilderness250SleepingBag.aspx' target='blank'><img onmouseover='javascript:show_prize_info(3);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/vango-wilderness-250.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/LightMyFire/TinderSticks.aspx' target='blank'><img onmouseover='javascript:show_prize_info(4);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/light-my-fire-tinder-sticks.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/Sigg/OutdoorCutlery3in1.aspx' target='blank'><img onmouseover='javascript:show_prize_info(5);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/sigg-outdoor-cutlery.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/Vango/SquarePillow-Small.aspx' target='blank'><img onmouseover='javascript:show_prize_info(6);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/vango-square-pillow.jpg'></a>";
	msg += "<a href='http://www.simplyhike.co.uk/products/Coleman/PackawayLantern-Yellow.aspx' target='blank'><img onmouseover='javascript:show_prize_info(7);' style='float:left;padding-left:17px;' src='graphics/competitions/simply-hike/coleman-packaway-lantern.jpg'></a>";
	
	
	msg += "</div>";
	
	msg += "<div id='prize_info' style='clear:both;width:600px;'><p class='tmtextg'>Move the mouse over the images for more information, or click them to view the product pages.</div>";
	
	
	show_message(600, 370, msg);
}

function show_prize_info(prize_number)
{
	var prize_info;
	
	if(prize_number == 1) prize_info = "<p class='tmtextg'>Campingaz Trekking Kit - 8 piece</p><p class='tmtextgt' style='padding-top:5px;'>An 8 piece trekking kit from Campingaz. Large camping pots and pans ideal for families in sedentary camping.</p>";
	if(prize_number == 2) prize_info = "<p class='tmtextg'>Coleman Libra Connect X1 Tent</p><p class='tmtextgt' style='padding-top:5px;'>The 2011 Libra Connect X1 camping tent from Coleman is a 1 man tent from their Adventure range. This Coleman tent uses the connect system which allows for super quick and easy pitching times allowing you more time to spend exploring.</p>";
	if(prize_number == 3) prize_info = "<p class='tmtextg'>Vango Wilderness 250 Sleeping Bag</p><p class='tmtextgt' style='padding-top:5px;'>The 2011 edition of the Wilderness 250 Sleeping bag is a synthetic, mummy sleeping bag from Vango's Adventure range which is designed for family camping, festivals and sleep overs.</p>";
	if(prize_number == 4) prize_info = "<p class='tmtextg'>Light My Fire Tinder Sticks</p><p class='tmtextgt' style='padding-top:5px;'>The Tinder Sticks are the perfect way to start a natural fire in any weather and replace the popular Maya sticks from Light My Fire. The Tinder Sticks are made of cultivated pine for Mexico and contain 80% resin making then easy to light even when wet.</p>";
	if(prize_number == 5) prize_info = "<p class='tmtextg'>Sigg Outdoor Cutlery 3 in 1</p><p class='tmtextgt' style='padding-top:5px;'>The Outdoor Cutlery 3 in 1 is an elegant set constructed from strong, and rust resistant stainless steel. The set includes a knife, fork and spoon which can clip together for storage but separated when in use and all fits into a Sigg carry pouch.</p>";
	if(prize_number == 6) prize_info = "<p class='tmtextg'>Vango Square Pillow - Small</p><p class='tmtextgt' style='padding-top:5px;'>Pillows are often forgotton on camping trips but are an absolute essential! This Vango pillow is more rectangular in shape, just like at home, but is designed specifically for camping trips.</p>";
	if(prize_number == 7) prize_info = "<p class='tmtextg'>Coleman Packaway Lantern - Yellow</p><p class='tmtextgt' style='padding-top:5px;'>Yellow Packaway Lantern from Coleman. A perfect travelling companion, the Pack-Away lantern features unique patented technology that allows the user to pack the main light into the light base when not in use.</p>";
	
	$('#prize_info').html(prize_info);
}

function show_stickers(has_stickers)
{	
	var msg = "";
	
	msg += "<p class='tmtextgb'>Free Camperstar stickers!</p>";
	
	msg += "<p class='tmtextgbt' style='padding-top:12px;'>It may be getting colder now but we hope that like us, you're still out finding great places to camp. To help spread the word, why not treat your van or car to a Camperstar sticker? We're giving them away free to all members.";
	
	msg += "<div style='width:100%'><div style='width:240px;float:left;padding-left:10px;padding-top:20px;'>";
	msg += "<img src='graphics/misc/stickers.png'>";
	msg += "</div><div id='stickers_form' style='width:330px;float:right;padding-top:10px;'>";
	
	if(has_stickers == 1) {
	
		msg += "<br><br><br><div style='text-align:center;width:250px;'><p class='tmtextgbt' style='padding-top:10px;'><b>Back for more, eh?</b> Unfortunately we can only offer stickers to each member once. If you haven't received them yet then they'll be with you soon. And don't worry, there's more free stuff and competitions on the way soon.</p></div>";
		
	
	} else {
	
		if(logged_in == 1) {
			msg += "<p class='tmtextg'>Please pick the stickers you want and enter your address below*</p>";
			
			// sticker options
			
			msg += "<div style='width:320px;padding-top:10px;'>";
			msg += "<div style='float:left;width:50px;'><p class='tmtextgb'>1) <input type='checkbox' id='chk_sticker_1'></p></div>";
			msg += "<div style='float:left;width:50px;'><p class='tmtextgb'>2) <input type='checkbox' id='chk_sticker_2'></p></div>";
			msg += "<div style='float:left;width:50px;'><p class='tmtextgb'>3) <input type='checkbox' id='chk_sticker_3'></p></div>";
			msg += "<div style='float:left;width:50px;'><p class='tmtextgb'>4) <input type='checkbox' id='chk_sticker_4'></p></div>";
			
			msg += "</div>";
			
			// address
			
			msg += "<div style='width:320px;clear:both;padding-top:10px;'>";
			msg += "<p><input type='text' class='txt_popup' style='width:250px;' id='txt_name' value='Your name'/></p>";
			msg += "<p style='padding-top:2px;'><input type='text' class='txt_popup' style='width:210px;' id='txt_housename' value='House name or number'/></p>";
			msg += "<p style='padding-top:2px;'><input type='text' class='txt_popup' style='width:210px;' id='txt_street' value='Street'/></p>";
			msg += "<p style='padding-top:2px;'><input type='text' class='txt_popup' style='width:170px;' id='txt_town' value='Town'/></p>";
			msg += "<p style='padding-top:2px;'><input type='text' class='txt_popup' style='width:110px;' id='txt_postcode' value='Postcode'/></p>";
			msg += "<p class='tmtextg' style='padding-top:10px;'>*we'll email your address to our sticker supplier, but we won't store it</p>";
			msg += "<div id='error_msg' style='padding-top:6px;'></div>";
			msg += "<br><a href='javascript:request_stickers();'><img src='graphics/common/msgsend.jpg'></a>"
			msg += "</div>";
		} else {
			// tell user to sign up!
			
			msg += "<br><br><br><div style='text-align:center;width:250px;'><p class='tmtextgbt' style='padding-top:10px;'><b>Wait a sec!</b> Before we can let you choose your stickers you need to <a href='javascript:show_login();' class='tmlinkgb'>login</a> or if you're not a member yet, then <a href='javascript:show_signup();' class='tmlinkgb'>sign up</a> today, and don't worry because that's free too!</p></div>";
		
		
		}
	}
	msg += "</div></div>";
	
	show_message(600, 400, msg);
}

function request_stickers()
{
	if(ajax_in_use == 1) return;
	
	// first check everything has been filled-in
	
	var user_name = $('#txt_name').val();
	var address_name = $('#txt_housename').val();
	var address_street = $('#txt_street').val();
	var address_town = $('#txt_town').val();
	var address_postcode = $('#txt_postcode').val();
	
	var req_1 = document.getElementById('chk_sticker_1').checked;
	var req_2 = document.getElementById('chk_sticker_2').checked;
	var req_3 = document.getElementById('chk_sticker_3').checked;
	var req_4 = document.getElementById('chk_sticker_4').checked;
	
	var error_msg = "";
	if(address_name == "") error_msg = "You need to enter your house name or number!";
	if(address_street == "") error_msg = "You need to enter your address street!";
	if(address_town == "") error_msg = "You need to enter your address town!";
	if(address_postcode == "") error_msg = "You need to enter your address postcode!";
	if(user_name == "") error_msg = "You need to enter your name!";
	
	if(req_1 == false && req_2 == false && req_3 == false && req_4 == false)
	{
		error_msg = "You haven't selected any stickers!";
	}
	
	if(error_msg != "") {
		
		$('#error_msg').html("<p class='tmtextg'>" + error_msg + "</p>");
	
		return;
	}

	// request stickers
	
	var str_req;
	if(req_1 == true) str_req += "1";
	if(req_2 == true) str_req += "2";
	if(req_3 == true) str_req += "3";
	if(req_4 == true) str_req += "4";
	
	ajax_in_use = 1;
	
	$.get(
		'_include/userprofile_rw_jq.php',
		{
			op: 'stickers' ,
			req: str_req ,
			name: user_name ,
			address_name: address_name ,
			address_street: address_street ,
			address_town: address_town ,
			address_postcode: address_postcode
		}, 
		
		function(data){
			ajax_in_use = 0;
			if(data.response == 'success') {
				// sent successfully
				
				$('#stickers_form').html("<br><br><br><div style='text-align:center;width:250px;'><p class='tmtextgbt' style='padding-top:10px;'><b>It's all sorted!</b><br><br>Within a week your stickers should be with you. Be sure to stick them where everyone'll see them!</p></div>");
				
				
			} else {
				alert("Something went wrong.");
			}
			ajax_in_use = 0;
		},
		'json'
	);
	//


}

//---------------------------------//
//     Message in popup window     //
//---------------------------------//

function show_message(img_width, img_height, message)
{
	var modal_content = "<div style='width:100%'><div style='float:right;'><a class='tmlinkgb' href='#' onclick='javascript:hide_modal_message();return false;'>Close</a></div></div>" + message;
	
	
	var lb_width = (parseInt(img_width) + 50);
	var cntr_width = lb_width - 154;
	var cntr_height = parseInt(img_height) + 21;
	
	var lb = "";
	lb += "<div style='width:" + lb_width + "px;'>";
	lb += "<div id='lb_tl'></div>";
	lb += "<div id='lb_tc' style='width:" + cntr_width + "px;'></div>";
	lb += "<div id='lb_tr'></div>";
	lb += "</div>";
	
	lb += "<div style='width:" + lb_width + "px;height:" + cntr_height + "px;clear:both;'>";
	lb += "<div id='lb_ml' style='height:" + cntr_height + "px;'></div>";
	lb += "<div id='lb_img' style='width:" + img_width + "px;height:" + cntr_height + "px;'>";
	
	lb += modal_content;
	
	lb += "</div>";
	lb += "<div id='lb_mr' style='height:" + cntr_height + "px;'></div>";
	lb += "</div>";
	
	lb += "<div style='width:" + lb_width + "px;'>";
	lb += "<div id='lb_bl'></div>";
	lb += "<div id='lb_bc' style='width:" + cntr_width + "px;'></div>";
	lb += "<div id='lb_br'></div>";
	lb += "</div>";

	$('#page_disable').show();					// display semi-transparent page cover
	$('#popup_container').show();
	$('#popup_content').css( { "left": (((parseInt(img_width) + 50) / 2) * (-1)) + "px", "top": (((parseInt(img_height) + 60) / 2) * (-1)) + "px" } );

	$('#popup_content').html(lb);	
}

//----------------------------//
//    Image in modal popup    //
//----------------------------//

// hide image

function hide_image()
{
	$('#page_disable').hide();					// hide semi-transparent page cover
	$('#popup_container').hide();
	$('#popup_content').html('');
}

// show image in modal popup

function show_image(user, img_chk)
{
	var modal_content;
	
	// use an ajax request for find the image details
	
	$.get(
		components_ajax_call_file,
		{
			u: user ,
			op: 'fetch_image' ,
			chk: img_chk
		}, 
		
		function(data){
			if(data.response == 'success') {
	
				var img_filename = data.filename;
				var img_caption = data.caption;
				var img_author = data.author;
				var img_date = data.date;
				var img_width = data.width;
				var img_height = data.height;
				var img_copyright = String(data.copyright);

				var copyright_info = "";
				if(img_copyright.length > 5) {
					copyright_info = "&nbsp;/&nbsp;Copyright " + img_copyright;
				}
				
				// create lightbox
				
				var lb_width = (parseInt(img_width) + 50);
				var cntr_width = lb_width - 154;
				var cntr_height = parseInt(img_height) + 21;
				
				var lb = "";
				lb += "<div style='width:" + lb_width + "px;'>";
				lb += "<div id='lb_tl'></div>";
				lb += "<div id='lb_tc' style='width:" + cntr_width + "px;'></div>";
				lb += "<div id='lb_tr'></div>";
				lb += "</div>";
				
				lb += "<div style='width:" + lb_width + "px;height:" + cntr_height + "px;clear:both;'>";
				lb += "<div id='lb_ml' style='height:" + cntr_height + "px;'></div>";
				lb += "<div id='lb_img' style='width:" + img_width + "px;height:" + cntr_height + "px;'>";
				lb += "<img src='" + img_filename + "'>";
				
				lb += "<div style='width:" + img_width + "px;padding-top:2px;'>";
				lb += "<div style='float:left;'><p class='tmtextg'>Contributed by <a class='tmlinkg' href='users/" + data.authorid + "'>" + img_author + "</a>" + copyright_info + "</p></div>";
				lb += "<div style='float:right;'><p class='tmtextg'><a class='tmlinkg' href='#' onclick='javascript:hide_image();return false;'>Close</a></p></div>";
				lb += "</div>";
				
				lb += "</div>";
				lb += "<div id='lb_mr' style='height:" + cntr_height + "px;'></div>";
				lb += "</div>";
				
				lb += "<div style='width:" + lb_width + "px;'>";
				lb += "<div id='lb_bl'></div>";
				lb += "<div id='lb_bc' style='width:" + cntr_width + "px;'></div>";
				lb += "<div id='lb_br'></div>";
				lb += "</div>";

				$('#page_disable').show();					// display semi-transparent page cover
				$('#popup_container').show();
				$('#popup_content').css( { "left": (((parseInt(img_width) + 50) / 2) * (-1)) + "px", "top": (((parseInt(img_height) + 60) / 2) * (-1)) + "px" } );

				if(img_caption != '') {
					img_caption = "<br><em class='data'>" + img_caption + "</em>";
				}
				img_author = "<br><em class='data'>Contributed by " + img_author + "</em>";
			
				$('#popup_content').html(lb);
	
			} else {
			
				alert("Unable to load image");
			}
		},
		'json'
	);
	//
}


