// script.js
var cmd = '/ConsorzioItaliano/doAction';
var cmdSPNode = cmd + '/site/ShowPage?ndname=';
var images_user_path = '/resources/consorzioita/users';
	//Funzione per controllo e sottomissione registrazione utente
//In :	objectForm		--> Form
//Controllo sulla validita' dei campi del form di registrazione
//Chiamata da registerForm.jsp
function RegUsrValidate(objectForm){
	var errore = false;
	var err_msg = "";
	var loggato = objectForm.isLogged.value;
	
	var nome = objectForm.fname.value;	
	var cognome = objectForm.lname.value;	
	var indi = objectForm.indi.value;
	var nciv = objectForm.nciv.value;
	var cap = objectForm.zipcode.value;
	var citta = objectForm.city.value;
	var nazione = objectForm.country[objectForm.country.selectedIndex].value;
	var prov = '';
	if(nazione == 'ITA') prov = objectForm.state[objectForm.state.selectedIndex].value;
	else prov = objectForm.state.value;
	var phone = objectForm.address3.value;
	var fax = objectForm.adfield1.value;
	var email = objectForm.email.value;	

	var piva = objectForm.address2.value;
	
	var domanda = objectForm.challenge_question.value;
	var risposta = objectForm.challenge_answer.value;
	
	
	// se sono loggato i tre campi LOGINID, PWD e PWDCONF non devono essere controllati
	var login = "X";
	var pwd = "X";
	var pwdconf = "X";	

	if (loggato=="NO") {
		login = objectForm.loginid.value;
		pwd = objectForm.password.value;
		pwdconf = objectForm.pswdconf.value;	
	}
	

	if((nome == '') || (cognome == '') || (email == '') || (login == '') || (pwd == '') || (pwdconf == '') ||
		(indi == '') || (nciv == '') || (cap == '') || (citta == '') || (nazione == '') || (prov == '') ||
		(phone == '') || (fax == '') || (email == '') || (piva == '')  ||
		(domanda == '') || (risposta == '')
	) {
		errore = true;
		err_msg = msgInfo('ERR_JS_INSFIELD');
	} else {
		// controlla la validita' della partita iva
		if(piva != ''){
			piva = piva.toUpperCase();
			var v1 = getIso(nazione);
			if(v1 == '') checkPIVA(piva);
			else {
				piva = v1 + piva;
				ckIva = checkPIVA_UE(piva);			
				if(!ckIva) {
					
					err_msg = msgInfo('ERR_JS_FIELDNOTVALID') + '\n';
					err_msg = msgInfo('PIVA');
					errore = true;
					
				}
			}
		}
		
		// controllo validita' compilazione campO MAIL
		var ckMail = checkMAIL(email);
		if(!ckMail)  {
			err_msg = msgInfo('ERR_JS_FIELDNOTVALID') + '\n';
			if(!ckMail) err_msg = err_msg + msgInfo('MAIL') + '\n';
			errore = true;
		}

		// controllo validita' compilazione campi CAP e TELEFONO
		if((cap != '') || (phone != '') ){
			var ckZip = IsNumeric(cap);
			var ckPhone = IsNumeric(phone);
			if(!ckZip || !ckPhone )  {
				err_msg = msgInfo('ERR_JS_FIELDNOTVALID') + '\n';
				if(!ckZip) err_msg = err_msg + msgInfo('ZIPCODE') + '\n';
				if(!ckPhone) err_msg = err_msg + msgInfo('PHONE') + '\n';
				errore = true;
			}
		}		

		// controllo ugualianza tra le due password solo se in registrazione, in modifica le password non ci sono
		if(loggato=='NO'){
			if(!CheckPwd(pwd, pwdconf)){
				errore = true;
				err_msg = msgInfo('PWD_NOTSAME');
			}
		}
	}
	
	if(errore) {
		alert(err_msg);
	} else {
		objectForm.address1.value = indi + "|" + nciv;	
		objectForm.submit();
	}
}


//Funzione per controllo ugualianza tra le due password	
//In :	pwd		--> password
//		pwdconf	--> conferma password
function CheckPwd(pwd,pwdconf){
	if(pwd != pwdconf) return false
	else return true
}       

