﻿
var PathToRoot = '';

function AddEvents() {

    var lis = document.getElementsByTagName("li");

    var fOver = function () {
        var uls = this.getElementsByTagName("ul")
        for (var i = 0; i < uls.length; i++) {
            if (uls[i].className != 'child') {
                uls[i].style.display = 'block';
            }
        }
    }

    var fOut = function () {
        var uls = this.getElementsByTagName("ul")
        for (var i = 0; i < uls.length; i++) {
            uls[i].style.display = 'none';
        }
    }

    var fPOver = function () {
        var uls = this.getElementsByTagName("ul")
        for (var i = 0; i < uls.length; i++) {
            uls[i].style.display = 'block';
        }
    }

    var fPOut = function () {
        var uls = this.getElementsByTagName("ul")
        for (var i = 0; i < uls.length; i++) {
            uls[i].style.display = 'none';
        }
    }


    for (var i = 0; i < lis.length; i++) {
        if (lis[i].className == 'top') {
            lis[i].onmouseover = fOver;
            lis[i].onmouseout = fOut;
        } else if (lis[i].className == 'has-children') {
            lis[i].onmouseover = fPOver;
            lis[i].onmouseout = fPOut;
        }
    }
}

//*****************************************************************************************
//*** Cookies
//*****************************************************************************************/

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


/////////////////////////////////////////////////////////////////////////////////////////
/// Format Currency

function FormatCurrency(str, decimals) {

    if (decimals == null) {
        decimals = 2;
    }

    // Convert to String    
    str = str.toString();

    // Ensure that it is a number
    if (str == 'NaN') {
        return '';
    }

    // Get rid of any commas
    str = str.replace(',', '');

    // round to the proper decimal places    
    str = parseFloat(str).toFixed(decimals);

    // Split the whole and decimal numbers into array
    var cols = str.split('.');
    var num = '';
    var count = 0;

    // Remove any negatives
    cols[0] = cols[0].replace('-', '');

    // Place a comman every three digits of the whole number
    for (var i = cols[0].length; i >= 0; i--) {
        if (IsNumericStrict(cols[0].charAt(i))) {
            num = cols[0].charAt(i) + num;
            count += 1;
            if (count == 3 && i > 0) {
                num = ',' + num;
                count = 0;
            }
        }
    }

    // Add decimals back to string
    if (cols[1] != null) {
        num += '.' + cols[1];
    }

    // check and see if orginal number was negative    
    if (str.indexOf('-') >= 0) {
        num = '-' + num;
    }

    return num
}

///////////////////////////////////////////////////////////////////////////////////////////
/// IsNumeric Strict

function IsNumericStrict(s) {
    var IsNumber = false;
    var Char;

    switch (s) {
        case '0':
            IsNumber = true;
            break;
        case '1':
            IsNumber = true;
            break;
        case '2':
            IsNumber = true;
            break;
        case '3':
            IsNumber = true;
            break;
        case '4':
            IsNumber = true;
            break;
        case '5':
            IsNumber = true;
            break;
        case '6':
            IsNumber = true;
            break;
        case '7':
            IsNumber = true;
            break;
        case '8':
            IsNumber = true;
            break;
        case '9':
            IsNumber = true;
            break;
    }

    return IsNumber;
}

function replaceAll(text, strA, strB) {
    return text.replace(new RegExp(strA, "g"), strB);
}

function FormatPhoneNumber(pn) {
    if (pn == '') {
        return '';
    }
    var r = '(';
    r += pn.substring(0, 3) + ') '
    r += pn.substring(3, 6) + '-'
    r += pn.substring(6, 10);
    return r;
}


//############################################################################
//############################################################################
//######
//###### Return X Y
//######


function ReturnX(o) {
    var x = 0;

    x = o.offsetLeft;
    o = o.offsetParent;
    while (o != null) {
        x += o.offsetLeft;
        o = o.offsetParent;
    }

    return x;

}

function ReturnY(o) {
    var y = 0;

    y = o.offsetTop;
    o = o.offsetParent;
    while (o != null) {
        y += o.offsetTop;
        o = o.offsetParent;
    }

    return y;
}

//############################################################################
//############################################################################
//######
//###### Log In
//######

//############################################################################
//######  Show Log In

function ShowLogIn(o, Path) {
    var s = document.getElementById('dShadow');
    var l = document.getElementById('dLoginHolder');
    s.style.display = 'block';
    l.style.display = 'block';
    l.style.top = ReturnY(o) + 'px';

    document.getElementById('txtUserName').focus();

    if (Path != null) {
        PathToRoot = Path;
    }

}

