// businessID and general calendar settings
var businessID = "6d7519"; // eneter busnessID provided by StrictManager.com
var block_today = 'Y'; // 'Y' for not disallowing reservation for current day.
var number_of_months_in_future = 'A'; // value shoud be 'A' for all months or positive number (1+).


var postCode = getUrlVars()['postCode'];
var charge = getUrlVars()['charge'];
var time_hr = getUrlVars()['time_hr'];
var optionalVal = decodeURIComponent(getUrlVars()['optionalVal']);
var optionalVal1 = decodeURIComponent(getUrlVars()['optionalVal1']);
var optionalVal2 = decodeURIComponent(getUrlVars()['optionalVal2']);
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
if(!postCode){
	postCode="__";
}
//////////////////////////////////////////////
////////////BUILD CALENDAR////////////////////
//////////////////////////////////////////////


var mdays = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var todayd = new Date();
var dd = todayd.getDate();
var mm = todayd.getMonth()+1; //January is 0!
var yyyy = todayd.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = yyyy+'-'+mm+'-'+dd;
	
build_cal(today, block_today);
getData(today);
function build_cal(date, currentMonth){
	

var cal = "";	

var year = date.substring(0,4),
	month = Number(date.substring(5,7)),
	day = Number(date.substring(8,10)),
	date = new Date(year,month-1,day),
	
	days_in_month = new Date(year,month,0).getDate(),
	title = mdays[month-1],
	//wdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
	//day_of_week = wdays[date.getDay()];
	blank = new Date(year,month-1,1).getDay();
	
	
	
	var ddf = day;
	var mmf = month;
	if(ddf<10) {
    ddf='0'+ddf
	} 
	if(mmf<10) {
    mmf='0'+mmf
	}
	var form_day = year+'-'+mmf+'-'+ddf;
	
	
if(month==12){
	var next_mo_title = mdays[0];
	}else{
	var next_mo_title = mdays[month];
	}

cal = cal+"<table border=0 width='100%' align='middle' bgcolor='#EBEBEB'>";

cal = cal+"<tr><th colspan=2 style='text-align:left;'><div id='prev_mo'></div></th><th colspan=3 style='font-family:Arial;'><font size='5'>"+title+" "+year+"</font></th><th colspan=2 style='text-align:right;'><div id='next_mo'><input type='button' class='monthButton' value='"+next_mo_title+" >>' id='next_mo_b' onclick=next_mo('"+form_day+"') /></div></th></tr>";

cal = cal+"<tr><td width='14%' align='center' class='week_day'>Sunday</td><td width='14%' align='center' class='week_day'>Monday</td><td width='14%' align='center' class='week_day'>Tuesday</td><td width='14%' align='center' class='week_day'>Wednesday</td><td width='14%' align='center' class='week_day'>Thursday</td><td width='14%' align='center' class='week_day'>Friday</td><td width='14%' align='center' class='week_day'>Saturday</td></tr><tr>";

var day_count = 1;

while (blank > 0){ 
cal = cal+"<td></td>";
blank = blank-1; 
day_count++;
} 

for(day_num=1;day_num<=days_in_month;day_num++){
	

if (day>day_num){
cal = cal + "<td width='14%' align='center' align='center' valign='top' bgcolor='#D0D0D0' min-height='100'>";
cal = cal + "<div style='min-height:100px'>";
cal = cal + "<font class='week_day'>";
cal = cal + title;
cal = cal + " ";
cal = cal + day_num;
cal = cal + "</font></div>";
// THIS PART UNCOMMENT IF PREFER TO BLOCK CURRENT DAY

}else if (day==day_num&&currentMonth=='Y'){
cal = cal + "<td width='14%' align='center' align='center' valign='top' bgcolor='#D0D0D0' min-height='100'>";
cal = cal + "<div style='min-height:100px'>";
cal = cal + "<font class='week_day'>";
cal = cal + title;
cal = cal + " ";
cal = cal + day_num;
cal = cal + "</font></div>";
	
// END UNCOMMENT PART IF PREFER TO BLOCK CURRENT DAY
}else{
cal = cal + "<td width='14%' align='center' valign='top' min-height='100'>";
cal = cal + "<div id='" + month + "_" + day_num + "' style='background-color:#FF9999;display:inline-block;height:100%;width:100%;min-height:100px;'>";
cal = cal + "<font class='week_day'>";
cal = cal + title;
cal = cal + " ";
cal = cal + day_num;
cal = cal + "</font>";
cal = cal + "<div id='" + month + "_" + day_num + "AM' style='padding:2px 2px;min-height:30px;'></div>"
cal = cal + "<div id='" + month + "_" + day_num + "PM' style='padding:2px 2px;min-height:30px;'></div>"
cal = cal + "</div>";
}

	
cal = cal + "</td>";
day_count++;
 //Make sure we start a new row every week
if(day_count > 7){
cal = cal + "</tr><tr>";
day_count = 1;
}
}

document.getElementById('calendar').innerHTML = cal;

}











