function book(theTime,theDIV) {
	if (lastDIV) {
		lastDIV.style.backgroundColor=lastDIVcolor;
	}
	lastDIV=theDIV;
	lastDIVcolor=theDIV.style.backgroundColor;
	theDIV.style.backgroundColor="#33FF33";
	document.getElementById('booking_frame').src="booking.php?id=" + theTime;
	//document.getElementById('selection_detail').style.visibility="visible";
	document.getElementById('selection_detail').style.display="block";
	document.getElementById('extraAd').style.display="none";
	//makeAJAXrequest("booking.php?id=" + theTime);
}
/**
 * highlights the element
 */
function hl(element) {
	element.setAttribute("class","available_highlight");
	document.body.style.cursor='pointer';
}
function norm(element) {
	element.setAttribute("class","available");
	document.body.style.cursor='auto';
}

function update_slot() {
	var c=slots[0];
	/*
	alert("now");	
	x=document.getElementById('submit_button');
	if (x) {
		alert (x.id);
	}
	*/
	// validate that it is a time
	var from_time = check_time(document.getElementById('from_time'));
	if (from_time=="error") {
		document.getElementById('booking_message').innerHTML=get_error_message("from");
		document.getElementById('submit_button').disabled=true;
		document.getElementById('from_time').focus();
		
		return;	
	} else {
		document.getElementById('from_time').value = from_time;
	}
	
	// validate that it is a time
	var to_time = check_time(document.getElementById('to_time'));
	if (to_time=="error") {
		document.getElementById('booking_message').innerHTML=get_error_message("to");
		document.getElementById('submit_button').disabled=true;
		document.getElementById('to_time').focus();
		return;	
	} else {
		document.getElementById('to_time').value = to_time;
	}
	
	// switch start and end if necessary
	if (to_time<from_time) {
		document.getElementById('from_time').value = to_time;
		document.getElementById('to_time').value = from_time;
		from_time=document.getElementById('from_time').value;
		to_time=document.getElementById('to_time').value;
	}
	
	// remove colon seperator for ease of processing
	from_time = (from_time.substring(0,2) + from_time.substring(3,5));
	to_time = (to_time.substring(0,2) + to_time.substring(3,5));
	
	// check session is long-enough
	requested_length = time_delta(from_time,to_time);
	if (requested_length < min_length) {
		document.getElementById('booking_message').innerHTML=get_error_message("short");
		document.getElementById('from_time').focus();
		document.getElementById('submit_button').disabled=true;
		return;	
	}
	
	// check for clashes with existing bookings	
	for (the_slot in slots) {
			slot_from = slots[the_slot].from_time.substring(slots[the_slot].from_time.length - 4);
			slot_to = slots[the_slot].to_time.substring(slots[the_slot].to_time.length - 4);
			if (!((slot_to<=from_time) || (slot_from>=to_time))) {
				// slot is relevant
				if (slots[the_slot].qty !=0) {
					// we have a clash
					s="Sorry, you cannot book at this time, because we already have a booking between ";
					s = s + insert_colon(slot_from) + " and " + insert_colon(slot_to);
					s=s + ". Please try again with a different time, then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";
					document.getElementById('booking_message').innerHTML=s;
					document.getElementById('submit_button').disabled=true;
					return;
				}
			}
	}

	
	if (from_time<session_from) {
		document.getElementById('booking_message').innerHTML=get_error_message("early");
		document.getElementById('from_time').focus();
		document.getElementById('submit_button').disabled=true;
		return;
	}
	
	if (to_time>session_to) {
		document.getElementById('booking_message').innerHTML=get_error_message("late");
		document.getElementById('from_time').focus();
		document.getElementById('submit_button').disabled=true;
		return;
	}
	
	update_price(from_time,to_time);
	
	var temptxt="The price for this booking (" +
			"from  " + document.getElementById('from_time').value + " to " + document.getElementById('to_time').value + 
			") is &pound;" + document.getElementById('price').value +".";
	if (prepay_discount!="") {
		temptxt=temptxt + " With our " + prepay_discount +
				" on-line discount (book and pay now) the price comes down to just <b>&pound; " +
				document.getElementById('prepayprice').value +"</b>";	
	}
	document.getElementById('booking_message').innerHTML=temptxt;

	document.getElementById('submit_button').disabled=false;
	
}

function set_initial_price () {
	fr_t=remove_colon(document.getElementById('from_time').value);
	to_t=remove_colon(document.getElementById('to_time').value);
	update_price(fr_t,to_t);
}
function update_price(from_time,to_time) {
	var current_base_price = get_price(from_time,to_time).toFixed(1)+"0";
	document.getElementById('price').value=current_base_price;
	var current_prepay = get_prepayprice(from_time,to_time).toFixed(1)+"0";
	document.getElementById('prepayprice').value=current_prepay;
}

