var poiiframe;
var agt=navigator.userAgent.toLowerCase(); 
var ie = (agt.indexOf("msie") != -1);
var ie6 = (ie && (agt.indexOf("msie 6.") > 0));
var timer;
var myPointsBox;
var myTripsProgress;
var ajax_req;
var airportfield;
var streetfield;
var cityfield;
var zipcodefield;
var statecombo;
var countrycombo;
var addresspopup;
aSuggestions=[];
var userID;
var brandingID;
function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function airportoverlay(curobj,airportfieldId){
	airportfield=document.getElementById(airportfieldId);
	addresspopup=false;
	if(!overlay(curobj, 'poipopup', 'leftbottom'))
	{}
}

function addressoverlay(curobj,streetfieldId, cityfieldId, statecomboId, zipcodefieldId,countrycomboId){
	streetfield=document.getElementById(streetfieldId);
	cityfield=document.getElementById(cityfieldId);
	statecombo=document.getElementById(statecomboId);
	zipcodefield=document.getElementById(zipcodefieldId);
	countrycombo=document.getElementById(countrycomboId);
	addresspopup=true;
	if(!overlay(curobj, 'poipopup', 'leftbottom'))
	{}
}

function overlay(curobj, subobjstr, opt_position){
if (document.getElementById){
var subobj=document.getElementById(subobjstr)
subobj.style.display=(subobj.style.display!="block")? "block" : "none";
if(poiiframe!=null){poiiframe.style.display=(subobj.style.display=="block")? "block" : "none";}
var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
subobj.style.left=xpos+"px"
subobj.style.top=ypos+"px"
myPointsBox=subobj;
if (typeof ssdp_hideDatePicker == "function") ssdp_hideDatePicker();
if(subobj.getAttribute('isLoaded')!='true')
{
	var userIDfield=document.getElementById("userID");
	var brandingIDfield=document.getElementById("brandingID");
	if(userIDfield!=null)
	{userID=userIDfield.value;}
	if(brandingIDfield!=null)
	{brandingID=brandingIDfield.value;}
	loadMyPoints (brandingID,userID);
}else{
 	var createddiv=createList(aSuggestions);
	var popupcontent=document.getElementById('poipopupcontent');
	popupcontent.innerHTML =createddiv.innerHTML;
	myPointsBox.setAttribute('isLoaded','true');
}
return false
}
else
return true
}

function overlayclosePOI(subobj){
document.getElementById(subobj).style.display="none"
if(poiiframe!=null)
poiiframe.style.display='none';
}

/* check for ie/firefox to get proper XML HTTP object */
function getAjaxObject() {
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
		return new XMLHttpRequest();
}
/* initiate call to get list of itineraries via AJAX */
function loadMyPoints (brandingID,userID) {

	ajax_req = getAjaxObject();				
	ajax_req.open('GET', 'poi.jsp?brandingID='+brandingID+'&userID='+userID, true);
	ajax_req.onreadystatechange = onAjaxResponse;
	ajax_req.send(null);
}
/* called when AJAX gets a reply from server - here we want to check if we got a valid response
	-if valid response, use it as html to fill in our results div 
	-otherwise, means we are in holding, so set a timeout and wait before trying again */
function onAjaxResponse () {
	if (ajax_req.readyState == 4) {
    	if (trim(ajax_req.responseText) == '') {
    		timer = setTimeout("loadMyPoints()", 2000);
    	} else {			    
			var xml = ajax_req.responseXML;
			aSuggestions=[];
			if(xml.getElementsByTagName('corporate')[0]){
			var corporate = xml.getElementsByTagName('corporate')[0].childNodes;
			for (var i=0;i<corporate.length;i++)
			{
				//if (corporate[i].hasChildNodes())
				aSuggestions.push( { 'id':corporate[i].getAttribute('id'), 'locationname':corporate[i].getAttribute('locationname'),'address':corporate[i].getAttribute('address'),'city':corporate[i].getAttribute('city'),'state':corporate[i].getAttribute('state'),'country':corporate[i].getAttribute('country'),'zipcode':corporate[i].getAttribute('zipcode'),'airportcode':corporate[i].getAttribute('airportcode')} );
			}
			}
			if(xml.getElementsByTagName('personal')[0]){
			var personal = xml.getElementsByTagName('personal')[0].childNodes;
			for (var i=0;i<personal.length;i++)
			{
				aSuggestions.push( { 'id':personal[i].getAttribute('id'), 'locationname':personal[i].getAttribute('locationname'),'address':personal[i].getAttribute('address'),'city':personal[i].getAttribute('city'),'state':personal[i].getAttribute('state'),'country':personal[i].getAttribute('country'),'zipcode':personal[i].getAttribute('zipcode'),'airportcode':personal[i].getAttribute('airportcode')} );
			}
			}	
	        var createddiv=createList(aSuggestions);
			var popupcontent=document.getElementById('poipopupcontent');
			popupcontent.innerHTML =createddiv.innerHTML;
			myPointsBox.setAttribute('isLoaded','true');    
        }
    }
}
function trim( value ) {	
	return value.replace(/\s*((\S+\s*)*)/, '$1').replace(/((\s*\S+)*)\s*/, '$1');
}

