function blockProperty(p, v) {
	this.property = p;
	this.value = v;
}
function blockProperties() {
	this.propertieslist = new Array();
	this.add = add;
	function add(p, v) {
		this.propertieslist[this.propertieslist.length] = new blockProperty(p, v);
	}
	this.get = get;
	function get(p) {
		for (i=0; i<this.propertieslist.length; i++) {
			if (this.propertieslist[i].property == p) return this.propertieslist[i].value;
		}
	}
	this.set = set;
	function set(p, v) {
		for (i=0; i<this.propertieslist.length; i++) {
			if (this.propertieslist[i].property == p) {
				return this.propertieslist[i].value = v;
			}
		}
	}
}

function ajaxViewArt(divname) {

	// Public methods
	this.setJSON = setJSON; // setJSON(json)
	this.displayArticle = displayArticle; // displayArticle()
	this.AjaxCall = AjaxCall; // AjaxCall(fCalled, dataToSend, handler)
	this.getHTTPReq_readyState = getHTTPReq_readyState; // getHTTPReq_readyState()
	this.setProperty = setProperty; // setProperty(p, v)
	this.setAjaxURL = setAjaxURL; // setAjaxURL(url)

	// Private data
	this.div = divname;
	this.jsonStr = "";
	this.jsonData;
	this.AjaxUrl = "_stuff/Ajaxpage.asp";
	
	// Per la visualizzazione
	this.props = new blockProperties();
	this.props.add('ArticleHeader', '');
	this.props.add('ArticleFooter', '');
	this.props.add('DateHeader', '');
	this.props.add('DateFooter', '');
	this.props.add('TitleHeader', '');
	this.props.add('TitleFooter', '');
	this.props.add('SubtitleHeader', '');
	this.props.add('SubtitleFooter', '');
	this.props.add('ContentHeader', '');
	this.props.add('ContentFooter', '');
	
	function setAjaxURL(url) {
		this.AjaxUrl = url;
	}
	
	function displayArticle() {
		htmlRet = "";

		// Recupera i dati da JSON
		try {
		    this.jsonData = YAHOO.lang.JSON.parse(this.jsonStr);
		}
		catch (e) {
		    alert("Invalid JSON data !");
		}

		try {
		    
		    htmlRet += this.getProperty('ArticleHeader');
			//htmlRet += "<br>authOK=" + this.jsonData.authOK + "<br>";
		    htmlRet += this.getProperty('ArticleHeader');
		    htmlRet += this.getProperty('TitleHeader') + this.jsonData.records[0].col[2].value + this.getProperty('TitleFooter');
		    htmlRet += this.getProperty('SubtitleHeader') + this.jsonData.records[0].col[3].value + this.getProperty('SubtitleFooter');
		    htmlRet += this.getProperty('DateHeader') + this.jsonData.records[0].col[1].value + this.getProperty('DateFooter');
		    htmlRet += this.getProperty('ContentHeader') + this.jsonData.records[0].col[4].value + this.getProperty('ContentFooter');
		    htmlRet += this.getProperty('ArticleFooter');
		    
		    // vedi se e' stato chiesto il display completo dell'articolo
		    if (this.jsonData.records[0].col.length > 5) if (this.jsonData.records[0].col[5].value == 'A') { // A=abstract, C=content (cioè autorizzato)
			    htmlRet += "<p class=info><b>Utente non abbonato!</b> Per poter leggere quest'articolo e' necessario effettuare la sottoscrizione.";
			    htmlRet += " Per abbonarti compila il form presente a <a href=\"index.asp?P=106\">questa pagina</a>.";
			    htmlRet += " Se sei gia' abbonato, effettua il login inserendo username e password.</p>";
			    htmlRet += '<form action="index.asp?P=114&CH=' + 
			    	this.jsonData.records[0].col[6].value +
			    	'&AR=' + this.jsonData.records[0].col[0].value +
			    	'" method="post">';
			    htmlRet += '<table width=300 align=right padding=4>'
			    htmlRet += '<tr><td>Username:</td>';
			    htmlRet += '<td><input type="text" name="UserName"></td></tr>';
			    htmlRet += '<tr><td>Password:</td>';
			    htmlRet += '<td><input type="password" name="PWD"></td></tr>';
			    htmlRet += '<tr><td colspan=2 align=right><br><input type="submit" value="entra"> ';
			    htmlRet += "<input type=\"button\" onclick=\"javascript:location.assign('index.asp?P=116&Lmode=R');\" value=\"Recupero password...\"/>";
			    htmlRet += '</td></tr></table>';
			    htmlRet += '</form>';
			    //htmlRet += "<a href=\"index.asp?P=116&Lmode=R\">Recupero password...</a>.";
		    }

			// Disegna la tabella
			document.getElementById(this.div).innerHTML = htmlRet;
		}
		catch (e) {
		    alert("Invalid access to data !");
		}
	}
	
	this.getProperty = getProperty;
	function getProperty(p) {
		return this.props.get(p);
	}
	
	function setProperty(p, v) {
		return this.props.set(p, v);
	}
	
	this.aVAhttpreq;
	function AjaxCall(fCalled, dataToSend, handler) {
		try {
			if (fCalled != '') {
				if (window.XMLHttpRequest) {
					this.aVAhttpreq = new XMLHttpRequest();
					this.aVAhttpreq.open("GET",this.AjaxUrl+"?MODE="+fCalled+dataToSend,true);
					this.aVAhttpreq.send(null);
					this.aVAhttpreq.onreadystatechange = handler;
				} else if (window.ActiveXObject) {
					this.aVAhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
					if (this.aVAhttpreq) {
						this.aVAhttpreq.open("GET", this.AjaxUrl+"?MODE="+fCalled+dataToSend,true);
						this.aVAhttpreq.send();
						this.aVAhttpreq.onreadystatechange = handler;
					}
				}
			}
		} catch (e) {
			alert('Exception su aVAAjaxCall()');
		}
	}
	
	function getHTTPReq_readyState() {
		return this.aVAhttpreq.readyState;
	}
	
	this.getResponseArray = getResponseArray;
	function getResponseArray() {
		var strRes;
		strRes=this.aVAhttpreq.responseText;
		strRes=strRes.replace("<complete>","");
		strRes=strRes.replace("<script>","");
		strRes=strRes.replace("</script>","");
		strRes=strRes.replace("</complete>","");
		return strRes;
	}

	this.setDIV = setDIV;
	function setDIV(divname) {
		this.div = divname;
	}
	
	function setJSON(json) {
		var myData = {
			header : [
				{ label: "Data" },
				{ label: "Titolo" },
				{ label: "Sottotitolo" },
				{ label: "Abstaract" }
			],
			records : [
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] },
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] },
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] }
		    ]
		};
		if (json == '') this.jsonStr = YAHOO.lang.JSON.stringify(myData);
		else this.jsonStr = json;
		//alert(this.jsonStr);
	}
	
}

