Ext.apply(Ext.EventObject, {
	within : function(el, related, allowEl){
		try {
			if(el){
				var t = this[related ? "getRelatedTarget" : "getTarget"]();
				return t && ((allowEl ? (t == Ext.getDom(el)) : false) || Ext.fly(el).contains(t));
			}
		} catch(e) {}
		return false;
	}
});

function validateZIP(whichfield) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var str = whichfield.value;

	if (str.length!=5 && str.length!=10) {
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	}
	for (var i=0; i < str.length; i++) {
	temp = "" + str.substring(i, i+1);
	if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			whichfield.style.backgroundColor="#FFAAAA";
			return false;
		}
		if ((hyphencount > 1) || ((str.length==10) && ""+str.charAt(5)!="-")) {
			whichfield.style.backgroundColor="#FFAAAA";
			return false;
		}
	}
	whichfield.style.backgroundColor="white";
	return true;
}

function checkEmail( whichfield ){
	var str = whichfield.value
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var chars = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < str.length ;i++){
		if( chars.indexOf(str.charAt(i))<0) {
			whichfield.style.backgroundColor="#FFAAAA";
			return (false);
		}
	}
	if (filter.test(str)){
		whichfield.style.backgroundColor="white";
		return true;
	} else {
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	}
}

function formatEmailorNumber( str, typ ){
	if( typ == 'email' ){
		return( str );
	} else if( typ =='phone' || typ == 'text' ){
		r = "";
		for (i=0; i < str.length; i++) {
			if (str.charAt(i) != '\n' && str.charAt(i) != '\r' && str.charAt(i) != '\t' && str.charAt(i) != '-' && str.charAt(i) != '\)' && str.charAt(i) != '(' && str.charAt(i) != ' ' && str.charAt(i) != '.') {
				r += str.charAt(i);
			}
		}
		t = "";
		for (j=0; j < 10; j++){
			t += r.charAt(j);
			if ((j==2) || (j==5)){
				t += "-";
			}
		}
		var useareacode=1;
		phoneRegex = /^\d\d\d-\d\d\d-\d\d\d\d$/;
		if( !t.match( phoneRegex ) ) {
			return( str );
		} else {
			return( t );
		}	
	}
}

function testEmailorNumber( str, typ ){
	if( typ == 'email' ){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var chars = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for(i=0; i < str.length ;i++){
			if( chars.indexOf(str.charAt(i))<0) {
				return( false );
			}
		}
		if (filter.test(str)){
			return( true );
		} else {
			return( false );
		}
	} else if( typ =='phone' || typ == 'text' ){
		r = "";
		for (i=0; i < str.length; i++) {
			if (str.charAt(i) != '\n' && str.charAt(i) != '\r' && str.charAt(i) != '\t' && str.charAt(i) != '-' && str.charAt(i) != '\)' && str.charAt(i) != '(' && str.charAt(i) != ' ' && str.charAt(i) != '.') {
				r += str.charAt(i);
			}
		}
		t = "";
		for (j=0; j < 10; j++){
			t += r.charAt(j);
			if ((j==2) || (j==5)){
				t += "-";
			}
		}
		var useareacode=1;
		phoneRegex = /^\d\d\d-\d\d\d-\d\d\d\d$/;
		if( !t.match( phoneRegex ) ) {
			return( false );
		} else {
			return( true );
		}	
	}
}

function checkPhone(whichfield) {
	r = "";
	s = whichfield.value;
	for (i=0; i < s.length; i++) {
		if (s.charAt(i) != '\n' && s.charAt(i) != '\r' && s.charAt(i) != '\t' && s.charAt(i) != '-' && s.charAt(i) != '\)' && s.charAt(i) != '(' && s.charAt(i) != ' ' && s.charAt(i) != '.') {
			r += s.charAt(i);
		}
	}
	t = "";
	for (j=0; j < 10; j++){
		t += r.charAt(j);
		if ((j==2) || (j==5)){
			t += "-";
		}
	}
	var useareacode=1;
	whichfield.value =  t;
	phoneRegex = /^\d\d\d-\d\d\d-\d\d\d\d$/;
	if( !t.match( phoneRegex ) ) {
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	} else {
		whichfield.style.backgroundColor="white";
		return true;
	}
}

function testNull(whichfield){
	str = whichfield.value;
	if(str==""){
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	} else {
		whichfield.style.backgroundColor="white";
		return true;
	}
}