function getIso(nazione) {
	nazione = nazione.toLowerCase();
	var v1 = '';
	if(nazione == 'aut') v1 = 'AT';
	if(nazione == 'bel') v1 = 'BE';
	if(nazione == 'bgr') v1 = 'BG';
	if(nazione == 'cyp') v1 = 'CY';
	if(nazione == 'cze') v1 = 'CZ';
	if(nazione == 'dnk') v1 = 'DK';
	if(nazione == 'est') v1 = 'EE';
	if(nazione == 'fin') v1 = 'FI';
	if(nazione == 'fra') v1 = 'FR';
	if(nazione == 'deu') v1 = 'DE';
	if(nazione == 'grc') v1 = 'EL';
	if(nazione == 'hun') v1 = 'HU';
	if(nazione == 'irl') v1 = 'IE';
	if(nazione == 'ita') v1 = 'IT';
	if(nazione == 'lat') v1 = 'LV';
	if(nazione == 'ltu') v1 = 'LT';
	if(nazione == 'lux') v1 = 'LU';
	if(nazione == 'mlt') v1 = 'MT';
	if(nazione == 'nld') v1 = 'NL';
	if(nazione == 'pol') v1 = 'PL';
	if(nazione == 'prt') v1 = 'PT';
	if(nazione == 'rou') v1 = 'RO';
	if(nazione == 'svk') v1 = 'SK';
	if(nazione == 'svn') v1 = 'SI';
	if(nazione == 'esp') v1 = 'ES';
	if(nazione == 'swe') v1 = 'SE';
	if(nazione == 'gbr') v1 = 'GB';
	return v1;
}   

//Controllo Partita iva
//out: return true 	--> se corretta
function checkPIVA(ctrlText) {
	var isCorrect = true;
	var txtLen = ctrlText.length;
	var rexp = '';
	if(txtLen == 11) rexp = new RegExp("[0-9]{11}");
	else if(txtLen > 11 && txtLen < 16) rexp = new RegExp("[a-zA-Z0-9]");
	else if(txtLen == 16) rexp = new RegExp("^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$");
  	else isCorrect = false;
  	if((txtLen >= 11)) isCorrect = rexp.test(ctrlText);  	
  	else isCorrect = false;

  	if(ctrlText.indexOf(" ")>-1) isCorrect = false;
  	return isCorrect;
}

