Advertisement
Guest User

Untitled

a guest
Jun 4th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // For an introduction to the Blank template, see the following documentation:
  2. // http://go.microsoft.com/fwlink/?LinkID=397704
  3. // To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints,
  4. // and then run "window.location.reload()" in the JavaScript Console.
  5. (function () {
  6.     "use strict";
  7.  
  8.     document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );
  9.  
  10.     var startDiv = document.getElementById('startDiv');
  11.     var signInDiv = document.getElementById('signInDiv');
  12.     var signUpDiv = document.getElementById('signUpDiv');
  13.     var buttonsDiv = document.getElementById('buttonsDiv');
  14.     var newMessageDiv = document.getElementById('newMessageDiv');
  15.     var answerDiv = document.getElementById('answerDiv');
  16.     var allDivs = [startDiv, signUpDiv, signInDiv, buttonsDiv, newMessageDiv];
  17.     var addressInput = document.getElementById('addressInput');
  18.     var searchDiv = document.getElementById('searchDiv');
  19.     var search_by_time_div = document.getElementById('search_by_time_div');
  20.     var situations = document.getElementById('situations');
  21.     var user;
  22.     var coordinates = [55,35];
  23.  
  24.     function onDeviceReady() {
  25.         document.getElementById('btn_sign_in').addEventListener('click', signInClick, false);
  26.         document.getElementById('btn_sign_up').addEventListener('click', signUpClick, false);
  27.     };
  28.  
  29.     function messagePost() {
  30.         answerDiv.innerHTML = '';
  31.         showDiv(newMessageDiv);
  32.  
  33.        // fix radio buttons
  34.        
  35.        // for (var i = 0; i < document.getElementsByName('rad_btn_location').length; i++) {
  36.             //document.getElementsByName('rad_btn_location')[i].addEventListener('', selectGpsOrAddress, false);
  37.       //  }
  38.        
  39.         //document.getElementById('rad2').addEventListener('selectionchange', selectGpsOrAddress, false);
  40.         //document.getElementById('btn_submit').addEventListener('click', locationByAddress, false);
  41.         document.getElementById('btn_submit').addEventListener('click', sendMessage, false);
  42.     }
  43.  
  44.     function selectGpsOrAddress() {
  45.         if( document.getElementById('rad_btn_location').selectedIndex == 1 ) {
  46.             addressInput.disabled = false;
  47.         }
  48.         else {
  49.             addressInput.disabled = true;
  50.         }
  51.     }
  52.  
  53.     function locationByAddress(adress) {      
  54.        var xmlhttp = getXmlHttp();
  55.        var adress = 'Moscow,+Kulakova,+15';
  56.        var request = 'https://maps.googleapis.com/maps/api/geocode/json?address='+adress+'&key=AIzaSyA5u_V-AjoMQPWLoRE3lNQXcb-AWDxGUf4';
  57.        xmlhttp.open('GET', request, true);
  58.        xmlhttp.onreadystatechange = function () {
  59.            if (xmlhttp.readyState == 4) {
  60.                if (xmlhttp.status == 200) {
  61.                    var obj = JSON.parse(xmlhttp.responseText);
  62.                    answerDiv.innerHTML = 'Latitude: ' + obj.results[0].geometry.location.lat + '<br/>'
  63.                        + 'Longitude: ' + obj.results[0].geometry.location.lng;
  64.                    coordinates = [obj.results[0].geometry.location.lng, obj.results[0].geometry.location.lat];
  65.                }
  66.            }
  67.        };
  68.        xmlhttp.send(null);
  69.     }
  70.  
  71.     function sendMessage() {
  72.         var place = '';
  73.         send('https://localhost:44369/Ecology.svc/addwork', 'POST', JSON.stringify({
  74.             Description: document.getElementById('description').value,
  75.             SituationId: situations.selectedIndex,
  76.             Longitude: coordinates[0],
  77.             Latitude: coordinates[1],
  78.             PlaceName: place,
  79.             Radius: document.getElementById('radius').value
  80.         }), function (x) {
  81.             answerDiv.innerHTML = x;
  82.         })
  83.     }
  84.  
  85.     function send(url, method, data, callback) {
  86.         var xmlHttp = getXmlHttp();
  87.         xmlHttp.onreadystatechange = function () {
  88.             if (xmlHttp.readyState == 4) {
  89.                 var obj = xmlHttp.responseText;
  90.                 if (obj == null || obj == "") {
  91.                     callback(null);
  92.                 }
  93.                 else {
  94.                     try {
  95.                         var result = eval("(" + obj + ")");
  96.                         callback(JSON.stringify(result));
  97.                     }
  98.                     catch (EX) {
  99.                         return (null);
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.         xmlHttp.open(method, url, true);
  105.         xmlHttp.setRequestHeader("Content-type", "application/json");
  106.         xmlHttp.send(data);
  107.     }
  108.  
  109.   //  function messageGet() {
  110.   //      var xmlhttp = getXmlHttp();
  111.   //      xmlhttp.open('GET', 'https://eco.cyrilmarten.com/Ecology.svc/work/Hello!', true);
  112.   //      xmlhttp.onreadystatechange = function () {
  113.   //          if (xmlhttp.readyState == 4) {
  114.   //              if (xmlhttp.status == 200) {
  115.   //                  answerDiv.innerHTML = xmlhttp.responseText;
  116.   //              }
  117.   //          }
  118.   //      };x
  119.   //      xmlhttp.send(null);
  120.   //  }
  121.  
  122.     function getXmlHttp() {
  123.         var xmlHttp;
  124.         try { xmlHttp = new XMLHttpRequest(); }
  125.         catch (e) {
  126.             try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  127.             catch (e) {
  128.                 try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  129.                 catch (e) { alert("This application only works in browsers with AJAX support"); }
  130.             }
  131.         }
  132.         return xmlHttp;
  133.     }
  134.  
  135.     function getPosition() {
  136.         navigator.geolocation.getCurrentPosition(onSuccess);
  137.     }
  138.  
  139.     function onSuccess(position) {
  140.         coordinates = [position.coords.latitude, position.coords.longitude];
  141.         answerDiv.innerHTML = 'Latitude: ' + position.coords.latitude + '<br/>' + 'Longitude: ' + position.coords.longitude;
  142.     }
  143.  
  144.     function signInClick() {
  145.         showDiv(signInDiv);
  146.  
  147.         document.getElementById('btn_submit_sign_in').addEventListener('click', signIn, false);
  148.        
  149.     }
  150.  
  151.     function signIn() {
  152.         send('https://eco.cyrilmarten.com/Ecology.svc/login', 'POST', JSON.stringify({
  153.             Login: document.getElementById('login').value,
  154.             Password: document.getElementById('password').value
  155.         }), function (x) {
  156.             if (x == '{"LoginUserResult":1}') {
  157.                 user = document.getElementById('login').value;
  158.                 answerDiv.innerHTML = 'Welcome, ' + user;
  159.                 showDiv(buttonsDiv);
  160.                 document.getElementById('btn_post_message').addEventListener('click', messagePost, false);
  161.                 //document.getElementById('btn_about_place').addEventListener('click', ?, false);
  162.                 //document.getElementById('btn_history').addEventListener('click', ?, false);
  163.                 //document.getElementById('btn_search').addEventListener('click', search, false);
  164.             } else {
  165.                 if (x == '{"LoginUserResult":-1}') {
  166.                     answerDiv.innerHTML = 'Sorry, you are not registred.';
  167.                 } else {
  168.                     answerDiv.innerHTML = 'Error.';
  169.                 }
  170.             }
  171.         })
  172.     }
  173.  
  174.     function signUpClick()
  175.     {
  176.         showDiv(signUpDiv);
  177.         document.getElementById('btn_submit_sign_up').addEventListener('click', signUp, false);
  178.     }
  179.  
  180.     function signUp() {
  181.         if (document.getElementById('login_new').value != '' && document.getElementById('password_new').value != '' &&
  182.             document.getElementById('email_new').value != '') {
  183.             send('https://eco.cyrilmarten.com/Ecology.svc/create', 'POST', JSON.stringify({
  184.                 Login: document.getElementById('login_new').value,
  185.                 Password: document.getElementById('password_new').value,
  186.                 Email: document.getElementById('email_new').value
  187.             }), function (x) {
  188.                 if (x == '{"CreateNewUserResult":-2}') {
  189.                     answerDiv.innerHTML = 'Sorry, this login already exists.';
  190.                 }
  191.                 else if (x != '{"CreateNewUserResult":-1}' && x != '{"CreateNewUserResult":-2}' && x != '{"CreateNewUserResult":-3}') {
  192.                     user = document.getElementById('login_new').value;
  193.                     answerDiv.innerHTML = 'Welcome, ' + user;
  194.                     showDiv(buttonsDiv);
  195.                     document.getElementById('btn_post_message').addEventListener('click', messagePost, false);
  196.                     //document.getElementById('btn_settings').addEventListener('click', )
  197.                 }
  198.                 else answerDiv.innerHTML = 'Sorry, some troubles occured.';
  199.             })
  200.         }
  201.         else answerDiv.innerHTML = 'Please, fill in all fields.';
  202.     }
  203.  
  204.     function showDiv(visibleDivName) {
  205.         visibleDivName.hidden = false;
  206.         for (var i = 0; i < allDivs.length; i++) {
  207.             if (visibleDivName != allDivs[i]) {
  208.                 allDivs[i].hidden = true;
  209.             }
  210.         }
  211.     }
  212.    
  213.     function search(){
  214.         showDiv(searchDiv);
  215.         //document.getElementById('btn_search_by_time_click').addEventListener('click', search_by_time_click, false);
  216.         //document.getElementById('btn_search_by_geoposition_click').addEventListener('click', search_by_geoposition_click, false);
  217.         document.getElementById('btn_search_last_ten_click').addEventListener('click', search_last_ten_click, false);
  218.     }
  219.     function search_last_ten_click() {
  220.         var xmlhttp = getXmlHttp();
  221.         xmlhttp.open('GET', 'https://eco.cyrilmarten.com/Ecology.svc/searchlast10"', true);
  222.         xmlhttp.onreadystatechange = function () {
  223.             if (xmlhttp.readyState == 4) {
  224.                 if (xmlhttp.status == 200) {
  225.                     answerDiv.innerHTML = xmlhttp.responseText;
  226.                 }
  227.             }
  228.         }; x
  229.         xmlhttp.send(null);
  230.     }
  231.     function search_by_time_click() {
  232.         showDiv(search_by_time_div);
  233.         document.getElementById('btn_search_by_time').addEventListener('click', search_by_time, false);
  234.     }
  235.     function search_by_time(){
  236.         send('https://eco.cyrilmarten.com/Ecology.svc/search', 'POST', JSON.stringify({
  237.            
  238.         }), function (x) {
  239.             answerDiv.innerHTML = x;
  240.         })
  241.     }
  242. } )();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement