/**
 * validate.js
 *
 * Manages the validation of a form
 */
 
/**
 * Minimum number of characters
 */
 function txtMin(obj,caption,min){
	valid=true;
	//obj=document.getElementsByName(name)[0];
	if (obj.value!="" && obj.value.length<min){
		alert("Inserire almeno "+min+" caratteri nel campo '"+caption+"'.");
		valid=false;
	}
	
	return valid;
 }

 /**
  * Obligatory field
  */
 function txtObligatory(obj,caption){
	valid=true;
	//obj=document.getElementsByName(name)[0];
	if (obj.value==""){
		alert("Il campo '"+caption+"' č obbligatorio.");
		valid=false;
	}
	
	return valid;
 }
 
 /**
  * Validation of fields
  */
 function txtValidation(obj,caption,type){
	valid=true;
	//obj=document.getElementsByName(name)[0];
	txtValue=obj.value;
	if (obj.value=="") return true;
	
	switch(type){
		case "mail":
			
			atpos=txtValue.indexOf("@");
			atpos2=txtValue.indexOf("@",atpos+1);
			if (atpos<1 || atpos==txtValue.length-1 || atpos2!=-1){
				valid=false;
			}
			else{

				dotpos=txtValue.lastIndexOf(".");
				if (dotpos==-1 || dotpos<atpos || dotpos==txtValue.length-1){
					valid=false;
				}
				else{
					var validc="0123456789.@-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
					for (var i=0;i<txtValue.length;i++){
						valid=false;
						for (var j=0;j<validc.length;j++){
							if (validc.charAt(j)==txtValue.charAt(i)) valid=true;
						}
					}	
				}
			}
						
			break;
		case "??":
			break;
	}
	
	if (!valid){
		alert("Il campo '"+caption+"' non č valido.");
	}
	
	return valid;
 }
 
/**
 * Validation of FrmSearch
 */ 
function validateFrmSearch(form){
	if (!txtObligatory(form.search,"Cerca nel sito")) return false;

	if (!txtMin(form.search,"Cerca nel sito",3)) return false;
		
	return true;
}

/**
 * Validation of FrmDistribute
 */ 
function validateFrmDistribute(form){
	if (!txtObligatory(form.txtCompanyName,"Ragione sociale")) return false;
	if (!txtObligatory(form.txtContactFirstName,"Cognome referente")) return false;
	if (!txtObligatory(form.txtContactLastName,"Nome referente")) return false;
	if (!txtObligatory(form.txtAddress,"Indirizzo")) return false;
	if (!txtObligatory(form.txtZIP,"CAP")) return false;
	if (!txtObligatory(form.txtCity,"Cittā")) return false;
	if (!txtObligatory(form.cmbProvince,"Provincia")) return false;
	if (!txtObligatory(form.txtPhone,"Telefono")) return false;
	if (!txtObligatory(form.txtMail,"E-Mail")) return false;
	if (!txtObligatory(form.txtJob,"Attivitā svolte")) return false;

	if (!txtMin(form.txtCompanyName,"Ragione sociale",3)) return false;
	if (!txtMin(form.txtContactFirstName,"Cognome referente",3)) return false;
	if (!txtMin(form.txtContactLastName,"Nome referente",3)) return false;
	if (!txtMin(form.txtAddress,"Indirizzo",5)) return false;
	if (!txtMin(form.txtZIP,"CAP",5)) return false;
	if (!txtMin(form.txtCity,"Cittā",3)) return false;
	if (!txtMin(form.txtPhone,"Telefono",5)) return false;
	if (!txtValidation(form.txtMail,"E-Mail","mail")) return false;
	if (!txtMin(form.txtJob,"Attivitā svolte",10)) return false;
		
	return true;
}

/**
 * Validation of FrmFreeCopy
 */ 
function validateFrmFreeCopy(form){
	if (!txtObligatory(form.txtContactFirstName,"Cognome")) return false;
	if (!txtObligatory(form.txtContactLastName,"Nome")) return false;
	if (!txtObligatory(form.txtAddress,"Indirizzo")) return false;
	if (!txtObligatory(form.txtZIP,"CAP")) return false;
	if (!txtObligatory(form.txtCity,"Cittā")) return false;
	if (!txtObligatory(form.cmbProvince,"Provincia")) return false;
	if (!txtObligatory(form.txtMail,"E-Mail")) return false;
	if (!txtObligatory(form.cmbAge,"Anno di nascita")) return false;

	if (!txtMin(form.txtContactFirstName,"Cognome",5)) return false;
	if (!txtMin(form.txtContactLastName,"Nome",5)) return false;
	if (!txtMin(form.txtAddress,"Indirizzo",5)) return false;
	if (!txtMin(form.txtZIP,"CAP",5)) return false;
	if (!txtMin(form.txtCity,"Cittā",3)) return false;
	if (!txtMin(form.txtPhone,"Telefono",5)) return false;
	if (!txtValidation(form.txtMail,"E-Mail","mail")) return false;
		
	return true;
}

