// businessID and general calendar settings
var businessID = "6d7519"; // eneter busnessID provided by StrictManager.com

var rsvp = getUrlVars()['rsvp'],
	dayTime = getUrlVars()['dayTime'],
	monthNo = Number(getUrlVars()['month'])-1,
	mdays = ['January','February','March','April','May','June','July','August','September','October','November','December'],
	month = mdays[monthNo],
	day = getUrlVars()['day'],
	charge = getUrlVars()['charge'],
	time_hr = getUrlVars()['time_hr'],
	optionalVal = decodeURIComponent(getUrlVars()['optionalVal']);
	optionalVal1 = decodeURIComponent(getUrlVars()['optionalVal1']);
	optionalVal2 = decodeURIComponent(getUrlVars()['optionalVal2']);
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

var dayText = month+" "+day+", arrival "+dayTime;
//document.getElementById("dayTextTop").innerHTML = dayText;
document.getElementById("dayText").innerHTML = dayText;




var firstname=document.getElementsByName('firstname')[0].value,
lastname=document.getElementsByName('lastname')[0].value,
address1=document.getElementsByName('address1')[0].value,
address2=document.getElementsByName('address2')[0].value,
city=document.getElementsByName('city')[0].value,
state=document.getElementsByName('state')[0].value,
phone_area=document.getElementsByName('phone_area')[0].value,
phone_prefix=document.getElementsByName('phone_prefix')[0].value,
phone_suffix=document.getElementsByName('phone_suffix')[0].value,
email=document.getElementsByName('email')[0].value,
how_did_you_hear=document.getElementsByName('how_did_you_hear')[0].value,
promo=document.getElementsByName('promo')[0].value,
message=document.getElementsByName('message')[0].value;


function reserve(){
document.getElementById("error_msg").innerHTML = "";
document.getElementsByName('firstname')[0].className -= ' backRed';
document.getElementsByName('lastname')[0].className -= ' backRed';
document.getElementsByName('address1')[0].className -= ' backRed';
document.getElementsByName('city')[0].className -= ' backRed';
document.getElementsByName('state')[0].className -= ' backRed';
document.getElementsByName('postCode')[0].className -= ' backRed';
document.getElementsByName('phone_area')[0].className -= ' backRed';
document.getElementsByName('phone_prefix')[0].className -= ' backRed';
document.getElementsByName('phone_suffix')[0].className -= ' backRed';
document.getElementsByName('email')[0].className -= ' backRed';


var firstname=document.getElementsByName('firstname')[0].value,
firstname=firstname.trim(),
lastname=document.getElementsByName('lastname')[0].value,
lastname=lastname.trim(),
address1=document.getElementsByName('address1')[0].value,
address1=address1.trim(),
address2=document.getElementsByName('address2')[0].value,
address2=address2.trim(),
city=document.getElementsByName('city')[0].value,
city=city.trim(),
state=document.getElementsByName('state')[0].value,
state=state.trim(),
postCode=document.getElementsByName('postCode')[0].value,
postCode=postCode.trim(),
phone_area=document.getElementsByName('phone_area')[0].value,
phone_area=phone_area.trim(),
phone_prefix=document.getElementsByName('phone_prefix')[0].value,
phone_prefix=phone_prefix.trim(),
phone_suffix=document.getElementsByName('phone_suffix')[0].value,
phone_suffix=phone_suffix.trim(),
phone = phone_area + phone_prefix + phone_suffix,
email=document.getElementsByName('email')[0].value,
email=email.trim(),
how_did_you_hear=document.getElementsByName('how_did_you_hear')[0].value,
promo=document.getElementsByName('promo')[0].value,
message=document.getElementsByName('message')[0].value;

var bVal = 0;
if(firstname.length<1){
document.getElementsByName('firstname')[0].className += ' backRed';
bVal = 1;
}
if(lastname.length<1){
document.getElementsByName('lastname')[0].className += ' backRed';
bVal = 1;
}
if(address1.length<3){
document.getElementsByName('address1')[0].className += ' backRed';
bVal = 1;
}
if(city.length<3){
document.getElementsByName('city')[0].className += ' backRed';
bVal = 1;
}
if(state.length<2){
document.getElementsByName('state')[0].className += ' backRed';
bVal = 1;
}
if(postCode.length<1){
document.getElementsByName('postCode')[0].className += ' backRed';
bVal = 1;
}
if(phone_area.length<1){
document.getElementsByName('phone_area')[0].className += ' backRed';
bVal = 1;
}
if(phone_prefix.length<1){
document.getElementsByName('phone_prefix')[0].className += ' backRed';
bVal = 1;
}
if(phone_suffix.length<1){
document.getElementsByName('phone_suffix')[0].className += ' backRed';
bVal = 1;
}
if(email.length<5){
document.getElementsByName('email')[0].className += ' backRed';
bVal = 1;
}
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if(!(re.test(email))){document.getElementsByName('email')[0].className += ' backRed';bVal = 1;}
   



if(bVal==0){
	
	var xmlhttp = new XMLHttpRequest();
//var url = "http://strictmanager.com/busav/avail.php?id="+businessID+"&date="+date;
var url = "https://www.strictmanager.com/busav/postRsvp.php";
xmlhttp.onreadystatechange = function() {
	   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		 //  alert(xmlhttp.responseText);
		var myArr = JSON.parse(xmlhttp.responseText);
		
		//alert(myArr);
		
		if(myArr=='expired'){
	    var resv_comp = "<br><strong>Unfortunatly this opening is not available anymore!</strong><br><a href='availPostCode.php'>Start from the beginning</a> and select a new reservation time."
			document.getElementById("main_body_div").innerHTML = resv_comp;
		
		}else if(Number(myArr) > 0){
			var resv_comp = "<br><strong>Thank you for requesting a service.<br>The service has not been scheduled yet.<br> Our representative will contact you within 24 hours by email or phone to confirm the reservation.</strong>"
			document.getElementById("main_body_div").innerHTML = resv_comp;
		}else{
			var resv_comp = "<br><strong>Unfortunatly there was a problem requesting reservation.</strong><br>Call/Email the company or try to make the reservation again."
			document.getElementById("main_body_div").innerHTML = resv_comp;
		}
    }

}
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

var cust_ref = "How did your hear: "+ how_did_you_hear;

message = "CUSTOMER SELECTIONS: "+ optionalVal + "\r\nPromo: "+ promo+"  \r\nComments: "+message;


xmlhttp.send("id="+encodeURIComponent(businessID)+"&rsvp="+encodeURIComponent(rsvp)+"&firstname="+encodeURIComponent(firstname)+"&lastname="+encodeURIComponent(lastname)+"&address1="+encodeURIComponent(address1)+"&address2="+encodeURIComponent(address2)+"&city="+encodeURIComponent(city)+"&state="+encodeURIComponent(state)+"&postCode="+encodeURIComponent(postCode)+"&phone="+encodeURIComponent(phone)+"&email="+encodeURIComponent(email)+"&cust_ref="+encodeURIComponent(cust_ref)+"&message="+encodeURIComponent(message)+"&charge="+encodeURIComponent(charge)+"&time_hr="+encodeURIComponent(time_hr)+"&optionalVal1="+encodeURIComponent(optionalVal1)+"&optionalVal2="+encodeURIComponent(optionalVal2)+"&optionalVal3="+encodeURIComponent(dayTime)+"&dayText="+encodeURIComponent(dayText));
	
	
}else{
document.getElementById("error_msg").innerHTML = "Fields marked in red are incorrect!";
}
	
	
}