

var cF=null;var cW=null;var g_tid=0;var g_cP,g_eD,g_eDP,g_dmin,g_dmax;
var nextFocus;var g_fNoCal=false;

if(document.documentElement && document.documentElement.offsetHeight)document.documentElement.onclick=CancelCal;
else document.onclick=CancelCal;

function getEventObj(e){if(!e)e=window.event;return e;}

function stopBubble(e){e=getEventObj(e);e.cancelBubble=true;if(e.stopPropagation)e.stopPropagation();}

function CB(){stopBubble(event);}

function SCal(cP,eD,eDP,dmin,dmax){
 clearTimeout(g_tid);
 if(g_fNoCal){g_fNoCal=false;return;}
	if(g_calShown && eD==g_eD)return;
	g_calShown = true;
	g_calCB = null;
 g_cP=cP;
 g_eD=eD;
 g_eDP=eDP;
 g_dmin=dmin;
 g_dmax=dmax;
 WaitCal();
}
function CancelCal(){clearTimeout(g_tid);if(!cF)cF=getObj('CalFrame');cF.style.visibility="hidden";g_calShown=false;}
function WaitCal()
{ 
 if(!cW)cW=frames['CalFrame'];

 if(null==cW||null==cW.g_fCL||false==cW.g_fCL){
	g_tid=setTimeout("WaitCal()", 200);
	}
 else{
 if(!cF)cF=getObj('CalFrame');
	cF.style.visibility="hidden";
	setTimeout("DoCal()",1);
	}
}
function DoCal(){PosCal(g_cP);
if(!cW)cW=frames['CalFrame'];
cW.DoCal(g_eD,g_eDP,g_dmin,g_dmax);}

function getScrollTop()
{
	if(document.documentElement.scrollTop) return document.documentElement.scrollTop;
	if(document.body.scrollTop) return document.body.scrollTop;
	if(window.pageYOffset) return window.pageYOffset;
	return 0;
}

function getWinHeight()
{
	if(window.innerHeight) return window.innerHeight;
	if(document.documentElement.clientHeight) return document.documentElement.clientHeight;
	if(document.body.clientHeight) return document.body.clientHeight;
	return 0;
}

function PosCal(cP)
{
	var dB=document.body;var eL=0;var eT=0;
 if(!cF)cF=getObj('CalFrame');
	for(var p=cP;p&&p.tagName!='BODY';p=p.offsetParent){eL+=p.offsetLeft;eT+=p.offsetTop;}
	var eH=cP.offsetHeight;var dH=parseInt(cF.style.height);var sT=getScrollTop();
	if(eT-dH>=sT&&eT+eH+dH>getWinHeight()+sT)eT-=dH;else eT+=eH;
	cF.style.left=eL+'px' ;cF.style.top=eT+'px';
}

function SetNextFocus(e){nextFocus=e;if(nextFocus)nextFocus.onfocus=CancelCal;}
function SetPrevFocus(e){if(e)e.onfocus=CancelCal;}

function FGoNextFocus(){if(nextFocus){nextFocus.focus();return true;}return false;}

function CalSetFocus(e){if(e){g_fNoCal=true;e.focus();setTimeout("EndCalFocus()", 200);}}
function EndCalFocus(){g_fNoCal=false;}

function CalDateSet(eInp,d,m,y,giveFocus)
{
	var ds=GetDateSep();
	var fmt=GetDateFmt();

	if (m < 10) m = '0' + m;
	if (d < 10) d = '0' + d;
	
	if(fmt=="mmddyy")eInp.value=m+ds+d+ds+y;
	else if(fmt=="ddmmyy")eInp.value=d+ds+m+ds+y;
	else eInp.value=y+ds+m+ds+d;
	if(!giveFocus)
	CalSetFocus(eInp);
}

var g_calShown = false;
function SetCalShown(fcshown){g_calShown=fcshown;}

var g_calCB;
function CalendarCallback(){if(g_calCB)g_calCB();}
function SetCalendarCallback(cb){g_calCB=cb;}


