// Copyright 1999-2006 eSolutions Limited
// Obtain written permission before using any part of this code from info@esol-group.com.

function xchkTime(s){ // 12|24 format OK
	s=s.replace(/(^[ ]*)|([ ]*$)/g,'')
	var h12=0,h=getMField(0,s,':'),m=getMField(1,s,':')
	if(m.search(/pm$/i)>=0){m=m.replace(/pm$/i,'');h12=12}else{m=m.replace(/am$/i,'')}
	if(h.replace(/[0-9]*/,'').length!=0 || m.replace(/[ 0-9]*/,'').length!=0) return false
	h=parseInt(h,10)+h12; m=parseInt(m,10)
	return h>=0 && h<=24 && m>=0 && m<=60 && getMField(2,s,':')==''
}

function xchkDate(s,t,yf){ // t=dmy|mdy|ymd, yf=2|4
	var d1,y,m,de
	function parseMDate(n){return parseInt(getMField(n,s,de),10)}
	if(yf==2)s=s.substring(0,s.length-2)+'19'+s.substr(s.length-2)
	switch(t){
		case 'dmy': de='/';d1=new Date( y=parseMDate(2), m=parseMDate(1)-1, d=parseMDate(0) ); break
		case 'mdy': de='/';d1=new Date( y=parseMDate(2), m=parseMDate(0)-1, d=parseMDate(1) ); break
		case 'ymd': de='/';d1=new Date( y=parseMDate(0), m=parseMDate(1)-1, d=parseMDate(2) ); break
		case 'ansi': de='.';d1=new Date( y=parseMDate(0), m=parseMDate(1)-1, d=parseMDate(2) ); break
	}
	return((s.replace(/([^\/])/g,'')=='//' || s.replace(/([^\.])/g,'')=='..') && (s.search(/([^\/0-9])/)==-1 || s.search(/([^\.0-9])/)==-1) &&
		y==d1.getFullYear() && m==d1.getMonth() && d==d1.getDate() && y>=1900 && y<=2100)
}

function xchkEmail1(s){
	return ( // a@b.c, a.b@c.d
		s.indexOf('@')>0 && s.indexOf('@')<s.lastIndexOf('.')-1 && // not begin w/ @ && contains @x.
		s.charAt(s.length-1)!='.' && s.replace(/([^\@])/g,'')=='@' && // not end w/ . && only contains 1 @
		s.charAt(s.indexOf("@")+1)!='.' && s.charAt(s.indexOf("@")-1)!='.' && // not contain @. (a@.b.c) and not contain .@ (a.@b.c)
    s.indexOf("'")==-1 && s.indexOf('"')==-1) // not contain funny chars
}
function xchkEmail(s,max){
  var ar=s.replace(/,/g,';').split(';')
  if(ar.length>max && max!=0)return false // if more than max and max!=0 (unlimited)
  for(var f=true,i=0;i<ar.length;i++)f=f&&xchkEmail1(ar[i])
  return f
}

