
function id(name) {
    return document.getElementById(name);
}


/* hover popups data */
var gHoverPopup = new Object();
gHoverPopup.status = 0; // 0=not in use, 1=open
gHoverPopup.el = null;

/* mouse over element */
var gMouseOverEl = '';

/* detection of the browser  */
var IE = document.all?true:false

/* Temporary variables to hold mouse x-y pos.s  */
var tempX = 0;
var tempY = 0;

var dragTempX=0;
var dragTempY=0;
/* Added variables related to drag n drop div */
var dragapproved=false
var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;
var tempDivId='';

/* Popup objects	*/
var overPopup = false;
var overPopup2 = false;
var calendar = new Object();
var locator = new Object();
var infopopup = new Object(); 

/* If NS -- that is, !IE -- then set up for mouse capture */
//if (!IE) document.captureEvents(Event.MOUSEMOVE)

/* Set-up to use getMouseXY function onMouseMove */
document.onmouseover = getMouseXY;


    /****
	Function:	This function makes selected div in draggable state.
	Input   :  	event 
	Returns :  	none
	****/
function drag_drop(e){
if ((ie5||IE)&&dragapproved&&(event.button==1)){
document.getElementById(tempDivId).style.left=dragTempX+event.clientX-offsetx+"px"
document.getElementById(tempDivId).style.top=dragTempY+event.clientY-offsety+"px"
}
else if (ns6&&dragapproved){
document.getElementById(tempDivId).style.left=dragTempX+e.clientX-offsetx+"px"
document.getElementById(tempDivId).style.top=dragTempY+e.clientY-offsety+"px"
}
}

    /****
	Function:	This function intialize the drag event and call the drag_drop() function 
	Input   :  	event , Division id
	Returns :  	none
	****/
function initializedrag(e, divId){
tempDivId = divId;
offsetx=ie5? event.clientX : e.clientX;
offsety=ie5? event.clientY : e.clientY;
window.status=document.getElementById(divId);
dragTempX=parseInt(document.getElementById(divId).style.left);
dragTempY=parseInt(document.getElementById(divId).style.top);
dragapproved=true;
document.getElementById(divId).onmousemove=drag_drop;
document.getElementById(divId).onmouseout=getmouseout;
}
	function getmouseout()
	{
		if (tempDivId!='')
		{
			if (document.getElementById(tempDivId)!=null) {
				stopdrag(tempDivId)
				document.getElementById(tempDivId).onmouseout=null;							
			}
		}

	}

    /****
	Function:	This methods stops the dragging of selected div.
	Input   :  	Division id
	Returns :  	none
	****/
function stopdrag(divId){
dragapproved=false;
document.getElementById(divId).onmousemove=null;
}

   /****
	Function:	This function get the id name on mouse over.
	Input   :  	none
	Returns :  	none
	****/
function mouseOverCtrl(el)
{
	if (el && el.id)
		gMouseOverEl = el.id;
}

    /****
	Function:	This function get the id name on mouse out.
	Input   :  	none
	Returns :  	none
	****/
function mouseOutCtrl(el)
{
	gMouseOverEl = '';
}

    /****
	Function:	This function calls gMouseOverEl.
	Input   :  	none
	Returns :  	none
	****/
function getMouseOverCtrl(){return gMouseOverEl;}


    /****
	Function:	This funtion creates the calendar with default values.
	Input   :  	none
	Returns :  	none
	****/
	function createCalendar()
{
	calendar.contents = initCalendar("departDate", "arrivalDate", "en");
}

    /****
	Function:	This funtion creates the new div for destination
	Input   :  	none
	Returns :  	none
	****/
function createLocator()
{
	locator.contents = document.createElement("DIV");
	locator.contents.id = "Airtradelocator";
}


   /****
	Function:	This funtion takes values from calendar like year and month.
	Input   :  	none
	Returns :  	none
	****/
