function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function checkNumber(val, defaultVal) {
	if (val.length > 0) returnVal = isInteger(val);
	else returnVal = defaultVal;
	return returnVal;
}

function makeParameterURLSafe(f_sString) {
  f_sString = f_sString.replace( /%/g, '%25' );
  f_sString = f_sString.replace( /&/g, '%26' );
  f_sString = f_sString.replace( /\?/g, '%3F' );
  return f_sString;
}

function reportAlert(transport) {
  alert(transport.responseText);
}

function reportNone(transport, f_blnReload) {
  //alert("Error: "+transport.responseText);
  if (f_blnReload) location.reload();
}

function reportError(transport) {
  alert("Error: "+transport.responseText);
}
function reportSuccess(transport, f_blnReload) {
  if (transport.responseText != "") alert(transport.responseText);
  if (f_blnReload) location.reload();
}

// ######################

function requestNeighborInit(toId) {
	if (nbhd_lang == "de") {
		lang_add = "Als Freund hinzufuegen?";
	} else {
		lang_add = "Add as friend?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_add)) {
    		var url = '/res/ajax/requestNeighborInit.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function addNeighbor(toId) {
	if (nbhd_lang == "de") {
		lang_add = "Als Freund hinzufuegen?";
	} else {
		lang_add = "Add as friend?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_add)) {
    		var url = '/res/ajax/addNeighbor.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function removeNeighbor(toId) {
	if (nbhd_lang == "de") {
		lang_quit = "Freundschaft wirklich beenden?";
	} else {
		lang_quit = "Really quit friendship?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = true;
    if (!boolConfirmation || confirm(lang_quit)) {
    		var url = '/res/ajax/removeNeighbor.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function requestNeighborAccept(fromId) {
	if (nbhd_lang == "de") {
		lang_accept = "Freundschaft akzeptieren?";
	} else {
		lang_accept = "accept friendship?";
	}
	
	if (isInteger(fromId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_accept)) {
	   		var url = '/res/ajax/requestNeighborAccept.php';
 	  		var params = 'fromId=' + fromId;
	   		var ajax = new Ajax.Request(
	   						url,
	   						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function requestNeighborDecline(fromId) {
	if (nbhd_lang == "de") {
		lang_decline = "Freundschaft ablehnen?";
	} else {
		lang_decline = "decline friendship?";
	}
	
	if (isInteger(fromId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_decline)) {
    		var url = '/res/ajax/requestNeighborDecline.php';
    		var params = 'fromId=' + fromId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

// ######################

function addEnemy(toId) {
	if (nbhd_lang == "de") {
		lang_add = "Als Feind hinzufuegen?";
	} else {
		lang_add = "Add as enemy?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_add)) {
    		var url = '/res/ajax/addEnemy.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function removeEnemy(toId) {
	if (nbhd_lang == "de") {
		lang_quit = "Kriegsbeil wirklich begraben?";
	} else {
		lang_quit = "Really bury the hatchet?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = true;
    if (!boolConfirmation || confirm(lang_quit)) {
    		var url = '/res/ajax/removeEnemy.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}
// ######################

function enterGroup(toId) {
	if (nbhd_lang == "de") {
		lang_add = "Gruppe anschliessen?";
	} else {
		lang_add = "Enter group?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_add)) {
    		var url = '/res/ajax/enterGroup.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

function leaveGroup(toId) {
	if (nbhd_lang == "de") {
		lang_quit = "Gruppe austreten?";
	} else {
		lang_quit = "Leave group?";
	}
	
	if (isInteger(toId)) {
		boolConfirmation = false;
    if (!boolConfirmation || confirm(lang_quit)) {
    		var url = '/res/ajax/leaveGroup.php';
    		var params = 'toId=' + toId;
    		var ajax = new Ajax.Request(
    						url,
    						{method: 'post', parameters: params, onFailure: function(transport) {reportError(transport);}, onSuccess : function(transport) { reportSuccess(transport, true);}});
    }
  }
}

// ######################

function showWaiting() {
	$('waitingDiv').setStyle({display: 'block'});
}
function removeWaiting() {
	$('waitingDiv').setStyle({display: 'none'});
}

function insertSmilie(field, txt) {
	field.value+=" "+txt;
}