function slot_detail(from_time,to_time,qty)	{
	this.from_time=from_time;
	this.to_time=to_time;
	this.qty=qty;
}	
function get_error_message(error_type) {
	if (error_type=="from") {
		s = "Please enter a valid start time in the 'from' box (use 24-hour clock, e.g. '17:30'), then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";
		return s;
	}
	if (error_type=="to") {
		s = "Please enter a valid end time in the 'to' box (use 24-hour clock, e.g. '21:45'), then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";		
		return s;
	}
	if (error_type=="short") {
		s = "We have set a minimum booking length of " +min_length+ " minutes at this time.  Please adjust your start- and end-time, and then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";		
		return s;
	}
	if (error_type=="early") {
		s = "Sorry, you cannot start before " + insert_colon(session_from)+ ".  Please adjust your start time, and then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";		
		return s;
	}
	if (error_type=="late") {
		s = "Sorry, you cannot end after " + insert_colon(session_to)+ ".  Please adjust your start time, and then click <a id=\"here\" href=\"javascript:update_slot()\" onclick=\"update_slot\">here</a> to see the price of the session";		
		return s;
	}
} 

function insert_colon(timestr) {
	return (timestr.substring(0,2)+":"+timestr.substring(2,4));
}

function remove_colon(timestr) {
	return (timestr.substring(0,2) + timestr.substring(3,5));
}
 
function time_delta(from_t, to_t) {
	from_h = parseInt(from_t.substring(0,2) );
	from_m = parseInt(from_t.substring(2,4) );
	
	to_h = parseInt(to_t.substring(0,2) );
	to_m = parseInt(to_t.substring(2,4) );
	
	return ((to_h*60)+to_m-(from_h*60)-from_m); 
	
}

function check_time(element) {
	var s=element.value;
	var l = s.length; 
	if (l<3) {
		return "error";
	}
	var mn = s.substring(l-2);
	
	if (IsNumeric(mn)) {
		if (parseInt(mn)>59) {
			return "error";
		} 
	} else {
		return "error";
	}
	var hr;
	if (IsNumeric(s.substring(l-3,l-2))) {
		// no seperator so include
		hr=s.substring(0,l-2);
	} else {
		hr=s.substring(0,l-3);
	}
	if (IsNumeric(hr)) {
		if (parseInt(hr)>23) {
			return "error";
		}
	} else {
		return "error";
	}
	if (hr.length<2) {
		hr= "0"+hr;
	}
	
	return (hr+":"+mn);
	
}
	
function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function get_price(from_time,to_time) {
	return time_delta(from_time,to_time)/60*base_price*resource_price;
}

function get_prepayprice(from_time,to_time) {
	the_base=get_price(from_time,to_time);
	if (prepayment_text=="pounds") {
		return (the_base - prepayment_value);
	} else if (prepayment_text=="percent") {
		return the_base * (1-(prepayment_value/100));
	} else {
		return the_base;
	}

}


	function makeAJAXrequest(url) {
        var httpRequest;
	
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send('');

    }

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                //alert(httpRequest.responseText);
                document.getElementById('myCell').innerHTML=httpRequest.responseText;
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
    function grab_data_from_subform() {
    	
    	var subDoc = getIFrameDocument("booking_frame");
    	
    	/*
    	oIframe=document.getElementById("booking_frame");
	    var subDoc = oIframe.contentWindow || oIframe.contentDocument;
	    if (subDoc.document) {
	        subDoc = subDoc.document;
	    }
	    */
	    
	    document.getElementById("from_time").value =subDoc.getElementById("from_time").value;
	    document.getElementById("to_time").value =subDoc.getElementById("to_time").value;
	    document.getElementById("date").value =subDoc.getElementById("date").value;
	    document.getElementById("price").value =subDoc.getElementById("price").value;
	    document.getElementById("prepayprice").value =subDoc.getElementById("prepayprice").value;
	    document.getElementById("sessionPatternID").value =subDoc.getElementById("sessionPatternID").value;
	    document.getElementById("resourceType").value =subDoc.getElementById("resourceType").value;
	    //document.getElementById("request_booking").submit();
		//alert ("got to here");	    
	    return true;
    }

function getIFrameDocument(aID){ 
	 var rv = null; 

	 // if contentDocument exists, W3C compliant (Mozilla) 
	 if (document.getElementById(aID).contentDocument){ 
	   rv = document.getElementById(aID).contentDocument; 
	 } else { 
	   // IE 
	   rv = document.frames[aID].document; 
	 } 

 	return rv; 
} 

function debugcheck() {
	alert ("got to here");
	return true;
}

   
    
 function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function padout(number) { 
	if (number<10) {
		return "0"+number;
	} else {
		return number;
	}
}