function google_map_switch ()
{
	//Show Map
	if (document.getElementById('map_canvas').style.display == 'none')
	{		
		document.getElementById('normal_search_link').style.display = 'inline';
		document.getElementById('advance_search_link').style.display = 'none';		
		document.getElementById('photos_reviews_container').style.display = 'none'; 
		document.getElementById('map_canvas').style.width = '400px'; 
		document.getElementById('map_canvas').style.height = '400px'; 
		document.getElementById('map_canvas').style.display = 'block'; 		
		if (document.getElementById('first_time_map_flag').value == 0)
		{
			initialize();
			document.getElementById('first_time_map_flag').value = 1;
		}
		
	}
	//Hide Advance Search Form
	else
	{
		document.getElementById('normal_search_link').style.display = 'none';
		document.getElementById('advance_search_link').style.display = 'inline';		
		document.getElementById('map_canvas').style.display = 'none'; 
		document.getElementById('photos_reviews_container').style.display = 'inline'; 
	}
}
function switch_reviews()
{
	var first_review_holder = document.getElementById('first_review_holder_id');
	
	//Hide reviews
	if (document.getElementById('review_display_id').value == 1)
	{
		document.getElementById('review_holder_id').className = 'hide';
		if (first_review_holder != null)
			document.getElementById('first_review_holder_id').className = 'show';
		document.getElementById('review_display_id').value = 0;
		document.getElementById('review_display_control_id').innerHTML="Show all Reviews";
		//alert("Hide");
	}
	//Show reviews
	else
	{
		document.getElementById('review_holder_id').className = 'show';
		if (first_review_holder != null)
			document.getElementById('first_review_holder_id').className = 'hide';
		document.getElementById('review_display_id').value = 1;
		document.getElementById('review_display_control_id').innerHTML="Hide Reviews";
		//alert("Show");
	}
}
function switch_blog(show_title, hide_title)
{
	var first_blog_holder = document.getElementById('first_blog_holder_id');
	//Hide blog
	if (document.getElementById('blog_display_id').value == 1)
	{
		document.getElementById('blog_holder_id').className = 'hide';
		if (first_blog_holder != null)
			document.getElementById('first_blog_holder_id').className = 'show';
		document.getElementById('blog_display_id').value = 0;
		document.getElementById('blog_display_control_id').innerHTML=show_title;
		//alert("Hide");
	}
	//Show blog
	else
	{
		document.getElementById('blog_holder_id').className = 'show';
		if (first_blog_holder != null)
			document.getElementById('first_blog_holder_id').className = 'hide';
		document.getElementById('blog_display_id').value = 1;
		document.getElementById('blog_display_control_id').innerHTML=hide_title;
		//alert("Show");
	}
}

function page_selector(page_number, anchor)
{
	var anchor_holder = document.getElementById('anchor_flag_id');
	if (anchor_holder != null)	
		document.getElementById('anchor_flag_id').value=anchor;
	//alert(document.getElementById('anchor_flag_id').value);

	document.getElementById('page_pointer_id').value=page_number;
	document.forms[0].submit();
	
}
function form_submit ()
{	
	document.review_list.submit();			
}
function form_submit_asc (arrow)
{
	//document.getElementById('search_button').value = "Search";
	document.getElementById('hidden_sorter_asc').value = arrow;	
	document.forms[0].submit();			
}

function go_to_anchor(anchor_name)
{
	window.location = window.location + anchor_name;
	//location.href = anchor_name;
}

