function highlightrow(id,columns,span) {
	for (y=1;y<=span;y++) {
		for (i=1; i<=columns; i++) {
			try {
				document.getElementById("c"+i+"-"+id).style.backgroundColor = '#ececec';
			} catch (e) {
			}
		};
		id++;
	}
};

function outhighlightrow(id,columns,span) {
	for (y=1;y<=span;y++) {
		for (i=1; i<=columns; i++) {
			try {
				document.getElementById("c"+i+"-"+id).style.backgroundColor = '#FFFFFF';
			} catch (e) {
			}
		};
		id++;
	}
};

function ChangePeriodText(){
	var selVal = document.frm.period.options[document.frm.period.selectedIndex].value;
	var obj    = document.getElementById("monthId");
	if (selVal!='NA') obj.innerText = selVal + 's';    
	else obj.innerText = '';    
}  

function saveReservation() {
		first=-1;
		all_inputs=document.getElementsByTagName("input");
		for (i=0;i<all_inputs.length;i++) {
			if (all_inputs[i].type=="checkbox") {
				if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
					if (first<0) {
						first=parseInt(all_inputs[i].id);
					} else {
						if ((parseInt(all_inputs[i].id)-first)>1) {
							alert("You should have consecutive selected timeslots");
							return false;
						}						
						first=parseInt(all_inputs[i].id);
					}
				}
			}
		}
		if (first>=0) {
			return true;
		} else {
			alert("Select at least 1 time slot");
		    return false;
		}
}

function getTimeSlotText(object) {
	input=document.getElementById(object);
	if ((input.disabled)&&(input.checked)) {
		return "already booked";
	} else if (input.disabled) {
		return "consecutive slots only";
	} else if (input.checked) {
		return "click on checkbox to remove from booking";
	} else {
		return "click on checkbox to book";
	}
}

function selectTimeSlot() {
	disableElements();
    singlePrice=document.getElementById("singlePrice").value;
    price=0;
	all_inputs=document.getElementsByTagName("input");
	for (i=0;i<all_inputs.length;i++) {
		if (all_inputs[i].type=="checkbox") {
			if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
				price=parseFloat(price)+parseFloat(singlePrice);
			}
		}
	}
    document.getElementById("price").value=price;
}

function disableElements() {
	first=-1;
    last=-1;
    emptySpot=false;
    all_inputs=document.getElementsByTagName("input");
	for (i=0;i<all_inputs.length;i++) {
		if (all_inputs[i].type=="checkbox") {
			if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
				if (emptySpot) {
                	all_inputs[i].checked=false;
                } else {
                    if (first<0) {
                        first=i;
                        last=i;
                    }
                    if ((i-last)>1) {
                        all_inputs[i].checked=false;
                        emptySpot=true;
                    } else {
                        last=i;
                    }
                }
			}
		}
	}

    if (first>=0 && last>=0) {
        for (i=0;i<all_inputs.length;i++) {
            if (all_inputs[i].type=="checkbox") {
                if ((i<(first-1)) || (i>(last+1))) {
                    if ((! all_inputs[i].checked) && (! all_inputs[i].disabled)) {
                        all_inputs[i].disabled=true;
                    }
                } else {
                    if ((! all_inputs[i].checked)) {
                        all_inputs[i].disabled=false;
                    }
                }
            }
        }
     } else {
     	for (i=0;i<all_inputs.length;i++) {
            if (all_inputs[i].type=="checkbox") {
                    if ((! all_inputs[i].checked)) {
                        all_inputs[i].disabled=false;
                    }
            }
        }
     }
}


function showTimeSlots(object,cid,rid,index) {
	ajaxpage('date-availability.php?dt='+object.value+'&cid='+cid+'&rid='+rid+'&sb=1'+'&index='+index,'timeSlots','get');

}

function ShowToolTip(object) {
    document.getElementById(object).style.visibility = 'visible';
}

function HideToolTip(object) {
    document.getElementById(object).style.visibility = 'hidden';
}

// JavaScript Document
var bustcachevar=1; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var bustcacheparameter="";

function ajaxpage(url, containerid, requesttype){
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	} else	return false
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid)
	}
	
	if (requesttype=='get'){
		if (bustcachevar) bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
		page_request.open('GET', url+bustcacheparameter, true)
		page_request.send(null)
	} else if (requesttype=='post') {
		page_request.open('POST', url, true);
		page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		page_request.setRequestHeader("Content-length", parameters.length);
		page_request.setRequestHeader("Connection", "close");
		page_request.send(parameters);
	};
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
	document.getElementById(containerid).innerHTML=page_request.responseText
	if (containerid=="timeSlots") {
		selectTimeSlot();
	}
}


function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

