
function checkContactForm(form) {
  if (form.contact) {
     if (! checkFilled(form.contact, (window.AP_SIF_CONTACT_NAME || 'Име за контакт'))) {return false};
  }
  if (form.email && form.phone) {
    if ((form.email.value == '') && (form.phone.value == '')) {
        NoFilledMessage((window.AP_SIF_EMAIL_OR_PHONE || 'email или телефон'));
        return false;
    }    
  }
  return true;
}
              
function submitNewOfferForm(form) {
  //TODO! това Вид имот трябва да зависи от настройка!
  return (checkContactForm(form) && checkFieldPositive(form.activity, (window.AP_SIF_ACTION || "Действие")) && checkFieldPositive(form.types, sProduct));
}

function submitLoginForm(form)  {
    return (checkFilled(form.user, (window.AP_SIF_USERNAME || 'Потребителско име')) && checkFilled(form.password, (window.AP_SIF_PASSWORD || 'Парола')));
}

function isEqual(field1, field2, msg) {
  if (field1.value != field2.value) {
     alert(msg);
     return false;
  }
  return true;
}

function checkRegisterForm(form) {
   return (
   checkFilled(form.user, (window.AP_SIF_USERNAME || "Потребителско име")) && 
   checkFilled(form.pass, (window.AP_SIF_PASSWORD || "Парола")) && 
   isEqual(form.pass, form.repass, (window.AP_SIF_ERROR_PASSES_DOESNT_MATCH || "Паролите не съвпадат")));
}

function submitContactForm(form) { 
  return (checkRegisterForm(form) && checkContactForm(form));
}

function submitRegisterFirmForm(form) {
   return (
   checkFilled(form.bulstat, (window.AP_SIF_BULSTAT || "Булстат"))
   );
}

function submitEditForm(form) {
  //document.getElementById('btnSave').disabled = true;
  //if (document.getElementById('btnDelete')) {document.getElementById('btnDelete').disabled = true;}
  this.form = form;
  this.checkLocation = function(field, fieldName) {
	  if (field.value.indexOf(',') != -1) {
	    field.value = field.value.replace(",",".");
	  }
	  if (field.value == '' || isNaN(field.value) || field.value == 0 || field.value == -100 || (this.form.city.value == -1 && this.form.region.value == 0)) {          
	    NoValueMessage(fieldName);
	    highlightInvalidField(field)
	    return false;
	  } else {
	    return true;
	  }
  }
  
  res = 
     checkFieldPositive(form.activity, (window.AP_SIF_ACTION || 'Действие')) &&
     checkFieldPositive(form.types, sProduct) &&
     (form.ProductGroup ? checkFieldPositive(form.ProductGroup, (window.AP_SIF_PRODUCT_GROUP || 'Продуктова директория')) : true) &&
     (isLocRequired ? checkLocation(form.city, (window.AP_SIF_LOCALITY || 'Местоположение')) : true) &&   
     
     ((form.trademark && isTrademarkRequired && !form.trademark.disabled)? checkFieldPositive(form.trademark, (window.AP_SIF_TRADEMARK || 'Търговска марка')) : true) &&
     ((form.model && isModelRequired && !form.model.disabled)? checkFieldPositive(form.model, (window.AP_SIF_MODEL || 'Модел')) : true)
   ;  
   
   if (res) {
     res = askToContinueIfNotFilled(form.price, sPrice, true);
   
     if (res) {
       res = res &&
         checkNonNegativeNumber(form.price, sPrice, true) &&   
         
         checkFilledAndPositive(form.package, sPackage) &&      
         checkNonNegativeNumber(form.total_price, sTotalPrice, true)        
     }
   }
      

  if (!res) {
    document.getElementById('btnSave').disabled = false;
    if (document.getElementById('btnDelete')) {document.getElementById('btnDelete').disabled = false;}
  }
 
  return res;    
}
