/* validation */
$(document).ready(function() {
  $("#bbk_form_1").validate({
    rules: {
      element_1: {
        required: true
      },
      element_2: {
        required: true,
        email: true
      },
      element_3: {
        required: true
      },
      element_4: {
        required: true
      },
      element_5: {
        required: true
      },
      element_6: {
        required: true
      },
      element_7: {
        required: true
      },
      element_8: {
        required: true
      },
      element_9: {
        required: true
      },
      element_10: {
        required: true
      },
      element_11: {
				required: true
			}
    },
    messages: {
      element_1: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_2: {
        required: 'Šis lauks nav aizpildīts!',
        email: 'E-pasts ir nekorekts!'
      },
      element_3: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_4: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_5: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_6: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_7: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_8: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_9: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_10: {
        required: 'Šis lauks nav aizpildīts!'
      },
      element_11: {
        required: 'Šis lauks nav aizpildīts!'
      }
    }
  });
});

/* generating terms of service popup */
$(document).ready(function() {
  
  //LOADING POPUP  
  
  /* 
  //Click the button event!  
  $(".popup").click(function(){  
    //centering with css  
    centerPopup();  
    //load popup  
    loadPopup();  
  }); 
  */
  
  if ($("#popupTos").hasClass("act")) {
  
    //centering with css  
    centerPopup();  
    //load popup  
    loadPopup();
    
    window.setTimeout(function() {
      disablePopup();
    }, 15000);
  
  }

  //CLOSING POPUP  
  //Click the x event!  
  $("#popupClose").click(function(){  
    disablePopup();  
  });  
  //Click out event!  
  $("#popupBg").click(function(){  
    disablePopup();  
  });  
  //Press Escape event!  
  $(document).keypress(function(e){  
    if(e.keyCode==27 && popupStatus==1){  
      disablePopup();  
    }  
  }); 
  
});

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;

//loading popup
function loadPopup() {  
//loads popup only if it is disabled  
  if (popupStatus == 0) {  
    $("#popupBg").css({ 
      "opacity": "0.7"  
    });  
    $("#popupBg").fadeIn("slow");  
    $("#popupTos").fadeIn("slow");  
    popupStatus = 1;  
  }
}

//disabling popup
function disablePopup() {
  //disables popup only if it is enabled
  if (popupStatus == 1) {
    $("#popupBg").fadeOut("slow");
    $("#popupTos").fadeOut("slow");
    popupStatus = 0;
  }
}

//centering popup  
function centerPopup() {  
  //request data for centering  
  var windowWidth = document.documentElement.clientWidth;  
  var windowHeight = document.documentElement.clientHeight;  
  var popupHeight = $("#popupTos").height();  
  var popupWidth = $("#popupTos").width();  
  //centering  
  $("#popupTos").css({  
    "position": "absolute",  
    "top": windowHeight/2-popupHeight/2,  
    "left": windowWidth/2-popupWidth/2  
  });  
  
  //only need force for IE6  
  $("#popupBg").css({  
    "height": windowHeight  
  }); 
}

/* form handling functions */
// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        //target:        '#uploadOutput',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#uploadForm').ajaxForm(options); 
}); 
 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    
    /* validating user input
    
      Mandatory fields:
      
      1. name                                   // id="element_1" 
      2. e-mail                                 // id="element_2"
      3. phone                                  // id="element_3"
      4. from lang                              // id="element_6"
      5. to lang                                // id="element_7"
      6. if checked one of the
            Rakstisks tulkojums                 // id="element_5_1"
            Rakstisks tulkojums ar korektūru    // id="element_5_2"
            Teksta korektūra                    // id="element_5_3"
          then
            Dokuments (-i) tulkošanai           // id="element_9"
          
    */
    
    // remove all previously set invalid classes
    $('.invalid').removeClass('invalid');
    
    // set validation counter
    var notValidCount = 0;
    
    // 1
    if ($('#element_1').val().length < 3) {
      notValidCount++;
      $('#element_1').addClass('invalid');
    }
    
    // 2
    if ($('#element_2').val().length < 6) {
      notValidCount++;
      $('#element_2').addClass('invalid');
    }
    
    // 3
    if ($('#element_3').val().length < 7) {
      notValidCount++;
      $('#element_3').addClass('invalid');
    }
    
    // 4
    if ($('#element_6').val() == 0) {
      notValidCount++;
      $('#element_6').addClass('invalid');
    }
    
    // 5
    if ($('#element_7').val() == 0) {
      notValidCount++;
      $('#element_7').addClass('invalid');
    }
    
    // 6
    if ($('#element_5_1').attr('checked') == true || 
      $('#element_5_2').attr('checked') == true || 
      $('#element_5_3').attr('checked') == true) {
      
      if ($('#element_9').val().length < 3 ) {
        notValidCount++;
        $('#element_9').addClass('invalid');
      }
      
    }
    
    if (notValidCount == 0) {
      return true;
    } else {
      return false;
    }
     
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
    $('#uploadOutput').removeClass('hidden');
}
