function setErrorMessages(msg, is_error){
    var messages = document.getElementById('bizdir_messages');
    messages.className=(is_error)?'bizdir_error_box':'bizdir_message';
    messages.innerHTML=msg; 
    var submit_message = document.getElementById('bizdir_submit_message');
    submit_message.className=(is_error)?'bizdir_error':'';
    submit_message.innerHTML=(is_error)?'Please fix the above errors and submit again.':'&nbsp;';
}
function validate_fields(flds){
  var message = '';
  for (var i=0; i<flds.length; ++i) {
    if(flds[i][2]){
      if(document.getElementById(flds[i][0]).selectedIndex==0) message+='Please select a '+flds[i][1]+'<br/>';
    }else{
      if(document.getElementById(flds[i][0]).value=='') message+=flds[i][1]+' cannot be blank.<br/>';
    }
  }
  if (message!=''){
    setErrorMessages(message,true); 
    return false;
  }
  return true;
}
function validate_contact() {
  var message = '';
  var org_website = document.getElementById('bizdir_website').value;
  var org_phone = document.getElementById('bizdir_phone').value;
  var org_email = document.getElementById('bizdir_cEmail').value;
  if (org_website == '' && org_phone == '' && org_email == '') {
    setErrorMessages('Please enter at least one method of contact (i.e. a website, an email, and/or a phone number).',true);
    return false;
  }
  return true;
}
function bizformSubmit(){
  var names = document.getElementById('bizdir_name').value.split(' ');
  if(names.length>2) for(var i=2; i<names.length; ++i){names[1]+=' '+names[i];}

  //Build SACK Call to Qwik
  document.getElementById('bizdir_submit').disabled = true;
  var submit_message = document.getElementById('bizdir_submit_message');
  submit_message.className = "bizdir_message";
  submit_message.innerHTML = "Submitting Form, Please Wait...";
  bizdir_clearAllAutoFill();
  if (!validate_fields([ 
        ['bizdir_name','Your Name',false],
        ['bizdir_username','Username',false],
        ['bizdir_password','Password',false],
        ['bizdir_password2','Confirm Password',false],
        ['bizdir_category_id','Category',true],
        ['bizdir_email','Your Email',false],
        ['bizdir_cName','Organization Name',false],
        ['bizdir_description','Org. Description',false],
        ['bizdir_street1','Org. Address',false],
        ['bizdir_city','Org. City',false]
      ]) || !validate_contact()) {
    document.getElementById('bizdir_submit').disabled = false;
    bizdir_populateAutofill();
    return false;
  }

  var mysack = new sack('/qs/customer_registration.php');
  mysack.method = 'POST';
  mysack.setVar('form_type','update_form');
  mysack.setVar('store_id','90');
  mysack.setVar('request','json');
  mysack.setVar('ship_to_billto','1');
  mysack.setVar('fname',(names.length>0)?names[0]:'');
  mysack.setVar('lname',(names.length>1)?names[1]:'');
  mysack.setVar('username',document.getElementById('bizdir_username').value);
  mysack.setVar('password',document.getElementById('bizdir_password').value);
  mysack.setVar('password2',document.getElementById('bizdir_password2').value);
  mysack.setVar('organization',document.getElementById('bizdir_cName').value);
  mysack.setVar('bill_website',document.getElementById('bizdir_website').value);
  mysack.setVar('bill_email',document.getElementById('bizdir_email').value);
  mysack.setVar('bill_phone',document.getElementById('bizdir_phone').value);
  mysack.setVar('bill_street1',document.getElementById('bizdir_street1').value);
  mysack.setVar('bill_street2',document.getElementById('bizdir_street2').value);
  mysack.setVar('bill_city',document.getElementById('bizdir_city').value);
  mysack.setVar('bill_province',document.getElementById('bizdir_state').value);
  mysack.setVar('bill_postal_code',document.getElementById('bizdir_zip').value);
  mysack.setVar('bill_country',document.getElementById('bizdir_country').value);
  mysack.onError = function() { alert('An ajax error occured while adding your listing. Please reload the page and try again.') };
  mysack.onCompletion = function(){
    var json=eval('(' + this.response + ')');
    document.getElementById('bizdir_submit').disabled = false;
    bizdir_populateAutofill();
    var messages = document.getElementById('bizdir_messages');
    var submit_message = document.getElementById('bizdir_submit_message');
    if(json.success){
      setErrorMessages('Your listing has been submitted and is pending approval from an administrator.', false);
      bizdir_add_listing();
    }else{
      setErrorMessages(json.status_message, true);
    }
  };
  mysack.runAJAX();

  return false;
}
function addBizDirField(fld, label, append_after){
  var ntdl = document.createElement('td');
  ntdl.setAttribute('class','bizdir_form_text');
  ntdl.appendChild(document.createTextNode(label));

  var ntdi = document.createElement('td');
  ntdi.setAttribute('class','bizdir_form_input');
  var inp = document.createElement('input');
  inp.setAttribute('id',fld);
  inp.setAttribute('name',fld);
  inp.setAttribute('value','For us to contact you...');
  inp.setAttribute('class','bizdir_input_style');
  inp.setAttribute('bizdir_autofill','YourInfo');
  inp.setAttribute('maxlength','YourInfo');
  inp.setAttribute('onfocus','bizdir_clearAutoFill("'+fld+'","YourInfo");');
  inp.setAttribute('onclick','bizdir_clearAutoFill("'+fld+'","YourInfo");');
  inp.setAttribute('style','color: rgb(153,153,153);');
  ntdi.appendChild(inp);

  var ntr = document.createElement('tr');
  ntr.appendChild(ntdl);
  ntr.appendChild(ntdi);
  var last_field_tr = document.getElementById(append_after).parentNode.parentNode;
  last_field_tr.parentNode.insertBefore(ntr, last_field_tr.nextSibling);
}

