﻿
// Site.js
// Description:  This JavaScript file is a global file to be linked when needed.

var objLoginName, objPassword, strSecureUrlPrefix;
var objALoginRedirect;

function HTTPCheck(e, Name, Address, City, State, Zip, Phone, Email, CommentQuestion, lblMsg) {

    var Msg = "Please remove any references containing 'http' from the following fields: ";
    var MsgLength = Msg.length;
    var rtnValue=false;

    if (Name == null) Name = new Object();
    if (Address == null) Address = new Object();
    if (City == null) City = new Object();
    if (State == null) State = new Object();
    if (Zip == null) Zip = new Object();
    if (Phone == null) Phone = new Object();
    if (Email == null) Email = new Object();
    if (CommentQuestion == null) CommentQuestion = new Object();
    if (lblMsg == null) lblMsg = new Object();

    objName = document.getElementById(Name);
    var NameValue = objName.value.toLowerCase();
    if (NameValue.indexOf('http') != -1) { Msg += 'Name'; }

    objAddress = document.getElementById(Address);
    var AddressValue = objAddress.value.toLowerCase();
    if (AddressValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', Address'; } else {  Msg += 'Address';} }

    objCity = document.getElementById(City);
    var CityValue = objCity.value.toLowerCase();
    if (CityValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', City'; } else { Msg += 'City'; } }

    objState = document.getElementById(State);
    var StateValue = objState.value.toLowerCase();
    if (StateValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', State'; } else { Msg += 'State'; } }

    objZip = document.getElementById(Zip);
    var ZipValue = objZip.value.toLowerCase();
    if (ZipValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', Zip'; } else { Msg += 'Zip'; } }

    objPhone = document.getElementById(Phone);
    var PhoneValue = objPhone.value.toLowerCase();
    if (PhoneValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', Phone'; } else { Msg += 'Phone'; } }

    objEmail = document.getElementById(Email);
    var EmailValue = objEmail.value.toLowerCase();
    if (EmailValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', Email'; } else { Msg += 'Email'; } }

    objCommentQuestion = document.getElementById(CommentQuestion);
    var CommentQuestionValue = objCommentQuestion.value.toLowerCase();
    if (CommentQuestionValue.indexOf('http') != -1) { if (Msg.length != MsgLength) { Msg += ', Comment'; } else { Msg += 'Comment'; } }

    objlblMsg = document.getElementById(lblMsg);   

    if (Msg.length != MsgLength) {
        objlblMsg.innerHTML = Msg;
        rtnValue = false;
    }
    else {
        objlblMsg.innerHTML = "";
        rtnValue = true;
    }
    
    return rtnValue;

}

function LoginRedirect1(loginName, password, secureUrlPrefix, currentPageName) {
    try {

        if (loginName == null) loginName = new Object();
        if (password == null) password = new Object();
        if (secureUrlPrefix == null) secureUrlPrefix = "";
        if (currentPageName == null) currentPageName = "";

        objLoginName = document.getElementById(loginName);
        var loginNameValue = objLoginName.value;

        objPassword = document.getElementById(password);
        var PasswordValue = objPassword.value

        var theForm = document.forms['aspnetForm'];
        if (!theForm) {
            theForm = document.aspnetForm;
        }
        theForm.__EVENTTARGET.value = 'loginParms';
        theForm.__EVENTARGUMENT.value = 'frmLoginParm1=' + loginNameValue 
            + '&frmLoginParm2=' + PasswordValue
            + '&frmLoginParm3=' + currentPageName;

        theForm.action = secureUrlPrefix + 'LoginHidden.aspx';
        theForm.method = 'post';
        theForm.submit();

    }
    catch (Error) {
        alert(Error.message);
    }
}

function MapEnterKeyToButtonClick_onkeydown_LoginRedirect1(e, loginName, password, secureUrlPrefix, currentPageName) {
    if (IsEnterKey_onkeydown(e)) {
        LoginRedirect1(loginName, password, secureUrlPrefix, currentPageName);
        return false;

        e.cancelBubble = true;
        //        event.cancelBubble = true;
        return false;
    }
}

function checkLength(field) {
    var len = field.getAttribute('MaxLength');
    if (field.value.length > len) // too long...trim it!
        field.value = field.value.substring(0, len);
}