function checkPIVA_UE(ctrlText)
	{		
		var alphabeth;
		var digits;
		var VATinput;
		
		alphabeth = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*+";
		digits = "0123456789";
		VATinput = ctrlText;
		VATinput = VATinput.replace(/^\s+/,'');
		VATinput = VATinput.replace(/\s+$/,'');	
		
		if (VATinput.indexOf(" ")>-1) return false;
		if (VATinput.length < 4) return false;
		
		for (i = 0; i < VATinput.length; i++)
		{
			if (digits.indexOf(VATinput.substr(i,1)) == -1 &&
				(alphabeth.indexOf(VATinput.substr(i,1).toUpperCase()) == -1))
			{
				return false;
			}
		}
		
		var iso = VATinput.substring(0,2);
		var vat = VATinput.substring(2, VATinput.length);
		
		if (iso == "AT"){
			if (vat.substring(0,1) == 'U' && vat.length == 9) return true;
			else return false;
		}
		if (iso == "BE"){
			if (vat.length == 9 || vat.length == 10) return true;
			else return false;
		}		
		if (iso == "BG"){
			if (vat.length == 9 || vat.length == 10) return true;
			else return false;
		}
		if (iso == "CY"){
			if (vat.length == 9) return true;
			else return false;
		}		
		if (iso == "DK"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}		
		if (iso == "EE"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			else return false;
		}		
		if (iso == "FI"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}
		if (iso == "FR"){
			if (vat.length == 11 && IsNumeric(vat.substring(2,11))) return true;
			else return false;
		}
		if (iso == "DE"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "GB"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			if (vat.length == 12 && IsNumeric(vat)) return true;
			if (vat.length == 5 && IsNumeric(vat.substring(2,5))) return true;
			return false;
		}	
		if (iso == "EL"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "IE"){
			if (vat.length == 8) return true;
			else return false;
		}		
		if (iso == "IT"){
			if (vat.length == 11 && IsNumeric(vat) && vat!='00000000000') return checkPIVA_IT(vat);
			else return false;
		}	
		if (iso == "LV"){
			if (vat.length == 11 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "LT"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			if (vat.length == 12 && IsNumeric(vat)) return true;
			return false;
		}	
		if (iso == "LU"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "MT"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "NL"){
			if (vat.length == 12 && vat.substring(9,10) == "B") return true;
			else return false;
		}	
		if (iso == "PL"){
			if (vat.length == 10 && IsNumeric(vat)) return true;
			else return false;
		}		
		if (iso == "PT"){
			if (vat.length == 9 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "CZ"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			if (vat.length == 9 && IsNumeric(vat)) return true;
			if (vat.length == 10 && IsNumeric(vat)) return true;
			return false;
		}	
		if (iso == "RO"){
			if (vat.length >= 2 && vat.length <= 10 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "SK"){
			if (vat.length == 10 && IsNumeric(vat)) return true;
			else return false;
		}		
		if (iso == "SI"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "ES"){
			if (vat.length == 9) return true;
			else return false;
		}	
		if (iso == "SE"){
			if (vat.length == 12 && IsNumeric(vat)) return true;
			else return false;
		}	
		if (iso == "HU"){
			if (vat.length == 8 && IsNumeric(vat)) return true;
			else return false;
		}																												
		return false;
	}
	
	function checkPIVA_IT(vat){
		var c=0;
		var s=0;
		var pari=0;
		var pariSum=0;
		var dispariSum=0;
		for (i = 0; i < vat.length; i++)
		{
			if (i==0 || i==2 || i==4 || i==6 || i==8) //dispari
			{
				dispariSum = dispariSum + eval(vat.substr(i,1));
			}
			if (i==1 || i==3 || i==5 || i==7 || i==9) //pari
			{
				pari = eval(vat.substr(i,1));
				pari = pari * 2;
				if (pari > 9) pari = pari - 9;
				pariSum = pariSum + pari;
			}			
		}
		
		s = s + dispariSum;
		s = s + pariSum;
		
		var r = s % 10;
		if (r==0)c=0;
		else c = 10 - r;

		var vat_c = eval(vat.substr(10,1));
		
		if (c == vat_c) return true;
		else return false;
		
		
	}

//Controllo EMAIL
//out: true 	--> se corretta
function checkMAIL(ctrlText) {
	var rexpNumeric = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4})(\\]?)$");
	var isCorrect = false;
	isCorrect = rexpNumeric.test(ctrlText);
  	return isCorrect;
}
function trim(str) {
	if (str != null) {
		var i;         
		for (i=0; i<str.length; i++) {           
			if (str.charAt(i)!=" ") {                
				str=str.substring(i,str.length);                
				break;           
			}        
		}             
		for (i=str.length-1; i>=0; i--) {            
			if (str.charAt(i)!=" ") {                
				str=str.substring(0,i+1);                
				break;           
			}         
		}                
		if (str.charAt(0)==" ") {            
			return "";         
		} else {            
			return str;         
		}    
	}
}       

function IsNumeric(sText) {	
	//isNan --> restituisce true se il valore NON è numerico
	var ckNum = isNaN(sText);
	return !ckNum;
}


function loadFile(objFrm) {
	var image1 = objFrm.imgFileName.value;
		if(image1 == '') {
			alert('Prego, inserire un file');
			return false;
		} else {
			document.getElementById('progressArea').style.display='block';
			document.getElementById('progressArea').style.visibility = 'visible';
			Loading();
		}
}

function hideWindow() {
	document.getElementById('progressAreaEnd').style.display='none';
	document.getElementById('progressAreaEnd').style.visibility = 'hidden';
}		
var espandi = 0;
function Loading() {
   // Incrementa la barra di stato dell'avanzamento
   	barra.style.width = espandi;
     espandi++;
        // Incrementa il contatore percentuale dell'avanzamento
        percentuale.innerHTML = Math.round(espandi / 2) + "%";
        if (espandi != 200)  window.setTimeout("Loading()", 100);
      }
	

/* 	upload image
	apre popup per caricare immagini
	*/
function searchImage(canReplace, typeImage) {
	var url = cmdSPNode + 'upload_image&canreplace=' + canReplace + '&typeImage=' + typeImage;
	document.getElementById('insertImage').style.visibility = 'visible';
	document.getElementById('goToUpload').src = url;
	//document.getElementById('insertImage').style.visibility='show';
	//window.open(url,'Upload','width=300,height=300 ,toolbar=yes, location=no,status=yes,menubar=yes,scrollbars=no,resizable=no');
}		          

