/********************************************************
Make this IE7 Compatible ;)
http://ajaxian.com/archives/ajax-on-ie-7-check-native-first
*********************************************************/
var mouseSwitch = 'on';

function mouseChange() {
	//alert (mouseSwitch);
	if (mouseSwitch = 'on') {
		document.body.style.cursor = 'pointer';
	}
	if (mouseSwitch = 'off') {
		//document.body.style.cursor = 'wait';
	}
}

createRequestObject = function() {
   var xmlhttp;
       /*@cc_on
   @if (@_jscript_version>= 5)
           try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
           } catch (e) {
                   try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
                   catch (E) {xmlhttp = false;}
           }
   @else
       xmlhttp = false;
   @end @*/
   if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
           try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
   }
   return xmlhttp;} 

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

//Click function
function setSearch(value, destID) {
	var destTarg = 'destination' + destID;
	var sugTarg = 'search_suggest' + destID;
	document.getElementById(destTarg).value = value;
	document.getElementById(sugTarg).innerHTML = '';
	document.getElementById(sugTarg).style.border = "none";
}

function searchSuggest(destID) 
{
	var http = createRequestObject();
	var strURL = '';
	var destTarg = 'destination' + destID;
	//alert('running');
	
	//if (http.readyState == 4 || http.readyState == 0) {
		var str = escape(document.getElementById(destTarg).value);
		var strURL = 'library/ajax_functions.asp?functype=searchSuggest&search=' + str;
		http.open("GET", strURL, true);
		//alert(strURL);
		http.onreadystatechange = function() {
			//alert(http.readyState);
			if (http.readyState == 4) {
				var ss = document.getElementById('search_suggest'+destID);
				ss.style.border = "#666666 1px solid";
				ss.innerHTML = '<ul style="margin:0; padding:0; list-style:none;>';
				//alert(http.responseText);
				var str = http.responseText.split(",");
				//alert(str[0]);
				for(i=0; i < str.length - 1; i++) {
					//Build our element string.  This is cleaner using the DOM, but
					//IE doesn't support dynamically added attributes.
					var suggest = '<li onmouseover="suggestOver(this)" ';
					suggest += 'onmouseout="suggestOut(this)" ';
					suggest += 'onclick="setSearch(this.innerHTML,' + destID + ')" ';
					suggest += 'class="suggest_link" style="display:block; cursor:pointer;">' + str[i] + '</li>';
					ss.innerHTML += suggest;
					
				}
				ss.innerHTML += '</ul>';
			}
		}
					
		http.send(null);
	//}
}

function savethedate(ID,daysleft,weekendsleft)
{
	var http = createRequestObject();
	var strURL = '';
	
	var waiting = document.getElementById('waiting');
	var span = document.getElementById('sp_'+ID);
	var checkboxID = 'chkdate'+ID;
	var thecheckbox = document.getElementById(checkboxID);
	//alert('Activated.');
	
	
	if (thecheckbox.checked)
	{
   		strURL = 'library/ajax_functions.asp?functype=addcookie&daysleft=' + daysleft + '&weekendsleft=' + weekendsleft + '&date=' + escape(thecheckbox.value);
		//alert(strURL);
	}else{
   		strURL = 'library/ajax_functions.asp?functype=removecookie&date=' + escape(thecheckbox.value);
		//alert(strURL);
	}

	http.open('get', strURL, true);
	mouseSwitch = 'off';
	waiting.style.zIndex = '+999';
	
   	http.onreadystatechange = function() {
       if(http.readyState == 4){
           var response = http.responseText;
             
			//alert(response);
			
			if (response == 'Added') {
			
				var liID = 'li_'+ID;
				//alert('Added. id='+liID);
				//var listyle = document.getElementById(liID).style;
				document.getElementById(liID).style.backgroundColor = "#ffd5a9";
				if (span.firstChild) {
					span.removeChild(span.firstChild);
				}
				span.appendChild(document.createTextNode('Click to remove date'));
				mouseSwitch = 'on';
				waiting.style.zIndex = '-999';
				
			}else if(response == 'Removed'){
				
				//alert('This date has been removed.');
				var liID = 'li_'+ID;
				//var listyle = document.getElementById(liID).style;
				document.getElementById(liID).style.backgroundColor = "#d5ffd5";
				if (span.firstChild) {
					span.removeChild(span.firstChild);
				}
				span.appendChild(document.createTextNode('Click to select date'));
				mouseSwitch = 'on';
				waiting.style.zIndex = '-999';
				
	  		}else if(response == 'Weekends'){
				
				var chkid = 'chkdate'+response;
				alert('You have used up your monthly weekend allocation.\n\nPlease call the RIBshare team and we may be able to allocate a different boat to you.');
				document.getElementById(chkid).checked = false;		
				mouseSwitch = 'on';
				waiting.style.zIndex = '-999';
				
	  		}else{
				
				var chkid = 'chkdate'+response;
				alert('Sorry, you have already used up your monthly allocation.\n\nPlease call the RIBshare team and we may be able to allocate a different boat to you.')
				document.getElementById(chkid).checked = false;
				mouseSwitch = 'on';
				waiting.style.zIndex = '-999';
				
			}

       }
   }

   http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   http.send(null); 
	
}


