var common = function() {

	return {
		
		removeArr : function(arr, action, mod, displayMessage){
			var data = new Array();
			common.doRemove(arr, action, mod, displayMessage, data)	;	
		},
	
		remove: function(id, action, mod, displayMessage){
			var data = new Array();
			var arr = new Array();
			arr[0] = id;
			common.doRemove(arr, action, mod, displayMessage, data);
		},	
	
		removeAdddata: function(id, action, mod, displayMessage, data){

			var arr = new Array();
			arr[0] = id;
			common.doRemove(arr, action, mod, displayMessage, data);
		},		
		
		doRemove : function(arr, action, mod, displayMessage, data) {
			var conf = confirm(displayMessage);
			//common.customConfirm
			if(!conf){
				return;
			}
			$.post("./index.php?mod=" + mod + "&xhr=1", {
				idArr : arr,
				action : action,
				data : data
			}, function(data) {
				if (data !== null) {
						if (data.result) {
							$("#content").html(data.content);
						} else {
							$("#contentError").html(data.message);
						}
					
				}else{
					
				}
			}, "json");				
		},		

		sendData : function(url, toSendData) {
			$.post(url, toSendData, function(data) {
				if (data !== null) {
					if (data.message !== undefined) {
						if (data.result) {
							if(data.content != ""){
								$("#content").html(data.content);
							}
						} else {
							$("#contentError").html(data.message);
						}
					}
				}
			}, "json");
		},
		
		checkLength : function(o,n,min,max){
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				this.updateTips(n + " mag minimaal "+min+" en maximaal "+max+" characters.");
				return false;
			} else {
				return true;
			}
		},
		
		 updateTips : function(element, value){
			$(element).html(value)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				element.removeClass('ui-state-highlight', 1500);
			}, 500);
		 },

		doLogin : function(userName, passw) {
			$("#error").html("");
			var dataToSend = {
				name : userName,
				passw : passw,
				action : "doLogin"
			};
			$.post("./index.php?mod=Mod_login&xhr=1", dataToSend, function(data) {
				if (data !== null) {
					if (data.result) {
						window.location="./index.php?mod=Mod_cms";
					} else {
						// $("#contentError").html(data.message);
						$("#error").html(data.message);
						
					}

				}
			}, "json");
		},
		
		/**
		 * Vul automatich input velden doormiddel van een key val array
		 */
		setInputValByName : function(prefix, data) {
			for(var key in data ){
				if($('input[name=' + prefix + key + ']').length > 0){
					$('input[name=' + prefix + key + ']').val(data[key]);
				}
			}
		},
		
		/**
		* initializatie van de tooltip
		*/
		reloadQtip : function(){
			
			/*$.fn.qtip.styles.infoTooltip = {
				    tip: {
				        corner: 'bottomMiddle',
				        color: '#f76205'
				    },
				    background: '#f76205',
				    color: '#fff',
				    border: { width: 0 },
				    fontSize: '13px'
				};

				$.fn.qtip.styles.errorTooltip = {
				    tip: {
				        corner: 'bottomMiddle',
				        color: '#d8362a'
				    },
				    background: '#d8362a',
				    color: '#fff',
				    border: { width: 0 },
				    fontSize: '13px'
				};	*/		
            $("span.qtipInfo:not(.done)").qtip({
		        content: { text: false },
                position: {
                    corner: {
                        target: 'topMiddle',
                        tooltip: 'bottomMiddle' 
                    }
                }
		    }).addClass("done");

            $("span.qtipError:not(.done)").qtip({
                content: { text: false },
                position: {
                    corner: {
                        target: 'topMiddle',
                        tooltip: 'bottomMiddle' 
                    }
                },
                style: { name: "red" }
            }).addClass("done");

        },
        

        sendDataWithHandler : function(url, data, type, succesHandler, failHandler){
			$.ajax( {
				type : type,
				url : url,
				data : data,
				success : function(data, textStatus, XMLHttpRequest) {
					resultModel.init(data);
					if (resultModel.hasErrors()) {
						resultModel.displayErrorPopup();
						errorHandler.addAll(resultModel.getErrorMessagesArr());
						errorHandler.displayErrors();
						return;
					}				
					succesHandler(data);	
				},
				dataType : "json"
			});	        	
        },
        
        customAlert : function( title, message, pClickFunction ) {
            if ( title === "" ) {
                title = globalvar_pagetitle;
            }
            var fClick = (pClickFunction == undefined ? "" : pClickFunction);
            if (fClick == "") {
              fClick = 
                function() { 
                  $(this).dialog("close"); 
                };
            }
            
            var arrBtn = new Array;
            arrBtn[0] = {text : "Ok",
                         click: fClick};
            
            $("<div title='" + title + "'>" + message + "</div>").dialog( {
                buttons: arrBtn        
            });
        },



        customConfirm : function( title, message, yesFunction, noFunction, modal ) {
            if ( title === "" ) {
                title = globalvar_pagetitle;
            }
            
            $("div#dialog-custom-confirm").dialog("destroy");
            $("<div id='dialog-custom-confirm' title='" + title + "'>" + message + "</div>").dialog( {
                modal: modal !== undefined ? modal : false,
                buttons: [
                    {
                        text: "Ja",
                        click: function() {
                            if ( yesFunction !== undefined ) {
                                yesFunction();
                            }
                            $(this).dialog("close");
                        }
                    },
                    {
                        text: "Nee",
                        click: function() {
                            if ( noFunction !== undefined ) {
                                noFunction();
                            }
                            $(this).dialog("close");
                        }
                    }]
            });
        }        
		
		
	};
}();

