/*
Spectangles - Javascript
---------------------------------------------------------
  Copyright©Red Cherry Solutions Ltd.
  http://www.RedCherrySolutions.co.uk

  Duplication of this script or any associated scripts
  in whole or in part is strictly forbidden.
---------------------------------------------------------
*/

/* Swap the active CSS */
function setActiveStyleSheet(title) {
	if (document.getElementsByTagName) {
		for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) a.disabled = true;
			if (a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

/* validate the email field */
function validateEmail(field) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) { return false; }
		else { return true; }
	}
}

/* validate contact us enquiry form */
function validateContactUs(oForm) {
	var bValid = true;

	//check for security code
	if (oForm.code.value.length < 4) {
		alert('Please enter the security code');
		return false;
	}

	//check email
	if (validateEmail(oForm.email) == false) {
		alert('Please enter a valid email address');
		return false;
	}

	//check normal form
	if (oForm.name.value == '' ||
		oForm.email.value == '' ||
		oForm.telephone.value == '' ||
		oForm.otherInfo.value == '') {
		bValid = false;
	}

	//return correctly
	if (bValid == false) {
		alert('Please fill out all fields');
		return false;
	}

	return true;
}

/* validate the registration form for wholesale */
function validateRegisterWholesale(oForm) {
	if (oForm.wholesaleTerms.checked == false) {
		alert('You must state that you agree to the terms and conditions to register.');
		return false;
	} else {
		var bValid = true;

		//check for security code
		if (oForm.code.value.length < 4) {
			alert('Please enter the security code');
			return false;
		}

		//check for registration and password
		if (oForm.wholesalePassword.value.length < 5) {
			alert('Your password must be 5 or more characters');
			return false;
		}
		if (oForm.wholesalePassword.value != oForm.wholesaleConfirm.value) {
			alert('Your password and confirmation do not match');
			return false;
		}

		//check email
		if (validateEmail(oForm.wholesaleEmail) == false) {
			alert('Please enter a valid email address');
			return false;
		}

		//check normal form
		if (oForm.wholesaleFullName.value == '' || 
			oForm.wholesaleCompany.value == '' || 
			oForm.wholesaleTelephone.value == '' ||
			oForm.wholesaleAddress1.value == '' ||
			oForm.wholesalePostcode.value == '' ||
			oForm.wholesaleCountry.options[oForm.wholesaleCountry.selectedIndex].text == 'Please Select') {
			bValid = false;
		}

		//return correctly
		if (bValid == false) {
			alert('Please fill out all required fields *');
			return false;
		}
	}
	return true;
}

/* validate the registration form for customers */
function validateRegisterCustomer(oForm) {
	if (oForm.customerTerms.checked == false) {
		alert('You must state that you agree to the terms and conditions to register.');
		return false;
	} else {
		var bValid = true;

		//check for security code
		if (oForm.code.value.length < 4) {
			alert('Please enter the security code');
			return false;
		}

		//check email
		if (validateEmail(oForm.customerEmail) == false) {
			alert('Please enter a valid email address');
			return false;
		}

		//check normal form
		if (oForm.customerFullName.value == '' || 
			oForm.customerAddress1.value == '' ||
			oForm.customerPostcode.value == '' ||
			oForm.customerCountry.options[oForm.customerCountry.selectedIndex].text == 'Please Select') {
			bValid = false;
		}

		//return correctly
		if (bValid == false) {
			alert('Please fill out all required fields *');
			return false;
		}
	}
	return true;
}

/* validate the invoice address */
function validateInvoiceAddress(oForm) {
	var bValid = true;

	//check normal form
	if (oForm.invoiceFullName.value == '' || 
		oForm.invoiceAddress1.value == '' ||
		oForm.invoicePostcode.value == '' ||
		oForm.invoiceCountry.options[oForm.invoiceCountry.selectedIndex].text == 'Please Select') {
		bValid = false;
	}

	//return correctly
	if (bValid == false) {
		alert('Please fill out all required fields *');
		return false;
	}

	return true;
}

/* validate the delivery address */
function validateDeliveryAddress(oForm) {
	var bValid = true;

	//check normal form
	if (oForm.deliveryFullName.value == '' || 
		oForm.deliveryAddress1.value == '' ||
		oForm.deliveryPostcode.value == '' ||
		oForm.deliveryCountry.options[oForm.deliveryCountry.selectedIndex].text == 'Please Select') {
		bValid = false;
	}

	//return correctly
	if (bValid == false) {
		alert('Please fill out all required fields *');
		return false;
	}

	return true;
}