/** * @author Kaj Str�m * @copyright DL Software Oy, 2014. */ $(document).ready(function() { /* Rivien v�ritys klikattaessa */ $(".rr_rsv_table tr").click(function() { $(this).addClass('selected'); }); /* Hakulomakeen eventtej� */ $("#btn_recurring_simulate").click(function () { ResourceReservation.SimulateRecurringHandler($(this)); }); $("#btn_next_step1").click(function () { ResourceReservation.SaveRecurringHandler($(this)); }); ResourceReservation.instructorSelectionHandling(Modernizr.touch) ResUpdateObjInfo(); }); /** * @type {Object} */ var ResourceReservation = {}; /** * Handle setting up a different type of instructor selection if the device is a touch device. * * @param {boolean} isTouchDevice Specify if the customer device is a touch device or not. */ ResourceReservation.instructorSelectionHandling = function (isTouchDevice) { if (isTouchDevice) { //Remove unnecessary form elements. $("#idInstructors").remove(); $("#idInstructorsLbl").remove(); $("#idInstructorList").prop("name", "instructors[]"); } else { $("#idInstructorList").click(function () { MoveOptions(this, document.getElementById('idInstructors')); DeleteEmptyOptions('idInstructors'); }); } }; ResourceReservation.searchFormTypeHandler = function (caller) { $("#btn_select_recurring").removeClass('btn-primary'); $("#btn_select_recurring").addClass('btn-default'); }; ResourceReservation.SimulateRecurringHandler = function (caller) { //JKU print simulated resource reservation list and create Continue button event handler (url) starttime = null; endtime= null; object_p=null; location_p=null; resource_id=null; space_code=null; if ($('[name="d_start_time"]').length > 0) starttime = $('[name="d_start_time"]').val(); else starttime = $('[name="res_stime"]').val(); if ($('[name="d_end_time"]').length > 0) endtime = $('[name="d_end_time"]').val(); else endtime = $('[name="res_etime"]').val(); if ($('[name="d_object_code"]').length > 0) object_p = $('[name="d_object_code"]').val(); else object_p = $('[name="objectCode"]').val(); if ($('[name="d_location"]').length > 0) location_p = $('[name="d_location"]').val(); else location_p = $('[name="location"]').val(); if ($('[name="d_resource_id"]').length > 0) resource_id = $('[name="d_resource_id"]').val(); else resource_id = $('[name="resourceId"]').val(); if ($('[name="d_space_code"]').length > 0) space_code = $('[name="d_space_code"]').val(); else space_code = $('[name="spaceCode"]').val(); //make parameter string of values chosen //urlparameters = 'simulate_only=T&frequency='+$('#frequency').val()+'&start_date='+$('#start_date').val()+'&start_end='+$('#start_end').val()+'&start_time='+$('#time').val()+'&time_end='+$('#time_end').val()+'&object='+$('#idObject').val()+'&location='+$('#location').val(); urlparameters = 'simulate_only=T&frequency='+$('#sel_frequency').val()+'&start_date='+starttime+'&start_end='+$('#start_end').val()+'&start_time='+starttime+'&time_end='+endtime+'&object='+object_p+'&location='+location_p+'&resource_id='+resource_id+'&space_code='+space_code+'&ccode='+$('[name="ccode"]').val(); //save values in forms //$('[name="d_start_end"]').val($('#start_end').val()); //$('[name="frequency"]').val($('#sel_frequency').val()); //$('[name="recurring_rsv"]').val('T'); //$("#id_is_recurring").val(1); try { $.getJSON('index.php?func=mod_rr&ShowRecurringRsvList=1&', urlparameters) .done(function (data) { $('#RecurringRsv').html(data); //get number of Ok reservations from cookie var num_rsv_ok = 0; num_rsv_ok = Cookie.get('rr_num_rsv_ok'); if (num_rsv_ok == '"0"'){ //all reservations failed, or there are none changeButtonMode('btn_next_step1',true, false, false); //disable continue button } else{ changeButtonMode('btn_next_step1', false, true, false); //show continue button } changeButtonMode('btn_recurring_simulate', false, false, true); //hide simulate button }) .fail(function( jqxhr, textStatus, error ) { var err = textStatus + ", " + error; console.log( "Request Failed: " + err ); alert( "Request Failed: " + err ); }) ; } catch(err){ //something went wrong, show error msg to user //alert(err); } return; } ResourceReservation.SaveRecurringHandler = function (caller) { //JKU get number of reservations and number of Ok reservations from cookie var num_rsv = 0; var num_rsv_ok = 0; num_rsv = Cookie.get('rr_num_rsv'); num_rsv_ok = Cookie.get('rr_num_rsv_ok'); if(num_rsv!=null){ //convert string to int num_rsv = num_rsv.replace('"',''); num_rsv = parseInt(num_rsv) ; } if(num_rsv_ok!=null){ num_rsv_ok = num_rsv_ok.replace('"',''); //convert string to int num_rsv_ok = parseInt(num_rsv_ok) ; } if (IsRecurring() && HasRecurringValuesChanged() && (num_rsv_ok > 0 && num_rsv_ok < num_rsv)) { //some reservations failed, click event generates warning showConfirmation(myCallback, confirmRecurringContinue); //$('#recurring_values_changed').val('0'); } else{ //all reservations ok window.location = getConfirmUrl(); } return; } function myCallback(status){ if(status){ confirmContinue=true; window.location = getConfirmUrl(); } else{ confirmContinue=false; return false; } } /* function setDefaultTimeEnd(objlength){ //sets endtime according to object length whenever user select starttime var starttime = $('#time').val().toString(); var strEndtime=''; var d1 = new Date(); var d2 = new Date(); var h1, h2, m1, m2; h1 = starttime.substr(0,2); m1 = starttime.substr(starttime.indexOf(':')+1,2); d1.setHours(parseInt(h1)); d1.setMinutes(parseInt(m1)); d2.setHours(d1.getHours()); d2.setMinutes(d1.getMinutes() + objlength); //pad hour with leading zeros if(d2.getHours().toString().length==1) strEndtime = '0'; strEndtime += d2.getHours().toString() + ':'; if(d2.getMinutes().toString().length==1) { //pad minutes with leading zeros strEndtime += '0'; } strEndtime += d2.getMinutes().toString(); //set endtime $('#time_end').val(strEndtime); } function setDefaultDateEnd(startdate){ //JKU set enddate to startdate when user selects startdate (if enddate is less than startdate) if(startdate.toString().length == 0) return; var d1 = Date.parse(startdate); var d2 = Date.parse($('#start_end').val()); if(d1>d2) { $('#start_end').val($('#start_date').val().toString()); //update calendar $(".datepicker").datepicker("setDate", startdate); } } */ function replaceAll(find, replace, str) { //replace all occurences of find with replace in str return str.replace(new RegExp(find, 'g'), replace); } function getConfirmUrl() { if(Cookie.get('rr_chknum')!=null && IsRecurring()) { url = 'index.php?func=mod_rr&d_resource_id=' + replaceAll('"','',Cookie.get('rr_instructor_resource_id')) + '&d_object_code=' + replaceAll('"','',Cookie.get('rr_obj_code')) + '&d_start_time=' + replaceAll('"','',Cookie.get('rr_starttime')) + '&d_end_time=' + replaceAll('"','',Cookie.get('rr_endtime')) + '&d_location=' + replaceAll('"','',Cookie.get('rr_location')) + '&ccode='+replaceAll('"','',Cookie.get('rr_ccode')) + '&d_space_code='+replaceAll('"','',Cookie.get('rr_space_code'))+'&d_chknum=' + replaceAll('"','',Cookie.get('rr_chknum')) + '&confirm=T&new_rsv=1'; url += '&frequency='+replaceAll('"','',Cookie.get('rr_frequency'))+'&recurring_rsv=T&step=2'; } else { //if($('#d_resource_id').length>0) //JKU uncommented because the lines were exactly the same. url = 'index.php?func=mod_rr&d_resource_id=' + $('#d_resource_id').val() + '&d_object_code=' + $('#d_object_code').val() + '&d_start_time=' + $('#d_start_time').val() + '&d_end_time=' + $('#d_end_time').val() + '&d_location=' + $('#d_location').val() + '&ccode=' + $('[name="ccode"]').val() + '&d_space_code=' + $('#d_space_code').val() + '&d_chknum=' + $('#d_chknum').val() + '&confirm=T&new_rsv=1&step=2'; //else //url = 'index.php?func=mod_rr&d_resource_id=' + $('#d_resource_id').val() + '&d_object_code=' + $('#d_object_code').val() + '&d_start_time=' + $('#d_start_time').val() + '&d_end_time=' + $('#d_end_time').val() + '&d_location=' + $('#d_location').val() + '&d_chknum=' + $('#d_chknum').val() + '&confirm=T&new_rsv=1&step=2'; } return url; // encodeURI } function LoadURL(url){ window.location = url; return; } function changeButtonMode(name, disable, makeprimary, hide){ if(hide) $('#'+name).hide(); else{ $('#'+name).show(); if(disable){ $('#'+name).attr('disabled', 'disabled'); $('#'+name).removeClass('btn-primary'); $('#'+name).addClass('btn-default'); } else { $('#' + name).removeAttr('disabled'); if (makeprimary) { $('#' + name).addClass('btn-primary'); $('#' + name).removeClass('btn-default'); } else { $('#' + name).addClass('btn-default'); $('#' + name).removeClass('btn-primary'); } } } } function IsRecurringHandler (checked) { if(checked){ $('#time_fields_recurring').show(); changeButtonMode('btn_recurring_simulate', false, true, false); //show simulate button } else{ $('#time_fields_recurring').hide(); changeButtonMode('btn_next_step1', false, true, false); //enable continue button changeButtonMode('btn_recurring_simulate', false, false, true); //hide simulate button } } function IsRecurring(){ return document.getElementById('chkIsRecurring').checked; } function HasRecurringValuesChanged(){ return $('#recurring_values_changed').val().toString()=='1'; } function RecurringValuesChanged(){ changeButtonMode('btn_next_step1', true, false, false); //disable continue button, user MUST test reservations first changeButtonMode('btn_recurring_simulate', false, true, false); //show simulate button $('#recurring_values_changed').val('1'); } function TabClick(name) { if(name=='Customer' && confirmContinue==false && IsRecurring() && HasRecurringValuesChanged()) { //user has clicked on Customer tab after changed recurring values, but selected not to continue on lightbox confirm ->do not open customer form return false; } var tmp; $("#tabs li").each(function(n) { tmp = $(this).attr('id').replace('tab',''); if(name!=tmp) { $(this).removeClass('active'); //unselect previously selected tab(s) $('#div'+tmp+'Form').hide(); //hide previously opened form(s) } else{ $(this).addClass('active'); //make tab selected $('#div'+tmp+'Form').show();//show Form } }); } function resetDoReservationAction(){ $('[name="func"]').val('mod_rr'); } function ResUpdateObjInfo () { var selObj = $('form[name="rr_search"] #idObject').val(); if (selObj) { xajax_ResUpdateObjInfo(selObj); ShowElement('res_info_area'); }else{ HideElement('res_info_area'); } } var confirmRecurringContinue = {title: 'Olet varaamassa', body: 'Kaikki varaukset ei tule onnistumaan, haluatko kuitenkin jatkaa?', buttons: {accept: 'Kyllä',reject:'Ei'}}; var confirmContinue = true;