var lastElId = '';
var rockTypeDD;
var rockTypeText;
var otherRocktypeSelected=0;
var ajaxBackList= new Array();
var currentAjaxURL;
var foundHighlightElement=false;
var routeSelect_onchange_skip = false;
var selTopoID;
//cars = ["Mercedes", "Ford", "Chrysler"]
//document.write(cars.pop())
function showLogging(routeId){
	alert("The route logging system is comming soon");
	//location.href='ascentLogger.php?routeId=' + routeId;
}
function getCragRouteListSelect(route_order,cragId){
	
	if(route_order==1){
		url='getCragRoutelistSelect.php?cragId=' + cragId + "&route_order=1";
	}
	if(route_order==2){
		url='getCragRoutelistSelect.php?cragId=' + cragId + "&route_order=2";
	}
	var xmlHttp;
	if(window.XMLHttpRequest){ // For Mozilla, Safari, ...
		var xmlHttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ // For Internet Explorer
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlHttp.open('GET', url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	try{                                  
		document.getElementById('cragRouteListSelect').innerHTML = '';
	}
	catch(e){};
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4){	
			var htmlStr=xmlHttp.responseText;// + strURL;		
			document.getElementById('cragRouteListSelect').innerHTML = htmlStr;
		}
	}
	xmlHttp.send(url);
}

function highlightElementBG(elId){
	if(lastElId!=''){
		var obj = document.getElementById(lastElId);
		obj.style.backgroundColor='#CCCCCC';
	}
	var obj = document.getElementById(elId);
	obj.style.backgroundColor='#FFFFFF';
	lastElId = elId;
	
}
function highlightElement(elId){
	
	if(lastElId!=''){
		try{
			var obj = document.getElementById(lastElId);
			if(obj!=null){
		  		obj.style.color='#008899';
		  		obj.style.fontSize='12px';		
			}
		}
		catch(e){
			
		}
	}
	
	var obj = document.getElementById(elId);
	
	if(obj!=null){
		obj.style.color='#FF9900';
		obj.style.fontSize='18px';
		foundHighlightElement = true;
		//alert(obj.id + ' highlighted');
	}
	
	//obj.style='header5style';
	try{
		document.getElementById(lastElId + '_settings').style.visibility='hidden';
	}
	catch(e){}
	
	try{
		//show settings button if element is topo
		document.getElementById(elId + '_settings').style.visibility='visible';
	}
	catch(e){}
	
	lastElId = elId;
	
}
function getTopoOrSector($itemId){
	
	var parts = $itemId.split('_','2');
	var TopoOrSector = parts[0];
	var id = parts[1];
	if(TopoOrSector=='TOPO'){
		getTopoInfo(id);
	}
	else if(TopoOrSector=='SECTOR'){
		
		getSectorInfo(id,0);
	}

}
	
