/*------------------------------------------------------------------------------------------------*\
  File:        validation.js
	Description: contains all javascript validation used throughout the marketplace.
\*------------------------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------------------------*\
  Function:    validateCommVendor
  Description: validates the commercial vendor registration and edit profile pages. Pops open 
	             an error message notifying the user of all fields that need to be filled in and 
							 focuses on first offending field.
  Parameter:   formname - the form object.
               usertype - admin / user. The only difference is that if it's set to "admin", it will 
							            check for the Vendor Account (to be deprecated, eventually).
  Returns:     true / false
\*------------------------------------------------------------------------------------------------*/
function validateCommVendor(formname, usertype)
{
  var errors = "";
  var focus_field = "";  // the name of the first incorrectly entered field (str)

  if (!formname._fpassword.value)
  {
    errors += "* Password is required.\n";
	if (!focus_field) focus_field = "_fpassword";
  }
  if (!formname._cpassword.value)
  {
    errors += "* Confirmation Password is required.\n";
    if (!focus_field) focus_field = "_cpassword";
  }
  if ((formname._fpassword.value && formname._cpassword.value) && 
     (formname._fpassword.value != formname._cpassword.value))
  {
    errors += "* Passwords should be identical.\n";
    if (!focus_field) focus_field = "_cpassword";
  }
  if (!formname._fname.value)
  {
    errors += "* Company Name is required.\n";
    if (!focus_field) focus_field = "_fname";
  }
  if (!formname._ffirstname.value)
  {
    errors += "* Contact First Name is required.\n";
    if (!focus_field) focus_field = "_ffirstname";
  }
  if (!formname._flastname.value)
  {
    errors += "* Contact Last Name is required.\n";
    if (!focus_field) focus_field = "_flastname";
  }
  if (!formname._faddress.value)
  {
    errors += "* Street Address is required.\n";
    if (!focus_field) focus_field = "_faddress";
  }
  if (!formname._fcity.value)
  {
    errors += "* City is required.\n";
    if (!focus_field) focus_field = "_fcity";
  }
  if (!formname._fstate.value)
  {
    errors += "* State is required.\n";
    if (!focus_field) focus_field = "_fstate";
  }
  if (!formname._fcountry.value)
  {
    errors += "* Country is required.\n";
    if (!focus_field) focus_field = "_fcountry";
  }
  if (!formname._fzipcode.value)
  {
    errors += "* Zip/Postal Code is required.\n";
    if (!focus_field) focus_field = "_fzipcode";
  }
  if (!formname._femail.value)
  {
    errors += "* Email is required.\n";
	if (!focus_field) focus_field = "_femail";
  }
  if (!formname._fphone.value)
  {
    errors += "* Phone Number is required.\n";
    if (!focus_field) focus_field = "_fphone";
  }

  if (usertype == "admin")
  {
	if (!formname._faccount.value)
	{
	  errors += "* Vendor Account is required.\n";
	  if (!focus_field) focus_field = "_faccount";
	}
  }

  // if there are any errors, alert the error message and return false
  if (errors)
  {
    alert("Please fix the following problems:\n\n" + errors);
	  formname[focus_field].focus();
    return false;
  }

  return true; 
}


/*------------------------------------------------------------------------------------------------*\
  Function:    validateForm
  Description: used in all non-commercial vendor registration / edit profile pages. If there's a 
	              problem, it alerts a list of fields to fix and focuses on the first offending field.
  Parameter:   formname - the form object
               action   - 'edit', 'register'. Only difference is that 'register' checks for 'is 18' 
							            field.
  Returns:     true / false
\*------------------------------------------------------------------------------------------------*/
function validateForm(formname, action)
{
  var errors = "";
  var focus_field = "";  // the name of the first incorrectly entered field (str)

  if (!formname._femail.value)
  {
    errors += "* Email is required.\n";
	if (!focus_field) focus_field = "_femail";
  }
  if (!formname._fpassword.value)
  {
    errors += "* Password is required.\n";
	if (!focus_field) focus_field = "_fpassword";
  }
  if (!formname._cpassword.value)
  {
    errors += "* Confirmation Password is required.\n";
    if (!focus_field) focus_field = "_cpassword";
  }
  if ((formname._fpassword.value && formname._cpassword.value) &&
     (formname._fpassword.value != formname._cpassword.value))
  {
    errors += "* Passwords should be identical.\n";
    if (!focus_field) focus_field = "_cpassword";
  }
  if (action == 'register')
  {
    if (!formname._folderthan18.checked)
    {
      errors += "* Older than 18 Confirmation should be checked.\n";
	  if (!focus_field) focus_field = "_folderthan18";
    }
  }

  // if there are any errors, alert the error message and
  if (errors)
  {
    alert("Please fix the following problems:\n\n" + errors);
  	formname[focus_field].focus();
    return false;
  }

  return true;
}



