/**************************************/
/*
Unite and Rule JavaScript
CopyrightŠUnite and Rule Ltd.
*/
/**************************************/

/**************************************/
/* 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;
		}
	}
}

/**************************************/
/* Copy invoice address to delivery address */
function copyAddress(oForm) {
	oForm.deliveryName.value = oForm.invoiceName.value;
	oForm.deliveryAddress1.value = oForm.invoiceAddress1.value;
	oForm.deliveryAddress2.value = oForm.invoiceAddress2.value;
	oForm.deliveryTown.value = oForm.invoiceTown.value;
	oForm.deliveryPostcode.value = oForm.invoicePostcode.value;
	oForm.deliveryCounty.selectedIndex = oForm.invoiceCounty.selectedIndex;
}

/**************************************/
/* Checkout Validation */
function validateCheckout(oForm) {
	var bValid = true;

	//invoice
	if (oForm.invoiceName.value == '') {
		alert('Please enter your name for the invoice.');
		return false;
	}
	if (oForm.invoiceAddress1.value == '') {
		alert('Please enter the first line of the address to display on the invoice.');
		return false;
	}
	if (oForm.invoiceTown.value == '') {
		alert('Please enter the town/city to display on the invoice.');
		return false;
	}
	if (oForm.invoiceCounty.options[oForm.invoiceCounty.selectedIndex].text == 'Please Select') {
		alert('Please select the county to display on the invoice.');
		return false;
	}
	if (oForm.invoicePostcode.value == '') {
		alert('Please enter the postcode for the invoice address.');
		return false;
	}
	if (oForm.telephone.value == '') {
		alert('Please enter your telephone number.');
		return false;
	}
	if (validateEmail(oForm.email) == false) {
		alert('Please enter a valid email address to send the order confirmation to.');
		return false;
	}

	//delivery
	if (oForm.deliveryName.value == '') {
		alert('Please enter the name for the delivery address.');
		return false;
	}
	if (oForm.deliveryAddress1.value == '') {
		alert('Please enter the first line of the delivery address.');
		return false;
	}
	if (oForm.deliveryTown.value == '') {
		alert('Please enter the town/city of the delivery address.');
		return false;
	}
	if (oForm.deliveryCounty.options[oForm.deliveryCounty.selectedIndex].text == 'Please Select') {
		alert('Please select the county of the delivery address.');
		return false;
	}
	if (oForm.deliveryPostcode.value == '') {
		alert('Please enter the postcode of the delivery address.');
		return false;
	}

	return true;
}

/* validate the checkout form */
function validatePayment(oForm) {

	if (oForm.terms.checked == false) {
		alert('You must state that you accept the terms and conditions to make a payment.');
		return false;
	} else {
		var bValid = true;

		//shipping country
		if (oForm.cardType.options[oForm.cardType.selectedIndex].value == 'Please Select') {
			alert('Please specify the type of card you will be paying with.');
			return false;
		}

		//card input
		if (oForm.cardNo.value.length < 19) {
			alert('Please enter your full card number including hyphens: ####-####-####-####.');
			return false;
		}

		//check form
		if (oForm.cardNo.value == '' ||
			oForm.cardSec.value == '' ||
			oForm.cardExp.value == '' ||
			oForm.cardName.value == '' ||
			oForm.code.value == '') {
			bValid = false;
		}

		//return correctly
		if (bValid == false) {
			alert('Please fill out all required fields *');
			return false;
		}
	}

	return true;
}

/**************************************/
/* validate the checkout form */
function validatePaymentCheque(oForm) {

	if (oForm.terms.checked == false) {
		alert('You must state that you accept the terms and conditions to make a payment.');
		return false;
	}

	return true;
}

/**************************************/
/* validate the checkout form */
function validatePaymentPhone(oForm) {

	if (oForm.terms.checked == false) {
		alert('You must state that you accept the terms and conditions to make a payment.');
		return false;
	}

	return true;
}

/**************************************/
/* validate the checkout form */
function validatePaymentCredit(oForm) {

	if (oForm.terms.checked == false) {
		alert('You must state that you accept the terms and conditions to make a payment.');
		return false;
	}

	return true;
}

/**************************************/
/* Feedback Validation */
function validateFeedback(oForm) {
	var bValid = true;

	if (oForm.name.value == '') {
		alert('Please enter your name for the feedback.');
		return false;
	}
	if (oForm.county.options[oForm.county.selectedIndex].text == 'Please Select') {
		alert('Please select the county to display with the feedback.');
		return false;
	}
	if (validateEmail(oForm.email) == false) {
		alert('Please enter a valid email address for your feedback.');
		return false;
	}

	if (oForm.feedbackType.value == 'Feedback') {
		//validate product feedback
		if (oForm.rating.options[oForm.rating.selectedIndex].text == 'Please Select') {
			alert('Please give us a rating for the product you are providing feedback for.');
			return false;
		}
	} else {
		//validate general feedback
		if (oForm.feedbackType.options[oForm.feedbackType.selectedIndex].text == 'Please Select') {
			alert('Please select the type of feedback you are giving us.');
			return false;
		}
	}

	if (oForm.feedback.value == '') {
		alert('Please provide us with your feedback comments.');
		return false;
	}

	return true;
}

/**************************************/
/* 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; }
	}
}

