/*
function Trim(s){
function isValidEmail(s){
function check(field) {	
function checkValueInteger(obj){
function chk_informativa(aForm){
validateForm_MailListaProdotti
addToBasket_superofferta(field, referrer_page){
*/

function Trim(s){
	s+="";
	var t="", u="";
	var flg=false;		
	for (var i=0;i<s.length;i++){
		if (flg){
			if (s.charAt(i)!=" "){
				t+=u;
				u="";
				t+=s.charAt(i);
			} else {
				u+=s.charAt(i);
			}
		} else {
			if (s.charAt(i)!=" "){
				t+=s.charAt(i);
				flg=true;
			}
		}
	}		
	return t;
}

//function to check valid email address
function isValidEmail(strEmail){
	var i = 1;
	var sLength = strEmail.length;
	var sMsgError = "E-mail not valid";
	
	// look for @
	while ((i < sLength) && (strEmail.charAt(i) != "@")){ 
		i++;
	}
	if ((i >= sLength) || (strEmail.charAt(i) != "@")) {
		alert(sMsgError);
		return false;
	} else {
		i += 2;	
	}
	// look for .
	while ((i < sLength) && (strEmail.charAt(i) != ".")){ 
		i++;
	}	
	// there must be at least one character after the .
	if ((i >= (sLength - 1)) || (strEmail.charAt(i) != ".")) {
		alert(sMsgError);
		return false;
	} else {
		return true;
	}
}

var checkflag = "false";
function check(field) {
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++) {			
			field[i].checked = true;
		}
		checkflag = "true";
	} else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = false; 	
		}
		checkflag = "false";
	}
	return false;  	
}

function checkValueInteger(value){
	if((value != "") ||(value != " ")){
		lunvalue = value.length;		
		i = 0;
		TROV = 0;
		while((i<lunvalue) && (TROV != 1)){					
			if(!((value.charCodeAt(i) >= 48) && (value.charCodeAt(i) <= 57))){
				TROV = 1;				
			}
			i++;		
		}
		if(TROV == 1){
			return false;
		} else {
			return true;
		}
	}else {	
		return false;
	}			
}

function chk_informativa(aForm_informativa,aForm_chk_tipo_utensile){
	if(!aForm_informativa.checked){		
		alert("Per proseguire devi accettare l'informativa");
		return false;
	}
	
	if(aForm_chk_tipo_utensile[0].checked){
		return true;
	} else {
		if(aForm_chk_tipo_utensile[1].checked){
			return true;			
		} else {
			alert("Selezionare il tipo di utensili");
			return false;					
		}
	}
}

function openWindow(URL,name,scrollbars,width,height) {
	if(URL != ""){			
		popupWin = window.open(URL,name,'scrollbars='+scrollbars+',resizable=0,menubar=0,width='+width+',height='+height);
	}
}

function validateForm_MailListaProdotti(aForm){
	with (aForm){
		s_email = Trim(eval(email).value);			
		s_nomecognome = Trim(eval(nomecognome).value);	
		i_cap = Trim(eval(cap).value);
		s_provincia = Trim(eval(provincia).value);
		i_telefono  = Trim(eval(telefono).value);
		
		if(s_nomecognome == ""){
			alert("You must insert name-surname");
			return false;
		}				
		if(i_cap != ""){
			if(!checkValueInteger(i_cap)){
				alert("Wrong Cap")
				return false;
			}
		}else{
			alert("You must insert the C.A.P.")
			return false;
		}
		if(s_provincia == ""){
			alert("You must insert the province");
			return false;
		}				
		if(i_telefono != ""){
			if(!checkValueInteger(i_telefono)){
				alert("Wrong telephone number")
				return false;
			}
		}else{
			alert("You must insert the telephone number")
			return false;
		}
		if(s_email != ""){	
			if(!isValidEmail(s_email)){
				alert("Wrong receiver e-mail");
				return false;
			}
		}else {
			alert("You must insert the e-mail")
			return false;
		}
		return true;
	}
}

function addToBasket_superofferta(field, referrer_page){

	var sCodComm = "";

	if(!field){
		return false;
	}

	if(field.length == undefined){		
		if(field.checked == true){
			sCodComm +=field.value+";";
		}
	} else {
		for (i = 0; i < field.length; i++) {						
			if(field[i].checked == true){
				sCodComm += field[i].value+";";
			}
		}	
	}
	if(sCodComm != ""){		
	    alert('The product has been inserted into basket.');    
    	url="add_basket_superofferta.htm?referrer_page="+referrer_page+"&operation=add_basket&codes_commerciale="+sCodComm;
    	location.href=url;
	} else {
		alert("Select at least one product")
		return false;
	}
}

