// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function set_active_brand(pid) {
	ptypes = $('product-types').getElementsBySelector('a');
	ptypes.each(function(s) {
		s.removeClassName('strong');
	});
	$('producttype-'+pid).addClassName('strong')
}

function set_product(brand_id,brand_name,product_id,product_name) {
	if (brand_id && brand_name) {
		t = $('stock_line_type').show();
		t.value = 0;
		b = $('stock_line_brands');
		b.childElements().each(Element.remove);
		b.insert("<option value=\""+brand_id+"\">"+brand_name+"</option>");
		b.show();
	}
	
	if (product_id && product_name) {
		p = $('stock_line_products');
		p.childElements().each(Element.remove);
		p.insert("<option value=\""+product_id+"\">"+product_name+"</option>");
		p.show();
	}
	$('product-searchresult').hide();
	addtocart_icon();
}

function select_all_products(div) {
	searchlist = $(div).getElementsBySelector('input');
	searchlist.invoke('writeAttribute', 'checked', $('select-all').checked == true ? true : false);
}

function addtocart_icon() {
	if ($('stock_line_type').value != '' && $('stock_line_brands').value != '' && $('stock_line_products').value != '' && $('stock_line_total').value > 0) {
		$('addtocart-icon').show();
	} else {
		$('addtocart-icon').hide();
	}
}

/* activeren van regel t.b.v. het scannen van EAN/IMEI nummers pakbonnen */
function activate_line(action,stockorder,id,ean) {
	row = $('line_'+id);
	// productregel oplichten
	row.toggleClassName("active");
	$$('#lines tr').each(function(r) {
		if (id != r.id.replace('line_','')) {
			r.removeClassName("active");
		}
	});
	
	// aantalveld activeren voor geselecteerde productregel
	$$('input.aantal').each(function(b) {
		b_id = b.name.replace('aantal_','');
		if (id != b_id && b.id == 'ts-input') b.writeAttribute("id","aantal_"+b_id);
		else if (id == b_id) b.writeAttribute("id","ts-input");
	});
	
	// class button (de)activeren (rood/grijs) en focus op actieve aantalveld plaatsen
	$$('a.scan').each(function(b) {
		b_id = b.id.replace('scan_','');
		if ($('ts-input') && id == b_id) $('ts-input').focus();

		b.removeClassName(id != b_id ? "button" : "button-inactive");
		b.addClassName(id != b_id ? "button-inactive" : "button");
	});
}

function scan_imei(action,stockorder,id,ean) {
	field = $('ts-input') ? $('ts-input') : $('aantal_'+id);
	$('scan_aantal').value = field.value;
	inactive = $('scan_'+id).readAttribute("class").indexOf('inactive');

	if (inactive == -1) {
		new_url = '/admin/stock/'+action+'/'+stockorder+'?stockorder='+stockorder+'&ean='+ean+'&line='+id+'&scan_aantal='+field.value;
		document.location.href = new_url;
	}

}

function display_in_place_errors(i,r,id){
	if(r){
		var a = r.responseText.evalJSON(true);
	}	else if(i){
		var a = i.responseText.evalJSON(true);
	}
	var v = a[0];
	if(id){
		if($(id).tagName != 'SELECT'){
			$(id).update(a[0]);	
		}
		if($(id).tagName == 'SELECT'){
			for (i=0; i<$(id).options.length; i++) {
				if ($(id).options[i].value == v) {
					$(id).options[i].selected = true;
				}else{
					$(id).options[i].selected = false;
		    }
		  }
		}
	}
	m = a[1].evalJSON(true);
	var str = 'De volgende fouten zijn gevonden:\n';
	m.each(function(e){
		str += '- '+ e[1].replace("^","")+'\n';
	})
	alert(str);
}

function notice_message(msg){
   hide_messages();
   $('notice_messages').update(msg);
   $('notice_messages').show();
   $('notice_messages').highlight({ startcolor: '#ffff99', endcolor: '#ffffff' })
}

function error_message(msg){
   hide_messages();
   $('error_messages').update(msg);
   $('error_messages').show();
   $('notice_messages').highlight({ startcolor: '#FF0000', endcolor: '#ffffff' })
}

function hide_messages(){
   $('error_messages').hide();
   $('notice_messages').hide();
}