function MapEnterKeyToButtonClick_onkeydown(e, btnClientID) {
    // used by MyAccountLogin.aspx, this page could not properly map imagebuttons clientid without wrapping it in single quotes in the code behind.
    if (IsEnterKey_onkeydown(e)) {
        obtn = document.getElementById(btnClientID);
        obtn.click();

        return false;   // return false is used to cancel the remaining events
    }
}

function Amt_onkeydown(e, allowDecimal, allowNegative) {

    // rls - 10-9-8 - modified to not allow enter key as a tab
    //    if (IsEnterKey_onkeydown(e)) {
    //        return true;
    //    }
    //    else if (IsTabKey_onkeydown(e)) {
    if (IsTabKey_onkeydown(e)) {
        return true;
    }
    else if (IsNavigationKey_onkeydown(e) || IsDeleteKey_onkeydown(e)) {
        return true;
    }
    else if (IsNumeric_onkeydown(e, allowDecimal, allowNegative)) {	//IsNumeric_onkeydown(e, allowDecimal, allowNegative )
        return true;
    }
    else {
        return false;
    }
}


/*  K E Y C O D E   I N F O

- Key events occur in the following order: KeyDown, KeyPress, Keyup
- For a Keycode explanation & tester check out: http://msconline.maconstate.edu/Tutorials/JSDHTML/JSDHTML15/jsdhtml15-05.htm
- onkeydown: 
- Returns a unique integer keyCode representing the the physical key that is pressed.
For instance, onkeydown will return a 49 when the 10 key number "1" is pressed and a 97 when the 
"1" key along the top of the keyboard is pressed
- onkeypress: 
- Returns an integer keyCode representing the Unicode value of the character associated with the key.
- The onkeypress event handler produces keyCodes representing the character that is typed. 
For non-character keys no keyCode is produced. These character keyCodes represent the Unicode values 
of the characters and present a second way to check for valid entry into a textbox

*/

function ConvertEnterToTab_onkeydown(e) { // Converts "Enter-13" or "Tab-9" into "Tab-9"

    if (e.keyCode == 9 || e.keyCode == 13) {
        e.keyCode = 9;
    }
    return e.keyCode;
}

function IsNumeric_onkeydown(e, allowDecimal, allowNegative) {

    // The number keys along the top of the keyboard have keyCode values of 48 - 57 for the 0 through 9 keys. 
    // The keys on the number pad have keyCode values of 96 - 105 for the ten number keys.

    if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
        return true;
    }
    else if (allowDecimal && (e.keyCode == 110 || e.keyCode == 190)) {
        return true;
    }
    else if (allowNegative && (e.keyCode == 109 || e.keyCode == 189)) {
        return true;
    }
    else {
        return false;
    }
}

function IsNavigationKey_onkeydown(e) {
    if (IsBackSpaceKey_onkeydown(e) || IsArrowKey_onkeydown(e)) {
        return true;
    }
    else {
        return false;
    }
}

function IsEnterKey_onkeydown(e) { if (e.keyCode == 13) { return true; } else { return false; } }
function IsTabKey_onkeydown(e) { if (e.keyCode == 9) { return true; } else { return false; } }
function IsBackSpaceKey_onkeydown(e) { if (e.keyCode == 8) { return true; } else { return false; } }
function IsArrowKey_onkeydown(e) { if (e.keyCode >= 37 && e.keyCode <= 40) { return true; } else { return false; } }
function IsDeleteKey_onkeydown(e) { if (e.keyCode == 46) { return true; } else { return false; } }

// rls - delete this later
//
//function Rebuild_aLoginRedirect(loginName, password, secureUrlPrefix) {
//    try {
//        if (loginName == null) loginName = new Object();
//        if (password == null) password = new Object();
//        if (secureUrlPrefix == null) secureUrlPrefix = "";
//        objLoginName = document.getElementById(loginName);
//        var loginNameValue = objLoginName.value;
//        objPassword = document.getElementById(password);
//        var PasswordValue = objPassword.value
//       objALoginRedirect = document.getElementById('ctl00_ucLogin_aLoginRedirect');
//        if (objALoginRedirect == null)
//            objALoginRedirect = document.getElementById('ctl00_ctl00_ucLogin_aLoginRedirect');
//        var theForm = document.forms['aspnetForm'];
//        if (!theForm) {
//            theForm = document.aspnetForm;
//        }
//        
//        theForm.__EVENTTARGET.value = 'loginParms';
//        theForm.__EVENTARGUMENT.value = 'frmLoginParm1=' + loginNameValue + '&frmLoginParm2=' + PasswordValue;
//        objALoginRedirect.href = secureUrlPrefix + 'LoginHidden.aspx';
////        theForm.action = secureUrlPrefix + 'LoginHidden.aspx';
////        theForm.method = 'post';
////        theForm.submit();
//    }
//    catch (Error) {
//        alert(Error.message);
//    }
//}

