﻿
function checkform()
{
    //Trim whitespace from values
    document.purchaseinfo.first.value = trim(document.purchaseinfo.first.value);
    document.purchaseinfo.last.value = trim(document.purchaseinfo.last.value);
    document.purchaseinfo.email.value = trim(document.purchaseinfo.email.value);
    document.purchaseinfo.email2.value = trim(document.purchaseinfo.email2.value);

      var success = true
      var first = document.purchaseinfo.first.value;
      var last = document.purchaseinfo.last.value;
      var email = document.purchaseinfo.email.value;
      var email2 = document.purchaseinfo.email2.value;
      var message = '';

	if (first == '')
	{
		// something is wrong
		message += 'First name is blank \n';
		success = false;
	}
	if (last == '')
	{
		// something else is wrong
		message += 'Last name is blank \n';
		success = false;
	}
	if (email != email2)
	{
	      message += 'Email does not match \n';
		success = false;
	}
	
	var isValid = validate_email(email);
	
	//alert(isValid.toString());
	
	if (isValid == false)
	{
	      message += 'Email invalid \n';
		success = false;
	}
	
	if (message.length > 0)
	{
	alert(message)
	}

	return success;
}

function validate_sn()
{
    var success = true
    var first = document.purchaseinfo.sn.value;
    var sep = first.indexOf("-dat");
    var message = '';

	if (first == '')
	{
		// No sn
		message += 'Please enter your Serial Number \n';
		success = false;
	}
	
	if (sep<1) 
	{
		// incorrect sn
		message += 'Invalid Serial Number \n';
		success = false;
	}
	
    if (message.length > 0)
	{
	    alert(message)
	}

	return success;
}


function validate_email(field)
{
      if (field == "") {return false;}
      
      var apos=field.indexOf("@");
      var dotpos=field.lastIndexOf(".");
      if (apos<1||dotpos-apos<2) 
      {
            return false;
      }                  
      else 
      {
            return true;
      }
  }

  String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, "");
  }
  String.prototype.ltrim = function() {
      return this.replace(/^\s+/, "");
  }
  String.prototype.rtrim = function() {
      return this.replace(/\s+$/, "");
  }
  function ltrim(str) {
      for (var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
      return str.substring(k, str.length);
  }
  function rtrim(str) {
      for (var j = str.length - 1; j >= 0 && isWhitespace(str.charAt(j)); j--);
      return str.substring(0, j + 1);
  }
  function trim(str) {
      return ltrim(rtrim(str));
  }
  function isWhitespace(charToCheck) {
      var whitespaceChars = " \t\n\r\f";
      return (whitespaceChars.indexOf(charToCheck) != -1);
  }
 

