// 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, '`');
};
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 URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}
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

function lightDown(){
    var nm=this.name;
	pop=jQuery(this).parents("form").attr('id')+"_"+nm+"_pop";
	if(jQuery("#" + pop).is(":visible")){
	    jQuery("#" + pop).hide();
	    this.select();
	 }
	return false;
};