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 changeConvenience(th){
switch(th){
case 'Visa' : fee = 1.6;
break;
case 'MasterCard' : fee = 1.6;
break;
case 'Diners' : fee = 4.35;
break;
case 'American Express' : fee = 4.35;
break;
default: fee = 1.6;
}

$('txtFee').value = fee;
if($('whatPaying').value == 'rentDiv'){
setTotal();
} else {
      if($('whatPaying').value == 'partDiv'){
      th = $('txtTotAmount').value;
      } else {
      th = $('txtOtherExpOther').value;
      }
setTotalVar(th);
}

}

function setTotal(){
perc = $('txtFee').value/100;

num = ($('txtTotRent').value*1 + $('txtOtherExpFull').value*1)*(1+perc);
num = Math.round(num*100)/100;

$('txtTotal').value = RoundNumber(num,2,true);
}

function setTotalVar(ths){
perc = $('txtFee').value/100;
num = (ths*1)*(1+perc);
num = Math.round(num*100)/100;
$('txtTotal').value = RoundNumber(num,2,true);
}

function showTenant(str){
strArr = str.split('§');

frm = eval('document.payRentFrm');
frm.txtName.value = strArr[0];
frm.txtAddress.value = strArr[2];
frm.txtSuburb.value = strArr[3];
frm.txtPostCode.value = strArr[4];
frm.txtState.value = strArr[5];
frm.txtRefNum.value = strArr[1];
frm.txtEmail.value = strArr[6];
frm.CurrentRent.value = strArr[7];
frm.PaymentFreq.value = strArr[8];
frm.paidtoDate.value = strArr[9];
frm.idUtente.value = strArr[10];
}

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').addClass('ajax-loading');
                      /**
                       * send takes care of encoding and returns the Ajax instance.
                       * onComplete removes the spinner from the log.
                       */
                      if($('txtNewsletterEmail').value=='' || $('txtNewsletterEmail').value=='Your Email Address'){
                      alert('Please insert an email address');
                      //log.removeClass('ajax-loading');
                      } else {


                                          this.set('send', {
                                   onComplete: function(response) {
                                                                alert(response);
                                                                          $('txtNewsletterEmail').value = 'Your Email Address';
                      }});

                      this.send();

                                          }

              });
}

});

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();
}
}


/* image loader */
function showPic(pic){
        $('imageHolder').innerHTML = "<img src='"+webroot+"images/uploads/content/"+pic+"_big.gif'><br><br><input type='button' value='close window' onclick=\"hideDiv('imageHolder')\" class='mainbuttons' /><br><br>";
        $('imageHolder').style.display = "block";

        }

/* end image loader */

/* cart functions */

function addToCart(id){
                                      var url = pageroot+"Cart/AddToCart/"+id;

                                      var req = new Request({
                                              method: 'POST',
                                              url: url,
                                              onSuccess: function(txt){
                                                                                                alert('The Item has been added to your shopping cart.');

                                                  $('itemsDiv').innerHTML = txt;
                                              }
                                      });
                                      req.send();
}

function deleteItem(id){
        risp = confirm('Are you sure you would like to remove this item from the cart?');

        if(risp==true){
                location = pageroot+"Cart/deleteItem/"+id;
                }
        }

function updateItems(id){
                                      var url = pageroot+"Cart/updateItem/"+id+"/"+$('txtQty'+id).value;

                                                                         if($('txtQty'+id).value==0){
                                                                         deleteItem(id);

                                                                         } else {
                                      var req = new Request({
                                              method: 'POST',
                                              url: url,
                                              onSuccess: function(txt){

                                                  $('itemsDiv').innerHTML = txt;
                                              }
                                      });
                                      req.send();
                                                                         }
}

function setNextDay(th){
        if(th.checked == true){
                pageVar = "SetNextDay";
                } else {
                pageVar = "UnsetNextDay";
                }

                                      var url = pageroot+"Cart/"+pageVar;

                                      var req = new Request({
                                              method: 'POST',
                                              url: url,
                                              onSuccess: function(){
                                                                               history.go();
                                              }
                                      });
                                      req.send();
}

