function getShortStrMonth(month)
{
    var months = new Array(12);
    months[0]  = "Jan";
    months[1]  = "Feb";
    months[2]  = "Mar";
    months[3]  = "Apr";
    months[4]  = "May";
    months[5]  = "Jun";
    months[6]  = "Jul";
    months[7]  = "Aug";
    months[8]  = "Sep";
    months[9]  = "Oct";
    months[10] = "Nov";
    months[11] = "Dec";

    month = month - 1;

    return months[month];
}

function getRound(value,roundToNearest)
{
    value = eval(value);
    roundToNearest = eval(roundToNearest);
    var returnValue = value;

    if (value <= 0)
        return 0;

    if(roundToNearest == 0)
        return (eval(returnValue)).toFixed(2);

    var returnValue = (Math.round(value / roundToNearest)*roundToNearest);

    if (returnValue<roundToNearest)
        return roundToNearest;

    returnValue = returnValue.toFixed(2);

    return returnValue*1.00;
}

function trim(inputString)
{
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
function updateEndDateAndCost(selectedmonths,totalcost,expirydate,totalSpRound,monthlyCost,proratedCost,setupcost)
{
    try
    {
        var monthsSelected=document.AddHostingMailStep2Form.noofmonths.value;

        var curdate = new Date();

        // +1 because javascript month starts from 0, +1 beause we want to skip the
        // current month
        var month = parseInt(curdate.getMonth()) + 1;

        var year  = parseInt(curdate.getYear());
        if(year<1900)
        {
            year = year+1900;
        }
        var totalmonth = month+parseInt(monthsSelected);

        if(totalmonth > 12)
        {
            month = parseInt(totalmonth%12);
            year  = year + parseInt(totalmonth/12);
            if(month==0)
            {
                month = 12;
                year = year - 1;
            }
        }
        else
            month=totalmonth;
		
        if(document.AddHostingMailStep2Form.productkey.value == 'lhbus'
            || document.AddHostingMailStep2Form.productkey.value == 'w2kus'
            || document.AddHostingMailStep2Form.productkey.value == 'mailboxus' )
            {
            var day = parseInt(curdate.getDate());
            var expiredate = getShortStrMonth(month)+ ' ' + day + ', ' + year ;
            monthlyCost = hosting_prices[monthsSelected];
        }
        else
        {
            if (parseInt(curdate.getDate())!=1)
            {
                month = month + 1;
            }
            var expiredate = getShortStrMonth(month)+ ' 01, ' + year ;
        }
        expirydate.innerHTML = expiredate;
        var totalSp =  monthlyCost*monthsSelected;
        totalSp = eval(totalSp) + setupcost;
        totalSp = getRound(totalSp,totalSpRound);
        totalcost.innerHTML = addCommas(round_decimals(totalSp,2));
    }
    catch(e)
    {
        alert(e);
    }
    return;
}
function updateEndDateAndCost_digicert(selectedmonths,totalcost,expirydate,totalSpRound,monthlyCost,proratedCost,setupcost,digi_cert_arr,license_detail_arr)
{
    try
    {
        var monthsSelected,curdate;
        var day,year,month,totalmonth,totalcost_calculated;
        var expiredate,digicert_price,addlicense_price;
        monthsSelected=document.AddHostingMailStep2Form.noofmonths.value;
        yearSelected=monthsSelected;
        if(monthsSelected==1)
            monthsSelected=12;
        else
            monthsSelected=24;
        curdate = new Date();
        day =  1;

        // +1 because javascreipt month starts from 0, +1 beause we want to skip the
        // current month
        month = parseInt(curdate.getMonth()) + 1;

        if (parseInt(curdate.getDate())!=1)
        {
            month = month + 1;
        }

        year  = parseInt(curdate.getYear());
        if(year<1900)
        {
            year = year+1900;
        }
        totalmonth = month+parseInt(monthsSelected);

        if(totalmonth > 12)
        {
            month = parseInt(totalmonth%12);
            year  = year + parseInt(totalmonth/12);
            if(month==0)
            {
                month = 12;
                year = year - 1;
            }
        }
        else
            month=totalmonth;

        expiredate = getShortStrMonth(month)+" 01, " + year ;
        expirydate.innerHTML = expiredate;
        digicert_price = digi_cert_arr[yearSelected];
        addlicense_price = license_detail_arr[yearSelected];
        totalcost_calculated = round_decimals(parseFloat(digicert_price)+parseFloat(setupcost),2);
        totalcost.innerHTML = addCommas(totalcost_calculated);

        if(document.AddHostingMailStep2Form.add_license.value != 0)
        {
            addlicense_price_temp = parseFloat(addlicense_price) * document.AddHostingMailStep2Form.add_license.value;
            total_calculated_price=addCommas(round_decimals(parseFloat(totalcost_calculated)+parseFloat(addlicense_price_temp),2));
        }
        else
        {
            total_calculated_price=totalcost_calculated;
        }
        document.getElementById('addlicense_price').innerHTML = addlicense_price;
        document.getElementById('sellingcost').innerHTML = total_calculated_price;
    }
    catch(e)
    {
    }
    return;
}
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}