﻿function hasFocus(jqueryObject){
	return jqueryObject.hasClass('has-focus');
}

function fillDropDown(id,JSONdata,postUpdateFn){
	if(id!=null){
		var jddl = $('#' + id);
		var ddl = jddl[0];
		var url = jddl.attr('data-url');
		var ie7 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) ? true : false; 
		
		ddl.options.length=0;
		ddl.options.add(new Option('Henter data...', '-1'));
		ddl.selectedIndex=0;
		//alert(JSONdata);
		$.ajax({
			type: 'GET'
			, url: url + (ie7 == true ? '&ie7=true' : '')
			, data: jQuery.parseJSON(JSONdata)
			, dataType: 'html'
			, async: true
			, success: function(html)
			{
				eval(html);
				if (typeof (JSONres) != "undefined") {
					if (JSONres != null && typeof (JSONres.element) != 'undefined'){
						
						var jObj = $('#'+JSONres.element);
						var ddl = jObj[0];
						var dataDefault = jObj.attr('data-default');
						
						ddl.options.length=0;
						if (typeof (JSONres.items) != 'undefined' && JSONres.items.length > 0) {
							var l = JSONres.items.length;
							var selectedIndex = 0;
							for (var i = 0; i < l; i++) {
								var opt;
								if(JSONres.items[i].text == '-' && JSONres.items[i].value == '-'){
									opt = new Option('------','-');
									opt.disabled=true;
								}else{
									opt = new Option(JSONres.items[i].text, JSONres.items[i].value);
									if(eval(JSONres.items[i].selected)){
										selectedIndex=i;
									}
								}
								ddl.options.add(opt);
							}
							ddl.selectedIndex = selectedIndex;
							ddl.disabled = false;
							jObj.removeAttr('data-sv');
							if(hasFocus(jObj)){ddl.focus();}
						} else {
							ddl.disabled=true;
							ddl.options.add(new Option('Ingen data...', "-1"));
						}
						if(eval(postUpdateFn)==null){
							$(ddl).change();
						}else{
							postUpdateFn(ddl);
						}
					}
				}
			}
		}); 
	}
}


function cancelEvent(e) {
	if (!e) var e = window.event;
	try {
		e.returnValue = false;
		e.cancelBubble = true;
	} catch (err) { }
	try {
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		if (e.returnValue) {
			e.returnValue = false;
		}
		if (e.cancelBubble) {
			e.cancelBubble = true;
		}
	} catch (err) { }
	try {
		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	} catch (err) { }
}

//Extending jQuery with a better trigger function
(function ($) {
	//Attach to jQuery
	$.fn.extend({
		triggerLink: function (e, url) {
			if (e.target.tagName.toLowerCase() != 'a') {
				cancelEvent(e);
				if (e.ctrlKey) {
					window.open(url, 'a' + Math.random());
				} else if (e.shiftKey) {
					window.open(url, '_blank', 'menubar=1,toolbar=1,status=1');
				} else {
					window.location = url;
				}
			}
		}
	});

	//pass jQuery to the function, 
	//So that we will able to use any valid Javascript variable name 
	//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
})(jQuery);

$(document).ready(function() {
	$('hr').each(function(){
		var obj = $(this);
		var cssStyle = obj.attr('style');cssStyle=(cssStyle?' style="'+cssStyle+'"':'');
		var cssClass = obj.attr('class');cssClass=' class="hr '+(cssClass?cssClass:'')+'"';
		obj.wrap('<div '+cssClass+cssStyle+' />');
		obj.removeAttr('class');
		obj.removeAttr('style');
	});
});
