
// return false if first 4 characters of card code is valid and carNumber length is 13, otherwise return false
function check_cardCode(toCheckCodeValue) {
  
  // retrieve first 4 charachters of passed cardNuber value
  first4Char = toCheckCodeValue.substring(0,4);
  
  // fill a global arry with first 4 valid characters for car number code
  validCodeCardArray = new Array("0402", "0403", "0405", "0401", "0406", "0408", "0409", "0414", "0415", "0417", "0418", "0419", "0404", "0411", "0430", "0407", "0420");
  
  if (toCheckCodeValue != '' && (!inArray(validCodeCardArray, first4Char) || toCheckCodeValue.length != 13))
    return true;

  return false;
} // end of check_cardCode

// check validity of email address
function check_email(email) {

var errors="";

// Posizione della chiocciola.
var chiocPos=email.indexOf("@")

// Insieme dei caratteri non ammessi in un e-mail.
var nochar="\\/^,;:+àèìòù'<>()%=?!| " + '"'

// Prima lettera dell'e-mail.
var first_letter=email.substring(0,1)

// Ultima lettera dell'e-mail.
var last_letter=email.substring(email.length-1,email.length)

// Penultima lettera dell'e-mail.
var Penultima_letter=email.substring(email.length-2,email.length-1)

// Lettera a sinistra della chiocciola.
var sx_chioc=email.substring(chiocPos-1,chiocPos)

// Lettera a destra della chiocciola.
var dx_chioc=email.substring(chiocPos+1,chiocPos+2)

if ((chiocPos<"1") || (chiocPos==(email.length-1)) ||
(chiocPos!=(email.lastIndexOf("@")))) {
errors+="\n  - carattere @ mancante o in posizione errata";
}
else {
  for (var i=0; i<=nochar.length-1; i++) {
    if (email.indexOf(nochar.substring(i,i+1))!="-1") {
     errors+="\n  - contiene caratteri non validi";
     break
    }
  }
}

if (errors=="") {
  if ((first_letter==".") || (sx_chioc==".") || (dx_chioc==".") ||
(last_letter==".") || (Penultima_letter==".") ) {
     errors+="\n  - carattere punto (.) in posizione errata";
  }  
  else {

    for (var i=0; i<=email.length-1; i++) {
      if ((email.substring(i,i+1)==".") && (email.substring(i+1,i+2)==".")) {
        errors+="\n  - ci sono due caratteri punto (.) consecutivi";
        break
      }
      if ((email.substring(i,i+1)=="-") && (email.substring(i+1,i+2)=="-")) {
        errors+="\n  - ci sono due caratteri trattino (-) consecutivi";
        break
      }
    }
  }
}
PuntoDopoChioc = 0
if (errors=="") {
  for (var i=chiocPos+1; i<=email.length-3; i++) {
    if (email.substring(i,i+1)==".") {
      PuntoDopoChioc = 1
      break
    }
  }
  if (PuntoDopoChioc == 0) {
    errors+="\n  - non è stato indicato il dominio (.it .com .net etc.)";
  }
}
return errors
} // end of check_email


// create popup window
function createPopUpWindow(popUpAddress, popUpName, height, width, scrollbars, left, top) {
  // if has been specified scrollbars value I set it to true
  if (scrollbars != null)
    scrollbars = '1';
  // if left and top has been not specified I show popup in center of user screen
  if (left == null)
    left = Math.floor((screen.width-width)/2);
  if (top == null)
    top = Math.floor((screen.height-height)/2);
  popUpWindow = window.open(popUpAddress, popUpName, 'height=' + height + ',width=' + width + ',directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,left=' +left + ',top=' + top);
  popUpWindow.focus();
} // end of createPopUpWindow



// check presence of username and password in login form
function checkLoginFormData(idForm) {
  
  // retrieve form object
  form = document.getElementById(idForm);

  if (form.login.value == '' || form.password.value == '') {
    window.alert('ATTENZIONE!\nInserire nome utente e password.');
    return false;
  }
  else
    return true;
} // end of checkLoginFormdata

// return true if passed velue is present in passed array
function inArray(myArray, value) {
  myArrayLength = myArray.length;
  for (index = 0; index < myArrayLength; index++) {
    if (myArray[index] == value)
      return true;
  }
  return false;
} // end of inArray


// rollover
function showImg(a, b) {

		   if (document.images) {
			    document.images[a].src = b;
	   			return true;
	  		}
		}


// Redirect opener window on promotion details page and blur current pop up page. Used in Vicini di spesa pop up flash.
function showPromotion(idPromotion) {
  window.opener.location = '../index.php?entity=superMarket&section=04&idPromotion=' + idPromotion;
  window.blur();
} //  end of showPromotion


// check presence of region, idProvince and city in search form 
function checkSearchFormData(idForm) {
  // retrieve form object
  form = document.getElementById(idForm);
  
  if (idForm == 'searchForm1') {
    if (form.region.value == '' && form.idProvince.value == '' && form.city.value == '') {
      window.alert('ATTENZIONE!\nIndicare una regione o una provincia o una località.');
      return false;
    }
  }
  else if (idForm == 'searchForm2') {
    if (form.city.value == '') {
      window.alert('ATTENZIONE!\nIndicare una località.');
      return false;
    }
    else if (form.city.value.length < 3) {
      window.alert('ATTENZIONE!\nLa località deve essere lunga almeno tre caratteri.');
      return false;
    }
  }

  return true;
} // end of checkSearchFormData


// controlla che sia stato almeno indicato un criterio di ricerca nel form per la ricerca delle ricette
function checkRicetteSearchForm() {
  form = document.getElementById('ricetteSearchForm');
  if (form.type.value == '' && form.level.value == '' && form.ingrediente.value == '') {
    window.alert('ATTENZIONE!\nImpostare almeno un criterio per la ricerca.');
    return false;
  }
  return true;
} // end of checkRicetteSearchForm


// controlla che sia stato almeno indicato un criterio di ricerca nel form per la ricerca dei vini
function checkViniSearchForm() {
  form = document.getElementById('viniSearchForm');
  if (form.region.value == '' && form.color.value == '' && form.nome.value == '') {
    window.alert('ATTENZIONE!\nImpostare almeno un criterio per la ricerca.');
    return false;
  }
  return true;
} // end of checkViniSearchForm


// setta il margin top dell'immagine numVerde modificando a runtime la proprietà margin del primo selettore (numVerde) del foglio di stile main.css
function setNumVerdeMarginTop() {
  // detect browser
  (navigator.appName == "Microsoft Internet Explorer") ? isie = true : isie = false;
  // recupero un riferimento all'oggetto style in base al browser
  (isie) ? myStyle = document.styleSheets[0].rules : myStyle = document.styleSheets[0].cssRules;
  cTD = document.getElementById('contentTD');
  // se l'utente è loggato (quindi non esiste un elemento di id = loginForm) sottraggo un valore all'offsetHeight altrimenti ne sottraggo un altro
  (document.getElementById('loginForm')) ? offHeight = cTD.offsetHeight - 597 : offHeight = cTD.offsetHeight - 515;
  myStyle[0].style.margin = offHeight + "px 0px 0px 0px";
} // end of setNumVerdeMarginTop