var pageroot = '/index.php/';
var webroot = '/';

function wordsonly(str){
         return str.replace(/[^a-zA-Z-\s]+/g,"");
}

function intonly(str){
         return str.replace(/[^\d]+/g,"");
}

function email_format(str){
         return str.replace(/[^a-zA-Z_\-0-9@\.]+/g,"");
}

function setRent(th,val){
         $('txtTotRent').value = (val*th);
         setTotal();
}

function hideFlashIntro(){
         $('flashIntro').style.display= 'none';
}

function setTotal(){
num = ($('txtTotRent').value*1 + $('txtOtherExp').value*1)*1.015;
num = Math.round(num*100)/100;
$('txtTotal').value = RoundNumber(num,2,true);
}

function RoundNumber(num, ndec, addz)
{
      if (num != "")
      {
            var factor = Math.pow(10, ndec);

            num = Math.round(num * factor);
            num = num / factor;
            if (addz)
            {
                  var dot = ("" + num).indexOf(".");
                  if (dot>=0)
                  {
                        var nzeros = dot + ndec + 1 - ("" + num).length;
                        for (i=0; i<nzeros;i++)
                        {
                              num = num + "0";
                        }
                  }
                  else
                  {
                        num = num + ".";
                        for (i=0; i<ndec;i++)
                        {
                              num = num + "0";
                        }
                  }
            }
      }

      return (num);
}




function showRegOpts(id){
if(id==1){
txtV = 'block';
} else {
txtV = 'none';
}

aziendaInput.style.display = txtV;
}


