var feedsrc = "ajaxxml.cfm";
var requestObj;
var xmlDoc;
var whatbrowser = "IE";
var parser;

function httpRequest() {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		requestObj = new XMLHttpRequest();
	
		if (requestObj.overrideMimeType) {
			requestObj.overrideMimeType("text/xml");
		}
	} else if (window.ActiveXObject) { // IE
		
		try {
			requestObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				requestObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return null;
			}
		}
	}
	if (!requestObj) {
		alert("Your browser does not support advanced features of this web page.");
		return null;
	} else {
		return requestObj;
	}
}


var params = "";

var http = new httpRequest;

function ajaxPost(form) {
	
	params = "";
    for (i = 0;i < form.elements.length;i++)
	{
	   params = params + form.elements[i].name + "=" + form.elements[i].value + "&";
	}
    
	http.open("POST", feedsrc, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {ajaxPostData(form)};

	http.send(params);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			ajaxPostData(http);
		}
	}
	catch (e) {}
	
}

function ajaxPostCateg(form) {
	
	params = "";
    for (i = 0;i < form.elements.length;i++)
	{
	   params = params + form.elements[i].name + "=" + form.elements[i].value + "&";
	}
    
	http.open("POST", feedsrc, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {ajaxPostCategData(form)};

	http.send(params);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			ajaxPostCategData(http);
		}
	}
	catch (e) {}
	
}

function userLogin(form) {
	
	params = "";
    for (i = 0;i < form.elements.length;i++)
	{
	   params = params + form.elements[i].name + "=" + form.elements[i].value + "&";
	}
    
	http.open("POST", feedsrc, false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	
	http.onreadystatechange = function() {getSessionData()};
	
	http.send(params);
	
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
	
	
}


function  archive(poi) {
	params = "archiveTu=" + poi;
	http.open("GET", feedsrc+"?"+params, false);
	
	http.onreadystatechange = function() {getSessionData()};

	http.send(null);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
}

function  notify(poi,notifyFlag) {
	params = "notifyTu=" + poi+"&notifyFlag="+notifyFlag;
	//alert(params);
	http.open("GET", feedsrc+"?"+params, false);
	
	http.onreadystatechange = function() {getSessionData()};

	http.send(null);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
}

function  savePOI(poi) {
	params = "saveTu=" + poi;
	http.open("GET", feedsrc+"?"+params, false);
	
	http.onreadystatechange = function() {getSessionData()};

	http.send(null);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
}

function getSession() {
	
	params = "sessionquery=1";
	
	http.open("POST", feedsrc, false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {getSessionData()};

	http.send(params);
	
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
}

function ajaxGet() {

	http.open("GET", feedsrc+"?"+params, true);
	
	http.onreadystatechange = function() {ajaxGetData()};

	http.send(null);
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && http.onreadystatechange == null) {
			//alert(http.responseText);
			getSessionData(http);
		}
	}
	catch (e) {}
}

function ajaxGetData() {		
	if (http.readyState == 4 && http.status == 200)		 {
		try //Internet Explorer
		  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(http.responseText);
		  //alert('IE');
		  }
		catch(e)
		  {
		  try //Firefox, Mozilla, Opera, etc.
		    {
		    parser=new DOMParser();
		    xmlDoc=parser.parseFromString(http.responseText,"application/xml");
			//alert('FF');
		    }
		  catch(e) {alert(e.message)}
		  }
											
			if (xmlDoc.getElementsByTagName("item").length > 0) {
				var sData = xmlDoc.getElementsByTagName("item")[i];
				
				sTest = sData.getElementsByTagName("data_displayname")[0].childNodes[0].nodeValue;
					
			}
	}
}

function ajaxPostData(form) {
	var ajaxResponse;
	if (http.readyState == 4 && http.status == 200)		 {
		
		try //Internet Explorer
		  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(http.responseText);
		  //alert('IE');
		  }
		catch(e)
		  {
		  try //Firefox, Mozilla, Opera, etc.
		    {
		    parser=new DOMParser();
		    xmlDoc=parser.parseFromString(http.responseText,"application/xml");
			//alert('FF');
		    }
		  catch(e) {alert(e.message)}
		  }
		
		
		if (xmlDoc.getElementsByTagName("item").length > 0) {
			//alert(xmlDoc.getElementsByTagName("item").length);
			var sData = xmlDoc.getElementsByTagName("item")[0];
			
			
			if (xmlDoc.getElementsByTagName("data_summary").length > 0) {
			
			if (xmlDoc.getElementsByTagName("data_summary")[0].hasChildNodes()) {
				var stars="";
				var newentry = "";
				for (i=0;i<xmlDoc.getElementsByTagName("data_rating")[0].childNodes[0].nodeValue;i++)
	  			{
				stars = stars + "<img alt='Free GPS Points of Interest Community' src='http://www.gointu.com/images/star.gif'> ";
				}
				var d = new Date();
			    var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
				var cur_day = d.getDate();
				if (cur_day < 10) cur_day = "0" + cur_day;
				var x=document.getElementById("user_comments_area")
	  			newentry= stars + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>" + xmlDoc.getElementsByTagName("data_summary")[0].childNodes[0].nodeValue + "</b><br/>";
				newentry= newentry + xmlDoc.getElementsByTagName("data_username")[0].childNodes[0].nodeValue + ":" + cur_day + "-" + m_names[d.getMonth()] + "-" + d.getFullYear()+ "<br/>";
				newentry= newentry + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>" + xmlDoc.getElementsByTagName("data_comments")[0].childNodes[0].nodeValue + "</font><br/><hr class='dash' />";
				x.innerHTML= newentry;
			}
			
			}
			
			if (xmlDoc.getElementsByTagName("data_userfound").length > 0) {
				if (xmlDoc.getElementsByTagName("data_userfound")[0].childNodes[0].nodeValue == 1) {
					document.getElementById("passdata").style.display='none';
					alert('An email has been sent to ' + xmlDoc.getElementsByTagName("data_emailaddress")[0].childNodes[0].nodeValue + '. \nPlease use the link provided in the email in order to reset your password.');
				}
				else {
					alert('The username/email values provided cannot be found! Please review and try again.');
				
				}
				
			}
			
			if (xmlDoc.getElementsByTagName("data_gps_type").length > 0) {
				if (xmlDoc.getElementsByTagName("data_logged")[0].childNodes[0].nodeValue == 1) {
					var expcontent = '<br/>Your Saved POIs have been exported to the ' + xmlDoc.getElementsByTagName("data_gps_type")[0].childNodes[0].nodeValue + ' format.<br/><br />Click on this link: <a href=\"http://www.gointu.com/Download/' + xmlDoc.getElementsByTagName("data_filename")[0].childNodes[0].nodeValue + '\" target=\"_blank\">' + xmlDoc.getElementsByTagName("data_filename")[0].childNodes[0].nodeValue + '</a> to download the file.<br/><br />Thanks.';
					document.getElementById("poiexportfile").innerHTML = expcontent;
					document.getElementById("poiexport").style.display='none';
					document.getElementById("poiexportcontent").style.display='block';
				}
				else {
					alert('The username/email values provided cannot be found! Please review and try again.');
				
				}
				
			}
						
		}
	}
}			

