loadRandomHeader();
loadRotatingBanner();
startPopulatingStores();

function startPopulatingStores() {
	if(window.stores === undefined) {
		var xhr = createServiceXHR("GET", "locations");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
					stores = JSON.parse(xhr.responseText);
					var sel = document.getElementById("store");
					var opt = sel.firstChild;
					sel.removeChild(opt);
					for(var i = 0; i < stores.length; i++) {
						var newOpt = opt.cloneNode(true);
						newOpt.name = stores[i].store;
						newOpt.firstChild.nodeValue = stores[i].store;
						sel.appendChild(newOpt);
					}
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
			}
		};
		xhr.send();
	}
}

function hideShowStep(stepA, stepB) {
	var errors = errorsFor(stepA);
	if(errors != "") {
		alert("Please correct the following problems before continuing to the next step:\n\n" + errors);
	} else {
		document.getElementById(stepA).className = "step invisible";
		document.getElementById(stepB).className = "step";
	}
}

function errorsFor(step) {
	var inq = createInquiryObject();
	var errors = "";
	switch(step) {
		case "step1":
			if(inq.firstName == "") {
				errors += "- First name is required.\n";
			}
			if(inq.lastName == "") {
				errors += "- Last name is required.\n";
			}
			if(inq.email == "") {
				errors += "- An e-mail address is required.\n";
			}
			if(document.getElementById("email2").value == "") {
				errors += "- An e-mail address confirmation is required.\n";
			}
			if(inq.email !== document.getElementById("email2").value) {
				errors += "- E-Mail and e-mail confirmation must match.\n";
			}
		break;
		case "step2":
		break;
		case "step3":
		break;
		case "step4":
			if(!inq.birthDate.match(/(^$)|(\d?\d\/\d?\d\/\d\d\d\d)/)) {
				errors += "- Birth date format: d/m/YYYY (Example: 30/12/1987).\n";
			}
			if(!inq.postalCode.match(/(^$)|([A-Za-z]\d[A-Za-z](\s)?\d[A-Za-z]\d)/)) {
				errors += "- Postal code should be in the X0X 0X0 format where X is any letter and 0 is any digit.\n";
			}
		break;
	}
	return errors;
}

function createInquiryObject() {
	var inq = {
		type: "newsletter",
		firstName: document.getElementById("firstName").value,
		lastName: document.getElementById("lastName").value,
		address: document.getElementById("address").value,
		city: document.getElementById("city").value,
		province: document.getElementById("province").value,
		postalCode: document.getElementById("postalCode").value,
		birthDate: document.getElementById("birthDate").value,
		gender: document.getElementById("genderMale").checked? "Male": document.getElementById("genderFemale").checked? "Female": null,
		email: document.getElementById("email").value,
		phone: document.getElementById("phone").value,
		contactBy: document.getElementById("contactByPhone").checked? "phone": document.getElementById("contactByPhone").checked? "email": null,
		newsletter: document.getElementById("newsletter").checked,
		appointment: document.getElementById("appointment").checked,
		garment1Type: null,
		garment1: null,
		garment2Type: null,
		garment2: null,
		bottoms: document.getElementById("bottoms").checked,
		tops: document.getElementById("tops").checked,
		jackets: document.getElementById("jackets").checked,
		outfit: document.getElementById("outfit").checked,
		bottomSize: document.getElementById("bottomSize").value,
		topSize: document.getElementById("topSize").value,
		jacketSize: document.getElementById("jacketSize").value,
		message: null,
		store: document.getElementById("store").value
	};
	return inq;
}

document.getElementById("send").onclick = function() {
	var inq = createInquiryObject();
	var errors = errorsFor("step4");
	if(errors != "") {
		alert("Please correct the following problems before sending your inquiry:\n\n" + errors);
	} else {
		document.getElementById("step4").className = "step invisible";
		document.getElementById("step5").className = "step";
		var data = JSON.stringify(inq);
		var xhr = createServiceXHR("POST", "inquiry");
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 201) {
					var returnedInq = JSON.parse(xhr.responseText);
					
					alert(
						"Your inquiry has been received. Thank you.\n\nInquiry Details:\n" +
						"Name: " + returnedInq.name + "\n" +
						"Email: " + returnedInq.email + "\n" +
						"Store: " + returnedInq.store + "\n" +
						"Inquiry Type: " + returnedInq.type + "\n" +
						"Date Received: " + returnedInq.dateSubmitted + "\n");
				} else {
					alert("There was an unexpected error. Please try again later or contact support. We are sorry for the inconveniences.");
				}					
				location = "/";
			}
		};
		xhr.send(data);
	}
}

document.getElementById("checkBalanceLeft").onclick = function() {
	openCheckBalance();
};

document.getElementById("next2").onclick = function() {
	hideShowStep("step1", "step2");
};

document.getElementById("next3").onclick = function() {
	hideShowStep("step2", "step3");
};

document.getElementById("next4").onclick = function() {
	hideShowStep("step3", "step4");
};

loadGA();