function createList(arr)
{	
	// if no results, and shownoresults is false, do nothing
	//
	//if (arr.length == 0)
	//	return false;
	// create holding div
	//

	var div = document.createElement("div")
	// create and populate ul
	//
	var ul = document.createElement("ul")
	
	// loop throught arr of suggestions
	// creating an LI element for each suggestion
	//
	for (var i=0;i<arr.length;i++)
	{
		// format output with the input enclosed in a EM element
		// (as HTML, not DOM)
		//
		//var val = arr[i].value;
		var val = arr[i].locationname;
		var a = cE("a",{},val);
		var airportcode = arr[i].airportcode;
		if(addresspopup==true){
		 a.setAttribute('onclick','setAddress(\"'+arr[i].address+'\",\"'+arr[i].city+'\",\"'+arr[i].state+'\",\"'+arr[i].zipcode+'\",\"'+arr[i].country+'\")');
		}else
		{
		 a.setAttribute('onclick','setAirportcode(\"'+airportcode+'\")');
		 }		
		var li 			= cE(  "li", {}, a  );		
		ul.appendChild( li );
	}
	
	// no results
	//
	if (arr.length == 0 )
	{	var li 			= cE(  "li", {className:"as_warning"}, "No points exist"  );
		ul.appendChild( li );}	
	div.appendChild( ul );
	if(ie6==true){
	poiiframe=document.getElementById("poipopupiframe");
	if(poiiframe==null){
	poiiframe=document.createElement("iframe");
	poiiframe.setAttribute('id','poipopupiframe');
	poiiframe.setAttribute('src','Javascript:false');
	poiiframe.setAttribute('scrolling','no');
    document.getElementsByTagName("body")[0].appendChild(poiiframe);
	}
	poiiframe.style.left=myPointsBox.style.left;
	poiiframe.style.position="absolute";
	poiiframe.style.top=myPointsBox.style.top;
	poiiframe.style.width=getStyle(myPointsBox, 'width');
	poiiframe.style.height=getStyle(myPointsBox, 'height');
	poiiframe.style.zIndex=79;
	myPointsBox.style.zIndex=80;
	poiiframe.style.border=0;
}	
	return div;	
}


/* create element */
cE = function ( type, attr, cont, html )
{
	var ne = document.createElement( type );
	if (!ne)
		return false;
	for (var a in attr)
		ne[a] = attr[a];		
	if (typeof(cont) == "string" && !html)
		ne.appendChild( document.createTextNode(cont) );
	else if (typeof(cont) == "string" && html)
		ne.innerHTML = cont;
	else if (typeof(cont) == "object")
		ne.appendChild( cont );
	return ne;
}

/* get element */
gE = function ( e )
{
	if (typeof(e) == "undefined")
		return false;
	else if (typeof(e) == "string")
	{
		var re = document.getElementById( e );
		if (!re)
			return false;
		else if (typeof(re.appendChild) != "undefined" ) {
			return re;
		} else {
			return false;
		}
	}
	else if (typeof(e.appendChild) != "undefined")
		return e;
	else
		return false;
}

/* remove element */
remE = function ( ele )
{
	var e = this.gE(ele);
	
	if (!e)
		return false;
	else if (e.parentNode.removeChild(e))
		return true;
	else
		return false;
}

/* get position */
getPos = function ( e )
{
	var e = this.gE(e);
	var obj = e;
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{curleft += obj.offsetLeft;
		 obj = obj.offsetParent;}
	}
	else if (obj.x)
		curleft += obj.x;
	var obj = e;
	var curtop = 0;
	if (obj.offsetParent)
	{ while (obj.offsetParent)
		{	curtop += obj.offsetTop;
			obj = obj.offsetParent;}
	}
	else if (obj.y)
		curtop += obj.y;
	return {x:curleft, y:curtop};
}

function setAirportcode(airportcode){
if(airportfield!=null)
airportfield.value=airportcode; 
if(airportfield.onchange!=null)
{airportfield.onchange();}
myPointsBox.style.display="none";
if(poiiframe!=null)
poiiframe.style.display='none';
}

function setAddress(street,city,state,zipcode,country){
if(streetfield!=null)
{streetfield.value=street; 
if(streetfield.onchange!=null)
streetfield.onchange();
} 
if(cityfield!=null)
{cityfield.value=city;
if(cityfield.onchange!=null)
cityfield.onchange();
}
if(zipcodefield!=null)
{zipcodefield.value=zipcode;
if(zipcodefield.onchange!=null)
zipcodefield.onchange();
}
if(statecombo!=null)
{	for (var i=0;i<statecombo.options.length;i++) {
    	 if (statecombo.options[i].value == state)
         {statecombo.options[i].selected = true;
         if(statecombo.onchange!=null)
         statecombo.onchange();
          break;}
		}
}
if(countrycombo!=null)
{for (var i=0;i<countrycombo.options.length;i++) {
    	 if (countrycombo.options[i].value == country)
         {countrycombo.options[i].selected = true;
         if(countrycombo.onchange!=null)
         countrycombo.onchange();
          break;}
		}
}
myPointsBox.style.display="none";
if(poiiframe!=null)
poiiframe.style.display='none';
}

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}