
window.loaded = false;

Ajax.implementEvent( window, 'load', function(){ window.loaded = true; } );
Ajax.MaxConcurrent = 2;

window.serv = {"prijzen":new Array(),"slots":new Array(),"dev_prijzen":new Array()}
serv = window.serv;

function add_serv_func( show_extra ){

	var extrastabel = Ajax.GetObject( 'extra_options' );
	var extraslink =  Ajax.GetObject( 'extras_link' );
	var extrasimg =  Ajax.GetObject( 'extras_img' );

	if( extraslink ){

		extraslink.onclick = function(){

			if( extrastabel.style.visibility == 'hidden' || extrastabel.style.visibility == 'collapse' ){
				Ajax.StaticHandler.DoShow( extrastabel );
				//clusterlink.innerHTML = '';
				extraslink.innerHTML = "klik hier om extra optie's te verbergen";
				extrasimg.src = 'https://www.argeweb.nl/images/open.gif';
			}else{
				Ajax.StaticHandler.DoHide( extrastabel );
				extraslink.innerHTML = "klik hier om extra optie's toe te voegen";
				extrasimg.src = 'https://www.argeweb.nl/images/dicht.gif';
			}
		}

		extraslink.onclick();

		if( show_extra > 0 )
			extraslink.onclick(); // nog een keer klikken :)

		extrasimg.onclick = extraslink.onclick;

	}

	update_prijs();
}

function update_prijs(){

	var termijn = Ajax.GetValue( Ajax.GetObject( 'termijn' ) );
	var maand = 0;
	var eenmalig = 0;
	var lice_maand = 0;
	var lice_eenmalig = 0;
	var prijs_debug ='';

	// eerst device prijs...

	var device = Ajax.GetValue( Ajax.GetObject( 'dev_combo' ) );

	//alert( device );
	//return;

	var tmp_ar = serv.dev_prijzen[device][termijn];

	for( freq in tmp_ar ){

		for( naam in tmp_ar[freq] ){

			if( naam == 'prijs' && ( tmp_ar[freq][naam] > 0 ) ){

				if( freq == 'maand' ){

					maand += parseFloat( tmp_ar[freq][naam] );

				}else if( freq == 'eenmalig' ){

					eenmalig += parseFloat( tmp_ar[freq][naam] );

				}

				prijs_debug += 'device: ' + device + ", termijn: " + termijn + ", " + freq + " + " + tmp_ar[freq][naam] + '\n';
			}
		}
	}

	// daarna per slot prijs ophalen...

	for( var slot in serv.slots ){

		var obj_slot = Ajax.GetObject( slot );

		if( obj_slot ){

			// bij een checkbox en textfields moet het product via de json array opgehaald worden...

			if( serv.slots[slot].length == 1 ){

				var prod = serv.slots[slot][0];

				if( obj_slot.type == 'checkbox' && !obj_slot.checked ){

					var aantal = 0;

				}else{

					if( obj_slot.type == 'select-one' || obj_slot.type == 'radio' ){

						var aantal = 1;

					}else{

						var aantal = Ajax.GetValue( obj_slot );

						if( !aantal.match( new RegExp( /^\d+$/ ) ) ){

							var aantal = 0;
							obj_slot.value = aantal;

						}

						var limit = Ajax.GetObject( 'limit_' + slot );

						if( limit ){

							var limit = parseInt( Ajax.GetValue( limit ) );

							if( limit != 0 && aantal > limit ){

								aantal = limit;
								obj_slot.value = limit;

							}

						}else{


							var aantal = 1;
						}
					}
				}

			}else{

				var prod = Ajax.GetValue( obj_slot );
				var aantal = 1;

			}

			if( prod != 'geen' && aantal > 0 ){

				var tmp_ar = serv.prijzen[prod][termijn];

				for( type in tmp_ar ){

					var tmp_ar2 = tmp_ar[type];

					for( freq in tmp_ar2 ){

						for( naam in tmp_ar2[freq] ){

							if( naam == 'prijs' && ( tmp_ar2[freq][naam] > 0 ) ){

								if( type == 'stuk' && freq == 'maand' ){

									maand += parseFloat( ( tmp_ar2[freq][naam] * aantal ) );

								}else if( type == 'stuk' && freq == 'eenmalig' ){

									eenmalig += parseFloat( ( tmp_ar2[freq][naam] * aantal ) );

								}else if( type == 'licentie_kosten' && freq == 'maand' ){

									lice_maand += parseFloat( ( tmp_ar2[freq][naam] * aantal ) );

								}else if( type == 'licentie_kosten' && freq == 'eenmalig' ){

									lice_eenmalig += parseFloat( ( tmp_ar2[freq][naam] * aantal ) );

								}

								prijs_debug += 'product: ' + prod + ", termijn: " + termijn + ", " + freq + " + " + tmp_ar2[freq][naam] + '\n';
							}
						}
					}
				}
			}

		}
	}

	prijs_debug += "\n" + "maand: " + maand + "\n" + "eenmalig: " + eenmalig + "\n" + "lice_maand: " + lice_maand + "\n" + "lice_eenmalig: " + lice_eenmalig + "\n";
	//alert( prijs_debug );



	var Request = Ajax.GetAjaxRequest();

	Request.AddPostValue( 'ajax_update_prijs', 'functie' );
	Request.AddPostValue( maand, 'maand' );
	Request.AddPostValue( eenmalig, 'eenmalig' );
	Request.AddPostValue( lice_maand, 'lice_maand' );
	Request.AddPostValue( lice_eenmalig, 'lice_eenmalig' );
	Request.AddPostValue( termijn, 'termijn' );

	Request.SuccessHandler.ReplaceById( null );

	//Request.SuccessHandler.Call( function(){ alert( Request.Requester.responseText ) } );

	Request.Send();

}

