//****************************************************************************************************************
//** Page Name		: scripts.js
//** Description	: Client side scripting functions
//** Comment			: 
//** Author				: Jason Sammon
//** Last Revised	: 08/11/2005
//****************************************************************************************************************
function addToCart(returnurl, contentpageid, categoryid, productid, variationid, quantity, price, description)
{	
	var url = 'cart.aspx?cp='+contentpageid+'&action=addtocart&ru='+returnurl+'&catid='+categoryid+'&itemname=&quantity='+quantity+'&price='+price+'&itemid='+productid+'&itemvar1id='+variationid;
	//alert(url);
	
	location.href = url;	
}

function openPopup(url) 
{
	var myWin=window.open(url,"popup","status=no,toolbar=no,menubar=no,scrollbars=yes"); 
        return 1;
 } 
 
function onUpdatePrices()
{
	document.mainform.submit();
}

//this is an email enkoder from Hiveware http://www.hiveware.com
function emailService(){var i,j,x,y,x=
"x=\"783d22793e2331793e5d23376433313835376737353767333438363731353238653736" +
"37363835376638353367373737663838386737343833373a38363834363537363364333334" +
"393333346638343764373233363833383137393837373a3733373637343736353734653631" +
"373238643333373638353765373237673737373a3764386737343835376734363834336238" +
"343766383637363833386333663737373a3734376737343736356534643331373238673732" +
"343638353766333333673737373a346334673734383134635d23363834333c7a3e28663836" +
"37283c67706333663773296a3e34376737313c6a3d65366433792f6d66333331386f687569" +
"35373a383c6a2c3e35376437332a7c7a363465362c3e766f64333336667464623437363771" +
"6629286637353326282c79313732372f7476636633313774757329363765376a2d332a3237" +
"3a372a3c7e7a233c6b3e6677626d29792f64696273427529312a2a3c793e792f7476637475" +
"7329322a3c7a3e28283c677073296a3e313c6a3d792f6d666f6875693c6a2c3e392a7c7a2c" +
"3e792f747663747573296a2d352a3c7e677073296a3e353c6a3d792f6d666f6875693c6a2c" +
"3e392a7c7a2c3e792f747663747573296a2d352a3c7e7a3e7a2f747663747573296b2a3c22" +
"3b793d27273b783d756e6573636170652878293b666f7228693d303b693c782e6c656e6774" +
"683b692b2b297b6a3d782e63686172436f646541742869292d313b6966286a3c3332296a2b" +
"3d39343b792b3d537472696e672e66726f6d43686172436f6465286a297d79\";y='';for(" +
"i=0;i<x.length;i+=2){y+=unescape('%'+x.substr(i,2));}y";
while(x=eval(x));}


function GenericCalculation(isActual, daysTextBoxName, dailyRateTextBoxName, dailySurchargesTextBoxName, deliveryChargesTextBoxName, totalChargesExcVatTextBoxName, vatTextBoxName, totalChargesIncVatTextBoxName, ddlVATPerentageId)
{
    var days;
    var dailyRate;
    var dailySurcharges;
    var deliveryCharges;
    var totalExcVat;
    var vat;
    var totalIncVat;
    
    var daysElement = document.getElementById(daysTextBoxName);
    var dailyRateElement = document.getElementById(dailyRateTextBoxName);
    var dailySurchargesElement = document.getElementById(dailySurchargesTextBoxName);
    var deliveryChargesElement = document.getElementById(deliveryChargesTextBoxName);
    var totalChargesExcVatElement = document.getElementById(totalChargesExcVatTextBoxName);
    var vatElement = document.getElementById(vatTextBoxName);
    var totalChargesIncVatElement = document.getElementById(totalChargesIncVatTextBoxName);
    var vatPercentage = parseFloat(document.getElementById(ddlVATPerentageId).value);
    
    //used to get the real value of the total charged. Not round value;
    var totalTemp = parseFloat(document.getElementById('txtTotalExcVatTemp').value);
    
    if (daysElement.value =='0' || daysElement.value =='')
    {
       //do nothing
    }
    else
    {
        days = parseFloat(daysElement.value);
        
        if (dailyRateElement.value == '')
            dailyRate = 0;
        else
            dailyRate = parseFloat(dailyRateElement.value.replace(',',''));
            
        if (dailySurchargesElement.value == '')
            dailySurcharges = 0;
        else
            dailySurcharges = parseFloat(dailySurchargesElement.value.replace(',',''));
            
        if (deliveryChargesElement.value == '')
            deliveryCharges = 0;
        else
            deliveryCharges = parseFloat(deliveryChargesElement.value.replace(',',''));
        
        //Is Actual
        if (isActual)
        {
            //Get the total without do the calculation (days * daily rate).
            totalExcVat = (days*(dailySurcharges) + totalTemp)+deliveryCharges;
        }
        else
        {
            totalExcVat = (days*(dailyRate+dailySurcharges))+deliveryCharges;
        }
        totalExcVat = parseFloat(totalExcVat);
        
        if (totalExcVat != 0)
        {            
            vat = totalExcVat * (vatPercentage / 100);
            vat = parseFloat(vat);
            totalIncVat = vat + totalExcVat;
            totalIncVat = parseFloat(totalIncVat);
            
            var nf = new NumberFormat(totalExcVat);
            nf.setPlaces(2);
            nf.setSeparators(true);
            totalChargesExcVatElement.value = nf.toFormatted();
            
            nf.setNumber(vat);            
            vatElement.value = nf.toFormatted();
            
            nf.setNumber(totalIncVat);
            totalChargesIncVatElement.value = nf.toFormatted();
        }
        else
        {
            totalChargesExcVatElement.value = '';
            vatElement.value = '';
            totalChargesIncVatElement.value = '';
        }        
    }
    
    //To be used to calculate the percentage
    var totalChargesActualValue = document.getElementById('txtTotalChargesIncVAT').value.replace(',','');
    var totalChargesPropValue = document.getElementById('txtPropTotalChargesIncVAT').value.replace(',','');
    var percentageElement = document.getElementById('txtPropPercentage');
    
    if (totalChargesActualValue != '' && totalChargesPropValue != '')
    {
        var actual = parseFloat(totalChargesActualValue);
        var prop = parseFloat(totalChargesPropValue);
        
        var percente = ((prop / actual) * 100.0000);
        
        var nf = new NumberFormat(percente);
        nf.setPlaces(1);
        percentageElement.value = nf.toFormatted();
    }
    else
    {
        percentageElement.value = ''        
    }
}

