﻿
function formCheck(imeForma)
	{
		if (trazilica.query.value == "traženi pojam")
		{
		alert("Upišite pojam za pretraživanje.\n\nProbajte ponovo!");
		return (false);
		}
	}

function ScrollText()
{
	message = document.title;
    status = message;
    setTimeout("ScrollText()",100);
}
ScrollText();

function doNothing()
{
//var x = document.title;
}

//**** FORM VALIDATION**************************
function validateFormOnSubmit(theForm) {
var reason = "";
alert("test");
/*  reason += validateUsername(theForm.username);
  reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
*/
  		reason += validateEmpty(theForm.ImePrezime);
        reason += validateEmail(theForm.Email);
  		reason += validatePhone(theForm.KontaktTelefon);  
  		 reason += validatePhone(theForm.Poruka);        
        
  if (reason != "") {
    alert("Greška pri unosu podataka!\nNiste ispunili sljedeća polja:\n\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#FFFF8B'; 
        error = "Obavezno polje nije upisano.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}


function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFFF8B'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#FFFF8B'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFFF8B'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#FFFF8B';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#FFFF8B';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#FFFF8B';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#FFFF8B';
    } 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 = '#FFFF8B';
        error = "Niste upisali email adresu.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFFF8B';
        error = "Upišite pravilno email adresu.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFF8B';
        error = "Email adresa sadrži nedozvoljene znakove.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "Niste upisali broj telefona.\n";
        fld.style.background = '#FFFF8B';
    } else if (isNaN(parseInt(stripped))) {
        error = "Broj telefona sadrži nedozvoljene znakove.\n";
        fld.style.background = '#FFFF8B';
    } else if (!(stripped.length == 10)) {
        error = "Telefonski broj sadrži previše brojeva.\n";
        fld.style.background = '#FFFF8B';
    } 
    return error;
}

// ***** FORM VALIDATION**************************