function del_dev( id, dev ){

	if( !confirm( "Zeker weten " + dev + " verwijderen?" ) )
		return;

	var form = Ajax.GetObject( 'serv_SC_form' );

	form.action = 'bestellen.php?functie=cart';

	form.actie.value = "del";
	form.sess_id.value = id;

	form.submit();

}

function edit_dev( id ){

	var form = Ajax.GetObject( 'serv_SC_form' );

	form.actie.value = "edit";
	form.sess_id.value = id;

	form.submit();

}

function check_cookie( session_id ){

	var Request = Ajax.GetAjaxRequest();

	Request.ErrorOnString = true;
	Request.AddPostValue( 'check_session', 'functie' );
	Request.AddPostValue( session_id, 'session_id' );

	Request.ErrorHandler.Call( function(){ geen_cookie();} );

	Request.Send();
}

function geen_cookie(){
	alert( "Uw browser ondersteunt geen cookies..." );
}

function reload_sub( form ){

	var Request = Ajax.GetAjaxRequest();
	var baseline = Ajax.GetObject( 'baseline' );
	var sess_id = Ajax.GetObject( 'sess_id' );
	//var device = Ajax.GetObject( 'device' );
	var device = Ajax.GetObject( 'dev_combo' );

	Request.AddPostValue( 'ajax_reload_sub', 'functie' );
	Request.AddPostValue( form.id, 'slot' );
	Request.AddPostValue( form.value, 'prod' );

	if( sess_id )
		Request.AddPostValue( Ajax.GetValue( sess_id ), 'sess_id' );

	if( baseline )
		Request.AddPostValue( Ajax.GetValue( baseline ), 'baseline' );

	if( !sess_id && !baseline && device )
		Request.AddPostValue( Ajax.GetValue( device ), 'device' );


	//Request.SuccessHandler.Call( function(){ alert( Request.Requester.responseText ) } );

	Request.SuccessHandler.JSEvalTags( null );
	Request.SuccessHandler.ReplaceById( null );

	Request.SuccessHandler.Call( function(){ update_prijs(); } );

	Request.Send();

}

