//function to change site Language
function doRedirect(newLang) {
	var href = $(location).attr('href');
	var urlAppend = '';
	
	// check if 'lang' is already a variable
	if (href.lastIndexOf("lang=") > 0) 
		// e.g. http://example.com/?xy=kz&lang=
		href = href.substring(0, href.lastIndexOf("lang="));
	else {
		if  ( href.lastIndexOf("?") > 0 )
			urlAppend = '&';
		else
			urlAppend = '?';
	}

	location.href = href +  urlAppend + "lang=" + newLang;	
}

/** 
 * @param name
 * @returns the element of the query string mapped by this name
 */
function getQueryParamValue(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function getLoggedUsername() {
	try{
		return $("#user-information").attr("username");
	} catch(e){ //This means that the user is not logged
		return false;
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
 * 
 * @param str - string to be print
 * @param dialog - if it's a dialog 
 * @param jNotify - if jnotify should be used
 * @param closable - if jnotify is a closable window
 * @return void
 * 
 * usage:
 * 1- Utils.i18n_alert('pippo') - alert('pippo')
 * 2- Utils.i18n_alert('pippo', Utils.confirm, callback ) - confirm('pippo') --> call callback to execute
 * 3- Utils.i18n_alert('pippo', Utils.jNotify ) - $.jnotify('pippo')
 * 3- Utils.i18n_alert('pippo', Utils.jNotify_closable) - $.jnotify('pippo', true)
 */
var Utils ={
	i18n_alert :function(str, notificationFuntion, callbackConfirmFunction, callbackParam) { 
	
		/* TODO : language if not specified is default browser -
		 * otherwise it can be retrieved from server via AJAX
		 */ 
		notificationFuntion = notificationFuntion ? notificationFuntion : function(str) {alert( jQuery.i18n.prop(str) );};
  		
		jQuery.i18n.properties({
 		    name:'messages',
 		    path:'script/messages/',
 		    mode:'both',
 		    callback: function() {
  				var notificationResult = notificationFuntion(str);
  			
 		    	if(notificationResult !== undefined){
 		    		callbackConfirmFunction(notificationResult, callbackParam);
 		    	}
  			}
		});
	},
	jNotify : function(str){
		$.jnotify( jQuery.i18n.prop(str) );
	},
	jNotify_closable : function(str){
		$.jnotify( jQuery.i18n.prop(str), true );
	},
	confirm : function(str){
		if(typeof str === 'object'){
				var finalStr = "";
				for (var elem in str) {
					// handle numbers and new line
					if (isFinite(str[elem])) { 
						finalStr +=  str[elem]; 
						
						// no blank space for newline  
						if (str[elem] !== '\n')
							finalStr += " ";
					}
					else
						finalStr +=  jQuery.i18n.prop(str[elem]) + " ";
				}
			} else
				finalStr = jQuery.i18n.prop(str);

			return confirm( finalStr );
	},
	
	/* based on jQuery.parseXML(<string>) */ 
	parseXML : function(response) {
		var xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
		if(response.indexOf(xmlDeclaration) == -1){
			response = xmlDeclaration + response;
		}
		//remove all <br> tags if present
		if(response.indexOf("<br>") > 0){
			response = response.replace("<br>", "") ;
		}
		return jQuery.parseXML(response);
	},
	printAsI18n : function(str) {
		jQuery.i18n.properties({
 		    name:'messages',
 		    path:'script/messages/',
 		    mode:'both',
 		    cache:false
		});
		
		return jQuery.i18n.prop(str);
	}
};
