/* FUNZIONI COMUNI*/

/* questa funzione serve a disabilitare il tasto destro del mouse 

function window.onload(){
	document.ondragstart = new Function("return false;");
	document.oncontextmenu = new Function("return false;");
}

function right(e) {
	if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2))
		return false;
	else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 || event.button == 3)) {
		alert("© Copyright www.tremotors.it  -  All rights reserved - Ogni riproduzione è severamente vietata ");
		return false;
	}
	return true;
}
*/
function doMouseOver(imgName) {
if (document.images) document [imgName].src = "img/" + [imgName] + "_on.png";
window.status=""; return true;
}

function doMouseOut(imgName) {
if (document.images) document [imgName].src = "img/" + [imgName] + "_off.png";
window.status=""; return true;
}

function RunFoo(myFile, iWidth, iHeight)
{
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width="'+iWidth+'" height="'+iHeight+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">\n');
	document.write('<param name="movie" value="flash/' + myFile + '.swf" />\n');
	document.write('<param name="loop" value="false" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="bgcolor" value="#ffffff" />\n');
	document.write('<embed src="flash/' + myFile + '.swf" loop="false" quality="high" width="'+iWidth+'" height="'+iHeight+'" name="' + myFile + '" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>\n');
    document.write('</object>');
}

function visualFoto(foto, idv) {
    window.open('VisualizzaFoto.asp?Foto='+foto,'popup','width=700,height=530,scrollbars=yes,resizable=yes,top=5,left=5');
}

function changeImage(name, text, index) {
	currentIndex = index;
        document.images["main"].src = name;
        document.getElementById("main").alt = text;
}


function checkFieldsInfo() {
	var input= document.getElementById('form_info');
	//var input = document.form_info;
	
	var fieldCheck   = true;
	var fieldsNeeded = "\n--------- ATTENZIONE ------------\n";
	if (input.cognome.value == "") {
		fieldsNeeded += "\nInserire Cognome";
		fieldCheck = false;
	}
	if (input.nome.value == "") {
		fieldsNeeded += "\nInserire Nome";
		fieldCheck = false;
	}
	if (!emailCheck(input.email.value)) {
		fieldsNeeded += "\nInserire un indirizzo di email valido.";
		fieldCheck = false;
	}
	if (fieldCheck == true) {
		//input.action = "SalvaModulo.asp";
		//input.submit();
		return true;
	}
	else	{
		alert(fieldsNeeded);
		return false;
	}	
}

function SegnalaAmico(idv) {
	var w = 640/3, h = 480/3, cw = w/2, ch = h/2;

	if (window.screen) {
		w = Math.floor(screen.availWidth);
		h = Math.floor(screen.availHeight);
		OffW = (w - 430) / 2;
		OffT = (h - 431) / 2;
    }
    window.open('segnala.asp?idv='+idv,'popup','width=400,height=300,scrollbars=yes,resizable=yes,top='+OffT+',left='+OffW);
}

function OpenWin(idv) {
var w = 640/3, h = 480/3, cw = w/2, ch = h/2;

if (window.screen) {
    w = Math.floor(screen.availWidth);
    h = Math.floor(screen.availHeight);
    OffW = (w - 430) / 2;
    OffT = (h - 431) / 2;
    }
    window.open('Scheda_Auto_pr.asp?f=st&idv='+idv,'popup','width=700,height=480,scrollbars=yes,resizable=yes,top='+OffT+',left='+OffW);
}

//Tell-a-friend script
//Carl Dimmer

var good;
function checkEmailAddress(field) {
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\..{2,2}))$)\b/gi);
	alert(field.value+"  "+goodEmail);
	if (goodEmail) {
		good = true;
	}
	else {
		alert('Inserire un indirizzo di email valido per favore.');
		field.focus();
		field.select();
		good = false;
	}
}


function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}


function mailThisUrl(idv) {
	var initialsubj="Scheda Auto";
	var initialmsg="\n Qui puoi inserire quello che vuoi ma non rimuovere questo indirizzo:\n\nhttp://www.autogrado.it/index.asp?f=s";
	alert(initialmsg);
	good = false
	checkEmailAddress(document.eMailer.email);
	if (good) {
		window.location = "mailto:"+document.eMailer.email.value+"?subject="+initialsubj+"&body="+initialmsg+"&idv="+idv;
	}
}

