function jq_fieldValidation(fieldtype,fieldname,mandatory){
	iconHtml = null;
	switch(fieldtype) {
		case "text":
			if(mandatory){
				field = $("#" + fieldname).get(0);
				fieldtext = field.value;
				if (fieldtext.length > 0) {
					fieldtext = trim(fieldtext);
				}
				if (fieldtext == ""){
					iconHtml = '<img src="/lib/img/error.gif" class="valid_icon" />';
				} else {
					iconHtml = '<img src="/lib/img/ok.gif" class="valid_icon" />';
				}
			}
		break;
	}
	if(iconHtml!=null) $("#validicon_"+fieldname).html(iconHtml);
};

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,'');
};

function rtf_editor(editmode,fieldid){
	switch(editmode){
        case 'email_template':
            tinyMCE.init({
                mode : "exact",
                elements : fieldid,
                theme : "advanced",
                plugins : "safari,paste,fullscreen,sporti_placeholder",
                language : 'da',
        
                theme_advanced_buttons1 : "sporti_fieldlist,|,pastetext,cleanup,|,undo,redo",
                theme_advanced_buttons2 : "",
                theme_advanced_buttons3 : "",
                theme_advanced_toolbar_location : "top",
                theme_advanced_toolbar_align : "left",
                theme_advanced_statusbar_location : "none",
                
                theme_advanced_disable : "bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,outdent,indent,link,unlink,image,help,hr,formatselect,fontselect,fontsizeselect,styleselect,sub,sup,forecolor,backcolor,forecolorpicker,backcolorpicker,charmap,visualaid,anchor,newdocument,blockquote",
                
                paste_block_drop : true,
                
                forced_root_block : '',
                force_br_newlines : true,
                force_p_newlines : false,
                
                custom_shortcuts : false,
                
                relative_urls : false,
                convert_urls : false,
                
                content_css : "/lib/css/editor/email.css"
            });
            
            break;
            
        case 'extended':
            tinyMCE.init({
                mode : "exact",
                elements : fieldid,
                theme : "advanced",
                plugins : "safari,contextmenu,paste,fullscreen",
                language : 'da',
        
                theme_advanced_buttons1 : "bold,italic,|,pastetext,|,bullist,numlist,outdent,indent,|,undo,redo,|,link,unlink,cleanup,|,image,|,fullscreen,|,code",
                theme_advanced_buttons2 : "",
                theme_advanced_buttons3 : "",
                theme_advanced_toolbar_location : "top",
                theme_advanced_toolbar_align : "left",
                theme_advanced_statusbar_location : "bottom",
                theme_advanced_resizing : true,
                theme_advanced_resize_horizontal : false,
                
                relative_urls : false,
                convert_urls : false,

                content_css : "/lib/css/editor/basic.css"
            });
            
            break;
            
		default:
			tinyMCE.init({
				mode : "exact",
				elements : fieldid,
				theme : "advanced",
				plugins : "safari,contextmenu,paste,fullscreen",
		
				theme_advanced_buttons1 : "bold,italic,|,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,cleanup,|,fullscreen,|,code",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",
				theme_advanced_toolbar_location : "bottom",
				theme_advanced_toolbar_align : "center",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
				theme_advanced_resize_horizontal : false,

                relative_urls : false,
                convert_urls : false,

				content_css : "/lib/css/editor/basic.css"
			});
	}
};

