// File:	reg\sys\sys.js
// Project:	Regsvr.NET Version 4.7
// Author:	Zach Groff
//			M&S Productions, LLC
//			http://www.multimediaandstaging.com
// Purpose:	Provides System Javascript Functions for use in reg\* subsystem.

function trim(str) {
    if (str === undefined)
        return str;

    // Trims the whitespace off the beginning and end but not out of the center.
    return str.replace(/^\s+|\s+$/g, '');
}

// Can have optional $ at beginning, an optional decimal with optional cents. However, commas are required and digits between punctuation must be in sets of 3
// Returns either the correct string, or ""
function conformToCurrency(str) {
    str = trim(str);
    if (/^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{0,2})?$/.test(str)) {
        if (/\.[0-9]$/.test(str)) {
            str += "0";
        }
        else if (/\.$/.test(str)) {
            str += "00";
        }
        else if (!/\.[0-9]{2}$/.test(str)) {
            str += ".00";
        }
        if (str.charAt(0) != '$')    // Add $
            str = "$" + str;

        return str;
    }
    else {
        return "";
    }
}

function SYS_IsCurrency(str) {
    str = trim(str);
    return /^\$?\d{1,3}(,\d{3})*(\.\d{0,2})?$/.test(str);
}

// Verify if text is integer with optional - or + preceding the value and an optional decimal at the end.
function SYS_IsInt(str) {
    str = trim(str);
    return (/^[-+]?\d{1,3}(,\d{3})*\.?$/.test(str) || /^\d+\.?$/.test(str));     // Allow any number of digits with commas in currency like format, or any number of digits straight and either ending with optional decimal.
}

// Will only work properly if SYS_IsInt passes as true!
function SYS_ToSystemInt(src) {
    var str = "";
    var i = 0;
    for (i = 0; i < src.length; i++)
        if (/^\d$/.test(src.charAt(i)))
        str = str + src.charAt(i);
    return str;
}

// Verify if text is floating point value with optional - or + preceding the value and optional . somewhere in the middle or even at the end.
function SYS_IsFloat(str) {
    str = trim(str);
    return (/^[-+]?\d{1,3}(,\d{3})*(\.\d*)?/.test(str) || /^\d+(\.\d*)?$/.test(str));     // Allow any number of digits with commas in currency like format, or any number of digits straight and either ending with optional decimal and digits after decimal
}

// Will only work properly if SYS_IsFloat passes as true!
function SYS_ToSystemFloat(src) {
    var str = "";
    var i = 0;
    for (i = 0; i < src.length; i++)
        if (/^\d|\.$/.test(src.charAt(i)))
            str = str + src.charAt(i);
    return str;
}

function SYS_CurrencyToFloat(strCur) {
    var str = "";
    var i=0;
    for (i = 0; i < strCur.length; i++)
        if (/^\d|\.$/.test(strCur.charAt(i)))
            str = str + strCur.charAt(i);
    return str;
}

function SYS_IsValidDateObj(dateObj) {
    if ( Object.prototype.toString.call(dateObj) === "[object Date]" ) {
        // it is a date
        if ( isNaN( dateObj.getTime() ) ) {  // dateObj.valueOf() could also work
            // date is not valid
            return false;
        } else {
            // date is valid
            return true;
        }
    } else {
        // not a date
        return false;
    }
}

function About()
{
	window.open(GetRoot() + 'about.aspx', 'AboutWindow', 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=500,width=600');
}
function Help()
{
	window.open(GetRoot() + 'help.aspx', 'HelpWindow', 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=500,width=600');
}
function Logout()
{
	var con = confirm('Are you sure you want to logout?');
	if (con)
		window.location = GetRoot() + 'reg/login.aspx?t1=0&s2=2';
}
function Profile()
{
	var con = confirm('Are you sure you want to navigate away from this page?');
	if (con)
		window.location = GetRoot() + 'reg/profile.aspx';
}
function Profile1()
{
	var con = confirm('Are you sure you want to leave this events registration?');
	if (con)
		window.location = GetRoot() + 'reg/profile.aspx';
}
function RegHome()
{
	var con = confirm('Are you sure you want to navigate away from this page?');
	if (con)
		window.location = GetRoot() + 'reg/default.aspx';
}
function RegHome1()
{
	var con = confirm('Are you sure you want to leave this events registration?');
	if (con)
		window.location = GetRoot() + 'reg/default.aspx';
}
function AdminHome()
{
	var con = confirm('Are you sure you want to navigate away from this page?');
	if (con)
		window.location = GetRoot() + 'admin/default.aspx';
}
function HelpWindow(curPage)
{
	window.open(GetRoot() + 'interactivehelp.aspx?page=' + curPage, 'InteractiveHelp', 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=500,width=600');
}

