if (!GENERAL) {
	var GENERAL = new Object();
};

/**
 * Method implements header domain search in domains module
 * 
 * @return bool
 */
GENERAL.searchDomains = function() {
	var form = $('#frmDomainsSearch');
	if (form.find('#searchDomains').val() != '') {
		form.submit();
		return true;
	}
	return false;
};

/**
 * Method send login credentials to server
 * 
 * @param integer id
 */
GENERAL.login = function(id) {
	if (!id) {
		return false;
	}	
	var form = $('#' + id);
	form.submit();
};

/**
 * Method runs function by name
 * 
 * @param string func
 */
GENERAL.run = function(func) {
	if((eval("typeof " + func + " == 'function'"))) {
		eval(functionName + '()');
	}
};

////////////////////////////////////////////////////////////GLOBALS
var g_ajax_timeout = 50000;
var g_topLoginUserText = "User name";
var g_topLoginPassText = "Password";
var g_topSearchUpdated = false;
var g_topLoginUserUpdated = false;
var g_topLoginPassUpdated = false;
var g_listFlag = '';
var request = null;

function handleAjaxError(XMLHttpRequest, textStatus, errorThrown) {
	$(window).Loader('hide');		
	try {
		var msg;
		if (textStatus == 'timeout') {	// If timeout - display timeout message
			msg = g_error_loader_message_timeout;
	    } else {	// Otherwise display exception code
	    	switch(XMLHttpRequest.status) {
	    		case 503:	// 401		    					    			
	    			// 401 HTTP error code doesn't work - browser authorization dialog popup before redirect (if IIS Integrated Windows authentication enabled, if disabled - everything is ok)
	    			// Fix it (maybe return 200 response code instead with redirect URL) 
	    			window.location = '/login';
	    			return;
	    			break;
		    	case 200:
		    	default:
			    	msg = g_error_loader_message;		    	
			    	var htmlResponse = XMLHttpRequest.responseText;
					if (htmlResponse.indexOf(';Code:') !== -1) {
						var errcode = parseInt(htmlResponse.split(';')[1].substr(6,4));
						if (typeof(errcode) == 'number' && errcode > 0) { 
							msg += '<br />' + 'Error Code: ' + errcode;
						}
					}
					break;
	    	}
	    }
		showModalMessage(msg, 'error');
	} catch (e) {	
		// ???
	}		
}

$.ajaxSetup({
	type: 'post',
	timeout: 50000,
	error: handleAjaxError
});

/**
 * Function load scripts on demand
 * 
 * @param array  scripts
 * @param string onComplete
 * @param bool   isCache
 * @param bool   forceCombined
 */
jQuery.getScripts = function(scripts, onComplete, isCache, forceCombined) {	
	if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}	
	if (g_combineJS || forceCombined) {		
		var scriptsToLoad = new Array();
		var debug = 0;
		for(var s in scripts) {
			var script = null;			
			if (scripts[s].indexOf('.js') == -1) {
				var script = g_minifyGroup + scripts[s] + '.js&debug=' + debug;			
			} else {
				var script = g_minifySingle + scripts[s] + '&debug=' + debug;
			}
			if (script != null && !in_array(script, scriptsToLoad)) {
				scriptsToLoad.push(script);
			}		
		}
		var scripts = scriptsToLoad;
	}		
	var loadedScripts = getLoadedScripts();
	var scriptsToLoad = Array();
	for(var s in scripts) {
		if (!in_array(scripts[s], loadedScripts)) {
			scriptsToLoad.push(scripts[s]);
		}
	}
	var count = scriptsToLoad.length;
	if (count == 0 && onComplete != undefined) {
		GENERAL.run(onComplete);		
		return false;
	} else {
		var i = 1;		
		var onScriptLoaded = function(data, response) {
			if (i++ == count) {
				GENERAL.run(onComplete);
			}
		};
		for(var s = 0; s < count; s++) {
			jQuery.getScript(scriptsToLoad[s], onScriptLoaded); 	
		}
	}	
};

/**
 * Function load combined style on demand
 *
 * @param string style
 */
jQuery.getStyle = function(style) {		
	if(typeof document.createStyleSheet == 'function') {
		document.createStyleSheet(style);
	} else {
		var newCSS	 = document.createElement('link');
		newCSS.rel	 = 'stylesheet';
		newCSS.type	 = 'text/css';
		newCSS.media = 'all';
		newCSS.href	 = style;
		// var styles	= "@import url(' " + url + " ');";
		// newSS.href	='data:text/css,'+escape(styles);
		document.getElementsByTagName('head')[0].appendChild(newCSS);
	}
}

