﻿
/* Restores the state of the check box list 
when popup is closed without hitting Cancel */
function resetSelections(hdnId, lstId)
{
	var hdn = document.getElementById(hdnId);
	if(hdn == null)
	{
		return;
	}
	var i = 0;	
	var selectedItems = hdn.value;
	var items = selectedItems.split(",");
	for(i = 0; i < items.length; i++)
	{
		var objItem = document.getElementById(lstId + "_" + i);
		if(objItem == null)
		{
			continue;
		}
		objItem.checked = parseInt(items[i], 10);
	}
}

/* Fires when Auto. Min or Auto. Max checkbox is clicked. 
Restores default Min./Max. and resets error indicator */
function OnAxisAutomaticClick(chkId, txtId, origTxt, lblId)
{
    var chk = document.getElementById(chkId);
    var txt = document.getElementById(txtId);
    var lbl = document.getElementById(lblId);
    if(chk == null || txt == null || lbl == null)
	{
		return;
	}
    if (chk.checked)
    {
        txt.value = origTxt;
        txt.disabled = true;
        lbl.style.display = "none";
    }
    else
    {
        txt.disabled = false;
    }
}

/* Restores the state of scale settings list
when popup is closed without hitting Cancel */
function resetScales(hdnId, modId)
{
    var hdn = document.getElementById(hdnId);
    if(hdn == null)
	{
		return;
	}
	var settings = hdn.value;
	var items = settings.split(",");
    var count = parseInt(items[0], 10);
    var autoMin;
    var axisMin;
    var autoMax;
    var axisMax;
    var chkMin;
    var txtMin;
    var lblMin;
    var chkMax;
    var txtMax;
    var lblMax;
    var idx;

    for (i = 1; i <= count; i++)
    {
        autoMin = items[((i - 1) * 4) + 1];
        axisMin = items[((i - 1) * 4) + 2];
        autoMax = items[((i - 1) * 4) + 3];
        axisMax = items[((i - 1) * 4) + 4];
       
        if (i <= 9)
            {idx = '0' + ((i * 1 + 1));}
        else
            {idx = '' + ((i * 1 + 1));}
            
        chkMin = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_chkAutomaticMinimum');
        txtMin = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_txtAxisMinimum');
        lblMin = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_lblAxisMinimum');
        chkMax = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_chkAutomaticMaximum');
        txtMax = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_txtAxisMaximum');
        lblMax = document.getElementById('dnn_ctr' + modId + '_Chart_grdScales_ctl' + idx + '_lblAxisMaximum');
        
        if(chkMin == null || txtMin == null || lblMin == null || chkMax == null || txtMax == null || lblMax == null)
		{
			continue;
		}
		else
		{
            if (autoMin == 1)
            {
                chkMin.checked = true;
                txtMin.disabled = true;  
            }
            else
            {
                chkMin.checked = false;
                txtMin.disabled = false;
            } 
            txtMin.value = axisMin;
        
            if (autoMax == 1)
            {
                chkMax.checked = true;
                txtMax.disabled = true;  
            }
            else
            {
                chkMax.checked = false;
                txtMax.disabled = false;
            } 
            txtMax.value = axisMax;
            
            lblMin.style.display = "none";
            lblMax.style.display = "none";
        }
    }
}

/* Stores the dimensions of the popup on page load */
function setPopupDimensions(hdnId, popupId)
{
    var hdn = document.getElementById(hdnId);
    var popup = document.getElementById(popupId);
    if(hdn == null || popup == null)
	{
		return;
	}
	hdn.value = popup.offsetHeight + "," + popup.offsetWidth;
}

/* Retrieves original popup height */
function getPopupHeight(hdnId)
{
    var hdn = document.getElementById(hdnId);
    if(hdn != null)
	{
		var dimensions = hdn.value;
	    var items = dimensions.split(",");
        if (items.length == 2)
        {
            if (!isNaN(parseInt(items[0], 10)))
            {
                return parseInt(items[0], 10);
            }
        }
	}
	return 300;
}

/* Retrieves original popup width */
function getPopupWidth(hdnId)
{
    var hdn = document.getElementById(hdnId);
    if(hdn != null)
	{
		var dimensions = hdn.value;
	    var items = dimensions.split(",");
	    if (items.length == 2)
        {
            if (!isNaN(parseInt(items[1], 10)))
            {
                return parseInt(items[1], 10);
            }
        }
	}
	return 150;
}

