function wait(){

	var widthW = $(window).width();
	var heightW = $('#content').height()+50;
	
	var posCommentiBox = $('#comment').position();
	posCommentiBox = posCommentiBox.top ;

	var posW = (widthW/2)-8;
	var posH = (heightW/2)-8;	

	var waitBox = "<div id='waitBox' style='width:"+widthW+"px;height:" + heightW + "px;top:0;left:0;background:#333333;z-index:999;position:absolute;opacity:0.8;filter:alpha(opacity=80)'><img src='./images/ajax-loader.gif' width='16' height='16' alt='attendi' style='margin-top:"+posCommentiBox+"px;margin-left:"+posW+"px'></div>";
	
	$('#waitDiv').html(waitBox).fadeOut(3000);
}

function Ccolor(id,type) {
	tablecolour = '#F6F6F6'
		if (type == 'in') { 
			id.bgColor = tablecolour;
		}
		else {
			id.bgColor = '#FFFFFF';
		}
}

function ColorBg(id,clr) 
{
	id.bgColor = clr;
}


function validate(form) {
	//alert(form);
	var j = 0;
	msg = "";
	for(var i = 0;i < ele.length;i++) {
		if(isBlank(form, ele[i])) {
			invalid(form, ele[i]);
			msg = msg + msgs[i] +"\n";
			j = j +1;
		}
	}
	if(j == 0) {
		wait()
		document.forms[form].submit();
	}
	else {
		alert(alertMsg + "\n"+ msg);
		return false;
	}
}
function invalid(form, ele) {
	if(!document.layers) {
		document.forms[form].elements[ele].style.borderColor = '#cccccc';
		document.forms[form].elements[ele].style.background = '#FF9900';
		}
	return msg;
}

function isBlank(form,ele) {
	//alert(form);
	str = document.forms[form].elements[ele].value;
	if(str == "") {
		return true;
	}
	else {
		return false;
	}

}


