var iBusy = 0;

var sPage = document.location.href;
var aSplit= sPage.split('/');
var sUrl = "http://"+aSplit[2];


function valSendOfferte()
{
	var post_string = "deurType="+document.getElementById('p_deur_type').value+
				   "&dagMaatBreedte="+document.getElementById('p_dagmaatbreedte').value+
				   "&dagMaatHoogte="+document.getElementById('p_dagmaathoogte').value+
				   "&bovenRuimte="+document.getElementById('p_bovenruimte').value+
				   "&opmerking="+document.getElementById('p_opmerking').value+
				   "&bedrijfsnaam="+document.getElementById('p_bedrijfsnaam').value+
				   "&contactpersoon="+document.getElementById('p_contactpersoon').value+
				   "&adres="+document.getElementById('p_adres').value+
				   "&postcode="+document.getElementById('p_postcode').value+
				   "&plaats="+document.getElementById('p_plaats').value+
				   "&telefoon="+document.getElementById('p_telefoon').value+
				   "&email="+document.getElementById('p_email').value;


	make_request(sUrl+'/ajax/aOfferte.php', 'returnSendOfferte', 'POST', post_string); 
}

function returnSendOfferte(result)
{
	var aResult = result.split("||-||");

	// Errors
	if (aResult[0]==100)
	{
		var aErrors = aResult[1].split("/-");
		for (i=0;i<aErrors.length;i++)
		{
			var errorElem = document.getElementById(aErrors[i]);
			errorElem.style.backgroundColor='#FFCECE';
			errorElem.style.borderColor='#DF8F8F';
		}

		var aOk = aResult[2].split("/-");
		if (aOk.length>0)
		{
			for (i=0;i<aOk.length;i++)
			{
				var okElem = document.getElementById(aOk[i]);
				okElem.style.backgroundColor='#EEEEEE';
				okElem.style.borderColor='#B5B5B5';
			}
		}
	}

	// succes
	if (aResult[0]==200)
	{
		document.getElementById('popup_h1').innerHTML = "Offerte verzonden";
		document.getElementById('popup_content').innerHTML='<p style="font-size:12px;">De offerte is succesvol verzonden. Er wordt zo spoedig mogelijk contact met u opgenomen.</p>';	
	}

}

function emptySearch(sValue)
{
	if (sValue=="Zoeken")
	{
		document.getElementById('searchValue').value='';
	}
}

function getOfferteValues()
{
	var dagmaatbreedte = document.getElementById('getOfferteValues')
	alert(dagmaatbreedte);
}

function callMeBack()
{
	var sValue = document.getElementById('callMeBackValue').value;
	if (sValue.length>0)
	{
		document.getElementById('callMeBackValue').value='';
		document.getElementById('callMeBackDiv').innerHTML = '<i style="font-size:11px;">Uw telefoonnummer is succesvol verstuurd. We nemen z.s.m. contact met u op</i>';

		var the_post = "number="+sValue;
       		make_request(sUrl+'/ajax/aCallMe.php', 'returnCallMeBack', 'POST', the_post); 
	}

	return false;
}

function returnCallMeBack(result)
{
	//alert(result);
}

function gotoSubCat(cat)
{
	document.location.href='http://'+aSplit[2]+'/producten/f/'+cat;

}

function changeSubCats(mainCat)
{
	if (aSplit[3]=="occasions")
	{
		document.location.href='http://'+aSplit[2]+'/producten/m/'+mainCat;
	}

	if (iBusy==0)
	{	
		iBusy=1;
		var elem = document.getElementById("subcat");
		elem.options.length=0;

		var theForm = "maincat="+mainCat;
       		make_request('http://'+aSplit[2]+'/ajax/aProductLoadSub.php', 'returnChangeSubCats', 'POST', theForm); 	
	}
}

