function URLencode(sStr) {
    return escape(sStr)
        .replace(/\+/g, '%2B')
        .replace(/\"/g,'%22')
        .replace(/\'/g, '%27')
}


$(document).ready(function() {

  if($('#calculations').length>0)
  {
    $('#calculations').hide()
    $('input[name="heads"]').focus()
  }

	// home
	$(".homeFeature").click(function() {
		window.location = $("h3 a",this).attr("href");
	});
	$(".homeFeature").mouseover(function() {
		$(this).css("background-position","0 -144px");
		$(this).css("cursor","pointer");
	});
	$(".homeFeature").mouseout(function() {
		$(this).css("background-position","0 0");
		$(this).css("cursor","pointer");
	});

    if($("#pageTitle").html() == "Distributors")
    {
        $('#id_country').change(function (){
            var c = $('#id_country').attr('value')
            if(c != '' && c !=undefined)
                window.location = '/distributors/international/' + c
        })
    }

    if($(".title_crumb a:first").html() == "Distributors")
    {
        $('#id_country').change(function (){
            var c = $('#id_country').attr('value')
            if(c != '' && c !=undefined)
                window.location = '/distributors/international/' + c
        })
    }
	
	//category
	$(".range, .product, .relatedProduct , .basketProduct").click(function() {
		window.location = $("a",this).attr("href");
	});
	$(".range, .product, .relatedProduct, .basketProduct").mouseover(function() {
		$(this).css("cursor","pointer");
	});
	$(".rightFeature").click(function() {
		window.location = $("h3 a",this).attr("href");
	});
	$(".rightFeature").mouseover(function() {
		$(this).css("background-position","0 -144px");
		$(this).css("cursor","pointer");
	});
	$(".rightFeature").mouseout(function() {
		$(this).css("background-position","0 0");
		$(this).css("cursor","pointer");
	});
	
	/* Products */
	$(".btn").mouseover(function() {
		if($.browser.msie) {
			$(this).css("opacity",".8");
		}			
	});
	$(".btn").mouseout(function() {
		if($.browser.msie) {
			$(this).css("opacity","1");
		}	
	});
	$("#productLarge").prepend('<a href="sdf" class="back">Back</a>').css("display","none");
	
	$("#productImg").click(function() {
		$(".productCol1,.productCol2").fadeOut("slow",function() {$("#productLarge").fadeIn("slow").css("cursor","pointer")});
		return false;
	});
	$("#productLarge").click(function() {
		$("#productLarge").slideUp("slow",function() {$(".productCol1,.productCol2").fadeIn("slow")});
		return false;
	});
	
	/* News */
	$("a.bigBtn").mouseover(function() {
		$(this).css("cursor","pointer");
		if(!$.browser.msie || $.browser.version != "6.0") {
        $(this).css("opacity",".8");		
		}
	});
	$("a.bigBtn").mouseout(function() {
		if(!$.browser.msie || $.browser.version != "6.0") {
        $(this).css("opacity",".9999");	
		}	
	});
	
	/* Headings */
	if(document.getElementById("pageTitle")) {
	if(!$.browser.msie || $.browser.version != "5.5") {
		txt = escape($("#content h2").html());
		var so = new SWFObject("/site_media/swf/h2.swf?txt="+txt, "h2", "800", "25", "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.write("pageTitle");
	}
	}

    /* Distributors */
    initial_values = []
    $('input, select, textarea').each(function(){
        initial_values[this.id] = this.value
    })
    $('#id_postcode').focus(function() {
        //this.select()
    })


  /* calculator */
	$("#calculate").click(function() {
	  
    // h = number of heads washed per week
    h = $('[name=heads]').val()
    if(h == '')
      alert('Please do a head count')
      

     switch (isInteger(h))
     {
         case true:
            
            /* calculate savings
              
                                     number of hairfoils used per week and per year – based on 52 weeks – (number of heads x 40)
                                     amount of time saved in man hours per week and year (number of heads x 8 minutes) displayed in hours and minutes if possible
                                     the amount of money saved on a weekly and an annual basis (number of man hours x £5 per hour)
                                */

            var hf_week = h * 40
            // hairfoils per year
            var hf_year = 52 * hf_week

            html = "<p>You are using approximately <strong>"+hf_week+"</strong> hair foils per week, which amounts to <strong>"+hf_year+"</strong> per year.</p> \
                    <p>By switching to the 24*7 you can save up to:</p><br />"

            $('#summary').html(html)


            $('#hw').html('<p>' + hf_week + ' per week</p>')
            $('#hy').html('<p>' + hf_year + ' per year</p>')


            // time saved per week and year in MINUTES
            var tsm_week = h * 8  // heads * 8
            var tsm_year = h * 52 * 8

            // time saved per week and year in HOURS and MINS
            tw = minutes_and_hours(tsm_week)

            if(tw.hours != 0)
              var tsh_week = tw.hours + ' hours and ' + tw.minutes + ' mins'
            else
              var tsh_week = tw.minutes + ' mins'   
            $('#tpw').html('<p>' + tsh_week + ' per week</p>')

            ty = minutes_and_hours(tsm_year)
  
            var tsh_year = ty.hours + ' hours and ' + ty.minutes + ' mins'
            $('#tpy').html('<p>' + tsh_year + ' per year</p>')

	    // wastage saved - 3.2p per head of 
	    var waste_per_week = (3.2 * h)/100
	    var waste_per_year = waste_per_week * 52
	    
	    $('#w_mpw').html('<p>&pound;' + round(waste_per_week,2) + ' per week</p>')
            $('#w_mpy').html('<p>&pound;' + round(waste_per_year,2) + ' per year</p>')
    
            // money saved
            if(tw.hours == 0)
              var ms_week = (tw.minutes/60) * 5            
            else
              var ms_week = tw.hours * 5 + (tw.minutes/60) * 5

            // var ms_week = tw.hours * 5
            var ms_year = ms_week * 52     

            $('#mpw').html('<p>&pound;' + round(ms_week,2) + ' per week</p>')
            $('#mpy').html('<p>&pound;' + round(ms_year,2) + ' per year</p>')

            $('#total_pw').html('<p>&pound;' + round(ms_week+waste_per_week,2) + ' per week</p>')
            $('#total_py').html('<p>&pound;' + round(ms_year+waste_per_year,2) + ' per year</p>')
	    
	    

            $('#visitstore').html('<a href="/dispenser_shop/" class="floatleft"><img src="/site_media/images/visit.gif" alt="Visit store" /></a><img src="/site_media/images/piggy-bank.jpg" alt="piggy bank" id="piggy" />')

            $('#calculations').fadeIn()
      
            break;

         case false:
            alert("Please enter an integer")
            $('[name=heads]').val('')
     }


    return false
  
  });

});

function order_popup(id) 
{ 
        var order_url = $('.print_btn').attr('href');
        order_win = window.open(order_url ,
                                "ord", 
                                "width=500,height=550, resizable=1, scrollbars=1");
}

function round(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

 function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }



function minutes_and_hours(mins)
{
  if(mins>60)
  {
    var hours = Math.floor(mins/60)
    var decimal = (mins/60) - hours
    var mins = mins - (hours*60)
    time = { 'hours' : hours, 'minutes': mins };
    return time
  }
  else
  {
    time = { 'hours' : 0, 'minutes': mins };
    return time
  }
}