// funzione verifica modifica/inserimento nuovo veicolo
function checkFieldsAutoChg(act, item) {
	/* verifica consistenza campi del form */
	myForm = document.frmMod;
 	// list dei drop-down nel form 
   	var requiredFields = new Array("modello", "colore", "TipoVeicolo", "alimentazione");
	// TEXT TO DISPLAY THAT DESCRIBES THE MISSING FIELD(S) TO THE USER
	var fieldNames = new Array("- Selezionare Modello", "- Selezionare Colore", "- Selezionare Tipo Veicolo", "- Selezionare Alimentazione");
	var fieldCheck   = true;
	var fieldsNeeded = "\n\ATTENZIONE\n----------------------------------------------\n";
	for(var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) 
	  {
		if (document.getElementById(requiredFields[fieldNum]).value == 0) {
			fieldsNeeded += fieldNames[fieldNum] + "\n";
	  		fieldCheck = false;
   	  	} 	
   	  } //End For
	
	if ( isNaN(document.getElementById("Mese").value) | document.getElementById("Mese").value == "") {
		fieldsNeeded += "- Specificare un numero di Mese valido.\n";
		fieldCheck = false;	
	}
	else {
		if ( (document.getElementById("Mese").value < 1) | (document.getElementById("Mese").value > 12) ) {
			fieldsNeeded += "- Il Mese deve essere compreso tra 1 e 12\n";
			fieldCheck = false;
		}
	}
	today = new Date();
	Anno = today.getFullYear();
	if ( isNaN(document.getElementById("Anno").value) | document.getElementById("Anno").value == "") {
		fieldsNeeded += "- Specificare un numero di Anno valido.\n";
		fieldCheck = false;
	}
	else {
		if ( (document.getElementById("Anno").value < 1950) | (document.getElementById("Anno").value > Anno) ) {
			fieldsNeeded += "- L'Anno non puo' essere superiore a quello corrente.\n";
			fieldCheck = false;
		}
	}  
	if ( isNaN(document.getElementById("Prezzo").value) | document.getElementById("Prezzo").value == "") {
		fieldsNeeded += "- Specificare un Prezzo in Euro valido.\n";
		fieldCheck = false;	
	}	
	else {
		if ( (document.getElementById("Prezzo").value < 0) ) {
			fieldsNeeded += "- Il Prezzo non puo' essere negativo.\n";
			fieldCheck = false;
		}
	}  
		
	if (fieldCheck == true) {
		document.frmMod.action = "SalvaDatiAuto.asp?f="+act+"&idv="+item;
		document.frmMod.submit();
	}
	else	{
		alert(fieldsNeeded);
	}	
}

function mmLoadMenus() {
  window.menu_admin = new Menu("root",110,18,"MS Sans Serif, Verdana",12,"#355E8F","#355E8F","#E6E6E6","#FFCC00","left","middle",0,0,50,15,7,true,true,true,0,true,true);
  menu_admin.addMenuItem("Inserimento Veicoli","location='Scheda_auto_chg.asp?Azione='");
  menu_admin.addMenuItem("Modelli","location='GestioneModelli.asp'");
  menu_admin.addMenuItem("Tipi Veicoli","location='GestioneTipoVeicoli.asp'");
  menu_admin.addMenuItem("Colori","location='GestioneColori.asp'");
  menu_admin.addMenuItem("Produttori","location='GestioneProduttori.asp'");
  menu_admin.addMenuItem("Novita'","location='GestioneAnnunci.asp'");
  menu_admin.addMenuItem("Ultimo Aggiornamento","location='UltimoAggiornamento.asp'");
  menu_admin.addMenuItem("Esci","location='Index.asp?a=0'");
  menu_admin.hideOnMouseOut=true;
  menu_admin.bgColor='#ffffff';
  menu_admin.menuBorder=1;
  menu_admin.menuLiteBgColor='#cde3fa';
  menu_admin.menuBorderBgColor='#000033';
  menu_admin.writeMenus();
}

