// AJAX by fernyb
function ajaxObject() {
	var ab;
	if (window.XMLHttpRequest) {
		ab = new XMLHttpRequest(); // For Mozilla browsers
	} else if (window.ActiveXObject) {
		ab = new ActiveXObject("Microsoft.XMLHTTP"); // For IE
		if(!ab) {
			ab = new ActiveXObject("Msxml2.XMLHTTP"); // For IE
		}
	} else {
		return false;
	}
	return ab;
}

var httpAjax;
httpAjax = ajaxObject();

function getPage(page) {
	requestPage = "request.page.php?page="+page;
	httpAjax.open("GET", requestPage, true);
	httpAjax.onreadystatechange = handlePageResponse;
	httpAjax.send(null);
}

function handlePageResponse() {
	if(httpAjax.readyState == 4) {
		if(httpAjax.status == 200) {
			document.getElementById("dynamicContent").innerHTML = httpAjax.responseText;
		} else {
			document.getElementById("dynamicContent").innerHTML = "Sorry I could not retrive the content.";
		}
	} else {
		document.getElementById("dynamicContent").innerHTML = "Loading Content...";	
	}
}

function validateForm() {
	f = document.spacontact;
	if(f.firstname.value == "") {
		window.alert("Please fill in your name");
		return false;
	}
	if(f.lastname.value == "") {
			window.alert("please fill in your last name");
			return false;
	}
	if(f.phonenumber.value == "") {
			window.alert("please enter your phone number");
			return false;
	}
	if(f.email.value == "") {
			window.alert("please enter your email address");
			return false;
	}
	if(f.appointment.value == "") {
			window.alert("Please enter the appointment time");
			return false;
	}
	if(f.comments.value == "") {
			window.alert("Please enter your comments");
			return false;
	}
	
	return true;
}


function validateForm2() {
	return true;	
}

function getEmailParts() {
	if(validateForm() == true) {
		d = document.getElementById("mailResponse");
		d.innerHTML = "<strong>Please wait... Processing</strong>";
		f = document.spacontact;
		query = "firstname="+  encodeURIComponent(f.firstname.value);
		query += "&lastname="+ encodeURIComponent(f.lastname.value);
		query += "&email="+ encodeURIComponent(f.email.value);
		query += "&service="+ encodeURIComponent(f.service.value);
		query += "&monthApp="+ encodeURIComponent(f.monthApp.value);
		query += "&dayApp="+ encodeURIComponent(f.dayApp.value);
		query += "&phonenumber="+ encodeURIComponent(f.phonenumber.value);
		query += "&yearApp="+ encodeURIComponent(f.yearApp.value);
		query += "&appointment="+ encodeURIComponent(f.appointment.value);
		query += "&comments="+ encodeURIComponent(f.comments.value);
		sendEmail(query);
	}
}

/* GET VALUES FROM THE GIFT CERTIFICATE PAGE */

