
// Limit maximum characters in a textbox
function LimitTextbox( oTextbox, iLimit, sRemain )
{
    if ( oTextbox.value.length > iLimit )
        oTextbox.value = oTextbox.value.substring( 0, iLimit );
        
    document.getElementById( sRemain ).innerHTML = (iLimit - oTextbox.value.length) + ' characters remaining';
}

// Force the date boxes to not show an invalid date
function ForceValidDate( sDayID, sMonthID, sYearID )
{
    var oDay    = document.getElementById( sDayID );
    var oMonth  = document.getElementById( sMonthID );
    var oYear   = document.getElementById( sYearID );

    var iDay    = oDay.options[oDay.selectedIndex].value;
    var iMonth  = oMonth.options[oMonth.selectedIndex].value;
    var iYear   = oYear.options[oYear.selectedIndex].value;

    var iMaxDay = 31;

    // This won't catch all leap years, but it will for the life time of this application
    var bIsLeapYear = false;
    if ( iYear % 4 == 0 )
        bIsLeapYear = true;

    if ( iMonth == 2 )
    { // 28/29 day months

        if ( bIsLeapYear )
        {
            iMaxDay = 29;
        }
        else
        {
            iMaxDay = 28;
        }
    }
    else if ( iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11 )
    { // 30 day months

        iMaxDay = 30;
    }

    if ( iDay > iMaxDay )
        oDay.selectedIndex = iMaxDay-1;
}


// Add text to a textbox, where the users cursor is
function InsertData(insText, oElement, bOverwrite) 
{
    oElement.focus();

    if (oElement.createTextRange) {
        if (bOverwrite) {
            document.selection.createRange().text = insText;
        } else {
            document.selection.createRange().text += insText;
        }
    }
    else if (oElement.setSelectionRange) {
        var len = oElement.selectionEnd;

        oElement.value = oElement.value.substr(0, len) + insText + oElement.value.substr(len);
        oElement.setSelectionRange(len + insText.length, len + insText.length);
    }
    else {
        oElement.value += insText;
    }
}


function Cancel(sCancelTo) 
{
	if ( confirm('Are you sure you want to cancel?\n\nDoing so will lose any changes you have made') )
		location.href = sCancelTo;
}


// Warn when opening a link to an external website
function ExternalLinkWarning()
{
	alert('Cranium Caviar\n' +
		  'is not responsible for the content\n' +
		  'of external websites.');
}

// Is any text currently highlighted?
function IsTextHighlighted()
{
    if ( document.selection.createRange().text.length > 0 )
    {
        return true;
    }
    else
    {
        return false;
    }
}


function openTellAFriend(sURL) {
    window.open("/TellAFriend/index.aspx?URL=" + sURL, "", "scrollbars=yes,height=620,width=660,left=50,top=50");
    return false;
}

// Regex to check email address
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}        

