var ajax_response;
function ajax(requestType, url, param, callbackFunc, divId) {  
var xmlHttp;
try {    // Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	}  
	catch (e) {    // Internet Explorer    
		try	{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
   		 catch (e) {      
		 	try {        
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
      		catch (e) {        
				//alert("Your browser does not support AJAX!");        
				return false;       
			 }
		}
	} 	 
	 xmlHttp.onreadystatechange=function()
	  {
	  if(xmlHttp.readyState==4)
	    {
	   	 	// Get the data from the server's response
			ajax_response=xmlHttp.responseText;
			switch(callbackFunc){
				case 'replaceVenueSelect':
					replaceVenueSelect(ajax_response);
					break;
				case 'replaceDivContent':
					replaceDivContent(ajax_response, divId);
					break;
				case 'displayCaptcha':
					displayCaptcha(ajax_response, divId);
					break;
				case 'showLodgingSkiMileage':
					showLodgingSkiMileage(ajax_response);
					break;
				case 'requestResult':
					requestResult(ajax_response);
					break;
				case 'doNothing':
					doNothing(ajax_response);
					break;
				default:
					doNothing(ajax_response);
			}		
	    }
	  }
	if(requestType == 'POST'){
		// Post Request
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlHttp.setRequestHeader("Content-length", param.length);
	    xmlHttp.setRequestHeader("Connection", "close");
	    xmlHttp.send(param);
	} else {
		// Get Request
		var geturl = url + '?' + param;
		xmlHttp.open("GET", geturl, true);
		xmlHttp.send(null);
	}
 }

 function doNothing(ajax_response){
 	// empty function
	 //alert(ajax_response);
 }
 
 function replaceDivContent(resp, divId){
 	if(resp != ''){
 		document.getElementById(divId).innerHTML = resp;
	}
 	document.getElementById(divId).style.display = 'block';
 }
 
 function showPhoneNumber(thing_id, divId){
 	var url;
	var param = 'thing_id=' + thing_id + '&click_type=phone';
	url = '/utah_logs/thing_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//document.write(url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function showTollFreeNumber(thing_id, divId){
 	var url;
	var param = 'thing_id=' + thing_id + '&click_type=tollphone';
	url = '/utah_logs/thing_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//document.write(url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function displayVideoListingPhoto(place_id, activity_id, video_thing, divId){
 	var url;
	var param = 'place_id=' + place_id + '&activity_id=' + activity_id + '&video_thing=' + escape(video_thing);
	url = '/video/getVideoListingPhoto.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function displayVideoListing(video_id){
 	var content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="406" height="600" id="video_interface" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="flashVars" value="videoId=' + video_id + '" /><param name="movie" value="video_interface_embedded2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="video_interface_embedded2.swf" flashVars="videoId=' + video_id + '" quality="high" bgcolor="#000000" width="406" height="600" name="video_interface" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	replaceDivContent(content, 'photoDiv')
 }
 
 function displayVideoListing2(video_id){
 	var content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="406" height="600" id="video_interface" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="flashVars" value="videoId=' + video_id + '" /><param name="movie" value="/video/video_interface_embedded2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="/video/video_interface_embedded2.swf" flashVars="videoId=' + video_id + '" quality="high" bgcolor="#000000" width="406" height="600" name="video_interface" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	replaceDivContent(content, 'video-shell')
 }
 
 function logVideoImpression(video_id, ad_video_id){
  	var url;
	var param = 'video_id=' + video_id + '&ad_video_id=' + ad_video_id + '&imp=1';
	url = '/video/logVideoImpression.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, 'doNothing', '');
 }
 
 function logVideoClick(thing_id, page_id){
  	var url;
	var param = 'thing_id='+thing_id+'&page_id='+page_id;
	url = '/utah_logs/video_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, '', '');
	//window.status = 'Log: thing=' + thing_id + ' page=' +page_id;
	//return true;
 }
 
function logVideoPlay(thing_id, page_id, banner_id){
	var url;
	var param = 'thing_id='+thing_id+'&page_id='+page_id+'&banner_id='+banner_id;
	url = '/utah_logs/video_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	ajax('POST', url, param, '', '');
	//alert('Log: video=' + tid + ' banner=' + bid + ' page=' + pid);
	//return true;
}
 
 function processRequest(frm){
 	var url;
	var param = 'id='+frm.id.value+'&fullName='+frm.fullName.value+'&address='+frm.address.value+'&city='+frm.city.value+'&state='+frm.state.value+'&zip='+frm.zip.value+'&phone='+frm.phone.value+'&email='+frm.email.value+'&preferredContact='+frm.preferredContact.value+'&request='+frm.request.value
	url = '/database/processProfileRequest.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert(url + '?' + param);
	ajax('POST', url, param, 'requestResult', '');
	//window.status = 'Log: thing=' + thing_id + ' page=' +page_id;
	//return true;
 }
 
 function requestResult(resp){
 	result = resp.split('|');
 	if(result[0] == 'T'){
 		document.getElementById('frm-msg').innerHTML = '';
 		document.getElementById('request-frm').innerHTML = result[1];
 	} else {
 		document.getElementById('frm-msg').innerHTML = result[1];
 	}
 }
 
 function findPos(obj) {	
	var curleft = curtop = 0;	
	if (obj.offsetParent) {		
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {			
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;		
		}	
	}	
return [curleft,curtop];
}

function getLodgingSkiMileage(thing_id){
 	var url;
	var param = 'thing_id=' + thing_id;
	url = '/database/lodging/getlodgingresortdistance.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//document.write(url + '?' + param);
	ajax('POST', url, param, 'showLodgingSkiMileage', '');
}

function showLodgingSkiMileage(resp){
	var data = resp.split('|');
	document.getElementById("ski-mileage").innerHTML = data[1];
	linkid = "ski-mileage-" + data[0];
	placement = findPos(document.getElementById(linkid));
	var left = placement[0] - 115;
	var top = placement[1] - 78;
	document.getElementById("ski-mileage").style.left=left+"px";
	document.getElementById("ski-mileage").style.top=top+"px";
	document.getElementById("ski-mileage").style.display="block";
}

function hideSkiMileage(){
	setTimeout("hideSkiMileageDiv()", 500);
}

function hideSkiMileageDiv(){
	document.getElementById("ski-mileage").style.display="none";
}

var dTimer = new Array();
dTimer[1] = null;
dTimer[2] = null;
dTimer[3] = null;
dTimer[4] = null;
dTimer[5] = null;
dTimer[6] = null;
dTimer[7] = null;
dTimer[8] = null;
dTimer[9] = null;
dTimer[10] = null;
dTimer[11] = null;
dTimer[12] = null;
dTimer[13] = null;
dTimer[14] = null;
dTimer[15] = null;
dTimer[16] = null;
dTimer[17] = null;
dTimer[18] = null;
dTimer[19] = null;
dTimer[20] = null;
dTimer[21] = null;
dTimer[22] = null;

function showDestinationLabel(label, pos){
	if(dTimer[pos] != null){
		clearTimeout(dTimer[pos]);
	}
	eName = "dest-label" + pos;
	linkid = "nav" + pos;
 	document.getElementById(eName).innerHTML = label;
	placement = findPos(document.getElementById(linkid));
	var left = placement[0] - 110;
	var top = placement[1] - 45;
	document.getElementById(eName).style.left=left+"px";
	document.getElementById(eName).style.top=top+"px";
	toggle2(eName);
}

function hideDestLabel(pos){
	var delay = function() { hideDestLabelDiv(pos); };
	dTimer[pos] = setTimeout(delay, 200);
}

function hideDestLabelDiv(pos){
	eName = "dest-label" + pos;
	togid = getSty(eName, window.document);
	togid.display='none';
	dTimer[pos] = null;
}

function toggle2(id){
	togid = getSty(id, window.document);
	if(togid.display=='block'){
		togid.display='none';
	} else {
		togid.display='block';
	}
}

// Venue autofill
 
 var t = 0;
 var t1 = 0;
 
 function resetHideTimer(){
 	clearTimeout(t1);
	t1 = setTimeout("hideVenueSelect()", 3000);
 }
 
 function handleKeyup(value){
	clearTimeout(t);
	clearTimeout(t1);
	t = setTimeout("getVenueSelect('" + escape(value) + "')", 100);
	//alert(value + ':' + searchType);
 }
 
 function getVenueSelect(value){
 	var url;
	var param = 'search=' + value;
	url = '/database/getEventVenueList.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert(param);
 	ajax('POST', url, param, 'replaceVenueSelect');
 }
 
 function replaceVenueSelect(resp){
 	//alert(resp);
	//document.writeln(resp);
	if(resp){
 		document.getElementById("select").style.display = "block";
 		document.getElementById("select").innerHTML = resp;
		resetHideTimer();
	} else {
		document.getElementById("select").style.display = "none";
 		document.getElementById("select").innerHTML = resp;
 		clearTimeout(t1);
 		hideVenueSelect();
	}
 }
 
 function hideVenueSelect(){
 	document.getElementById("select").style.display = "none";
 }
 
 function highlight(divobj, highlight){
 	if(highlight){
 		divobj.style.background = "#ff8000";
	} else {
		divobj.style.background = "#fff";
	}
 }
 
function checkEnter(e){ 
var characterCode
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true 
	} else{
		return false 
	}
}

function arrowNavigation(id, e){
	window.status=id;
	var characterCode
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	switch(characterCode){
		case 38: // Up
			resetHideTimer();
			c = document.getElementById(id);
			cc = c.parentNode.childNodes;
			for(i = 0; i < cc.length; i++){
				if(cc[i].id == id){
					prevNode = cc[i].previousSibling;
					if(prevNode != null){
						focusId = 'a' + prevNode.id;
						document.getElementById(focusId).focus();
					} else {
						document.getElementById("suggest").focus();
					}
					return true;
				}
			}
			break;
		case 40: // Down
			resetHideTimer();
			if(id == 'suggest'){
				firstDivId = document.getElementById("select").firstChild.id;
				focusId = 'a' + firstDivId;
				document.getElementById(focusId).focus();
			} else {
				c = document.getElementById(id);
				cc = c.parentNode.childNodes;
				for(i = 0; i < cc.length; i++){
					if(cc[i].id == id){
						nextNode = cc[i].nextSibling;
						if(nextNode != null){
							focusId = 'a' + nextNode.id;
							document.getElementById(focusId).focus();
							return true;
						}
					}
				}
			}
			break;
		default:
	}
	return true;
}

function fillEventForm(v){
	var values = v.split(' / ');
	document.eventinput['event_venue'].value = values[0];
	document.eventinput['event_address'].value = values[1];
	s = document.eventinput['city_id'];
	
	for ( var i = 1; i < s.options.length; i++ ) {    
		if ( s.options[i].firstChild.nodeValue == values[2] ) {         
			s.options[i].selected = true;            
			break;        
		}    
	}
	return false;
}

