/* ----------------------------------------------------------------------------------------------------------------
*	
Function:	validateField, saveRecord

Functionality:
Makes payment Date on or after the current date. Also checks if the CC number entered is valid.

Deployment:
The script can be deployed on External Debit/Credit Card form.

Event triggering the script:
	1) validateField.
	2) onSave
	
Custom Fields:
+-----------------------------------------------------------------------------------------------------------------+
| S. No.|	Id					|	Type			|	Level	|		Description							
+-----------------------------------------------------------------------------------------------------------------+
| (1)	|	_pay_date			|	Date			|	Body	|	    This field holds value for Credit Card pay date |
| (2)	|	_cc_number			|	Free Form text	|	Body	|	    This field holds Credit/Debit Card number |
Create above custom fields with their id and type as mentioned in table.
*------------------------------------------------------------------------------------------------------------------
*/

function saveRecord(type,name)
{	
	// check for Payment date
	var paymentDate = nlapiGetFieldValue ( 'custbody_pay_date' ) ;
	var newPaymentDate = nlapiStringToDate(paymentDate);
	var presentDate = new Date();
	var yesterDate = presentDate.setDate(presentDate.getDate()-1);
	if (  paymentDate != null && newPaymentDate < yesterDate )
	{
	  alert ( "The payment date should be on or after the current date." ) ;
	  return false ;
	}

	return true;
}

function validateField(type, name)
{
	if(name == 'custbody_cc_number')
	{
		var ccNumb = String(nlapiGetFieldValue('custbody_cc_number'));
		if (IsNumeric(ccNumb) == false)
		  {
			  alert ("Please enter a numeric value for the Card Number with no Spaces (ex. 5555445144555555)!")
			  return false
		  }

		  else if (ccNumb.length != 16)
		  {
			alert ("Please enter 16 digits in the Card Number field.")
			return false
		  }
	}

	return true;
}

function IsNumeric(strString)
//  check for valid numeric strings         
{
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
  }

  return blnResult;
}