
// validation routines.
// copyright (c) 2010 alvin ward. all rights reserved.

function validateContactus(frm) {
	if (!validText("name", "You need to enter a name")) return false;
	if (!validText("email", "You need to enter a e-mail address")) return false;
	if (!validText("message", "You need to enter a message")) return false;
	return true;
}

function validateMembers(frm) {
	if (!validText("class", "You need to enter a class")) return false;
	if (!validText("name", "You need to enter a name")) return false;
	if (!validText("time", "You need to enter a time")) return false;
	if (!validText("date", "You need to enter a date")) return false;
	if (!validText("score", "You need to enter a score")) return false;
	return true;
}

function hideshowParents(chk) {
	if (chk.checked) {
		$("parentsblock1").show();
		$("parentsblock2").show();
		$("parentsblock3").show();
	} else {
		$("parentsblock1").hide();
		$("parentsblock2").hide();
		$("parentsblock3").hide();
	}
}

function validateIndemnity(frm) {
	if (!validText("name", "You need to enter a name and surname")) return false;
	//if (!validText("surname", "You need to enter a surname")) return false;
	if (!validText("address", "You need to enter a address")) return false;
	if (!validText("dob", "You need to enter a date of birth")) return false;
	if (!validText("email", "You need to enter a email")) return false;
	if (!validText("telephone", "You need to enter a contact no")) return false;
	if (!validText("tcname", "You need to enter a name for terms and conditions")) return false;
	if (!$("agreement").checked) {
		alert("You need to agree to the terms");
		$("agreement").activate();
		return false;
	}
	if ($("under18").checked) {
		if (!validText("minor", "You need to enter a minor name")) return false;
		if (!validText("parentname", "You need to enter a parent name")) return false;
		if (!validText("parentsurname", "You need to enter a parent surname")) return false;
	}
	if ($("doctorno").checked && $("chestphysicalno").checked
	 && $("chestno").checked && $("dizzinessno").checked
	 && $("jointsno").checked && $("medicineno").checked
	 && $("reasonno").checked) {
		alert("Congratulations.\nYou have answered 'NO' to all questions, which means that you can be reasonably sure that you can safely start a gradually increasing exercise program.\nWelcome to Forrest Fitness!");
	} else {
		alert("You have answered 'YES' to one or more of the PARQ questions. Please consult your doctor for medical clearance before embarking on the Forest Fitness program.\nFor more information, please contact us at ppf@ppfitness.co.za");
	}
	return true;
}

function validatebookTM(frm) {
	if (!validText("STthercontact", "You need to enter a contact person")) return false;
	if (!validText("STthercontactno", "You need to enter a contact number")) return false;
	if (!validText("STtheremail", "You need to enter a e-mail address")) return false;
	if (!validText("STtherstart", "You need to enter a starting date")) return false;
	if (!validText("STtherend", "You need to enter a ending date")) return false;
	return true;
}

function validatebookOFF(frm) {
	if (!validText("bookOFFcontact", "You need to enter a contact person")) return false;
	if (!validText("bookOFFcontactno2", "You need to enter a contact number")) return false;
	if (!validText("bookOFFemail3", "You need to enter a e-mail address")) return false;
	if (!validText("bookOFFcompname2", "You need to enter a company name")) return false;
	if (!validText("bookOFFstart2", "You need to enter a date")) return false;
	return true;
}

function validateStarterPack(frm) {
	if (!validText("name", "You need to enter a name")) return false;
	if (!validText("surname", "You need to enter a surname")) return false;
	if (!validText("tel", "You need to enter a telephone number")) return false;
	if (!validText("email", "You need to enter a e-mail address")) return false;
	return true;
}

function calcSPTotal() {
	$("totdumbbells").value = val($F("dumbbells")) * 120;
	$("totballs").value = val($F("balls")) * 30;
	$("totmats").value = val($F("mats")) * 65;
	$("tottherabands").value = val($F("therabands")) * 35;
	var total = 0;
	total += val($F("dumbbells")) * 120;
	total += val($F("balls")) * 30;
	total += val($F("mats")) * 65;
	total += val($F("therabands")) * 35;
	$("total").value = total;
}