function next_mo(date){
	var year = date.substring(0,4),
	month = Number(date.substring(5,7)),
	day = Number(date.substring(8,10)),
	date = new Date(year,month,01);
	
	if(number_of_months_in_future=='A'){
	future_month(date, month);
	}else{
	var cch_date = new Date();	
	number_of_months = Number(number_of_months_in_future),
	cch_date.setMonth(cch_date.getMonth() + number_of_months);
	if(cch_date){
	if(cch_date<date){
		
//var prev_mo_title = mdays[month-1],
	
	if(month==0){
	var prev_mo_title = mdays[11];
	}else{
	var prev_mo_title = mdays[month-1];
	}
	if(month==12){
	var title = mdays[0];
	}else{
	var title = mdays[month];
	}
	
	
	dd = date.getDate(),
	mm = date.getMonth()+1, //January is 0!
	yyyy = date.getFullYear();
	if(dd<10){dd='0'+dd}
	if(mm<10){mm='0'+mm} 
	new_date = yyyy+'-'+mm+'-'+dd;
	
var cal = "<table border=0 width='100%' align='middle' bgcolor='#EBEBEB'>";
cal = cal+"<tr><th colspan=2 style='text-align:left;'><div id='prev_mo'><input type='button' class='monthButton' value='<< "+prev_mo_title+"' id='next_mo_b' onclick=prev_mo('"+new_date+"') /></div></th><th colspan=3 style='font-family:Arial;'><font size='5'>"+title+" "+year+"</font></th><th colspan=2 style='text-align:right;min-width:100;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></th></tr><tr height=600><td colspan=7  style='text-align:center;font-family:Arial;font-size:16px;font-weight:bold;'>Availability not posted for the month of "+title+".</td></tr></table>";
document.getElementById('calendar').innerHTML = cal;
	}else{
	future_month(date, month);	
	}
	}else{
	future_month(date, month);
	}
		
		
		
		
	}
	
}


function future_month(date, month){
	
	dd = date.getDate(),
	mm = date.getMonth()+1, //January is 0!
	yyyy = date.getFullYear();
	if(dd<10){dd='0'+dd}
	if(mm<10){mm='0'+mm} 
	new_date = yyyy+'-'+mm+'-'+dd;
	build_cal(new_date);
	getData(new_date);
	var prev_mo_title = mdays[month-1];
	var val = "<input type='button' class='monthButton' value='<< "+prev_mo_title+"' id='next_mo_b' onclick=prev_mo('"+new_date+"') />";
	document.getElementById('prev_mo').innerHTML = val;
	
	
}