function gatherInfo(wform) {
var sndstr;
var f = eval("document."+wform);
for(var x=0; x<parseInt(f.elements.length); x++) {
    if(x<1) {
        sndstr = f.elements[x].name+"="+ encodeURIComponent(f.elements[x].value); 
    } else {
        sndstr += "&"+f.elements[x].name+"="+ encodeURIComponent(f.elements[x].value);     
    }
}
	// sndReq(sndstr); // send the string
	return sndstr;
}
/* VALIDATE GIFT CERTIFICATE FORM */
function validateGiftCerifiate() {
	g = document.giftCertificate;
	// SHIPPING FIELD REQUIRED FIELDS
	if(g.shipFirstname.value == "" || g.shipFirstname.value.length < 2) {
		window.alert("Please fill in the shipping first name field");
		return false;
	}
	if(g.shipLastname.value == "" || g.shipLastname.value.length < 2) {
		window.alert("Please fill in the shipping last name field");
		return false;
	}
	if(g.shipAddress.value == "" || g.shipAddress.value.length < 5) {
		window.alert("Please fill in the Shipping Address field");
		return false;
	}
	if(g.shipCity.value == "" || g.shipCity.value.length < 4) {
		window.alert("Please fill in the Shipping City");
		return false;
	}
	if(g.shipState.value == "" || g.shipState.value.length < 2) {
		window.alert("Please fill in the Shipping State");
		return false;
	}
	if(g.shipZip.value == "" || g.shipZip.value.length < 5) {
		window.alert("Please fill in the shipping zipcode field");
		return false;
	}
	if(g.shipPhone.value == "" || g.shipPhone.value.length < 10) {
		window.alert("Please fill in the shipping phone number field");
		return false;
	}
	if(g.shipEmail.value == "" || g.shipEmail.value.length < 5) {
		window.alert("Please fill in the shipping email field");
		return false;
	}
	// BILLING FIELDS REQUIRED
	if(g.payment.value == "") {
		window.alert("Please select a payment method");
		return false;
	}
	if(g.billFirstname.value == "" || g.billFirstname.value.length < 2) {
		window.alert("Please enter the Billing first name");
		return false;
	}
	if(g.billLastname.value == "" || g.billLastname.value.length < 2) {
		window.alert("Please enter billing last name");
		return false;
	}
	if(g.billAddress.value == "" || g.billAddress.value.length < 5) {
		window.alert("Please enter the billing address");
		return false;
	}
	if(g.billCity.value == "" || g.billCity.value.length < 4) {
		window.alert("Please enter the billing city name");
		return false;
	}
	if(g.billZipcode.value == "" || g.billZipcode.value.length < 5) {
		window.alert("Please enter the billing zipcode");
		return false;
	}
	if(g.billPhonenumber.value == "" || g.billPhonenumber < 10) {
		window.alert("Please enter the billing phone number");
		return false;
	}
	if(g.billEmail.value == "" || g.billEmail.value.length < 5) {
		window.alert("Please enter the Billing Email Address");
		return false;
	}
	if(g.billState.value == "") {
		window.alert("Please choose a Billing state");
		return false;
	}
	if(g.billCreditCardNumber.value == "" || g.billCreditCardNumber.value.length < 10) {
		window.alert("Please enter your Credit Card number");
		return false;
	}
	if(g.billExpMonth.value == "Month") {
		window.alert("Please enter the expiration month for the credit card");
		return false;
	}
	if(g.billExpYear.value == "Year") {
		window.alert("Please enter the expiration year for the credit card");
		return false;
	}
	if(g.billCCV.value == "" || g.billCCV.value.length < 3) {
		window.alert("Please enter the CCV number for the credit card");
		return false;
	}
	return true; // ALL FIELDS WERE FILLED AND WE ALLOW IT TO SUBMIT
}	
/* SEND OUT GIFT CETIFICATE */

function getGiftCertificateInformation() {
	/* WE FIRST VALIDATE THE FORM TO CHECK IF ALL FIEDS ARE FILLED IN */
	if(validateGiftCerifiate() == true) {
		gc = gatherInfo("giftCertificate");
		sendGiftCertificateOrder(gc);
	}
}
function sendGiftCertificateOrder(query) {
	httpAjax.onreadystatechange = handleGiftOrder;
	httpAjax.open("post", "mail.cerificate.php", true);
	httpAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	httpAjax.send(query); /* IF WE ARE NOT SENDING ANYTHING WE SEND null */
}
function handleGiftOrder() { // WE NEED TO FIX THIS PIECE
	d = document.getElementById("mailResponse");
	if(httpAjax.readyState == 4) {
		d.style.visibility = "visible";
		if(httpAjax.status == 200) {
			d.innerHTML = httpAjax.responseText;
			document.getElementById("formCertFields").style.visibility = "hidden";
			document.body.scrollTop = 0;
		} else {
		/*	d.innerHTML = "<strong>Sorry we could not send your message at this moment please try again later. Thank You.</strong>";
			*/
			document.getElementById("formCertFields").style.visibility = "hidden";
			document.body.scrollTop = 0;
		}
	}	
}


/* POST */
function sendEmail(query) {
	httpAjax.onreadystatechange = handleMailResponse;
	httpAjax.open("post", "send.mail.php", true);
	httpAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	httpAjax.send(query); /* IF WE ARE NOT SENDING ANYTHING WE SEND null */
}

function handleMailResponse() {
d = document.getElementById("mailResponse");
	if(httpAjax.readyState == 4) {
		d.style.visibility = "visible";
		if(httpAjax.status == 200) {
			d.innerHTML = httpAjax.responseText;
			clearFormBox();
			document.body.scrollTop = 0;
		} else {
			d.innerHTML = "<strong>Sorry we could not send your message at this moment please try again later. Thank You.</strong>";
			document.body.scrollTop = 0;
		}
	}
}

function clearFormBox() {
	f = document.spacontact;
	f.firstname.value = "";
	f.lastname.value = "";
	f.phonenumber.value = "";
	f.email.value = "";
	f.appointment.value = "";
	f.comments.value = "";
}


//------------------------------
