// funcoes gerais (!)
function html_to_element(content){
	
	
	//find first tag
	content = content.replace(/^(\s+|\t+|\r+|\n+)+/g,"");
	
	var container='div';
	if(content.substring(0,3)=="<li"){
		container='ul';
	} else if(content.substring(0,3)=="<td"){
		container='tr';
	} else if(content.substring(0,3)=="<tr"){
		container='tbody';
	} else if(content.substring(0,6)=="<tbody"){
		container='table';
	}
	
	var new_el = new Element(container, {'html':content});

	var new_el_childs = new_el.getChildren();
	
	if(new_el_childs.length == 1){
		
		return new_el_childs[0];
		
	} else {
		
		return new_el;
		
	}
	
}

// pretty inject
function soft_inject(id, parent_el, where){
	
	var el = $(id);
	
	el.setStyle('opacity', 0);
	
	var fx = new Fx.Morph(el, {duration:550, wait:false});
	
	fx.set('opacity', 0);
	fx.set('visibility', 'visible');
	
	el.inject(parent_el, where);
	
	fx.start({
		"background-color": "#ffffcc",
		"opacity": 1
	}).chain(function(){
		fx.start({
			"background-color": "#fff"
		}).chain(function(){
			el.setStyle('background-color', "");
		})
	});
	
}

function add_to_cart(id){

	qt = $('line_qt_' + id);
	
	var number = parseInt(qt.get('value'));
	
	if (qt.get('value') != number || number <= 0) {
			
		alert("Quantidade não suportada.");
		qt.focus();
		
	} else {
		
		if (qt.get('value') == "") {

			alert("Tem que estipular uma quantidade");
			qt.focus();

		} else {
			
			$('items_on_cart_box_' + id).setStyle("display", "none");
			$("cart_stuff_"+id).setStyle("display", "none");
			$("line_loader_"+id).setStyle("display", "");
			var quantidade = qt.get('value');
			$("line_qt_"+id).set("value", "");

			new Request.JSON({
				secure: false,
				url: 'ajaxed/carrinho/add_to_cart.php',
				data: { 
						id: id,
						qt: quantidade
					  }
			}).send();

		}
		
	}
	
}

function update_cart(id){
	
	qt = $('cart_line_qt_' + id);
	
	var number = parseInt(qt.get('value'));
	
	if (qt.get('value') != number || number <= 0) {
			
		alert('Quantidade não suportada.');
		qt.focus();
		
	} else {
		
		$("cart_line_qt_" + id).setStyle("display", "none");
		$("each_cart_line_loader_" + id).setStyle("display", "");

		new Request.JSON({
			secure: false,
			url: '../ajaxed/carrinho/update_cart.php',
			data: { 
					id: id,
					qt: qt.get('value')
				  }
		}).send();
		
	}
	
}

function give_desc(id){
	
	desconto = $('line_desconto_' + id);
	
	var number = parseInt(desconto.get('value'));
	
	if (desconto.get('value') != number || number < 0 || number > 100) {
			
		alert('Desconto não suportado.');
		desconto.set('value', '0');
		desconto.focus();
		
	} else {
		
		$("line_desconto_" + id).setStyle("display", "none");
		$("each_desconto_loader_" + id).setStyle("display", "");

		new Request.JSON({
			secure: false,
			url: '../ajaxed/carrinho/desconto_item.php',
			data: { 
					id: id,
					desconto: desconto.get('value')
				  }
		}).send();
		
	}
	
}

function delete_cart_line(id){
	
	if (confirm("Têm a certeza que quer eliminar este produto?")) {
		
		$("cart_each_line_" + id).destroy();
		$("on_delete_loader_" + id).setStyle("display", "");
		
		new Request.JSON({
			secure: false,
			url: '../ajaxed/carrinho/delete_cart_line.php?id=' + id
		}).send();
		
	};
	
}

function save_obs(){
	
	$('carrinho_description').setStyle('display', 'none');
	$('loader_cart_description').setStyle('display', '');
	
	new Request.JSON({
		secure: false,
		url: '../ajaxed/carrinho/description.php',
		data: {
			
			desc: $('carrinho_description').get('value')
			
		}
	}).send();
	
}

function save_client_name(){

	$('client-name-error').setStyle('display', 'none');

	new Request.JSON({
		secure: false,
		url: '../ajaxed/carrinho/client_name.php',
		data: {
			
			name: $('client_name').get('value')
			
		}
	}).send();
	
}