function add_klant_func(){

	var reset_form = Ajax.GetObject('show_reset_form');

	Ajax.GetObject('reeds_klant[0]').onchange = function(){

		if( Ajax.GetValue( this ) == null ){
		}else if( Ajax.GetValue( this ) == 0 ){
			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_login') );
			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_new_ww') );
			Ajax.StaticHandler.DoShow( Ajax.GetObject('form_nieuwe_klant') );
		}else if( Ajax.GetValue( this ) == 1 ){
			Ajax.StaticHandler.DoShow( Ajax.GetObject('form_login') );
			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_new_ww') );
			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_nieuwe_klant') );

			if( Ajax.GetObject('ww_kwijt') )
				Ajax.GetObject('ww_kwijt').innerHTML = "wachtwoord vergeten?";

		}
	}

	Ajax.GetObject('reeds_klant[1]').onchange = Ajax.GetObject('reeds_klant[0]').onchange;
	Ajax.GetObject('reeds_klant[0]').onclick = Ajax.GetObject('reeds_klant[0]').onchange;
	Ajax.GetObject('reeds_klant[1]').onclick = Ajax.GetObject('reeds_klant[0]').onchange;

	var reset_tabel = Ajax.GetObject( Ajax.GetObject('form_new_ww') );

	if( Ajax.GetObject('ww_kwijt') ){

		Ajax.GetObject('ww_kwijt').onclick = function(){

			Ajax.GetObject('reeds_klant[1]').checked = true;
			var login_tabel = Ajax.GetObject( 'form_login' );

			if( reset_tabel.style.visibility == 'hidden' || reset_tabel.style.visibility == 'collapse' ){

				Ajax.StaticHandler.DoHide( Ajax.GetObject('form_login') );
				Ajax.StaticHandler.DoHide( Ajax.GetObject('form_nieuwe_klant') );
				Ajax.StaticHandler.DoShow( reset_tabel );

				this.innerHTML = "inloggen";

			}else{

				if( login_tabel.style.visibility == 'hidden' || login_tabel.style.visibility == 'collapse' ){

					Ajax.StaticHandler.DoShow( Ajax.GetObject('form_login') );
					Ajax.StaticHandler.DoHide( Ajax.GetObject('form_nieuwe_klant') );
					Ajax.StaticHandler.DoHide( reset_tabel );

					this.innerHTML = "wachtwoord vergeten?";

				}else{

					Ajax.StaticHandler.DoHide( Ajax.GetObject('form_login') );
					Ajax.StaticHandler.DoHide( Ajax.GetObject('form_nieuwe_klant') );
					Ajax.StaticHandler.DoShow( reset_tabel );

					this.innerHTML = "inloggen";

				}
			}
		}
	}

	Ajax.GetObject('bedrijf[0]').onchange = function(){

		if( Ajax.GetValue( this ) == null ){
		}else if( Ajax.GetValue( this ) == 0 ){

			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_bedrijf') );

			if(Ajax.GetObject('form_geb_datum')){
				Ajax.StaticHandler.DoShow( Ajax.GetObject('form_geb_datum') );
			}

		}else{
			Ajax.StaticHandler.DoShow( Ajax.GetObject('form_bedrijf') );

			if(Ajax.GetObject('form_geb_datum')){
				Ajax.StaticHandler.DoHide( Ajax.GetObject('form_geb_datum') );
			}
		}
	}

	Ajax.GetObject('bedrijf[1]').onchange = Ajax.GetObject('bedrijf[0]').onchange;

	Ajax.GetObject('bedrijf[0]').onclick = Ajax.GetObject('bedrijf[0]').onchange;
	Ajax.GetObject('bedrijf[1]').onclick = Ajax.GetObject('bedrijf[0]').onchange;

	Ajax.GetObject('_postadres[0]').onchange = function(){

		if( Ajax.GetValue( this ) == null ){
		}else if( Ajax.GetValue( this ) == 0 ){
			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_postadres') );
		}else{

			Ajax.StaticHandler.DoShow( Ajax.GetObject('form_postadres') );
		}

		Ajax.GetObject('bezoekadres[land]').onchange();
	}

	Ajax.GetObject('_postadres[1]').onchange = Ajax.GetObject('_postadres[0]').onchange;

	Ajax.GetObject('_postadres[0]').onclick = Ajax.GetObject('_postadres[0]').onchange;
	Ajax.GetObject('_postadres[1]').onclick = Ajax.GetObject('_postadres[0]').onchange;

	Ajax.GetObject('bezoekadres[land]').onchange = function(){

		if( Ajax.GetValue( Ajax.GetObject('bedrijf[0]') ) == 0 ||
			( Ajax.GetValue( Ajax.GetObject('bedrijf[1]') ) != 0
			&& Ajax.GetValue( Ajax.GetObject('bezoekadres[land]') ) == 'NL'  ) ){

			Ajax.StaticHandler.DoHide( Ajax.GetObject('form_btwnummer') );

		}else  // buitenlands bedrijf, dus btw nummer vragen
			Ajax.StaticHandler.DoShow( Ajax.GetObject('form_btwnummer') );

	}

	Ajax.GetObject('postadres[land]').onchange = Ajax.GetObject('bezoekadres[land]').onchange;

	// aan alle input velden een onunfocus plakken voor ajax validatie...

	var area = document.getElementById( 'afrekenen' );
	var fields = area.getElementsByTagName( "input" );

	for( var i=0; i < fields.length; i++ ){

		if( fields[i].type == 'text' ){

			Ajax.GetObject( fields[i] ).onblur = function(){

				check_klant_field( this.id, this.value );

				if( this.id == 'contact[emailadres]' )
					check_email_field( this.id, this.value );

			}
		}
	}


	Ajax.GetObject('reeds_klant[0]').onchange();
	Ajax.GetObject('bedrijf[0]').onchange();
	Ajax.GetObject('_postadres[0]').onchange();
	Ajax.GetObject('bezoekadres[land]').onchange();

	if( Ajax.GetObject('ww_kwijt') && Ajax.GetValue( reset_form ) == 1 )
		Ajax.GetObject('ww_kwijt').onclick();


	// js voor het betaal gedeelte...

	var tmp_radio;
	var tmp_other = null;

	if( Ajax.GetObject('incasso[0]') ){

		tmp_radio = Ajax.GetObject('incasso[0]');

		if( Ajax.GetObject('incasso[1]') )
			tmp_other = Ajax.GetObject('incasso[1]');

	}else{

		tmp_radio = Ajax.GetObject('incasso[1]');
	}

	if( tmp_radio ){

		tmp_radio.onchange = function(){

			if( Ajax.GetValue( this ) == null ){
			}else if( Ajax.GetValue( this ) == 0 ){
				Ajax.StaticHandler.DoHide( Ajax.GetObject('form_bankgegevens') );
			}else{
				Ajax.StaticHandler.DoShow( Ajax.GetObject('form_bankgegevens') );
			}
		}
		tmp_radio.onclick = tmp_radio.onchange;

		if( tmp_other ){
			tmp_other.onchange = tmp_radio.onchange;
			tmp_other.onclick = tmp_radio.onchange;
		}

		tmp_radio.onchange();
	}

}

