var creditcardsOldOnload = window.onload;

window.onload = function()
{
    if (typeof creditcardsOldOnload == 'function')
    {
         creditcardsOldOnload();
    }

    window.creditcardsInstance = new creditcards(1);
   	creditcardsInstance.init();
    window.creditcardsInstance2 = new creditcards(2);
   	creditcardsInstance2.init();
}

creditcards = function (formId)
{
	this.formId = formId;
    this.listContainer    = null;
    this.filterForm       = null;
    this.creditTypeSelect = null;
    this.creditPageSelect = null;
}


creditcards.prototype.init = function()
{
    this.listContainer = document.getElementById('creditcardsBlock');
    if (!this.listContainer)
    {
        return;
    }
    this.form = document.getElementById('creditcardsFilterForm' + this.formId);
    if (!this.form)
    {
        return;
    }

    this.creditTypeSelect = document.getElementById('cat' + this.formId);
    if (!this.creditTypeSelect)
    {
        return;
    }

    // attach submit trigger to select onchange
    var script = this;
    this.creditTypeSelect.onchange = function()
    {
        script.submitFormAsBlock();
    }



    this.creditPageSelect = document.getElementById('page' + this.formId);
    if (!this.creditPageSelect)
    {
        return;
    }

    // attach submit trigger to select onchange
    var script = this;
    this.creditPageSelect.onchange = function()
    {
        script.submitFormAsBlock();
    }
	
	var link = document.getElementById('next' + this.formId);
	if(link)
	{
		link.blockUrl = link.href + '&block=cards&random=' + Math.random();
		link.onclick = function()
		{
			var el = this;
			loadXmlHttp(el.blockUrl, script.processFormResponse, script);
			el = null;
			return false;
		}
	}

	var link = document.getElementById('previous' + this.formId);
	if(link)
	{
		link.blockUrl = link.href + '&block=cards&random=' + Math.random();
		link.onclick = function()
		{
			var el = this;
			loadXmlHttp(el.blockUrl, script.processFormResponse, script);
			el = null;
			return false;
		}
	}
}

creditcards.prototype.submitFormAsBlock = function()
{
    Blocks.submitFormAsBlock(this.form, 'cards', this.processFormResponse, this);
    return false; // prevent full page form submit
}

creditcards.prototype.processFormResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.listContainer.innerHTML = xmlhttp.responseText;
   	creditcardsInstance.init();
   	creditcardsInstance2.init();
}