function GetInputDate(t)
{
	if(!t.length) return null;
	t=t.replace(/\s+/g,"");
	if(t.match(/[^-|\d|\.|\/]/)) return null;
	var rgt=t.split(/-|\.|\//);
	for(var i=0;i<rgt.length;i++) rgt[i]=parseInt(rgt[i],10);
	if(!rgt[1]) return null;
	var m,d,y;
	var fmt=GetDateFmt();
	if(fmt=="yymmdd")
	{
	if(!rgt[2]) return null;
	m=rgt[1];d=rgt[2];y=rgt[0];
	}
	else
	{
	if(fmt=="mmddyy"){m=rgt[0];d=rgt[1];}
	else{m=rgt[1];d=rgt[0];}//fmt=="ddmmyy"
	if(rgt[2])y=rgt[2];
	else y=DefYr(m-1,d);
	}
	m-=1;if(y<100)y+=2000;
	if(y<1601||y>4500||m<0||m>11||d<1||d>GetMonthCount(m,y))return null;
	return new Date(y,m,d);
}


function GetInputDate(t,fmt)
{
	if(!t.length) return null;
	t=t.replace(/\s+/g,"");
	if(t.match(/[^-|\d|\.|\/]/)) return null;
	var rgt=t.split(/-|\.|\//);
	for(var i=0;i<rgt.length;i++) rgt[i]=parseInt(rgt[i],10);
	if(!rgt[1]) return null;
	var m,d,y;
	if(fmt=="yymmdd")
	{
		if(!rgt[2]) return null;
		m=rgt[1];d=rgt[2];y=rgt[0];
	}
	else
	{
		if(fmt=="mmddyy"){m=rgt[0];d=rgt[1];}
		else{m=rgt[1];d=rgt[0];}//fmt=="ddmmyy"
		if(rgt[2])y=rgt[2];
		else y=DefYr(m-1,d);
	}
	m-=1;if(y<100)y+=2000;
	if(y<1601||y>4500||m<0||m>11||d<1||d>GetMonthCount(m,y))return null;
	return new Date(y,m,d);
}


var rM=new Array(12);rM[0]=rM[2]=rM[4]=rM[6]=rM[7]=rM[9]=rM[11]=31;rM[3]=rM[5]=rM[8]=rM[10]=30;rM[1]=28;
function GetMonthCount(m,y){var c=rM[m];if((1==m)&&IsLY(y))c++;return c;}
function IsLY(y){if(0==y%4&&((y%100!=0)||(y%400==0)))return true;else return false;}
function DefYr(m,d){var dt=new Date();var yC=(dt.getYear()<1000)?1900+dt.getYear():dt.getYear();if(m<dt.getMonth()||(m==dt.getMonth()&&d<dt.getDate()))yC++;return yC;}

var zz, zv, d, fTSR;
d = new Date();
fTSR=0;
zv = d.getTime();
zz = "&zz="+zv;

var gBF=false;
function GoTo(u){window.top.location = u + zz;}
function Go(u){window.top.location = u;} 

function BF(){gBF=true;}

function Foci(o){if(!gBF && IsVis(o)){o.focus();}}

function IsVis(o)
{
	if(!o || o.type=="hidden")
	return false;
	
	while(o && o.style && o.style.display!='none')
	{
		o = o.parentNode;       
	}
	return !o || !o.style;
}

function getObj(objID)
{
	if (document.getElementById) {return document.getElementById(objID);}
	else if (document.all) {return document.all[objID];}
	else if (document.layers) {return document.layers[objID];}
}
		
function objNext(f,d)
{
	var fFnd=false,el=f.elements,i=0;
	for(;i < el.length;i++)
	{
	if('hidden'!=el[i].type && false==el[i].disabled && IsVis(el[i]) && fFnd)return el[i];
	if((d.id&&d.id==el[i].id)||(d.name&&d.name==el[i].name))fFnd=true;
	}
	return null;
}

function objPrev(f,d)
{
	var fFnd=false,el=f.elements,i=el.length - 1;
	for(;i >= 0;i--)
	{
	if('hidden'!=el[i].type && false==el[i].disabled && IsVis(el[i]) && fFnd)return el[i];
	if((d.id&&d.id==el[i].id)||(d.name&&d.name==el[i].name))fFnd=true;
	}
	return null;
}

function addEvent( obj, szEventType, fnHandler, fUseCapture )
{
    if( obj.attachEvent != null )
    {
    obj.attachEvent( "on" + szEventType, fnHandler );
    return true;
    }
    else if( obj.addEventListener != null )
    {
    obj.addEventListener(szEventType, fnHandler, fUseCapture );
    return true;
    }
    else
    {
    return false;
    }
}
function insertOption(s,o,i)
{
    if(!s || !o || (i != null && (i < 0 || i >= s.options.length)))
    {
    
    return false;
    }
    if(i == null) 
    {
    try {s.add(o);} 
    catch(e) {s.add(o,null);} 
    }
    else 
    {
    try {s.add(o, i);} 
    catch(e) {s.add(o, s.options[i]);}
    }
}

function removeEvent( obj, szEventType, fnHandler, fUseCapture )
{
    if( obj.detachEvent != null )
    {
    obj.detachEvent( "on" + szEventType, fnHandler );
    return true;
    }
    else if( obj.removeEventListener != null )
    {
    obj.removeEventListener(szEventType, fnHandler, fUseCapture );
    return true;
    }
    else
    {
    return false;
    }
}


var rgOnLoad=new Array();
function AddLoadFn(szfn){window.onload=RunLoadFn;rgOnLoad[rgOnLoad.length]=szfn;}
function RunLoadFn(){for(var i=0;i<rgOnLoad.length;i++)eval(rgOnLoad[i]);}
var rgOnClick=new Array();
function AddClickFn(fn){document.body.onclick=RunClickFn;rgOnClick[rgOnClick.length]=fn;}
function RunClickFn(){for(var i=0;i<rgOnClick.length;i++) {rgOnClick[i]();}}


var g_rgFreeEventElements=new Array();
g_rgFreeEventElements[0] = window;
function AddFreeEventElement(elm){FreeEventsOnUnLoad(); g_rgFreeEventElements[g_rgFreeEventElements.length]=elm;}
var g_clearElementProps = ['onmouseover','onmouseout','onmousedown','onmouseup','onmousemove','onclick','onkeypress','onkeydown','onkeyup','onblur','onfocus',];
var g_fFreeEventOnUnLoad = false;
function FreeEventsOnUnLoad()
{
    if (!g_fFreeEventOnUnLoad)
    {
    if (window.attachEvent)
    {
    window.attachEvent("onunload", function() {
    var el;
    for(var d = g_rgFreeEventElements.length;d--;){
    el = g_rgFreeEventElements[d];
    for(var c = g_clearElementProps.length;c--;){
    el[g_clearElementProps[c]] = null;
    }
    }
    });
    }
    g_fFreeEventOnUnLoad=true;      
    }
}

function WriteCookie(c)
{
        document.cookie=c
}


function DoNothing() {return false;}
function NoSelScroll()
{
        var oColl = document.all.tags("Select");
        var i = oColl.length-1;
        for(i; i >= 0 ; i--)
        {
        var oObj = oColl[i];
        if(!oObj.onmousewheel)
        {
        oObj.onmousewheel = DoNothing;
        }
        }
}

function GetDowStart() {return 1;}
function GetDateFmt() {return "ddmmyy";}
function GetDateSep() {return "/";}

function ShowCalendar(eP,eD,eDP,dmin,dmax)
{
		SCal(eP,eD,eDP,dmin,dmax);
}

function ShowCalSimp(fm,eD,eDP,dmin,dmax){
	var tDate = new Date()
	var sDate = tDate.getMonth()+1 + '/' + tDate.getDate() + '/' + tDate.getFullYear();
	tDate.setFullYear(tDate.getFullYear() + 3)
	var eDate = tDate.getMonth()+1 + '/' + tDate.getDate() + '/' + tDate.getFullYear();
	
	if(!dmin)dmin=sDate;if(!dmax)dmax=eDate;
	if(fm){SetNextFocus(objNext(fm,eD));SetPrevFocus(objPrev(fm,eD));}
	SCal(eD,eD,eDP,dmin,dmax);
}

function ifblank(a,b) {
	if (a == null || a.value == '')
	return b;
	return a;
}

function ResetNextDate(pLowDate,pHighDate,pDays)
{
	var mLowDate  = GetInputDate(pLowDate.value,"ddmmyy");
	var mHighDate = GetInputDate(pHighDate.value,"ddmmyy");
	if (mLowDate == null){
		alert("Invalid Date");
	}else {
		if (mHighDate < mLowDate){
			mLowDate.setDate = mLowDate.getDate() + pDays
			var iDays = mLowDate.getDate() 
			var iMonth = mLowDate.getMonth() + 1;
			if (iDays < 10) iDays = '0' + iDays;
			if (iMonth < 10) iMonth = '0' + iMonth;
			pHighDate.value = iDays + '/' + iMonth + '/' + mLowDate.getFullYear();
		}
	}
}

function checkdatesext(starValue1,starValue2,LowBound,HighBound,Mode)
{
	var status = false;
	var msgtext1='',msgtext2='';
	switch(Mode)
	{
		case 1:
			msgtext1 = 'Start';
			msgtext2 = 'End';
			break;
		default:
			msgtext1 = 'Pickup';
			msgtext2 = 'Dropoff';
			break;
	}
	var tLowBound  = GetInputDate(LowBound,"mmddyy")
	var tHighBound = GetInputDate(HighBound,"mmddyy")
	var mLowBound  = tLowBound.getDate() + '/' + (tLowBound.getMonth()+1) + '/' + tLowBound.getFullYear();
	var mHighBound = tHighBound.getDate() + '/' + (tHighBound.getMonth()+1) + '/' + tHighBound.getFullYear();
	
	if (GetInputDate(starValue1,'ddmmyy') > GetInputDate(starValue2,'ddmmyy')){
		alert('Pickup date cannot be after Dropoff date. Please correct the date selection in the right order.');
	}
	else{
		status = checkdate(starValue1) && checkdate(starValue2);
		if (status == true)
		{
			status = CheckDateBounds(starValue1,LowBound,HighBound);
			if (status == true){
				status = CheckDateBounds(starValue2,LowBound,HighBound);
				if (status == false)
					alert(msgtext2 + ' date is not within a valid date range. Valid date range is ' + mLowBound + ' - ' + mHighBound);
			}else
				alert(msgtext1 + ' date is not within a valid date range. Valid date range is ' + mLowBound + ' - ' + mHighBound);
		}
	}
	return status;
}

function checkdateext( strValue, LowBound, HighBound)
{
	var status = false;

	var tLowBound  = GetInputDate(LowBound,"mmddyy")
	var tHighBound = GetInputDate(HighBound,"mmddyy")
	var mLowBound  = tLowBound.getDate() + '/' + (tLowBound.getMonth()+1) + '/' + tLowBound.getFullYear();
	var mHighBound = tHighBound.getDate() + '/' + (tHighBound.getMonth()+1) + '/' + tHighBound.getFullYear();

	status = checkdate(strValue)
	if (status == true)
	{
		status = CheckDateBounds(strValue,LowBound,HighBound);
		if (status == false)
			alert('Date is not within a valid date range. Valid date range is ' + mLowBound + ' - ' + mHighBound);
	}
	
	return status;
}

function checkdate( strValue ) {
  var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue)){
    alert('Invalid date format. Please enter date in dd/mm/yyyy format.');
    return false; //doesn't match pattern, bad date
  }else{
    var arrayDate = strValue.split('/'); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31,
						'1' : 31,'3' : 31, '4' : 30,'5' : 31,'6' : 30,'7' : 31,
                        '8' : 31,'9' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[1],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  alert('Invalid date selection. Please enter date in dd/mm/yyyy format.');
  return false; //any other values, bad date
}

function CheckDateBounds(pDate,pLowDate,pHighDate)
{
	var mLowDate  = GetInputDate(pLowDate,"mmddyy");
	var mHighDate = GetInputDate(pHighDate,"mmddyy");
	var mDate	  = GetInputDate(pDate,"ddmmyy");
	return (mDate >= mLowDate && mDate <= mHighDate)
}
