﻿var alerted;

function validateEmail(email)
{
	if((email.indexOf('@') == -1) || (email.indexOf('.') == -1)
		|| (email.indexOf('.') == 0) || (email.indexOf('.') == (email.length - 1)))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validateSave()
{
	if(document.SaveCart.txtName.value == "")
	{
		alert("You have not entered a name for your cart");
		return false;
	}
	else
	{
		return true;
	}
}

function Round(toRound, numberDecimals)
{
	var num = new Number(toRound);
	return num.toFixed(numberDecimals);
}

function chargeSurcharge(CardForm)
{
	var extra = 0;
	var chargeString = "Additional AUD";
	if(surcharges[CardForm.CardType.value] != 0)
	{
		CardForm.ccExtra.value = Round(Number(surcharges[CardForm.CardType.value]) * CardForm.OrderTotal.value / 100, 2);
		chargeString += Round(CardForm.ccExtra.value, 2) + " surcharge";
	}
	else
	{
		chargeString = "";
		CardForm.ccExtra.value = 0;
	}
	CardForm.lblSurcharge.value = chargeString;
}

function windowPopup(newUrl, windowHeight, windowWidth)
{
	var newWindow;
	newWindow = window.open(newUrl, 'Popup', 'height=' + windowHeight + ',width=' + windowWidth
		+ 'resizable=no,scrollbars=no,toolbar=no,menubar=no');
	if (window.focus)
	{
		newWindow.focus();
	}
}

function ChangePayment(PaymentType)
{
	var Card = document.getElementById("CreditCard");
	var Cheque = document.getElementById("ChequePayment");
	var Deposit = document.getElementById("DirectDeposit");
	var Callback = document.getElementById("CallBack");
	if(PaymentType == 1)//Credit Card
	{
		Card.className = "VisiblePayment";
		Callback.className = "InvisiblePayment";
		Cheque.className = "InvisiblePayment";
		Deposit.className = "InvisiblePayment";
	}
	else if(PaymentType == 2)//Call Back
	{
		Card.className = "InvisiblePayment";
		Callback.className = "VisiblePayment";
		Cheque.className = "InvisiblePayment";
		Deposit.className = "InvisiblePayment";
	}
	else if(PaymentType == 3)//Cheque
	{
		Card.className = "InvisiblePayment";
		Callback.className = "InvisiblePayment";
		Cheque.className = "VisiblePayment";
		Deposit.className = "InvisiblePayment";
	}
	else//Direct Deposit
	{
		Card.className = "InvisiblePayment";
		Callback.className = "InvisiblePayment";
		Cheque.className = "InvisiblePayment";
		Deposit.className = "VisiblePayment";
	}
}

function ChangeSearchPages(StartPage)
{
	document.Pages.StartNumber.value = StartPage;
	document.Pages.submit();
}

function resizeImage(currentImage)
{
	if(currentImage.width > currentImage.height)
	{
		currentImage.width = 100;
	}
	else
	{
		currentImage.height = 100;
	}
}

function ShowHideCategories(CategoryCheckbox)
{
	if(document.getElementById("CategorySection"))
	{
		var CategoryListing = document.getElementById("CategorySection");
		if(CategoryCheckbox.checked)
		{
			CategoryListing.className = "VisibleCategories";
		}
		else
		{
			CategoryListing.className = "HiddenCategories";
		}
	}
}

function validateEditCart()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	var index;
	var textField;
	for(index = 0; index < document.frmCart.elements.length; index ++)
	{
		textField = document.frmCart.elements[index];
		if(textField.name.indexOf("txtQty") != -1)
		{
			if((textField.value == "") || isNaN(textField.value))
			{
				valid = false;
				errorString += "\nYour product quantity cannot must be a number >= 0";
			}
			else
			{
				if(Number(textField.value) < 0)
				{
					valid = false;
					errorString += "\nYour product quantity cannot must be a number >= 0";
				}
			}
		}
	}
	if(!valid)
	{
		alert(errorString);
	}
	return valid;
}

