/*****************************************************\
 **	Javascript Widget and Validation Library 0.01   **
 ** Jan 23 2003                                     **
\*****************************************************/
function validate_kp(o,ev) { 
	if (typeof(ev)=='undefined') ev=null;
	return Summit.Form.Validate(o,true,false,null,ev);
}

function validate_ch(o,ev) { 
	if (typeof(ev)=='undefined') ev=null;
	return Summit.Form.Validate(o,false,true,null,ev);
}


/* declare the Summit.Form namespace */
if (typeof(Summit)=='undefined') var Summit = {};
if (typeof(Summit.Form)=='undefined') Summit.Form = {};


Summit.Form.Validate = function(o,kp,change,submitform,ev) { 
	//KP_RE="/[0-9.]/" KP_FIND="/,/", KP_REPL="." CH_RE=""
	var kp_re, re_find, re_repl, ch_re, msg, ch_formatfunc, ch_validatefunc, optional, fieldtitle;
	var inputtype;
	kp_re = re_find = ch_re = null; 
	inputtype = fieldtitle = formatmsg = re_repl = '';
	ch_formatfunc = ch_validatefunc = '';
	optional = o.getAttribute("optional");
	inputtype = o.getAttribute("type");
//	console.log(o.getAttribute("name"));
	if (inputtype==null) inputtype=''; else inputtype=inputtype.toUpperCase();
	if (optional==null) optional=false; else optional=true;
	fieldtitle = o.getAttribute("TITLE");
	if (fieldtitle==null) fieldtitle=''; 

	if (typeof(ev)=='undefined') ev = window.event;
	else if (ev==null) ev = window.event;
	if (kp) if (ev.keyCode==13) return true;

	var index = -1;
	switch (inputtype) {
		case "RADIO":
			index = Summit.Form.GetRadioIdx(o.form[o.name]);
			break;
		default:
			break;
	}

//KP_RE="/[0-9.]/" KP_FIND="/,/", KP_REPL="." CH_RE="/[0-9]*\.[0-9]{2}/"
	var a = o.getAttribute("FIELDTYPE");
//	if (optional) alert(a + ' ' + o.name);
	switch (a) {
		case "EMAIL":
			kp_re = /\S/;
			ch_re = /.*/;
			//ch_re = /(^[a-z]([-a-z_\.]*)@([-a-z_\.]*)([.][a-z]{2,3})$)|(^[a-z]([-a-z_\.]*)@([-a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
			ch_validatefunc = emailCheck
			formatmsg = 'valid email address: user@somewhere.com';
			break;
		case "PHONE":
			ch_re = /^(?:\(?\d{3}\)?\s*-?)?(\d{3})\s*-?\s*(\d{4})$/;
			formatmsg = 'Valid number: (123) 456-7890';
			break;
		case "NUMBER":
			kp_re = ch_re = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
			formatmsg = 'valid number';
			//re_find = /,/;
			break;
		case "INTEGER":
			kp_re = ch_re = /(^-?\d\d*$)/;
			formatmsg = 'integer';
			//re_find = /,/;
			break;
		case "TELEPHONE":
			if (kp && o.value.length==3) {
				var s = String.fromCharCode(ev.keyCode); 
				o.value+='-';
				if (s=='-') return false;
			}
			kp_re = /\d/;
			ch_re = /\d{3}-\d{4}/;
			formatmsg = 'valid phone number';
			break;
		case "TELEPHONE_PARTIAL":
			if (kp && o.value.length==3) {
				var s = String.fromCharCode(ev.keyCode); 
				o.value+='-';
				if (s=='-') return false;
			}
			kp_re = /\d/;
			ch_re = /[\d-]/;
			//ch_re = /\d{3}-\d{4}/;
			//formatmsg = 'valid phone number';
			break;
		case "USZIP":
			kp_re = /[0-9-]/;
			ch_re = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
			formatmsg = 'valid US zip code: 99999 or 99999-9999';
			break;
		case "CANPOSTCODE":
			switch(o.value.length%2) {
				case 0: kp_re = /[A-Z]/i; break;
				default: kp_re = /[0-9]/; break;
			}
			ch_formatfunc = function(s){return s.toUpperCase();}
			ch_re = /^\D{1}\d{1}\D{1}[\- ]?\d{1}\D{1}\d{1}$/;
			formatmsg = 'valid canadian postal code: Z5Z-5Z5 or Z5Z5Z5';
			break;
		case "TEXT":
		case "NOTEMPTY":
			formatmsg = 'field can\'t be empty';
			kp_re = ch_re = /.*/;
			break;
		case "CURRENCY":
			kp_re = /[-\$0-9\.]/;
			ch_re = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{0,2}))$/;
