validfm = new function(){
	this.jsondecode = function(){
		var	filter, result, self, tmp;
		if($$("toString")) {
			switch(arguments.length){
				case	2:
					self = arguments[0];
					filter = arguments[1];
					break;
				case	1:
					if($[typeof arguments[0]](arguments[0]) === Function) {
						self = this;
						filter = arguments[0];
					}
					else
						self = arguments[0];
					break;
				default:
					self = this;
					break;
			};
			if(rc.test(self)){
				try{
					result = e("(".concat(self, ")"));
					if(filter && result !== null && (tmp = $[typeof result](result)) && (tmp === Array || tmp === Object)){
						for(self in result)
							result[self] = v(self, result) ? filter(self, result[self]) : result[self];
					}
				}
				catch(z){}
			}
			else {
				//wrong data
			}
		};
		return result;
	};

	this.jsonencode = function(){
		var	self = arguments.length ? arguments[0] : this,
			result, tmp;
		if(self === null)
			result = "null";
		else if(self !== undefined && (tmp = $[typeof self](self))) {
			switch(tmp){
				case	Array:
					result = [];
					for(var	i = 0, j = 0, k = self.length; j < k; j++) {
						if(self[j] !== undefined && (tmp = JSON.encode(self[j])))
							result[i++] = tmp;
					};
					result = "[".concat(result.join(","), "]");
					break;
				case	Boolean:
					result = String(self);
					break;
				case	Date:
					result = '"'.concat(self.getFullYear(), '-', d(self.getMonth() + 1), '-', d(self.getDate()), 'T', d(self.getHours()), ':', d(self.getMinutes()), ':', d(self.getSeconds()), '"');
					break;
				case	Function:
					break;
				case	Number:
					result = isFinite(self) ? String(self) : "null";
					break;
				case	String:
					result = '"'.concat(self.replace(rs, s).replace(ru, u), '"');
					break;
				default:
					var	i = 0, key;
					result = [];
					for(key in self) {
						if(self[key] !== undefined && (tmp = JSON.encode(self[key])))
							result[i++] = '"'.concat(key.replace(rs, s).replace(ru, u), '":', tmp);
					};
					result = "{".concat(result.join(","), "}");
					break;
			}
		};
		return result;
	};

	var	c = {"\b":"b","\t":"t","\n":"n","\f":"f","\r":"r",'"':'"',"\\":"\\","/":"/"},
		d = function(n){return n<10?"0".concat(n):n},
		e = function(c,f,e){e=eval;delete eval;if(typeof eval==="undefined")eval=e;f=eval(""+c);eval=e;return f},
		i = function(e,p,l){return 1*e.substr(p,l)},
		p = ["","000","00","0",""],
		rc = null,
		rd = /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/,
		rs = /(\x5c|\x2F|\x22|[\x0c-\x0d]|[\x08-\x0a])/g,
		rt = /^([0-9]+|[0-9]+[,\.][0-9]{1,3})$/,
		ru = /([\x00-\x07]|\x0b|[\x0e-\x1f])/g,
		s = function(i,d){return "\\".concat(c[d])},
		u = function(i,d){
			var	n=d.charCodeAt(0).toString(16);
			return "\\u".concat(p[n.length],n)
		},
		v = function(k,v){return $[typeof result](result)!==Function&&(v.hasOwnProperty?v.hasOwnProperty(k):v.constructor.prototype[k]!==v[k])},
		$ = {
			"boolean":function(){return Boolean},
			"function":function(){return Function},
			"number":function(){return Number},
			"object":function(o){return o instanceof o.constructor?o.constructor:null},
			"string":function(){return String},
			"undefined":function(){return null}
		},
		$$ = function(m){
			function $(c,t){t=c[m];delete c[m];try{e(c)}catch(z){c[m]=t;return 1}};
			return $(Array)&&$(Object)
		};
	try{rc=new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')}
	catch(z){rc=/^(true|false|null|\[.*\]|\{.*\}|".*"|\d+|\d+\.\d+)$/}
    this.validmedata=null;

this.setData = function(data){
	this.validmedata = data;
};



this.classOf = function (o) {
if (undefined === o) return "Undefined";
if (null === o) return "Null";
return {}.toString.call(o).slice(8, -1);
} ;
this.isArray = function(o){
  return (o instanceof Array || o instanceof Object);
};
this.arrayindexOf = function(a,v,n){
  n = (n==null)?0:n; var m = a.length;
  for(var i = n; i < m; i++)
    if(a[i] == v)
       return i;
  return -1;
};
this.objectindexOf = function(a,v,n){
  n = (n==null)?0:n; var m = a.length;
  for(var i = n; i < m; i++)
    if(a[i] == v)
       return i;
  return -1;
};

this.Trim = function(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){return "";}
	TRIM_VALUE = this.RTrim(TRIM_VALUE);
	TRIM_VALUE = this.LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){return "";}
	else{return TRIM_VALUE;}
};

// Right Trim Function
this.RTrim = function(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){return"";}
	var iTemp = v_length -1;
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){ }
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;}
		iTemp = iTemp-1;
	}
	return strTemp;
};

//Right Trim Function
this.LTrim = function(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){return"";}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length)	{
		if(VALUE.charAt(iTemp) == w_space){}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;}
		iTemp = iTemp + 1;
	}
	return strTemp;
};