function returnChangeSubCats(result)
{
	iBusy=0;
	var aSubCats = result.split('|%%%|');
	var elem = document.getElementById("subcat");
	var sCurrent = "";

	elem.options[0]=new Option("Kies...", "0", true, false)
	for (i=0;i<aSubCats.length;i++)
	{
		sCurrent = aSubCats[i];
		var aCurrent = sCurrent.split("/-/");
		
		var optionID = eval(i+1);

		elem.options[optionID]=new Option(aCurrent[1], aCurrent[0], true, false)

	}
}

function emptySearchInput()
{
	if (document.getElementById('searchInput').value=="Zoeken")
	{
		document.getElementById('searchInput').value='';
	}
}

function searchSiteTop(sInput)
{
	var elem = document.getElementById('topSearchResult');

	if (sInput.length > 0)
	{
		elem.style.display='block';

		document.getElementById('topSearchResult').innerHTML='<center><br /><img src=\'http://'+aSplit[2]+'/images/loading.gif\'></center>';
		theForm = 'search='+sInput;
        	make_request('http://'+aSplit[2]+'/ajax/aSearch.php', 'returnSearch', 'POST', theForm); 

	} else {
		elem.style.display='none';
	}
}

function closeSearch()
{
	document.getElementById('topSearchResult').style.display='none';
}

function returnSearch(result)
{
	document.getElementById('topSearchResult').innerHTML=result;
}

function searchArticles(sValue,m_id,c_name)
{
	if (iBusy==0)
	{
		theForm = "search="+sValue+"&m_id="+m_id+"&content_name="+c_name;
       		make_request('http://'+aSplit[2]+'/ajax/aArticles.php', 'returnNewsPage', 'POST', theForm); 
	}
}

function gotoNewsPage(iPage,m_id,c_name)
{
	if (iBusy==0)
	{
		document.getElementById('article_items').innerHTML = '<img class="normal" align="absmiddle" src="'+sUrl+'/images/icons/loading.gif" alt="De berichten worden geladen" /> De berichten worden geladen.';
		theForm = "page="+iPage+"&m_id="+m_id+"&content_name="+c_name;
        	make_request('http://'+aSplit[2]+'/ajax/aArticles.php', 'returnNewsPage', 'POST', theForm); 
		iBusy=1;
	}
}

function returnNewsPage(result)
{
	//alert(result);

	var aResult = result.split('|%|%|');
	document.getElementById('article_items').innerHTML = aResult[0];
	document.getElementById('pag_nav').innerHTML = aResult[1];

	iBusy=0;

}

 function create_http_object() 
    { 
        var ActiveXTypes = [ 
            "Microsoft.XMLHTTP", 
            "MSXML2.XMLHTTP.5.0", 
            "MSXML2.XMLHTTP.4.0", 
            "MSXML2.XMLHTTP.3.0", 
            "MSXML2.XMLHTTP" 
        ]; 

        for( var i = 0; i < ActiveXTypes.length; i++ ) 
        { 
            try 
            { 
                return new ActiveXObject( ActiveXTypes[i] ); 
            } 
            catch( e ) 
            { } 
        } 

        try 
        { 
            return new XMLHttpRequest(); 
        } 
        catch( e ) 
        { } 

        return false; 
    } 

    function make_request(url, callback_function, http_method, post_values, return_xml) 
    { 
        http = create_http_object(); 

        if(!http) 
        { 
            alert('Je browser ondersteunt deze feature niet.'); 
            return false; 
        } 


        http.onreadystatechange = function() 
        { 
            if(http.readyState == 4) 
            { 
                if(http.status == 200) 
                { 
                    if(callback_function) 
                    { 
                        if(return_xml) 
                        { 
                            eval(callback_function + '(http.responseXML)'); 
                        } 
                        else 
                        { 
                            eval(callback_function + '(http.responseText)'); 
                        } 
                    } 
                } 
                else 
                { 
                    alert('Error! (' + http.status + ')'); 
                } 
            } 
        } 

        if(!post_values) 
        { 
            post_values = null; 
        } 
        if(!http_method) 
        { 
            http_method = "GET"; 
        } 

        http.open(http_method, url, true); 

        if(http_method == "POST") 
        { 
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        } 

        http.send(post_values); 
    } 