function getTopoInfo(topId,dest,maxWidth){	
	
	if(dest==null){
		dest='topoDiv';
	}
	
	var obj = document.getElementById(dest);
	obj.innerHTML = '';
	url = 'getTopo.php?topoId=' + topId;
	
	try{
		maxWidth=page_topo_dim;
	}
	catch(e){}
	
	if(maxWidth!=null){
			url=url + "&maxWidth=" + maxWidth;
	}
	topoDisplayMode=1;
	try{
		topoDisplayMode = page_topoDisplayMode;
	}	
	catch(e){}
	
	if(topoDisplayMode==2){
		url=url + '&displayMode=2';
	}
	   
	
	try{                                  
		obj.innerHTML = '<div style="height:600px; text-align:center;">Loading Topo (If the topo takes more than 30 seconds to load then <a href="' + pageName + '?topoId=' + topId + '&tId=' + topId + '">click here</a>)<br/><br/><br/><br/><br/><br/><br/><br/><img src="/images/symbols/ajax-loader.gif"/></div>';
	}
	catch(e){}
	
	getData(url,dest);
	
	try{
		if(currentAjaxURL!=null){
			if(trim(currentAjaxURL)!=''){
				var tempUrl=currentAjaxURL;
				ajaxBackList.push(tempUrl);
			}
		}
	}
	catch(e){}
	
	currentAjaxURL = url;
	try{
		getNonEmptyData('getTopoFavLink.php?topoId=' + topId,'favlink');
	}catch(e){}
	try{		
		highlightElement(topId);
	}
	catch(e){
		
	}
	//alert(ajaxBackList.length);
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
	//alert("ok");
}
function getTopoAtCrag(topId,cragId){	

// want to load a different crag page if the user clicks on an inset topo from a neighboring crag
	var obj = document.getElementById('topoDiv');
	obj.innerHTML = '';
	url = 'getTopo.php?topoId=' + topId;
	try{                                  
		document.getElementById('topoDiv').innerHTML = '<div style="height:600px; text-align:center;"><br/><br/><br/><br/><br/><br/><br/><br/><img src="/images/symbols/ajax-loader.gif"/></div>';
	}
	catch(e){}
	getData(url,'topoDiv');
	
	if(currentAjaxURL!=null){
		if(trim(currentAjaxURL)!=''){
			var tempUrl=currentAjaxURL;
			ajaxBackList.push(tempUrl);
		}
	}
	currentAjaxURL = url;
	try{
		getNonEmptyData('getTopoFavLink.php?topoId=' + topId,'favlink');
	}
	catch(e){}
	try{		
		highlightElement(topId);
	}
	catch(e){
		
	}
	//alert(ajaxBackList.length);
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
	
	alert("Loading inset topo");
	//alert("innerhtml=" + document.getElementById('topoCragId').innerHTML);
	var thisTopoCragId =document.getElementById('topoCragId').innerHTML;
	//alert("THISTCID " + thisTopoCragId);
	if(thisTopoCragId!=cragId){
		//alert("Redirectiong");
		window.location = "../crag.php?tId=" + topId;
	}
	else{
		alert("dobra " + thisTopoCragID + " " + cragID);
	}
}
function getSectorInfo(sId,edit){	
	var obj = document.getElementById('topoDiv');
	obj.innerHTML = '';
	url= 'getSector.php?sectorId=' + sId;
	getData(url,'topoDiv');
	if(currentAjaxURL!=null){
		if(trim(currentAjaxURL)!=''){
			var tempUrl=currentAjaxURL;
			ajaxBackList.push(tempUrl);
		}
	}
	currentAjaxURL = url;
	//hideRockTypeText();
	//getNonEmptyData('getTopoFavLink.php?topoId=' + topId,'favlink');
	highlightElement('sector_' + sId);
	
}
function getRouteInfo(routeId,maxTopoDimension,selTopoId,routeName){
	getRouteInoTopoDivContent(routeId,maxTopoDimension,selTopoId,routeName);
	try{
	var obj = document.getElementById('topoListLink');
	var topoId = document.getElementById('topoToHighlight').innerHTML
	obj.innerHTML= '<a href="crag.php?tId=' + topoId + '"><strong>TOPO LIST</strong></A>'
	}
	catch(e){}
	return;
}

function getRouteInoTopoDivContent(routeId,maxTopoDimension,selTopoId,routeName){
	var url= 'getOneRoute.php?routeId=' + routeId;
	try{
		maxTopoDimension = page_topo_dim;
	}catch(e){}
	if(maxTopoDimension>0){
		url=url + "&maxTSide=" + maxTopoDimension;
	}
	if(selTopoId>0){
		url=url + "&selTopoId=" + selTopoId;
	}
	if(routeName!=null){
		url=url + "&routeName=" + routeName;
	}
	
	topoDisplayMode=1;
	try{
		topoDisplayMode = page_topoDisplayMode;
	}
	catch(e){}
	
	if(topoDisplayMode==2){
		url=url + '&displayMode=2';
	}

	var xmlHttp;
	if(window.XMLHttpRequest){ // For Mozilla, Safari, ...
		var xmlHttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ // For Internet Explorer
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlHttp.open('GET', url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	try{                                  
		document.getElementById('topoDiv').innerHTML = '<div style="height:600px; text-align:center;"><br/>Loading Topo (If the topo takes more than 30 seconds to load then <a href="' + pageName + '?routeId=' + routeId + '&tId='+ selTopoId + '"&rId=' + routeId + '>click here</a>)<br/><br/><br/><br/><br/><br/><br/><img src="/images/symbols/ajax-loader.gif"/></div>';
	}
	catch(e){}
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4){	
			var htmlStr=xmlHttp.responseText;// + strURL;			
			if(trim(htmlStr)=='IGNORE'){
			}
			else{
				 htmlStr = htmlStr + "<br>";
				 document.getElementById('topoDiv').innerHTML = htmlStr;
				 try{
					var topoId = document.getElementById('topoToHighlight').innerHTML
					highlightElement(topoId);
				}
				catch(ex){}
			}			
		}
	}
	xmlHttp.send(url);
	if(currentAjaxURL!=null){
		if(trim(currentAjaxURL)!=''){
			var tempUrl=currentAjaxURL;
			ajaxBackList.push(tempUrl);
		}
	}
	currentAjaxURL = url;
	
	routeSelect_updateOption('route_' + routeId);
	//alert("!!!!!!!!!!!!");
	highlightElement('route_' + routeId);
	//alert("?????????");
	
	
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
}

