var loadingProcess = "";
var existence;
$(document).ready(function() {//On page load
$("#number").val("");
loadCities();
$("#number").keyup(changeNumber);
$("#number").focusout(changeNumber);
$("#search").button({icons: {primary: "ui-icon-search"}});
$("#search").attr("disabled", true);
$("#addProcess").button({icons: {primary: "ui-icon-check"}});
});
function changeNumber() {
var size = $("#number").val().length;
$("#textCounter").html("(" + size + ")");
var disabled = true;
if(size == 23) {
disabled = false;
}
$("#search").attr("disabled", disabled);
}
function loadCities() {
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "json",
data: {f:"gC"},
success: function(data) {
$("#cities").empty();
if(data == null) {
$("#error-message").show("drop");
} else if(data!=""){
$("#cities").append('<option value="">Seleccione uno</option>');
for(var i = 0; i<data.length; i++) {
$("#cities").append('<option value="' + data[i].value + '">' + data[i].name + '</option>');
}
$("#cities").removeAttr("disabled");
$("#cities").change(citySelected);
}
},
error: function() {
$("#error-message").show("drop");
}
});
}
function citySelected() {
var selectedCity = $("#cities").val();
$("#loading").hide();
$("#results").hide();
$("#process").hide();
$("#errorMessage").hide();
$("#notFound").hide();
$("#processName").val("");
$("#savingPanel").hide();
$("#corporations").empty();
$("#corporations").attr("disabled", true);
$("#corporations").append('<option>Cargando...</option>');
$("#number").attr("disabled", true);
$("#number").val("");
$("#search").attr("disabled", true);
if(selectedCity != "") {
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "json",
data: {f:"gCbC", c: selectedCity},
success: function(data) {
$("#corporations").empty();
if(data == null) {
$("#error-message").show("drop");
} else if(data!=""){
$("#corporations").append('<option value="">Seleccione uno</option>');
for(var i = 0; i<data.length; i++) {
$("#corporations").append('<option value="' + data[i].value + '">' + data[i].name + '</option>');
}
$("#corporations").removeAttr("disabled");
$("#corporations").change(corporationSelected);
}
},
error: function() {
$("#error-message").show("drop");
}
});
} else {
$("#corporations").empty();
}
}
function corporationSelected() {
$("#loading").hide();
$("#results").hide();
$("#process").hide();
$("#errorMessage").hide();
$("#notFound").hide();
$("#processName").val("");
$("#savingPanel").hide();
if($("#corporations").val() != "") {
$("#number").removeAttr("disabled");
}
}
function mostrarAyuda() {
$("#help").dialog();
}
function clickSearch() {
$("#results").hide();
$("#loading").show();
$("#process").hide();
$("#errorMessage").hide();
$("#notFound").hide();
$("#processName").val("");
$("#savingPanel").hide();
$("#error-message-unhandled").hide();
var city = $("#cities").val();
var corporation = $("#corporations").val();
var number = $("#number").val();
if(number.length == 23) {
checkProcessExistence(city, corporation, number);
if(existence == true) {
searchProcess();
} else {
$("#loading").hide();
$("#repeated").dialog({
modal:true,
buttons: {Ok: function() {
$(this).dialog("close");
}}
});
return;
}
} else {
searchProcesses(1);
}
}
function checkProcessExistence(city, corporation, number) {
$.ajax({
type: "POST",
url: "php/contenedor.php",
async: false,
cache: false,
dataType: "xml",
data: {f:"cPE", c: city, cp: corporation, n: number},
success: function(xml) {
if(xml == null) {
$("#loading").hide();
$("#error-message-unhandled").show("drop");
} else {
if(xml.documentElement.getAttribute('c') != "0") {
existence = false;
} else {
existence = true;
}
}
},
error: function() {
$("#loading").hide();
$("#error-message-unhandled").show("drop");
}
});
}
function searchProcess() {
$("#resultsContent").html("").hide();
$("#results").hide();
$("#process").hide();
$("#errorMessage").hide();
$("#notFound").hide();
$("#processName").val("");
$("#savingPanel").hide();
var city = $("#cities").val();
var cityName = $("#cities option:selected").text().trim();
var corporation = $("#corporations").val();
var corporationName = $("#corporations option:selected").text().trim();
var number = $("#number").val();
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "json",
cache: false,
data: {f:"lPbN", c: city, cp: corporation, n: number, v: "", ck: "", cpn: corporationName, cn: cityName},
success: function(json) {
$("#loading").hide();
if(json == null) {
$("#error-message-unhandled").show("drop");
} else {
if(json[0] == true) {
$("#process").html(json[1]).show();
$("#savingPanel").show();
} else {
$("#notFound").show("drop");
}
}
},
error: function() {
$("#loading").hide();
$("#error-message-unhandled").show("drop");
}
});
}
function searchProcesses(page) {
$("#savingPanel").hide();
$("#errorMessage").hide();
$("#process").hide();
if(page != 1) {
$("#resultsTable").fadeOut('fast');
}
var city = $("#cities").val();
var cityName = $("#cities option:selected").text().trim();
var corporation = $("#corporations").val();
var corporationName = $("#corporations option:selected").text().trim();
var number = $("#number").val();
var viewstate = "";
var cookie = "";
if($("#viewstate") && page != 1) {
viewstate = $("#viewstate").val();
}
if($("#cookie") && page != 1) {
cookie = $("#cookie").val();
}
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "html",
cache: false,
data: {f:"sPbPN", c: city, cp: corporation, n: number, p: page, v: viewstate, ck: cookie, cpn: corporationName, cn: cityName},
success: function(html) {
if(html == null) {
$("#loading").hide();
$("#error-message-unhandled").show("drop");
} else {
$("#loading").hide();
$("#resultsContent").html(html)
$("#results").tabs().show("blind");
$("#previous").button({icons: {primary: "ui-icon-triangle-1-w"}});
$("#next").button({icons: {secondary: "ui-icon-triangle-1-e"}});
}
},
error: function() {
$("#loading").hide();
$("#error-message-unhandled").show("drop");
}
});
}
function showLoading(sign) {
if(sign < 0) {
$("#loadingPrevious").show();
} else if(sign > 0) {
$("#loadingNext").show();
}
}
function resetState() {
if($("#viewstate")) {
$("#viewstate").val("");
}
if($("#cookie")) {
$("#cookie").val("");
}
}
function selectProcess(number) {
$("#process").hide();
$("#errorMessage").hide();
$("#notFound").hide();
$("#processName").val("");
$("#savingPanel").hide();
$("#loading_" + number).show();
loadingProcess = number;
var city = $("#cities").val();
var cityName = $("#cities option:selected").text().trim();
var corporation = $("#corporations").val();
var corporationName = $("#corporations option:selected").text().trim();
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "json",
cache: false,
data: {f:"lPbN", c: city, cp: corporation, n: number, v: "", ck: "", cpn: corporationName, cn: cityName},
success: function(json) {
if(json == null) {
$("#loading_" + loadingProcess).hide();
$("#error-message-unhandled").show("drop");
} else {
$("#loading_" + loadingProcess).hide();
$("#process").html(json[1]).show();
$("#savingPanel").show();
}
},
error: function() {
$("#loading_" + loadingProcess).hide();
$("#error-message-unhandled").show("drop");
}
});
}
function addProcess() {
var city = $("#cities").val();
var cityName = $("#cities option:selected").text().trim();
var corporation = $("#corporations").val();
var corporationName = $("#corporations option:selected").text().trim();
var number = $("#foundProcessNumber").val();
var name = $("#processName").val().trim();
if(name == "") {
alert("Debe ingresar un nombre para el proceso");
return;
}
$("#cities").attr("disabled", true);
$("#corporations").attr("disabled", true);
$("#number").attr("disabled", true);
$("#search").attr("disabled", true);
$("#addProcess").attr("disabled", true);
$("#loadingSave").show();
$.ajax({
type: "POST",
url: "php/RobotServiceOld.php",
dataType: "json",
cache: false,
data: {f:"sP", c: city, cp: corporation, n: number, d: name, cpn: corporationName, cn: cityName},
success: function(json) {
$("#cities").removeAttr("disabled");
$("#corporations").removeAttr("disabled");
$("#number").removeAttr("disabled");
$("#search").removeAttr("disabled");
$("#addProcess").removeAttr("disabled");
$("#loadingSave").hide();
if(json == null) {
$("#error-message-unhandled").show("drop");
} else {
if(json[0] == true) {
$("#divForm").hide();
$("#savingPanel").hide();
$("#results").hide();
$("#saveSuccess").show();
} else {
$("#errorMessageText").html(json[1]);
$("#errorMessage").show();
}
}
},
error: function() {
$("#error-message-unhandled").show("drop");
}
});
}