function tinymcePlugin(plugin,options){
    
    switch(plugin){
        
        case 'placeholder':
        
            // Creates a new plugin class and a custom listbox
            tinymce.create('tinymce.plugins.sporti_placeholder', {
                
                createControl: function(n, cm) {
                    
                    switch (n) {
                        case 'sporti_fieldlist':
                            var mlb = cm.createListBox('sporti_fieldlist', {
                                 title : 'Indsæt felt',
                                 onselect : function(v) {
                                     tinyMCE.activeEditor.execCommand('mceInsertContent', false, v);
                                     tinyMCE.editorManager.get(tinyMCE.activeEditor).controlManager.get('sporti_fieldlist').selectByIndex(-1);
                                 }
                            });
                            
                            for (i in options) {
                                list_label = options[i].substr(0,1).toUpperCase() + options[i].substr(1);
                                field_label = '{' + options[i] + '}';
                                class_str = 'INPUT_FIELD_PLACEHOLDER_' + i;
                                size_value = options[i].length + 2;
                                
                                mlb.add(list_label, '<input type="text" disabled="disabled" value="' + field_label + '" class="' + class_str + '" size="' + size_value + '" />');
                            };

                            // Return the new listbox instance
                            return mlb;
                    }

                    return null;
                }
                
            });

            // Register plugin with a short name
            tinymce.PluginManager.add('sporti_placeholder', tinymce.plugins.sporti_placeholder);    
            
        break;
    }
    
}

function jq_dateInterval(startdateid,enddateid,days){
	$('#'+startdateid).bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#'+enddateid).dpSetStartDate(d.addDays(days).asString());
			}
		}
	);
	$('#'+enddateid).bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#'+startdateid).dpSetEndDate(d.addDays(days*-1).asString());
			}
		}
	);
};

function jq_postcodeCheck(countryid,postcodeid,cityid,script_url){
	$("#"+countryid).add("#"+postcodeid).keyup(function(){
    
		var country = $("#"+countryid).val();
		var postcode = $("#"+postcodeid).val();
        
		var search = false;
        var maxlen = -1;
        var noSpaces = false;
        
        switch (country){
            case "DEN":
            case "NOR":
                maxlen = 4;
                noSpaces = true;
            break;
        
            case "FRO":
                maxlen = 3;
                noSpaces = true;
            break;
        }
        
        if (noSpaces) postcode = postcode.replace(" ","");
        
        var len = postcode.length;

        if (maxlen != -1 && len > maxlen) {
            postcode = postcode.substr(0,maxlen);
            len = postcode.length;
        }
		
		switch(country){
			case "DEN":
			case "NOR":
				$("#"+postcodeid).attr("maxlength","4");
				if (len==4) search = true;
				else $("#"+cityid).val("");
			break;
			
			case "FRO":
				$("#"+postcodeid).attr("maxlength","3");
				if (len==3) search = true;
				else $("#"+cityid).val("");
			break;
			
			default:
				$("#"+postcodeid).attr("maxlength","10");
		}
		
		if (search) {
			phpurl = script_url+"?country="+country+"&postcode="+postcode;
			
			AjaxRequest.get({
				"url" : phpurl,
				"onSuccess" : function(req){
					city = req.responseText;
					if (city.length >= 1) $("#"+cityid).val(city);
					else {
                        $("#"+cityid).val("[ukendt postnr]");
					}
				},
                "onError" : function(req){
                    alert("Der opstod en fejl ved opslag af by - check venligst at det er rigtigt postnr.");
                }
			});
		}
	});
};

function form_submitEx(formid,postkeyid,postvalue){
	elemForm = $("#"+formid).get(0);
	if (postkeyid) $("#"+formid+" #"+postkeyid).val(postvalue);
	elemForm.submit();
};

function ajax_replace(ajax_url,divid,replace_content_only){
	AjaxRequest.get({
		"url":ajax_url,
		"onSuccess":function(req){ 
            if (replace_content_only) $("#"+divid).html(req.responseText);
            else $("#"+divid).replaceWith(req.responseText);
        }
	});
};

function sporti_frametest(redirect_url){
	if (location.href != top.location.href) top.location.href = location.href;
}

function confirm_msg_ajax(get_url){
    jQuery.facebox({ ajax: get_url + '&ajax=confirm_msg'});
}

function ajax_loadStartgroups(optionid,paramEntrant,activityPath){

    $("#fieldset_startgroup div.field").html("Indlæser...");
    AjaxRequest.get({
        "url": activityPath + "&form=startgroup&option=" + optionid + paramEntrant,
        "onSuccess":function(req){ $("#fieldset_startgroup div.field").html(req.responseText); }
    });
}
