function IrBlog(iname, terms, padid, baseurl)
{
    this.iname = iname;
    this.terms = terms;
    this.pad = document.getElementById( padid );
    this.baseurl = baseurl;
    this.disclaimerPad = null;
    this.comment = null;
    this.request = null;
}

IrBlog.prototype.showOpen = function(yesno)
{
    var buttons = document.getElementsByTagName( "SPAN" );
    var dispvalue = "";

    if( yesno ) {
	dispvalue = "";
    } else {
	dispvalue = "none";
    }

    for( var j = 0; j < buttons.length; j ++ ) {
	var b = buttons[ j ];
	if( b.getAttribute ) {
	    if( b.getAttribute( "pop" ) == "1" ) {
		b.style.display = dispvalue;
	    }
	}
    }
}

IrBlog.prototype.onShowPad = function()
{
    this.pad.style.display = "block";
    this.showOpen( false );
    document.location.href = "#comment";
}

IrBlog.prototype.onHidePad = function()
{
    this.showOpen( true );
    this.pad.style.display = "none";
}

IrBlog.prototype.hideDisclaimer = function()
{
    if( this.disclaimerPad != null ) {
	document.body.removeChild( this.disclaimerPad );
	this.disclaimerPad = null;
    }
}

IrBlog.prototype.showDisclaimer = function()
{
    this.disclaimerPad = document.createElement( "DIV" );
    this.disclaimerPad.style.display = "block";
    this.disclaimerPad.style.position = "absolute";
    this.disclaimerPad.style.background = "#ffffff";

    var xy = getPageCoords( this.pad );

    this.disclaimerPad.style.left = xy.x + "px";
    this.disclaimerPad.style.top = xy.y + "px";
    this.disclaimerPad.style.border = "solid 1px #bfbebe";
    this.disclaimerPad.style.padding = "8px 8px 0px 8px";
    this.disclaimerPad.style.width = (this.pad.offsetWidth - 7) + "px";
    this.disclaimerPad.style.height = this.pad.offsetHeight + "px";

    this.disclaimerPad.innerHTML = "<p align=\"left\">" + this.terms[ "confirmnotice" ] + "</p><center><textarea style=\"width: 475px; height: 80px; margin: 12px 0px 12px 0px;\" readonly=\"yes\" rows=\"3\">" + this.terms[ "disclaimer" ] + "</textarea><br /><input type=\"button\" onclick=\"" + this.iname + ".onSend()\" value=\"" + this.terms[ "yes" ] + "\" /><input type=\"button\" onclick=\"" + this.iname + ".onAbort()\" value=\"" + this.terms[ "no" ] + "\" /></center>";

    document.body.appendChild( this.disclaimerPad );
}

IrBlog.prototype.onAbort = function()
{
    this.hideDisclaimer();
    this.comment = null;
}

IrBlog.prototype.onRequestStatus = function()
{
    if( (this.request.readyState == 4) &&
	(this.request.status==200 || window.location.href.indexOf("http")==-1))
    {
	this.hideDisclaimer();
	this.onHidePad();
	document.location = this.baseurl + "(thanks)";
    }
}

IrBlog.prototype.onSend = function()
{
    if( this.comment != null ) {
	// waiter
	this.disclaimerPad.innerHTML = "";
	this.disclaimerPad.style.background = "#ffffff url(/layout/wait.gif) no-repeat center center";

	var postVar = "v=" + this.comment.toJSONString();

	if (window.XMLHttpRequest)
	    this.request = new XMLHttpRequest();

	else if (window.ActiveXObject)
	{
	    try {
		this.request = new ActiveXObject("Msxml2.XMLHTTP")
	    } catch (e) {
		try{
		    this.request = new ActiveXObject("Microsoft.XMLHTTP")
		} catch (e) {
		    return false;
		}
	    }
	}
	else {
	    return false
	}

	this.request.onreadystatechange = function() { irbg.onRequestStatus(); }
	this.request.open( "POST", this.baseurl + "/blogpost.xml" );
	this.request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	this.request.send( postVar );
    }
}

IrBlog.prototype.onSubmit = function(frm)
{
    var isok = true;

    if( frm[ "cauthor" ].value == "" ) {
	frm[ "cauthor" ].style.background = "#f00000";
	isok = false;
    } else {
	frm[ "cauthor" ].style.background = "";
    }

    if( frm[ "csubject" ].value == "" ) {
	frm[ "csubject" ].style.background = "#f00000";
	isok = false;
    } else {
	frm[ "csubject" ].style.background = "";
    }

    if( frm[ "ccomment" ].value == "" ) {
	frm[ "ccomment" ].style.background = "#f00000";
	isok = false;
    } else {
	frm[ "ccomment" ].style.background = "";
    }

    if( frm[ "cemail" ].value == "" ) {
	frm[ "cemail" ].style.background = "#f00000";
	isok = false;
    } else {
	var mask = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/;
	if( !mask.test( frm[ "cemail" ].value ) ) {
	    frm[ "cemail" ].style.background = "#f00000";
	    isok = false;
	} else {
	    frm[ "cemail" ].style.background = "";
	}
    }

    if( !isok ) return false;

    // save the comment
    this.comment = {
	author : frm[ "cauthor" ].value,
	email : frm[ "cemail" ].value,
	website : frm[ "csite" ].value,
	subject : frm[ "csubject" ].value,
	comment : frm[ "ccomment" ].value
    };

    this.showDisclaimer();

    return false;
}