function popup(input, option, halign, valign, popFrame)
{	
	configCalendar();
	var popupDiv = document.getElementById("dapopup"); //data popup
	if(dtfield == 'departDate')
	{
		if(input.id=='year' || input.id=='month')
		{
		var input=document.getElementById("departDate")
		}
	}
	
	if(popupDiv.firstChild)
		popupDiv.removeChild(popupDiv.firstChild);
		
	//now populate according to option
	if (option == "calendar")
	{
		input.select();
		popupDiv.appendChild(calendar.contents);
		currentDateField = input;
		displayDates();	
	}
	else if(option == "locator")
	{ 
		popupDiv.appendChild(locator.contents);
		currentLocator = input;		
	}
	else if (option == "infopopup")
	{
		popupDiv.appendChild(infopopup.contents);
	}
		
	popupDiv.halign = halign?halign:0;
	popupDiv.valign = valign?valign:0;
	popupDiv.fixed = (option=="locator"?true:false);
	popupDiv.popFrame = popFrame;
	popupDiv.calledby = input;	

	if (popupDiv.className.indexOf(option) == -1)
	{
		popupDiv.className = "popup "+option;
	}
	positionpopup(popupDiv);
	setPopupCover();
	
 }




function popup2(input, option, halign, valign, popFrame)
{	
	configCalendar();
	var popupDiv = document.getElementById("dapopup2"); //data popup
	//(option == "infopopup")
		popupDiv.appendChild(infopopup.contents);
		
	popupDiv.halign = halign?halign:0;
	popupDiv.valign = valign?valign:0;
	popupDiv.fixed = (option=="locator"?true:false);
	popupDiv.popFrame = popFrame;
	popupDiv.calledby = input;	

	if (popupDiv.className.indexOf(option) == -1)
	{
		popupDiv.className = "popup "+option;
	}
	positionpopup(popupDiv);
	setPopupCover();
	
 }




   /****
	Function:	This funtion removes the date modification from FindFlights once date is modified in all the pages.
	Input   :  	none
	Returns :  	none
	****/
 function popupff(input, option, halign, valign, popFrame)
{	
	//configCalendar();
	var popupDiv = document.getElementById("dapopup"); //data popup
	/*if(dtfield == 'departDate')
	{
		if(input.id=='year' || input.id=='month')
		{
		var input=document.getElementById("departDate")
		}
	}*/
	
	if(popupDiv.firstChild)
		popupDiv.removeChild(popupDiv.firstChild);
		
	//now populate according to option
	if (option == "calendar")
	{
		input.select();
		popupDiv.appendChild(calendar.contents);
		currentDateField = input;
		displayDates();	
	}
	else if(option == "locator")
	{ 
		popupDiv.appendChild(locator.contents);
		currentLocator = input;		
	}
	else if (option == "infopopup")
	{
		popupDiv.appendChild(infopopup.contents);
	}
		
	popupDiv.halign = halign?halign:0;
	popupDiv.valign = valign?valign:0;
	popupDiv.fixed = (option=="locator"?true:false);
	popupDiv.popFrame = popFrame;
	popupDiv.calledby = input;	

	if (popupDiv.className.indexOf(option) == -1)
	{
		popupDiv.className = "popup "+option;
	}
	positionpopup(popupDiv);
	setPopupCover();
	
}

   /****
	Function:	This funtion removes the calendar from the page.
	Input   :  	none
	Returns :  	none
	****/
function cancelHoverPopup()
{
	if (gHoverPopup.status == 1)
	{
		// !!! remove
		assert( (gHoverPopup.el == null), "cancelHoverPopup err2" );

		popdown(true);

		gHoverPopup.status = 0;
		gHoverPopup.type = null;
		gHoverPopup.el = null;
		gHoverPopup.timeout = null;
	}
}

   /****
	Function:	This funtion removes the calendar from the page on certain time out.
	Input   :  	none
	Returns :  	none
	****/