function save_client_company(){
	
	$('client-company-error').setStyle('display', 'none');
	
	new Request.JSON({
		secure: false,
		url: '../ajaxed/carrinho/client_company.php',
		data: {
			
			name: $('client_empresa').get('value')
			
		}
	}).send();
	
}

function check_comercial_fields(){
	
	if ($('client_name').get('value') == '' || $('client_empresa').get('value') == '') {
		
		if ($('client_name').get('value') == '') {
			
			$('client-name-error').setStyle('display', '');
			
		};
		
		if ($('client_empresa').get('value') == '') {
			
			$('client-company-error').setStyle('display', '');
			
		};
		
	} else {

		window.location = 'compra';
		
	}
	
}

function need_reason(){
	
	if($('razao_textarea').get('value') == ''){
		
		$('need_reason').setStyle('display', '');
		return false;
		
	} else {
		
		$('second_activation_form').submit();
		return true;
		
	}
	
}

function insere_artigo(){
	
	cod = $('artigo_codigo');
	qt = $('artigo_quantidade');
	desconto = $('artigo_desconto');
	
	check = false;
	
	var number = parseInt(qt.get('value'));
	
	if (qt.get('value') != number || number <= 0) {
		
		alert('Quantidade não suportada.');
		qt.set('value', '');
		qt.focus();
		
	} else {
		
		if (cod.get('value') == '') {

			$('art_cod_error').set('text', 'Tem de preencher um código.');

		} else {

			$('art_cod_error').set('text', '');

		}

		if (qt.get('value') == '') {

			$('art_quant_error').set('text', 'Tem de especificar uma quantidade.');

		} else {

			$('art_quant_error').set('text', '');

		}

		if (qt.get('value') != '' && cod.get('value') != '') check = true;

		if (check == true) {

			new Request.JSON({
				secure: false,
				url: 'ajaxed/carrinho/insert.php',
				data: {

					cod: cod.get('value'),
					quant: qt.get('value'),
					desc: desconto.get('value')

				}
			}).send();

		};
		
	}
	
}

function delete_from_quick_encomenda(id){
	
	if (confirm('Tem a certeza?')) {
		
		new Request.JSON({
			secure: false,
			url: 'ajaxed/carrinho/delete_quick.php?id=' + id
		}).send();
		
	};
	
}

function update_quick_line_qt(id){
	
	var number = parseInt($('each_quick_line_qt_' + id).get('value'));
	
	if ($('each_quick_line_qt_' + id).get('value') != number || number <= 0) {
		
		alert('Quantidade não suportada.');
		$('each_quick_line_qt_' + id).set('value', '');
		$('each_quick_line_qt_' + id).focus();
		
	} else {
		
		new Request.JSON({
			secure: false,
			url: 'ajaxed/carrinho/update_quick.php?id=' + id,
			data: {

				qt: $('each_quick_line_qt_' + id).get('value')

			}
		}).send();
		
	}
	
}

function update_quick_line_desc(id){
	
	var number = parseInt($('each_quick_line_desc_' + id).get('value'));
	
	if ($('each_quick_line_desc_' + id).get('value') != number || number <= 0) {
		
		alert('Quantidade não suportada.');
		$('each_quick_line_desc_' + id).set('value', '');
		$('each_quick_line_desc_' + id).focus();
		
	} else {
		
		new Request.JSON({
			secure: false,
			url: 'ajaxed/carrinho/update_desconto.php?id=' + id,
			data: {

				desc: $('each_quick_line_desc_' + id).get('value')

			}
		}).send();
		
	}
	
}

function show_search_input(){
	
	$('search-box').setStyle('display', '');
	
	$('q').focus();
	
}

function apply_discount(){
		
	var number = parseInt($('global-discount').get('value'));
	
	if ($('global-discount').get('value') != number || number < 0 || number > 100) {
		
		$('alert-error').setStyle('display', '');
		$('global-discount').set('value', '');
		$('global-discount').focus();
		
	} else {
		
		$('discount-all-div').setStyle('display', 'none');
		$('all-loader').setStyle('display', '');
		$('apply-button').setStyle('display', 'none');
		
		new Request.JSON({
			secure: false,
			url: '../ajaxed/carrinho/discount_all.php',
			data: {

				desc: $('global-discount').get('value')

			}
		}).send();
		
	}
	
}