//function LoginRedirect2(loginName, password, secureUrlPrefix) {
//    //
//    // note this has not been finished, not sure it would work as desired.
//    //
//    try {
//        if (loginName == null) loginName = new Object();
//        if (password == null) password = new Object();
//        if (secureUrlPrefix == null) secureUrlPrefix = "";
//        objLoginName = document.getElementById(loginName);
//        var loginNameValue = objLoginName.value;
//        objPassword = document.getElementById(password);
//        var PasswordValue = objPassword.value
//        var theForm = document.forms['aspnetForm'];
//        if (!theForm) {
//            theForm = document.aspnetForm;
//        }
//        
//        theForm.__EVENTTARGET.value = 'loginParms';
//        theForm.__EVENTARGUMENT.value = 'frmLoginParm1=' + loginNameValue + '&frmLoginParm2=' + PasswordValue;
////        theForm.action = secureUrlPrefix + 'LoginHidden.aspx';
////        theForm.method = 'post';
////        theForm.submit();
//        var url = secureUrlPrefix + 'LoginHidden.aspx';
//        
//        var requester;
//        try {
//            requester = new XMLHttpRequest();
//        }
//        catch (Error) {
//            try {
//                requester = new ActiveXObject("Microsoft.XMLHTTP");
//            }
//            catch (Error) {requester = null;}
//        }

//        if (requester != null) {
//            requester.open("POST", url, false);
//            requester.send;
////            requester.Send(HTTP.encodeFormData(values));
//        }
//        
//    }
//    catch (Error) {
//        alert(Error.message);
//    }
//}


//function LoginRedirect(loginName, password, secureUrlPrefix) {
//    //
//    // note:
//    //  - in internet explorer, page redirected from default, base page pages, authenticate pages; however, the page load of
//    //  loginhidden.aspx.cs never ran.
//    //  - in firefox, page would redirect from authenticate pages but would not from default and base pages.
//    //
//    try {
//        if (loginName == null) loginName = new Object();
//        if (password == null) password = new Object();
//        if (secureUrlPrefix == null) secureUrlPrefix = "";
//        objLoginName = document.getElementById(loginName);
//        var loginNameValue = objLoginName.value;
//        objPassword = document.getElementById(password);
//        var PasswordValue = objPassword.value
//        var theForm = document.forms['aspnetForm'];
//        if (!theForm) {
//            theForm = document.aspnetForm;
//        }
//        theForm.__EVENTTARGET.value = 'loginParms';
//        theForm.__EVENTARGUMENT.value = 'frmLoginParm1=' + loginNameValue + '&frmLoginParm2=' + PasswordValue;
//        window.location = secureUrlPrefix + 'LoginHidden.aspx';
//        
//    }
//    catch (Error) {
//        alert(Error.message);
//    }
//}

//function SetLoginParms(txtLoginName, txtPassword, txtSecureUrlPrefix) {
//// rls - this works on default page but not on any other pages with masterpages because of name mangling.
////
//    objLoginName = document.getElementById(txtLoginName);
//    objPassword = document.getElementById(txtPassword);
//    strSecureUrlPrefix = txtSecureUrlPrefix;
//}

//var xmlhttp;
//function loadXMLDoc(url) {
//    xmlhttp = null;
//    if (window.XMLHttpRequest) {// code for all new browsers
//        xmlhttp = new XMLHttpRequest();
//    }
//    else if (window.ActiveXObject) {// code for IE5 and IE6
//        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//    }
//    if (xmlhttp != null) {
//        xmlhttp.onreadystatechange = state_Change;
//        xmlhttp.open("GET", url, true);
//        xmlhttp.send(null);
//    }
//    else {
//        alert("Your browser does not support XMLHTTP.");
//    }
//}

//function state_Change() {
//    if (xmlhttp.readyState == 4) {// 4 = "loaded"
//        if (xmlhttp.status == 200) {// 200 = OK
//            // ...our code here...
//            var varResponse = xmlhttp.responseText;
//            alert(varResponse);
//        }
//        else {
//            alert("Problem retrieving XML data");
//        }
//    }
//}
