lastMarker = new Array();

function datestring_to_date(dateString) {
	dateStringSp1 = dateString.split('_');
	dateStringSp2 = dateStringSp1[2].split('-');
	day = dateStringSp1[3];
	month = dateStringSp2[0];
	year = dateStringSp2[1];	  
	var newDate = new Date();
	newDate.setFullYear(year, (month-1), day);
	return newDate;
}
function date_to_datestring(fromDate,propID) {
	dateString = 'day_'+document.getElementById('booking-property').value+'_'+(fromDate.getMonth()+1)+'-'+fromDate.getFullYear()+'_'+fromDate.getDate();
	return dateString;
}
function date_adddays(myDate,no) {
	myDate.setDate(myDate.getDate()+no);
}




function nbm_calendar_popup(action, dateString, dateStringTo)
{	
	thisPopup = document.getElementById('booking-popup');
	thisForm =  document.getElementById('booking-form');
	thisError = document.getElementById('booking-error');
	document.getElementById('booking-loading').style.display = 'none';
	document.getElementById('booking-success').style.display = 'none';
	
	bookingEmailStatus.style.display = 'none';
	bookingNameStatus.style.display = 'none';
	bookingDateStatus.style.display = 'none';
	bookingValidDateStatus.style.display = 'none';

	if (lastMarker.length > 0) {
	  for (b = 0; b < lastMarker.length; b ++ ) {
	    document.getElementById(lastMarker[b]).className = document.getElementById(lastMarker[b]).className.replace('day-requested','');
	  }
	  lastMarker.clear(); //lastMarker.length = 0;	 
	}
	  
	


	if (action == 1)
	{
	  thisPopup.style.display = 'block';
	  thisForm.style.display = 'block';
	  thisError.style.display = 'none';
	  
	  thisPopupFrom = document.getElementById('popup-from');
	  thisPopupTo = document.getElementById('popup-to');
	  
	  dateStringSp1 = dateString.split('_');
	  dateStringSp2 = dateStringSp1[2].split('-');
	  day = dateStringSp1[3];
	  month = dateStringSp2[0];
	  year = dateStringSp2[1];
	  
	  var myDate = new Date();
		myDate.setFullYear(year, (month-1), day);
		
		//var tempDate = myDate;
		
		
		if (dateStringTo) myDate = datestring_to_date(dateStringTo);
		else myDate.setDate(myDate.getDate()+6);
 
	  tempDate = datestring_to_date(dateString);
	  

	  firstBroken = 0;
	  broken = false;
    a = 0;
	  myDate.setDate(myDate.getDate()+1);
	  
	  do {
	    thisTDid = date_to_datestring(tempDate,dateStringSp1[1]);
	    if (document.getElementById(thisTDid)) {
				if (!document.getElementById(thisTDid).className.match('booked')) {
					document.getElementById(thisTDid).className += ' day-requested';
					lastMarker[a] = thisTDid;
					if (firstBroken == 1) {
						firstBroken = 2;
						firstBrokenDate = date_to_datestring(tempDate);
					}
					tempDate.setDate(tempDate.getDate()+1);
					a ++;
				}
				else {
					if (a == 0) {
						firstBroken = 1;
						tempDate.setDate(tempDate.getDate()+1);
						continue;
					}
					else {
						broken = true;
						break;
					}
				}
      }
      else break;
	
	  }
	  while (date_to_datestring(tempDate,dateStringSp1[1]) != date_to_datestring(myDate,dateStringSp1[1]));
	 
	  tempDate.setDate(tempDate.getDate()-1);
	  
    if (broken == true && dateStringTo) {
      bookingDateStatus.style.display = 'block';
	    bookingDateStatus.innerHTML = 'End date that you selected is already booked so it has been adjusted.';
    }
    
    
	  if (firstBroken == 2) {
	    tempDate2 = datestring_to_date(firstBrokenDate);
	    thisPopupFrom.value = addZeros(tempDate2.getDate(),2)+'/'+addZeros((tempDate2.getMonth()+1),2)+'/'+tempDate2.getFullYear();
	    
	    bookingDateStatus.style.display = 'block';
	    bookingDateStatus.innerHTML = 'Start date that you selected is already booked so it has been adjusted.';
	    
	  }
	  else thisPopupFrom.value = addZeros(day,2)+'/'+addZeros(month,2)+'/'+year;
	  
	  thisPopupTo.value = addZeros(tempDate.getDate(),2)+'/'+addZeros((tempDate.getMonth()+1),2)+'/'+tempDate.getFullYear();

	}
	else if (action == 2) {
	  thisPopup.style.display = 'block';
	  thisForm.style.display = 'none';
	  thisError.style.display = 'block';
	}
	else if (action == 0) {
    thisPopup.style.display = 'none';
	}

	 
}


function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true					
}


function nbm_checkform_booking_request() {

	thisStatus = true;
	
	if ( document.getElementById('booking-name') ) {
		bookingNameValue = document.getElementById('booking-name').value;
		if (bookingNameValue == '') {
			bookingNameStatus.style.display = 'block';
			thisStatus = false;
		}
		else bookingNameStatus.style.display = 'none';
	}
	
	
	if (document.getElementById('booking-email')) {
	
		bookingEmailValue = document.getElementById('booking-email').value;
		if (bookingEmailValue == '' || !echeck(bookingEmailValue)) {
			bookingEmailStatus.style.display = 'block';
			thisStatus = false;
		}
		else bookingEmailStatus.style.display = 'none';
	
	}

	if ( document.getElementById('popup-from') && document.getElementById('popup-to') ) {

		thisPopupFromValue = document.getElementById('popup-from').value;
		thisPopupToValue = document.getElementById('popup-to').value;
		
		if ( isDate(thisPopupFromValue) == false || isDate(thisPopupToValue) == false ) {
			bookingValidDateStatus.style.display = 'block';
			thisStatus = false;
		}
	
	}
	else {
	
		thisPopupFromValue = thisPopupToValue = 'vendor';
	
	}
	
	
	
	thisPropertyValue = document.getElementById('booking-property').value;
	
	
	if (thisStatus == false) { return false; }
	else { // We are sending here
		document.getElementById('booking-request-submit').disabled = true;
		document.getElementById('booking-form').style.display = 'none';
		document.getElementById('booking-loading').style.display = 'block';
		
		new Ajax.Request(wpurl+'/wp-content/plugins/clearskys/request.php?booking-from='+thisPopupFromValue+'&booking-to='+thisPopupToValue+'&booking-name='+bookingNameValue+'&booking-email='+bookingEmailValue+'&booking-property='+thisPropertyValue,
		{
			method:'get',
			onSuccess: function(transport) {
				var response = transport.responseText || "no response text";
				
				if ( response == 1 ) {
				  document.getElementById('booking-request-submit').disabled = false;
				  document.getElementById('booking-loading').style.display = 'none';
				  document.getElementById('booking-success').style.display = 'block';
				}
			},
			onFailure: function()	{ 
				document.getElementById('booking-loading').innerHTML = '<strong>An error occured.</strong><p>Please refresh the page and try again.</p>';
			}
		});
		return false;
	}
	
}