this.IsBlank = function(obj,typ){
    var tmp;
    if(typ=='input-text' || typ=='input-file' || typ=='textarea'){
    	if(this.Trim(obj.value) == ""){return true;}
    }else if(typ=='select'){
        if(this.Trim(obj.options.selectedIndex==0 || obj.options.selectedIndex==-1)){return true;}
    }else if(typ=='input-radio' || typ=='input-checkbox'){
        if(!this.IsChecked(obj)){return true;}
    }else{
        try{if(this.Trim(obj.value) == ""){return true;}}catch(e){return false;}
    }
	return false;
};
this.IsChecked =function(obj)
{
    var tmp;
    tmp = false;//blank assumed
    if(obj.length && typeof(obj.length) != "Undefined" )
    {
      var l =obj.length;for(var k=0;k<l;k++){if(obj[k].checked == true){tmp=true;break;}}
    }else if(obj.checked == true)
  	{
  		tmp=true;
  	}
    return tmp;
};
this.SetChecked =function(obj,val)
{
    var ia = this.isArray(val);
    if(obj.length && typeof(obj.length) != "Undefined" )
    {
      var l =obj.length;for(var k=0;k<l;k++){if((!ia && obj[k].value == val) || (ia && this.arrayindexOf(val,obj[k].value)!=-1)){obj[k].checked=true;}else{obj[k].checked=false;}}
    }else
  	{
  	    if(obj.value==val)obj.checked=true;
        else  obj.checked=false;
  	}
};

this.SetSelected =function(obj,val)
{
    var ia = this.isArray(val);
    if(obj.options.length && typeof(obj.options.length) != "Undefined" )
    {
      var l =obj.length;
      for(var k=0;k<l;k++)
      {if((!ia && obj.options[k].value == val) || (ia && this.arrayindexOf(val,obj.options[k].value)!=-1)){obj.options[k].selected=true;}else{obj.options[k].selected=false;}}//|| obj.options[k].text == val
    }else{}
};
this.SetValue =function(obj,typ,val,err)
{
    if( typ=='input-file'){}
    else if(typ=='input-text' || typ=='textarea'){
    	obj.value = val;
    }else if(typ=='select'){
        this.SetSelected(obj,val);
    }else if(typ=='input-radio' || typ=='input-checkbox'){
        this.SetChecked(obj,val);
    }else{
        try{obj.value = val;}catch(e){}
    }
    if(err){
       obj.className = 'form-error';
    }
};

this.SetTypeValue =function(obj,val)
{
    var objinstance =(obj.nodeName)?obj:obj[0];
  	nodetype = objinstance.nodeName.toLowerCase();
  	name = objinstance.name;
  	type = objinstance.type.toLowerCase();
    if( nodetype == 'input' && type=='file'){}
    if( (nodetype == 'input' && type!='checkbox' && type!='radio') || nodetype == 'textarea'){
    	obj.value = val;
    }else if(nodetype == 'select'){//'select-multiple','select-one'
        this.SetSelected(obj,val);
    }else if(nodetype == 'input' && (type=='checkbox' || type=='radio')){//(typ=='input-radio' || typ=='input-checkbox'){
        this.SetChecked(obj,val);
    }else{
        try{obj.value = val;}catch(e){}
    }
};
//from jmform
//does the actual checking
this.CheckForm =function(theform) {
        var form_err = '';
        for (var i=0;i<theform.elements.length;i++) {
            theform.elements[i].disabled = 'true';
            if (theform.elements[i].getAttribute('required') != undefined && theform.elements[i].getAttribute('required') == 'true') {
                if (theform.elements[i].value == undefined || theform.elements[i].value == '') {
                    //for "select" type elements
                    if (theform.elements[i].length != undefined)
                    {
                        if (theform.elements[i].selectedIndex == undefined || theform.elements[i].options[theform.elements[i].selectedIndex] == undefined || theform.elements[i].options[theform.elements[i].selectedIndex].value == '' ) {
                            form_err = form_err + '\n  -- ' + theform.elements[i].name;
                        }
                    }
                    else form_err = form_err + '\n  -- ' + theform.elements[i].name;
                 }
            }
        }
        for (var i=0;i<theform.elements.length;i++) theform.elements[i].disabled = '';

        if (form_err != '') {
            form_err = "The following fields are required:" + form_err;
            alert(form_err);
            return false;
        } else return true;
};

//for full browser compatibility, instead of javascript: void or similar
this.doNothing =function(){};
this.actTemp='';
this.prer_req='';
this.poer_req=' is required';
this.validateFormPg = function(theform){
   var flg = true;
   if(this.validmedata && this.validmedata.length){
        var l=this.validmedata.length;
        var fdnm ='';
        if(l){
          for(var i=0;i<l;i++){
            fdnm =this.validmedata[i].fdnm;
            if(this.objectindexOf(this.validmedata[i].validations,'required')!=-1){
              if(theform[fdnm] && typeof(theform[fdnm])!= 'undefined'){
              r=this.IsBlank(theform[fdnm],this.validmedata[i].type);
              if(r){
                  alert(this.prer_req+this.validmedata[i].name+this.poer_req);try{theform[fdnm].focus();}catch(e){}flg=false;break;
              }}
            }
          }
       }
    }
    return flg;
   /*if(this.validmedata!=null){for(var ob in this.validmedata){}}return false;*/
};
/*this.refillFormPg = function(){if(this.validmedata!=null){}}*/
};
function SendRequest(theform) {
    var formConf=false;
    if(theform){var r; try{
    if (theform.action != 'javascript:validfm.doNothing();' && theform.action != '')               validfm.actTemp = theform.action;
    theform.action = 'javascript:validfm.doNothing();';
    var formConf = validfm.validateFormPg(theform);
    }catch(e){
    //alert("error in validation: "+ e.description );
    formConf=true;}}else{//alert("error in form-validation ");
    formConf=true;}
    if (formConf) {
            theform.action = validfm.actTemp;
            theform.onsubmit = '';
            theform.submit();
    }
}