/*------------------------------------------------------------------------------------------------*\
  Function:    validateEcommerceFields
  Description: called in ecommerce page to verify that all required fields are entered depending 
	             on selected negotiation model.
  Parameter:   f - the form object
  Returns:     true / false
\*------------------------------------------------------------------------------------------------*/
function validateEcommerceFields(f)
{
  // determine negotiation model
  var negModelID = 0;
  for (i=0; i<f._fnegotiationmodel.length; i++)
  {
    if (f._fnegotiationmodel[i].checked)
	  var negModelID = f._fnegotiationmodel[i].value;
  }

  var errors = "";
  var focus_field = "";  // the name of the first incorrectly entered field (str)


  if (!f._ftotalquantity.value)
  {
    errors += "* Quantity is required.\n";
  	if (!focus_field) focus_field = "_ftotalquantity";
  }
  else if (!_isNumber(f._ftotalquantity.value))
  {
    errors += "* Quantity should have integer values only.\n";
	  if (!focus_field) focus_field = "_ftotalquantity";
  }
  if (!f._fvalue.value)
  {
    errors += "* Value is required.\n";
	  if (!focus_field) focus_field = "_fvalue";
  }
  else if (!_isPrice(f._fvalue.value))
  {
    errors += "* Value must be a number.\n";
	  if (!focus_field) focus_field = "_fvalue";
  }

  // depending on the negotiation model, validate the appropriate fields
  switch (negModelID)
  {
    // Bidding Only
    case "0":

	    //  reserve value and starting bid are required
      if (!f._freservevalue.value)
	    {
	      errors += "* Reserve Value is required.\n";
	     	if (!focus_field) focus_field = "_freservevalue";
      }
	    else if (!_isPrice(f._freservevalue.value))
	    {
	      errors += "* Reserve Value must be a number.\n";
     		if (!focus_field) focus_field = "_freservevalue";
	    }
      if (!f._fstartbid.value)
   	  {
	      errors += "* Starting Bid is required.\n";
	     	if (!focus_field) focus_field = "_fstartbid";
      }
	    else if (!_isPrice(f._fstartbid.value))
	    {
	      errors += "* Starting Bid must be a number.\n";
		    if (!focus_field) focus_field = "_fstartbid";
	    }

      // empty the Asking Price field value to prevent it from failing validation [bug #1262]
      f._fbuyprice.value = "";

	    break;

    // bidding and fixed price
    case "1":
      if (!f._fbuyprice.value)
  	  {
  	    errors += "* Asking Price is required.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
      }
  	  else if (!_isPrice(f._fbuyprice.value))
  	  {
  	    errors += "* Asking Price must be a number.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
  	  }  
      if (!f._freservevalue.value)
  	  {
  	    errors += "* Reserve Value is required.\n";
  		  if (!focus_field) focus_field = "_freservevalue";
      }
  	  else if (!_isPrice(f._freservevalue.value))
  	  {
  	    errors += "* Reserve Value must be a number.\n";
  		  if (!focus_field) focus_field = "_freservevalue";
  	  }  
      if (!f._fstartbid.value)
  	  {
  	    errors += "* Starting Bid is required.\n";
  		  if (!focus_field) focus_field = "_fstartbid";
      }
  	  else if (!_isPrice(f._fstartbid.value))
  	  {
  	    errors += "* Starting Bid must be a number.\n";
  		  if (!focus_field) focus_field = "_fstartbid";
  	  }
  	  break;

    // fixed price only
    case "2":
      if (!f._fbuyprice.value)
  	  {
	      errors += "* Asking Price is required.\n";
	     	if (!focus_field) focus_field = "_fbuyprice";
      }
	    else if (!_isPrice(f._fbuyprice.value))
	    {
	      errors += "* Asking Price must be a number.\n";
		    if (!focus_field) focus_field = "_fbuyprice";
	    }

  		// empty Reserve price and Start Bid fields value to prevent it failing validation [bug #1262]
      f._freservevalue.value = "";
      f._fstartbid.value = "";  
  	  break;

	  // best offer only
    case "3":
      if (!f._fbuyprice.value)
	    {
  	    errors += "* Asking Price is required.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
      }
  	  else if (!_isPrice(f._fbuyprice.value))
  	  {
  	    errors += "* Asking Price must be a number.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
  	  }
  
  		// empty Reserve price and Start Bid fields value to prevent it failing validation [bug #1262]
      f._freservevalue.value = "";
      f._fstartbid.value = "";
  	  break;

	  // fixed price or best offer
    case "4":
      if (!f._fbuyprice.value)
  	  {
  	    errors += "* Asking Price is required.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
      }
  	  else if (!_isPrice(f._fbuyprice.value))
  	  {
  	    errors += "* Asking Price must be a number.\n";
  		  if (!focus_field) focus_field = "_fbuyprice";
  	  }

  		// empty Reserve price and Start Bid fields value to prevent it failing validation [bug #1262]
      f._freservevalue.value = "";
      f._fstartbid.value = "";
  	  break;

    // no (or unknown) negotiation model selected
  	default:
      errors += "* Please select the sales method.\n";
      if (!focus_field) focus_field = "_ftotalquantity"; // on purpose.
	    break;
  }

  // if there are any errors, alert the error message and
  if (errors)
  {
    alert("Please fix the following problems:\n\n" + errors);
    f[focus_field].focus();
    return false;
  }
  else
    return true;
}


// returns false if empty
function _isNumber(val)
{
  return !val.match(/\D/);
}

// returns false if empty
function _isPrice(val)
{
  return !val.match(/[^\d.,]/);
}
