var DOM = {
	isParentOf: function( parentElm, contextElm) {
		while(contextElm && (contextElm != parentElm))
			contextElm = contextElm.parentNode;
		return (contextElm == parentElm);
	},
	getParentOrSelf: function( contextElm, nodeName ) {
		nodeName = nodeName.toLowerCase();
		while(contextElm.nodeName.toLowerCase() != nodeName && contextElm.parentNode)
			contextElm = contextElm.parentNode;
		return contextElm;
	},
	addClass: function( elm, className ) {
		elm.className += ' '+className;
	},
	removeClass: function( elm, className) {
		var classMatch = new RegExp('\\b'+className+'\\b', 'g');
		if(classMatch.test(elm.className))
			elm.className = elm.className.replace(classMatch, ' ');
	}	
};
var gClientIsGecko = (window.controllers) ? true : false;
var gClientIsOpera = (window.opera) ? true : false;
var gClientIsIE    = (document.all && !gClientIsOpera) ? true : false;
var gClientIsIE5   = (gClientIsIE && /MSIE 5\.0/.test(navigator.appVersion)) ? true : false;
var gClientIsMac   = (/Mac/.test(navigator.appVersion)) ? true : false;

function tickCheckBox(el){
	if (document.getElementById) {
		if (document.getElementById(el)){
			document.getElementById(el).checked=true;
		}
	}
	return true;
}
// Getting element dimensions
function getDimensions( elm ) {
	var box = { x:0, y:0, w:0, h:0 };
	if(document.getBoxObjectFor) {
		var boxRef = document.getBoxObjectFor(elm);
		box.x = boxRef.x;
		box.y = boxRef.y;
		box.w = boxRef.width;
		box.h = boxRef.height;
	}
	else if(elm.getBoundingClientRect) {
		var rxIE50 = /MSIE 5\.0/g;
		//alert(rxIE50 + '.test("' + navigator.appVersion + '" = ' + rxIE50.test(navigator.appVersion));
		var boxRef = elm.getBoundingClientRect();
		box.x = boxRef.left;
		box.y = boxRef.top;
		box.w = (boxRef.right - boxRef.left);
		box.h = (boxRef.bottom - boxRef.top);
		//var s='';for(p in boxRef) s+=p+'    '; alert(s);
		// Damn IE...
		if(document.compatMode && document.compatMode != 'BackCompat') {
			// IE6/compliance mode
			box.x += document.documentElement.scrollLeft - 2;
			box.y += document.documentElement.scrollTop - 2;
		}
		else if(!gClientIsIE5) {
			// IE5.5
			box.x += document.body.scrollLeft - 2;
			box.y += document.body.scrollTop - 2;
		}
	}
	else {
		// No known box information available, walking
		// manually through offsetParents to calculate x/y coordinates
		box.w = elm.offsetWidth;
		box.h = elm.offsetHeight;
		while(elm) {
			box.x += elm.offsetLeft;
			box.y += elm.offsetTop;
			if(elm.offsetParent) // Required for Safari 1.3 :(
				elm = elm.offsetParent;
			else
				break;
		}
	}
	var cc;
	if(cc = document.getElementById('bodyconstraint'))
		box.x -= cc.offsetLeft;
	return box;
}



/* */
// showCalendar without requirements for unique id's
//	@me	DOMNode context node
//	@calId	String	id of calendar node
//	@dt	String	'checkin' or 'checkout' prefix
function showCalendar(me, calId, dt){
	getDimensions(me);
    if (document.getElementById){
        var c = document.getElementById(calId);
		var f = DOM.getParentOrSelf(me, 'form');
        calendar.cal = c;
        calendar.caldt = dt;
        calendar.calf = f;
    td=new Date();
if(f['y'].value != '') {y = f['y'].value;}else{y = td.getFullYear();}
if(f['m'].value != '') {m = f['m'].value;}else{m = td.getMonth()+1;}
if(dt=="checkout") {
if(f['d'].value != '') {d = f['d'].value;}else{d = td.getDate()+1;}
}else{
if(f['d'].value != '') {d = f['d'].value;}else{d = td.getDate();}
}
        buildCal(y,m,d);
		var left = 800, top = 200;
        c.style.left = left+'px';
        c.style.top = top+'px';
        c.style.display="block";
	}
}

function closeCal() {
    calendar.cal.style.display='none';
}

