function showError(div_name, msg)
{	  
	  $(div_name).style.display = 'block';
      $(div_name).innerHTML 	= msg;	
}

function showConfirm(div_name, msg)
{
	  //$(div_name).className = '';
      $(div_name).innerHTML = msg;	
}


function validateName(name, error_container)
{
   re_name = new RegExp("^([" + mi_unicode_alnum + " ])+$");
   
   var ret = true;
   
   var match_nombre = re_name.exec(name);
   if (name.length < 2)
   {
       ret = false;
       showError(error_container, _('The name, must have al least two characters.'));
   }
   else if (match_nombre != null)
   {
       showConfirm(error_container, _(''));      
   }
   else
   {
       ret = false;
       showError(error_container, _('The name only can contain letters.'));
   }
   return ret;
}

function validatePhone(phone, error_container)
{
   re_phone = new RegExp("^\\([0-9][0-9][0-9]\\)[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]+$");
   
   var ret = true;
   
   var match_phone = re_phone.exec(phone);
   if (match_phone != null)
   {
       showConfirm(error_container, _(''));
   }
   else
   {
       ret = false;
       showError(error_container, _('Invalid phone number, the phone must be entered whith the form (###)###-####'));
   }
   return ret;
}

function validateCity(city, error_container)
{
   var ret = true;
   if (city != -1)
   {
       showConfirm(error_container, _(''));
   }
   else
   {
       ret = false;
       showError(error_container, _('You must chose a city.'));
   }
   return ret;
}

function validateZip(region, city, zip, error_container)
{
   var ret = true;
   
   if (region == -1 && city == -1)
   {
   	   showConfirm(error_container, _(''));
   }
   else 
   {
	  //showError('msg_email', '');
      new Ajax.Request('/ajax_verify_zip_code.php', {method:'post',
                                                    parameters:'region=' + region + "&city=" + city + "&zip=" + zip,
                                                    onSuccess:function()
													{
													   // $('err_msg').innerHTML = t.responseText;   
													   re = new RegExp('ER:(.*)');
													   var match_err = re.exec(t.responseText);
													   if (match_err != null)
													   {
														   var error_msg = match_err[1];
														   if (error_msg == "Don't belong to the city.")		   
																showError(error_container, _("The zip code, doesn't belong to the city."));
														   else if (error_msg == "Don't belong to the region.")
																showError(error_container, _("The zip code, doesn't belong to that region."));														   
														   else
																showError(error_container, _('The entered zip code is incorrect, please verify.'));
													   }
													   else
													   {
														   re_ok = new RegExp('OK:(.*)');
														   var match_ok = re_ok.exec(t.responseText);
														   if (match_ok != null)
														   {
																showConfirm(error_container, _(''));
														   }
														   else
															   showError(error_container, _('The entered zip code is incorrect, please verify.'));
													   }
													}
                                                   }
                      );
   }
   return ret;
	
}

function validateAddress(address, error_container)
{
   var ret = true;
   if (address != "")
   {
       showConfirm(error_container, _(''));
   }
   else
   {
       ret = false;
       showError(error_container, _('Must enter a valid address.'));
   }
   return ret;
}


function validateEmail(email, error_container)
{
   var ret = true;
   re_email = new RegExp("([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+");
   
   var match_email = re_email.exec(email);

   if (email.length < 2)
   {
	   ret = false;
   	   showError(error_container, _('You must enter a valid email address.'));
   }
   else if (match_email != null)
   {
	  //showError('msg_email', '');
      new Ajax.Request('/ajax_email_available.php', {method:'get',
                                                    parameters:'email=' + email,
                                                    onSuccess:function()
													{
													   // $('err_msg').innerHTML = t.responseText;   
													   re = new RegExp('ER:(.*)');
													   var match_err = re.exec(t.responseText);
													   if (match_err != null)
													   {
														   var error_msg = match_err[1];
														   if (error_msg == 'Email already taken')		   
																showError(error_container, _('The email has been already taken. <a href="/olvido_clave">Forgot your password?</a></p>'));
														   else
																showError(error_container, _('The entered mail address is incorrect, please verify.'));
													   }
													   else
													   {
														   re_ok = new RegExp('OK:(.*)');
														   var match_ok = re_ok.exec(t.responseText);
														   if (match_ok != null)
														   {
																showConfirm(error_container, _(''));
														   }
														   else
															   showError(error_container, _('The entered mail address is incorrect, please verify.'));
													   }														
													}
                                                   }
                      );
   }
   else
   {
   	  showError(error_container, _('The entered mail address is incorrect, please verify.'));
	  ret = false;
   }
   return ret;
	
}


function validatePassword(password, error_container)
{
   var ret = true;
   password = password.trim();
   if ( password.length >= 4 )
   {
      showConfirm(error_container, _(''));
   }
   else
   {
	   ret = false;
      showError(error_container, _('Password must be at least 4 characters long.'));
   }
   return ret;
}

function validatePasswordConf(password, password_conf, error_container)
{
   var ret = true;
   password = password.trim();
   if ( password.length >= 1 )
   {
	   if (password != password_conf)
	   {
	   		ret = false;	  
			showError(error_container, _("Password and password confirmation doesn't match"));
	   }
	   else
	      showConfirm(error_container, _(''));
   }
   else
   {
	   showConfirm(error_container, _(''));
   }
   return ret;
}

function max_categories()
{
	var i;
	var count = 0;
	for ( i = 0; i < document.forms[0].elements.length; i++ )
	{
		if (document.forms[0].elements[i].type == 'checkbox' &&  document.forms[0].elements[i].id.substr(0,4) == "cat_" )
		{	
			if(document.forms[0].elements[i].checked)
				count++;
		}
	}
	return count <= 3; 
}