function validateOrder() {
	if (!validText("name", "You need to enter a name")) return false;
	if (!validText("surname", "You need to enter a surname")) return false;
	if (!validText("delivery", "You need to enter a delivery address")) return false;
	if (($F("delivery").length > 300) || ($F("delivery").indexOf("http") > -1) || ($F("delivery").indexOf(".com") > -1)) {
		alert("Invalid delivery address");
		$(delivery).activate();
		return false;
	}
	if (!validText("email", "You need to enter a e-mail address")) return false;
	if (!validText("tel", "You need to enter a telephone number")) return false;
	switch($F("quoteorder")) {
		case "Order":
			$("emailsubject").value = "Rockets Range - Online Order from " + $F("name") + " " + $F("surname");
			break;
		default:
			$("emailsubject").value = "Rockets Range - Online Quote Request from " + $F("name") + " " + $F("surname");
			break;
	}

	if ($F("ajax") == "yes") {
		$("sendoq").hide();
		$("busyoq").show();
		ajaxOrder(1);
		return false;
	} else {
		return true;
	}
}

function ajaxOrder(callcount) {
	if (callcount > 3) {
		alert("There was a problem sending your request, please try again by clicking the Submit button.");
		$("busyoq").hide();
		$("sendoq").show();
	} else {
		$("order_quote").request({
			onComplete: function(transport) {
				if (transport.responseText == $F("url")) {
					//alert(transport.responseText);
					window.location = transport.responseText;
				} else {
					ajaxOrder(callcount+1);
				}
			}
		})
	}
}

function showOrderItem(itemid) {
	// disable and hide all
	$("divsox"+itemid).hide();
	$("soxcolor"+itemid).disable();
	$("soxsize"+itemid).disable();

	$("divleg"+itemid).hide();
	$("legcolor"+itemid).disable();
	$("legstyle"+itemid).disable();
	$("legsize"+itemid).disable();

	$("divtop"+itemid).hide();
	$("topcolor"+itemid).disable();
	$("topstyle"+itemid).disable();
	$("topsize"+itemid).disable();

	$("divbal"+itemid).hide();
	$("balweight"+itemid).disable();

	$("divbag"+itemid).hide();
	$("bagweight"+itemid).disable();

	$("divlol"+itemid).hide();
	$("lolweight"+itemid).disable();

	// enable and show the selected item
	switch ($F("prod"+itemid)) {
		case "RCG CompresSox (tm)":
			$("divsox"+itemid).show();
			$("soxcolor"+itemid).enable();
			$("soxsize"+itemid).enable();
			break;

		case "RCG Leggings":
			$("divleg"+itemid).show();
			$("legcolor"+itemid).enable();
			$("legstyle"+itemid).enable();
			$("legsize"+itemid).enable();
			break;

		case "RCG Tops":
			$("divtop"+itemid).show();
			$("topcolor"+itemid).enable();
			$("topstyle"+itemid).enable();
			$("topsize"+itemid).enable();
			break;

		case "Rocket Balls":
			$("divbal"+itemid).show();
			$("balweight"+itemid).enable();
			break;

		case "Rocket Bags":
			$("divbag"+itemid).show();
			$("bagweight"+itemid).enable();
			break;

		case "Rocket Line Out Lifters":
			$("divlol"+itemid).show();
			$("lolweight"+itemid).enable();
			break;
	}
}

// utility functions
function validText(id, msg) {
	if ($F(id) == "") {
		alert(msg);
		$(id).activate();
		return false;
	}
	return true;
}

function val(value) {
	value = new String(value).replace(/[^0-9.-]/gi, "");
	return (isNaN(parseFloat(value)) ? 0 : parseFloat(value));
}

function confirmRemove () {
	return confirm('Are you sure you want to remove this item?');
}