function ajaxPostCategData(form) {
	var ajaxResponse;
	if (http.readyState == 4 && http.status == 200)		 {
		
		try //Internet Explorer
		  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(http.responseText);
		  //alert('IE');
		  }
		catch(e)
		  {
		  try //Firefox, Mozilla, Opera, etc.
		    {
		    parser=new DOMParser();
		    xmlDoc=parser.parseFromString(http.responseText,"application/xml");
			//alert('FF');
		    }
		  catch(e) {alert(e.message)}
		  }
		
		
		if (xmlDoc.getElementsByTagName("item").length > 0) {
			//alert(xmlDoc.getElementsByTagName("item").length);
			var sData = xmlDoc.getElementsByTagName("item")[0];
			
			if (xmlDoc.getElementsByTagName("data_categ_name").length > 0) {
			
			if (xmlDoc.getElementsByTagName("data_categ_name")[0].hasChildNodes()) {
				//alert('Category updated!');
			}
			
			}
					
		}
	}
}			

											
function getSessionData() {
	
	var ajaxResponse;
	
	if (http.readyState == 4 && http.status == 200)		 {
	
		try //Internet Explorer
		  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(http.responseText);
		  
		  }
		catch(e)
		  {
		  try //Firefox, Mozilla, Opera, etc.
		    {
		    parser=new DOMParser();
			xmlDoc=parser.parseFromString(http.responseText,"application/xml");
			
		    }
		  catch(e) {alert(e.message)}
		  }
		
		if (xmlDoc.getElementsByTagName("item").length > 0) {
			
			var x=document.getElementById("logged");
			var sData = xmlDoc.getElementsByTagName("item")[0];
			
			if (xmlDoc.getElementsByTagName("data_logged")[0].hasChildNodes()) {	
				
				if (x.value != xmlDoc.getElementsByTagName("data_logged")[0].childNodes[0].nodeValue) {
					if (xmlDoc.getElementsByTagName("data_logged")[0].childNodes[0].nodeValue > 0) {
						var date = new Date();
						if (document.getElementById("rememberChk").checked) {
							// current date + 20 years
							date.setTime(date.getTime()+(7300*24*60*60*1000));
						}
						else {
							// minus 1 day
							date.setTime(date.getTime()-(24*60*60*1000));
						}
						var expires = "; expires="+date.toGMTString(); 
						
						document.cookie = 'gointuID='+document.getElementById("username").value+expires+'; path=/';
						
					}
				}
				if (xmlDoc.getElementsByTagName("data_logged")[0].childNodes[0].nodeValue > 0) {
					document.getElementById("loggeduser").innerHTML = 'Welcome <span id="sessionuser" class="quoter">' + xmlDoc.getElementsByTagName("data_sessionuser")[0].childNodes[0].nodeValue + '</span>!<br/>Saved TUs: <span id="savedPOIs" class="quoter"><a href="myTus.cfm">' + xmlDoc.getElementsByTagName("data_savedpois")[0].childNodes[0].nodeValue + '</a></span><br/><a href=\"##\" onclick=\"document.getElementById(\'poiexport\').style.display=\'block\';\">Export POIs</a>';
					
				}
				else {
					document.getElementById("loggeduser").innerHTML = '<br/><span id="sessionuser" class="quoter"></span><span id="savedPOIs" class="quoter"></span><a href="loginTu();">login</a> | <a href="account.cfm">sign up</a>';
			
				}
				
				if ((xmlDoc.getElementsByTagName("data_notifyflag").length > 0) && (document.getElementById("notifydata")))  {	
					//alert(xmlDoc.getElementsByTagName("data_notifyflag")[0].childNodes[0].nodeValue);
					if (xmlDoc.getElementsByTagName("data_notifyflag")[0].childNodes[0].nodeValue > 0) {
						document.getElementById("notifydata").innerHTML = '<a href="##" onclick="notifyTu();">Remove my subscription to this POI</a>';
					}
					else {
						
						document.getElementById("notifydata").innerHTML = '<a href="##" onclick="notifyTu();">Notify me when POI data is changed</a>';
			
					}
				}
				
				x.value= xmlDoc.getElementsByTagName("data_logged")[0].childNodes[0].nodeValue;
				
						
			}
			
		}
	}
}			
				