/**
 * Function load stylesheets on demand
 * 
 * @param arary  styles
 * @param string onComleteFunction
 * @param bool   iscache 
 */
jQuery.getStyles = function(styles, onCompleteFunction, iscache) {
	/*if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}*/
	if (g_combineCSS) {		
		var groups = new Array();
		var single = new Array();
		for(var s in styles) {
			if (styles[s].indexOf('.css') == -1) {
				groups.push(styles[s]);
			} else {
				single.push(styles[s]);
			}
		}
		if (groups.length > 0) {
			jQuery.getStylesCombined(groups, 'group', onCompleteFunction, iscache);
		}
		if (single.length > 0) {
			jQuery.getStylesCombined(single, 'single', onCompleteFunction, iscache);
		}
	} else {
		var loadedStyles = getLoadedStyles();
		var stylesToLoad = Array();
		for(var s in styles) {
			if (!in_array(styles[s], loadedStyles)) {
				stylesToLoad.push(styles[s]);
			}
		}
		//if there is no styles, run the complete function
		if(stylesToLoad.length == 0){
			//runFunctionByName(onCompleteFunction);
			return false;
		}		
		var i = 1;
		/*var onScriptLoaded = function(data, response) { 
			if (i++ == stylesToLoad.length) { //run the onComplete function on load all scripts
				runFunctionByName(onCompleteFunction);
			}
		} ;	*/
		for(var s in stylesToLoad) { 
			//jQuery.getScript(stylesToLoad[s], onScriptLoaded);			
			jQuery.getStyle(stylesToLoad[s]);
		};
	}
};

/**
 * Method return link for combined script/style
 * 
 * @param array  styles
 * @param string type
 * @param string onCompleteFunction
 * @param bool   isCache
 */
jQuery.getStylesCombined = function(styles, type, onCompleteFunction, isCache) {
	/*if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}*/		
	var debug = 0;
	var loadedStyles = getLoadedStyles();
	var stylesToLoad = new Array();
	for(var s in styles) {
		switch(type) {
			case 'group':
				var style = g_minifyGroup + styles[s] + '.css&debug=' + debug;
				break;
			case 'single':
			default:
				var style = g_minifySingle + styles[s] + '&debug=' + debug;
				break;
		}
		if (!in_array(style, loadedStyles)) {
			stylesToLoad.push(style);
		}
	}
	if (stylesToLoad.length == 0) {
		//runFunctionByName(onCompleteFunction);
		return false;
	}			
	var i = 1;
	/*var onScriptLoaded = function(data, response) {
		if (i++ == scriptsToLoad.length) { //run the onComplete function on load all scripts
			runFunctionByName(onCompleteFunction);
		}
	};*/		
	for(var s in stylesToLoad) { 
		//jQuery.getScript(stylesToLoad[s], onScriptLoaded);
		jQuery.getStyle(stylesToLoad[s]);
	};
}

//---------------------------------------------------------------------------
/**
 * Function return a list of loaded scripts
 * 
 * @return array
 */
function getLoadedScripts() {
	var scripts = new Array();
	jQuery('head > script').each(function(index) {
		var src = jQuery(this).attr('src');
		if (src != null) {
			src = src.substring(src.indexOf(window.location.hostname));// + window.location.hostname.length
			scripts.push(src);
		}
	});
	return scripts;
}

/**
 * Function return a list of loaded stylesheets
 * 
 * @return array
 */
function getLoadedStyles() {
	var styles = new Array();
	jQuery("head > link[rel='stylesheet']").each(function(index) {
		var src = jQuery(this).attr('href');
		if (src != null) {
			src = src.substring(src.indexOf(window.location.hostname));// + window.location.hostname.length
			styles.push(src);
		}
	});
	return styles;
}

//---------------------------------------------------------------------------
// get if the script is in the scripts list or not.
function in_array(needle, haystack) {
	for(var i=0; i < haystack.length; i++) {
		if(haystack[i] && haystack[i].indexOf(needle) != -1) {
			return true;			
		}
	}	
	return false;
}

$.fn.clearFields = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form') {
			return $(':input',this).clearFields();
		}
		if (type == 'text' || type == 'password' || tag == 'textarea') {
			this.value = '';
		} else if (type == 'checkbox' || type == 'radio') {
			this.checked = false;
		} else if (tag == 'select') {
			$(this).find('option:first').attr('selected', 'selected');
		}
	});
};

$.fn.reset = function() {
    return this.each(function() {
        $(this).is('form') && this.reset();
    }); 
};