function which_form( form ){

	// als er met enter wordt gesubmit, wordt de bovenste submit button gebruikt, dat is niet de bedoeling...

	var reeds_klant = Ajax.GetValue( Ajax.GetObject('reeds_klant[0]') );

	if( reeds_klant == 0 ){

		form.do_login.disabled = true;
		form.get_new_ww.disabled = true;
		form.show_reset_form.value = 0;

	}else if( reeds_klant == 1 ){

		var login_tabel = Ajax.GetObject( 'form_login' );

		if( login_tabel.style.visibility == 'hidden' || login_tabel.style.visibility == 'collapse' ){

			form.do_login.disabled = true;
			form.show_reset_form.value = 1;

		}else{

			form.get_new_ww.disabled = true;
			form.show_reset_form.value = 0;

		}
	}
}

function check_klant_field( id, value ){

	check_field( 'klant', id, value );

}

function check_field( dienst, id, value ){

	var Request = Ajax.GetAjaxRequest();

	Request.AddPostValue( 'ajax_check_field', 'functie' );
	Request.AddPostValue( id, 'id' );
	Request.AddPostValue( value, 'waarde' );
	Request.AddPostValue( dienst, 'dienst' );

	Request.SuccessHandler.ReplaceById( null );

	Request.Send();
}

function check_email_field( id, value ){

	var Request = Ajax.GetAjaxRequest();

	Request.AddPostValue( 'ajax_check_field', 'functie' );
	Request.AddPostValue( id, 'id' );
	Request.AddPostValue( value, 'waarde' );
	Request.AddPostValue( 'emailadres', 'dienst' );

	Request.SuccessHandler.ReplaceTBODYById( null );
	//Request.SuccessHandler.Call( function(){ alert( Request.Requester.responseText ) } );

	Request.Send();
}

function update_cart( term ){

	var Request = Ajax.GetAjaxRequest();

	Request.AddPostValue( 'ajax_update_cart', 'functie' );
	Request.AddPostValue( term.value, 'termijn' );

	Request.SuccessHandler.ReplaceTBODYById( null );
	//Request.ErrorHandler.Call( function(){ alert( 'Error!' ) } );
	//Request.SuccessHandler.Call( function(){ alert( Request.Requester.responseText ) } );

	Request.Send();

}

function change_dev( dev ){

	if( !confirm( "Het formulier moet herladen worden waardoor uw instellingen verdwijnen." + "\n\n" + "Zeker weten?" ) )
		return;

	var form = Ajax.GetObject( 'action_form' );

	form.device.value = dev.value;
	form.submit();

}
