function submit_form(name){
try {
	f = document.forms[name];
	if(typeof f == 'object')f.submit();
	return false;
} catch(e){ 
	alert(e.description); 
	return false;
	}
}


function submit_cart(type, other_vars){
	f = document.forms['cart'];
	if(!type){ calc_price(1);
	} else {
		switch(type){
			case 'check_amount':
				inp = f.getElementsByTagName('INPUT');
				ok = true;
				for(i=0; i<inp.length; i++){
					if(inp[i].id.substring(0,7) == 'amount_'){
						if(!check_errors_onkeypress(inp[i], 'IS_NUMERIC', true, true))ok=false;
					}
				}
				if(!ok)return false;
				break;
			case 'adddiscount':
				inp = f.getElementsByTagName('INPUT');
				ok = true;
				for(i=0; i<inp.length; i++){
					if(inp[i].name != 'discountcode'){
						if(inp[i].type != 'hidden')inp[i].disabled = 'disabled';
					}
				}
				f.action = '/shop/cart/adddiscount.html';
				break;
			case 'removediscount':
				inp = f.getElementsByTagName('INPUT');
				ok = true;
				for(i=0; i<inp.length; i++){
					if(inp[i].name != 'discountcode'){
						//inp[i].disabled = 'disabled';
					}
				}
				document.getElementById('othervar').value = other_vars;
				//f.discountcode.value = other_vars;
				f.action = '/shop/cart/removediscount.html';
				break;
			case 'recalculate':
				f.action = '/shop/cart/update.html';
				break;
			case 'order':
				f.action = '/shop/order.html';
				break;
			case 'delete_and_recalculate':
				//if(!other_vars)other_vars=false;
				//else {
					inp = f.getElementsByTagName('SELECT');
					for(i=0; i<inp.length; i++){
						//if(inp[i].type == 'text'){
							if(inp[i].name.indexOf(other_vars) > -1){
								inp[i].selectedIndex = 0;
								f.action = '/shop/cart/update.html';
							} else {
								//inp[i].value = 'skip';
								inp[i].disabled = true;
							}
						//}
					}
				
				//}
				break;
			case 'shipping':
				f = document.forms[type];
				break;
		}
	}
	if(typeof f == 'object')f.submit();
	return false;
}


function switchAdvancedElements(classe, hide, label){
	if(!hide)hide=false;
	if ( classe ) {
		obj = document.getElementsByClassName(classe);
		for(i=0; i<obj.length; i++){
			if(label && obj[i].className.indexOf('lab_organisationname') > -1){
				obj[i].innerHTML = label+'*';	
			}
			obj[i].style.display=!hide ? 'block' : 'none';
		}
	}
}

document.getElementsByClassName = function (needle)
{
    var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
    var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');

    do
    {
        e = s[i];

        while (e)
        {
            if (e.nodeType == 1)
            {
                if (e.className && re.test(e.className)) r[l++] = e;

                s[i++] = e.firstChild;
            }

            e = e.nextSibling;
        }
    }
    while (i--);

    return r;
}

check_shipping = function(obj){
	if(!obj)obj=false;
	if(typeof(obj) == 'boolean'){
		// checken of er een waarde geselecteerd is
		ta = document.getElementsByClassName('TAKEAWAY');
		for(i=0; i<ta.length; i++){
			if(ta[i].checked){
				obj = ta[i];	
			}
		}
	}
	/*
	if(typeof(obj) == 'object'){
		hide = obj.className.indexOf('TAKEAWAY') > -1 ? true : false;
		document.getElementById('display_shippingaddress').style.display = !hide ? 'block' : 'none';	
		if(!hide){
			//alert(document.getElementById('btn_shippingaddress').className);
			document.getElementById('btn_shippingaddress').className = 	document.getElementById('btn_shippingaddress').className.replace('disabled','');
			//alert(document.getElementById('btn_shippingaddress').className);
		} else {
			if(document.getElementById('btn_shippingaddress').className.indexOf('disabled') == -1)document.getElementById('btn_shippingaddress').className+= ' disabled';
		}
		document.getElementById('btn_shippingaddress').onclick = function(){
			if(this.className.indexOf('disabled') != -1)return false;
		};
	}
	*/
}

checkRelatedElement = function(_this){
	try {
		c = _this.className+'';
		m = c.match(/rel_([a-z_0-9]+)/gi);
		m = m+'';
		v = c.match(/val_([a-z_0-9]+)/gi);
		v = v+'';
		ma = _this.value == v.substring(4) ? true : false;
		obj = document.getElementById(m.substring(4));
		obj.style.display = !ma ? 'block' : 'none';
		if(obj.id){
			par = obj.parentNode;
			labels = par.getElementsByTagName('LABEL');
			if(labels.length == 0){
				par = obj.parentNode.parentNode;
				labels = par.getElementsByTagName('LABEL');
			}
			br = par.getElementsByTagName('BR');
			for(x=0; x<labels.length; x++){
				f = labels[x].getAttribute('for');
				if(!f){
					f = labels[x].attributes["for"].value;
				}
				if(f == obj.id){
					 labels[x].style.display = !ma ? 'block' : 'none';
					 labels[x].id = 'rel_'+obj.id;
				}				
			}
			if(get_nextsibling(obj).tagName == 'BR'){
				 get_nextsibling(obj).style.display = !ma ? 'block' : 'none';
			}
		}	
	} catch(e){};
}


