﻿// JScript File
function SetFocusCtrl(sIdCtrl)
{
	document.getElementById(sIdCtrl).focus()
}

function EmptyValue(sIdCtrl)
{
	var strValue = "";
	strValue = document.getElementById(sIdCtrl).getAttribute("value");
	return ((strValue == "") || (strValue == null) || (strValue == "BLANK") || (strValue == "0")|| (strValue == "null"))
}

function CheckBrowser()
{
    var x;
    var oMainDiv;
    var iLeftMargin;
    x = window.screen.availWidth;
    
    if ( x > 1024 )
    {
        iLeftMargin = (x - 1024)/2;
        oMainDiv = document.getElementById("MainDiv");
        oMainDiv.style.left=iLeftMargin;
        oMainDiv.style.position = 'absolute';
    }            
}

function ConfirmDeleteCitation()
{
    return confirm( "Are you sure you want to delete this Litter Citation?" );
}

function ConfirmDeleteRecord()
{
    return confirm( "Are you sure you want to delete selected record(s)?" );
}

function ConfirmSubmission() {
    return confirm("Are you sure you want to submit the report?  Please note that submitting this form will not allow you to make any further changes.");
}

function ValidateCboIntID( source, args)
{
    var sID;
    
    sID = args.Value;
    
    if (sID == 0 || sID == "BLANK")
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}

function ValidateCboCharID( source, args)
{
    var sID;
    
    sID = args.Value;
    
    if (sID == 'BLANK')
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}

function CheckNumericValue(sCtrl)
{
    var sVal;
    sVal = document.getElementById(sCtrl).getAttribute("value");
    
    if (!isNumeric(sVal) )
    {
        alert("Please insert a valid numeric value!");
        document.getElementById(sCtrl).setAttribute("value", "0");
        SetFocusCtrl(sCtrl)
    }
}

function CheckIntegerValue(sCtrl)
{
    var sVal;
    sVal = document.getElementById(sCtrl).getAttribute("value");
    
    if (!isInteger(sVal) )
    {
        alert("Please insert a valid integer value!");
        document.getElementById(sCtrl).setAttribute("value", "0");
        SetFocusCtrl(sCtrl)
    }
}

function CheckYearValue(sCtrl)
{
    var sVal;
    sVal = document.getElementById(sCtrl).getAttribute("value");
    
    if (sVal.length != 4)
    {
        alert("Please insert a valid year!");
        document.getElementById(sCtrl).setAttribute("value", "");
        SetFocusCtrl(sCtrl)
    }
    
    if (!isInteger(sVal) )
    {
        alert("Please insert a valid year!");
        document.getElementById(sCtrl).setAttribute("value", "");
        SetFocusCtrl(sCtrl)
    }
}

function isNumeric(sValue)
{
    if (sValue == "")
    {
        return false;
    }
    
    return (!isNaN(sValue));
}

function isInteger(sValue)
{
    var nModulo;
    
    if (sValue == "")
    {
        return false;
    }
    
    nModulo = sValue % 1;
    
    if (nModulo == 0)
    {
        if (sValue >= 0 && sValue <= 2147483648)
        {
            return true;
        }
        else
        {
            return false;
        }
     }
     else
     {
        return false;
     }
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.
// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) 
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) 
    {
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) 
    { // check month range
        return false;
    }

    if (day < 1 || day > 31) 
    {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
    {
        return false;
    }

    if (month == 2) 
    { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        
        if (day > 29 || (day==29 && !isleap)) 
        {
            return false;
        }
    }
    return true; // date is valid
}


function CheckDate(sControl)
{
    var sValue;
    sValue = document.getElementById(sControl).getAttribute("value");
    
    if (sValue == "")
    {
        return;
    }
    
    if (!isDate(sValue))
    {
        alert("Please enter a valid date!");
        document.getElementById(sControl).setAttribute("value", "");
        SetFocusCtrl(sControl);
    }
}

function GetTodaysDate(sControl)
{
    var now = new Date();
    document.getElementById(sControl).setAttribute("text", now.getMonth() + "/" + now.getDay() + "/" + now.getFullYear());
}

function ExportToExcel()
{
    window.open("ExcelViewer.aspx");
    //window.open("ExcelViewer.xls");
}