var preloadFlag = false;

function preloadImages(img) {
	var d = document;
	if (d.images) {
		if (!d.p) d.p = new Array();
		var i,j = d.p.length;
		for (i=0; i < img.length; i++)
		if (img[i].indexOf("#") !=0) { 
			d.p[j] = new Image;
			d.p[j++].src = img[i];
		}
	}
}

function changeImageState(img, state) {
	if (document.images && (preloadFlag == true)) {
		window.status = menuroot + img + state +'.gif';
		document.images[img].src = menuroot + img + state +'.gif';
	}
}

function Init()	{
	a = Init.arguments;
	var im = new Array();
	for (i=0; i < a.length; i++){
		im.push(menuroot+a[i]+'.gif');
		im.push(menuroot+a[i]+'over.gif');
		im.push(menuroot+a[i]+'down.gif');
	}
	preloadImages(im);
	preloadFlag = true;
}


function Redirect(pg) {
	if (typeof(base) == "string" && pg.indexOf("http:")<0) pg = base+pg;
	if (!getCookie("sid")) pg += (pg.indexOf("?")>=1?"&":"?")+"sid="+sidz;
	window.location.href = pg;
}

function getCookie(val) {
	var c = document.cookie.split("; ");
	for (var i=0; i < c.length; i++) {
		var pair = c[i].split("=");
		if (val == pair[0]) return unescape(pair[1]);
	}
	return null;
}

function go(URL){
	window.location.href = URL;
}

function gonw(URL, width, height){
	params = (typeof(width) != "undefined" && typeof(height) != "undefined") ? ",width="+width+",height="+height : "";
	nw = window.open(URL, "", params+" ,resizable=yes, scrollbars=yes");
	return false;
}

function isIE() {
	return (document.all && 'Mi'==navigator.appName.substring(0,2));
}

function addFavorite(address, name, errmsg) {
	if (isIE()) window.external.AddFavorite(address, name);
	else alert(errmsg);
	return false;
}

function setAsHomePage(page, url, errmsg) {
	if (isIE()) {
		page.style.behavior='url(#default#homepage)';
		page.setHomePage(url);
	}
	else alert(errmsg);
	return false;
}

function changePreview(id,src){
	document.getElementById(id).src=src;
}

function speurtochtCart(speurtocht_id, parent_type, parent_id, cart_action){
	
	$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/speurtochten/cart/',
   			data: 'speurtocht_id='+speurtocht_id+'&parent_type='+parent_type+'&parent_id='+parent_id+'&cart_action='+cart_action,
   			success: function(msg){

						$('#speurtocht_cart').html(msg) ;	
				}
 		});
}

function shoppingCart(product_id, cart_action, order_view){
	
	$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/webshop/cart/',
   			data: 'product_id='+product_id+'&product_cnt='+$('input#shc_aantal').val()+'&cart_action='+cart_action+'&order_view='+order_view,
   			success: function(msg){

					$('#shopping_cart').html(msg) ;	
					
				}
 		});
}

function shoppingCartChangeCnt(product_id, cart_action, order_view){
	
	$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/webshop/cart/',
   			data: 'product_id='+product_id+'&cart_action='+cart_action+'&order_view='+order_view,
   			success: function(msg){

					$('#shopping_cart').html(msg) ;	
					
				}
 		});
}

function AantalChangeCnt(cnt_action){
	
	var $cnt = parseInt($('input#shc_aantal').val());
	
	if($cnt=='')$cnt = 1;
	
	if(cnt_action=='inc'){
		
		$cnt = $cnt+1;
		
	} else{
		if($cnt!=1){ $cnt = $cnt-1; }
		else $cnt = 1;
	}
	
	$('input#shc_aantal').val($cnt);
		
}

function changeStateCity(country){
		
		getStateField(country, '');
		getCityField(country, '', '')
		
}
	
	function getStateField(country, sel_state){
		$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/address/state/',
   			data: 'country='+country+'&sel_state='+sel_state,
   			success: function(msg){

					$('#state_sel').html(msg) ;	
					
				}
 		});
	}
	
	function getCityField(country, state, sel_city){
		$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/address/city/',
   			data: 'country='+country+'&state='+state+'&sel_city='+sel_city,
   			success: function(msg){

					$('#city_sel').html(msg) ;	
					
				}
 		});
	}
	
	function getUSStateField(sel_state){
		$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/address/usstate/',
   			data: '&sel_state='+sel_state,
   			success: function(msg){

					$('#state_sel').html(msg) ;	
					
				}
 		});
	}
	
	function getUSCityField(state, sel_city){
		$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/address/uscity/',
   			data: '&state='+state+'&sel_city='+sel_city,
   			success: function(msg){

					$('#city_sel').html(msg) ;	
					
				}
 		});
	}
	
function clearMasseurHistory(){
	
	$.ajax({ 	type: 'POST',
   			url: HTTP_ROOT+'main/masseur-history/clear/',
   			data: 'action=clear',
   			success: function(msg){

					$('#div_MasseurHistory').html('') ;	
					
				}
 		});
}

function setupAjaxForm(id_form, id_error, success_handler) {
	$('#' + id_form).ajaxForm({
		dataType: 'json',
		timeout: 15000,
		error: function() {
			$('#' + id_error).html('An error occured while processing the request');
		},
		beforeSubmit: function () {
			//$("#container-submit-button").hide();
			//$("#container-loading-image").show();
			//$('#' + id_form + ' label').css('color', '');
			//$('#' + id_error).html('');
		},
		success: function(respond) {
			//$("#container-submit-button").show();
			//$("#container-loading-image").hide();
			if (respond.success == 1) {
				if (success_handler.length != 0) {
					var func = success_handler + '(respond);'
					eval(func);
				}
			} else {
				processErrors(id_form, id_error, respond);
			}
		}
	});
}

function processErrors(id_form, id_error, respond) {
	var error_str = '';
	$.each(respond.errors, function() {
		
			error_str = error_str + this + "\n";
	
	});
	
	if(error_str!=''){ alert(error_str); }
	
	//$.each(respond.eFields, function(){
		//$('#' + id_form + ' label[for=' + this + ']').css('color', 'red');
	//})
}
