function loggati(){
	var u = document.getElementById('username').value;
	var p = document.getElementById('password').value;
	
	if(u=='' || p=='') {
		alert("E' necessario inserire sia USERNAME che PASSWORD !");
	} else {
		startRequest('./include/login.php?username='+u+'&password='+p,'');	
		ricarica();
	}
}

function loggati2(){
	var u = document.getElementById('username').value;
	var p = document.getElementById('password').value;
	
	if(u=='' || p=='') {
		alert("E' necessario inserire sia USERNAME che PASSWORD !");
	} else {
		startRequest('./include/login.php?username='+u+'&password='+p,'');	
		gotourl('index.php?modulo=gest_basket&step=2')
	}
}


function logout(){
	startRequest('./include/logout.php?esci=ESCI','');	
	ricarica();
}


function gotourl(url){
	setTimeout("window.location.href = '"+url+"';",500);
}

function trova(){
	var s = document.getElementById('search').value;
	startRequest('./moduli/homepagelog/trova.php?s='+s,'resultsearch');
}

function ricarica(){
	setTimeout("window.location.href = 'index.php';",500);
}


function ricaricaflag(){
	setTimeout("window.location.href = window.location.href;",500);
}

function mostradesc(id){
 
 	if(document.getElementById(id).style.display=='block')
 		document.getElementById(id).style.display='none';
 	else
 		document.getElementById(id).style.display='block';
}

function chkpass(p,p1){
	var pa = document.getElementById(p).value;
	var pb = document.getElementById(p1).value;
	
	if(pa!=pb) {
		alert('ERRORE : Le password inserite sono differenti!');
		return 0;
	}
	
	return 1;
}

function aprichiudi(id){
 	var e = document.getElementById(id);
 	var val = e.style.display;
 	
 	if(val=='block') {
 		document.getElementById('img'+id).src='./img/zip-plus.gif';
		e.style.display='none';
	} else {
	 	document.getElementById('img'+id).src='./img/zip-minus.gif';
		e.style.display='block';
	}
}

function aprichiudipop(id){

 	var e = document.getElementById(id);
 	var val = e.style.display;
 	
 	if(val=='block') {
 		document.getElementById('img'+id).src='../../img/zip-plus.gif';
		e.style.display='none';
	} else {
	 	document.getElementById('img'+id).src='../../img/zip-minus.gif';
		e.style.display='block';
	}
}


function selectdatepop(id,targ,noclose,diff,newdt){
 
 	if(document.getElementById(targ).style.display=='block' && noclose=='0') {
		document.getElementById(targ).style.display='none';
	} else {
		var dt = document.getElementById(id).value;
		
		if(diff!='0') dt=newdt;
		var pz = dt.split('-');
	
		var url='../../include/selectdatepop.php?id='+id+'&y='+pz[2]+'&m='+pz[1]+'&d='+pz[0]+'&targ='+targ+'&diff='+diff;

		document.getElementById(targ).style.display='block';
		startRequest(url,targ);
	}
}

function selectdate(id,targ,noclose,diff,newdt,mindt,maxdt){
 
 	if(document.getElementById(targ).style.display=='block' && noclose=='0') {
		document.getElementById(targ).style.display='none';
	} else {
		var dt = document.getElementById(id).value;
		
		if(diff!='0') dt=newdt;
		var pz = dt.split('-');
	
		var url='./include/selectdate.php?id='+id+'&y='+pz[2]+'&m='+pz[1]+'&d='+pz[0]+'&mindt='+mindt+'&maxdt='+maxdt+'&targ='+targ+'&diff='+diff;

		document.getElementById(targ).style.display='block';
		startRequest(url,targ);
	}
}


function impostadata(id,dt,targ){
	if(document.getElementById(id)){
		document.getElementById(id).value=dt;
		if(document.getElementById(targ)) {
			document.getElementById(targ).style.display='none';
			document.getElementById(id).onchange();
		}
	}
}


function chkcampiReg(campi){
 	if(document.getElementById('privacy').checked){
		chkcampi(campi);
	} else {
		alert("Per proseguire con la registrazione è necessario accettare l'informativa sulla privacy");
	}
}

function chkcampi(campi){
	var pz = campi.split(',');

	var err=0;
	var i;
    for(i=0; i<pz.length; i++) {
    	if(document.getElementById(pz[i]).value==""){
			document.getElementById(pz[i]).style.backgroundColor='#ff8888';
			err=1;
		} else {
			document.getElementById(pz[i]).style.backgroundColor='#ffffff';
		}		
    }
    if(err==0) return(1); // OKKK
	else {
		alert('Completare tutti i campi obbligatori! ');// ERROR
	}
	return(0);
}

function openclose(id,img,time){
 	document.getElementById(id).style.display='block';
	if(document.getElementById(img))
		document.getElementById(img).style.display='none';	
  
 	setTimeout("document.getElementById('"+id+"').style.display='none';",time);
 	
	 if(document.getElementById(img))
	 	setTimeout("document.getElementById('"+img+"').style.display='inline';",time);
}

function previewMSG(){
	var txt="<div class='testo' style='font-size:16px;padding:2px;background:#dddddd;width:99%;margin-top:2px;margin-bottom:5px;'><b>"+document.getElementById('msg_title').value+"</b></div><div class='testo' style='border-bottom:1px solid #dddddd;'><table width='100%' cellspacing='0' cellpadding='0'><tr>	<td>Da : <b>"+document.getElementById('msg_from').value+"</b></td>	<td>Data : <b>gg/mm/aaaa</b></td></tr></table></div><div class='testo' style='margin-top:5px;'>"+document.getElementById('msg_text').value+"</div>";
 
	document.getElementById('preview').innerHTML=txt;
}

