// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

function ajax_do (url)
{
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http')
	{
			url = base_url + url;
	}

	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;

	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);
}

function ask_winkelwagen(prodvar_id, prod_desc)
{
	document.getElementById('ask_add').style.display = '';
	document.getElementById('ask_prodvar_id').value = prodvar_id;
	document.getElementById('ask_add_product').innerHTML = '<b>' + prod_desc + '</b>';
}

function set_language(selected_language)
{
	if (document.location.href.indexOf("?") > 0)
	{
		if (document.location.href.indexOf("setlang=") > 0)
		{
			pos = document.location.href.indexOf("setlang=");
			oldlang = document.location.href.substring(pos+8,pos+8+3);
			newurl = document.location.href.replace(oldlang, selected_language);
			document.location.replace(newurl);
		}
		else
		{
			document.location.replace(document.location.href + "&setlang=" + selected_language);
		}
	}
	else
	{
		document.location.replace(document.location.href + "?setlang=" + selected_language);
	}
}

function add_winkelwagen(prodvar_id,bestelAantal)
{
	ajax_do('wagen.php?md=add&prodvar_id=' + prodvar_id + '&bestelAantal=' + bestelAantal);
	ajax_do('wagen.php?md=update_wagen_status');
	document.getElementById('ask_add').style.display = 'none';
	document.getElementById('confirm_add').style.display = '';
}

function zoek()
{
	if (document.getElementById('zoek').value.length > 0)
	{
		document.location.replace('?p=producten&zoek=' + document.getElementById('zoek').value);
	}
	else
	{
		alert("Uw zoekopdracht dient minimaal te bestaan uit 2 tekens.");
	}
}

function expand(itemobj)
{
	var currentTime = Math.floor(new Date().getTime()/1000);
	ajax_do("dynmenu.php?mode=getmenu&submenu=" + itemobj.substring(3) + "&tm=" + currentTime);
}

function restore_menu(menuact)
{
	if (menuact.length > 5)
	{
		items = menuact.split(";");
		for (i=0;i<items.length;i++)
		{
			if (items[i] != '')
			{
				document.getElementById(items[i]).style.display = '';
			}
		}
	}
}

function format_coupon(couponinputval, couponinputobj, event)
{

	if (isNumber(couponinputval.substring(0,1)))
	{	
		if (getunicode(event) != 8 && getunicode(event) != 46)
		{
			if (couponinputval.length == 5)
			{
				couponinputval = couponinputval.toString() + "-";
				couponinputobj.value = couponinputval;
			}
			
			if (couponinputval.length == 11)
			{
				couponinputval = couponinputval.toString() + "-";
				couponinputobj.value = couponinputval;
			}
		}
			
		if (couponinputval.charAt(5) != "-" && couponinputval.length > 5)
		{										
			if (couponinputval.substring(0,5).indexOf("-") < 0)
			{
				couponinputval = couponinputval.substring(0,5) + "-" + couponinputval.substring(5);
				couponinputobj.value = couponinputval;
			}		
		}
		
		if (couponinputval.charAt(10) != "-" && couponinputval.length > 11)		
		{		
			if (couponinputval.indexOf("-",7) < 0)
			{
				couponinputval = couponinputval.substring(0,11) + "-" + couponinputval.substring(11);
				couponinputobj.value = couponinputval;
			}		
		}
		
		if (removeSpaces(couponinputval).length > 17)
		{
			alert("U heeft een onjuiste coupon ingevoerd. Controleer uw coupon-code. Bijv. 12345-12345-12345");
		}


	}
}

function getunicode(e)
{
	var unicode=e.keyCode? e.keyCode : e.charCode;
	return unicode;
}


function isNumber(n)
{ 
  return !isNaN(parseFloat(n)) && isFinite(n); 
} 

function removeSpaces(string)
{
	return string.split(' ').join('');
}

