var bookingStarted = false;
var bookingCompleted = false;
var bookingStartDate = '';
var hilitedStart = 0;
var hilitedEnd = 0;
var hilited = [];
var styleBeforeHilite = [];
var baseurl = "";

var site_url;
if(site_url == undefined){
	site_url = "/codeigniter/mvrmsvn/mvrm/"; // should not be hard coded but is for now [Steve]
}


//for multibookings
var newBookingCounter = 0;
var propertyId; //needed for multibookings
var prefix = ""; //for multibookings a prefix and suffix are needed
var suffix = "";



function setPrefixAndSuffix(pref, suf){
	prefix = pref;
	suffix = suf;
}


function inMultiMode(){
	return ((prefix != "") || (suffix != ""));
}

function setBookingDate($month, $day, $year, dayId){
	
	if(bookingCompleted){
		if(inMultiMode()){
			//in multicalendar mode so dont bother with the warning just load a new slot			
		}else{
			//in single booking mode so show the warning
			var answer = confirm ("You have not saved the current booking. Continue without saving current booking?");
			if(!answer){
				return;
			}else{
				bookingCompleted = false;
			}
		}
	}
	
	if (!bookingStarted){  //!(document.getElementById('start_date')).value
		document.getElementById(prefix + 'start_date' + suffix).value = $month + '/' + $day + '/' + $year;
		bookingStartDate = document.getElementById(prefix + 'start_date' + suffix).value;
		
		if(hilitedStart > 0){ //check to see if we have been through hilighting before this is the first time through
			unHiliteDays(hilitedStart-1, !inMultiMode()); // dont actually change the color back to white because we are in multi mode
		}
		
		bookingStarted = true;
		hilitedStart = dayId;
		hiliteDays(dayId);				
	}
	else if (bookingStarted) { //(document.getElementById('start_date')).value
		document.getElementById(prefix + 'end_date' + suffix).value = $month + '/' + $day + '/' + $year;
		if(bookingStartDate == document.getElementById( prefix + 'end_date' + suffix).value){
			alert("This is the booking's start date. Please select another date.");
			document.getElementById(prefix + 'end_date' + suffix).value = '';
		} else {		
			bookingStarted = false;		
			bookingCompleted = true;
			
			newBookingCounter = newBookingCounter + 1;
			
			ajaxLoadBooking('newbooking' + newBookingCounter, ''); //load the slot for the new booking
			
			if(document.getElementById('generaldivcontainer') != null){ //scroll back to the top
				document.getElementById('generaldivcontainer').scrollTop = 0;
			}else{ // we are using the multicalendar interface
				document.getElementById('calendardivcontainer').scrollTop = 0;
			}
			document.getElementById(prefix + 'status' + suffix).focus();
		}
	}
}


/**
  * changes the tip based on where user is at in the booking creation process
  */
function getTip(date, dayId){
	var startTip = "Start Booking on ";
	var endTip = "End Booking on ";
	var tip = "";
	
	if(bookingStarted){
		tip = endTip + date;
		
		hiliteDays(dayId);
		
	}else{
		tip = startTip + date;
		//hilitedStart = dayId;
	}
	
	return tip;
}

/**
 * hilites days so that we know what booking we are working on
 */
function hiliteDays(dayId){
	if(bookingStarted){
	
		//change the colors of the days
		for(i=hilitedStart; i<=dayId; i++){
			var day = document.getElementById('d'+i);
			if(day != null){
				styleBeforeHilite[i] = day.style.backgroundColor;
				day.style.backgroundColor  = "#ff0000";
			}
			hilited[i] = true;
		}
		
		unHiliteDays(dayId, true);
	}/*else{
		unHiliteDays(hilitedStart);
	}*/
}

function unHiliteDays(dayId, doUnHilite){
	
	for(j=(dayId+1); j<=hilitedEnd; j++){
		var day = document.getElementById('d'+j);  
		if(day != null){
			/*if(styleBeforeHilite[j] != null){
				day.style.backgroundColor = styleBeforeHilite[j];
			}else{*/
				if(doUnHilite){
					day.style.backgroundColor = "#fff";
				}
			//}
		}
		hilited[j] = false;
	}		
	
	hilitedEnd = dayId;
}

/**
 * clears the booking form
 */
/*function clearDateForm(){
	document.getElement.ById('id').value = '';
	document.getElement.ById('start_date').value = '';
	document.getElement.ById('end_date').value = '';
	//document.getElement.ById('status').selectedIndex = '';
	document.getElement.ById('client_name').value = '';
	document.getElement.ById('description').value = '';
	document.getElement.ById('staff_notes').value = '';
}*/

