// THIS FUNCTION RETURNS DATES WITH THE CORRECT SUFFIX IE 25th,2nd ETC

function getDaySuffix(dNum)
{
    var dateNum = dNum;
    daySuffix = 'th';
    if(dNum != 11 && dNum != 12 && dNum != 13)
    {
        if(dNum > 9) { dNum = dNum % 10; }
        if (dNum == 1) { daySuffix = 'st'; }
        if (dNum == 2) { daySuffix = 'nd'; }
        if (dNum == 3) { daySuffix = 'rd'; }
    }
    dateWithSuffix = dateNum + daySuffix;
    return dateWithSuffix;
}

function calc2Func(currentPrice, historicPrice){
if (window.location.search.indexOf("shares") != -1){
        var finds = window.location.search.split("shares=")[1].split("&amp;transform")[0];
        var sharesPurchased = finds;
        var valuePurchased = 0;
    } else if (window.location.search.indexOf("values") != -1){
        var findv = window.location.search.split("values=")[1].split("&amp;transform")[0];
        var valuePurchased = findv;
        var sharesPurchased = 0;
    } else {
        var valuePurchased = 0;
        var sharesPurchased = 0;
    }

    if ((sharesPurchased == '0') && (valuePurchased == '0')) {
        var valueChange = '0.00';
        var totalValue = '0.00';
    } else if (sharesPurchased != '0') {
        var valueChange = FormatNumber((((currentPrice - historicPrice) * sharesPurchased) / 100),'2','');
        var totalValue = FormatNumber(((currentPrice * sharesPurchased) / 100),'2','');
    } else if (valuePurchased != '0') {
        var valueChange = FormatNumber((Math.round(((valuePurchased / historicPrice) * currentPrice) - valuePurchased)),'2','');
        var totalValue = FormatNumber((Math.round((valuePurchased / historicPrice) * currentPrice)),'2','');
    }

    this.document.forms.historic_share_price_calc.valuePurchased.value = FormatNumber(valuePurchased,'2','');
    this.document.forms.historic_share_price_calc.sharesPurchased.value = sharesPurchased;
    this.document.forms.historic_share_price_calc.totalValue.value = FormatNumber(totalValue,'2','');
    var perctChange = FormatNumber((Math.round(((currentPrice / historicPrice) - 1) * 1000) / 10),'2','');
    this.document.forms.historic_share_price_calc.perctChange.value = perctChange;

    if ((this.document.forms.historic_share_price_calc.sharesPurchased.value != '') || (this.document.forms.historic_share_price_calc.valuePurchased.value != '')) {
        this.document.forms.historic_share_price_calc.valueChange.value = valueChange;
    } else {
        this.document.forms.historic_share_price_calc.valueChange.value = '0.00';
    }
}


function validateMovementForm(selectDayValue,selectMonthValue,selectYearValue,valuePurchased,sharesPurchased)
{
    if  (!validateDate(selectDayValue,selectMonthValue,selectYearValue))
    {
        return false;
    }
    if (sharesPurchased != parseInt(sharesPurchased) && isNaN(valuePurchased))
    {
        alert('Enter either the value of your holding at purchase or the number of shares purchased.');
        document.historic_share_price_calc.valuePurchased.focus();
        return false;
    } 
    else
    {
        return true;
    }
}


function getHistoricalSharePrice(selectDayValue,selectMonthValue,selectYearValue) 
{
    var lookupURL = "";
    
    if (validateDate(selectDayValue,selectMonthValue,selectYearValue))
    {
        lookupURL = '/servlet/HsPublic?context=ir.access.jsp&ir_client_id=415&ir_option=HISTORIC_PRICE&nav=investors&when='+selectDayValue+'-'+selectMonthValue+'-'+selectYearValue
    }
    else 
    {
        lookupURL = "#";
    }
    return lookupURL;
   
}

function validateDate(selectDayValue,selectMonthValue,selectYearValue)
{
    var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    var currentDate = new Date();
    var floatDate = new Date(gFLOAT_YEAR,gFLOAT_MONTH-1,gFLOAT_DATE);
    var submittedDate = new Date(selectYearValue,selectMonthValue-1,selectDayValue);
   
    if ((floatDate) && (floatDate > submittedDate))
    {
        alert('Please check the dates selected. The date cannot be before the flotation date of ' + getDaySuffix(floatDate.getDate()) + " " + (monthname[floatDate.getMonth()] + " ") + floatDate.getFullYear() + '.');
        return false;
    }
    else if (submittedDate > currentDate)
    {
        alert('Please check the dates selected. The date cannot be in the future.'); 
        return false;
    }
    else 
    {
        return true;
    }
}


function calculatePriceMovement(selectDayValue,selectMonthValue,selectYearValue,valuePurchased,sharesPurchased)
{
    
    windowURL = '/servlet/HsPublic?context=ir.access.jsp&ir_client_id=415&ir_option=HISTORIC_PRICE&nav=investors';
            
    sharesPurchased = parseInt(sharesPurchased);
    valuePurchased = parseFloat(valuePurchased);
    
      
    if (!isNaN(sharesPurchased) && sharesPurchased > 0) 
    {
  
        windowURL += '&when='+selectDayValue+'-'+selectMonthValue+'-'+selectYearValue+'&shares='+sharesPurchased+'#movement'
         
    } 
    else 
    {
        windowURL += '&when='+selectDayValue+'-'+selectMonthValue+'-'+selectYearValue+'&values='+valuePurchased+'#movement'
    }
    return windowURL;
}


function addNewOption(selectbox,text,value )
{
var optn = document.createElement("option");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}


function writeYear2(theYear, elementID){
  var d = new Date();
  y = d.getFullYear();

  startYear = theYear;



  for (i=startYear; i<(y+1) ; i++) {
     addNewOption(elementID,i, i); 
  }
}