//loop for selected options to support multiple select listbox - was "return obj.options[obj.selectedIndex].value"
function getSelectValue(obj){for(var o='',i=0;i<obj.options.length;i++)if(obj.options[i].selected)o+=(o==''?'':',')+obj.options[i].value; return o}
function xValidateDoc(fm,ar){ // type format: type:opt,nosquo,nodquo,noquo
	var w=5
	this.length=ar.length/w
	this.getName=function(n){return ar[n*w]}
//	this.getType=function(n){return ar[n*w+1]}
	this.getType=function(n){return getMField(0,ar[n*w+1],':')}
    this.getType_isOpt=function(n){return getMField(1,ar[n*w+1],':').indexOf('opt')>=0} // return boolean
    this.getType_noQuo=function(n){return getMField(1,ar[n*w+1],':').indexOf('noquo')>=0} // return boolean
    this.getType_noSp=function(n){return getMField(1,ar[n*w+1],':').indexOf('nosp')>=0} // return boolean
	this.getMin=function(n){return ar[n*w+2]}
	this.getMax=function(n){return ar[n*w+3]}
	this.getMsg=function(n){return ar[n*w+4]}
//	this.getElement=function(n){return getMField(0,this.getType(n),':')=='ar'?null:eval('document.'+fm+'.'+this.getName(n))}
//	this.getElement=function(n){return this.getType(n)=='ar'?null:eval('document.'+fm+'.'+this.getName(n))}
	this.getElement=function(n){return this.getType(n)=='ar'?null:eval('document.'+fm)[this.getName(n)]}
	this.getValue=function(n){return this.getElement(n).value}
	this.getRadioValue=function(n){var s=getRadioValue(this.getElement(n)); return s==null?'':s}
	this.getSelectValue=function(n){return getSelectValue(this.getElement(n))}
	this.trimValue=function(n){this.getElement(n).value=this.getValue(n).replace(/(^[ ]*)|([ ]*$)/g,'')}
	this.trimValueInt=function(n){this.getElement(n).value=this.getValue(n).replace(/([^0-9])/g,'')}
	this.trimValueIntK=function(n){this.getElement(n).value=this.getValue(n).replace(/([^0-9kK])/g,'')}
}
function xValidate(fm,ar){
	this.doc=new xValidateDoc(fm,ar)
	this.multi=false
  this.alert=this.focus=true
	this.light=false // document.all
	this.debug=false
	this.cssError='background:white url(/sys/xchkBg.gif) top right no-repeat; border:2px solid #A00000'
	this.cssErrorOpt='background:white; border:2px solid #A00000'
	var mMsg, elError, isLighted=false, pDoc=this.doc
  this.extOK=function(n){ //extended error checking, true if OK or no error
    var s=pDoc.getValue(n)
    if(pDoc.getType_noQuo(n)){if(s.indexOf("'")!=-1||s.indexOf('"')!=-1)return(false)}
    if(pDoc.getType_noSp(n)){if(s.indexOf(" ")!=-1)return(false)}
    return true
  }
/*
  if(window.location.href.toLowerCase().indexOf('www.esol-group.com')==-1){
    alert('This code has been illegally pirated from eSolutions.\n'
      +'Usage and modification of this code is illegal and will be \nprosecuted to the fullest extend of the law.\n'
      +'Please send inquiry or report abuse to info@esol-group.com.')
  }
*/
	this.getError=function(){return mMsg}
	this.doFocus=function(){if(elError)if(elError.type!='hidden'){try{elError.focus()}catch(e){}}}
	this.doLight=function(){if(elError){elError.style.cssText2=elError.style.cssText; elError.style.cssText+=';'+(elError.opt?this.cssErrorOpt:this.cssError)}} // add css to existing cssText
	this.noLight=function(){
		for(var i=0; i<pDoc.length; i++)
			if(this.light && typeof document[fm][pDoc.getElement(i)] != 'undefined')
        if(pDoc.getElement(i)){
          // pDoc.getElement(i).style.cssText='' // this will remove all existing styles, so don't do it, remove old this.cssError & this.cssErrorOpt instead
          // pDoc.getElement(i).style.cssText=pDoc.getElement(i).style.cssText.replace(new RegExp(';*('+this.cssError+')|('+this.cssErrorOpt+')','gi'),'')
          if(pDoc.getElement(i).style.cssText2!='undefined') pDoc.getElement(i).style.cssText=pDoc.getElement(i).style.cssText2
        }else{
          var tDoc=new xValidate(fm,eval(pDoc.getName(i)))
          tDoc.noLight()
        }
	}
	this.onError=function(n){
		if(elError=pDoc.getElement(n))elError.opt=pDoc.getType_isOpt(n)//isOptional(n)
		mMsg=pDoc.getMsg(n)+'\n'+(this.multi?mMsg:'')
		if(this.light&&this.multi)this.doLight()
		if(this.focus)this.doFocus()
	}
	this.onErrorAr=function(n){
		this.onError(n)
		if(!this.multi)this.noLight()
		var xv1=new xValidate(fm,eval(pDoc.getName(n))); xv1.cssError=this.cssError
		xv1.alert=this.alert; xv1.focus=this.focus; xv1.light=this.light; xv1.multi=true
		for(var i=xv1.doc.length-1;i>=0;i--)xv1.validate1(i)
	}
	this.validateAr=function(n, isOpt){
		var isOK, xv1=new xValidate(fm,eval(pDoc.getName(n)))
    isOpt?isOK=true:isOK=false
		xv1.alert=xv1.focus=xv1.light=false
		for(var i=xv1.doc.length-1;i>=0;i--)isOpt?isOK=isOK&&xv1.validate1(i):isOK=isOK||xv1.validate1(i)
		return isOK
	}
	this.getArValue=function(n){
		var retVal='', xv1=new xValidate(fm,eval(pDoc.getName(n)))
		xv1.alert=xv1.focus=xv1.light=false
		for(var i=xv1.doc.length-1;i>=0;i--)retVal=retVal+xv1.doc.getValue(i)
		return retVal
	}

  this.validate1=function(n,isArOpt){
		if(typeof document[fm][pDoc.getName(n)] == 'undefined' && pDoc.getType(n) != 'ar'){
		  if(this.debug){
		    alert(fm+'.'+pDoc.getName(n)+' is undefined.')
		    return false
		  }else{
		    return true
		  }
    }
		var isOK=true
    isOpt=pDoc.getType_isOpt(n)//isOptional(n)
		switch(pDoc.getType(n)){
			case('txt'):
        pDoc.trimValue(n)
        if(isOpt&&(pDoc.getValue(n)==''))return true
        if(!(isOK=(pDoc.getValue(n).length>=pDoc.getMin(n) && this.extOK(n))))this.onError(n)
        // new max check. ignore if not numeric and -1
        var max=pDoc.getMax(n)
        if(isOK && !isNaN(max) && max>0){
          if(!(isOK=(pDoc.getValue(n).length<=max)))this.onError(n)
        }
        return isOK
			case('lst'):if(isOpt&&(pDoc.getSelectValue(n)==''))return true; if(!(isOK=pDoc.getSelectValue(n).length>=pDoc.getMin(n)))this.onError(n); return isOK
			case('radio'):if(isOpt&&(pDoc.getRadioValue(n)==''))return true; if(!(isOK=pDoc.getRadioValue(n).length>=pDoc.getMin(n)))this.onError(n); return isOK
			case('int'):// pDoc.trimValueInt(n)
			  if(isOpt&&(pDoc.getValue(n)==''))return true
				var v=parseInt(pDoc.getValue(n),10)
				if(!(isOK=(v>=pDoc.getMin(n)&&v<=pDoc.getMax(n))))this.onError(n)
				return isOK
			case('intK'):pDoc.trimValueIntK(n); if(isOpt&&(pDoc.getValue(n)==''))return true; var v=parseInt(pDoc.getValue(n).replace(/k/gi,'000'),10); if(!(isOK=(v>=pDoc.getMin(n)&&v<=pDoc.getMax(n))))this.onError(n); return isOK
			case('time'):pDoc.trimValue(n); if(isOpt&&(pDoc.getValue(n)==''))return true; if(!(isOK=xchkTime(pDoc.getValue(n),pDoc.getMin(n))))this.onError(n); return isOK
			case('date'):pDoc.trimValue(n); if(isOpt&&(pDoc.getValue(n)==''))return true; if(!(isOK=xchkDate(pDoc.getValue(n),pDoc.getMin(n),pDoc.getMax(n))))this.onError(n); return isOK
			case('email'):pDoc.trimValue(n); if(isOpt&&(pDoc.getValue(n)==''))return true; if(!(isOK=xchkEmail(pDoc.getValue(n),pDoc.getMax(n))))this.onError(n); return isOK
			case('ar'): if(isOpt&&(this.getArValue(n)==''))return true; if(!(isOK=this.validateAr(n,isOpt)))this.onErrorAr(n); return isOK
      case('rexp'): if(isOpt&&(pDoc.getValue(n)==''))return true; if(!(isOK=pDoc.getMin(n).test(pDoc.getValue(n))))this.onError(n); return isOK
      case('custom'): if(isOpt&&(pDoc.getValue(n)==''))return true; if(!(isOK=eval(pDoc.getMin(n))(pDoc.getMax(n))))this.onError(n); return isOK
		}
	}
  this.validate=function(){
		var passed=true
		mMsg=''; elError=null; this.noLight()
		for(var i=pDoc.length-1; i>=0; i--){passed=this.validate1(i)&&passed;}
		if(passed)this.noLight();else{
			if(this.light){if(!this.multi&&elError){this.noLight()}this.doLight()}
			if(this.alert && this.getError().replace(/\n$/,''))alert(this.getError())
		}
		return passed
	}
}
