var URL = "http://localhost:5000";
//var URL = "http://sportpager.herokuapp.com";

function restRequest(ruta, metodo, datos, dbg_msg) {

	var datosEnviados;
	var tmtWS = 60000;


    console.log(dbg_msg + ": " + ruta + " " + metodo + " " + JSON.stringify(datos));

    if(metodo !== "GET") {
    	datosEnviados = JSON.stringify(datos);
    }
    else {
    	datosEnviados = datos;
    }

	/*
	if( $('#inputTmt').val() > 0 )
	{
		tmtWS = $('#inputTmt').val() * 1000;
	}
	else
	{
		tmtWS = 60000; // 60 segundos
		$('#inputTmt').val(60);
	}
	*/

	var ret = $.ajax({
 
		// The 'type' property sets the HTTP method.
		// A value of 'PUT' or 'DELETE' will trigger a preflight request.
		method: metodo,

		// The URL to make the request to.
		url: URL + ruta,
		timeout: tmtWS,
	    data: datosEnviados,
	    //data: {"Status":"OK","StatusDescription":""},

		// The 'contentType' property sets the 'Content-Type' header.
		// The JQuery default for this property is
		// 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
		// a preflight. If you set this value to anything other than
		// application/x-www-form-urlencoded, multipart/form-data, or text/plain,
		// you will trigger a preflight request.
		contentType: 'application/json',
		//contentType: 'text/plain',
		//contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
		dataType: 'json',

		xhrFields: {
			// The 'xhrFields' property sets additional fields on the XMLHttpRequest.
			// This can be used to set the 'withCredentials' property.
			// Set the value to 'true' if you'd like to pass cookies to the server.
			// If this is enabled, your server must respond with the header
			// 'Access-Control-Allow-Credentials: true'.
			//withCredentials: false,
		},

		headers: {
			// Set any custom headers here.
			// If you set any non-simple headers, your server must include these
			// headers in the 'Access-Control-Allow-Headers' response header.
		},

		/*
		success: function(data, textStatus, jqXHR) {
			var local = new Date();
		    var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" + local.getSeconds();

			console.log(localdatetime + " " +dbg_msg + " OK: ", data);
			if(out != null) {
				out = data;
			};
		},

		error: function(jqXHR, textStatus, errorThrown) {
		console.log(dbg_msg + " error: " + textStatus + " " + errorThrown);
		// Here's where you handle an error response.
		// Note that if the error was due to a CORS issue,
		// this function will still fire, but there won't be any additional
		// information about the error.
		}
		*/
	});

	return ret;
}