/**
 * Validation of FrmFreeCopyCompany
 */ 
function validateFrmFreeCopyCompany(form){
	if (!txtObligatory(form.txtCompanyName,"Ragione sociale")) return false;
	if (!txtObligatory(form.txtContactFirstName,"Cognome")) return false;
	if (!txtObligatory(form.txtContactLastName,"Nome")) return false;
	if (!txtObligatory(form.txtAddress,"Indirizzo")) return false;
	if (!txtObligatory(form.txtZIP,"CAP")) return false;
	if (!txtObligatory(form.txtCity,"Cittā")) return false;
	if (!txtObligatory(form.cmbProvince,"Provincia")) return false;
	if (!txtObligatory(form.txtMail,"E-Mail")) return false;
	if (!txtObligatory(form.txtJob,"Attivitā svolte")) return false;

	if (!txtMin(form.txtCompanyName,"Ragione sociale",5)) return false;
	if (!txtMin(form.txtContactFirstName,"Cognome",5)) return false;
	if (!txtMin(form.txtContactLastName,"Nome",5)) return false;
	if (!txtMin(form.txtAddress,"Indirizzo",5)) return false;
	if (!txtMin(form.txtZIP,"CAP",5)) return false;
	if (!txtMin(form.txtCity,"Cittā",3)) return false;
	if (!txtMin(form.txtPhone,"Telefono",5)) return false;
	if (!txtValidation(form.txtMail,"E-Mail","mail")) return false;
	if (!txtMin(form.txtJob,"Attivitā svolte",10)) return false;
		
	return true;
}

/**
 * Validation of FrmCard
 */ 
function validateFrmCard(form){
	if (!txtObligatory(form.txtCompanyName,"Ragione sociale")) return false;
	if (!txtObligatory(form.txtContactFirstName,"Cognome referente")) return false;
	if (!txtObligatory(form.txtContactLastName,"Nome referente")) return false;
	if (!txtObligatory(form.txtAddress,"Indirizzo")) return false;
	if (!txtObligatory(form.txtZIP,"CAP")) return false;
	if (!txtObligatory(form.txtCity,"Cittā")) return false;
	if (!txtObligatory(form.cmbProvince,"Provincia")) return false;
	if (!txtObligatory(form.txtPhone,"Telefono")) return false;
	if (!txtObligatory(form.txtMail,"E-Mail")) return false;
	if (!txtObligatory(form.txtJob,"Attivitā svolte")) return false;

	if (!txtMin(form.txtCompanyName,"Ragione sociale",3)) return false;
	if (!txtMin(form.txtContactFirstName,"Cognome referente",5)) return false;
	if (!txtMin(form.txtContactLastName,"Nome referente",5)) return false;
	if (!txtMin(form.txtAddress,"Indirizzo",5)) return false;
	if (!txtMin(form.txtZIP,"CAP",5)) return false;
	if (!txtMin(form.txtCity,"Cittā",3)) return false;
	if (!txtMin(form.txtPhone,"Telefono",5)) return false;
	if (!txtValidation(form.txtMail,"E-Mail","mail")) return false;
	if (!txtMin(form.txtJob,"Attivitā svolte",10)) return false;
		
	return true;
}

/**
 * Validation of FrmComment
 */ 
function validateFrmComment(form){
	if (!txtObligatory(form.name,"Nome")) return false;
	if (!txtObligatory(form.comment,"Commento")) return false;
    if (!txtObligatory(form.email,"E-Mail")) return false;

	if (!txtMin(form.name,"Nome",3)) return false;
	if (!txtMin(form.comment,"Commento",5)) return false;
	if (!txtValidation(form.email,"E-Mail","mail")) return false;
		
	return true;
}

/*
 * Open new windows
 */ 
function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
} 

/*
 * Validation of FrmSendEmail
 */ 
function validateFrmSendEmail(form){
	if (!txtObligatory(form.s_name,"Il tuo nome")) return false;
	if (!txtObligatory(form.s_email,"Il tuo indirizzo e-mail")) return false;
	if (!txtObligatory(form.r_name,"Il nome del tuo amico")) return false;
	if (!txtObligatory(form.r_email,"L'indirizzo e-mail del tuo amico")) return false;
	if (!txtObligatory(form.msg,"Scrivi il tuo messaggio")) return false;

	if (!txtValidation(form.s_email,"Il tuo indirizzo e-mail","mail")) return false;
	if (!txtValidation(form.r_email,"L'indirizzo e-mail del tuo amico","mail")) return false;
	if (!txtMin(form.msg,"Scrivi il tuo messaggio",5)) return false;
		
	return true;
}

/**
 * Validation of FrmAdvancedSearch
 */ 
function validateFrmAdvancedSearch(form){
	return true;
}

/**
 * Validation of FrmAdvancedSearchHomePage
 */ 
function validateFrmAdvancedSearchHomePage(form){
	if (!txtObligatory(form.RGID,"Provincia")) return false;

	return true;
}