checkFormRelations = function(form){
	r = document.getElementsByClassName('related');
	for(i=0; i<r.length; i++){
		r[i].onchange = function(){
			checkRelatedElement(this);
		}
		checkRelatedElement(r[i]);
	}
	
}

check_errors_onkeypress = function(obj, type, skip_classname, check_now){
	if(!skip_classname)skip_classname=false;
	if(!check_now)check_now=false;
	//value_is_valid(this.value, this.rel) == false
	if(typeof(obj) == 'object'){
		if(!skip_classname)obj.className += ' fielderror';
		obj.rel = type;
		obj.onkeypress = obj.onkeyup = obj.onkeydown = obj.onblur = function(){
			try {
				if(this.className.indexOf('fielderror') > -1 && value_is_valid(this.value, this.rel)){
					this.className = this.className.replace('fielderror','');
					l = document.getElementById('rel_'+this.id);
					if(typeof l == 'object'){
						l.className = l.className.replace('labelerror','');
					}
				} 
				else if(this.className.indexOf('fielderror') == -1 && value_is_valid(this.value, this.rel) == false){
					this.className += ' fielderror';
					l = document.getElementById('rel_'+this.id);
					if(typeof l == 'object'){
						l.className += ' labelerror';
					}
				}
			} catch(e){}
		}
		// checken voor label
		if(obj.id){
			labels = obj.parentNode.getElementsByTagName('LABEL');
			if(labels.length == 0)labels = obj.parentNode.parentNode.getElementsByTagName('LABEL');
			for(x=0; x<labels.length; x++){
				f = labels[x].getAttribute('for');
				if(!f){
					f = labels[x].attributes["for"].value;
				}
				if(f == obj.id){
					 if(!skip_classname)labels[x].className += ' labelerror';
					 labels[x].id = 'rel_'+obj.id;
				}
				
			}
		}
	}
	if(check_now){
		valid = value_is_valid(obj.value, obj.rel);
		if(!valid)obj.className += ' fielderror';
		return valid;
	}
};


showFormErrors = function(form, fieldstr){
	f = document.forms[form];
	if(typeof(f) == 'object' && fieldstr.length > 0){
		fields = fieldstr.split(',');
		fe = f.getElementsByTagName('*');
		for(i=0; i<fe.length; i++){
			if(fe[i].name){
				for(j=0; j<fields.length; j++){
					parts = fields[j].split('::');
					if(fe[i].name == parts[0]){
						check_errors_onkeypress(fe[i], parts[1]);
					} 
				}
			}
		}
	}
};

select_address = function(){
	a = document.getElementById('address_select');
	if(typeof(a) == 'object'){
		d = document.getElementsByTagName('DIV');
		for(i=0; i<	d.length; i++){
			if(d[i].className.indexOf('c_grey') > -1 && d[i].getElementsByTagName('INPUT')[0].name == 'address'){
				d[i].onmouseover = function(){
					this.style.background='orange';
					this.style.cursor = 'pointer';
				}
				d[i].onmouseout = function(){
					if(!this.getElementsByTagName('INPUT')[0].checked)this.style.background='#ebeaeb';
				}
				d[i].onclick = function(){
					par =  document.getElementById('address_select');
					//d = par.parentNode.getElementsByTagName('DIV');
					for(i=0; i<	d.length; i++){
						if(d[i].className.indexOf('c_grey') > -1){
							if(d[i].getElementsByTagName('INPUT')[0].checked){
								d[i].style.background='#ebeaeb';
								d[i].getElementsByTagName('INPUT')[0].checked = false;
							}
						}
					}
					this.getElementsByTagName('INPUT')[0].checked = true;
					document.forms['address'].check_id.value = 'N';	
				}
			};
		}
		f = document.forms['address'];
		if(typeof(f) == 'object'){
			//alert('object');
			inp = f.getElementsByTagName('INPUT');	
			//alert(inp.length);
			for(i=0; i<inp.length; i++){
				inp[i].onkeypress = inp[i].onfocus = function(){
					this.form.check_id.value = 'Y';	
				}
			/*	inp[i].onfocus = function(){
					alert(this);
					alert(this.form.check_id);
					this*.form.check_id.value = 'Y';	
				}*/
			}
		}
	}
}

