// JavaScript Document
//Custom.js will hold js fcns


/*this fcn will put the city name in the location bar and submit the search when clicked*/
function updateSearch(city){
	document.getElementById('locationsearch').value = city;
	
	submitform(true);
		
	
}
/*toggles the hidden city list*/
function revealCities(){
	document.getElementById('more_link').style.display="none";
	document.getElementById('hidden_cities').style.display="inline";
}
/* this fcn submits the search form*/
function submitform(manual){
//if manual is set it means the form is being submitted via a links
//locationsearch set via form submit vs. map pan
setCookie('from_pan', false);
//first check to see if empty
var catinput = document.getElementById("catsearch").value;   
var locinput = document.getElementById("locationsearch").value; 
//set cookies
document.cookie = 'catsearch=' + catinput; 
document.cookie = 'locationsearch=' + locinput;
//alert("calling sortby; carsearch = " + catinput + "locationinput = " + locinput);
//sortBy('catinput', 'distance');
//sortBy(catinput, 'distance');
//alert("catinput = " + catinput + "; locinput = " + locinput);
  //don't submit is nothing is in search boxes
  if ((catinput == "" || catinput == null) && (locinput == "" || locinput == null)){	
  }else{
	  if(manual){
		  document.form1.submit();
	  }else{
	  	return true;
	  }
  }

return false;
}



function map_markprop(point,category,rec_id,type,types,title, iconName) {
		this.point=point;
		this.category=category;
		this.type=type;
		this.types=types;
		this.rec_id=rec_id;		
		this.title=title;
		this.iconName=iconName;		
	}
					
					
	/**JNM: this funtion is setup using PHPLiveX which allows javascript to call php fcns using ajax
	//rec_id is the same as spot_id
	getDetails is bound to the click fcn on the map icons.  The result titles have an action bound so clicking on them is like clicking on the map icons
	**/
	function getDetails(category, rec_id)
	{
		//'preloader' here refers to which div the preloader icon will appear in.  Note that a div with id="pr" must exist or these fcns will fail without an error msg
		getSpotDetails(rec_id,{'preloader':'pr',
				"onFinish": function(response){
				//For pages where these elements may not exist, check to see if they exist first
				var mydetails = document.getElementById('details');
				if(mydetails){
					if(response == 0){
							mydetails.innerHTML = "Sorry spot not found.";
						}
					else{
							mydetails.innerHTML = response;
					}
				}
			}
			});
		getSpotRatings(rec_id,{'preloader':'pr',
				"onFinish": function(response){
								var myratings = document.getElementById('ratings');
								if(myratings){
									if(response == 0){
										myratings.innerHTML = "Sorry no ratings found for this spot.";
									}else{
										no_of_ratings ="";
										for(i=1;i<=response;i++)
										{
											no_of_ratings = no_of_ratings + '<img src="images/img_review1.jpg" width="22" height="28">'; 
										}
										
										myratingsz.innerHTML = "&nbsp;&nbsp;&nbsp;" + no_of_ratings;
									}//end else
								}//end if myratings
							}//end function(response)
	
			});
		//alert(rec_id);
		getSpotReviews(rec_id,{'preloader':'pr',
				"onFinish": function(response){
				//alert("getspotreviews response = " + response);
				//For pages where these elements may not exist, check to see if they exist first
				var myreviews = document.getElementById('reviews');
				if(myreviews){
					if(response == 0){	
						myreviews.innerHTML = "<table><tr><td>Sorry no reviews found for this spot.</td></tr><tr><td><span id=\"write-review-link\"><a href='review_spot.php?point_id=" + rec_id + "'>Write a review &raquo;</a></span></td></tr></table>";
					}else{
						myreviews.innerHTML = response;
					}
				}//end if(myreviews)
			}
			});
		dv.innerHTML='<div id="tmp" style="width:auto; height:100px;"><img src="images/loading.gif"></div> <br>';
 		var url11 = "showdetails.php";
		var qry11="?categoryid=" +  category +"&pointid="+rec_id; 
		var result1='tmp';             
		
		try{
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					alert("asdasd");
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
				alert("Your browser broke!");
				return false;
				}
			}
		} 
		ajaxRequest.onreadystatechange = function(){
		//alert(ajaxRequest.readyState);
			if(ajaxRequest.readyState == 4){
			var a=ajaxRequest.responseText;
				try{
				//alert(document.getElementById('tmp').innerHTML); alert('try');
					document.getElementById('tmp').innerHTML=ajaxRequest.responseText;	
					//alert(document.getElementById('tmp').innerHTML); alert('try');
				}catch(e){ 		
							
					dv.innerHTML=ajaxRequest.responseText;
					//ajaxRequest.open("POST", url11+qry11, true);
					//ajaxRequest.send(null); 
					//alert(dv.innerHTML); alert('catch');
				}
			}
		}
		//if(firstime=="0")
		//{
			ajaxloaderr=setInterval("checkajax('"+url11+qry11+"')",3000);		
		//}
		//else
		//{
			//ajaxRequest.open("POST", url11+qry11, true);
			//ajaxRequest.send(null); 
		//}
		return  '<div id="tmp" style="width:auto; height:120px;" ><img src="images/loading.gif"></div>';
	}
	function checkajax(url1)
	{
		ajaxRequest.open("POST", url1, true);
	 	ajaxRequest.send(null); 
		clearInterval(ajaxloaderr);
		firstime=1;
	}
	/*function validate(){
		if(onSubmitCheck(document.forms['addshape']))
		{ 
			return true;
		}else{
			return false;
		}
	} */
	
	 function makemarker_map(thismap_marker) 
	 {
		thismap_marker.marker =new GMarker(thismap_marker.point , {icon:customIcons[thismap_marker.types], title:thismap_marker.title});
		map.addOverlay(thismap_marker.marker);
	 	
	   	GEvent.addListener(thismap_marker.marker, 'click', function() {
		//alert("clicked");
    	   	thismap_marker.marker.openInfoWindowHtml(getDetails(thismap_marker.category,thismap_marker.rec_id),10000);
		});	
		
			
	 	thismap_marker.marker.bindInfoWindowHtml(dv.innerHTML);		
    }    
	
	
	
	
	
	/*not sure what this is doing.  Moved from searchresults.php */
	function onLoadFn()
	{		
		alert('Function Calling End Here');
		document.getElementById('upperDiv').style.width = '0px';
		document.getElementById('upperDiv').style.height = '0px';
		document.getElementById('upperDiv').style.display = 'none';
	}

