var reg    = /\w/;
var regInt = /^\d+$/;

function submit_coupon_category_value()
{
   var docForm      = document['coupon_category_value'];
   var docFormField = docForm['category_coupon__category_value_id'];
   if ( docFormField )
      {
      if ( docFormField.options[docFormField.selectedIndex].value != '' ) { document.coupon_category_value.submit(); }
      }
}

function submit_link_category_value()
{
   var docForm      = document['link_category_value'];
   var docFormField = docForm['category_link__category_value_id'];
   if ( docFormField )
      {
      if ( docFormField.options[docFormField.selectedIndex].value != '' ) { document.link_category_value.submit(); }
      }
}

function validateCoupon()
{
   if ( isEmpty('create_coupon','coupon','Coupon Description') ) { return false; }
   if ( isEmpty('create_coupon','company','Company')   ) { return false; }
   if ( isEmpty('create_coupon','expire','Expire Date')  ) { return false; }
   if ( isEmpty('create_coupon','conditions','Conditions') ) { return false; }
   if ( isEmpty('create_coupon','contact','Contact')   ) { return false; }
   if ( isEmpty('create_coupon','address_1','Address')  ) { return false; }
   if ( isEmpty('create_coupon','city','City')  ) { return false; }
   if ( isEmpty('create_coupon','state','State') ) { return false; }
   if ( isEmpty('create_coupon','country','Country')   ) { return false; }
   if ( isEmpty('create_coupon','url','URL')  ) { return false; }
   if ( isEmpty('create_coupon','email','Email')  ) { return false; }
   var docForm      = document['create_coupon'];
   var docFormField = docForm['email'];
   if ( !emailCheck(docFormField.value) )
      {
      docFormField.focus();
      docFormField.select();
      return false;
      }
   if ( isEmpty('create_coupon','phone','Phone')  ) { return false; }
   return true;
}

function isEmpty(formName,field,name)
   {
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( !docFormField.value.match(reg) && docFormField.value != '?')
         {
         alert(name + ' must be entered.' );
         docFormField.focus();
         docFormField.select();
         return true;
         }
      }
   return false;
   }

function isInt(formName,field,name)
   {
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( !docFormField.value.match(regInt) )
         {
         alert(name + ' must be an integer.' );
         docFormField.focus();
         docFormField.select();
         return false;
         }
      }
   return true;
   }

function isSelected(formName,field,name)
{
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( docFormField.options[docFormField.selectedIndex].value == '' )
         {
         alert(name + ' must be selected.' );
         docFormField.focus();
         return false;
         }
      }
   return true;
}

function emailCheck (emailStr)
{
var emailPat     = /^(.+)@(.+)$/;
var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars   = "\[^\\s" + specialChars + "\]";
var quotedUser   = "(\"[^\"]*\")";
var ipDomainPat  = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom         = validChars + '+';
var word         = "(" + atom + "|" + quotedUser + ")";
var userPat      = new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat    = new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray   = emailStr.match(emailPat);
if ( matchArray == null )
   {
   alert("Email address seems incorrect (check @ and .'s)");
   return false;
   }
   
var user   = matchArray[1];
var domain = matchArray[2];

if ( user.match(userPat) == null)
   {
   alert("The username doesn't seem to be valid.");
   return false;
   }

var IPArray = domain.match(ipDomainPat);
if ( IPArray != null )
   {
   for ( var i = 1; i <= 4; i++ )
       {
       if ( IPArray[i] > 255 )
          {
          alert("Destination IP address is invalid!");
          return false;
          }
       }
   return true;
   }

var domainArray=domain.match(domainPat)
if (domainArray==null)
   {
   alert("The Email domain name doesn't seem to be valid.");
   return false;
   }

return true;
}