function show_video(video_path, preview, title, autostart, host, video_id, ip)
{				

			document.getElementById('main_video_description').innerHTML=title;
			var s1 = new SWFObject("player.swf","ply","441","366","9","#FFFFFF");
			s1.addParam("allowfullscreen","true");
			/*s1.addParam("allowscriptaccess","always");*/	
			if (autostart == 0 )
			{
				autostart_var = "false";
			}
			else
			{
				autostart_var = "true";
				//alert(autostart + 'http://' + host + '/stream/video_counter.php?video_id=' + video_id + '&ip=' + ip);
				http.open('get', 'http://' + host + '/stream/video_counter.php?video_id=' + video_id + '&ip=' + ip);
				http.onreadystatechange = handleProducts; 	
				http.send(null);				
			} 		
			s1.addParam("flashvars","file=" + video_path + "&image=" + preview + "&autostart=" + autostart_var);			
			s1.write("video_holder");			
							
}
function bbr_load_towns(host)
{
		var county = document.getElementById('county_id').value;
		//Clear postcode field
		document.getElementById('testinput').value = "Or enter a postcode";				
		if (county == 0)
		{
			document.getElementById('town_holder').className="hide";	
			var options = {
			script:"autocomplete/auto/test.php?json=true&",
			varname:"input",
			json:true,
			callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};
			var as_json = new AutoSuggest('testinput', options);														 										
		}
		else
		{
			//alert(county);		
			var options = {
				script:"autocomplete/auto/test.php?county_id=" + county + "&json=true&",
				varname:"input",
				json:true,
				callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};
			var as_json = new AutoSuggest('testinput', options);														 													
			document.getElementById('town_holder').className="frmRow";
			http.open('get', 'http://' + host + '/services/load_towns.php?county=' + county);
			http.onreadystatechange = bbr_handleProducts_load_towns; 	
			http.send(null);
		}											
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function bbr_handleProducts_load_towns(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert("Resp status - " + http.readyState);
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;

		document.getElementById('town_holder').innerHTML="<label>Town</label>" + response;
		//alert(response);
	}		
}

function load_towns(host)
{
		var county = document.getElementById('county_id').value;
		//Clear postcode field
		document.getElementById('testinput').value = "Or enter a postcode";				
		if (county == 0)
		{
			document.getElementById('town_holder').className="hide";	
			var options = {
			script:"autocomplete/auto/test.php?json=true&",
			varname:"input",
			json:true,
			callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};
			var as_json = new AutoSuggest('testinput', options);														 										
		}
		else
		{
			//alert(county);		
			var options = {
				script:"autocomplete/auto/test.php?county_id=" + county + "&json=true&",
				varname:"input",
				json:true,
				callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};
			var as_json = new AutoSuggest('testinput', options);														 													
			document.getElementById('town_holder').className="frmRow";
			http.open('get', 'http://' + host + '/services/load_towns.php?county=' + county);
			http.onreadystatechange = handleProducts_load_towns; 	
			http.send(null);
		}											
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts_load_towns(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert("Resp status - " + http.readyState);
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;

		var town_spacer = document.getElementById('town_holder_spacer');		
		if (town_spacer != null)
			document.getElementById('town_holder').innerHTML= '<label id="town_holder_spacer"></label>' + response;
		else
			document.getElementById('town_holder').innerHTML=response;
		//alert(response);
	}		
}

function narrow_county_by_postcode()
{
	var narrow_by = document.getElementById('narrow_by').value;
	//Clear postcode field
	document.getElementById('testinput').value = "Or enter a postcode";	
	//alert(narrow_by);
	if (narrow_by != 0)
	{
		//What do we narrow by
		selector_name = document.getElementById('narrow_by').name		
		if (selector_name == "search_town")
		{
			//alert(selector_name);
			var options = {
				script:"autocomplete/auto/test.php?town_id=" + narrow_by + "&json=true&",
				varname:"input",
				json:true,
				callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};		
			var as_json = new AutoSuggest('testinput', options);	
		}
		if (selector_name == "search_postcode")
		{
			var options = {
				script:"autocomplete/auto/test.php?postcode_id=" + narrow_by + "&json=true&",
				varname:"input",
				json:true,
				callback: function (obj) { document.getElementById('testid').value = obj.id; }
			};			
			var as_json = new AutoSuggest('testinput', options);		
		}		
		//alert(document.getElementById('narrow_by').name);
	}
	//Show list of all postcode for county/town
	else
	{
		var county = document.getElementById('county_id').value;
		var options = {
			script:"autocomplete/auto/test.php?county_id=" + county + "&json=true&",
			varname:"input",
			json:true,
			callback: function (obj) { document.getElementById('testid').value = obj.id; }
		};			
		var as_json = new AutoSuggest('testinput', options);	
	} 		
}


