function TrialMgr()
  {

  	this.formName = 'trialform';
  	this.formObject = 'formtrial';
	this.statusMessage = 'statusMessage';
  	this.baseUrl = '/mpassofferv4/v5/';
  	this.imageTrue = 'field_true.gif';
  	this.imageFalse = 'field_false.gif';
  	this.imageLoad = 'field_load.gif';
	this.noteObject = 'fieldnote_';
	this.errTransition = '<br />';
	this.submitUrl = "/createAccount.php?action=createAccount";
	
	this.ErrArray = new Object();

	this.CheckDyn = false;

	this.dynReq = new srvReq();

	this.use_noteObject = true;
	this.formBlocked = false;


	this.field = new Object();

	this.field.fname = new Object();
	this.field.fname.type = "text";
	this.field.fname.publicname = "First name";
	this.field.fname.min = 2;
	this.field.fname.max = 30;
	this.field.fname.mandatory = 1;
	this.field.fname.regexp = false;
	this.field.fname.special = false;
	this.field.fname.valid = false;
	
	this.field.lname = new Object();
	this.field.lname.type = "text";
	this.field.lname.publicname = "Last name";
	this.field.lname.min = 2;
	this.field.lname.max = 30;
	this.field.lname.mandatory = 1;
	this.field.lname.regexp = false;
	this.field.lname.special = false;
	this.field.lname.valid = false;


	this.field.myemail = new Object();
	this.field.myemail.type = "text";
	this.field.myemail.publicname = "Email address";
	this.field.myemail.min = 2;
	this.field.myemail.max = 30;
	this.field.myemail.mandatory = 1;
	this.field.myemail.regexp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	this.field.myemail.special = "mailCheck";
	this.field.myemail.valid = false;

	this.field.login = new Object();
	this.field.login.type = "text";
	this.field.login.publicname = "Username";
	this.field.login.min = 5;
	this.field.login.max = 12;
	this.field.login.mandatory = 1;
	this.field.login.regexp = /^([a-zA-Z0-9]+)$/;
	this.field.login.special = "loginCheck";
	this.field.login.valid = false;

	this.field.mphone = new Object();
	this.field.mphone.type = "text";
	this.field.mphone.publicname = "Mobile phone number";
	this.field.mphone.min = 6;
	this.field.mphone.max = 24;
	this.field.mphone.mandatory = 1;
	this.field.mphone.regexp = false;
	this.field.mphone.special = "numCheck";
	this.field.mphone.valid = false;


	this.field.pict = new Object();
	this.field.pict.type = "text";
	this.field.pict.publicname = "Security picture text";
	this.field.pict.min = 6;
	this.field.pict.max = 24;
	this.field.pict.mandatory = 1;
	this.field.pict.regexp = false;
	this.field.pict.special = "pictCheck";
	this.field.pict.valid = false;

	

	 this.initialize = function()
    	{




			for(var i in this.field)
				{
									
					window.document['pict_'+i].src = this.baseUrl + this.imageFalse;
							
				}


	    }
	    
	   this.checkAll = function()
	   	{
	   	
	   		if(!this.formBlocked)
	   			{
			this.resetError();
			

			for(var i in this.field)
				{

					if(!this.field[i].valid) {				
					
						Vres = this.checkField(i);
						if(Vres == "load") {
							this.field[i].valid = true;
						} else if(Vres)
							{
								window.document['pict_'+i].src = this.baseUrl + this.imageTrue;
						
							} else {
						
								window.document['pict_'+i].src = this.baseUrl + this.imageFalse;
						
							}
					}	
				}

		   	err = false;
	   		for(var i in this.field)
				{
					if(!this.field[i].valid) {				
						err = true;
					}	
				}
			
			
			if(err == false)
				{
					q = this.doPS(window.document[this.formName]);
					this.dynReq.resIsJs = true;
			 		this.dynReq.GET(this.submitUrl + q);
					this.w2w(this.statusMessage,'<div><table border="0"><tr><td align="center" valign="middle"><img src="/mpassofferv4/v5/' + this.imageLoad + '" border="0" /></td><td><span style="font-size: 14px; color: #CC0000; font-weight: bold; ">Processing your account creation, please wait.</span></td></tr></table></div>');
	
					for(var i in this.field)
						{
							window.document[this.formName][i].disabled = true;
						
						}
							
					this.formBlocked = true;		
	
				} else {
				
				
					msg = "The form is not completed correctly, please correct the following:\n\n";
					for(var i in this.field)
						{
							if(!this.field[i].valid)
								{
									msg += "\t\b" + this.field[i].publicname + "\b: " + this.field[i].errMsg + "\n\n";
								}
						}
					alert(msg);	
				
				}
				
				}
	
	   	}
	   this.doPS = function(VF) { res = ''; for(i=0; i < VF.elements.length; i++) { if(VF.elements[i].name.length > 0) { res += '&' + VF.elements[i].name + '=' + this.crep(encodeURI(VF.elements[i].value)); } } return res; }

		this.crep = function (mstr) { res = mstr.replace(/&/g,'%26'); res = mstr.replace(/\//g,'%2F'); return res; }


	  this.fieldVerification = function(fname)
	   	{

			this.resetError();
			
			Vcheck = this.field[fname];	
	  		Cform = window.document[this.formName];
	  		Cvalue = Cform[fname].value;
			Vres = this.checkField(fname);	
			if(Vres == 'load')
				{
							if(this.use_noteObject) {
								this.w2w(this.noteObject + fname,'Checking value');
							}
							window.document['pict_'+fname].src = this.baseUrl + this.imageLoad;
							
				} else {
				
					if(Vres)
						{
							if(this.use_noteObject) {
								this.w2w(this.noteObject + fname,'');
							}
							this.fieldValid(fname);
						} else {
							if(this.use_noteObject) {
								this.w2w(this.noteObject + fname,this.ErrArray[fname]);
							}
							window.document['pict_'+fname].src = this.baseUrl + this.imageFalse;
						
						}
	   			}
	   	
	   	}
	 this.fieldValid = function(fname)
	 	{
	 	
	 		this.field[fname].valid = true;
	 		window.document['pict_'+fname].src = this.baseUrl + this.imageTrue;
	 		if(this.use_noteObject) {
								this.w2w(this.noteObject + fname,'');
			}
	 	}
	  this.fieldInvalid = function(fname,errMsg)
	 	{
	 		 this.resetError();
	 	
	 		this.field[fname].valid = false;
	 		window.document['pict_'+fname].src = this.baseUrl + this.imageFalse;
	 		if(this.use_noteObject) {
	 			if(errMsg) {
					this.w2w(this.noteObject + fname,errMsg);
					this.addError(fname,errMsg);	 			
	 			} else {
					this.w2w(this.noteObject + fname,'Invalid value for field ' + this.field[fname].publicname);
					
				}
			}
	 	} 	
	   	
	  this.resetError = function()
	  	{
	  	
	  		this.ErrArray = new Object();
	  	
	  	}
	  	
	  this.addError = function(fname,err)
	  	{
	  		if(!this.ErrArray[fname])
	  			{
		  			this.ErrArray[fname] = "";	  			
	  			}
	  		this.ErrArray[fname] += err + this.errTransition;
	  		this.field[fname].errMsg = err; 
	  	
	  	}
	  	
	  this.checkField = function(fname)
	  	{
	  	
	  		Vcheck = this.field[fname];	
	  		Cform = window.document[this.formName];
	  		
	  		Cvalue = Cform[fname].value;
			haveErr = false;

	  		
		  		if(Cvalue.length < Vcheck.min)
		  			{
	  			
	  					msg = "The field " + Vcheck.publicname + " is too short, minimum: " + Vcheck.min + " characters.";
	  					this.addError(fname,msg);
						haveErr = true;
	  			
	  				}

		  		if(Cvalue.length > Vcheck.max)
		  			{
	  			
		  				msg = "The field " + Vcheck.publicname + " is too long, maximum: " + Vcheck.max + " characters.";
	  					this.addError(fname,msg);
						haveErr = true;
	  			
	  				}

	  			if(Vcheck.regexp)
	  				{

	  					pattern = Vcheck.regexp;
	  					if(!pattern.test(Cvalue))
	  						{
	  						
	  							msg = "The value " + Cvalue + " is invalid for field " + Vcheck.publicname + " please check.";
							  	this.addError(fname,msg);
								haveErr = true;

	  						
	  						}
	  				

	  				}
	  			
	  			if(Vcheck.special  && haveErr == false && this.CheckDyn)
	  				{
	  				
	  					Veval = "this." + Vcheck.special + "('" + fname + "','" + Cvalue + "');";
	  					res = eval(Veval);
	  					return "load";	  				
	  				} 
	  				

	  		if(haveErr) {
	  			return false;
	  		} else {
	  		
	  			return true;
	  		}

		
	  	}
	  	
	  	this.resetForm = function() {
	  	
			for(var i in this.field)
				{
				
					window.document[this.formName][i].value = '';
					this.field[i].valid = false;
				}
	  	
	  	}
	  	
	  	
	    this.setValue = function (fname,value) {
	    
	    	if(this.field[fname])
	    		{
	    		
	    			Vform = window.document[this.formName];
	    			Vform[fname].value = value;
	    			
	    		}
	    
	    }
	   
		this.w2w = function (w,s) {  document.getElementById(w).innerHTML = s; }
		this.c2c = function (w) { return document.getElementById(w).innerHTML; }
		this.gobj = function (w) { return document.getElementById(w); }
	    
	    
	    // Special function check
	    
	     this.setPicture = function (imgsrc) {
			MyKa = new kaptcha();
			
	    	window.document[MyKa.pictObjectName].src = MyKa.baseUrl + imgsrc + '.png';
	    	window.document[this.formName].imgname.value = imgsrc ;
	    }
	    
	    this.pictCheck = function (field,value)
	    	{
	    
	    		KA = new kaptcha();
	    		KA.checkKaptcha(window.document[KA.pictObjectName].src,value);
	    	
	    	
	    	}
	    	
	    	
	    this.loginCheck = function (field,value)
	    	{
	    
	    			
	    			this.dynReq.resIsJs = true;
	    			q = "/createAccount.php?action=checkLogin";
			 		q += '&login=' + value;
			 		q += '&fieldname=' + field;
			 		this.dynReq.GET(q);
	    	
	    	}	
	    this.numCheck = function (field,value)
	    	{
	    		
	    			this.dynReq.resIsJs = true;
	    			q = "/createAccount.php?action=checkMobile";
			 		q += '&mphone=' + value;
			 		q += '&fieldname=' + field;
			 		this.dynReq.GET(q);
	    	
	    	}
	    this.mailCheck = function (field,value)
	    	{
	    		
	    			this.dynReq.resIsJs = true;
	    			q = "/createAccount.php?action=checkEmail";
			 		q += '&myemail=' + value;
			 		q += '&fieldname=' + field;
			 		this.dynReq.GET(q);
	    	
	    	}	
}



function srvReq()
  {
  this.url = '';
  this.busy = false;
  this.xmlhttp = null;
  this.resTarget = false;
  this.resIsJs = false;
  this.animReq = false;

  // reset object properties
  this.reset = function()
    {
    // set unassigned event handlers
    this.onloading = function(){ };
    this.onloaded = function(){ };
    this.oninteractive = function(){ };
//    this.oncomplete = function(){ };
    this.onabort = function(){ };
    this.onerror = function(){ };
    
    this.url = '';
    this.busy = false;
    this.xmlhttp = null;
    }

	this.oncomplete = function()
		{ 
			if(this.resIsJs) {
					eval(this.get_text());

				} else {
		
			if(this.resTarget)
				{
					MAw2w(this.resTarget,this.get_text());
				
				}  
			}
		}
  // create HTMLHTTP object
  this.build = function()
    {
    if (window.XMLHttpRequest)
      this.xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject)
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    else
      {
      
      }
    }

  // sedn GET request
  this.GET = function(url)
    {
    this.build();

    if (!this.xmlhttp)
      {
      this.onerror(this);
      return false;
      }
	if(this.resTarget)
		{
			if(this.animReq) {
				MAw2w(this.resTarget,getProReq(this.resTarget));				
			}
		}

	 


    var ref = this;
    this.url = url;

    this.busy = true;

    this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };

    	this.xmlhttp.open('GET','' + url);
 
    this.xmlhttp.send(null);
    };


  this.POST = function(url, a_param)
    {
    // not implemented yet
    };


  // handle onreadystatechange event
  this.xmlhttp_onreadystatechange = function()
    {
    if(this.xmlhttp.readyState == 1)
      this.onloading(this);

    else if(this.xmlhttp.readyState == 2)
      this.onloaded(this);

    else if(this.xmlhttp.readyState == 3)
      this.oninteractive(this);

    else if(this.xmlhttp.readyState == 4)
      {
      try {
        if (this.xmlhttp.status == 0) {
          this.onabort(this);
        } else if(this.xmlhttp.status == 200) {
			//undoTrans(this.resTarget);

          this.oncomplete(this);
       } else {
          this.onerror(this);
		}
        this.busy = false;
        }
      catch(err)
        {
        this.onerror(this);
        this.busy = false;
        }
      }
    }

  // getter method for HTTP headers
  this.get_header = function(name)
    {
    return this.xmlhttp.getResponseHeader(name);
    };

  this.get_text = function()
    {
    return this.xmlhttp.responseText;
    };

  this.get_xml = function()
    {
    return this.xmlhttp.responseXML;
    };

  this.reset();
  
  }  // end class 

 function kaptcha() {
 
 	this.baseUrl = "/imglib/ImageGenerator/dynimg/";
 	this.checkUrl = "/secuimgcheck.php";
 	this.pictObjectName = "secupict";
 	
 	this.checkKaptcha = function (imgname,Vkey) {

		q = this.checkUrl + '?k=' + Vkey + '&i=' + imgname;		
 		SR = new srvReq();
 		SR.resIsJs = true;
 		SR.GET(q);
 	}
 
 	this.renewPict = function(imgname)
 		{
 		
 			window.document[this.pictObjectName].src = this.baseUrl + imgname + '.png';
 		
 		}
 
 }
 
var TM = new TrialMgr();
var KA = new kaptcha(); 
