var articleChoice = new Array();
var countElemInsert = 0;
function addArticle(articleId, titolo) {
    //Controlliamo che l'articolo non sia stato gia' inserito
    if (articleChoice[articleId]==1) {
	alert("Articolo già selezionato");
	return;
    } else {
	articleChoice[articleId]=1;       
	var objImg = document.createElement('img');
	objImg.id = "ab_art_img_"+articleId;
	objImg.src = "img/delx.gif";
	objImg.width = '10';
	objImg.height = '10';
	var tableObj = document.getElementById("tableArticoliScelti");
	var tbodyObj = document.getElementById("tbodyArticoliScelti");
	//tableObj.id = "ab_tblnews_"+articleId;
	//newTableBody = parent.document.createElement('tbody');
	var rigo=document.createElement('tr');
	rigo.id = "art_tr_"+articleId;
	colonna1 = document.createElement('td');
	colonna1.innerHTML = "<img src=\"img/delx.gif\" width=\"10\" height=\"10\" onClick=\"delArticle("+articleId+");\">";
	rigo.appendChild(colonna1);       
	var spanObj = document.createElement('span');
	spanObj.id = "ab_spn_"+articleId;
	spanObj.innerHTML = titolo;
	colonna2 = document.createElement('td');
	colonna2.appendChild(spanObj);
	rigo.appendChild(colonna2);
	//ABtableObj.appendChild(rigo);
	
	tbodyObj.appendChild(rigo);
	//tableObj.appendChild(newTableBody);

	var divObj = document.getElementById("divArtScelti");
	var objHidden;
	//Controlliamo che non esista
	if(document.getElementById("ab_arth_"+articleId)) {
	    objHidden = document.getElementById("ab_arth_"+articleId);
	} else {
	    objHidden = document.createElement('input');
	    objHidden.type = "hidden";
	    objHidden.id = "ab_arth_"+articleId;
	    objHidden.name = "ab_arth_"+articleId;
	    divObj.appendChild(objHidden);
	}
	objHidden.value = countElemInsert+"_"+articleId;
	countElemInsert++;
    }
}

function delArticle(articleId) {
    articleChoice[articleId]=0;
    var divObj = document.getElementById("tbodyArticoliScelti");    
    divObj.removeChild(document.getElementById("art_tr_"+articleId));
    document.getElementById("ab_arth_"+articleId).value = "-1";
}

var newsletterCheck = 0;
function updateNumCheck(elem) {
    if (elem.checked) 
	newsletterCheck++;
    else
	newsletterCheck--;
}

function checkInfoNewsletterSend() {
    var strAlert = "";
    if (trim(document.getElementById('title').value) == "")
	strAlert += "Inserire il titolo.\n";
    //Controlliamo se abbiamo selezionato una newsletter...
    if (newsletterCheck<=0)
	strAlert += "Selezionare almeno una newsletter.\n";
    if (strAlert!="") {
        alert(strAlert);
        return false;
    }
    document.forms[0].submit();
    return true;
}

var newsletterCheckIscrizione = 0;
function updateNumCheckIscrizione(elem) {
    if (elem.checked)
        newsletterCheckIscrizione++;
    else
        newsletterCheckIscrizione--;
}
function checkInfoNewsletter(checkN) {
    var strAlert = "";
    if (trim(document.getElementById('email').value) == "")
	strAlert += "Email obbligatoria.\n";        
    else {
	var eml = checkEmail(document.getElementById("email"));
	if(!eml)
	    strAlert += "Formato E-Mail non valido \n";
    }
    if (checkN && newsletterCheckIscrizione<=0)
        strAlert += "Selezionare almeno una newsletter.\n";
    if (strAlert!="") {
	alert(strAlert);
	return false;
    }    
    document.forms[0].submit();
    return true;
}

function checkInfoSendFotoVideo(isFoto) {
    var strAlert = "";
    if(trim(document.getElementById('name').value) == "")
	strAlert += "Nome obbligatorio.\n";
    if(trim(document.getElementById('surname').value) == "")
	strAlert += "Cognome obbligatorio.\n";
    if(trim(document.getElementById('email').value) == "")
	strAlert += "Email obbligatoria.\n";
    else {
        var eml = checkEmail(document.getElementById("email"));
        if(!eml)
            strAlert += "Formato E-Mail non valido \n";
    }
    if(trim(document.getElementById('titolo').value) == "")
	strAlert += "Titolo obbligatorio.\n";
    if(trim(document.getElementById('descrizione').value) == "")
	strAlert += "Descrizione obbligatoria.\n";    
    if(!isFoto) {
	if(trim(document.getElementById('url').value) == "")
	    strAlert += "Url obbligatoria.\n";
	else {
	    url = trim(document.getElementById('url').value);
	    if (url.indexOf("http://www.youtube.com/watch?v=")<0)
		strAlert += "Url deve essere nella forma:http://www.youtube.com/watch?v=ID_VIDEO";
	}	    
    } else {
	if(trim(document.getElementById('img_upl').value) == "")
	    strAlert += "Foto obbligatoria.\n";
    }
    if (strAlert!="") {
        alert(strAlert);
        return false;
    }

    document.forms[0].submit();
    return true;
}


function trim(stringa) {
    return stringa.replace(/^\s+|\s+$/g,"");
}

function checkEmail(email) {
    //var email = document.getElementById(’emailaddress’);
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
	return false;
    }
    return true;
}