function check_punter_username(host)
{
				var username = document.getElementById('username_holder').value;
				http.open('get', 'http://' + host + '/services/username_check.php?username=' + username + '&punter=1');
				http.onreadystatechange = handleProducts_check_punter_username; 	
				http.send(null);											
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts_check_punter_username(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert("Resp status - " + http.readyState);
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		//alert(response);		
		//Username - available
		if (response == 1) {
			document.getElementById('avail_username_res').innerHTML = 'Username available!';
			document.getElementById('avail_username_res').style.color = '';						
		}
		else
		{ 
			if (response == -1)
			{
				document.getElementById('avail_username_res').innerHTML = 'Please do not include spaces in your username.';
				document.getElementById('avail_username_res').style.color = '#ff1111';
			
			}		
			else
			{
				document.getElementById('avail_username_res').innerHTML = 'Sorry, username unavailable. Please choose another.';
				document.getElementById('avail_username_res').style.color = '#ff1111';
			
			}
		}
		//previous_response = document.getElementById('current_content_status_id').value;				
		//
			
			/*if (response == 1) {
				document.getElementById('preview_container').style.display = 'none';
				document.getElementById('content_container').style.display = 'inline';					
			}
			else {
				document.getElementById('content_container').style.display = 'none';
				document.getElementById('preview_container').style.display = 'inline';									
			}
			*/
			//alert(response);
			/*previous_response = response;
			window.location.reload();
			getStatus();
		}
		else {
			//alert(response);
			setTimeout("getStatus();", 5000);			
		}*/
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		/*document.getElementById('product_cage').innerHTML = response;*/
	}
		
}

function check_username(host)
{				
				var username = document.getElementById('username_holder').value;
				http.open('get', 'http://' + host + '/services/username_check.php?username=' + username);
				http.onreadystatechange = handleProducts_check_username; 	
				http.send(null);											
}

/* The following function creates an XMLHttpRequest object... */
	var http = createRequestObject(); 
	var previous_response;
	
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}


/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts_check_username(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert("Resp status - " + http.readyState);
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		//alert(response);		
		//Username - available
		if (response == 1) {
			document.getElementById('avail_username_res').innerHTML = 'Username available!';
			document.getElementById('avail_username_res').style.color = '';						
		}
		else
		{ 
			if (response == -1)
			{
				document.getElementById('avail_username_res').innerHTML = 'Please do not include spaces or special characters in your username.';
				document.getElementById('avail_username_res').style.color = '#ff1111';			
			}		
			else
			{
				document.getElementById('avail_username_res').innerHTML = 'Sorry, username unavailable. Please choose another.';
				document.getElementById('avail_username_res').style.color = '#ff1111';
			
			}
		}
				//previous_response = document.getElementById('current_content_status_id').value;
				
		//
			
			/*if (response == 1) {
				document.getElementById('preview_container').style.display = 'none';
				document.getElementById('content_container').style.display = 'inline';					
			}
			else {
				document.getElementById('content_container').style.display = 'none';
				document.getElementById('preview_container').style.display = 'inline';									
			}
			*/
			//alert(response);
			/*previous_response = response;
			window.location.reload();
			getStatus();
		}
		else {
			//alert(response);
			setTimeout("getStatus();", 5000);			
		}*/
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		/*document.getElementById('product_cage').innerHTML = response;*/
	}
		
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert("Resp status - " + http.readyState);
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
	}
		
}

function return_back()
{
	history.go(-1) 
}

function go_home(host_domain)
{
	window.location = host_domain;
}

function character_counter(max_size)
{
	var content = document.getElementById('personal_message_id').value;
	var chars_left = max_size - content.length;
	if (chars_left <= 0)
	{
		var cropped_content = content.substr(0,max_size);
		document.getElementById('personal_message_id').value = cropped_content;
		document.getElementById('character_counter').innerHTML = 0;
	}
	else
	{
		document.getElementById('character_counter').innerHTML = chars_left;
	} 			
	//alert();
}

function show_rate(rate_cut, tag_in_id, tag_out_id)
{
	var rate = document.getElementById(tag_in_id).value;
	var ready_rate = rate_cut * rate ;		
	var result = ready_rate.toFixed(2);	
	document.getElementById(tag_out_id).innerHTML = "<h4>" + result + "GBP</h4>";	 		
	//alert();	
}

