/*	copyright Runebrand */
/*	copyright Runebrand */
/*	v20051004			*/
/*	Refactoring Total	*/

/*	27/05/05	- Añadido GetElementByID()	- BOFH Mate							*/
/*	04/10/05	- Añadidas _minLength, _maxLength, _numMin, _numMax	- BOFH Mate	*/

/*	Modo de empleo:
		myForm= new dynForm(document.formulario,"post","register.php");
		(formulario, método de envío (post/get), página destino)
		
	Funciones disponibles:
		myForm.addcheck_blank("campo","mensaje de error");
		myForm.addcheck_alfa("campo","mensaje de error");
		myForm.addcheck_minLength("campo","mensaje de error","longitud_del_campo");
		myForm.addcheck_maxLength("campo","mensaje de error","longitud_del_campo");
		myForm.addcheck_number("campo","mensaje de error");
		myForm.addcheck_numMin("campo","mensaje de error","longitud_del_campo");
		myForm.addcheck_numMax("campo","mensaje de error","longitud_del_campo");
		myForm.addcheck_length("campo","mensaje de error","longitud_del_campo");
		myForm.addcheck_email("campo_email","mensaje de error");
		myForm.addcheck_select("campo_select","mensaje de error","0");
		(campo, mensaje de error, valor por defecto NO válido);
		myForm.addcheck_radio("campo_radiobutton","mensaje de error");
		
		myForm.submit();
		
	Futuras Funciones Disponibles:
		set_maxlength: Indicar el maxlength del campo del formulario
*/

function dynForm(form,method,action) 
{
	//propiedades
	 this.form=form;
	 this.method=method;
	 this.action=action;
	 this.target="_self";
	 this.count=0;
	 this.inputs=new Array();
	 this.types=new Array();
	 this.messages=new Array();
	 this.parameters=new Array();
	 //metodos
	 this.addcheck=Addcheck;
	 this.addcheck_alpha=Addcheck_alpha;
	 this.addcheck_number=Addcheck_number;
	 this.addcheck_blank=Addcheck_blank;
	 this.addcheck_email=Addcheck_email;
	 this.addcheck_radio=Addcheck_radio;
	 this.addcheck_select=Addcheck_select;
	 this.addcheck_checkbox=Addcheck_checkbox;
	 this.addcheck_length=Addcheck_length;
	 this.addcheck_minLength=Addcheck_minLength;
	 this.addcheck_maxLength=Addcheck_maxLength;
	 this.addcheck_numMin=Addcheck_numMin;
	 this.addcheck_numMax=Addcheck_numMax;
	 this.set_target=Set_target;
	 this.evaluate=Evaluate;
	 this.value=Value;
	 this.set=Set;
	 this.validate=Validate;
	 this.submit=Submit;
	 return this;
}

function Set_target(target)
{
	this.target=target;
}

function Addcheck(input,type,message) //input, type, mesagges, parameters
{
	this.count++;
	this.inputs[this.count]=arguments[0];
	this.types[this.count]=arguments[1];
	this.messages[this.count]=arguments[2];
	this.parameters[this.count]=new Array();
	j=0;
	for(var i=3; i<arguments.length; i++){
      	  this.parameters[this.count][j]=arguments[i];
	  j++;
	}
}


function Addcheck_blank(input,message)
{
	this.addcheck(input,"chkBlank",message);
}

function Addcheck_alpha(input,message)
{
	this.addcheck(input,"chkAlpha",message);
}

function Addcheck_number(input,message)
{
	this.addcheck(input,"chkNumber",message);
}

function Addcheck_email(input,message)
{
	this.addcheck(input,"chkEmail",message);
}

function Addcheck_radio(input,message)
{
	this.addcheck(input,"chkRadio",message);
}

function Addcheck_select(input,message,notvalue)
{
	this.addcheck(input,"chkSelect",message,notvalue);
} 

function Addcheck_minLength(input,message,size)
{
	this.addcheck(input,"minLength",message,size);
} 

function Addcheck_maxLength(input,message,size)
{
	this.addcheck(input,"maxLength",message,size);
}

function Addcheck_numMin(input,message,value)
{
	this.addcheck(input,"munMin",message,value);
}