function checkFieldsModello(azione) {
    var fieldCheck   = true;
    var fieldsNeeded = "";
	if (document.form1.Modello.value == "") {
		fieldsNeeded += "Il campo Modello non puo' essere vuoto.\n";
		fieldCheck = false;
	}
	
	if (document.form1.produttore.options[document.form1.produttore.selectedIndex].value == "0") {
		fieldsNeeded += "Bisogna selezionare un Produttore."
		fieldCheck = false;
	}
		
    if (fieldCheck == true) { 
		document.form1.action = "Index.asp?f=gest&sa=1&f3=" + azione;		
		return true;
	}
    else {
		alert(fieldsNeeded);
		return false;
    } //End If

}
function checkFieldsTipoVeicolo(azione) {
	if (document.form1.TipoVeicolo.value != "") {
		document.form1.action = "Index.asp?f=gest&sa=2&f3=" + azione;
		return true;
		}
	else {
		alert("Il campo Tipo Veicolo non puo' essere vuoto.");
		return false;
	}	
}

function checkFieldsColore(azione) {
	if (document.form1.Colore.value != "") {
		document.form1.action = "Index.asp?f=gest&sa=3&f3=" + azione;
		return true;
		}
	else {
		alert("Il campo colore non puo' essere vuoto.");
		return false;
	}	
}
function checkFieldsProduttore(azione) {
	if (document.form1.Produttore.value != "") {
		document.form1.action = "Index.asp?f=gest&sa=4&f3=" + azione;
		return true;
		}
	else {
		alert("Il campo Produttore non puo' essere vuoto.");
		return false;
	}	
}

function update(textbox, counter, limit) {
	
	/*
	textbox is the input text name
	counter is the input text to count the chars (readonly)
	limit is the max number of chars to enter
	*/
	var old = counter.value;
	var q = eval('textbox.value.length');
	var leftchars = limit - q; 
	if (q > limit) 
	{
		alert('Numero massimo di caratteri per il campo ' + textbox.name + ' è ' + limit + '.!');
		textbox.value = textbox.value.substring(0, limit);
    } 
	else
		counter.value = leftchars
}
function PrintDate() {
 var MyDate = new Date()
 var vDay = MyDate.getDate().toString();
 vDay = (vDay<10)? '0'+vDay:vDay;
 var vMonth = MyDate.getMonth()+1;
 vMonth = (vMonth<10)? '0'+vMonth:vMonth;
 var vYear = MyDate.getFullYear();
 var Date_IT = vDay +"/"+ vMonth + "/"+ vYear;
 document.form1.UltimoAggiornamento.value = Date_IT;
}
function checkFieldsUltAgg() {
	if (document.form1.UltimoAggiornamento.value != "") {
		document.form1.action = "Index.asp?f=gest&sa=6&f3=Modifica";
		return true;
		}
	else {
		alert("Il campo Data non puo' essere vuoto.");
		return false;
	}	
}

function eliminaContatto(item){
	document.form1.action = "Index.asp?f=gest&sa=7&idc="+item+"&f3=Delete";
	document.form1.submit();
}

//statistiche
function visualizzaStats() {
	var fieldCheck   = true;
	var fieldsNeeded = "\n\ATTENZIONE\n--------\n";
	anno = document.form_stats.anno.options[document.form_stats.anno.selectedIndex].value;
	mese = document.form_stats.mese.options[document.form_stats.mese.selectedIndex].value;
	if (anno == 0) {
		alert("Selezionare almeno l'Anno");
	  	fieldCheck = false;
	}
	else {
		if (mese == 0)
			document.form_stats.action = "Index.asp?f=gest&sa=8&mode=anno&anno="+anno;	
		else
			document.form_stats.action = "Index.asp?f=gest&sa=8&mode=mese&anno="+anno+"&mese="+mese;	
		return true;
	}
}

// funzioni usate nella scheda auto per la dinamicizzazione delle foto
function MM_findObj(n, d) {
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
	n=n.substring(0,p);
	}
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
  x=MM_findObj(n,d.layers[i].document);
  return x;
}
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr;
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments;
  document.MM_sr=new Array;  for(i=0;i<(a.length-2);i+=3) 	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2]; }
}