function openPrintable(section, id, lang) 
{
	var newWindow = window.open("./html/printable.html?nid=" + id + "&lang=" + lang,"PrintArtcle","height=500,width=550,toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
}
function openWin(what, lang) 
{
	var newWindow = window.open("./downloads/curriculum.html?p=" + what + "&lang=" + lang,"Curr","height=650,width=600,toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
}
function popUp(page, width, height, lang) 
{
	var newWindow = window.open(page,"GenericPage","height=" + height + ",width=" + width + ",toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
}


function deleteRec (msg,url)
{
	var conferma=null;
	conferma = confirm(msg + '?\n');
	if(conferma == false) 
	{
		return false;
	}
	else
	{
		document.location.href = url;
	}
}

function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function checkAll_byForm(form){

    var elLength = document.forms[form].elements.length;

    for (i=0; i<elLength; i++)
    {
        var type = document.forms[form].elements[i].type;
        if (type=="checkbox"){
            document.forms[form].elements[i].checked = true ;
        }
     }
}

function uncheckAll_byForm(form){

    var elLength = document.forms[form].elements.length;

    for (i=0; i<elLength; i++)
    {
        var type = document.forms[form].elements[i].type;
        if (type=="checkbox"){
            document.forms[form].elements[i].checked = false ;
        }
     }
}

function reverseAll_byForm(form){

    var elLength = document.forms[form].elements.length;

    for (i=0; i<elLength; i++)
    {
        var type = document.forms[form].elements[i].type;
        
        if (type=="checkbox" && document.forms[form].elements[i].checked == true){
            document.forms[form].elements[i].checked = false ;
        }
        else if (type=="checkbox" && document.forms[form].elements[i].checked == false){
            document.forms[form].elements[i].checked = true ;
        }
     }
}

function styleAlert(message,timeout)
{
	$.blockUI({
		theme:false,
		message:message,
		timeout:timeout
	});
}


function checkVAT(form, pi, codfisc)
{
   this.disabled = 'true';

   err = 0;

   if(pi.length <= 11)
   {
   		
   		if(document.forms[form].cf.value.length == 16 && !pi)
   		{

			var conferma=null;
			conferma=confirm ('PARTITA pi mancante.\n\nSei sicuro che questo è un CLIENTE PRpiTO e non necessita di P. pi?\n\n - Premi "Ok" per cliente prpito\n - premi "Annulla" per inserire la P.pi');
			if (conferma==false)
			{
				return (false);
			}
			else
			{
				err = 1;
			}

   		}
   		else
   		{

   		
			if( pi.length != 11)
			{
				err = 1;
			}

			validi = "0123456789";

			for( i = 0; i < 11; i++ )
			{
				if( validi.indexOf( pi.charAt(i) ) == -1 )
				{
					err = 2;
				}
			}
			s = 0;

			for( i = 0; i <= 9; i += 2 )
			{
				s += pi.charCodeAt(i) - '0'.charCodeAt(0);
			}
			for( i = 1; i <= 9; i += 2 )
			{
				c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
				if( c > 9 )
				{
					c = c - 9;
				}
				s += c;
			}
			if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
			{
				err = 3;
			}
		}	}

	if(codfisc.length > 12)
	{

			var validi, i, s, set1, set2, setpari, setdisp;
			if( codfisc == '' )  return '';
			codfisc = codfisc.toUpperCase();
			if( codfisc.length != 16 )
			{
				err = 4;
			}
			validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			for( i = 0; i < 16; i++ ){
				if( validi.indexOf( codfisc.charAt(i) ) == -1 )
				{
					err = 4;
				}
			}
			set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
			s = 0;
			for( i = 1; i <= 13; i += 2 )
				s += setpari.indexOf( set2.charAt( set1.indexOf( codfisc.charAt(i) )));
			for( i = 0; i <= 14; i += 2 )
				s += setdisp.indexOf( set2.charAt( set1.indexOf( codfisc.charAt(i) )));
			if( s%26 != codfisc.charCodeAt(15)-'A'.charCodeAt(0) )
			{
				err = 4;
			}

	}

	if(codfisc.length < 11)
	{
		err = 4;
	}

	if(err == 3)
	{
		document.forms[form].pi.style.borderColor = '#AA0000';
		document.forms[form].pi.style.background = '#FFFFDD';

		alert('--------------- Attenzione ---------------\n\nSi prega di inserire una PARTITA IVA valida');
		return false;
 	}
 	else if(err == 4)
 	{
		document.forms[form].cf.style.borderColor = '#AA0000';
		document.forms[form].cf.style.background = '#FFFFDD';

		alert('--------------- Attenzione ---------------\n\nSi prega di inserire un CODICE FISCALE valido');
		return false;
	}
	

}



function validatePwd(form,namefieldpsw1,namefieldpsw2) {
	var invalid = " "; // Invalid character is a space
	var minLength = 8; // Minimum length
	var pw1 = document.forms[form].elements[namefieldpsw1].value;
	var pw2 = document.forms[form].elements[namefieldpsw2].value;
	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		alert('--------------- Attenzione ---------------\n\nInserisci qui la password di controllo!');
		return false;
	}
	// check for minimum length
	if (document.forms[form].elements[namefieldpsw1].value.length < minLength) {		
		
		document.forms[form].elements[namefieldpsw1].style.borderColor = '#cccccc';
		document.forms[form].elements[namefieldpsw1].style.background = '#FF9900';
		document.forms[form].elements[namefieldpsw2].style.borderColor = '#cccccc';
		document.forms[form].elements[namefieldpsw2].style.background = '#FF9900';
		
		document.forms[form].elements[namefieldpsw1].value = '';
		document.forms[form].elements[namefieldpsw2].value = '';		
		
		alert('--------------- Attenzione ---------------\n\nLa password deve assere almeno' + minLength + ' caratteri!');
		
		return false;
	}
	// check for spaces
	if (document.forms[form].elements[namefieldpsw1].value.indexOf(invalid) > -1) {
			
		document.forms[form].elements[namefieldpsw1].value = '';
		document.forms[form].elements[namefieldpsw2].value = '';	
	
		document.forms[form].elements[namefieldpsw1].style.borderColor = '#cccccc';
		document.forms[form].elements[namefieldpsw1].style.background = '#FF9900';
		document.forms[form].elements[namefieldpsw2].style.borderColor = '#cccccc';
		document.forms[form].elements[namefieldpsw2].style.background = '#FF9900';
		
		alert('--------------- Attenzione ---------------\n\nGli spazi non sono ammessi');
		
		return false;
	}
	else {
		if (pw1 != pw2) {
		
			document.forms[form].elements[namefieldpsw2].value = '';
			document.forms[form].elements[namefieldpsw2].style.borderColor = '#cccccc';
			document.forms[form].elements[namefieldpsw2].style.background = '#FF9900';
			
			alert('--------------- Attenzione ---------------\n\nLe due password NON corrispondono');
			
			return false;
		}
		else {
			//alert('Nice job.');
			return true;
	      	}
	}
}

function checkEmail(form,nameEmailField) {
	var email = document.getElementById(nameEmailField);
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		document.getElementById(nameEmailField).style.borderColor = '#cccccc';
		document.getElementById(nameEmailField).style.background = '#FF9900';	
		
		alert('--------------- ATTENZIONE ---------------\n\nSi prega di inserire un indirizzo e-mail corretto');
		
		return false;		
	}
}


 function checkUrl(form,nameUrlField) {
 	var url = document.forms[form].elements[nameUrlField].value;
	var regexp = /^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i
	if(!regexp.test(url))
	{
		document.forms[form].elements[nameUrlField].style.borderColor = '#cccccc';
		document.forms[form].elements[nameUrlField].style.background = '#FF9900';	
		
		alert('--------------- ATTENZIONE ---------------\n\nL\'indirizzo inserito non è corretto,\n\rverifica di aver inserito anche "http://" prima dell\'url');
	}
}

function checkHttp(form,nameUrlField) {
 
 	var url = document.forms[form].elements[nameUrlField].value;
	var regexp3 = /^(http|ftp)\:\/\//
	if(!regexp3.test(url))
	{
		document.forms[form].elements[nameUrlField].value = 'http://' + document.forms[form].elements[nameUrlField].value;		
	}
}

function editItemStep(step,field) {
	document.location.href='signin.html?step=' + step + '&field=' + field;	
}

function fieldFocus(){

	var querystring = location.search; // restituisce ?id=nome_elemento
	var field = querystring.substring(14); // elimino ?id= e mi rimane nome_elemento	
	
	window.onload = function() {
		document.getElementById(field).focus();
		document.getElementById(field).style.borderColor = '#cccccc';
		document.getElementById(field).style.background = '#FBF3CA';
	}

}