function buildCal(y,m,d){
    var daysInMonth=[31,0,31,30,31,30,31,31,30,31,30,31];
    td=new Date();
    if (!y) y = td.getFullYear();
    if (!m) m = td.getMonth()+1;
    if (!d) d = td.getDate;
	//var frm = calendar.calfrm;
    var dt = calendar.caldt;

    var mDate = new Date(y, m-1, 1);
    var firstMonthDay = mDate.getDay();
    daysInMonth[1]=(((mDate.getFullYear()%100!=0)
        &&(mDate.getFullYear()%4==0)) || (mDate.getFullYear()%400==0))?29:28;

    var t='<table class="caltable" cellspacing="0"><tr>';
    t+='<td class="calheader" colspan="7" class="monthYear">';
    if (y==td.getFullYear() && m==td.getMonth()+1) {
        t+='......';
    }
    else {
        t+='<a class="calPrevMonth" href="" onclick="prevMonth('+y+','+m+'); return false;" title="'+tr.prevMonth+'"><<<</a>';
    }
    t+='&nbsp;<select name="ym" onchange="goYearMonth(this.options[this.selectedIndex].value)">';
    var mn=1;var yr=td.getFullYear();
    var last_month=0;
    for(n=0;n<=36;n++){
        t+='<option value="' + yr + '-' + mn + '"';
        if (mn == m && yr == y) {
            t+=' selected="selected"';
            last_month=1;
        } else {
            last_month=0;
        }
        t+='>' + months[mn-1] + ' ' + yr +'</option>';
        mn++; if (mn>12) { mn=1;yr++ }
    }
    t+= ' </select>&nbsp;';
    if (last_month==1) {
        t+='......';
    }
    else {
        t+='<a class="calNextMonth" href="" onclick="nextMonth('+y+','+m+'); return false;" title="' + tr.nextMonth +'">>>></a>';
    }
    t+='</td></tr>';
    t+='<tr class="dayNames">';
    for(dn=0;dn<7;dn++){
        var cl = '';
        if ((dn%7==5) || (dn%7 == 6)) cl += ' weekend';
        t+='<td class="'+cl+'">'+days[dn]+'</td>';
    }
    t+='</tr><tr class="days">';
    for(i=1;i<=42;i++){
        var x = i - (firstMonthDay+6)%7;
        if (x > daysInMonth[m-1] || x <1) x = '&nbsp;';
        var cl = '';
        var href = 0;
        if ((i%7==0) || (i%7 == 6)) cl += ' weekend';
        if (x>0){
            var xDay = new Date(y, m-1, x);
            if ((xDay.getFullYear() == y) && (xDay.getMonth()+1 == m)
                && (xDay.getDate() == d))
                { cl += ' selected' ; href=1}
            if ((xDay.getFullYear() == td.getFullYear())
                && (xDay.getMonth() == td.getMonth())
                && (xDay.getDate() == td.getDate()))
                { cl += ' today'; href=1;}
            else {
                if (xDay > td){ cl += ' future'; href=1; }
                else {
                    if (xDay < td) { cl += ' past'}
                }
            }
        };
        t+='<td class="'+cl+'">';
        if (href){
            t+='<a href="#" onclick="pickDate('+y+','+m+','+x+',\''+dt+'\'); return false;">'+x+'</a>';
        } else {
            t+=x;
        }
        t+='</td>';
        if(((i)%7==0)&&(i<36)) {
            t+='</tr><tr class="days">';
        }
    }
    t+='</tr><tr><td colspan="7"><a href="#" onclick="closeCal();return false;">' + tr.closeCalendar + '</a></td></tr></table>';
    document.getElementById("calendar").innerHTML= t;
}

function prevMonth(y,m) {
    if (new Date(y,m-1,1) < td) return;
    if (m > 1) {m--} else {m = 12; y--};
    buildCal(y,m);
}

//does this finction need to check for max month/year?
function nextMonth(y,m) {
    if (m<12){m++;} else {m=1;y++;}
    buildCal(y,m);
}

function goYearMonth(ym){
	var ymlist = ym.split("-");
    buildCal(ymlist[0],ymlist[1]);
}

function pickDate(y,m,d,dt){
    // set form values
    var f = calendar.calf;
    var dt = calendar.caldt;
	if (dt == "checkin"){
    f['arrival'].value = d+"/"+m+"/"+y;
	d++;
    f['departure'].value = d+"/"+m+"/"+y;

	f['y'].value = y;
	f['m'].value = m;
	f['d'].value = d;
	}else{
    f['departure'].value = d+"/"+m+"/"+y;
	}

    closeCal();
//	updateDaySelect(f);
}
function initDaySelect() {
	var forms = document.getElementsByTagName('form');
	for(var i=0; i<forms.length; i++)
		if(forms[i]['arrival'])
			updateDaySelect(forms[i]);
}

function buildDaysForMonth( year, month ) {
	// Month index starts on 0(-11) in Date()-object
	var monthDate = new Date(year, month-1);
	var orgMonth = monthDate.getMonth();
	var dayArray = weekDay;
	while(monthDate.getMonth() == orgMonth) {
		// Week starts on Sunday in Date()-object
		weekDay = (monthDate.getDay() == 0) ? 6 : (monthDate.getDay()-1);
		//dayArray.push(days3[weekDay]);
		monthDate.setDate(monthDate.getDate()+1);
	}
	return dayArray;
}




// Searches children to find image
function getChildImage( contextElm ) {
	contextElm = contextElm.firstChild;
	while(contextElm.nodeName.toLowerCase() != 'img' && contextElm.nextSibling)
		contextElm = contextElm.nextSibling;
	return contextElm;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// OBSOLETE - use DOM.getParentOrSelf in base.js instead
// Searches upwards from the current context element until it finds the specified tag
function getParentOrSelf( contextElm, nodeName ) {
	nodeName = nodeName.toLowerCase();
	while(contextElm.nodeName.toLowerCase() != nodeName && contextElm.parentNode)
		contextElm = contextElm.parentNode;
	return contextElm;
}
