winPopUp=null;
winAdsPopUp=null;
winPrintProd=null;
function isEmpty(s){
	return (s==null || s.length==0)
}

function isWhitespace(s){
	var i;
	var whitespace;
	
	whitespace=" \t\n\r";
	for(i=0; i<s.length; i++)   
		if(whitespace.indexOf(s.charAt(i))==-1) return false;
	
    return true;
}

function isEmail(s){
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while (i<sLength && s.charAt(i) != "@")
    	i++;

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != "."))
    	i++;
    
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checktext(testo){
	i=0;
	while (i < testo.length){
		l = testo.substr(i, 1);
		if (l=="\"" || l==" " || l=="&" || l=="?" || l=="=" || l=="." || l=="'" ||  
			l=="/" || l=="+" || l=="%" || l=="!" || l=="£" || l=="|" || l=="(" ||
			l==")" || l=="ì" || l=="[" || l=="]" || l==" " || l=="°" || l=="§" ||
			l=="ù" || l=="#" || l=="{" || l=="}" || l=="è" || l=="é" || l=="*" ||
			l==":" || l=="," || l==";" || l=="<" || l==">")
			return false;
		i++;
	}

	return true;
}

function checkquotes(testo){
	i = 0;
	while (i < testo.length){
		if (testo.substr(i, 1) == "\"")
			return false;
		i++;
	}

	return true;
}

//////////
function SendMsg(f){
	f.Command.value="sendmsg";
	f.submit();
}
function ConfirmOrder(f){
	f.Command.value="confirm";
	f.submit();
}
function ContinueOrder(f){
	f.Command.value="continue";
	f.submit();
}
function CancelOrder(f){
	f.Command.value="cancel";
	f.submit();
}
function GalleryPOP(Lang, ID, PHPSESSID){
	loc="gallery.php?Lang="+Lang+"&ID="+ID+"&PHPSESSID="+PHPSESSID;
	window.open(loc,Lang,"toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1,status=0,width=500,height=450,left=100,top=100");
}
function ImmPOP(Lang, ID, Command, w, h, IDImm){
	loc="gallery.php?Lang="+Lang+"&ID="+ID+"&IDImm="+IDImm+"&Command="+Command;
	if(winAdsPopUp)
		winAdsPopUp.close();
	if(winPopUp)
		winPopUp.close();
	winPopUp=window.open(loc,Lang,"toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width="+w+",height="+h+",left=100,top=100");
}
function OpenContact(ID, Command){
	loc="ads_contact.php?ID="+ID+"&Command="+Command;
	if(Command=="contact"){
		w=390;
		h=370;
	}
	if(Command=="friend"){
		w=390;
		h=412;	
	}
	if(Command=="print"){
		w=500;
		h=620;	
	}
	if(winPopUp)
		winPopUp.close();
	if(winAdsPopUp)
		winAdsPopUp.close();
	winAdsPopUp=window.open(loc,"1","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width="+w+",height="+h+",left=100,top=100");
}
function OpenProduct(ID, Command){
	loc="prod_print.php?ID="+ID+"&Command="+Command;
	menuBar="1";
	if(Command=="friend"){
		w=520;
		h=620;
		menuBar="0";
	}
	if(Command=="print"){
		w=520;
		h=620;	
	}
	if(winPrintProd)
		winPrintProd.close();
	if(winPopUp)
		winPopUp.close();
	if(winAdsPopUp)
		winAdsPopUp.close();
	winPrintProd=window.open(loc,"1","toolbar=0,location=0,directories=0,menuBar="+menuBar+",scrollbars=1,resizable=1,status=0,width="+w+",height="+h+",left=100,top=100");
}
function ResendPassword(clientusername){
	user = prompt("Username",clientusername);
	if(user!=null){
		if(user.length>0){
			window.location="index.php?Mode=send&Command=pwd&user="+user;
		}else{
			ResendPassword();
		}
	}
}
function CheckProd_Print(){
	r = true;
	f = document.forms['prod_print'];
	f.action="prod_print.php";
	f.Command.value="send";
	if(f.NomeRic.value==""){
		alert("Inserire il proprio nome");
		r = false;
	}
	if(!isEmail(f.EmailRic.value)){
		alert("Inserire un indirizzo email valido");
		r = false;
	}
	if(!f.Consenso.checked){
		alert("Per inviare bisogna accettare le condizioni sul trttanmento dei dati.");
		r = false;
	}
	if(f.TypeSend.value=="friend"){
		if(f.NomeAmicoRic.value==""){
			alert("Inserire il campo nome amico");
			r = false;
		}
		if(!isEmail(f.EmailAmicoRic.value)){
			alert("Inserire un indirizzo email valido dell'amico");
			r = false;
		}		
	}
	if (r){ f.submit(); }
}
function CheckAds_Contact(){
	r = true;
	f = document.forms['ads_contact'];
	f.action="ads_contact.php";
	f.Command.value="send";
	if(f.NomeRic.value==""){
		alert("Inserire il proprio nome");
		r = false;
	}
	if(!isEmail(f.EmailRic.value)){
		alert("Inserire un indirizzo email valido");
		r = false;
	}
	if(!f.Consenso.checked){
		alert("Per inviare bisogna accettare le condizioni sul trttanmento dei dati.");
		r = false;
	}
	if(f.TypeSend.value=="friend"){
		if(f.NomeAmicoRic.value==""){
			alert("Inserire il campo nome amico");
			r = false;
		}
		if(!isEmail(f.EmailAmicoRic.value)){
			alert("Inserire un indirizzo email valido dell'amico");
			r = false;
		}		
	}
	if (r){ f.submit(); }
}
function CheckAd(){
	r = false;
	f = document.forms['ads'];
	f.action="ads.op.php";
	f.Command.value="insert";
	if(f.Title.value!=""){
		r = true;
	}else
		alert("riempire il campo Titolo");
	if (r){ f.submit(); }
}
function ConfDel (mode, id, descr){
	msg = "sei sicuro di voler eliminare " + descr;
	if (confirm(msg))
		window.location = mode+".op.php?Command=delete&ID="+id;
}
function CheckBacheca(){
	r = true;
	f = document.forms['bacheche'];
	f.action="bacheche.op.php";
	f.Command.value="insert";
	if(f.Title.value==""){
		r = false;
		alert("riempire il campo Titolo");
	}
	if (r){ f.submit(); }
}
function CheckBachecaImm(){
	f = document.forms['bacheche_imm'];
	f.action="bacheche.op.php";
	f.Command.value="ins_imm";
	f.submit();
}

