
$(function() {
	
	$('#qr_gen').ready(function() {
		
		var $this = $(this);
		$('select').find('option:eq(0)').attr('selected', 'selected');
		$(this).find('select[name=qr_c]').change(function() {
			$('.smsg p, .qr').html('');
			$.ajax({
				type: "post",
				url: c_http + "_web/_inc/_ajax/load.php",
				data: 'type=' + $(this).find('option:selected').attr('value'),
				cache: false,
				success: function(response) {
					//clearTimeout(timeout);
					if(response.length > 0) {
						//alert(response);
						$('#form').html(response);
					}
					//else alert("didnt add or remove");
				}
			});
		});
		
		//$(this).submit(function() {
		$('.smsg .submit').click(function() {
			var qrst = false;
			$('.smsg p').html('');
			var $inp = $this.find('input:not([type=submit]), select, textarea').each(function() {
//				if($('select[name=qr_c] option:selected').attr('value') == 'text') return false;
				if($(this).attr('name') == 'qr_c' && ($(this).attr('value') == 'bizcard' || $(this).attr('value') == 'sms' || $(this).attr('value') == 'mms')) { qrst = true; }
				if($(this).attr('name') == 'qr_query') return false;
				
				if(qrst && $(this).attr('name') == 'qr_name' && $(this).val().length == 0) {
				  	$(this).css('border-right', '3px solid red');
					$('.smsg p').html('F&ouml;rnamn m&aring;ste minst vara ifyllt.').css('color', 'red');
					return false;
				}
				else if(qrst && $(this).attr('name') == 'qr_number' && $(this).val().length == 0) {
				  	$(this).css('border-right', '3px solid red');
					$('.smsg p').html('Numret m&aring;ste minst vara ifyllt.').css('color', 'red');
					return false;
				}
				else if(!qrst && $(this).val().length == 0) {
				  	$(this).css('border-right', '3px solid red');
					$('.smsg p').html('Fyll i markerade f&auml;t.').css('color', 'red');
				}
			});
			
			if($('.smsg p').html().length == 0) {
				$('#qr_gen').submit();
			}
				
			else return false;
		});
	});
});

function maxlen(obj) {
	var len = document.getElementById(obj);
	if(len.value.length >= 255) { 
		len.value = len.value.substr(0, 255);
		len.onkeyup = function() { maxlen(obj); return false; }
	}
}

function form_submit(id) {
	var id; var doc = document.getElementById(id);
	var form = document.getElementById(id + "_submit");
	var tags = doc.getElementsByTagName("input");
	form.innerHTML = 'Laddar...'; form.onclick = '';
	for(var i = 0; i < tags.length; i++) tags[i].onkeypress = '';
	doc.onkeypress = ''; doc.submit();
}

function enterkey(e, name) {
	var num, name;
	if(e.which) num = e.which;
	else if(window.event) num = e.keyCode;
	if(num == 13) {
		form_submit(name);
		return false;
	} else return true;
}

function block_chars(e) {

	var num, char, reg, res;
	if(e.which) num = e.which;
	else if(window.event) num = e.keyCode;
 
	char = String.fromCharCode(num);
	reg  = /[a-zA-Z0-9_\-]/;
	
	if(!char.match(reg)) res = false;
	if(e.shiftKey && !(e.shiftKey && num >= 65 && num <= 90 )) res = false;
	if((num >= 35 && num <= 40) || num == 8 || num == 9 || num == 13) res = true;
	if(num >= 96 && num <= 105) res = true;
	if(e.shiftKey && num == 109) res = true;
	if(num == 189) res = true;
	
	return res;
}
function decapitalize(str, id) {
	var str, id;
	document.getElementById(id).value = str.toLowerCase();
}

function strip(val, len) {
	var val, len;
	out = val.value.replace(/[^a-zA-Z0-9_-]/g, '');
	if(len) out = out.substr(0, len);
	document.getElementById(val.id).value = out;// decapitalize(out, val.id);
}

function preview(val) {
	var val, str = document.getElementById("prev");
	var nval = unescape(escape(val.value).replace(/(%E5|%E4|%C5|%C4)/gi, 'a').replace(/(%F6|%D6)/gi, 'o'));
	nval = nval.replace(/[^a-zA-Z0-9]+/g, '-');
	str.innerHTML = decapitalize(nval, "prev");
}

function aswitch(i) {
	var i;
	for(var p = 'i'; p.length <= 3; p += 'i') {
		var ddoc = document.getElementById(p);
		var pdoc = document.getElementById("a_" + p);
		var disp = ddoc.style.display == 'none' ? true : false;
		ddoc.style.display = "none"; pdoc.style.backgroundColor = "#fff";
		if(p == i && disp) {
			ddoc.style.display = "block"; pdoc.style.backgroundColor = "#f7f7f7";
		}
	}
}

function nswitch(id) { var id, topic = document.getElementById("nt_" + id);
	$('#short_' + id).slideToggle();
	$('#full_' + id).slideToggle();
	topic.style.color = topic.style.color == 'rgb(147, 61, 124)' ? '#000' : '#933D7C';
}