/* end cart functions */

/* check out functions */
function selectCurrentAddress(id,th){

if(th.checked == false){
        $('txtCurrentAddress').value = '';
        $('txtCurrentAddress2').value = '';
        $('txtCurrentSuburb').value = '';
        $('txtCurrentState').value = '';
        $('txtCurrentPostcode').value = '';

        $('txtCurrentAddress').readOnly = false;
        $('txtCurrentAddress2').readOnly = false;
        $('txtCurrentSuburb').readOnly = false;
        $('txtCurrentState').readOnly = false;
        $('txtCurrentPostcode').readOnly = false;

        $('txtCurrentAddress').style.backgroundColor = '#ffffff';
        $('txtCurrentAddress2').style.backgroundColor = '#ffffff';
        $('txtCurrentSuburb').style.backgroundColor = '#ffffff';
        $('txtCurrentState').style.backgroundColor = '#ffffff';
        $('txtCurrentPostcode').style.backgroundColor = '#ffffff';
        } else {

                                      var url = pageroot+"Checkout/selectCurrentAddress/"+id;

                                      /**
                                       * The simple way for an Ajax request, use onRequest/onComplete/onFailure
                                       * to do add your own Ajax depended code.
                                       */

                                      var req = new Request({
                                              method: 'POST',
                                              url: url,
                                              onSuccess: function(txt){
                                                                                        var stringa = new String(txt);
                                                                                        Arr = stringa.split('*');
                                                                                        $('txtCurrentAddress').value = Arr[0];
                                                                                        $('txtCurrentAddress2').value = Arr[1];
                                                                                        $('txtCurrentSuburb').value = Arr[2];
                                                                                        $('txtCurrentState').value = Arr[3];
                                                                                        $('txtCurrentPostcode').value = Arr[4];

        $('txtCurrentAddress').readOnly = true;
        $('txtCurrentAddress2').readOnly = true;
        $('txtCurrentSuburb').readOnly = true;
        $('txtCurrentState').readOnly = true;
        $('txtCurrentPostcode').readOnly = true;

        $('txtCurrentAddress').style.backgroundColor = '#e0e0e0';
        $('txtCurrentAddress2').style.backgroundColor = '#e0e0e0';
        $('txtCurrentSuburb').style.backgroundColor = '#e0e0e0';
        $('txtCurrentState').style.backgroundColor = '#e0e0e0';
        $('txtCurrentPostcode').style.backgroundColor = '#e0e0e0';

                                              }
                                      });
                                      req.send();
        }

}

function setPaymentFields(what,idC){
        var url = pageroot+"Checkout/showPaymentFields/"+what+"/"+idC;

        var req = new Request({
                                              method: 'POST',
                                              url: url,
                                              onSuccess: function(txt){
                                                                                                $('paymentFields').innerHTML = txt;
                                                                                                }
                                      });
                                      req.send();
}

function printOrder(){
        $('tempOrder').innerHTML = "<p><img src='"+webroot+"images/home/whitelogo.gif'></p>";
        $('tempOrder').innerHTML += "<p><br /><br />Thank You for ordering. As soon as Your order is be processed, we will contact you with the delivery details.<br /><br /></p>";
        $('tempOrder').innerHTML += $('confirmOrderPrint').innerHTML;

        //print();

        }

function confirmAndPhone(){
        str = $('insClientFrm').action+"/1";
        $('insClientFrm').action = str;
        $('insClientFrm').submit();
        }
/* end checkout functions */
function rotateCarousel()
{

        setTimeout("stepcarousel.stepBy('mygallery', 1)",5000);
        repeatRotate();
}

function repeatRotate()
{
        setTimeout("rotateCarousel()",5000);
        }
		
function changeImg(what){
$('bigImageDiv').innerHTML = "<img src='"+what+"' width='500'>";
}