function hideWindow() {
	document.getElementById('progressAreaEnd').style.display='none';
	document.getElementById('progressAreaEnd').style.visibility = 'hidden';	
}	

function close() {
	document.getElementById('insertImage').style.display='none';
	document.getElementById('insertImage').style.visibility = 'hidden';		
}

function closeImageWindow() {
	parent.document.getElementById('insertImage').style.visibility = 'hidden';	
}

function userImage(typeImage) {
	
	element = document.getElementById('insertImage');
	var url = cmdSPNode + 'upload_user_image&canReplace=yes&typeImage=' + typeImage;
	document.getElementById('goToUpload').style.top="800px";
	document.getElementById('goToUpload').src = url;
	document.getElementById('insertImage').style.visibility = 'visible';
}



/* 	Visualizza immagini prodotto
	typeImage 	--> Tipologia immagine small o big
	event		--> Evento
	Con evento acquisisco informazioni sulla posizione in cui devo 
	visualizzare l'immagine.
	Con il tipo di immagine ricostruisco il nome del div che devo 
	visualizzare con il contentuto immagine.
	Il path corretto dell'immagine è contenuto nell'id dell'immagine stessa
	(div in uuploadImage.jspf)
*/
function showImage(typeImage,event) {
	var pathImage = '';
	var nameImage = '';
	var posL = event.clientX;
	var posT = document.documentElement.scrollTop + event.clientY;
	var nameDiv = 'show_' + typeImage + '_image';
	document.getElementById(nameDiv).style.left = posL + 'px';
	document.getElementById(nameDiv).style.top = posT + 'px';
	if(typeImage == 'small') {
		pathImage = document.smallImage.id;
		nameImage = document.frmProduct.cethmb.value;
		document.smallImage.src=pathImage + '/' + nameImage;
	} else {
		pathImage = document.bigImage.id;
		nameImage = document.frmProduct.cefull.value;
		document.bigImage.src=pathImage + '/' + nameImage;
	}
	document.getElementById(nameDiv).style.visibility = 'visible';
	document.getElementById(nameDiv).style.display = 'block';
	document.getElementById(nameDiv).style.position = 'absolute';		
}
/* La differenza rispetto a sopra che c'è sempre immagine */
function showImageUser(typeImage,event) {
	var pathImage = '';
	var nameImage = '';
	var posL = event.clientX;
	var posT = document.documentElement.scrollTop + event.clientY;
	var nameDiv = 'show_' + typeImage + '_image';
	document.getElementById(nameDiv).style.left = posL + 'px';
	document.getElementById(nameDiv).style.top = posT + 'px';
	nameImage = document.frmRegister.userId.value;
	if(typeImage == 'small') {
		nameImage = nameImage + "_small.jpg";
		pathImage = document.smallImage.id;
		document.smallImage.src=pathImage + '/' + nameImage + '?data='+escape(new Date()); //aggiunta data per refresh
	} else {
		nameImage = nameImage + ".jpg";
		pathImage = document.bigImage.id;
		document.bigImage.src=pathImage + '/' + nameImage + '?data='+escape(new Date());
	}
	document.getElementById(nameDiv).style.visibility = 'visible';
	document.getElementById(nameDiv).style.display = 'block';
	document.getElementById(nameDiv).style.position = 'absolute';		
}
/* Nasconde div */
function closeElement(id) {
document.getElementById(id).style.visibility='hidden'
}



/* INFORMAZIONI PRODOTTO */
function checkProductInfo(objectForm) {
	var name = objectForm.cedname.value;
	var cethmb = objectForm.cethmb.value;
	var cefull = objectForm.cefull.value;
	var cpprice = objectForm.cpprice.value;
	var ckError = false;
	if((name == '') || (cethmb == '') || (cefull == '') || (cpprice == '')) {
		var message = msgInfo('ERR_JS_FIELDNOTVALID') + '\n';
		if(name == '') message = message + msgInfo('PROD_NAME') + '\n';
		if(cethmb == '')message = message + msgInfo('PROD_TIMG') + '\n';
		if(cefull == '') message = message + msgInfo('PROD_FIMG') + '\n';
		//if(cpprice == '') message = message + msgInfo('PROD_PRICE') + '\n';
		alert(message);
		ckError = true;
	}
	if(ckError) return false;
	else return true;
}