function cancelHoverTimeout()
{
	// cancel any pending timeout
	if (gHoverPopup.timeout)
	{
		window.clearTimeout(gHoverPopup.timeout);
		gHoverPopup.timeout = null;
	}
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function dopopover(el)
{
	cancelHoverTimeout();
	cancelHoverPopup();
	gHoverPopup.el = el;	
	gHoverPopup.timeout = window.setTimeout(doHoverPopup, 500);
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function dopopout()
{
	cancelHoverTimeout();
	if (gHoverPopup.status == 1)
		gHoverPopup.timeout = window.setTimeout(cancelHoverPopup, 600);
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function mouseOverPopupLayer(ev)
{
	if (checkMouseEnter(this, ev) == false)
		return;
	assert( (gHoverPopup.status != 1), "mouseOverPopupLayer err1" );
	assert( (gHoverPopup.timeout == null), "mouseOverPopupLayer err2" );
	cancelHoverTimeout();
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function mouseOutPopupLayer(ev)
{
	if (checkMouseLeave(this, ev) == false)
		return;
	cancelHoverPopup();
}

/****
	Function:	used for enhanced feature of fare search
	Input   :  	none
	Returns :  	none
	****/
function popdown2(force)
{  
	if(force || !overPopup)
	{   
		if(!closepop2){
			document.getElementById("dapopup2").style.display="none";
			document.getElementById("dapopupCover2").style.display="none";
		}
		var popupDiv = document.getElementById("dapopup");	
		popupDiv.className = "popup";
		popupDiv.style.height = "";
		if(popupDiv.firstChild)
			popupDiv.removeChild(popupDiv.firstChild);
		popupDiv.calledby = null;
		currentLocator = null;
		currentDateField = null;
		
		var cov = document.getElementById("dapopupCover");	
		cov.style.visibility = "hidden";
		cov.style.display = "none"; 
	}
}


/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function popdown(force)
{
      if(force || !overPopup)
      {
            var popupDiv = document.getElementById("dapopup");    
            popupDiv.className = "popup";
            popupDiv.style.height = "";
            if(popupDiv.firstChild)
                  popupDiv.removeChild(popupDiv.firstChild);
            popupDiv.calledby = null;
            currentLocator = null;
            currentDateField = null;
      
            var cov = document.getElementById("dapopupCover");    
            cov.style.visibility = "hidden";
            cov.style.display = "none";
      }
}

 
/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function align(t,p,whc,whp)
{
	if(t)
	{
		if(t == 1)
			return p+whc;
		if(t == 2)
			return p-whp;
		if(t == 3)
			return p+whc-whp;
		if (t == 4)
			return Math.round(p+whc/2);
	}
	return p;
}

/*
*positionpopup() to list according to position of text
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function positionpopup(p)
{
	p = document.getElementById("dapopup");
	var popupDiv = p?p:document.getElementById("dapopup");
	popupDiv.style.visibility = "hidden";
	popupDiv.style.position = "absolute";	
	popupDiv.style.display = "block";
	if(popupDiv.calledby)
	{
		var cx = findPosX(popupDiv.calledby);
		var cy = findPosY(popupDiv.calledby);
		var x = align(popupDiv.halign,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
		var y = align(popupDiv.valign,cy,popupDiv.calledby.offsetHeight,popupDiv.offsetHeight);

		if(!popupDiv.fixed)
		{
			if (p.popFrame && p.popFrame != '' && document.getElementById(p.popFrame))
				var r = elementRect(document.getElementById(p.popFrame));
			else
				var r = clientRect();

			var c = contained({'l':x,'t':y,'r':x+popupDiv.offsetWidth,'b':y+popupDiv.offsetHeight},r);
			
			if(c.axis == 'y') //x okay
			{
				//flip or nudge
				if(popupDiv.halign == 0 || popupDiv.halign == 3)//flip up/down			
					y = align(c.diff>0?1:2,cy,popupDiv.calledby.offsetHeight, popupDiv.offsetHeight);
				else y += c.diff			
			}
			else if(c.axis == 'x') //y okay
			{
				//flip or nudge
				if(popupDiv.valign == 0 || popupDiv.valign == 3)//flip to side
					x = align(c.diff>0?1:2,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
				else x += c.diff;
			}	
		}		
		popupDiv.style.left = x + 10 + "px";
		popupDiv.style.top = y + "px";
	}

	popupDiv.style.visibility = "visible";
}

function positionpopup2()
{
	p = document.getElementById("dapopup2");
	var popupDiv=p;
	popupDiv.style.visibility = "hidden";
	popupDiv.style.position = "absolute";	
	popupDiv.style.display = "block";
	if(popupDiv.calledby)
	{
		var cx = findPosX(popupDiv.calledby);
		var cy = findPosY(popupDiv.calledby);
		var x = align(popupDiv.halign,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
		var y = align(popupDiv.valign,cy,popupDiv.calledby.offsetHeight,popupDiv.offsetHeight);

		if(!popupDiv.fixed)
		{
			if (p.popFrame && p.popFrame != '' && document.getElementById(p.popFrame))
				var r = elementRect(document.getElementById(p.popFrame));
			else
				var r = clientRect();

			var c = contained({'l':x,'t':y,'r':x+popupDiv.offsetWidth,'b':y+popupDiv.offsetHeight},r);
			
			if(c.axis == 'y') //x okay
			{
				//flip or nudge
				if(popupDiv.halign == 0 || popupDiv.halign == 3)//flip up/down			
					y = align(c.diff>0?1:2,cy,popupDiv.calledby.offsetHeight, popupDiv.offsetHeight);
				else y += c.diff			
			}
			else if(c.axis == 'x') //y okay
			{
				//flip or nudge
				if(popupDiv.valign == 0 || popupDiv.valign == 3)//flip to side
					x = align(c.diff>0?1:2,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
				else x += c.diff;
			}	
		}		
		popupDiv.style.left = x + 10 + "px";
		popupDiv.style.top = y + "px";
	}

	popupDiv.style.visibility = "visible";
	popupDiv.style.display="";
}
/*
* 	horzPosPopup() to adjust the horizantal position
* 	used by adjustLocatorHeight() in AutoList.js
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function horzPosPopup()
{
	var popupDiv = document.getElementById("dapopup");
	var cx = findPosX(popupDiv.calledby);
	var x = align(popupDiv.halign,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);

		if(!popupDiv.fixed)
		{
			var r = clientRect();
			var y = 0;
			var c = contained({'l':x,'t':y,'r':x+popupDiv.offsetWidth,'b':y+popupDiv.offsetHeight},r);
			
			if(c.axis == 'x') 
			{
				//flip or nudge
				if(popupDiv.valign == 0 || popupDiv.valign == 3)//flip to side
					x = align(c.diff>0?1:2,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
				else x += c.diff;
			}	
		}		
		popupDiv.style.left = x + "px";
}

/*
*	used by adjustLocatorHeight() in AutoList.js
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function setPopupCover()
{
	if(navigator.appName != "Microsoft Internet Explorer")
		return;
	var popupDiv = document.getElementById("dapopup");
	var cov = document.getElementById("dapopupcover");
	cov.style.display = "block";
	cov.style.left = popupDiv.style.left;
	cov.style.top = popupDiv.style.top;
	cov.style.width = popupDiv.offsetWidth;
	cov.style.height = popupDiv.offsetHeight;
	cov.style.visibility = "visible";
}

/****
	Function:	to Cover dapopup2 in IE.
	Input   :  	none
	Returns :  	none
	****/
function setPopupCover2()
{
	if(navigator.appName != "Microsoft Internet Explorer")
		return;
	var popupDiv = document.getElementById("dapopup2");
	var cov = document.getElementById("dapopupcover2");
	cov.style.display = "block";
	cov.style.left = popupDiv.style.left;
	cov.style.top = popupDiv.style.top;
	cov.style.width = popupDiv.offsetWidth;
	cov.style.height = popupDiv.offsetHeight;
	cov.style.visibility = "visible";
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function overpopup()
{	
	overPopup = true;
	
}

function overpopup2()
{	
	overPopup = true;
	if(pop2 && onpop2) {setTimeout('closePopup2();',250); onpop2=false;}
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function leavepopup()
{	
	overPopup = false;
//	closepop2=false;
}
function leavepopup2()
{	
	overPopup2 = false;
}
/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function clickpopup()
{
	var popupDiv = document.getElementById("dapopup");
}
function clickpopup2()
{
	var popupDiv = document.getElementById("dapopup2");
}


/*
* findPosX()to find position x of text box
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

/*
* findPosY()to find position x of text box
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

/*
* elementRect()to find position of list
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function elementRect(el)
{
	var l = findPosX(el);
	var t = findPosY(el);
	
	return {'l':l,'t':t,'r':l+el.offsetWidth,'b':t+el.offsetHeight};
}

/*
* clientRect()to find position related to top window
*/

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function clientRect()
{
	var d = getInnerDimension();
	return {'l':d.l,'t':d.t,'r':d.l+d.w,'b':d.t+d.h};
}


//1: x and y contained, 2,3:x contained, 4,5:y contained, 0:x and y outside
/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function contained(p,r)
{
	var axis = '';
	var diff = 0;
	if(p.l>=r.l && p.r<=r.r)
	{
		if(p.t<r.t || p.b>r.b)
		{
			axis='y';
			if(p.t<r.t) diff=r.t-p.t;
			else diff=r.b-p.b;			
		}
	}
	else if(p.t>=r.t && p.b<=r.b)
	{
		axis = 'x';
		if(p.l<r.l) diff=r.l-p.l;
		else diff=r.r-p.r;
	}
	return {'axis':axis,'diff':diff};
}


/*
*getInnerDimension() to find positons window
*/
/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function getInnerDimension()
{
	var d = {'w':0, 'h':0, 'l':0, 't':0};
	if (window.innerWidth)
	{
		d.w = window.innerWidth;
		d.h = window.innerHeight;
		d.l = window.scrollX;
		d.t = window.scrollY;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		d.w = document.documentElement.clientWidth;
		d.h = document.documentElement.clientHeight;
		d.l = document.documentElement.scrollLeft;
		d.t = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		d.w = document.body.clientWidth;
		d.h = document.body.clientHeight;
		d.l = document.body.scrollLeft;
		d.t = document.body.scrollTop;
	}
	return d;
}

/*
* events for list
*/
/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function stopBubble(ev) {
	ev = (ev) ? ev : (window.event) ? event : null;
	if (ev)
	{
		ev.cancelBubble = true;
		if (ev.stopPropagation) ev.stopPropagation();
	}
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function getEvTarget(ee)
{
	var targ;
	var e = ee;
	if (!e) e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)
		targ = targ.parentNode;
	return targ;
}


/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function checkMouseLeave (element, evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

/****
	Function:	.
	Input   :  	none
	Returns :  	none
	****/
function checkMouseEnter (element, evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

/** function for holiding the X, Y coordinate of the mouse on page.
 *      Method getMouseXY()
 *  @param : e , event object to window.
**/
/****
        Function:       This funtion removes the calendar from the page on certain time out.
        Input   :       none
        Returns :       none
        ****/
function getMouseXY(e) {

  if (IE) { // grab the x-y pos.s if browser is IE

    tempX = event.x//+ document.body.scrollLeft
    tempY = event.y //+ document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
}


/****
Function:	This funtion removes the calendar from the page on certain time out.
Input   :  	none
Returns :  	none
Desc:       Method to check and trim the decimal price values.
			it returns the decimal point based on the decimal point parameter	
****/
function Checkdecimalvalue( decval , decpoint )
{
	var dvalue = Math.pow(10,decpoint);
	var decval=Math.round(decval*dvalue)/dvalue
	decval = decval + '';
	decval = decval.replace(/^\s*|\s*$/g,"");
	if(decval != null)
	{
		if(decval.indexOf(".") != -1)
		{
			var beforeDec = decval.substr(0,decval.indexOf("."));
			var afterDec  = decval.substr(decval.indexOf(".")+1,decval.length);
			if(afterDec.length < decpoint) 
			{
				for(var i=0; i <= (decpoint- afterDec.length);i++)
					afterDec = afterDec + 0;
			}	
			if(afterDec.length > decpoint) 
			{
				afterDec = afterDec.substr(0,2);
			}	
			decval = 	beforeDec +"." + afterDec;
		}
		else
		{
			for(var i=0; i < (decpoint);i++)
			{
				if(i==0)decval = decval + ".";
				decval = decval + 0;
			}	
		}
	}
	return decval;
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s*/, '').replace(/\s*$/,'');
}


function openExitPopup( )
{
	var wnd = window.open( contextPath + "/jsp/home/exitPopupSS.jsp", "exitPopup", "toolbar=no,status=no,scrollbars=no,resizable=yes,width=500,height=405,left=270,top=120" );
	
	if(wnd) {
		wnd.blur();
		window.focus();
	}
	
	return false;
}