window.addEvent('domready', function(){

if($('insNewsletterFrm')){
              $('insNewsletterFrm').addEvent('submit', function(e) {
                      /**
                       * Prevent the submit event
                       */
                      new Event(e).stop();

                      /**
                       * This empties the log and shows the spinning indicator
                       */
                      //var log = $('response').empty().addClass('ajax-loading');

                      var log = $('newsLetterResponse').empty().addClass('ajax-loading');
                      /**
                       * send takes care of encoding and returns the Ajax instance.
                       * onComplete removes the spinner from the log.
                       */
                      if($('txtNewsletterEmail').value==''){
                      alert('Please insert an email address');
                      //log.removeClass('ajax-loading');
                      } else {
                      $('newsLetterResponse').style.display = 'block';
                      this.send({
                              update: log,
                              onComplete: function() {
                                      log.removeClass('ajax-loading');
                                      $('txtNewsletterEmail').value = '';
                                      setTimeout("hideDiv('newsLetterResponse')",2500);
                              }
                      });
                      }

              });
}

if($('editDetailsFrm')){
              $('editDetailsFrm').addEvent('submit', function(e) {
                      /**
                       * Prevent the submit event
                       */
                      new Event(e).stop();

                      /**
                       * This empties the log and shows the spinning indicator
                       */

                      var log = $('checkLoading').empty().addClass('ajax-loading');
                      /**
                       * send takes care of encoding and returns the Ajax instance.
                       * onComplete removes the spinner from the log.
                       */
                      if($('txtTenantName').value=='' || $('txtEmailAddress').value=='' || $('txtMobilePhone').value=='' || $('txtUsername').value==''){
                      alert('Please fill all fields marked by *');
                      log.removeClass('ajax-loading');
                      } else if($('txtPassword').value != $('txtPasswordConf').value){
                      alert('Both passwords must match');
                      log.removeClass('ajax-loading');
                      } else if($('oldPass').value != '' && ($('oldPass').value == $('txtPassword').value || $('txtPassword').value == '')){
                      alert('You must change your default password.');
                      log.removeClass('ajax-loading');
                      } else {
                      this.send({
                              update: log,
                              onComplete: function() {
                                      log.removeClass('ajax-loading');
                              }
                      });
                      }

              });
}

if($('payRentFrm')){
              $('payRentFrm').addEvent('submit', function(e) {
                      /**
                       * Prevent the submit event
                       */
                      new Event(e).stop();

                      risp = confirm('Make sure all your details are correct.\nIf you have any doubts press "cancel" then submit the form.');
                      if(risp==true){
                                            /**
                                             * This empties the log and shows the spinning indicator
                                             */
                                            $('submitEmail').disabled = true;
                                            $('resetPayment').disabled = true;
                                            var log = $('checkLoading').empty().addClass('ajax-loading');
                                            /**
                                             * send takes care of encoding and returns the Ajax instance.
                                             * onComplete removes the spinner from the log.
                                             */
                                            if($('txtName').value=='' || $('txtEmail').value=='' || $('txtWeeks').value=='' || $('txtWeeks').value==0 || $('txtCCName').value=='' || $('txtCC').value=='' || $('txtExpMonth').value=='' || $('txtExpYear').value=='' || $('txtCVV').value==''){
                                            alert('Please fill all fields marked by *');
                                            log.removeClass('ajax-loading');
                                            $('submitEmail').disabled = false;
                                            $('resetPayment').disabled = false;
                                            } else {
                                            this.send({
                                                    update: log,
                                                    onComplete: function() {
                                                    log.removeClass('ajax-loading');
                                                    if(log.innerHTML == 'done'){
                                                    location = pageroot+"Rent/TenantsArea";


                                                    }

                                                    }
                                            });
                                            }
                      }

              });
}

if($('inviaEmailFrm')){
              $('inviaEmailFrm').addEvent('submit', function(e) {
                      /**
                       * Prevent the submit event
                       */
                      new Event(e).stop();

                      /**
                       * This empties the log and shows the spinning indicator
                       */
                      //var log = $('emailResult').empty().addClass('ajax-loading');

                      var log = $('emailResult').empty().addClass('ajax-loading');
                      /**
                       * send takes care of encoding and returns the Ajax instance.
                       * onComplete removes the spinner from the log.
                       */
                      if($('txtNome').value=='' || $('txtEmail').value=='' || $('txtMessaggio').value==''){
                      alert('All fields marked by * are compulsory.');
                      log.removeClass('ajax-loading');
                      } else {
                      this.send({
                              update: log,
                              onComplete: function() {
                                      log.removeClass('ajax-loading');

                                     if($('emailResult').innerHTML=='The Email Address inserted is not valid.' || $('emailResult').innerHTML=='There has been an error sending your email. Please try again.'){
                                     $('txtEmail').focus();
                                     $('txtEmail').value = '';
                                     } else {
                                      setTimeout("hideEmailDiv()",2500);
                                     }

                              }
                      });
                      }

              });
}

if($('inviaFriendEmailFrm')){
              $('inviaFriendEmailFrm').addEvent('submit', function(e) {
                      /**
                       * Prevent the submit event
                       */
                      new Event(e).stop();

                      /**
                       * This empties the log and shows the spinning indicator
                       */

                      var log = $('emailFriendResult').empty().addClass('ajax-loading');
                      /**
                       * send takes care of encoding and returns the Ajax instance.
                       * onComplete removes the spinner from the log.
                       */
                      if($('txtFNome').value=='' || $('txtFriendNome').value=='' || $('txtFriendEmail').value=='' || $('txtFEmail').value=='' || $('txtFMessaggio').value==''){
                      alert('All fields marked by * are compulsory.');
                      log.removeClass('ajax-loading');
                      } else {
                      this.send({
                              update: log,
                              onComplete: function() {
                                      log.removeClass('ajax-loading');

                                     if($('emailFriendResult').innerHTML=='The Email Address inserted is not valid.' || $('emailFriendResult').innerHTML=='There has been an error sending your email. Please try again.'){
                                     $('txtEmail').focus();
                                     $('txtEmail').value = '';
                                     } else {
                                      setTimeout("hideFriendEmailDiv()",2500);
                                     }

                              }
                      });
                      }

              });
}

});

function hideDiv(div){
$(div).empty();
$(div).style.display = 'none';
}

function hideEmailDiv(){
document.getElementById('EmailDiv').style.display = 'none';
emailResult.innerHTML="";
document.inviaEmailFrm.reset();
}

function hideFriendEmailDiv(){
document.getElementById('FriendDiv').style.display = 'none';
emailFriendResult.innerHTML="";
document.inviaFriendEmailFrm.reset();
}

function showFriendDiv(){
document.getElementById('FriendDiv').style.display = 'block';

if($('EmailDiv').style.display=='block'){
hideEmailDiv();
}
}

function showEmailDiv(id){
document.getElementById('EmailDiv').style.display = 'block';

document.inviaEmailFrm.idConsulente.value = id;
if($('FriendDiv').style.display=='block'){
hideFriendEmailDiv();
}
}

function changeImg(what){
$('bigImageDiv').innerHTML = "<img src='"+what+"' width='650'>";
}


