﻿/* -- Child Window functions 
 * -- require these script-level variables (updated by popup) -- */
var childWindow;
var childrenCount = 0;
/* -------------------------------------------------------- */

setBrowseCookie();

function navigate(pageAddress) {
  window.location.href = pageAddress;
  window.location.reload;
}

function popup(winurl, winname, width, height, winattribs) {
    if (typeof winattribs == "undefined") {
        winattribs = 'toolbar=no, location=no, directories=no, status=yes, menubar=yes, resizable=yes, copyhistory=no, scrollbars=yes';
    }
    winattribs += ', width=' + width
    winattribs += ', height=' + height;
    // child open, close it first
    if (parseInt(childrenCount) > 0) {
        childWindow.close();
        childrenCount = 0;
    }
    childWindow = window.open(winurl, winname, winattribs);
    childrenCount = 1;
    childWindow.onUnload = childrenCount = 0;
    childWindow.focus();
}

/* --------------------------------------------------------
   Cookie functions
   -------------------------------------------------------- */
function setCookie(name, value, expires, interval, path, domain, secure) {
	var factor, today = new Date();
	today.setTime(today.getTime()); // set the time in milliseconds
	if (expires) {
		if (!interval) interval = 'm';
		switch (interval.toLowerCase()) {
			case 'd': factor = 1000 * 60 * 60 * 24; break;
			case 'h': factor = 1000 * 60 * 60; break;
			default: factor = 1000 * 60;
		}
		expires = expires * factor; // convert expires to minutes
	}
	var expires_date = new Date(today.getTime() + (expires));
	// write the cookie
	document.cookie = name + "=" + escape( value ) + 
	((expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	((path)    ? ";path="    + path : "") + 
	((domain)  ? ";domain="  + domain : "") +
	((secure)  ? ";secure" : "");
}

function getCookie(cookie_name) {
	var start = document.cookie.indexOf(cookie_name + "=");
	var len = start + cookie_name.length + 1;
	if ((!start) && (cookie_name != document.cookie.substring(0, cookie_name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function getCookieRE(cookie_name) { // via RegExp
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
    if (results)
        return (unescape(results[2]));
    else
        return null;
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) document.cookie = name + "=" +
	((path) ? ";path=" + path : "") +
	((domain) ? ";domain=" + domain : "") +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setSessionCookie(name, value) {
    // write the cookie
    document.cookie = name + "=" + escape(value) + ";path=/";
}

/* --------------------------------------------------------
CheckBox functions
-------------------------------------------------------- */
function SelectAll(formName) {
    if (!document.forms[formName])
        return;

    var frm, ele, frmlen, obj;
    frm = document.forms[formName];
    frmlen = frm.elements.length;
    for (ele = 0; ele < frmlen; ele++) {
        obj = frm.elements[ele];
        if (obj.type == "checkbox") {
            obj.checked = true;
        }
    }
}

function DeselectAll(formName) {
    if (!document.forms[formName])
        return;

    var frm, ele, frmlen, obj;
    frm = document.forms[formName];
    frmlen = frm.elements.length;
    for (ele = 0; ele < frmlen; ele++) {
        obj = frm.elements[ele];
        if (obj.type == "checkbox") {
            obj.checked = false;
        }
    }
}




function isExplorer() {
    if (window.navigator.appName.toUpperCase() == "MICROSOFT INTERNET EXPLORER")
        return true;
    else
        return false;
}

function registerEnter() {
    var formPOST = document.formPOST;
    if (formPOST) {
        for (var i = 0; i < formPOST.elements.length; i++) {
            formPOST.elements[i].onKeyPress = userPressed
        }
    }
}

function userPressed(event) {
    var frm;
    if (document.formPOST)
        frm = document.formPOST;
    else
        frm = document.forms[0];

    if (isExplorer()) {
        if (window.event.keyCode == 13)
            frm.submit();
    }
    else {
        if (event.which == 13)
            frm.submit();
    }
}

function signOn() {
    if (validate()) {
        document.signOn.action += "?Validate=1";
        document.signOn.submit();
    }
}

function validateSignOn() {
    var errors = ""; //

    var username = String(document.signOn.username.value);
    var password = String(document.signOn.password.value);

    if (username.length < 4) errors += "* Username must contain at least 4 characters.\n";
    if (password.length < 4) errors += "* Password must contain at least 4 characters.\n";

    if (errors.length > 0) {
        window.alert("Your \"Sign On\" requires attention: \n------------------------------------------\n" + errors);
        return false;
    }
    return true;
}

/* --------------------------------------------------------
Form functions
-------------------------------------------------------- */
function resetForm() {
    if (confirm('Reset your selections and start over?')) {
        document.forms["aspnetForm"].ctl00_ContentPlaceHolderBody_HiddenFieldReset.value = "Yes";
    }
}

/*--------------------------------------------------------------------------------------------
 * Below are generic security functions for scrubbing form inputs
 * ---------------------------------------------------------------------------------------- */
function isInputSafe(obj)
{
    //-- Get input's value
    var OriginalText = obj.valueOf().toString();

    //-- Concatenate ViolationPattern
    var ViolationString = "";
    
    //-- prevent cross-site JavaScripting 
    ViolationString += "(<)|(\\%3C)|"; //-- less than
    ViolationString += "(>)|(\\%3E)|"; //-- greater than
    ViolationString += "(\\/)|(\\%2F)"; //-- slash
    
    //-- prevent SQL injection 
    ViolationString += "|(\\')|(\\%27)|"; //-- apostrophe
    ViolationString += "(=)|(\\%3D)|"; //-- equals
    ViolationString += "(;)|(\\%3B)|";  //-- semicolon
    ViolationString += "(\\-\\-)|(\\%23\\%23)"; //-- two consecutive hyphens

    //-- remove suspicious characters
    var ViolationPattern = new RegExp(ViolationPattern + "/g");
    var ReplacePattern = "";
    var ScrubbedText = OriginalText.Replace(ViolationPattern, ReplacePattern);
    if (ScrubbedText != OriginalText) {
        var msg = "Your entry of '" + OriginalText + "' was modified, as the following characters cannot be accepted:\n\n" +
         "  less than (<)     greater than (>)     equals (=)        hyphens (--)\n" +
         "  slash (/)         apostrophe (')       semicolon (;)";
        alert(msg);
        return (false);
    }
    return (true);
}

/* --------------------------------------------------------
   String functions
-------------------------------------------------------- */
function Trim(str) {
    str = String(str)
    var retString = "";
    var wasLetter = false;

    for (i = 0; i <= str.length; i++) {
        if (wasLetter || (str.charAt(i) != ' ')) {
            retString = retString + str.charAt(i);
            wasLetter = true;
        }
    }
    wasLetter = false;

    str = retString;
    retString = ""

    for (i = str.length - 1; i >= 0; i--) {
        if (wasLetter || (str.charAt(i) != ' ')) {
            retString = str.charAt(i) + retString;
            wasLetter = true;
        }
    }
    return String(retString);
}


function getUniqueString() {
    var rand_no = Math.floor(Math.random() * 1000000000).toString();
    var now = new Date();  // current date & time
    var now_time = now.getTime().toString();
    var ret = now_time + rand_no;
    //alert("Unique value for cookie named 'browse': " + ret);
    return (ret);
}

function setBrowseCookie() {
    if (getCookieRE("browse") == null)
        setSessionCookie("browse", getUniqueString());
}


