// Copyright 2001 eSolutions Ltd.
// Obtain written permission before using any part of this code from info@esol-group.com.

function getMField(n,s,d){var ar=s.split(d);return(n<ar.length?ar[n]:'')}

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>=1800 && y<=2200)
}

function xchkEmail(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)
}
function getSelectValue(obj){return obj.options[obj.selectedIndex].value}
function xValidateDoc(fm,ar){
	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.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.getValue=function(n){return this.getElement(n).value}
	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=this.alert=this.focus=true;
	this.light=document.all
	this.cssError='background:white url(xchkBg.gif) top right no-repeat; border:2 solid #A00000'
	this.cssErrorOpt='background:lightSteelBlue'
	var mMsg, elError, isLighted=false, pDoc=this.doc
	function isOptional(n){return getMField(1,pDoc.getType(n),':')=='opt'}
	this.getError=function(){return mMsg}
	this.doFocus=function(){if(elError)if(elError.type!='hidden')elError.focus()}
	this.doLight=function(){if(elError)elError.style.cssText=elError.opt?this.cssErrorOpt:this.cssError}
	this.noLight=function(){
		for(var i=0; i<pDoc.length; i++)
			if(this.light)if(pDoc.getElement(i))pDoc.getElement(i).style.cssText=''; else{var tDoc=new xValidate(fm,eval(pDoc.getName(i))); tDoc.noLight()}
	}
	this.onError=function(n){
		if(elError=pDoc.getElement(n))elError.opt=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){
		var isOK=true;
    isOpt=isOptional(n)
		switch(getMField(0,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.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('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))))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
		}
	}
  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)alert(this.getError());
		}
		return passed
	}
}
