/***************************************
	TRIM SPACES
***************************************/
function trim(str)		{	return str.replace(/^\s*|\s*$/g,"");	}
function trimAll(str)	{	return str.replace(/\s+/g," ");	}
function loader( id )	{
	var imgLoader	= '...controllo...';
	document.getElementById( id ).innerHTML = imgLoader;
}
function checkSteps( myID , myValue , minChar , badWords , spelling )	{

	minChar		= (typeof(minChar) == 'undefined')	? 120 : minChar;
	badWords	= (typeof(badWords) == 'undefined')	? true : badWords;
	spelling	= (typeof(spelling) == 'undefined')	? true : spelling;

	myValue	= trimAll( myValue );
	if (myValue == '')	{	return true;	}
	var validation	= true;
	//	------------------------------------------------------------
	var myChars = true;
	if (minChar > 0)	{
		loader( 'minChar-'+myID );
		myChars	= checkMinChar( myID , myValue , minChar );
		if (!myChars)	{	validation = false;	}
	}
	//	------------------------------------------------------------
	var checkBadWords = true;
	if (badWords)	{
		loader( 'badWords-'+myID );
		var checkBadWords	= noBadWords( myID , myValue );
		if (!checkBadWords)	{	validation = false;	}
	}
	//	------------------------------------------------------------
	var checkSpelling	= true;
	if (spelling && checkBadWords)	{
		loader( 'spelling-'+myID );
		checkSpelling	= spellingService( myID , myValue );
	}
	//	------------------------------------------------------------
	return validation;
}

function checkMinChar( myID , myValue , minChar )	{
	defaultMinChar	= (typeof(minChar) == 'undefined')	? 120 : minChar;
	
	var myMinCharResp		= document.getElementById( 'minChar-'+myID );
	myMinCharResp.innerHTML	= '';
	myMinCharResp.style.display	= 'none';
	
	var strLen	= myValue.length;
	if (strLen == 0)	{	return true;	}
	if (strLen < defaultMinChar)	{
		myMinCharResp.style.display	= 'block';
		myMinCharResp.innerHTML	= 'Hai usato ' + strLen + ' caratteri, scrivi almeno ' + defaultMinChar + ' caratteri';
		return false;
	}
	return true;
}
function noBadWords( myID , myValue )	{
	var noBad	= true;
	var badWordsResp = document.getElementById( myID );
	var myBadWordsResp	= document.getElementById( 'badWords-'+myID);
	myBadWordsResp.innerHTML	= '';
	badWordsResp.className		= 'txtarea2';
	myBadWordsResp.style.display	= 'none';
	$.ajax({
		type	: 'POST',
		url		: '/services/badwords/badwords.php',
		data	: 'q=' + myValue + '&chk=check',
		async	: false,
		success	: function( t ){
			var split = new RegExp("~~");
			var b = t.substring(2).split(split);
			if(b[0] != 'correct') {
				badWordsResp.className		= 'errBadWordsField';
				myBadWordsResp.style.display	= 'block';
				myBadWordsResp.innerHTML	= '<b>Sono state usate parole non consentite!</b>';
				noBad	= false;
			}
		}
	});
	return noBad;
}
function spellingService( myID , myValue )	{
	var spell	= true;
	var spellResp = document.getElementById( 'spelling-' + myID );
	spellResp.innerHTML	= '';
	spellResp.style.display	= 'none';
	$.ajax({
		type	: 'POST',
		url		: '/services/spellcheck/spell.php',
		data	: 'q=' + myValue + '',
		async	: false,
		success	: function( t ){
			var split = new RegExp("~~");
			var b = t.substring(2).split(split);
			if(b[0] != 'correct') {
				spellResp.style.display	= 'block';
				//spellResp.style.color	= 'red';
				spellResp.innerHTML	= '' + b[1];
				spell	= false;
			}
		}
	});
	return spell;
}
function saveThisDraft( formID )	{
	var myForm	= document.forms[formID];
	//console.log( myForm );
	myForm.saveDraft.value	= '1';
	//formObj.submit();
}
function prevThisStep( formID )	{
	var myForm	= document.forms[formID];
	//console.log( myForm );
	myForm.prevStep.value	= '1';
	//formObj.submit();
}
function beforeSaveStepIntro()	{
	var myTitleID	= 'titolo';
	var myTitleEl	= document.getElementById( myTitleID );
	var checkTitle	= checkSteps( myTitleID , myTitleEl.value , false , true , false );
	//	------------------------------------------------------------
	var myIntroID	= 'introduzione';
	var myIntroEl	= document.getElementById( myIntroID );
	var checkIntroduzione	= checkSteps( myIntroID , myIntroEl.value , 30 , true , false );
	//	------------------------------------------------------------
	
	if (!checkTitle || !checkIntroduzione)	{
		alert('Correggi gli errori prima di proseguire!');
		return false;
	}
	document.step1.nextStep.value	= '1';
	document.step1.submit();
}
function beforeSaveSteps()	{
	var mySteps	= document.getElementsByTagName( "textarea" );
	var numSteps	= mySteps.length;
	for (var i=0;i<numSteps;i++)	{
		thisID		= mySteps[i].id;
		thisValue	= mySteps[i].value;
		if (thisID.indexOf( 'step-' ) != -1)	{
			checkThis	= checkSteps( thisID , thisValue , 120 , true , false );
			if (!checkThis)	{
				alert( 'Correggi gli errori prima di proseguire!' );
				return false;
			}
		}
	}
	document.step2.nextStep.value	= '1';
	document.step2.submit();
}