function LogIn() {
    var URL = PathToRoot + "AJAXMonitor.aspx?id=2";
    URL += '&un=' + document.getElementById('txtUserName').value;
    URL += '&pw=' + document.getElementById('txtPassword').value;

//    window.open(URL);
//    return;

    //alert(PathToRoot);

    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {  // Internet Explorer 5/6   
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", URL, false);
    xhttp.send("");
    if (xhttp.readyState == 4) {
        LogInResults(xhttp.responseXML);
    }
}

function LogInResults(data) {    
    var rows = data.getElementsByTagName("Results");
    IDUser = rows[0].getElementsByTagName("USER_ID")[0].childNodes[0].nodeValue;
        

    if (IDUser == 0) {
        alert('Invalid User Name or Password');
    }
    else {
        //document.getElementById('dLogin').style.display = 'none';
        //document.getElementById('dReview').style.display = 'block';
        window.location = window.location
    }

}

function CancelLogIn() {
    document.getElementById('dLoginHolder').style.display = 'none';
    document.getElementById('dShadow').style.display = 'none';
}


function DoNothing() {
}

function RegisterMe() {
    window.location = PathToRoot + '/Membership/Register.aspx';
}


function CheckForEnter(o, e) {
    var a = e ? e.which : event.keyCode;



    if (parseFloat(a) == 13) {
        if (o.id == 'txtUserName') document.getElementById('txtPassword').focus();
        if (o.id == 'txtPassword') LogIn();
    }

}



function LogOff() {
    var URL = PathToRoot + "AJAXMonitor.aspx?id=4";
    
    //    window.open(URL);
    //    return;

    //alert(PathToRoot);

    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {  // Internet Explorer 5/6   
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", URL, false);
    xhttp.send("");
    if (xhttp.readyState == 4) {
        LogOffResults(xhttp.responseXML);
    }
}

function LogOffResults(data) {
    window.location = PathToRoot + '\Default.aspx';
}

//############################################################################
//############################################################################
//######
//###### Recover Password
//######

//############################################################################
//######  Show Recover Password

function ShowRecoverPassword() {
    document.getElementById('dLogin').style.display = 'none';
    document.getElementById('dRecoverPassword').style.display = 'block';
}


//############################################################################
//######  Cancel Recover Password

function CancelRecoverPassword() {
    document.getElementById('dLogin').style.display = 'block';
    document.getElementById('dRecoverPassword').style.display = 'none';
}

function RecoverPassword() {
    var URL = PathToRoot + "Membership/AJAXMonitor.aspx?id=3";
    URL += '&ea=' + document.getElementById('txtPWREmailAddress').value;

    //window.open(URL);
    //return;

    //alert(PathToRoot);

    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {  // Internet Explorer 5/6   
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", URL, false);
    xhttp.send("");
    if (xhttp.readyState == 4) {
        RecoverPasswordResults(xhttp.responseXML);
    }
}

function RecoverPasswordResults(data) {
    CancelRecoverPassword();
    alert('Your password has been emailed to you.');
}




/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////////////
////////////  Only Numbers
////////////


function OnlyNumbers(evt) {
    var e = event || evt;
    // for trans-browser compatibility        
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45) return false;
    return true;
}

function OnlyDecimals(evt) {
    var e = event || evt;
    // for trans-browser compatibility    
    var charCode = e.which || e.keyCode;
    if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode != 46 && charCode != 45) return false;
    return true;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////////////
////////////  Select Item
////////////

function SelectItem(l, value) {
    for (var i = 0; i < l.length; i++) {
        if (l[i].value == value) {
            l.selectedIndex = i;
            return;
        }
    }
}

function ReturnSelectedValue(l) {
    return l[l.selectedIndex].value;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////////////
////////////  Format SQL Date
////////////


function FormatSQLDate(date) {
    var dates = date.split('T');
    var parts = dates[0].split('-');
    var r = '';

    r = parts[1] + '/' + parts[2] + '/' + parts[0];

    return r;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////////////
////////////  Format SQL Time
////////////


function FormatSQLTime(date) {
    var dates = date.split('T');
    var parts = dates[1].split(':');
    var r = '';

    r = parts[0] + ':' + parts[1];

    return r;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////////////
////////////  Format Phone Number
////////////


function FormatPhoneNumber(PNum, IDStyle) {
    var PhoneNumber = '';

    if (IDStyle == null) IDStyle = 1;

    if (PNum.length == 10 && IDStyle == 1) {
        PhoneNumber = PNum.substring(0, 3) + '-';
        PhoneNumber += PNum.substring(3, 6) + '-';
        PhoneNumber += PNum.substring(6, 10);
    }

    if (PNum.length == 10 && IDStyle == 2) {
        PhoneNumber = PNum.substring(0, 3) + '.';
        PhoneNumber += PNum.substring(3, 6) + '.';
        PhoneNumber += PNum.substring(6, 10);
    }

    if (PNum.length == 10 && IDStyle == 3) {
        PhoneNumber = '(' + PNum.substring(0, 3) + ') ';
        PhoneNumber += PNum.substring(3, 6) + '-';
        PhoneNumber += PNum.substring(6, 10);
    }

    return PhoneNumber;
}



function trim(s) {
    return rtrim(ltrim(s));
}

function ltrim(s) {
    var l = 0;
    while (l < s.length && s[l] == ' ')
    { l++; }
    return s.substring(l, s.length);
}

function rtrim(s) {
    var r = s.length - 1;
    while (r > 0 && s[r] == ' ')
    { r -= 1; }
    return s.substring(0, r + 1);
}

