// ====================================================================================
// DATA CREAZIONE		: 06 settembre 2000
// TITOLO												: Scripts.js
// PROGETTO									: Ulixnet
// PROGRAMMATORE		: Alessandro Faustini
//------------------------------------------------------------------------------------
// FUNZIONE DELLA PAGINA
// Parte applicativa lato client 
//------------------------------------------------------------------------------------
// DIPENDENZE ESTERNE
//====================================================================================



// VERIFY USER DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyUserData(theForm)
{
	if(theForm.nome.value.length == 0)
	{
		alert("NOME e COGNOME sono dati indispensabili per la registrazione");
		theForm.nome.focus();
		return(false);
	}
	if(theForm.email.value.length == 0)
	{
		alert("L'indirizzo di E-MAIL  indispensabile per la registrazione");
		theForm.email.focus();
		return(false);
	}
	if(theForm.email.value.indexOf("@") == -1 || theForm.email.value.indexOf(".") == -1)
	{
		alert("L'indirizzo di E-MAIL ha un formato sconosciuto");
		theForm.email.focus();
		return(false);
	}
	if(theForm.telefono.value.length == 0)
	{
		alert("Il numero di telefono  indispensabile per la registrazione");
		theForm.telefono.focus();
		return(false);
	}
	if(theForm.userid.value.length == 0)
	{
		alert("Uno USERID sar indispensabile per accedere alle aree riservate");
		theForm.userid.focus();
		return(false);
	}
	if(theForm.password.value.length == 0 && theForm.task.value == "INSERT")
	{
		alert("Una PASSWORD sar indispensabile per accedere alle aree riservate");
		theForm.password.focus();
		return(false);
	}
	if(theForm.password.value != theForm.verificapassword.value && theForm.task.value == "INSERT")
	{
		alert("La PASSWORD non corrisponde");
		theForm.password.value="";
		theForm.verificapassword.value="";
		theForm.password.focus();
		return(false);
	}
	if(theForm.point_of_presence.value.length > 0 && theForm.isAdmin.value!="YES")
	{
		for(cnt=0; cnt < numOfPop; cnt++)
		{
			if(popArray_pc_id[cnt] == theForm.point_of_presence.value)
				return(true);
		}
		alert("P.O.P. non valido");
		theForm.point_of_presence.focus();
		return(false);
	}
	return(true);
}
//==================================================================================


// VERIFY TEMI DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyTemiData(theForm)
{
	if(theForm.td_titolo.value.length == 0)
	{
		alert("Il titolo  indispensabile");
		theForm.td_titolo.focus();
		return(false);
	}

	if(theForm.td_descrizione.value.length == 0)
	{
		alert("Una breve descrizione  indispensabile");
		theForm.td_descrizione.focus();
		return(false);
	}
	
	if(theForm.td_inizio.value.length > 0)
		if(VerifyDateData(theForm.td_inizio, "Data inizio attivit") == false)
			return(false);
			
	if(theForm.td_fine.value.length > 0)
		if(VerifyDateData(theForm.td_fine, "Data fine attivit") == false)
			return(false);
	
	return(true);
}
//==================================================================================



// VERIFY CONTR DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyContrData(theForm)
{
	if(theForm.ct_testo.value.length == 0)
	{
		alert("Nessun testo da memorizzare");
		theForm.ct_testo.focus();
		return(false);
	}

	return(true);
}
//==================================================================================



// VERIFY EXPERT DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyExpertData(theForm)
{
	if(theForm.er_domanda.value.length == 0)
	{
		alert("La domanda  indispensabile");
		theForm.er_domanda.focus();
		return(false);
	}

	if(theForm.er_id_utente.options[theForm.er_id_utente.selectedIndex].value == 0)
	{
		alert("L'utente che ha inserito la domanda   indispensabile");
		theForm.er_id_utente.focus();
		return(false);
	}
	
	if(theForm.er_inserita.value.length > 0)
		if(VerifyDateData(theForm.er_inserita, "Data inserimento domanda") == false)
			return(false);
	
	return(true);
}
//==================================================================================



// VERIFY FAQ DATA
//--------------------------------------------------------------------------------------------------------------------------
/*function VerifyFaqData(theForm)
{
	if(theForm.faq_oggetto.value.length == 0)
	{
		alert("L'oggetto  indispensabile");
		theForm.faq_oggetto.focus();
		return(false);
	}

	if(theForm.faq_domanda.value.length == 0)
	{
		alert("La domanda  indispensabile");
		theForm.faq_domanda.focus();
		return(false);
	}
	
	return(true);
}*/
//==================================================================================



// CLEAR USER DATA
//--------------------------------------------------------------------------------------------------------------------------
function ClearUserData(theForm)
{
	// 0 = task (hidden) e non lo voglio azzerare
	// Ultimi tre sono il textarea con le condizioni e i pulsanti che non voglio azzerare
	for(cnt=1; cnt < theForm.elements.length-3; cnt++)
	{
		if(theForm.elements[cnt].type == "radio" || theForm.elements[cnt].type == "checkbox")
			theForm.elements[cnt].checked = false;
		else
			theForm.elements[cnt].value = "";
	}
	// Questo azzera sia che siano arrivati dati in post sia che siano stati inseriti a mano, se non blocco l'evento reset
	// e i dati sono arrivati in post, verranno ripristinati.
	return(false);
}



