//riceve in input una stringa e verifica se si tratta di una mail
function check_mail(mail) {
	var ok=true;
	var pe=mail.indexOf('.');
	var ss=mail.indexOf(' ');
	var lch=mail.length-1;
	var atr=mail.indexOf('@');
	var att=atr+1;
	var sq=mail.substring(0,atr); 
	var sw=mail.substring(atr,pe); 
	var se=mail.substring(pe,lch);

	if((atr<1) ||  (pe==lch) || (pe<1)  || (ss != -1) || (sq.length<=2) || (sw.length<=3) || (se.length<=1)) {
	     return false;
	} else return true;
}

//Verifica se i campi per l'iscrizione alla newsletter sono stati correttamente compilati (codice in Index.asp e UPage.asp)
function check_newsletter() {
	if (check_mail(document.getElementById('news').email.value) & (document.getElementById('news').name.value!='') & (document.getElementById('news').name.value.substring(0,2)!='**') & (document.getElementById('news').object.selectedIndex != 0)){
		return true;
	} else {
		alert ("Please check to have filled properly the fields to activate our newsletter.");
		return false;
	}
}

//Verifica se i campi obbligatori del "Contact Us" sono stati correttamente compilati (codice in Contact.asp)
function check_contact() {
	if (check_mail(document.getElementById('f1').email.value) & (document.getElementById('f1').name.value != '')){
		return true;
	} else {
		alert ("Please fill the yellow fields correctly");
		return false;
	}
}

//Verifica se i campi obbligatori del "Tell a Friend" sono stati correttamente compilati (Codice in Recommend.asp)
function check_recommend() {
	if (check_mail(document.getElementById('f1').email.value) & check_mail(document.getElementById('f1').Remail.value) & (document.getElementById('f1').name.value != '') & (document.getElementById('f1').Rname.value != '')){
		return true;		
	} else {
		alert ("Please fill the yellow fields correctly");
		return false;
	}
}

// Inserisce i nomi del mittente e del destinatario nell'email da inviare all'amico (codice in recommend.asp)
function modtext() {
    var mes = ",\n\ntake a look at www.CamilloMonoCultivar.com.\n\nThey produce in Puglia Single Estate MonoCultivar EV Olive Oil and Pasta with the old durum wheat 'Senatore Cappelli' and they deliver directly in the UK!\n\n";
	document.getElementById('f1').message.value = "Dear " + document.getElementById('f1').Rname.value + mes + document.getElementById('f1').name.value;
}

//Serve per validare la pagina col validatore W3C: permette di associare ad un link con class=blank/par
//le funzioni target=_blank/_parent ormai deprecate
function intercetta() { 
    for (var i=0; i<document.links.length; i++) {
        if (document.links[i].className=="blank") {
            document.links[i].target="_blank";
        }
		else if (document.links[i].className=="par") {
            document.links[i].target="_par";
        } 
	}
window.onload = intercetta;
}

//Riceve in input un oggetto (modulo), ne legge il valore e lo pone='' se inizia per ** (codice contact.asp, Index, Upage)
function cancella(elemento) {
	var valore=elemento.value;
	if (valore.substring(0,2)== '**') {
		elemento.value='';
		elemento.style.color='#000000';
	}		
}

//Gestione dei messaggi d'avviso nella textArea nel Contact US
function ctesto(indice) {

	switch (indice) { 

 			case 0: 
 				document.getElementById("Texarea").value= "** Next olive crop will be harvested in November 2007. Our production is limited but you can make a pre-seasonal booking here in order to receive the freshest possible EVOO supply all year round. We will dispatch your fresh EVOO just before Christmas 2007. **";
// 				document.getElementById("Texarea").style.color='gray';
			break;

			case 1: 
				document.getElementById("Texarea").value= "** For us to advise the closest stockists to you, please remember to put your postcode. **";
			break; 

			case 2: 
				document.getElementById("Texarea").value= "** If you are a Retailer and wish to stock Olio Nuovo Camillo, please contact us with this form or call us. **"; 
//				document.getElementById("Texarea").style.color='gray';
			break; 
			
			case 3: 
				document.getElementById("Texarea").value= "** If you are a Restaurateur and wish to stock Olio Nuovo Camillo, please contact us with this form or call us. **"; 
//				document.getElementById("Texarea").style.color='gray';
			break; 

			case 4: 
				document.getElementById("Texarea").value= "** If you want to send a personalised Gift to a friend, please contact us with this form or call us. **"; 
//				document.getElementById("Texarea").style.color='gray';
			break; 
				
			case 5: 
				document.getElementById("Texarea").value= "** If you are looking for personalised Corporate Gifts, please contact us with this form or call us. **"; 
//				document.getElementById("Texarea").style.color='gray';
			break;
			 
			case 6: 
				document.getElementById("Texarea").value= "** Please specify here the nature of your enquiry or call us. **"; 
//				document.getElementById("Texarea").style.color='gray';
			break;
			 		
			default: 
				document.getElementById("Texarea").value= "** default **"; 
	}
}

//Apre una nuova finestra con i parametri presi in ingresso - avrei potuto farne a meno! Bah!
function pop(theURL,winName,features) { 
	window.open(theURL,winName,features);
}