function validateSearch()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	if((document.SearchForm.txtName.value == "") && (document.SearchForm.txtCode.value == "")
		&& ((document.SearchForm.txtPriceMin.value == "") || (document.SearchForm.txtPriceMin.value == "Minimum"))
		&& ((document.SearchForm.txtPriceMax.value == "") || (document.SearchForm.txtPriceMax.value == "Maximum")))
	{
		valid = false;
		errorString += "\nYou must enter some search criteria to search for";
	}
	if((document.SearchForm.txtPriceMin.value != "") && (document.SearchForm.txtPriceMin.value != "Minimum"))
	{
		if(isNaN(document.SearchForm.txtPriceMin.value))
		{
			valid = false;
			errorString += "\nYou must enter a valid minimum price or leave it blank for the mininum";
		}
		else if(document.SearchForm.txtPriceMin.value < 0)
		{
			valid = false;
			errorString += "\nThe minimum price should be greater than or equal to 0";
		}
	}
	if((document.SearchForm.txtPriceMax.value != "") && (document.SearchForm.txtPriceMax.value != "Maximum"))
	{
		if(isNaN(document.SearchForm.txtPriceMax.value))
		{
			valid = false;
			errorString += "\nYou must enter a valid maximum price or leave it blank for the maximum";
		}
		else if(document.SearchForm.txtPriceMax.value < 1)
		{
			valid = false;
			errorString += "\nThe maximum price should be greater than 0";
		}
	}
	if((document.SearchForm.txtNumber.value != "") && (document.SearchForm.txtNumber.value != "All"))
	{
		if(isNaN(document.SearchForm.txtNumber.value))
		{
			valid = false;
			errorString += "\nYou must enter a valid number of products to display or leave it blank to display all";
		}
		else if(document.SearchForm.txtNumber.value < 1)
		{
			valid = false;
			errorString += "\nThe number of products to display should be greater than 0";
		}
	}
	if(valid == true)
	{
		if(document.SearchForm.txtPriceMin.value == "Minimum")
		{
			document.SearchForm.txtPriceMin.value = "";
		}
		if(document.SearchForm.txtPriceMax.value == "Maximum")
		{
			document.SearchForm.txtPriceMax.value = "";
		}
		if(document.SearchForm.txtNumber.value == "All")
		{
			document.SearchForm.txtNumber.value = "";
		}
	}
	else
	{
		alert(errorString);
	}
	return valid;
}

function validatePayment()
{
	var valid = true;
	if (!submittedPayment)
	{
		var errorString = "The following errors occurred:";
		var user_input;
		var Cards = document.getElementById("CardType")
		var SelectedOption = Cards.selectedIndex;
		var SelectedText = Cards.options[SelectedOption].text.replace(/\s/g, "");
		if(Number(document.frmPayment.ServiceID.value) == -1)
		{
			valid = false;
			errorString += "\nYou cannot proceed without an option for freight";
		}
		else
		{
			for (i=0; i < document.frmPayment.rbPayment.length; i++)
			{
				if (document.frmPayment.rbPayment[i].checked)
				{
					user_input = document.frmPayment.rbPayment[i].value;
				}
			}		
			if(((Number(document.frmPayment.ServiceID.value) == 4) || (Number(document.frmPayment.ServiceID.value) == 5))
				&& (!document.FreightChoice.cbPost.checked))
			{
				errorString += "\nYou have selected Australia Post but you have not accepted the disclaimer";
				valid = false;
			}
			if(user_input == "cCard")
			{
				if(document.frmPayment.txtCardnumber.value == "")
				{
					errorString += "\nYou selected credit card payment and you have not entered a card number";
					valid = false;
				}
				if(document.frmPayment.txtCVN.value == "")
				{
					errorString += "\nYou selected credit card payment and you have not entered a CVN number";
					valid = false;
				}
				if(document.frmPayment.txtCardname.value == "")
				{
					errorString += "\nYou selected credit card payment and you have not entered a cardholder name";
					valid = false;
				}
				if(document.frmPayment.CardType.value == 0)
				{
					errorString += "\nYou selected credit card payment and you have not selected a card type";
					valid = false;
				}
				if(valid)
				{
					if(!checkCreditCard(document.frmPayment.txtCardnumber.value, SelectedText))
					{
						errorString += "\n" + ccErrors[ccErrorNo];
						valid = false;
					}
				}
			}
			if(user_input == "Credit Card Call Back")
			{
				if(document.frmPayment.txtCallback.value == "")
				{
					errorString += "\nYou selected credit card (Callback) and you have not entered a call back number";
					valid = false;
				}
			}
		}
		if(!valid)
		{
			alert(errorString);
		}
		else
		{
			document.frmPayment.txtCardnumber.value = document.frmPayment.txtCardnumber.value.replace(/\s/g, "");
			submittedPayment = true;
		}
	}
	else
	{
		return false;
	}
	return valid;
}