function dateValidate(whichfield){
	xval=whichfield.value;
	if (xval==''){
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	} else {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = datePat.test(xval); // is the format ok?
		if (matchArray == false) {
			whichfield.style.backgroundColor="#FFAAAA";
			return false;
		}else {
			whichfield.style.backgroundColor="white";
			return true;
		}
	}
}

function dateValidateBlankOK(whichfield){
	xval=whichfield.value;
	if (xval==''){
		whichfield.style.backgroundColor="white";
		return true;
	} else {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = datePat.test(xval); // is the format ok?
		if (matchArray == false) {
			var datePat2 = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
			var matchArray2 = datePat2.test(xval); // is the format ok?
			if (matchArray2 == false) {
				whichfield.style.backgroundColor="#FFAAAA";
				return false;
			} else {
				whichfield.style.backgroundColor="white";
				return true;
			}
		}else {
			whichfield.style.backgroundColor="white";
			return true;
		}
	}
}

function isInt(whichfield) {
	if (isNaN (whichfield.value)){
		whichfield.style.backgroundColor="#FFAAAA";
		return false;
	}else {
		whichfield.style.backgroundColor="white";
		return true;
	}
}

function formatPhone( num ) {
  if(num.length != 10) {
	_return = num;
  } else {
	_return = "(" + num.substring(0,3) + ") " + num.substring(3,6) + "-" + num.substring(6,10);
  }
  return _return;
}

function formatPhone2( num ) {
  if(num.length != 10) {
	_return = '';
  } else {
	_return = "(" + num.substring(0,3) + ") " + num.substring(3,6) + "-" + num.substring(6,10);
  }
  return _return;
}

function phoneNumbersOnly( pn ){
	var valid = "0123456789";
	var temp = '';
	var newstr = '';
	for (var i=0; i < pn.length; i++) {
		temp = pn.substring(i, i+1);
		if (valid.indexOf(temp) != "-1") {
			newstr += temp;
		}
	}
	return newstr;
}

function lettersNumbersOnly( fld ){
	var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
	var str = fld.value;
	var temp = '';
	var newstr = '';
	for (var i=0; i < str.length; i++) {
		temp = str.substring(i, i+1);
		if (valid.indexOf(temp) != "-1") {
			newstr += temp;
		}
	}
	fld.value = newstr;
}

var n;
var p;
var p1;
function reFormatPhone(){
	p=p1.value
	if(p.length==3){
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(')')
		if(d4==-1){
			pp="("+pp;
		}
		if(d5==-1){
			pp=pp+")";
		}
		//pp="("+pp+")";
		document.frmPhone.txtphone.value="";
		document.frmPhone.txtphone.value=pp;
	}
	if(p.length>3){
		d1=p.indexOf('(')
		d2=p.indexOf(')')
		if (d2==-1){
			l30=p.length;
			p30=p.substring(0,4);
			p30=p30+")"
			p31=p.substring(4,l30);
			pp=p30+p31;
			document.frmPhone.txtphone.value="";
			document.frmPhone.txtphone.value=pp;
		}
	}
	if(p.length>5){
		p11=p.substring(d1+1,d2);
		if(p11.length>3){
			p12=p11;
			l12=p12.length;
			l15=p.length
			p13=p11.substring(0,3);
			p14=p11.substring(3,l12);
			p15=p.substring(d2+1,l15);
			document.frmPhone.txtphone.value="";
			pp="("+p13+")"+p14+p15;
			document.frmPhone.txtphone.value=pp;
		}
		l16=p.length;
		p16=p.substring(d2+1,l16);
		l17=p16.length;
		if(l17>3&&p16.indexOf('-')==-1){
			p17=p.substring(d2+1,d2+4);
			p18=p.substring(d2+4,l16);
			p19=p.substring(0,d2+1);
			pp=p19+p17+"-"+p18;
			document.frmPhone.txtphone.value="";
			document.frmPhone.txtphone.value=pp;
		}
	}
	setTimeout(ValidatePhone,100)
}

function getIt(m){
	n=m.name;
	p1=m
	ValidatePhone()
}

function testphone( obj1 ){
	p=obj1.value
	p=p.replace("(","")
	p=p.replace(")","")
	p=p.replace("-","")
	p=p.replace("-","")
	if (isNaN(p)==true){
		alert("Check phone");
		return false;
	}
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
