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

sfHover = function() {
        var sfEls = document.getElementById('nav').getElementsByTagName('LI');
        for (var i=0; i<sfEls.length; i++) {
                sfEls[i].onmouseover=function() {
                        this.className+=" sfhover";
                }
                sfEls[i].onmouseout=function() {
                        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
                }
        }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

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 = '';
                                      $('txtNewsletterName').value = '';
                                      $('optNewsletterState').value = '';
                                      setTimeout("hideDiv('newsLetterResponse')",2500);
                              }
                      });
                      }

              });
}


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($('tellFriendForm')){
              $('tellFriendForm').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($('txtTellFriendName').value=='' || $('txtTellFriendEmail').value=='' || $('txtTellFriendYourName').value=='' || $('txtTellFriendYourEmail').value=='' || $('txtTellFriendMessage').value==''){
                      alert('All fields are compulsory.');
                      log.removeClass('ajax-loading');
                      } else {
                      this.send({
                              update: log,
                              onComplete: function() {
                                      log.removeClass('ajax-loading');

                                     if($('emailFriendResult').innerHTML=='The Email Addresses inserted is not valid.' || $('emailFriendResult').innerHTML=='There has been an error sending your email. Please try again.'){
                                     $('txtTellFriendEmail').focus();
                                     $('txtTellFriendEmail').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(){
$('emailFriendResult').empty();
document.tellFriendForm.reset();
}

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

function showTermsConditions(id){
window.open(pageroot+'Competition/TermsAndConditions/'+id,'_blank','width=500,height=500,scrollbars=yes,toolbar=no,resize=no,menubar=no,status=no');
}


function submitEntryForm(){
                      if($('txtName').value=='' || $('txtEmail').value=='' || $('txtTelephone').value=='' || $('txtSuburb').value=='' || $('txtState').value=='' || $('txtBarcode').value==''){
                            alert('All fields are compulsory.');
                      } else if($('txtBarcode').value != $('chkComp').value){
                             alert('The Barcode inserted is not correct.\nPlease make sure you insert all 13 digits of the barcode');
                      } else {
                            $('insCompetitionEntriesFrm').action = pageroot+'Competition/insEntry';
                            $('insCompetitionEntriesFrm').submit();
                      }
}
