//----------------------------------------------------------------------
// Validate the purchase form
//----------------------------------------------------------------------
function do_validate(f)
// ignore unsure validation to avoid reject true customer.
{
	if (!validate_forename(f))
		return false;

	if (!validate_surname(f))
		return false;

//	if (!validate_email(f))
//		return false;

	if (!validate_phone_number(f))
		return false;

//	if (!validate_address(f))
//		return false;

//	if (!validate_postcode(f))
//		return false;

//	if (!validate_state(f))
//		return false;

	return true;
}

//----------------------------------------------------------------------
// Validate the email
//----------------------------------------------------------------------
function validate_email(f)
{
	var email = f.em.value;

	email = email.replace(/^\s+|\s+$/g,'');
	email = email.toLowerCase();

	f.em.value = email;

	if (!is_valid_email(email)) {
		alert("Please enter a valid e-mail address");
		f.em.focus();
		return false;
	}

	email = f.em2.value;
	email = email.replace(/^\s+|\s+$/g,'');
	email = email.toLowerCase();
	f.em2.value = email;

	if ( email.length > 0 ) {
		if (!is_valid_email(email)) {
  		alert("Please enter a valid Alternate E-Mail Address or leave it blank.");
  		f.em2.focus();
  		return false;
		}
	}

	return true;
}

//----------------------------------------------------------------------
// Validate the postcode
//----------------------------------------------------------------------
function validate_postcode(f)
{
	var idx = f.ct.selectedIndex;

	if (f.ct.options[idx].text == 'United States')
	{
		if (f.zp.value == "")
		{
			alert("Please enter your Zip Code");
			f.zp.focus();
			return false;
		}
	}
	else
	if (f.ct.options[idx].text == "United Kingdom")
	{
		if (f.zp.value == "")
		{
			alert("Please enter your Post Code");
			f.zp.focus();
			return false;
		}
	}

	return true;
}

//----------------------------------------------------------------------
// Validate the forename
//----------------------------------------------------------------------
function validate_forename(f)
{
	if (f.fn.value == '')
	{
		alert("Please enter your first name.");
		f.fn.focus();
		return false;
	}

	return true;
}

//----------------------------------------------------------------------
// Validate the surname
//----------------------------------------------------------------------
function validate_surname(f)
{
	if (f.sn.value == '')
	{
		alert("Please enter your family name.");
		f.sn.focus();
		return false;
	}

	return true;
}


//----------------------------------------------------------------------
// Validate state
//----------------------------------------------------------------------
function validate_state(f)
{
	var idx = f.ct.selectedIndex;
    var idx2 = f.st.selectedIndex;

	if (f.ct.options[idx].text == 'United States' || f.ct.options[idx].text == "Canada")
	{
    	var state = f.st.options[idx2].text;
    	if ( state == 'Choose a state / province' ||
        	state == 'Non-USA/Canada' ||
            state == '')
		{
			alert("Please select the state.");
			return false;
		}
	}
	return true;
}

//----------------------------------------------------------------------
// Validate the address
//----------------------------------------------------------------------
function validate_address(f)
{
	if (f.a1.value == '')
	{
		alert("Please enter your street address");
		f.a1.focus();
		return false;
	}

	if (f.a3.value == '')
	{
		alert("Please enter the city");
		f.a3.focus();
		return false;
	}

	if (f.ct.selectedIndex == 0)
	{
		if (f.ct.options[f.ct.selectedIndex].value == 'Other ----')
		{
			if (f.add6.value == '')
			{
				alert("Please enter the country.");
				return false;
			}
		}
		else
		{
			alert("Please select the country.");
			return false;
		}
	}

//	if ((f.ct.selectedIndex == 1)&&(f.add6.value == ''))
//	{
//		alert("Please specify the other country where you live");
//		return false;
//	}

	return true;
}

//----------------------------------------------------------------------
// Validate the phone number
//----------------------------------------------------------------------
function validate_phone_number(f)
{
	if (f.pn.value == '')
	{
		alert("Please enter your contact phone number");
		f.pn.focus();
		return false;
	}

	return true;
}


//----------------------------------------------------------------------
// Validate an email address
//----------------------------------------------------------------------
function is_valid_email(email) {
  var retval = false;

  if ( email.length > 0 ) {
    if ( email.match(/^[^\@]+\@([a-z0-9\-]{1,63}\.)+[a-z]{2,4}$/i ) ) {
      retval = true;
    }
  }

  return retval;
}

