document.getElementById("loader").hidden = true;
//document.getElementById('additional_selections').hidden = true;
// businessID and general calendar settings
var businessID = "6d7519"; // enter businessID provided by StrictManager.com

var quote_values,
preformated_text,
cust_input,
cleantype,
q_text;
var price = 0;
var time = 0;
var xmlhttp = new XMLHttpRequest();
//var url = "http://strictmanager.com/busav/avail.php?id="+businessID+"&date="+date;
var url = "https://www.strictmanager.com/busav/getCleanPrice.php?id="+businessID;

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

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


document.getElementById('res_butt').style.visibility = "hidden"
var postCode = getUrlVars()['postCode'],
optionalVal = getUrlVars()['optionalVal'];
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

var calculated = 0;

function value_changed(){
if(calculated==1){
get_quote()
}
}

function clean_type_changed(){
var add_exist = "N";
var cleantypeCH =document.getElementsByName('cleantype')[0].value;
if(cleantypeCH == "one_time"){
var add_sel_val = "<div style='padding-top:14px; padding-bottom:14px;'><select name='add_sel' id='add_sel' onChange='add_sel_changed()'><option value='N' price='0'>Select Additional</option>";
if(Number(quote_values.o_oven) > 0){
add_sel_val = add_sel_val + "<option value='oven' price="+quote_values.o_oven+">Inside Oven ($"+quote_values.o_oven+")</option>";
add_exist = "Y";
}
if(Number(quote_values.o_fridge) > 0){
add_sel_val = add_sel_val + "<option value='fridge' price='"+quote_values.o_fridge+"'>Inside Refrigerator ($"+quote_values.o_fridge+")</option>";
add_exist = "Y";
}
add_sel_val = add_sel_val +"</select></div>";
}
if(add_exist == "Y"){
document.getElementById('additional_selections').hidden = false;
document.getElementById('add_options').innerHTML = add_sel_val;
}else{
document.getElementById('additional_selections').hidden = true;
}

value_changed();
}

function add_sel_changed(){
var add_sel = document.getElementsByName('add_sel')[0].value;
if(add_sel != "N"){
var priceEle = document.getElementsByName("add_sel")[0];
var add_price = priceEle.options[priceEle.selectedIndex].getAttribute("price");
var prev_sel = document.getElementById('add_selected_list').innerHTML;
var full_name = add_sel;
if(add_sel == 'oven'){
full_name = "Inside Oven";
}else if(add_sel == 'fridge'){
full_name = "Inside Refrigerator";
}
var add_selected = prev_sel + "<div name='"+add_sel+"' price='"+add_price+"'>"+full_name+" ($"+add_price+") <span onclick=remove_add_list('"+add_sel+"') style='text-decoration:underline;cursor:pointer;'>(remove)</span></div>";

document.getElementById('add_selected_list').innerHTML = add_selected;
document.getElementById('add_selected').hidden = false;
document.getElementsByName("add_sel")[0].value = 'N';
}
value_changed();
}

function remove_add_list(val){

var prev_sel = document.getElementById('add_selected_list');
var divs = prev_sel.getElementsByTagName("div");
var removed = "N";//to remove only one if more selected.
for(var i = 0; i < divs.length; i++){
if(removed == "N"){
var to_remove = divs[i].getAttribute("name");
if(val == to_remove){
divs[i].remove();
removed = "Y";
}
}
}
value_changed();

}

