Zadig.Form = function (element, args)
{
	if(element.jsClass != undefined) return;
	this.element = element;
	this.element.jsClass = this;
	this.url = args.url;
	this.dialogSuccessMsg = args.dialogSuccessMsg;
	this.dialogSuccessElement = args.dialogSuccessEl;
	Zadig.addListener('submit',element,Zadig.bind(this.onSubmit, this));
	this.refOnResponse= Zadig.bind(this.onResponse, this);
	this.hint = document.createElement("DIV");
	this.hint.className = 'hint';
	this.element.appendChild(this.hint);
	this.sending = false;
}
Zadig.Form.prototype = 
{
'onSubmit':function(e)
{
	//if(this.sending){Zadig.stopEvent(e); return false;}
	//this.sending = true;
	if(e!=null)Zadig.stopEvent(e);
	var post = '';
	for(var i=0; i< this.element.elements.length; i++)
	{
		if(this.element.elements[i].name == null) continue;
		if(this.element.elements[i].name == '') continue;
		if(this.element.elements[i].constrain != null)
			if(!this.element.elements[i].constrain.check())
			{
				this.hint.innerHTML = this.element.elements[i].constrain.hint;
				if(e!=null)Zadig.stopEvent(e);
				return false;
			} 
		if(this.element.elements[i].type=='radio' && this.element.elements[i].checked==false) continue;	
		post+=this.element.elements[i].name +'='+ escape(this.element.elements[i].value)+'&';
	}
	new Zadig.Request(this.url, true, post, this.refOnResponse);
	return false; 
},
'onResponse':function(data)
{
	var response;
	this.sending = false;
	try {
		eval('response = ' + data);
	}
	catch(e){this.hint.innerHTML=data;}
	
	if(response.info.contentType=='success')
  {
   
    this.success(response.content.message);
    return;  
  }
  if(response.info.contentType=='error')
  {
    this.hint.innerHTML = response.content.message;
    return;  
  }
  if(response.info.contentType=='command')
  {
    eval(response.content);
    return;  
  }
  this.hint.innerHTML = data;
}
,
'success':function(message)
{
		if(this.dialogSuccessMsg != null)
		{
			this.element.parentNode.style.height = 'auto';
			this.element.innerHTML = "<div class='success'>"+message+"</div>";
			return;
		}
		if(this.dialogSuccessElement != null)
		{
			this.element.parentNode.style.height = 'auto';
			this.element.innerHTML = this.dialogSuccessElement.innerHTML ;
			return;
		}
	 this.hint.innerHTML = message;
	 
}
};
Zadig.Form2 =  function (element, args)
{
	this.superClass(element,args);
}

Zadig.Form2.prototype = 
{
'success':function(message)
{
	var x = Zadig.showAlert(message);
	Zadig.posAt(x.element,'cm', this.element);
},
'errorHint':function(el,message)
{
	var i = document.createElement('span')
	i.className = 'errorHint';
	i.innerHTML = "<span class='beginCap'></span><span class='label'>"+message+"</span><span class='endCap'></span>";
	document.body.appendChild(i);
	Zadig.posAt(i,'b',el,{x:3,y:-3});
	setTimeout(function(){document.body.removeChild(i)},4500);
}
};
Zadig.inherit(Zadig.Form, Zadig.Form2);


Zadig.ComboBox = function(element, args)
{
  this.element = element;
  for(var i = 0; i<element.length; i++)
  {
  	if(element.options[i].value != args.value) continue;
  	element.selectedIndex = i; break;
  }
};

Zadig.FieldConstrain = function(element, args)
{
  this.element = element;
  element.constrain = this;
  this.pattern = args.pattern;
  this.hint = args.hint;
  Zadig.addListener('blur', this.element, Zadig.bind(this.focusOut, this));
};



Zadig.FieldConstrain.prototype.focusOut = function(e)
{
  if(this.check()){ this.element.form.jsClass.hint.innerHTML = ''; return;}
  this.element.form.jsClass.hint.innerHTML = this.hint;
};
Zadig.FieldConstrain.prototype.check = function()
{
  if(this.element.value.match(this.pattern))return true;
  return false;
};

Zadig.FieldConstrainPrevious = function(element, args)
{
  this.element = element;
  element.constrain = this;
  this.previous = args.previous;
  this.hint = args.hint;
  Zadig.addListener('blur', this.element, Zadig.bind(this.onBlur, this));
};

Zadig.FieldConstrainPrevious.prototype.onBlur = function(e)
{
  if(this.element.value==this.element.form.elements[this.previous].value){this.element.form.jsClass.hint.innerHTML = ''; return;}
  if(this.element.form.jsClass.errorHint)this.element.form.jsClass.errorHint(this.element, this.hint);
  else this.element.form.jsClass.hint.innerHTML = this.hint;
};
Zadig.FieldConstrainPrevious.prototype.check = function()
{
  if(this.element.value==this.element.form.elements[this.previous].value)return true;
  return false;
};