//			ch_formatfunc = Summit.Form.MakeCurrency;
			formatmsg = 'valid dollar amount, using period as cents separator';
			break;
		case "DECIMAL":
			kp_re = /[-\$0-9\.]/;
			ch_re = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{0,2}))$/;
//			ch_formatfunc = Summit.Form.MakeCurrency;
			formatmsg = 'valid decimal value, using period as cents separator';
			break;
		case "CC":
			kp_re = /[\d ]/;
			ch_re = /[\d ]/;
			ch_validatefunc = Summit.Form.ValidateCC;
			formatmsg = 'valid credit card number';
			break;
		case "CUSTOM":
			a = o.getAttribute("KP_RE"); if (a!=null) eval('var kp_re = '+a+';');
			a = o.getAttribute("KP_FIND"); if (a!=null) eval('re_find = '+a+';');
			a = o.getAttribute("KP_REPL"); if (a!=null) re_repl=a;
			a = o.getAttribute("CH_RE"); if (a!=null) eval('var ch_re = '+a+';');
			//return true;
			break;
		default:
			if (!submitform && (o.name=='')) return true;
			if (optional || (o.name=='')) return '';
			if (inputtype=='CHECKBOX') {
				if (o.checked) return '';
			} else if (inputtype=='RADIO') {
				if (index>-1) return '';
			} else {
				if (o.value!='') return '';
			}
			return fieldtitle;
			break;
	}

/*	if (kp) {
		var s = String.fromCharCode(ev.keyCode); 
		if (re_find!=null) {
			s=s.replace(re_find,re_repl);
			ev.keyCode=s.charCodeAt(0);
		}
		if (kp_re==null) return true;
		return (null!=s.match(kp_re));
	} */
	if (change || submitform) {
		v = o.value;
		if (re_find!=null) {
			v=v.replace(re_find,re_repl);
			o.value=v;
		}
		if (ch_validatefunc) r = ch_validatefunc(v); else r=true;
		//alert(o.name + ' ' + r);
		//alert(o.name + ' => ' + v + ', ' + (v==''));
		if (r && (v!='') && v.match(ch_re)!=null) {
			if (ch_formatfunc) o.value = ch_formatfunc(v);
			return (submitform) ? '' : true;
		} else {
			if (submitform) {
			//if (v=='') return '';
			//else 
			if (formatmsg!='') fieldtitle += ' (' + formatmsg + ')';
			return (fieldtitle);
			} else {
				alert('Please Enter a ' + formatmsg);
				o.focus();
				o.select();
				return false;
			}
		}
	}
}

Summit.Form.ValidateForm = function( f ) {
	var el, msg, elementGroupName, elementGroup;
	msg = '';
	
	var groups = new Hash();
	var hiddenRows = $(f).select('.hidden'); // get list of hidden rows
	
	for (var i=0; i< f.elements.length; i++) {
		el = f.elements[i];

		var isHidden = false;
		// go through hidden rows to check if the element is disabled
		for (var j = 0; j < hiddenRows.length; j++) {
			if ($(el).descendantOf(hiddenRows[j])) {
				isHidden = true;
				break;
			}
		}
		
		
		if (!isHidden && !el.disabled) { // if it's disabled, don't validate it
		  
		  // check if element belongs to a group
		  elementGroupName = el.getAttribute('requiredGroup');
			if (elementGroupName) {
				if (!groups.get(elementGroupName)) {
					var newGroup = new Hash();
					newGroup.set('groupName', elementGroupName);
					newGroup.set('fields', new Hash());
					groups.set(elementGroupName, newGroup);
				}
				elementGroup = groups.get(elementGroupName);
				elementGroupFields = elementGroup.get('fields');
				elementGroupFields.set(el.name, el);
			}
			
		a = el.getAttribute('OPTIONAL');
		if (a!=null) if (a=='YES' && el.value=='') continue;
		  
		  
		var msg2 = Summit.Form.Validate(el,false, false, true);
		if (msg2!='') msg2 = "- " + msg2 + "\n";
		msg += msg2;
	}
		
	}
	
	// go through groups and validate they have values
	groups.each(
		function(gr) { 
			var msg2 = Summit.Form.ValidateRequiredGroup(f, gr.value);
			if (msg2!='') msg += "- " + msg2 + "\n";
		}
	);
	
	if (msg!='') {
		msg = "Please complete or correct the following fields:\n" + msg;
		alert(msg);
	}
	return (msg=='');
}

