//  -------------------------------------------------------------------------
//  (C) Copyright Stuart Jensen 2002.  All rights reserved.
//  Not to be reproduced in any form or by any means without prior written
//  permission of 
//  Stuart Jensen
//  27 Broadleaf Cir
//  Windsor, CT 06095
//  (860) 285-8404
//  -------------------------------------------------------------------------
//  Creation Date       : 11/08/2002
//  -------------------------------------------------------------------------
/// <reference path="jquery-1.3.2-vsdoc.js"/>

function resizeIframeToFitContent(iframe, min) {
    {
        try {
            var DocH = iframe.contentWindow.document.body.scrollHeight
            if (DocH < min)
                DocH = min

            iframe.style.height = DocH + 'px';
        }
        catch (err) {
            iframe.style.height = min + 'px';
        }
    }





    //var DocH = obj.contentWindow.document.body.scrollHeight
    //var DocH = obj.contentDocument.document.body.scrollHeight

    //if (DocH < min)
//        DocH = min
//
  //  obj.style.height = DocH + 'px';
    //            print( "scrollHeight " + obj.contentWindow.document.body.scrollHeight)
    //            print("offsetheith " + obj.contentWindow.document.body.offsetHeight)
    
}


//----------------------------------------------------------------------
// Procedure: FireClickButton 
// Notes:
//
//----------------------------------------------------------------------
function FireClickButton(sButtonName) {
    var c_keyPressed = '';
    var e;
    if (window.event) {
        e = window.event; //DOM
    } else {
        return; 			//TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
    }
    var chr;
    if (typeof (e.which) == 'number') {
        chr = e.which; 		//NS 4, NS 6+, Mozilla 0.9+, Opera
    } else if (typeof (e.keyCode) == 'number') {
        chr = e.keyCode; 		//IE, NS 6+, Mozilla 0.9+
    } else {
        return; 				//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
    }
    // got the key - act on it.
    c_keyPressed = String.fromCharCode(chr);
    if (c_keyPressed == '\r' || c_keyPressed == '\n') {
        e.returnValue = false;

        document.all.item(sButtonName).click();

        //return false;
    }
    else {
        return;
    }
}

//----------------------------------------------------------------------
// Procedure: showObject 
// Notes:
//
//----------------------------------------------------------------------
function showObject(obj) {
    if (ns4) obj.visibility = "show"
    else if (ie4) obj.visibility = "visible"
}

//----------------------------------------------------------------------
// Procedure: hideObject 
// Notes:
//
//----------------------------------------------------------------------
function hideObject(obj) {
    if (ns4) obj.visibility = "hide"
    else if (ie4) obj.visibility = "hidden"
}




//----------------------------------------------------------------------
// Procedure: InjectEmail 
// Notes:
//
//----------------------------------------------------------------------
function InjectEmail(sUserName, sURL, sDesc, sTitle) {
    document.write('<a Title="' + sTitle + '" href="mailto:' + sUserName + '@' + sURL + '">' + sDesc + '</a>');
}

//----------------------------------------------------------------------
// Procedure: PopImage 
// Notes:
//
//----------------------------------------------------------------------
function PopImage(ImageSrc, WinTitle, WinWidth, WinHeight) {
    var Features = 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=25,top=25,' + 'width=' + WinWidth + ',height=' + WinHeight + ','
    var popwin
    var DocHtml

    //add code so you can pass the title, left, Top

    DocHtml = "<title>" + WinTitle + "</title><body><img title='" + WinTitle + "' src='" + ImageSrc + "'></body>"

    popwin = window.open("", "PopImage", Features)
    popwin.document.open()
    popwin.document.write(DocHtml)
    popwin.document.close()

}

//----------------------------------------------------------------------
// Procedure: PopWindow
// Notes:
//
//----------------------------------------------------------------------
function PopWindow(URL, WinWidth, WinHeight) {
    var wleft = ((screen.width - WinWidth) / 2);
    var wtop = (screen.height - WinHeight) / 2;

    var Features = 'toolbar=yes,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,left=' + wleft + ',top=' + wtop + ',' + 'width=' + WinWidth + ',height=' + WinHeight + ','
    var popwin

    //add code so you can pass the title, left, Top

    popwin = window.open(URL, "_blank", Features)


}

