//  useful form handling functions

function  set_field_by_id( id, val ) {
  o = new Element_From_Id(id);
  if  ( o.obj ) {
    o.obj.value = val;
  }
}

function  clear_field_by_id( id, val ) {
  o = new Element_From_Id(id);
  if  ( o.obj.value == val ) {
    return  set_field_by_id(id,'');
  }
  else  {
    o.obj.select();
  }
}

function  reset_field_by_id( id, val )  {
  o = new Element_From_Id(id);
  if  ( o.obj.value == '' ) {
    return  set_field_by_id(id,val);
  }
}


function check_form( frm, prefix ) {
   if ( !frm.IO_REQUIRED_FIELDS || frm.IO_REQUIRED_FIELDS.value == "" )   {
      return true;
   }
   
   prefix   =  (!prefix || trim(prefix) == "") ? "mail" : prefix;
   
   fields_to_check   =  frm.IO_REQUIRED_FIELDS.value.split(",");
   
   e  =  "";
   fs =  new Array();
   for   ( i in fields_to_check )   {
      a  =  i;
      fn =  prefix+bar_space(uc_words(trim(fields_to_check[i])));
      f  =  frm[fn];
      if  ( !f )  {
        alert('This form has not had its required fields configured correctly');
        return  false;
      }
      if ( f && f.value == "" ) {
         if ( e == "" ) {
            e  =  a;
				    f.focus();
         }
         fs[a] =  uc_words(fields_to_check[a]);
      }
      else
      if ( fn.search(/email/i) > -1 && !check_email(f.value) ) {
         if ( e == "" ) {
            e  =  a;
            f.focus();
         }
         fs[a] =  uc_words(fields_to_check[a]);
      }
   }
   
   if ( e != "" )  {
      e_msg =  "Please ensure that the following (highlighted) fields are correctly completed:\n";
      for   ( i in fs ) {
			e_fs	=	trim(fs[i].replace(/_/," "));
         e_msg += e_fs+"\n";
			e_fs	=	null;
      }
      alert(e_msg);
      return   false;
   }
   return   true;
}

function check_enquiry( frm, shoes )  {
  fields_to_check = new Array(  'name',
                                'email',
                                'main_phone_number',
										  'existing_customer',
										  'delivery_address',
										  'delivery_postcode'
										);

	if	( shoes == 'shoes')	{
		fields_to_check.push('minimum_size');
		fields_to_check.push('maximum_size');
	}
   
   e  =  "";
   fs =  new Array();
   for   ( i in fields_to_check )   {
      a  =  i;
      fn =  trim(fields_to_check[i]);
      f  =  frm[fn];
      if  ( !f )  {
        alert('This form has not had its required fields configured correctly');
        return  false;
      }
      if ( f && f.value == "" ) {
         if ( e == "" ) {
            e  =  a;
				    f.focus();
         }
         fs[a] =  uc_words(fields_to_check[a]);
      }
      else
      if ( fn.search(/email/i) > -1 && !check_email(f.value) ) {
         if ( e == "" ) {
            e  =  a;
            f.focus();
         }
         fs[a] =  uc_words(fields_to_check[a]);
      }
   }
   
   if ( e != "" )  {
      e_msg =  "Please ensure that the following (highlighted) fields are correctly completed:\n";
      for   ( i in fs ) {
			e_fs	=	trim(fs[i].replace(/_/g," "));
         e_msg += e_fs+"\n";
			e_fs	=	null;
      }
      alert(e_msg);
      return   false;
   }
   return   true;
}

function check_email( addr )  {
   pattern  =  /^[-_\.a-zA-Z0-9]+@[-_a-zA-Z0-9]+\.[-_\.a-zA-Z0-9]{2,}$/;
   addr  =  trim(addr);
   return   ( addr.search(pattern) > -1 );
}

function redirect_form( frm_id, new_action ) {
  frm = new Element_From_Id(frm_id);
  frm.obj.action  = new_action;
}

function submit_form( id ) {
   f  =  new Element_From_Id(id);
   if ( !f || !f.obj.submit )   {
      return   false;
   }
   return   f.obj.submit();
}