// CONVERT SPACES
//--------------------------------------------------------------------------------------------------------------------------
function ConvertSpaces(stringToModify)
{
	var modifiedString;
	modifiedString = "";
	for(cnt=0; cnt < stringToModify.length; cnt++)
	{
		modifiedString += (stringToModify.charAt(cnt) == " " ? "+" : stringToModify.charAt(cnt));
	}
	return(modifiedString);
}
//==================================================================================





// CONVERT COMMAS 
//--------------------------------------------------------------------------------------------------------------------------
function ConvertCommas(stringToModify)
{
	var modifiedString;
	modifiedString = "";
	for(cnt=0; cnt < stringToModify.length; cnt++)
	{
		modifiedString += (stringToModify.charAt(cnt) == "\'" ? "\'\'" : stringToModify.charAt(cnt));
	}
	return(modifiedString);
}
//==================================================================================



// VERIFY DATE DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyDateData(theDateField, nameOfDateField)
{
	if(theDateField.value.length == 0 || theDateField.value.length < 10)
	{
		if(theDateField.value.length == 0)
			alert("Please, specify the " + nameOfDateField + "\n                           (DD/MM/YYYY)");
		else
			alert("Please, verify the format of " + nameOfDateField + "\n                              (DD/MM/YYYY)");
		theDateField.focus();
		return false;
	}
	else
	{
		if(isNaN(theDateField.value.substring(0,2)))
		{
			alert("Please, verify the day format of " + nameOfDateField + "\n                                (DD/MM/YYYY)");
			theDateField.focus();
			return false;
		}
		else
			giorno = parseInt(theDateField.value.substring(0,2),10);
				
		if(isNaN(theDateField.value.substring(3,5)))
		{
			alert("Please, verify the month format of " + nameOfDateField + "\n                                     (DD/MM/YYYY)");
			theDateField.focus();
			return false;
		}
		else
			mese = parseInt(theDateField.value.substring(3,5),10);
				
		if(isNaN(theDateField.value.substring(6,10)))
		{
			alert("Please, verify the year format of " + nameOfDateField + "\n                                     (DD/MM/YYYY)");
			theDateField.focus();
			return false;
		}
		else
			anno = parseInt(theDateField.value.substring(6,10),10);
			
		d = new Date();
		
		if(mese < 1 || mese > 12)
		{
			alert("Please, invalid month of " + nameOfDateField + "\n                                (1-12)");
			theDateField.focus();
			return false;
		}
		switch(mese)
		{
			case 01:	case 03: case 05: case 07: case 08: case 10: case 12:
				if(giorno < 1 || giorno > 31)
				{
					alert("Please, invalid day of " +  nameOfDateField + "\n                                (1-31)");
					theDateField.focus();
					return false;
				}
			break;
			case 04:	case 06: case 09: case 11:
				if(giorno < 1 || giorno > 30)
				{
					alert("Please, invalid day of " + nameOfDateField + "\n                                (1-30)");
					theDateField.focus();
					return false;
				}
			break;
			case 02:
				if(giorno < 1 || giorno > 29)
				{
					alert("Please, invalid day of " + nameOfDateField + "\n                                (1-" + (anno%4 ? "28" : "29") + ")");
					theDateField.focus();
					return false;
				}
				if(giorno == 29 && anno%4)
				{
					alert("Please, invalid day of " + nameOfDateField + "\n                                (1-28)");
					theDateField.focus();
					return false;
				}
			break;
		}
		// Se l'utente usa formati diversi da '-' (DD-MM-YYYY) per il separatore, lo sistemo da qui
		theDateField.value = giorno + "-" + mese + "-" + anno
	}
	return true;
}
//==================================================================================



// VERIFY PARTNER DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyPartnerData(theForm, mode)
{
	if(mode == "")
		return(false);	
	if(theForm.pc_nome_rag_sociale.value.length == 0)
	{
		alert( (mode == "C" ? "NOME e COGNOME sono dati indispensabili"  : "La ragione sociale  indispensabile") + " per la registrazione");
		theForm.pc_nome_rag_sociale.focus();
		return(false);
	}
	if(theForm.pc_email.value.length > 0)
	{
		if(theForm.pc_email.value.indexOf("@") == -1 || theForm.pc_email.value.indexOf(".") == -1)
		{
			alert("L'indirizzo di E-MAIL ha un formato sconosciuto");
			theForm.pc_email.focus();
			return(false);
		}
	}
	return(true);
}
//==================================================================================


// VERIFY PRODUCT DATA
//--------------------------------------------------------------------------------------------------------------------------
function VerifyProductData(theForm)
{
	if(theForm.art_nome.value.length == 0)
	{
		alert("Il NOME del prodotto  un dato indispensabile");
		theForm.art_nome.focus();
		return(false);
	}
	if(theForm.art_prezzo.value.length == 0)
	{
		alert("Il PREZZO del prodotto  un dato indispensabile");
		theForm.art_prezzo.focus();
		return(false);
	}
	else if(isNaN(theForm.art_prezzo.value))
	{
		alert("Formato del PREZZO non riconosciuto");
		theForm.art_prezzo.focus();
		return(false);
	}
	if(theForm.art_tipo.options[theForm.art_tipo.selectedIndex].value == 0)
	{
		alert("Il TIPO di appartenenza del prodotto  un dato indispensabile");
		theForm.art_tipo.focus();
		return(false);
	}

	return(true);
}
//==================================================================================