/*function routeSelect_onchange(routeId){
	if(routeSelect_onchange_skip==true){
		return;
	}
	var obj = document.getElementById('topoDiv');
	obj.innerHTML = '';
	var url= 'getOneRoute.php?routeId=' + routeId;
	
	getData(url,'topoDiv');
	if(currentAjaxURL!=null){
		if(trim(currentAjaxURL)!=''){
			var tempUrl=currentAjaxURL;
			ajaxBackList.push(tempUrl);
		}
	}
	currentAjaxURL = url;
	
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
	
}
*/
/*
function getRouteDescriptions(routeId,selTopoId){
	var obj = document.getElementById('topoDiv');
	
	obj.innerHTML = '';
	var url= 'getOneRoute.php?routeId=' + routeId + '&selTopoId=' + selTopoId;
	
	getData(url,'topoDiv');
	
	url='adjacentRoutes.php?routeId=routeId'
	
	if(currentAjaxURL!=null){
		if(trim(currentAjaxURL)!=''){
			var tempUrl=currentAjaxURL;
			ajaxBackList.push(tempUrl);
		}
	}
	currentAjaxURL = url;
	routeSelect_updateOption('route_' + routeId);
	highlightElement('route_' + routeId);
	try{
		var topoId = document.getElementById('topoToHighlight').innerHTML
		highlightElement(topoId);
	}
	catch(ex){}
	
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
	
	
}
*/
function loadPreviousAjaxUrl(){
	var maxWidth;
	var obj = document.getElementById('topoDiv');
	obj.innerHTML = '';
	try{
		var url=ajaxBackList.pop();
	}
	catch(e){return;}
	
	if(url != null){
		
		try{
			maxWidth=page_topo_dim;
		}
		catch(e){}
	
		if(maxWidth!=null){
			url=url + "&maxWidth=" + maxWidth;
		}
	
		
		getData(url,'topoDiv');
		
		var elems = url.split('?');
		sciptName = elems[0];	
		qstr = elems[1];
		nvpairs=qstr.split('&');
		for(i=0;i<nvpairs.length;i++){
			nameAndValuePair=nvpairs[i];
			
			nameAndValueElems=nameAndValuePair.split('=');
			if(nameAndValueElems[0]=='topoId' || nameAndValueElems[0]=='selTopoId'|| nameAndValueElems[0]=='routeId'){
				id=nameAndValueElems[1];	
			}
		}
			
		currentAjaxURL = url;
		//alert(id);
	}
	try{		
		foundHighlightElement = false;
		routeSelect_updateOption('route_' + id);
		highlightElement(id);
		if(foundHighlightElement==false){
			highlightElement('route_' + id);
			if(foundHighlightElement==false){
				highlightElement('sector_' + id);
			}
		}		
	}
	catch(e){}
	
	if(ajaxBackList.length>0){
		document.getElementById('backbutton').style.visibility='visible';
	}	
	else{
		document.getElementById('backbutton').style.visibility='hidden';
	}
}

function routeSelect_updateOption(optId){
	var foundoption=false;
	routeSel = document.getElementById('routeSelect');
	if(routeSel==null){
		return;
	}
	// function to change the selected option in the route select drop down on cragRouteList.php when back button is pressed.
	routeSelect_onchange_skip = true;
	try{
	//change the selected option in the route select but dont do the stuff in this onchange event procedure
	//alert('NO OF OPTS ' + routeSel.options.length);
		for(i=0;i<routeSel.options.length-1;i++){
			if(routeSel.options[i].id==optId){
				routeSel.selectedIndex=i;
				foundoption=true
			}
		}
		if(foundoption==false){
			routeSel.selectedIndex=0;
		}
	}
	catch(e){alert("err" + e.description);}
	routeSelect_onchange_skip = false;
}

function showRockTypeText(sel){				
	var s = sel.options[sel.selectedIndex].value;
	otherRocktypeSelected=1;
	if(s=='Other'){
		var pnode = sel.parentNode
		pnode.removeChild(sel) 
		//alert("ok£");
		pnode.appendChild(rockTypeText);
		rockTypeText.focus();
	}
}
function hideRockTypeText(){	
	if(!otherRocktypeSelected){
		try{
			rtt=document.getElementById('rockTypeText');
			var str=rtt.name;
		}
		catch(ex){
		//alert('nah');
			return;
		}
		rockTypeText = document.getElementById('rockTypeText');
		rockTypeText.parentNode.removeChild(rockTypeText);
	}
	
}
function sayhello(){
	alert('hello');
}
function debugMsg(msg){
	//alert(msg);
}

String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}
function loadCrag(cragId){
	location.href='crag.php?cragId=' + cragId;
}
function loadCragRouteList(cragId){
	//alert(cragId);
	location.href='cragRouteList.php?cragId=' + cragId;
}