function CalculateProposed()
{
    GenericCalculation(false, 'txtPropDays', 'txtPropDailyRate', 'txtPropDailySurcharges', 'txtPropDeliveryCharges', 'txtPropTotalChargesExcVat', 'txtPropVAT', 'txtPropTotalChargesIncVAT', 'ddlVATPercentageProposed');
}

function Calculate()
{
    GenericCalculation(true, 'txtDays', 'txtDailyRate', 'txtDailySurcharges', 'txtDeliveryCharges', 'txtTotalChargesExcVat', 'txtVAT', 'txtTotalChargesIncVAT', 'ddlVATPercentage');    
}

function NumbersOnly(e)
{
  var unicode=e.charCode? e.charCode : e.keyCode
  if (unicode!=8)
  { //if the key isn't the backspace key (which we should allow)
    if (unicode<48||unicode>57) //if not a number
    {
        alert('Not number');
      return false //disable key press
    }
  }
}

function DecimalNumbers(e)
{
  var unicode=e.charCode? e.charCode : e.keyCode;
  //alert( "code: " + unicode);
  
  if (unicode!=8)
  { //if the key isn't the backspace key (which we should allow), dot key or tab key
  //&& (unicode!=44)
    if ((unicode<48||unicode>57)  && (unicode!=46) && (unicode!=9) )//if not a number
    {
       // alert('Not Number'  + "code: " + unicode);
        return false //disable key press
     }
  }
}

function PrintIrisResult()
{
  if(document.all) 
  {
    if(document.all.divButtonsBottom)
        document.all.divButtonsBottom.style.visibility = 'hidden';
    if (document.all.divButtonsTop)
        document.all.divButtonsTop.style.visibility = 'hidden';
    if (document.all.divCalculationsHideButton)
        document.all.divCalculationsHideButton.style.visibility = 'hidden'; 
    window.print();
    if(document.all.divButtonsBottom)
        document.all.divButtonsBottom.style.visibility = 'visible';
    if (document.all.divButtonsTop)
        document.all.divButtonsTop.style.visibility = 'visible';
    if (document.all.divCalculationsHideButton)
        document.all.divCalculationsHideButton.style.visibility = 'visible'; 
  } 
  else 
  {
    if(document.getElementById('divButtonsBottom'))
        document.getElementById('divButtonsBottom').style.visibility = 'hidden';
    if (document.getElementById('divButtonsTop'))   
        document.getElementById('divButtonsTop').style.visibility = 'hidden';
    if (document.getElementById('divCalculationsHideButton'))   
        document.getElementById('divCalculationsHideButton').style.visibility = 'hidden';
    window.print();
    if(document.getElementById('divButtonsBottom'))
        document.getElementById('divButtonsBottom').style.visibility = 'visible';
    if (document.getElementById('divButtonsTop'))   
        document.getElementById('divButtonsTop').style.visibility = 'visible';
    if (document.getElementById('divCalculationsHideButton'))   
        document.getElementById('divCalculationsHideButton').style.visibility = 'visible';
  }
}

function Scroll()
{

	if(document.forms[0].txtScroll.value > 0)
	{
		scrollTo(0, parseInt(document.forms[0].txtScroll.value));
	}
}

function SaveScroll()
{
	document.forms[0].txtScroll.value = document.documentElement.scrollTop;
}

function togglenoteshide()
{    
    var elem = null;
    var elemButton = null;
    if (document.all)
    {
        elem = document.all.noteshide;
        elemButton = document.all.noteshidebutton;
    }
    else
    {
        elem = document.getElementById("noteshide");
        elemButton = document.getElementById("noteshidebutton");
    }
    
    if (elem)
    {
        if (elem.style.display == "none")
        {
            elem.style.display = "";
        }
        else
        {
            elem.style.display = "none";
        }
    }
    
    if (elemButton)
    {
        if (elemButton.value == "View notes")
        {
            elemButton.value  = "Hide notes";
        }
        else
        {
            elemButton.value = "View notes";
        }
    }
}

function togglecalculationhide()
{    
    var elemDiv = null;
    var elemButton = null;
    if (document.all)
    {
        elemDiv = document.all.divCalculator;
        elemButton = document.all.btncalculationshide;
    }
    else
    {
        elemDiv = document.getElementById("divCalculator");
        elemButton = document.getElementById("btncalculationshide");
    }
    
    if (elemDiv)
    {
        if (elemDiv.style.display == "none")
        {
            elemDiv.style.display = "";
        }
        else
        {
            elemDiv.style.display = "none";
        }
    }
    
    if (elemButton)
    {
        if (elemButton.value == "View Calculator Features")
        {
            elemButton.value  = "Hide Calculation Features";
        }
        else
        {
            elemButton.value = "View Calculator Features";
        }
    }
}