function convert(str)
{
	//alert(str);
//sanitize input pasted from word.  This is adapted from the tool at dan.hersam.com/tools/smart-quotes.html
var s = str;
    s = s.replace( /\u2018/g, "'" );
    s = s.replace( /\u2019/g, "'" );
    s = s.replace( /\u201c/g, '"' );
    s = s.replace( /\u201d/g, '"' );
    s = s.replace( /\u2013/g, '-' );
    s = s.replace( /\u2014/g, '--' );
    s = s.replace( /\uFFFD/g, "'" );
 // Strip out all line breaks.  This seems to work but if it causes problems then use the handleEnter fcn below to disable the enter key in the text areas
 s = s.replace(
 // Replace out the new line character.
 new RegExp( "\\n", "g" ),
  
 // Put in ... so we can see a visual representation of where
 // the new line characters were replaced out.
 "<br />"
 );
   return(s);

}

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
   /*disabling enter key in review desc field to avoid linebreaks in db*/             
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}   
	
	/*sorting fcns*/
	function sortBy(start, sorttype, radius){
		//set radius = 25 if not defined
		var radius = (radius == null) ? 25 : radius;
		//setting these cookies to preserve sorting when panning the map
		setCookie('radius', radius);
		setCookie('sorttype', sorttype);
		var catsearch = getCookie('catsearch');
		var locationsearch = getCookie('locationsearch');
		//var radius = getCookie('radius');
		//alert("catsearch = " + catsearch + "; start = " + start + "; sorttype = " + sorttype);
		createQuery(start, catsearch, locationsearch, sorttype, radius, {'preloader':'pr',
				"onFinish": function(response){
				//For pages where these elements may not exist, check to see if they exist first
				//document.getElementById("searchresults").value = response;
				//clear map
				//alert("finished");
				map.clearOverlays();
				document.getElementById("searchresults").innerHTML = response;
				//now parse new xml and find markers
				findpoint_new();
				//recenter map and keep zoom the same
				//map.setCenter(center);
				//map.setZoom(zoom);
				
			}
			});	
	}
	
	/*handle the buttons from review_spot and review_preview page*/
function validate(myform, button){
		//alert("myform = " + myform);	
	if(document.getElementById('rtdes').value == '')
	{
		if(button == "submit" || button == "save"){
			//alert('Please write a review.');	
			//return false;
		}
	}else{
		//sanitize input pasted from word
		//find all form fields
		//for(i=0; i<document.forms[myform].elements.length; i++)
		//{
		//	var myelement = document.forms[myform].elements[i];
		//set input field equal to sanitized input before form is submitted
		//document.forms[myform].elements[i].value = convert(document.forms[myform].elements[i].value);
		document.getElementById('rtdes').value = convert(document.getElementById('rtdes').value);
		//}
	}
	//now check to see which button was pressed
	//if(isset($_POST['preview'])){
		//alert("preview");
		//change form action
		//alert(button);
		if(button == 'preview'){
			document.forms[myform].action = "/review_preview.php";
		}else if(button == 'edit'){
			//change mode before sending back to review_spot.php
			document.getElementById('mode').value = "edit";
		}else if(button == 'upload_image'){
			document.getElementById('mode').value = "upload_image";
		}
			
	}