function cancelthedate(B_ID)
{
	
	 if (confirm("Are you sure you want to cancel this booking?"))  {
	
			var http = createRequestObject();
			var strURL = '';
			strURL = 'library/ajax_functions.asp?functype=cancelbooking&b_id=' + escape(B_ID);
		
			http.open('get', strURL, true);
			
			http.onreadystatechange = function() {
			   if(http.readyState == 4){
				   var response = http.responseText;
					 
					if (response == 'Removed') {
						//alert('Your booking has been cancelled');
						window.location.reload(true);
					}
			   }
		   }
		
		   http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		   http.send(null); 
	 }
}

function info_popup(filename,windowname,winWidth,winHeight) {
  
  var theWindow = window.open(filename, windowname, 'width='+winWidth+',height='+winHeight+',Top=100,Left=100,scrollbars=yes');
  theWindow.focus();
  return theWindow;
}



function timecheck(timeid)
{
	
	if (document.getElementById('return'+timeid).options[document.getElementById('return'+timeid).selectedIndex].value <= document.getElementById('departure'+timeid).options[document.getElementById('departure'+timeid).selectedIndex].value)
	{
		
	alert('Choose a time after the departure time');
	return false
	}
	
}


function checkthecheckbox(checkboxID,daysleft,weekendsleft)
{
	var chkid = 'chkdate'+checkboxID;
	if (document.getElementById(chkid).checked == true)
	{
		document.getElementById(chkid).checked = false;
	}
	else
	{
		document.getElementById(chkid).checked = true;
	}
	savethedate(checkboxID,daysleft,weekendsleft);
}


function timechecksubmit(timedays)
{
	var bvalid = true;
	var validtext = '';
	
	validtext += 'Some required fields are missing.\n\nPlease check the following fields:\n\n';
	
	for (i = 0;i <= timedays;i++){
		var day = i + 1;
		if (document.getElementById('waterproofs'+i).options[document.getElementById('waterproofs'+i).selectedIndex].value == '')
		{
			bvalid = false;
			validtext = validtext + '- Number or Water proofs on day #' + day + '.\n';
		}
		if (document.getElementById('destination'+i).value == '')
		{
			bvalid = false;
			validtext = validtext + '- Destination for day #' + day + '.\n';
		}
		if (document.getElementById('return'+i).options[document.getElementById('return'+i).selectedIndex].value <= document.getElementById('departure'+i).options[document.getElementById('departure'+i).selectedIndex].value)
		{
			bvalid = false;
			validtext = validtext + '- Please select a planned return time later than the planned departure time on day #' + day + '.\n';
		}
		if (document.getElementById('total'+i).options[document.getElementById('total'+i).selectedIndex].value == '')
		{
			bvalid = false;
			validtext = validtext + '- Group size (inc. Skipper) on day #' + day + '.\n';
		}
		
		if ((document.getElementById('chkLunchYes'+i).checked == true) && (document.getElementById('chkLunchNo'+i).checked == true))
		{
				bvalid = false;
				validtext = validtext + '- Catering / Accommodation on day #' + day + '.\n';
		}
		if ((document.getElementById('chkLunchYes'+i).checked == false) && (document.getElementById('chkLunchNo'+i).checked == false))
		{
				bvalid = false;
				validtext = validtext + '- Catering / Accommodation on day #' + day + '.\n';
		}
	}
	if (bvalid == false){
		alert(validtext);
		return false
	}
}

function checkContinue(reqMonth, reqYear)
{
	var bvalid = false;
	var validtext = '';
	var right_now=new Date();
	
	if (reqYear == ""){
		reqYear = 	right_now.getYear();
	}
	if (reqMonth == ""){
		reqMonth = 	right_now.getMonth();
	}
	
	for (check=1;check<=31;check++)
	{
		if (document.getElementById('chkdate'+ check) != null)
		{
			if (document.getElementById('chkdate'+ check).checked)
			{
				bvalid = true;
			}
		}
	}
	if (bvalid == false)
	{
		alert('Please select a date before pressing Continue.');
	}
	else
	{
		location.href='bookDates.asp?month='+reqMonth+'&year='+reqYear	
	}
}



