var xmlHttp = false;

function postIForm(form_id, form_action)
{
	f = document.getElementById(form_id);
	if( f )
	{
		var rDiv = document.createElement('div');
		rDiv.id = 'remotingDiv';
		rDiv.innerHTML = '<iframe style="display:none;" name="formtarget"></iframe>';
		document.body.appendChild(rDiv);
		f.action = form_action;
		f.target = 'formtarget';
		f.submit();
	}
	else
	{
		alert('Could not locate form');
	}
}

function addToCard(product,iAmount)
{
	if (iAmount==undefined) { iAmount=1; }
	xmlHttp=GetXmlHttpObject("text")
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url=URL_BASE+"/index.php"
	url=url+"?op=addToCard"
	url=url+"&prod="+product
	url=url+"&count="+iAmount
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=replyAddToCard
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function replyAddToCard()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(xmlHttp.status == 200)
		{
			if (document.getElementById('shopping_card_block')==undefined) {
                window.location.href=URL_BASE+"/index.php?op=viewCard";
			} else {
            	updateShoppingCardBlock();
            }
		}
	}
}
function updateShoppingCardIfExists() {
    if (document.getElementById('shopping_card_block')!=undefined && document.getElementById('shoppingCardForm')==undefined) {
        updateShoppingCardBlock();
	}
}
function updateShoppingCardBlock() {
    new Ajax.Updater('shopping_card_block', '/index.php', {
	  parameters: { op: 'viewCardBlock',sid: Math.random()  }
	});
}
function postAddToCardForm() {
	var oForm = $('product');
    oForm.request({
	  onComplete: function(){ updateShoppingCardIfExists();
        Dialogs.confirm(
			'Het product is toegevoegd aan uw winkelwagen.',
			function(){
				Dialogs.close();
			},
			function(){
				document.location.href='/index.php?op=viewCard';
			}
		);


	  }
	});
	return false;
}
function showImg(sUrl) {
    Dialogs.alert('<img src="'+sUrl+'" />');
}
function debug()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(xmlHttp.status == 200)
		{	alert(xmlHttp.responseText);
		}
		else
		{	alert(xmlHttp.status);
		}
	}
}

function getXML(xmlHttp)
{
	var xmlDoc = null;
	if (document.implementation && document.implementation.createDocument)
	{	xmlDoc = xmlHttp.responseXML;
	}	
	else if (window.ActiveXObject)
	{
		var testandoAppend = document.createElement('xml');
		testandoAppend.setAttribute('innerHTML',xmlHttp.responseText);
		testandoAppend.setAttribute('id','_formjAjaxRetornoXML');
		document.body.appendChild(testandoAppend);
		document.getElementById('_formjAjaxRetornoXML').innerHTML = xmlHttp.responseText;
		xmlDoc = document.getElementById('_formjAjaxRetornoXML');
		document.body.removeChild(document.getElementById('_formjAjaxRetornoXML'));
	}
	return xmlDoc;
}

function GetXmlHttpObject(object_type)
{ 
object_type = (object_type == null || object_type != "text") ? "xml" : "text";
	
	xmlHttp = false;
	
	if (window.XMLHttpRequest){
		// If IE7, Mozilla, Safari, etc: Use native object
		xmlHttp = new XMLHttpRequest()
	}
	else
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
	 		{
	 			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e2){
				alert("Your browser does not support AJAX");
			}
		}
	}
	
	if (xmlHttp.overrideMimeType && object_type == 'text') 
	{
	  xmlHttp.overrideMimeType('text/plain');
	} 
	else if (xmlHttp.overrideMimeType && object_type == 'xml') 
	{
	  xmlHttp.overrideMimeType('text/xml');
	}

	return xmlHttp;
}
function showBanner() {
//	if ($$('.banner') == undefined || $$('.banner')==null) return false
//	var aBanners = $$('.banner');
//	for (i=0;i<aBanners.length;i++) {
//        aBannerItems = aBanners[i].getElementsByClassName('banner_item');
//        iBannerToShow = Math.ceil(Math.random()*aBannerItems.length);
//        aBannerItems[parseInt(iBannerToShow)-1].style.display='block';
//	}
}
var oldActive = '';

function showSearchCat(iCatId) {
	var newActive = document.getElementById('cat_'+iCatId);
	if (oldActive != '') {
		oldActive.style.display='none';
	}
	newActive.style.display='block';
	oldActive = newActive;
	return true;
}
function onLoads() {
    updateShoppingCardIfExists();
    showBanner();
}
window.onload = onLoads;