var selectedRow = null;

// Method changes color of row on MouseOver event
function MouseOver(row)
{
   row.style.color = '#FF0000';  //Red
   row.style.cursor='pointer';
}

// Method changes color of row on MouseOut event
function MouseOut(row)
{
   row.style.color = 'blue';     //blue
}

// Method changes color of row on MouseOver event on a "Delete" LinkButton
function MouseOverDelete(row)
{
   row.style.color = '#065820';       //green
}

// Method changes color of row on MouseOut event on a "Delete" LinkButton
function MouseOutDelete(row)
{
   row.style.color = '#FF0000';     //red
}

// The "MaxLength" property is ignored on MultiLine Text Boxes.
// This method will make sure the length isn't exceeded
function textboxMultilineMaxNumber(textbox, e, maxLen)
{   
    try
    {   
        if(textbox.value.length > (maxLen-1))
            if (window.event)
                e.returnValue = false;
    }
    catch(e)
    {   
    }   
}   

// The "MaxLength" property is ignored on MultiLine Text Boxes.
// This method will keep track of how many chars typed and how many remaining
function textboxMultilineTracker(textbox, e, lblCharsUsedClientID, maxLen)
{   
    var lblCharsUsed;
    var textLength;
       
    // Get the length
    textLength = textbox.value.length;
    
    // If pasted too much, truncate and warn.
    if (textLength > maxLen)
    {
        textbox.value = textbox.value.substring(maxLen,0);   
        alert("You have attempted to paste more text than is allowed.  You currently have used " + textLength + " characters. You are only allowed to enter a maximum of " + maxLen + " characters.  Any text beyond this maximum has been truncated.");
    }   
    
    // Get a Handle on the label
    lblCharsUsed = document.getElementById(lblCharsUsedClientID);  
    
    // Set new length
    // Note: we need special logic for FireFox as "innerText" isn't a valid property
    if (lblCharsUsed != null)
    {
        if(document.all)
            lblCharsUsed.innerText = textbox.value.length;
         else
            lblCharsUsed.textContent = textbox.value.length;
    }  
}   


// Function to validate a Date Control (Ajax TextBox) is in the future
function validateCurrentAJAXDateControl(strDateControlClientID)
{
    var strDateControl;
    var strValue;
    
    // Get a Handle on the Date Control
    strDateControl = document.getElementById(strDateControlClientID);  
    
    if (strDateControl != null)
    {
        // Get the Date value
        strValue = strDateControl.value;
        
        // Call Method to validate the Date
        // Note: Don't allow Past Dates or today's date (indicated by false)
        if (strValue.length > 0)
        {
            return isDate(strValue, false);
        }
    }
    return true;
}


// Function to validate a Date Control (Ajax TextBox)
function validateAJAXDateControl(strDateControlClientID)
{
    var strDateControl;
    var strValue;

    // Get a Handle on the Date Control
    strDateControl = document.getElementById(strDateControlClientID);  
    
    if (strDateControl != null)
    {
        // Get the Date value
        strValue = strDateControl.value;
        
        // Call Method to validate the Date
        if (strValue.length > 0)
        {
            return isDate(strValue, true);
        }
    }
    return true;
}
 
// Function to validate if a string is a date
function isDate(dateStr, boolAllowPastDates) 
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) 
    {
        alert("You have entered an invalid date.  Please enter a date in a mm/mm/yyyy format.");
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) 
    { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) 
    {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
    {
        alert("Month " + month + " doesn`t have 31 days!")
        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)) 
        {
            alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    
    // If boolean to allow Past/current dates is set to true, return now
    // If set to false, validate that date isn't a past date or today
    if (boolAllowPastDates == true)
        return true; // date is valid
    else
    {
        var today = new Date();
        var startDate=new Date();
        
        // Set the Date
        // Note: Month is zero-based; go figure.
        startDate.setFullYear(year,month - 1,day);
        
        // Validate date is not before today
        if (startDate <= today)
        {
            alert("The Start Date for schedule payments must be set to any day after today's date.")
            return false;
        }
    }
    
    return true;
}

// Functon to unformat currrency
function unFormatCurrency(strValue)
{
    return strValue.replace(/[^\d\.-]/g,'');
}

// Method to round a value to 2 decimal places
function roundAmount(decValue)
{
    return Math.round(decValue*100)/100  
}
    
    
// Method to format a number into Currency
function formatCurrency(strValue, boolShowSign)
{
    strValue = strValue.toString().replace(/\$|\,/g,'');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue*100+0.50000000001);
    intCents = dblValue%100;
    strCents = intCents.ToString().Trim();
    dblValue = Math.floor(dblValue/100).ToString().Trim();
    if(intCents<10)
	    strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
	    dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
	    dblValue.substring(dblValue.length-(4*i+3));
    
    if (boolShowSign == true)
        strReturn = (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
    else
        strReturn = (((blnSign)?'':'-') + dblValue + '.' + strCents);
    
    return strReturn;
}
    
// Method to format a number into Percentage
function formatPercentage(strValue, boolShowSign)
{
    strValue = strValue.toString().replace(/\%/g,'');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue*100+0.50000000001);
    intCents = dblValue%100;
    strCents = intCents.ToString().Trim();
    dblValue = Math.floor(dblValue/100).ToString().Trim();
    if(intCents<10)
	    strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
	    dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
	    dblValue.substring(dblValue.length-(4*i+3));
	
	if (boolShowSign == true)
        strReturn = (((blnSign)?'':'-') + dblValue + '.' + strCents + '%' );
    else
        strReturn = (((blnSign)?'':'-') + dblValue + '.' + strCents);
    
    return strReturn;
}

// Method to format the amount field
function formatAmount(tbCampaignGoalClientID)
{
    var tbCampaignGoal;
    var decAmount;

    // Get a Handle on the Amount field
    tbCampaignGoal = document.getElementById(tbCampaignGoalClientID);  
    
    // Remove any non-numeric chars
    if (tbCampaignGoal != null)
    {
        decAmount = tbCampaignGoal.value;
        tbCampaignGoal.value = roundAmount(unFormatCurrency(decAmount));        
    }
}



// Function to validate a Desig Pager
function validateDesigPager(tbGoToPageClientID, lblTotalPagesClientID)
{
    var columnID
    var tbGoToPage;
    var lblTotalPages;
    var totalPages;
    var goToPage;
    
    // Get a Handle on the Fields 
    tbGoToPage = document.getElementById(tbGoToPageClientID);  
    lblTotalPages = document.getElementById(lblTotalPagesClientID);  

    if (tbGoToPage != null && lblTotalPages != null)
    {
        // Validate that value is numeric
        if (isNaN(tbGoToPage.value))
        {
            alert("Paging value must be numeric");
            tbGoToPage.value = "";
            return false;
        }
        
        // Validate that the value is between 1 and the Number of Pages
        totalPages = lblTotalPages.innerHTML * 1;
        goToPage = tbGoToPage.value * 1;

        if (goToPage < 1 || goToPage > totalPages)
        {
            alert("Paging value must be between 1 and " + lblTotalPages.innerHTML);
            tbGoToPage.value = "";
            return false;
        }
    }
    
    // Return
    return true;
}  


