
var DmplForm = function (instanceName, container, apikey, width, height) {
    // Properties
    this.InstanceName = instanceName;
    this.Width = width || '100%';
    this.Height = height || '100';
    this.DivContainer = container || null;
    this.CheckBrowser = true;
    this.DisplayErrors = true;
    this.Theme = 'default';
    this.ApiKey = apikey || '';
    //this.ApiUrl = 'http://forms.dmplanner.dk/default.aspx';
    this.ApiUrl = document.location.protocol + '//' + 'forms.dmplanner.dk/default.aspx';
    this.BasePath = document.location.protocol + '//' + document.location.host + document.location.pathname.substring(0, document.location.pathname.lastIndexOf('/') + 1);
    this.CSSFile = null;
    this.ConfigFile = null;
    this.OnError = null;
    this.InitialEmail = null;
    this.InitialSID = null;
}
DmplForm.prototype.Version = '1.0.1';
DmplForm.prototype.Create = function () {
    // find the container div-tag for html-insert.
    if (!this.CheckBrowser || this._IsCompatibleBrowser()) {
        // We must check the elements first using the Id and then the name.
        var oDiv = document.getElementById(this.DivContainer);
        var colElementsByName = document.getElementsByName(this.DivContainer);
        var i = 0;
        while (oDiv || i == 0) {
            if (oDiv && oDiv.tagName.toLowerCase() == 'div')
                break;
            oDiv = colElementsByName[i++];
        }
        if (!oDiv) {
            alert('Error: The DIV with id or name set to "' + this.DivContainer + '" was not found');
            return;
        }
    }
    var args = getArgs();
    if (args.email)
        this.InitialEmail = args.email;
    if (args.sid)
        this.InitialSID = args.sid;


    oDiv.innerHTML = this.CreateHtml();
}
DmplForm.prototype.CreateHtml = function()
{
	var sHtml = '';
	// Check for errors
	if ( !this.InstanceName || this.InstanceName.length == 0 )
	{
		this._ThrowError( 701, 'You must specify an instance name.' ) ;
		return '' ;
	}
	if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
		sHtml += this._GetIFrameHtml();
	return sHtml ;
}
DmplForm.prototype._GetIFrameHtml = function()
{
	var sLink = this.ApiUrl + '?InstanceName=' + encodeURIComponent(this.InstanceName);
	sLink += '&amp;base=' + encodeURIComponent(this.BasePath);
	sLink += '&amp;akey=' + encodeURIComponent(this.ApiKey);
	if (this.CSSFile)
		sLink += '&amp;css=' + encodeURIComponent(this.CSSFile);
	if (this.ConfigFile)
		sLink += '&amp;conf=' + encodeURIComponent(this.ConfigFile);
	if (this.InitialEmail)
	    sLink += '&amp;email=' + encodeURIComponent(this.InitialEmail);
	if (this.InitialSID)
	    sLink += '&amp;sid=' + encodeURIComponent(this.InitialSID);
	
	var html = '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
	return html ;
}
DmplForm.prototype._IsCompatibleBrowser = function()
{
	return DmplForm_IsCompatibleBrowser() ;
}
DmplForm.prototype._ThrowError = function( errorNumber, errorDescription )
{
	this.ErrorNumber		= errorNumber ;
	this.ErrorDescription	= errorDescription ;
	if ( this.DisplayErrors )
	{
		document.write( '<div style="COLOR: #ff0000">' ) ;
		document.write( '[ DmplForm Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
		document.write( '</div>' ) ;
	}
	if ( typeof( this.OnError ) == 'function' )
		this.OnError( this, errorNumber, errorDescription ) ;
}
function DmplForm_IsCompatibleBrowser()
{
	var sAgent = navigator.userAgent.toLowerCase() ;
	// Internet Explorer 5.5+
	if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
	{
		var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
		return ( sBrowserVersion >= 5.5 ) ;
	}
	// Gecko (Opera 9 tries to behave like Gecko at this point).
	if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
		return true ;
	// Opera 9.50+
	if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.5 )
		return true ;
	// Adobe AIR
	// Checked before Safari because AIR have the WebKit rich text editor
	// features from Safari 3.0.4, but the version reported is 420.
	if ( sAgent.indexOf( ' adobeair/' ) != -1 )
		return ( sAgent.match( / adobeair\/(\d+)/ )[1] >= 1 ) ;	// Build must be at least v1
	// Safari 3+
	if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
		return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ;	// Build must be at least 522 (v3)
	return false ;
}
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0, pos);
        var value = pairs[i].substring(pos + 1);
        args[argname] = unescape(value);
    }
    return args;
}