function chkreg(){
 	us=document.getElementById('newusername').value;
 	p1=document.getElementById('password1').value;
 	p2=document.getElementById('password2').value;
 	m1=document.getElementById('mail1').value;
	startRequest('./moduli/register/chk_user.php?u='+us+'&p1='+p1+'&p2='+p2+'&m1='+m1,'chkusername');
}



function trim(inputString) {
 	if (typeof inputString != "string") { return inputString; }
  var retValue = inputString;
  var ch = retValue.substring(0, 1);
  while (ch == " ") { // Check for spaces at the beginning of the string
     retValue = retValue.substring(1, retValue.length);
     ch = retValue.substring(0, 1);
 	  }
 	  ch = retValue.substring(retValue.length-1, retValue.length);
 	  while (ch == " ") { // Check for spaces at the end of the string
     retValue = retValue.substring(0, retValue.length-1);
     ch = retValue.substring(retValue.length-1, retValue.length);
 	  }
 	  while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
     retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
 	  }
 	return retValue; // Return the trimmed string back to the user
  } // Ends the "trim" function


function fieldClean(fld){
  	reClean = new RegExp("[*]*","gi");
  	fld.value=fld.value.replace(reClean, "");
  }

function fieldLenCheck(fld,lbl){
	if (fld.value.length!=0){
		fld.value = trim(fld.value);
		fieldClean(fld);

		if ( fld.value.length==0 || fld.value.length<3 ) {
			alert(lbl);
			fld.select();
			return false;
		} else {
	 	return true;
		}
	}else{
		return true;
	}
}

function formCheck(fld,msgerr){
	var formOk;
	if (!fieldLenCheck(fld,msgerr)) { return false }
	if (fld.value.length==0) { return false }
	return(true);
}

function formCheckAdv(msgerr){
		var formOk;
		if (!fieldLenCheck(document.ricerca.autore,msgerr)) { return false }
		if (!fieldLenCheck(document.ricerca.titolo,msgerr)) { return false }
		if (!fieldLenCheck(document.ricerca.editore,msgerr)) { return false }
		if (!fieldLenCheck(document.ricerca.anno,msgerr)) { return false }
		if (!fieldLenCheck(document.ricerca.daprezzo,msgerr)) { return false }
		if (!fieldLenCheck(document.ricerca.aprezzo,msgerr)) { return false }
		
	  if ((document.ricerca.autore.value.length==0) && (document.ricerca.titolo.value.length==0) && (document.ricerca.editore.value.length==0)&& (document.ricerca.anno.value.length==0)&& (document.ricerca.daprezzo.value.length==0)&& (document.ricerca.aprezzo.value.length==0)) { return false }
		return(true);
}

function valueCheck(fld){
	var arrCodes = new Array(159,192,193,194,195,196,197,199,200,201,202,203,204,205,206,207,209,210,211,213,214,215,217,218,219,220,221,223); //valori Unicode
	var arrCharA = new Array("Y","A'","A'","A","A","A","A","C","E'","E'","E","E","I'","I'","I","I","N","O'","O'","O","O","O","U'","U'","U","U","Y","SS"); //caratteri accentati da sostituire
	var arrCharN = new Array("Y","A","A","A","A","A","A","C","E","E","E","E","I","I","I","I","N","O","O","O","O","O","U","U","U","U","Y","SS"); //caratteri normali da sostituire
	var val = fld.value.toUpperCase()+" ";
	for (i=0; i<val.length; i++){
		for(j=0; j<arrCodes.length; j++){
			if (val.charCodeAt(i) == arrCodes[j] && val.charAt(i+1) == " "){
				val = val.substring(0,i) + arrCharA[j] + val.substring(i + 1, val.length);
				i = i + (arrCharA[j].length - 1);
				break;
			}
			if (val.charCodeAt(i) == arrCodes[j] && val.charAt(i+1) != " "){
				val = val.substring(0,i) + arrCharN[j] + val.substring(i + 1, val.length);
				i = i + (arrCharN[j].length - 1);
				break;
			}
		}
	}
	fld.value = val.substring(0,val.length-1);
}

function isnum(obj,consenti_vuoto, msg_errvuoto, msg_errnumber) {
    if (consenti_vuoto=='0'&& obj.value==""){
    	alert(msg_errvuoto);
    	return false;
    }	
		if(obj.value!=""){
		  if(!isNaN(obj.value)||parseInt(obj.value)<=0){
				alert(msg_errnumber);
    		return false;
			}
		} 
}

function salvaBAN() {
  val = confirm('Effettuare la modifica?');

  if (val) {

	if (document.getElementById('banner_showNEW').value==""){
		alert('Indicare lo stato del banner');
		return false;
	}

	if (document.getElementById('banner_name').value==""){
		alert('Indicare il nome del banner');
		return false;
	}

	if (document.getElementById('banner_internal_mmNEW').value==""){
		alert('Indicare La tipologia del banner');
		return false;
	}	
	
  	if (document.getElementById('banner_imageurl').value==""){	
  	 	alert("Indicare il nome dell'immagine");
		return false;
  	} 
  	
    document.getElementById('subNEW').value = "SALVA";
    document.docban.submit();
  }
}

function eliminaBAN(){
  val = confirm('Effettuare la cancellazione?');
  if (val) {
    document.getElementById('subNEW').value = "ELIMINA";
    document.docban.submit();
  }
}


function addtobasket(id,path){
	startRequest(path+'./include/addtobasket.php?id='+id,'');
	
	if(document.getElementById('modulocarrello'))
		setTimeout("startRequest('"+path+"./moduli/carrello/dettaglio_carrello.php','modulocarrello');",200);
		

}