function get_quote(){

document.getElementById('quote_results').innerHTML = "";
document.getElementById('recurring_quote_results').innerHTML = "";
document.getElementById("error_msg").innerHTML = "";
document.getElementById("error_specific").innerHTML = "";
document.getElementById('res_butt').style.visibility = "hidden";
document.getElementsByName('cleantype')[0].className -= ' backRed';
document.getElementsByName('residence')[0].className -= ' backRed';
document.getElementsByName('sq_footage')[0].className -= ' backRed';
document.getElementsByName('bathrooms')[0].className -= ' backRed';
document.getElementsByName('bedrooms')[0].className -= ' backRed';
document.getElementsByName('pets')[0].className -= ' backRed';

var cleantype =document.getElementsByName('cleantype')[0].value,
residence=document.getElementsByName('residence')[0].value,
sq_footages=document.getElementsByName('sq_footage')[0].value,
sq_footage = Number(sq_footages);
bathroomss=document.getElementsByName('bathrooms')[0].value,
bathrooms=Number(bathroomss),
bedroomss=document.getElementsByName('bedrooms')[0].value,
bedrooms=Number(bedroomss),
pets=document.getElementsByName('pets')[0].value;
cust_input = "Clean type: "+cleantype+"; Residence: "+residence+"; Sq. Footage: " + sq_footages + "; Bathrooms: " + bathroomss + "; Bedrooms: " + bedroomss + "; Pets: " + pets;
var bVal = 0;
if(cleantype=="0"){
document.getElementsByName('cleantype')[0].className += ' backRed';
bVal = 1;
}
if(residence=="0"){
document.getElementsByName('residence')[0].className += ' backRed';
bVal = 1;
}
if(sq_footage==0){
document.getElementsByName('sq_footage')[0].className += ' backRed';
bVal = 1;
}
if(bathrooms==0){
document.getElementsByName('bathrooms')[0].className += ' backRed';
bVal = 1;
}
if(bedroomss=='none'){
document.getElementsByName('bedrooms')[0].className += ' backRed';
bVal = 1;
}
if(pets=='0'){
document.getElementsByName('pets')[0].className += ' backRed';
bVal = 1;
}

if(residence=='apartment'){
if(bedrooms > 3){
document.getElementsByName("residence")[0].value = 'house';
residence = 'house';
document.getElementById("error_specific").innerHTML = "Too many bedrooms for regular apartment pricing. Residence was automatically converted to house pricing.";
}
if(bathrooms > 3){
document.getElementsByName("residence")[0].value = 'house';
residence = 'house';
document.getElementById("error_specific").innerHTML = "Too many bathrooms for regular apartment pricing. Residence was automatically converted to house pricing.";
}
if(sq_footage > 2500){
document.getElementsByName("residence")[0].value = 'house';
residence = 'house';
document.getElementById("error_specific").innerHTML = "Sq footage is too large for regular apartment pricing. Residence was automatically converted to house pricing.";
}
if(sq_footage == 700 && (bedrooms == 3 || bathrooms == 3)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for 3 bed/bath apartment. Please select higher square footage.";
bVal = 1;
}
if(bedrooms == 0 && sq_footage > 1400){
document.getElementsByName('bedrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too large for studio. Please select higher number of bedrooms.";
bVal = 1;
}

}else if(residence=='house'){

if(bedrooms == 0){
document.getElementsByName('bedrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "House should have more than 0 bedrooms.";
bVal = 1;
}

if(bedrooms == 1 && sq_footage > 3500){
document.getElementsByName('bedrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too large for 1 bedroom. Please select higher number of bedrooms.";
bVal = 1;
}
if(bathrooms == 1 && sq_footage > 3500){
document.getElementsByName('bathrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too large for 1 bathroom. Please select higher number of bathrooms.";
bVal = 1;
}
if(bedrooms == 2 && sq_footage > 5000){
document.getElementsByName('bedrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too large for 2 bedrooms. Please select higher number of bedrooms.";
bVal = 1;
}
if(bathrooms == 2 && sq_footage > 5000){
document.getElementsByName('bathrooms')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too large for 2 bathrooms. Please select higher number of bathrooms.";
bVal = 1;
}

if(sq_footage == 700 && (bedrooms > 2 || bathrooms > 2)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for number of bedrooms or bathrooms. Please select higher square footage.";
bVal = 1;
}
if(sq_footage == 1000 && (bedrooms > 3 || bathrooms > 3)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for number of bedrooms or bathrooms. Please select higher square footage.";
bVal = 1;
}
if(sq_footage == 1400 && (bedrooms > 4 || bathrooms > 4)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for number of bedrooms or bathrooms. Please select higher square footage.";
bVal = 1;
}
if(sq_footage == 1900 && (bedrooms > 5 || bathrooms > 5)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for number of bedrooms or bathrooms. Please select higher square footage.";
bVal = 1;
}
if(sq_footage == 2500 && (bedrooms > 6 || bathrooms > 6)){
document.getElementsByName('sq_footage')[0].className += ' backRed';
document.getElementById("error_specific").innerHTML = "Square footage is too small for number of bedrooms or bathrooms. Please select higher square footage.";
bVal = 1;
}

}



if(bVal==0){

price_text_short = "";
calculated = 1;

document.getElementById('res_butt').style.visibility = "visible";

if(cleantype=="one_time" || cleantype=="recurring"){

var one_price_1, one_price_2;


if(residence=='apartment'){
one_price_1 = "oa_"+bedroomss+"b_"+sq_footages;
one_price_2 = "oa_"+bathroomss+"b_"+sq_footages;
}else{
one_price_1 = "oh_"+bedroomss+"b_"+sq_footages;
one_price_2 = "oh_"+bathroomss+"b_"+sq_footages;
}

var one_price_1_val = Number(quote_values[one_price_1]),
one_price_2_val = Number(quote_values[one_price_2]);
price = (one_price_1_val + one_price_2_val)/2;
if(pets!='N'){
if(residence=='apartment'){
one_pets = "oa_p_"+sq_footages;
}else{
one_pets = "oh_p_"+sq_footages;
}
var pets_add_price = Number(quote_values[one_pets]);
price = price + pets_add_price; 
}

var prev_sel = document.getElementById('add_selected_list');
var divs = prev_sel.getElementsByTagName("div");
var additional_items = "";
for(var i = 0; i < divs.length; i++){
var name = divs[i].getAttribute("name");
var add_price = divs[i].getAttribute("price");
if(Number(add_price)>0){
price = price + Number(add_price);
additional_items = additional_items + " "+name+"($"+add_price+")/";
}
}
if(additional_items.length > 1){
additional_items = " Additional Items: "+additional_items;
}

var o_base = Number(quote_values.o_base),
o_hour = Number(quote_values.o_hour);
time = (price - o_base) / o_hour;
price = 5 * Math.round(price/5);
time1 = .5*Math.round(time/.5);

cust_input = cust_input + additional_items + "; Total hours: " + time1.toString();

var text_o, text_s, o_e = 0;

if(cleantype=="recurring"){
text_o = "<strong>First Time Cleaning Price: $"+price+"</strong><br>";
}else{
text_o = "<strong>Price: $"+price+"</strong><br>";
}

text_s = "Price: $"+price+" ";

if(preformated_text.show_1_emp=="Y"){
text_o = text_o + "for up to "+time+" hours for 1 person crew<br>"
o_e = 1;
}

if(preformated_text.show_2_emp=="Y"){
var time2 = time/2;
time2 = .25*Math.round(time2/.25);
var time2text,
time2text_short;

if((time2/.5)% 1 == 0){
time2text = time2.toString() + " hours";
time2text_short = time2.toString() + "hr";
}else{
time2h = Math.floor(time2);
time2m = time2 - time2h;
// here we round up 15 minutes to 30 minutes and 45 minutes to an hour.
if(time2m == .25){var minute_text = " 30 minutes"; time2text = time2h.toString()+ " hours" + minute_text;
time2text_short = time2h.toString()+ "hr 30min";
}
if(time2m == .75){time2h = time2h + 1; time2text = time2h.toString()+ " hours";
time2text_short = time2h.toString()+ "hr";
}
}
text_o = text_o + "for up to "+time2text+" for 2 person crew<br>"
text_s = text_s + "for up to "+time2text+" for 2 person crew; "
o_e = 1;
}

if(preformated_text.show_3_emp=="Y"){
var time3 = time/3;
time3 = .25*Math.round(time3/.25);
var time3text,
time3text_short;
if((time3/.5)% 1 == 0){
time3text = time3.toString() + " hours";
time3text_short = time3.toString()+ "hr";
}else{
var time3h = Math.floor(time3);
time3m = time3 - time3h;
var minute_text,
min_text_short;
// here we round up 15 minutes to 30 minutes and 45 minutes to an hour.
if(time3m == .25){ minute_text = " 15 minutes"; min_text_short = " 15min";}
if(time3m == .75){ minute_text = " 45 minutes"; min_text_short = " 45min";}
time3text = time3h.toString()+ " hours" + minute_text;
time3text_short = time3h.toString()+ "hr" + min_text_short;
}
text_o = text_o + "for up to "+time3text+" for 3 person crew<br>"
text_s = text_s + "for up to "+time3text+" for 3 person crew; "
o_e = 1;
}

if(preformated_text.show_4_emp=="Y"){
var time4 = time/4;
time4 = .25*Math.ceil(time4/.25);
var time4text,
time4text_short;
if((time4/.5)% 1 == 0){
time4text = time4.toString() + " hours";
time4text_short = time4.toString()+ "hr";
}else{
time4h = Math.floor(time4);
time4m = time4 - time4h;
var minute_text,
min_text_short;
// here we round up 15 minutes to 30 minutes and 45 minutes to an hour.
if(time4m == .25){ minute_text = " 15 minutes"; min_text_short = " 15min";}
if(time4m == .75){ minute_text = " 45 minutes"; min_text_short = " 45min";}
time4text = time4h.toString()+ " hours" + minute_text;
time4text_short = time4h.toString()+ "hr" + minute_text_short;
}
text_o = text_o + "for up to "+time4text+" for 4 person crew<br>"
text_s = text_s + "for up to "+time4text+" for 4 person crew; "
o_e = 1;
}
if(o_e==0){
text_o = text_o + "for up to "+time+" hours for 1 person crew<br>"
text_s = text_s + "for up to "+time+" hours for 1 person crew. "
}
q_text = text_s;

text_o = text_o + preformated_text.web_after_one;

document.getElementById('quote_results').innerHTML = text_o;

if(cleantype=="recurring"){
//<div id="recurring_quote_results"></div>

//////////////////////////////////////////////////////////////
/////////////////recurring calculations///////////////////////
//////////////////////////////////////////////////////////////

var rec_price_1, rec_price_2;

if(residence=='apartment'){
rec_price_1 = "ra_"+bedroomss+"b_"+sq_footages;
rec_price_2 = "ra_"+bathroomss+"b_"+sq_footages;
}else{
rec_price_1 = "rh_"+bedroomss+"b_"+sq_footages;
rec_price_2 = "rh_"+bathroomss+"b_"+sq_footages;
}

var rec_price_1_val = Number(quote_values[rec_price_1]),
rec_price_2_val = Number(quote_values[rec_price_2]);
var rec_price = (rec_price_1_val + rec_price_2_val)/2;
if(pets!='N'){
if(residence=='apartment'){
rec_pets = "ra_p_"+sq_footages;
}else{
rec_pets = "rh_p_"+sq_footages;
}
var rec_pets_add_price = Number(quote_values[rec_pets]);
rec_price = rec_price + rec_pets_add_price; 
}

var price_bi = rec_price,
price_w = price_bi * Number(quote_values.r_weekly),
price_mo = price_bi * Number(quote_values.r_monthly);

price_w = 5 * Math.round(price_w/5);
price_bi = 5 * Math.round(price_bi/5);
price_mo = 5 * Math.round(price_mo/5);
var text_r;
text_r = "<br>Weekly Price: $" + price_w+"<br>Bi-weekly Price: $" + price_bi+ "<br>Monthly Price: $" + price_mo+"<br>";
text_r = text_r + preformated_text.web_after_rec;

document.getElementById('recurring_quote_results').innerHTML = text_r;


//////////////////////////////////////////////////////////////
/////////////////end recurring calculations///////////////////
//////////////////////////////////////////////////////////////


}// end recurring
}else if(cleantype=="move_in_out"){

var move_price_1, move_price_2;

if(residence=='apartment'){
move_price_1 = "ma_"+bedroomss+"b_"+sq_footages;
move_price_2 = "ma_"+bathroomss+"b_"+sq_footages;
}else{
move_price_1 = "mh_"+bedroomss+"b_"+sq_footages;
move_price_2 = "mh_"+bathroomss+"b_"+sq_footages;
}

var move_price_1_val = Number(quote_values[move_price_1]),
move_price_2_val = Number(quote_values[move_price_2]);
var move_price = (move_price_1_val + move_price_2_val)/2;

var m_base = Number(quote_values.m_base),
m_hour = Number(quote_values.m_hour);
time = (move_price - m_base) / m_hour;

price = 5 * Math.round(move_price/5);
time1 = .5*Math.round(time/.5);

cust_input = cust_input + "; Total hours: " + time1.toString();

var text_m, text_s, o_e = 0;
text_m = "<strong>Price: $"+price+"</strong><br>";
text_s = "Price: $"+price+" ";
if(preformated_text.show_1_emp=="Y"){
text_m = text_m + "for up to "+time+" hours for 1 person crew<br>"
text_s = text_s + "for up to "+time+" hours for 1 person crew; "
o_e = 1;
}
if(preformated_text.show_2_emp=="Y"){
var time2 = time/2;
time2 = .25*Math.round(time2/.25);
var time2text;
if((time2/.5)% 1 == 0){
time2text = time2.toString() + " hours";
}else{
time2h = Math.floor(time2);
time2m = time2 - time2h;
if(time2m == .25){var minute_text = " 30 minutes"; time2text = time2h.toString()+ " hours" + minute_text;}
if(time2m == .75){time2h = time2h + 1; time2text = time2h.toString()+ " hours";}
}
text_m = text_m + "for up to "+time2text+" for 2 person crew<br>"
text_s = text_s + "for up to "+time2text+" for 2 person crew; "
o_e = 1;
}
if(preformated_text.show_3_emp=="Y"){
var time3 = time/3;
time3 = .25*Math.round(time3/.25);
var time3text; 
if((time3/.5)% 1 == 0){
time3text = time3.toString() + " hours";
}else{
var time3h = Math.floor(time3);
time3m = time3 - time3h;
var minute_text,
min_text_short;
// here we round up 15 minutes to 30 minutes and 45 minutes to an hour.
if(time3m == .25){ minute_text = " 15 minutes"; min_text_short = " 15min";}
if(time3m == .75){ minute_text = " 45 minutes"; min_text_short = " 45min";}
time3text = time3h.toString()+ " hours" + minute_text;
}
text_m = text_m + "for up to "+time3text+" for 3 person crew<br>"
text_s = text_s + "for up to "+time3text+" for 3 person crew; "
o_e = 1;
}
if(preformated_text.show_4_emp=="Y"){
var time4 = time/4;
time4 = .25*Math.ceil(time4/.25);
var time4text,
time4text_short;
if((time4/.5)% 1 == 0){
time4text = time4.toString() + " hours";
time4text_short = time4.toString()+ "hr";
}else{
time4h = Math.floor(time4);
time4m = time4 - time4h;
var minute_text,
min_text_short;
// here we round up 15 minutes to 30 minutes and 45 minutes to an hour.
if(time4m == .25){ minute_text = " 15 minutes"; min_text_short = " 15min";}
if(time4m == .75){ minute_text = " 45 minutes"; min_text_short = " 45min";}
time4text = time4h.toString()+ " hours" + minute_text;
}
text_m = text_m + "for up to "+time4text+" for 4 person crew<br>"
text_s = text_s + "for up to "+time4text+" for 4 person crew; "
o_e = 1;
}
if(o_e==0){
text_m = text_m + "for up to "+time+" hours for 1 person crew<br>"
text_s = text_s + "for up to "+time+" hours for 1 person crew. "
}
q_text = text_s;

text_m = text_m + preformated_text.web_after_move;
document.getElementById('quote_results').innerHTML = text_m;


}// end move_in_out
}else{
document.getElementById("error_msg").innerHTML = "Fields marked in red are incorrect!";
}


}// end get_quote() function



function start_reservation(){
cust_input = cust_input + "; Price: " + price;
window.location.href = "/availDates.html?postCode="+postCode+"&charge="+price+"&time_hr="+time1+"&optionalVal="+encodeURIComponent(cust_input)+"&optionalVal1="+encodeURIComponent(cleantype)+"&optionalVal2="+encodeURIComponent(q_text);
}