/* Helper function used in isDate() */
function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
        {
        return false;
        }
    }
    return true;
}

/* Helper function used in isDate() */
function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
        {
            returnString += c;
        }
    }
    return returnString;
}

/* Helper function used in isDate() */
function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

/* Helper function used in isDate() */
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11) 
		{
		    this[i] = 30;
        }
		if (i == 2)
		{
		    this[i] = 29;
        }
   } 
   return this;
}

/* Validates whether a string is a valid date in the format dd/mm/yyyy */
function isDate(dtStr)
{
    var dtCh = "/";
    var minYear = 1000;
    var maxYear = 9999;
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
	var strDay = dtStr.substring(0, pos1);
	var strMonth = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1);
	
	var strYr = strYear;
	
	if (strDay.charAt(0) == "0" && strDay.length > 1) 
	{
	    strDay = strDay.substring(1);
    }
	
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) 
	{
	    strMonth=strMonth.substring(1);
    }
	
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0) == "0" && strYr.length > 1)
		{
		    strYr = strYr.substring(1);
		}
	}
	
	var month = parseInt(strMonth, 10);
	var day = parseInt(strDay, 10);
	var year = parseInt(strYr, 10);
	
	if (pos1 == -1 || pos2 == -1)
	{
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12)
	{
		return false;
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
	{
		return false;
	}
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
	{
		return false;
	}
	if (dtStr.indexOf(dtCh, pos2 + 1)!= -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
	{
		return false;
	}
    return true;
}

/* Custom DateAdd property for date object */
Date.prototype.Add = function(sInterval, iIncrement) 
{
    var sValidValues = " Y M D h m s uY uM uD uh um us ";
    
    if (sInterval.indexOf(sInterval) == -1) 
    {
        throw("Date.Add: Interval parameter must be: Y, M, D, h, m, s, uY, uM, uD, uh, um or us");
    }
    if (typeof(iIncrement) != "number")
    {
        throw("Date.Add: Increment parameter must be a number");
    }
    
    switch(sInterval)
    {
        case "Y":
        this.setYear(parseInt(this.getYear(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "M":
        this.setMonth(parseInt(this.getMonth(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "D":
        this.setDate(parseInt(this.getDate(), 10) + parseInt(iIncrement, 10));
        break;
    
        case "h":
        this.setHours(parseInt(this.getHours(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "m":
        this.setMinutes(parseInt(this.getMinutes(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "s":
        this.setSeconds(parseInt(this.getSeconds(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "uY":
        this.setUTCFullYear(parseInt(this.getUTCFullYear(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "uM":
        this.setUTCMonth(parseInt(this.getUTCMonth(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "uD":
        this.setUTCDate(parseInt(this.getUTCDate(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "uh":
        this.setUTCHours(parseInt(this.getUTCHours(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "um":
        this.setUTCMinutes(parseInt(this.getUTCMinutes(), 10) + parseInt(iIncrement, 10));
        break;
        
        case "us":
        this.setUTCSeconds(parseInt(this.getUTCSeconds(), 10) + parseInt(iIncrement, 10));
        break;
    }
    return this;
}

/* Converts a string to a date object - assumes dtStr is already in dd/mm/yyyy or dd/mm/yyyy hh:mm:ss format */
function getNewDate(dtStr)
{
    var dtCh1 = "/";
	var pos1 = dtStr.indexOf(dtCh1);
	var pos2 = dtStr.indexOf(dtCh1, pos1 + 1);
	var strDay = dtStr.substring(0, pos1);
	var strMonth = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1);
	
	var strYr = strYear;
	
	if (strDay.charAt(0) == "0" && strDay.length > 1) 
	{
	    strDay = strDay.substring(1);
    }
	
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) 
	{
	    strMonth = strMonth.substring(1);
    }
	
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0) == "0" && strYr.length > 1)
		{
		    strYr = strYr.substring(1);
		}
	}
	
	var monthIdx = (parseInt(strMonth, 10) -1);
	var day = parseInt(strDay, 10);
	var year = parseInt(strYr, 10);
	
	var dtCh2 = " ";
    var dtCh3 = ":";
    var pos3 = dtStr.indexOf(dtCh2)
    var pos4 = dtStr.indexOf(dtCh3, pos3 + 1);
    var pos5 = dtStr.indexOf(dtCh3, pos4 + 1);
    var strHour = '';
    var strMinute = '';
    var strSecond = '';
    var hour = 0;
    var minute = 0;
    var second = 0;

    if (pos3 != -1 && pos4 != -1 && pos5 != -1)
    {
        strHour = dtStr.substring(pos3 + 1, pos4);
        strMinute = dtStr.substring(pos4 + 1, pos5);
        strSecond = dtStr.substring(pos5 + 1);

        if (strHour.charAt(0) == "0" && strHour.length > 1) 
        {
            strHour = strHour.substring(1);
        }

        if (strMinute.charAt(0) == "0" && strMinute.length > 1) 
        {
            strMinute = strMinute.substring(1);
        }

        if (strSecond.charAt(0) == "0" && strSecond.length > 1) 
        {
            strSecond = strSecond.substring(1);
        }

        hour = parseInt(strHour, 10);
        minute = parseInt(strMinute, 10);
        second = parseInt(strSecond, 10);
    }

	var dt = new Date();
	dt.setFullYear(year, monthIdx, day);
    dt.setHours(hour);
    dt.setMinutes(minute);
    dt.setSeconds(second);
    
    return dt;
}

/* DateDiff function */
function dateDiff(interval, dt1, dt2)
{
	if(!interval || !dt1 || !dt2) 
	{
	    return;
    }
	
	var v;
	var s = 1;
	var m = 1;
	var h = 1;
	var dd = 1;
	var i = interval;
	
	if(i == "month" || i == "year")
	{
		dt1 = new Date(dt1);
		dt2 = new Date(dt2);
		years = dt2.getFullYear() - dt1.getFullYear();
		
		if (i == "year")
		{
		    v = years;
        }
		else if(i == "month")
		{
			v=(dt2.getMonth() + 1) - (dt1.getMonth() + 1);
			if(years != 0) 
			{
			    v += (years * 12);
            }
		}
	}
	else if (i == "second" || i == "minute" || i == "hour" || i == "day")
	{
		dt1 = Date.parse(dt1);
		dt2 = Date.parse(dt2);
		if(isNaN(dt1) || isNaN(dt2))
		{
		    return;
        }
		v = dt2 - dt1;
		if(i == "second")
		{
		    s = 1000;
        }
		if(i == "minute")
		{
		    s = 60000;
        }
		if(i == "hour")
		{
		    s = 60000; 
		    m = 60
        }
		if(i == "day")
		{
		    s = 60000;
		    m = 60;
		    h = 24;
        }
		v = ((((v / s) / m) / h) / dd);
	}
	return v;
}

/* Custom validator client validation function */
function valIsDate(source, args)
{
    var dtStr = trim(args.Value);
    if (dtStr != "")
    {
        args.IsValid = isDate(dtStr);
        return;
    }
    args.IsValid = true;
}

/* Returns time interval in hours (e.g. '1 D' will return 24) */
function getTimespanHours(str)
{
    var iTimespan = 0;
    var items = str.split(" ");
  
    if (items.length == 2)
    {
        var iNum = 0;
        if (!isNaN(parseInt(items[0], 10)))
        {
            iNum = parseInt(items[0], 10); 
        }
        var sInterval = items[1];
       
        switch(sInterval)
        {
            case "hour":
            iTimespan = iNum;
            break;
            
            case "day":
            iTimespan = (iNum * 24);
            break;
            
            case "week":
            iTimespan = (iNum * 24 * 7);
            break;
            
            case "year":
            iTimespan = (iNum * 365 * 7);
            break;
        }
    }
    return iTimespan;
}

/* LTrim (removes leading whitespace) string function */
function ltrim(str)
{ 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}

/* RTrim (removes trailing whitespace) string function */
function rtrim(str) 
{
	for(var j = str.length -1; j>=0 && isWhitespace(str.charAt(j)); j--);
	return str.substring(0, j + 1);
}

/* Trim (removes leading and trailing whitespace) string function */
function trim(str) 
{
	return ltrim(rtrim(str));
}

/* Checks whether charToCheck is white space */
function isWhitespace(charToCheck) 
{
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

/* Checks whether validity check value and operator are valid */
function validityCheckIsValid(selId, valId)
{
    var isValid = true;
	var selOperators = document.getElementById(selId);
	var op = selOperators.options[selOperators.selectedIndex].value;
	var ctlVal = document.getElementById(valId);
	var val = ctlVal.value;
	if (op.length > 0 && val.length == 0 || val.length > 0 && op.length == 0)
	{
	    isValid = false;
    }
	return isValid;
}