// VARIOUS VALIDATION scripts -JDILL
String.prototype.stripPunctuation = function() {
	return this.replace( /[~`\.,;!@#\$%\^\*&\+=<>"\/:\?'\|\(\)\[\]_\-\\\s]/g, '');
};
String.prototype.stripLetters = function() {
	return this.replace( /[A-Za-z]/g, '');
};
String.prototype.cleanQuotes = function() {
	return this.replace( /["']/g, '`');
};
function isempty(id) { //src=http://www.codetoad.com/javascript/form_validation_function.asp
  if (typeof id != 'undefined') {
	if (typeof id!='object') id=document.getElementById(id); 
	   if ((id.value.length==0) ||
	   (id.value==null)) {
		  return true;
	   }
  }
   else { return false; }
};

function checkNumber(e){
		// detect last key pressed. if not a number, then dont do anything.
	var key = e.keyCode;
	return ((key>47 && key<58)||(key>95&&key<106))
};

function FormatISODate(p1){
  if(p1.value!="") {
	p=p1.value.stripPunctuation();
	a=p.substring(0,4);
	b=p.substring(4,6);
	c=p.substring(6,8);
	p=a;
	if (b!=""||a.length==4){
		p=p+"-"+b;
	}
	if (c!=""||b.length==2){
		p=p+"-"+c;
	}
	p1.value=p;
  }//close if
};
function FormatPhone(p1){
  if(p1.value!="") {
	p=p1.value.stripPunctuation();
	a=p.substring(0,3);
	b=p.substring(3,6);
	c=p.substring(6,10);
	if(a.length>0) p="("+a;
	if (b!=""||a.length==3){
		p=p+") "+b;
	}
	if (c!=""||b.length==3){
		p=p+"-"+c;
	}
	p1.value=p;
  }//close if
};

function getFormat(m,e,vtype){
	//maxlen=m.getAttribute('maxlength');
	switch(vtype) {
	case "isodate":
		if (checkNumber(e)){	
			FormatISODate(m);
		} else {
			if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
		}
	break;
	case "ph":
		if (checkNumber(e)){	
			FormatPhone(m);
		} else {
			if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
		}
	break;	
	case "caps":
		if(m.value!=m.value.stripPunctuation()){m.value=m.value.stripPunctuation();}
		if(m.value!=m.value.toUpperCase()){m.value=m.value.toUpperCase();}
	break;
	case "num":
		if (!checkNumber(e)){
			if(m.value!=m.value.stripLetters()){m.value=m.value.stripLetters();}
		}
	break;	
	} //close switch
}; //close function