function add_photo_upload()
{
	var tbl = document.getElementById('photos_holder');
	  var lastRow = tbl.rows.length;
	  // if there's no header row in the table, then iteration = lastRow + 1
	  var iteration = lastRow;
	  var row = tbl.insertRow(lastRow);
	  
	  // left cell
	  var cellLeft = row.insertCell(0);
	  var new_node = document.createElement("div");
//	  var cell_content = '<div id=\"photo_holder_' + lastRow + '"></div>&nbsp;<input type="button" name="upload_photo_' + lastRow + '" value="Upload" class="button" onClick="window.open(\'cropper/uploader.php?num=' + lastRow + '&mode=3\', \'myWin\', \'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, fullscreen=yes\'); return false" />&nbsp;<input type="button" name="remove_photo_' + lastRow + '" value="Remove" class="button" onClick="remove_photo_premium(\'photo_id[' + lastRow + ']\', \'photo_holder_' + lastRow + '\',' + lastRow + ');" /><input type="hidden" name="photo_id[' + lastRow + ']" value="" id="photo_id[' + lastRow + ']" size="4" /><input type="hidden" name="old_photo_id[' + lastRow + ']" value="" size="4" /><br />';	  
	  var cell_content = '<div id=\"photo_holder_' + lastRow + '"></div>&nbsp;';
	  //alert(cell_content);
	  new_node.innerHTML = cell_content;
	  cellLeft.appendChild(new_node);
	  
	  //rigth cell
	  var cellRight = row.insertCell(1);
	  var new_node = document.createElement("div");
	  var cell_content = '<input type="button" name="upload_photo_' + lastRow + '" value="Upload" class="button" onClick="window.open(\'cropper/uploader.php?num=' + lastRow + '&mode=3\', \'myWin\', \'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, fullscreen=yes\'); return false" />&nbsp;<input type="button" name="remove_photo_' + lastRow + '" value="Remove" class="button" onClick="remove_photo_premium(\'photo_id[' + lastRow + ']\', \'photo_holder_' + lastRow + '\',' + lastRow + ');" /><input type="hidden" name="photo_id[' + lastRow + ']" value="" id="photo_id[' + lastRow + ']" size="4" /><input type="hidden" name="old_photo_id[' + lastRow + ']" value="" size="4" /><br />';	  	  
	  new_node.innerHTML = cell_content;
  	  //var textNode = document.createTextNode('aaaa');
	  cellRight.appendChild(new_node);	  	  
	  
}
//This function charge punter for content and shows it. or insufficient credit it balance is too low
function show_content(host, content_id, content_type, punter)
{
//content_type: 1. video 2. photo set	
	//alert(host + " - " +content_id+" - "+content_type+" - "+punter );
	http.open('get', 'http://' + host + '/services/content_charge.php?content_id=' + content_id + '&content_type=' + content_type + '&punter=' + punter);
	http.onreadystatechange = show_content_handleProducts; 	
	http.send(null);
}
function show_content_handleProducts()
{
	//	alert("Resp status - " + http.readyState);
	if(http.readyState == 4)
	{ 
		//Finished loading the response
		var response = http.responseText;
		//	alert(response);		
		//Not enough credit
		if (response == -1) {
			alert('You have insufficient account balance. Please top up account first');
		}
		//User is not logged in - redirect to login page
		else if (response == -2) {
			window.location = "register.php";
		}
		else
		{
		    var brokenstring=response.split(";"); 			
		    var new_balance = brokenstring[0];
		    var content_type = brokenstring[1];
		    var content_id = brokenstring[2];
		    var content_html = brokenstring[3];
		    //Now update balance on screen		    
		    update_punter_balance(new_balance);		    
		    //!!!May be update content thumb as well - show that this content is free for this punter from now on.  		   
		     
			//alert('ALL GOOD!' + new_balance + ' - ' + content_type + ' - ' +  content_id + ' - ' + content_html);
		    //Video
			if (content_type == 1)
			{
				//document.getElementById('main_video_description').innerHTML=title;
				var s1 = new SWFObject("player.swf","ply","441","366","9","#FFFFFF");
				s1.addParam("allowfullscreen","true");	
				autostart_var = "true";
				//alert(autostart + 'http://' + host + '/stream/video_counter.php?video_id=' + video_id + '&ip=' + ip);
				s1.addParam("flashvars", content_html + "&autostart=" + autostart_var);			
				s1.write("video_holder");							
			}
			//Photo set
			else				
				if (content_type = 2)
				{					
					document.getElementById('photo_set_holder').innerHTML=content_html;
					//document.getElementById('photo_set_description').innerHTML=content_html;										
				}	    		    
		}
	}	
}
function update_punter_balance(balance)
{
	document.getElementById('balance_holder').innerHTML=balance;	
}