function prev_mo(date){
	
	var year = date.substring(0,4),
	month = Number(date.substring(5,7)),
	day = Number(date.substring(8,10)),
	date = new Date(year,month-2,01);
	
	var curr_date = new Date();
	if(curr_date>date){
	date = curr_date;
	dd = date.getDate(),
	mm = date.getMonth()+1, //January is 0!
	yyyy = date.getFullYear();
	if(mm==1){
	var prev_mo_title = mdays[11];
	}else{
	var prev_mo_title = mdays[mm-2];
	}
	if(dd<10){dd='0'+dd} 
	if(mm<10){mm='0'+mm} 
	new_date = yyyy+'-'+mm+'-'+dd;
	build_cal(today, block_today);
	getData(new_date);	
	document.getElementById('prev_mo').innerHTML = "";		
	}else{
		
	dd = date.getDate(),
	mm = date.getMonth()+1, //January is 0!
	yyyy = date.getFullYear();
	if(mm==1){
	var prev_mo_title = mdays[11];
	}else{
	var prev_mo_title = mdays[mm-2];
	}
	if(dd<10){dd='0'+dd} 
	if(mm<10){mm='0'+mm} 
	new_date = yyyy+'-'+mm+'-'+dd;
	build_cal(new_date);
	getData(new_date);


var val = "<input type='button' class='monthButton' value='<< "+prev_mo_title+"' id='next_mo_b' onclick=prev_mo('"+new_date+"') />";

document.getElementById('prev_mo').innerHTML = val;
		
	}
	
	
	
	

}


//////////////////////////////////////////////
////////////END BUILD CALENDAR////////////////
//////////////////////////////////////////////

function getData(date){
		document.getElementById('calendar').className += ' overlay';



var xmlhttp = new XMLHttpRequest();
//var url = "http://strictmanager.com/busav/avail.php?id="+businessID+"&date="+date;
var url = "https://www.strictmanager.com/busav/getAvail.php?id="+businessID+"&date="+date+"&postCode="+postCode;

xmlhttp.onreadystatechange = function() {
	   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		
        var myArr = JSON.parse(xmlhttp.responseText);
	    myFunction(myArr);
		document.getElementById('calendar').className -= ' overlay';
    }

}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {
    var out = "";
    var i;
	
    for(i = 0; i < arr.length; i++) {
		
		var av_st = arr[i].av_st,
		z_date = arr[i].z_date,
		hours = arr[i].hours,
		month = Number(z_date.substring(5,7)),
		day = Number(z_date.substring(8,10)),
		houre = arr[i].houre,
		availResvId = arr[i].availResvId,
		AMPM = hours.slice(-2), 
		divID = month + "_" + day + AMPM;
		var insert = document.getElementById(divID);
		if(insert){
		if(hours.charAt(0)=="0"){
		hours = hours.slice(1);
		}
		if(houre.charAt(0)=="0"){
		houre = houre.slice(1);
		}
		
		var butt = "<input type='button' act='Y' class='dayButton' value='"+hours+"-"+houre+"' month='"+month+"' day='"+day+"' id='"+availResvId+"' onclick=click_res('"+availResvId+"') />";
		
		insert.innerHTML = insert.innerHTML + butt;
		var blockID = month + "_" + day;
		document.getElementById(blockID).style.backgroundColor = "#CCFFCC";	
		}
		//style='-moz-border-radius:7px;-webkit-border-radius:7px;border-radius:7px;'
    //out = out + '<a href="' + arr[i].z_date + '">' + arr[i].display + '</a>';
    //document.getElementById("id01").innerHTML = out;
	}
    
	
}


}






function click_res(availResvId){
var dayTime = document.getElementById(availResvId).value;
var month = document.getElementById(availResvId).getAttribute("month");
var day = document.getElementById(availResvId).getAttribute("day");
window.location.href = "http://capitalmaid.com/availForm.html?rsvp="+availResvId+"&dayTime="+dayTime+"&month="+month+"&day="+day+"&charge="+charge+"&time_hr="+time_hr+"&optionalVal="+optionalVal+"&optionalVal1="+optionalVal1+"&optionalVal2="+optionalVal2;
}