// checks that fields that have been grouped together have at least one value entered
Summit.Form.ValidateRequiredGroup = function(frm, group) {
	var retVal = '';
	var value = '';
	var numOfValuesEntered = 0;
	/*var groupMaxValuesAllowed = 0;
	if (frm.elements[group.get('groupName') + '_maxValuesAllowed']) groupMaxValuesAllowed = $F(frm.elements[group.get('groupName') + '_maxValuesAllowed']);*/
	var groupMaxValuesAllowed = 1; // for now this is hardcoded to 1 value per group
	
	var fields = group.get('fields');
	fields.each(
		function(el) {
			fld = el.value;
			if (fld.getAttribute('type') == 'radio') {
				val = (Summit.Form.GetRadioIdx(frm[fld.name]) >= 0);
			}
			else {
				val = fld.value;
			}
			
			if (val) numOfValuesEntered++;
		}
	);
	
	if (!numOfValuesEntered) { // no values were entered in the group
		retVal = group.get('groupName') + ' (group can\'t be empty.)';
	}
	else if (groupMaxValuesAllowed && numOfValuesEntered > groupMaxValuesAllowed) { 
		// there is a maximum # of values set for this group, and it was exceeded
		retVal = group.get('groupName') + ' (can\'t enter more than ' + groupMaxValuesAllowed + ' value(s).)';
	}
	
	return retVal;
}

Summit.Form.GetRadioIdx = function( arr ) {
	for (var i=0;i<arr.length;i++)
		if (arr[i].checked) return i;
	return -1;
}

Summit.Form.MakeCurrency = function( strValue ) {
	/************************************************
	DESCRIPTION: Formats a number as currency.
	
	PARAMETERS:
	  strValue - Source string to be formatted
	
	REMARKS: Assumes number passed is a valid
	  numeric value in the rounded to 2 decimal
	  places.  If not, returns original value.
	*************************************************/
  var f = parseFloat(strValue);
  if (isNaN(f)) return strValue;
  strValue = parseFloat(strValue).toFixed(2);  // if it is taxes - they might have precisions up to 4... need to have 2
  var i = Math.floor(f*100);

var f = parseFloat(strValue).toFixed(4); // need to do the jump, because JS loses precisions in calculations
var  f2= (parseFloat(100)*f).toFixed(4);

  var cents = Math.abs(f2%100);
  if (cents<10) cents='0'+cents;
	strValue =  Math.floor(i/100)+'.'+cents;
//  alert(strValue);
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = Summit.Form.addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
	  strValue = strValue.replace(',', '');
      return strValue;
//      return '$' + strValue;
    }
    else {
	  strValue = strValue.replace(',', '');
      return strValue;
	 }
}

Summit.Form.addCommas = function( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
  strValue - source string containing commas.

RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is
  returned.

REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match,
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}


Summit.Form.ValidateCC = function(cardNumber) {
	var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

		if ((cardNumberLength == 16) && (/^5[1-5]/.test(cardNumbersOnly))) {
			isValid = true;
			cardType='mastercard';
		} else if (((cardNumberLength == 13) || (cardNumberLength == 16)) &&
					 (/^4/.test(cardNumbersOnly)) ) {
			isValid = true;
			cardType='visa';
		} else if ((cardNumberLength==15) && (/^3(4|7)/.test(cardNumbersOnly))) {
			isValid = true;
			cardType = 'amex';
		} else {
      ; //alert("Invalid card number.");
    }
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += parseInt(numberProduct.charAt(productDigitCounter));
      }
    }
    isValid = (checkSumTotal % 10 == 0);
  }

  return isValid;
}



<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Changes:
/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */
// -->

<!-- Begin
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end
with. */

var
knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/i;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address.
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of
non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is
valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
} return true;
}

// Domain is symbolic name.  Check if it's valid.

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The e-mail address must end in a well-known domain or two letter " +
"country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End -->

