/***********************************************
Javascript functions for pay on line
***********************************************/

/*Code for showing instructions for CVC Code in poy on line*/
function overlay()
{
	var table = document.getElementById('cvc_code');
	
	if(table.style.visibility=="hidden")
	{
		table.style.visibility = "visible";
		table.style.display = "inline";
	}
	else
	{
		table.style.visibility = "hidden";
		table.style.display = "none";
	}
}

//Funcion TRIM utilizada para validar campos del formulario
	function trim(s)
	{
		while (s.substring(0,1) == ' ')
		{
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ')
		{
			s = s.substring(0,s.length-1);
		}
		return s;
	}


/**
*	Funcion que valida que un email esté correcto, es decir, que posea @ y .
*	@param	string	correo	email que se quiere validar
*/
function validar_email(correo)
{
	if(correo.indexOf('@')!=-1 && correo.indexOf('.')!=-1)
		return true;
	else
		return false;
}


/*Function that tells if a string contains only numbers*/
function IsNumeric(sText, otherchars)
{
   var ValidChars = "0123456789"+otherchars;
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
    }
   return IsNumber;
   
}


/*Function to validate the data in the form of pay on line*/
function validate_form(form)
{
	//form data
	var email = trim(form.x_email.value);
	var invoice = trim(form.x_invoice_num.value);
	var card_num = trim(form.x_card_num.value);
	var exp_date = trim(form.x_exp_date.value);
	var card_code = trim(form.x_card_code.value);
	var ammount = trim(form.x_amount.value);
	
	//Value to return. If false, there is an error in the form
	var res = true;
	
	//Errors will keep a registry of the errors
	var errors = "The following errors have been found:\n";
	
	if(email==null || email =="")
	{
		errors+="\tThe email is required.\n";
		res=false;
	}
	else if(!validar_email(email))
	{
		errors+="\tThe email is not valid.\n";
		res=false;
	}
	
	if(invoice==null || invoice =="")
	{
		errors+="\tAn invoice number is required.\n";
		res=false;
	}
	
	if(card_num==null || card_num =="")
	{
		errors+="\tA card number is required.\n";
		res=false;
	}
	else if(!IsNumeric(card_num, ""))
	{
		errors+="\tThe card number should only contain numbers.\n";
		res=false;
	}
		
	if(exp_date == null || exp_date =="")
	{
		errors+="\tThe expiration date is required.\n";
		res=false;
	}
	else if(!IsNumeric(exp_date, "/"))
	{
		errors+="\tThe expiration date should only contain numbers or '/'.\n";
		res=false;
	}
	else if(exp_date.length<3)
	{
		errors+="\tThe expiration date is not valid.\n";
		res=false;
	}
	
	if(card_code==null || card_code =="")
	{
		errors+="\tThe card_code is required.\n";
		res=false;
	}
	else if(!IsNumeric(card_code, ""))
	{
		errors+="\tThe card code should only contain numbers.\n";
		res=false;
	}
	
	if(ammount==null || ammount =="")
	{
		errors+="\tThe amount is required.\n";
		res=false;
	}
	else if(!IsNumeric(ammount, "."))
	{
		errors+="\tThe amount should only contain numbers or '.'.\n";
		res=false;
	}
	
	if(!res)
		alert(errors);
	
	return res;
}