function Addcheck_numMax(input,message,value)
{
	this.addcheck(input,"numMax",message,value);
} 

function Addcheck_length(input,message,size)
{
	this.addcheck(input,"chkLength",message,size);
} 

function Addcheck_checkbox(input,message)
{
	this.addcheck(input,"chkCheckbox",message);
} 

function Evaluate(which)
{
	func=this.types[which];
	//value=document.getElementById(this.inputs[i]).value;
	//eval ("value=document."+this.form.name+"."+this.inputs[i]+".value");

	listValues = document.getElementsByName(this.inputs[i]);
	if (listValues)
	 {
	   if (listValues.length>0)
	     value = listValues[0].value;
	 }
	else{
	  eval ("value=document."+this.form.name+"."+this.inputs[i]+".value");
	}
	if (func=="chkRadio"){
		value=eval("this.form."+this.inputs[i]);
		result=eval(func + "(value)");
	}
	else if (func=="chkCheckbox"){
		value=eval("this.form."+this.inputs[i]);
		result=eval(func + "(value)");
	}
	else if (func=="chkSelect"){
//		value=eval("this.form."+this.inputs[i]+".value");
		value2=this.parameters[i][0]
		result=eval(func + "(value,value2)");
	}
	else if (func=="chkLength"){
//		value=eval("this.form."+this.inputs[i]+".value");
		size=this.parameters[i][0]
		result=eval(func + "(value,size)");
	}
	else if (func=="minLength") {
		size=this.parameters[i][0]
		result=eval(func + "(value,size)");
	}
	else if (func=="maxLength") {
		size=this.parameters[i][0]
		result=eval(func + "(value,size)");
	}
	else if (func=="numMin") {
		value2=this.parameters[i][0]
		result=eval(func + "(value,value2)");
	}
	else if (func=="numMax") {
		value2=this.parameters[i][0]
		result=eval(func + "(value,value2)");
	}
	else {
//		value=eval("this.form."+this.inputs[i]+".value");
		result=eval(func + "(value)");
	}
	return result;
}

function Validate()
{
	error=false;
	tester=false;
	i=1;
	while (i<this.count+1 && !error)
	{	
		tester=this.evaluate(i);
		if (tester)
		{
			error=false;
		}
		else
		{
			alert(this.messages[i]);
			if (this.types[i]!="chkRadio") document.getElementsByName(this.inputs[i])[0].focus();
			error=true;
		}	
		i++;
	} 
    return !error
}

function Submit()
{     
	 if (this.validate())
	 {
	 	this.form.method=this.method;
		this.form.action=this.action;
		this.form.target=this.target;
		
		this.form.submit();
	 }
}
function chkEmail(email)
{
	var patron  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!chkBlank(email)) return true;
	if (patron.test(email)) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}

function chkBlank(str)
{
	str=str.replace(/^\s*|\s*$/g, "");
	if (str.length>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkAlpha(str)
{
	var patron  = /^[a-zA-ZñÑáÁéÉíÍóÓúÚ \.\-]+$/;
	if (patron.test(str)) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}

function chkNumber(str)
{
	var patron  = /^[0-9]+$/;
	if (patron.test(str)) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}

function chkRadio(object)
{
 for (var i=0; i<object.length;i++)
 {
  if (object[i].checked == true)
  {
	return true;
  }
 }
  return false;
}

function chkCheckbox(object)
{
 return object.checked;
}

function chkSelect(value1,value2)
{
	
	if (value1!=value2) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}

function chkLength(value1,size)
{
	if (value1.length==size) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}
function minLength(value1,size)
{
	if (value1.length>=size) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}
function maxLength(value1,size)
{
	if (value1.length<=size) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}
function numMin(value1,value2)
{
	if (value1>=value2) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}
function numMax(value1,value2)
{
	if (value1<=value2) 
	{
		return true;
	}
	else
	{ 
		return false;
	}
}
//refactoring. Hay que hacerlo mas funcional, que compruebe si el nombre que le pasamos
// existe en el array inputs. En caso contrario devuelva un error
function Value(name)
{
 val=eval("this.form."+name+".value");
 return val;
}

function Set(name,value)
{
	eval("this.form."+name+".value='"+value+"'");
}