function validateCheckout()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	
	if(document.frmAddress.txtAddress.value == "")
	{
		errorString += "\nYou have not entered an address";
		valid = false;
	}
	if(document.frmAddress.txtCity.value == "")
	{
		errorString += "\nYou have not entered a city";
		valid = false;
	}
	if(document.frmAddress.Country.value == "AU")
	{
		if(document.frmAddress.txtState.value == "")
		{
			errorString += "\nIf the country is Australia, the state must be entered";
			valid = false;
		}
		if(document.frmAddress.txtPostcode.value == "")
		{
			errorString += "\nIf the country is Australia, the postcode must be entered";
			valid = false;
		}
		if(document.frmAddress.txtCity.value == 0)
		{
			errorString += "\nYou have not selected a city";
			valid = false;
		}
	}
	if(valid)
	{
		if((document.frmAddress.txtAddress.value.replace(' ','').substr(0,1) == "p")
			|| (document.frmAddress.txtAddress.value.replace(' ','').substr(0,1) == "P")
			|| (document.frmAddress.txtAddress.value.replace(' ','').substr(0,1) == "B")
			|| (document.frmAddress.txtAddress.value.replace(' ','').substr(0,1) == "b"))
		{
			if(!document.frmAddress.cbPobox.checked)
			{
				if(confirm('This looks like a P O Box\n'
					+ 'click on \"OK\" if this is a P O Box else click \"Cancel\"\n'))
				{
					document.frmAddress.cbPobox.checked = true;
				}
			}
		}
		document.frmAddress.txtAddress.disabled = false;
		document.frmAddress.txtCity.disabled = false;
		document.frmAddress.txtPostcode.disabled = false;
		document.frmAddress.txtState.disabled = false;
		document.frmAddress.Country.disabled = false;
	}
	else
	{
		alert(errorString);
	}
	
	return valid;
}

function ChangePostCode(Country, Code)
{
	if(((Country != "AU") && (document.frmAddress.txtCity.type != "text")) || (Country == "AU"))
	{
		ChangeSuburbs(Country, Code);
	}
}

function checkCapsLock(e)
{
	var myKeyCode=0;
	var myShiftKey=false;
	var myMsg='Caps Lock is On.\n\nPassword is case sensitive.';
	if(!alerted)
	{
		// Internet Explorer 4+
		if(document.all)
		{
			myKeyCode=e.keyCode;
			myShiftKey=e.shiftKey;
		// Netscape 4
		}
		else if(document.layers )
		{
			myKeyCode=e.which;
			myShiftKey=( myKeyCode == 16 ) ? true : false;
		// Netscape 6
		}
		else if(document.getElementById)
		{
			myKeyCode=e.which;
			myShiftKey=( myKeyCode == 16 ) ? true : false;
		}
		// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
		if((myKeyCode >= 65 && myKeyCode <= 90) && !myShiftKey)
		{
			alert(myMsg);
		// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
		}
		else if((myKeyCode >= 97 && myKeyCode <= 122) && myShiftKey)
		{
			alert( myMsg );
		}
		alerted = true;
	}
}

function showCartSummary()
{
	var Cart = document.getElementById("CartSummary");
	Cart.className = "VisibleCart";
}