//cpf
Zadig.MatchCPF = function(element, args)
{
	this.element = element;
  element.constrain = this;
  this.hint = args.hint;
  Zadig.addListener('blur', this.element, Zadig.bind(this.onBlur, this));
}

Zadig.MatchCPF.prototype.check = function()
{
	var cpf = this.element.value.replace(/[^\d]/g,'');
	var i2 = 0;
	var t1 = 0;
	var t2 = 0

	for(var i=10; i>=2; i--)
	{
		t1 += parseInt(cpf.charAt(i2))*i;
		t2 += parseInt(cpf.charAt(i2++))*(i+1);
	}

	var r = t1 % 11;
	var t = (t1 - r) / 11; 
	var d1 = r < 2 ? 0 : 11 - r;
	
	t2 += 2 * d1;
	var r2 = t2 % 11;
	var d2 = r2 < 2 ? 0 : 11 - r2;
	
	return cpf.match( new RegExp('.{9}'+d1+''+d2+'$') );
}

Zadig.MatchCPF.prototype.onBlur = function(e)
{
	if(this.check())
	{
		this.element.form.jsClass.hint.innerHTML='';
  	return;
	}
  this.element.form.jsClass.hint.innerHTML= this.hint;
}

//cnpj
Zadig.MatchCNPJ = function(element, args)
{
	this.element = element;
  element.constrain = this;
  this.hint = args.hint;
  Zadig.addListener('blur', this.element, Zadig.bind(this.onBlur, this));
}
Zadig.MatchCNPJ.prototype.onBlur = function(e)
{
	if(this.check())
	{
		this.element.form.jsClass.hint.innerHTML='';
  	return;
	}
  this.element.form.jsClass.hint.innerHTML= this.hint;
}
Zadig.MatchCNPJ.prototype.check = function()
{
	var cnpj = this.element.value.replace(/[^0-9]/g,'');
	var i2 = 0;
	var t1 = 0;
	var t2 = 0
	var const1 = '543298765432';
	var const2 = '6543298765432';

	for(var i=0; i<12; i++)
	{
		var r = parseInt(cnpj.charAt(i))*parseInt(const1.charAt(i));
		t1 += r;
		var r2 = parseInt(cnpj.charAt(i))*parseInt(const2.charAt(i));
		t2 += r2;
		i2++;
	}
	r = t1%11;

	var t = (t1 -r)/11; 
	var d1 = r < 2 ? 0 : 11-r;
	
	t2 += 2*d1;
	r2 = t2 % 11;
	t = (t2 -r2)/11; 
	var d2 = r2 < 2 ? 0 : 11-r2;
	return cnpj.match(new RegExp('.*'+d1+''+d2+"$"));
}

//masks
Zadig.FieldMask = function(element, args)
{
  this.element = element;
  this.active = true;
  this.max = 0;
  this.numChars = new Array();
  this.maskChars = new Array();
  this.get='';
  this.replace=''; 
  this.count = 1;
  args.mask.replace(/(#+|[^#]+)/g, Zadig.bind(this.parse, this));
  Zadig.addListener('keyup', element, Zadig.bind(this.onkup, this));
  this.get = new RegExp(this.get+".*?",'g');
  
}

Zadig.FieldMask.prototype.parse = function()
{
	var e = arguments[0];
	if(!e.match('#+'))
	{
		if( this.max==0) this.numChars.push(0);
		this.maskChars.push(e);
		this.max += e.length;
		this.replace+=e;
	}
	else
	{
		this.numChars.push(e.length);
		this.max += e.length;
		this.replace+='$'+ (this.count++);
		this.get+="(\\d{"+e.length+"})";
	}
}

Zadig.FieldMask.prototype.onkup = function(e)
{
  if(!this.active) return;
  if (e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 39) return;
  var s = this.element.value;
  if(s.length >= this.max) {s = s.substring(0, this.max);
  s = s.replace(/[^\d]+/g, ""); this.element.value = s.replace(this.get, this.replace); return;
  }
	s = s.replace(/[^\d]+/g, "");
	var nc = 0;
	
	for(var i = 0; i < this.numChars.length; i++)
	{
		nc += this.numChars[i];
		if(s.length < nc) break;
		s = s.substring(0, nc) + this.maskChars[i] + s.substring(nc);
		nc+= this.maskChars[i].length;
	}
	this.element.value = s;
}
Zadig.FieldMaxLen = function(element, args)
{
  this.element = element;
  this.max = args.maxlen;
  Zadig.addListener('keyup', element, Zadig.bind(this.onkup, this));
}
Zadig.FieldMaxLen.prototype.onkup = function(e)
{
  var s = this.element.value;
  if(s.length > this.max) this.element.value = s.substring(0, this.max);
}

