	/*
 	 * Ka-Ching Shopping Cart System Version 1.0
 	 * Release Date: 17/12/2002
 	 * Copyright (C) 2000 - 2002 Miro International Pty Limited 
	 */

function validateFormReg()
{
	if ((document.registration.firstname.value.length == 0) || (document.registration.lastname.value.length == 0) || (document.registration.address.value.length == 0) || (document.registration.suburb.value.length == 0) || (document.registration.country.options[document.registration.country.options.selectedIndex].value.length == 0) || (isNaN(document.registration.postcode.value)) || (document.registration.phone.value.length == 0) || (!validateEmail(document.registration.email.value)) || (document.registration.username.value.length == 0))
	{
		alert("Please fill out all required fields correctly");
		return false;
	}
	else 
	{
		return true;
	}
}

function validateLogin()
{
	if ((document.UserLogin.username.value == "") || (document.UserLogin.password.value == ""))
	{
		alert("Username and password are incorrect, please try again");
		return false;
	}
	return true;
}
		
function validateFormChangeDetails()
{
	if ((document.registration.firstname.value.length == 0) || (document.registration.lastname.value.length == 0) || (document.registration.address.value.length == 0) || (document.registration.suburb.value.length == 0) || (document.registration.country.options[document.registration.country.options.selectedIndex].value.length == 0) || (isNan(document.registration.postcode.value)) || (isNaN(document.registration.phone.value)) || (!validateEmail(document.registration.email.value)) || (document.registration.username.value.length == 0))
	{
		alert("Please fill out all required fields correctly");
		return false;
	}
	
	if ((document.registration.password.value.length != 0) && (document.registration.vpassword.value.length != 0))
	{
	 	if (document.registration.password.value != document.registration.vpassword.value)
		{
			alert ('Your passwords do not seem to match, please try again');
			return false;
		}
	}
	return true;
}

function validateQty()
{
	var qty = document.kaching.quantity.value;
	if ((qty.search(/^[0-9]/) != -1) && qty != 0)
	{
		return true;
	}
	else
	{
		alert ("Quantity not valid");
		return false;
	}
}

function validateContact()
{
	if (document.contactus.name.value == "" || !validateEmail(document.contactus.from.value) || document.contactus.text.value == "")
	{
		alert("Please complete all fields correctly");
	}
	else
	{
		document.contactus.submit();
	}
}

function  validateEmail( emailIn )  
{
	/// Number of '@' chars present in input string.
	var  numAtChars;

	/// The part of input address before the '@' character.
	var  userNameIn;

	/// The part of input address after the '@' character.
	var  domainNameIn;

	/// Holds the fields of the entered address, delimitted by '@' chars.
	var  addressFields = new Array();

	// Concatenate all alert output to here.
	var  alertString = "";

	/// Divide the input email address into fields.
	///  Note that the array "addressFields" will have one more element
	///  than the number of '@' signs in the input string.
	/// IE4 handles this OK with '@' as last character but NS4,4.5 and Opera 3.5 don't.
	addressFields = emailIn.split( '@' );

	numAtChars = addressFields.length - 1;
	//alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\n";

	if ( emailIn == "" )
		alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\n is EMPTY.";
	else if ( numAtChars  ==  0 )
		alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\n contains no '@' character and is therefore Not a valid Email Address.";
	else if ( numAtChars  >  1 )
		alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\ncontains " + numAtChars + " '@' characters and is therefore Not a valid Email Address.";
	else if ( addressFields[0] == "" )
		alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\nhas no Username before the '@' character and is therefore Not a valid Email Address.";
	else if ( addressFields[1] == "" )
		alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\nhas no Domain Name after the '@' character and is therefore Not a valid Email Address.";
	else {
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if ( userNameIn.indexOf( " " ) != -1 )
			alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\nhas one or more Spaces in the Username before the '@' character and is therefore Not a valid Email Address.";
		else if ( domainNameIn.indexOf( " " ) != -1 )
			alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\nhas one or more Spaces in the Domain Name after the '@' character and is therefore Not a valid Email Address.";
		else if ( isStandardDomain( domainNameIn ) == false )
			alertString += "The contents of the Email Address field:   \"" + emailIn + "\"\n\noes not end with a '.com' style domain or two letter country code domain and is therefore Not a valid Email Address.";
	
		} // Ends else from outer string of if-then-else's.

	// Post the alert box.
	if (alertString.length > 0)
		return false;
	else
		return true;
} // Ends validateEmailAddress2().

function  isStandardDomain( domainIn )  
{
	/// The VALUE to return, start out assuming invalid domain.
	var  isStandardReturn = false;
	/// Holds the last 4 characters of domain name.
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );
	/// Holds the last 3 characters of domain name.
	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );
	/// Uppercase it for comparison purposes.
	last4chars = last4chars.toUpperCase();
	/// A regular expression pattern to match country code domains.
	///  In otherwords a Dot character followed by two alphabetic characters.
	/// NOTE:  This line doesn't work at all in Opera3.5 and prevents the
	///  entire script from running!!!  BUMMER!!!
	///  It also seems to not work in Opera4.02 but only prevents
	///  the 2 letter codes from working but doesn't crash the script.
	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;

	if      ( last4chars == ".COM" ) isStandardReturn = true;
	else if ( last4chars == ".EDU" ) isStandardReturn = true;
	else if ( last4chars == ".GOV" ) isStandardReturn = true;
	else if ( last4chars == ".NET" ) isStandardReturn = true;
	else if ( last4chars == ".MIL" ) isStandardReturn = true;
	else if ( last4chars == ".ORG" ) isStandardReturn = true;

	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		isStandardReturn = true;

	return  isStandardReturn;

} // Ends isStandardDomain( domainIn ).

function isChecked(isitchecked)
{
	if (isitchecked == false)
	{
		document.shopping.boxchecked.value--;
	}
	else 
	{
		document.shopping.boxchecked.value++;
	}
}
	
function remove_product()
{
	if (document.shopping.boxchecked.value == 0)
	{
		alert('To remove an item, please check the box to the left of the product');
	}
	else
	{
		document.shopping.MDIL_task.value = 'remove_product';
		document.shopping.action = 'index.php';
		document.shopping.submit();
	}
}
	
function update_product()
{
	for (i = 0; i < document.shopping['product_id[]'].length; i++)
	{
		document.shopping['product_id[]'][i].checked = true;
	}
	document.shopping.MDIL_task.value = 'update_product';
	document.shopping.action = 'index.php';
	document.shopping.submit();
	
}

function validateForgottenPassword()
{
	if (document.forgotPassword.checkusername.value == "" || !validateEmail(document.forgotPassword.confirmEmail.value))
	{
		alert ("Username or email address are invalid, please try again");
		return false;
	}
	return true;
}

function checkQty()
{
	var var_title = document.kaching.attribute.value +  '_attribute[]';
	for (var i = 0; i < document.kaching[var_title].length; i++)
	{
		if (document.kaching[var_title][i].checked)
		{
			var attr_temp = document.kaching[var_title][i].value;
		}
	}

	var soh_store = attr_temp.split("|");
	var soh = soh_store[3];
	if (document.kaching.quantity.value > soh)
	{
		alert ('The quantity amount entered is greater than the amount of stock available. ');
	}
	alert (attr_temp);
	return false;
}