function hideCartSummary()
{
	var Cart = document.getElementById("CartSummary");
	Cart.className = "HiddenCart";
}

function validateAddProduct(currentForm)
{
	if((currentForm.txtQty.value == "") || isNaN(currentForm.txtQty.value))
	{
		alert("You have not entered a valid quantity");
		return false;
	}
	else
	{
		if(Math.floor(currentForm.txtQty.value) != currentForm.txtQty.value)
		{
			alert("You have not entered a valid quantity");
			return false;
		}
		return true;
	}
}

function validateForgotEmail()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	
	if(document.Forgot.txtEmail.value == "")
	{
		errorString += "\nYour e-mail address is a required field";
		valid = false;
	}
	else
	{
		if(!validateEmail(document.Forgot.txtEmail.value))
		{
			errorString += "\nThe e-mail address you supplied is not a valid e-mail address";
			valid = false;
		}
	}
	if(!valid)
	{
		alert(errorString);
	}
	return valid;
}

function validateUserDetails()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	
	if(document.User.txtFirstname.value == "")
	{
		errorString += "\nYour first name is a required field";
		valid = false;
	}
	if(document.User.txtLastname.value == "")
	{
		errorString += "\nYour last name is a required field";
		valid = false;
	}
	if(document.User.txtEmail.value == "")
	{
		errorString += "\nYour e-mail address is a required field";
		valid = false;
	}
	else
	{
		if(!validateEmail(User.txtEmail.value))
		{
			errorString += "\nThe e-mail address you supplied is not a valid e-mail address";
			valid = false;
		}
	}
	if(document.User.txtPassword.value == "")
	{
		errorString += "\nYour password is a required field";
		valid = false;
	}
	else
	{
		if(document.User.txtPassword.value.length < 6)
		{
			errorString += "\nYour password needs to be at least 6 characters";
			valid = false;
		}
		else
		{
			if(document.User.txtPassword.value != document.User.txtConfirmPassword.value)
			{
				errorString += "\nYour password and your confirm password are not the same";
				valid = false;
			}
			if(document.login.txtPassword.value.indexOf("'") > -1)
			{
				errorString += "\nYour password cannot contain apostrophes";
				valid = false;
			}
		}
	}
	/*if(document.User.Profession.value == 0)
	{
		errorString += "\nYou have not selected a profession";
		valid = false;
	}
	if(document.User.Channel.value == 0)
	{
		errorString += "\nYou have not selected a member type";
		valid = false;
	}*/
	if(!valid)
	{
		alert(errorString);
	}
	return valid;
}

function validateLogin()
{
	var valid = true;
	var errorString = "The following errors occurred:";
	
	if(document.login.txtUsername.value == "")
	{
		errorString += "\nYou have not supplied a username";
		valid = false;
	}
	else
	{
		if(!validateEmail(document.login.txtUsername.value))
		{
			errorString += "\nThe username address you supplied is not a valid e-mail address";
			valid = false;
		}
	}
	if(document.login.txtPassword.value == "")
	{
		errorString += "\nYou have not supplied a password";
		valid = false;
	}
	else if(document.login.txtPassword.value.indexOf("'") > -1)
	{
		errorString += "\nYour password cannot contain apostrophes";
		valid = false;
	}
	if(!valid)
	{
		alert(errorString);
	}
	return valid;
}

function ShowImage()
{
	var screenWidth = screen.width;
	var largeImage = document.getElementById("LargeImage");
	var divWidth = parseInt(largeImage.style.width);
	largeImage.className = "VisibleImage";
	largeImage.style.left = Math.floor((screenWidth / 2) - (divWidth / 2)) + "px";
}

function HideImage()
{
	var largeImage = document.getElementById("LargeImage");
	largeImage.className = "HiddenImage";
}

function showDisclaimer()
{
	var disclaimer = document.getElementById("disclaimer");
	disclaimer.className = "VisibleDisclaimer";
}

function hideDisclaimer()
{
	var disclaimer = document.getElementById("disclaimer");
	disclaimer.className = "HiddenDisclaimer";
}