//----------------------------------------------------------------------
// Procedure: PopWindow2
// Notes:
//
//----------------------------------------------------------------------
function PopWindow2(URL, WinWidth, WinHeight) {

    var wleft = ((screen.width - WinWidth) / 2);
    var wtop = (screen.height - WinHeight) / 2;

    var Features = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,left=' + wleft + ',top=' + wtop + ',' + 'width=' + WinWidth + ',height=' + WinHeight + ','
    var popwin

    //add code so you can pass the title, left, Top

    popwin = window.open(URL, "_blank", Features)

}

//----------------------------------------------------------------------
// Procedure: PopModalWindow
// Notes:
//
//----------------------------------------------------------------------
function PopModalWindow(URL, Args, WinWidth, WinHeight) {

    var sWindowAttribs = ""
    var sQueryString = ""

    sWindowAttribs = "dialogWidth:" + WinWidth + "px;" +
                    "dialogHeight:" + WinHeight + "px;" +
                    "dialogLeft:" + ((screen.width - WinWidth) / 2) + ";" +
                    "dialogTop:" + (screen.height - WinHeight) / 2 + ";" +
                    "resizable:yes;" + "status:yes;"

    window.showModalDialog("PopupWin.htm?" + URL, Args, sWindowAttribs);

}


//----------------------------------------------------------------------
// Procedure: Trim 
// Notes:
//
//----------------------------------------------------------------------
function Trim(sIn) {
    try {
        var i
        var sOut
        var sWork

        if (sIn == null) return null

        sOut = sIn

        for (i = 1; i <= sIn.length; i++) {
            if (Mid(sIn, i, 1) == " ") {
                if (sIn.length > i) {
                    sOut = Mid(sIn, i + 1)
                }
                else {
                    sOut = ""
                }
            }
            else break
        }

        sWork = sOut

        for (i = sWork.length; i > 0; i--) {
            if (Mid(sWork, i, 1) == " ") {
                if (sWork.length > 1) {
                    sOut = Left(sWork, i - 1)
                }
                else {
                    sOut = ""
                }
            }
            break
        }

        return sOut
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: LCase 
// Notes:
//
//----------------------------------------------------------------------
function LCase(s) {
    try {
        if (s == null) return null

        var sWork = s
        return sWork.toLowerCase()
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: UCase 
// Notes:
//
//----------------------------------------------------------------------
function UCase(s) {
    try {
        if (s == null) return null

        var sWork = s
        return sWork.toUpperCase()
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: InStr
// Notes:
//
//----------------------------------------------------------------------
function InStr(p1, p2, p3, p4) {
    try {

        var start
        var StringToSearch
        var StringToSearchFor
        var SearchType

        if (typeof (p1) == "number") {
            start = p1
            StringToSearch = p2
            StringToSearchFor = p3
            SearchType = p4
        }
        else if (typeof (p1) == "string") {
            StringToSearch = p1
            StringToSearchFor = p2
            SearchType = p3
        }
        else {
            return 0
        }

        if (StringToSearch == null) return 0
        if (StringToSearch == "") return 0
        if (StringToSearchFor == null) return 0
        if (StringToSearchFor == "") return 0
        var sWork = StringToSearch
        return sWork.indexOf(StringToSearchFor, start - 1) + 1
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure:  UBound
// Notes:
//
//----------------------------------------------------------------------
function UBound(x) {
    try {
        return x.length
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: Len
// Notes:
//
//----------------------------------------------------------------------
function Len(s) {
    try {
        if (s == null) return 0
        return s.length
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: Left 
// Notes:
//
//----------------------------------------------------------------------
function Left(s, n) {
    try {
        if (s == null) return ""
        return s.substr(0, (n ? n : s.length))
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: Right 
// Notes:
//
//----------------------------------------------------------------------
function Right(s, n) {
    try {
        if (s == null) return ""
        if (s == "") return ""
        if (typeof (s) != "string") return ""
        return s.substr(s.length - n)
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: Mid 
// Notes:
//
//----------------------------------------------------------------------
function Mid(s, b, n) {
    try {
        if (s == null) return ""
        if (n == null) return s.substr(b - 1)
        else return s.substr(b - 1, n)
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: Val 
// Notes:
//
//----------------------------------------------------------------------
function Val(x) {
    try {
        if (typeof (x) == "string") {
            x = TrimNumber(x)
        }
        var y = new Number(x)
        return y.valueOf()
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: CInt 
// Notes:
//
//----------------------------------------------------------------------
function CInt(x) {
    try {
        y = Math.floor(x)
        z = Math.ceil(x)
        return (y % 2) ? z : y
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: CStr 
// Notes:
//
//----------------------------------------------------------------------
function CStr(x) {
    try {
        return x.toString()
    } catch (e) { catchRoutine(e); return null }
}

//----------------------------------------------------------------------
// Procedure: catchRoutine
// Notes:
//
//----------------------------------------------------------------------
function catchRoutine(e) {
    var prop;
    var proplist = "";
    var routine
    var tmpstring = catchRoutine.caller
    tmpstring = tmpstring.toString().split(" ")[1]
    tmpstring = tmpstring.split("(")[0]
    proplist = ""
    for (prop in e) {
        proplist += (prop + "= " + eval("e." + prop) + " ");
    }
    alert("Error in " + tmpstring + ": " + proplist)
}

//----------------------------------------------------------------------
// Cookie Functions,  Handles both dictionary cookies and standard 
// name/value pair cookies
// Examples:
// getCookie("User","Name")
// getCookie("User","Password")
// getCookie("FullName")
//
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// Procedure: getCookie
//----------------------------------------------------------------------
function getCookie(name, key) {
    try {
        var cookies = parseCookie(name);
        var sKey = key
        if (cookies == null)
            return null;

        if (typeof (key) == 'undefined')
            sKey = name

        return cookies[sKey];
    }
    catch (e) {
        catchRoutine(e);
        return null;
    }

}

//----------------------------------------------------------------------
// Procedure: addToCookie
//----------------------------------------------------------------------
function addToCookie(cookieName, key, value) {
    try {
        var cookies = parseCookie(cookieName);
        cookies[key] = value;
        var d = new Date();
        d.setFullYear(d.getFullYear() + 1);
        var cookieStr = cookieName + "=";

        for (var item in cookies)
            cookieStr += item + "=" + cookies[item] + "&";

        cookieStr = cookieStr.substring(0, cookieStr.length - 1);
        cookieStr = cookieStr + "; expires=" + d.toUTCString();
        cookieStr = cookieStr + "; path=/";
        document.cookie = cookieStr;
    }
    catch (e) {
        catchRoutine(e);
        return null;
    }
}

//----------------------------------------------------------------------
// Procedure: parseCookie
//----------------------------------------------------------------------
function parseCookie(name) {
    try {
        var str = "";
        var a = document.cookie.split(";");

        for (var i = 0; i < a.length; i++) {
            a[i] = a[i].trim();
            if (a[i].substring(0, name.length) == name)
                str = a[i].substring(name.length + 1);
        }

        var a = str.split("&");
        var temp = "";

        if (str.length) {
            var obj = new Object();
            for (i = 0; i < a.length; i++) {
                temp = a[i].split("=");
                if (temp.length == 1)
                    obj[name] = temp[0].replace("+", " ");
                else
                    obj[temp[0]] = temp[1].replace("+", " ");
            }

            return obj;
        }
        else {
            return null;
        }
    }
    catch (e) {
        catchRoutine(e);
        return null;
    }
}

//----------------------------------------------------------------------
// Procedure: Trim
//----------------------------------------------------------------------
String.prototype.trim = function() {
    try {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }
    catch (e) {
        catchRoutine(e);
        return null;
    }
}