function displayBooking($id, baseurl){
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
	    try{
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e){
	      alert("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	
	xmlHttp.onreadystatechange = function() {
	    if (xmlHttp.readyState == 4) {
	        // this is where the return information is
	        
	        var div = document.getElementById('bookingformlet');
	        div.innerHTML = xmlHttp.responseText;
	        
	        bookingStarted = false; //so that we can reset the tooltips
	        
	        if(document.getElementById('generaldivcontainer') != null){ //scroll back to the top
				document.getElementById('generaldivcontainer').scrollTop = 0;
			}else{ // we are using the multicalendar interface
				document.getElementById('calendardivcontainer').scrollTop = 0;
			}
	        document.getElementById(prefix + 'start_date' + suffix).focus();
	    }
	}
	
	document.getElementById('bookingformlet').innerHTML="<div id=\"loader\"><img src='" + site_url + "assets/layout/ajaxtabs/loading.gif' /> Requesting content...</div>"; //Display "fetching page message"
	
	xmlHttp.open('POST', baseurl+'/bookings/formlet');
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('id='+$id);
}

function displayNewBooking($property_id, baseurl){
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
	    try{
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e){
	      alert("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	
	xmlHttp.onreadystatechange = function() {
	    if (xmlHttp.readyState == 4) {
	        // this is where the return information is
	        
	        var div = document.getElementById('bookingformlet');
	        div.innerHTML = xmlHttp.responseText;
	        
	        bookingStarted = false; //so that we can reset the tooltips
	        
	        if(document.getElementById('generaldivcontainer') != null){ //scroll back to the top
				document.getElementById('generaldivcontainer').scrollTop = 0;
			}else{ // we are using the multicalendar interface
				document.getElementById('calendardivcontainer').scrollTop = 0;
			}
	        document.getElementById(prefix + 'start_date' + suffix).focus();
	    }
	}
	
	document.getElementById('bookingformlet').innerHTML="<div id=\"loader\"><img src='" + site_url + "assets/layout/ajaxtabs/loading.gif' /> Requesting content...</div>"; //Display "fetching page message"
	
	xmlHttp.open('POST', baseurl+'/bookings/formlet');
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('id=&property_id='+$property_id);
}


function ajaxLoad(container, url, params, message, hideOnComplete){	
	
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
	    try{
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e){
	      alert("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	
	xmlHttp.onreadystatechange = function() {
	    if (xmlHttp.readyState == 4) {
	        // this is where the return information is
	        
	        var div = document.getElementById(container);
	        div.innerHTML = xmlHttp.responseText;
			
			if (hideOnComplete) {
				displayElement(container); // hide the container on completion
			}		
	    }
	}
	
	if(message == ""){
		message = "Requesting content...";
	}
	
	document.getElementById(container).innerHTML="<div id=\"loader2\"><img src='" + site_url + "assets/layout/ajaxtabs/loading.gif' /> " + message + "</div>"; //Display "fetching page message"
	
	if (url.indexOf("http", 0) == -1) {				
		if(url.indexOf(site_url, 0) == -1){
			xmlHttp.open('POST', site_url + url);
		}else{
			tmpurl = url.replace(site_url, '/');
			xmlHttp.open('POST', site_url + tmpurl);
		}
	}else{
		xmlHttp.open('POST', url);
		//alert(url);
	}
	
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(params);
	return xmlHttp;
}



function ajaxLoadBooking(container, baseurl){
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
	    try{
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e){
	      alert("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	
	xmlHttp.onreadystatechange = function() {
	    if (xmlHttp.readyState == 4) {
	        // this is where the return information is
	        
	        var div = document.getElementById(container);
	        div.innerHTML = xmlHttp.responseText;
	        
	        bookingStarted = false; //so that we can reset the tooltips
	        
	        /*if(document.getElementById('generaldivcontainer') != null){ //scroll back to the top
				document.getElementById('generaldivcontainer').scrollTop = 0;
			}else{ // we are using the multicalendar interface
				document.getElementById('calendardivcontainer').scrollTop = 0;
			}*/
	        document.getElementById(prefix + 'start_date' + suffix).focus();
	    }
	}
	
	document.getElementById(container).innerHTML="<div id=\"loaderinline\"><img src='" + site_url + "assets/layout/ajaxtabs/loading.gif' /> Requesting content...</div>"; //Display "fetching page message"
	
	//a bit of a hack sice script doesnt run on the ajax loaded page... need to fix this
	setPrefixAndSuffix('bookings['+newBookingCounter+'][', ']');
	
	xmlHttp.open('POST', site_url + baseurl + '/bookings/formlet');
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('id=&propertyId=' + propertyId + '&multi=true&newBookingCounter='+newBookingCounter); //$id
}


function setArrivalDate(){
	var now = new Date();
	
	if(prefix == null) prefix = "";
	if(suffix == null) suffix = "";
	
	var cal = new CalendarPopup(); //"dateDiv"
	cal.showYearNavigation();
	cal.showYearNavigationInput();
	
	cal.select(document.getElementById(prefix + 'start_date' + suffix), 'arrival_link','M/d/yyyy');
	return false;
}

function setDepartureDate(){
	var now = new Date();
	
	if(prefix == null) prefix = "";
	if(suffix == null) suffix = "";
	
	var cal = new CalendarPopup(); //"dateDiv"
	cal.showYearNavigation();
	cal.showYearNavigationInput();
	
	cal.select(document.getElementById(prefix + 'end_date' + suffix ), 'departure_link','M/d/yyyy');
	return false;
}

function setExpiryDate(){
	var now = new Date();
	var cal = new CalendarPopup(); //"dateDiv"
	cal.showYearNavigation();
	cal.showYearNavigationInput();
	
	cal.select(document.getElementById(prefix + 'expiry_date' + suffix), 'expiry_link','M/d/yyyy');
	return false;
}

function setOrDisplay($month, $day, $year, $bookingId, $baseurl, $startOrEnd, $dayId){
	/*var editBooking = confirm('This is the ' + $startOrEnd + ' date for booking # ' + $bookingId + '.\nClick OK to edit, or Cancel to create a new booking.');
	
	if(!editBooking){*/
		setBookingDate($month, $day, $year, $dayId);
	/*} else{
		displayBooking($bookingId, $baseurl);
	}*/
}

function cancelBooking(id){
	if (document.getElementById('bookinginfo' + id) != null) {
		document.getElementById('bookinginfo' + id).innerHTML = '';
	}
}
