function validateDealerAddForm(theForm) {
  var reason ="";
  reason += validateEmpty(theForm.name);
  reason += validateEmpty(theForm.surname);
  reason += validateEmpty(theForm.password);
  reason += validateEmail(theForm.email);
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}
function validateProfileForm(theForm){
  var reason="";
  reason += validatePhone(theForm.phone);
  reason += validatePhoto(theForm.photo);
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}
function validateOfferForm(theForm){
  var reason="";
  reason += validateEmpty(theForm.locality);
  reason += validateNumber(theForm.size);
  reason += validateNumber(theForm.price);
  reason += validatePositive(theForm.info_id);
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}

function validateTopForm(theForm){
  var reason="";
  reason += validateEmpty(theForm.locality);
  reason += validateNumber(theForm.size);
  reason += validateNumber(theForm.price);
  reason += validateEmpty(theForm.description);
  reason += validatePositive(theForm.info_id);
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}


function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
  reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}

function validatePositive(fld){
  var error = "";
  if(fld.value == "") {
    error = "Zadejte číslo\n";
    fld.style.background = 'Yellow';
  } else if (isNaN(parseInt(fld.value))) {
    error = "Číslo obsahuje neplatné znaky\n";
    fld.style.background = 'Yellow';
  }else if (parseInt(fld.value) <= 0){
    error = "Zadejte kladné číslo.\n";
    fld.style.background = 'Yellow';
  } else{
    fld.style.background = 'White';
  }
  return error;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Požadované pole nebylo vyplněno.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Vyplňte emailovou adresu.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Zadejte validní emailovou adresu.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Emailová adresa obsahuje neplatné znaky.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateNumber(fld) {
  var error = "";
  if(fld.value == "") {
    error = "Zadejte číslo\n";
    fld.style.background = 'Yellow';
  } else if (isNaN(parseInt(fld.value))) {
    error = "Číslo obsahuje neplatné znaky\n";
    fld.style.background = 'Yellow';
  }else{
    fld.style.background = 'White';
  }
  return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "";
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "Telefonní číslo obsahuje neplatné znaky.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePhoto(fld) {
    var error = "";        

   if (fld.value == "") {
        error = "";
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "Nepodporovan formt fotografie.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

extArray = new Array(".jpeg", ".jpg");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) 
{	
	form.submit();
	return;
}

while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert("Nepodporovan formt fotografie! Podporovan typy:  " 
+ (extArray.join("  ")));
}

function LimitAttachArr(form, files) {

for (var i = 0; i < files.length; ++i) {
	allowSubmit = false;
	if (!files[i]) {
		allowSubmit = true;
		continue;
	}
	while (files[i].indexOf("\\") != -1)
		files[i] = files[i].slice(files[i].indexOf("\\") + 1);
	ext = files[i].slice(files[i].indexOf(".")).toLowerCase();
	for (var j = 0; j < extArray.length; j++) {
		if (extArray[j] == ext) { allowSubmit = true; break; }
	}
	if (!allowSubmit) {
		break;
	}
}
if (allowSubmit) form.submit();
else
alert("Nepodporovan formt fotografie! Podporovan typy:  " 
+ (extArray.join("  ")));}

