﻿String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

var individualId = null;
var companyId = null;
var ocpUserId = null;
var oepSessionId = null;
var oepUserId = null;

sfHover = function(sfEls) 
{         
    if (!$.browser.msie || ($.browser.version >= 7))
        return;
    
    for (var i = 0; i < sfEls.length; ++i)
    {         
        sfEls[i].onmouseover = function() 
        {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function()
        {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    } 
} 

sfHoverclear = function(sfEls) 
{         
    if (!$.browser.msie || ($.browser.version >= 7))
        return;
    
    for (var i = 0; i < sfEls.length; ++i)
        sfEls[i].onmouseover = sfEls[i].onmouseout = null;
} 


function isEnter(e)
{
	if ((e.keyCode ? e.keyCode : e.which ? e.which : e.charCode) == 13) 
		return true;
	return false;
}

function isControlDataEntered(id)
{
	var c = document.getElementById(id);
	if (null == c)
		return false;
	if ((null != c.disabled) && (true == c.disabled))
		return false;
	if ((null == c.value) || ('' == c.value.replace(' ', '')))
		return false;
	return true;
}


function isText(o, text)
{
    return (((typeof(o.innerText) != 'undefined') && (o.innerText == text))
        || ((typeof(o.textContent) != 'undefined') && (o.textContent == text))
        || (o.text == text));
}

function setText(o, text)
{
    if (typeof(o.innerText) != 'undefined')
        o.innerText = text;
    else if (typeof(o.textContent) != 'undefined')
        o.textContent = text;
    else
        o.text = text;
}

function getListBoxSelectedText(control)
{
    if ((null == control) || (null == control.options) || (0 == control.options.length))
        return '';

    for (var i = 0; i < control.options.length; ++i)
        if (control.options[i].selected)
        {
            if (typeof(control.innerText) != 'undefined')
                return control.options[i].innerText;
            else if (typeof(control.textContent) != 'undefined')
                return control.options[i].textContent;

            return control.options[i].text;
        }
    
    return '';
}
//CORK 9/28/2010
function getListBoxSelectedValue(control) {
    if ((null == control) || (null == control.options) || (0 == control.options.length))
        return '';

    for (var i = 0; i < control.options.length; ++i)
        if (control.options[i].selected) {
        if (typeof (control.innerText) != 'undefined')
            return control.options[i].value;
        else if (typeof (control.textContent) != 'undefined')
            return control.options[i].value;

        return control.options[i].value;
    }

    return '';
}
//END CORK 9/28/2010
function getCookieValue(name)
{
    if (null == document.cookie)
        return "";
    var cookieValue = null;
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; ++i)		    
    {
        var splitterPosition = cookies[i].indexOf('=');
        if (splitterPosition <= 0)
            break;
        if (name == cookies[i].substring(0, splitterPosition).trim())
        {
            cookieValue = cookies[i].substring(splitterPosition + 1, cookies[i].length);
            break;
        }
    }
    
    return cookieValue;
}

function getCookieCrumbs(cookie)
{
    var crumbs = new Array();
    if (null == cookie)
        return crumbs;
    var cookieCrumbs = cookie.split('&');    
    for (var i = 0; i < cookieCrumbs.length; ++i)
    {
        var splitCrumb = cookieCrumbs[i].split('=');
        if (splitCrumb.length == 1)
            crumbs.push([unescape(splitCrumb[0]), '']);
        else if (splitCrumb.length > 1)
            crumbs.push([unescape(splitCrumb[0]), unescape(splitCrumb[1])]);
    }    
    
    return crumbs;
}

function setPersistentCookieValue(name, value)
{
    deleteExistingCookie(name);
    //now pop in the replacement cookie
    var expire = new Date();
    //setYear / getYear no work in FireFox ... getFullYear / setFullYear does, but might be less supported on old browsers
    //setMonth / getMonth seems to have the highest compatibility
    document.cookie = escape(name) + '=' + escape(value) + ';expires=' + new Date(expire.setMonth(expire.getMonth()+12)).toGMTString() + ';path=/;';
}

function deleteExistingCookie(name)
{
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; ++i)		    
    {
        var splitCookie = cookies[i].split('=');
        if ((splitCookie.length >= 1) && (name == splitCookie[0].trim()))
            //delete existing cookie
            document.cookie = cookies[i] + ';expires=' + new Date().toGMTString() + ';;';
    }
}

function loadOnyxCookie()
{
    var crumbs = getCookieCrumbs(getCookieValue('SessionData'));
    for (var i = 0; i < crumbs.length; ++i)
    {
        switch (crumbs[i][0])
        {
            case 'IndividualId':
                individualId = Number(crumbs[i][1]);
            break;
            
            case 'CompanyId':
                companyId = Number(crumbs[i][1]);               
            break;
            case 'OCPUserId':
                ocpUserId = crumbs[i][1];
            break;
        }
    }
    
    var crumbs = getCookieCrumbs(getCookieValue('S'));
    for (var i = 0; i < crumbs.length; ++i)
    {
        switch (crumbs[i][0])
        {
            case '6':
                oepSessionId = Number(crumbs[i][1]);
            break;            
            case '4':
                oepUserId = Number(crumbs[i][1]);               
            break;
        }
    }

}



function loadBox(clear, control, items, css)
{
    if (clear)
        clearBox(control);
    for (var i = 0; i < items.length; ++i)
        addOptionElement(control, items[i][0], items[i][1], css);
}

function addOptionElement(control, val, text, css)
{
    var o = document.createElement("OPTION");
    //Safari fix
    if (control.options && control.options.add)
        control.options.add(o);
    else
        control.appendChild(o);

    o.value = val;
    setText(o, text);
    if (null != css)
    {
        if (typeof(o.className) != 'undefined')
            o.className = css;
        //ie won't load this script with this in it
        //else if (typeof(o.class) != 'undefined')
        //    o.class = css;
    }
}


function clearTextBox(name)
{
    var textBox = document.getElementById(name);
    if (!textBox.disabled)
    {
        textBox.value = "";
        //reset any attached validators (based on submitted or not)        
        resetAttachedValidator(name);
    }
}

function resetAllValidators()
{
    for (var i = 0; i < Page_Validators.length; ++i)
        resetValidator(Page_Validators[i]);    
}

function resetAttachedValidator(name)
{
    for (var i = 0; i < Page_Validators.length; ++i)
        if ((null != Page_Validators[i].controltovalidate) && (Page_Validators[i].controltovalidate.toLowerCase() == name.toLowerCase()))
            resetValidator(Page_Validators[i]);
}

function resetValidator(val)
{
    var wasEnabled = val.enabled;
    ValidatorEnable(val, submitted ? val.enabled : false);
    if (!submitted) val.enabled = wasEnabled;
}


function clearBox(control)
{
    var l = control.options.length;
    for (var i = 0; i < l; ++i)
    {
        if (typeof(control.options.remove) != 'undefined')
            control.options.remove(0);
        else
            control.remove(0);
    }
}


function clearBoxSelections(control)
{
    for (var i = 0; i < control.options.length; ++i)
        control.options[i].selected = false;
    control.selectedIndex = -1;
}


function addSelectedToArray(listbox, itemArray)
{
    for (var i = 0; i < listbox.options.length; ++i)
        if (listbox.options[i].selected)
            itemArray[itemArray.length] = parseInt(listbox.options[i].value, 10);
}        

function selectListItemByIndex(controlName, index)
{
    var control = document.getElementById(controlName);
    for (var i = 0; i < control.options.length; ++i)
        control.options[i].selected = (i == index);
    control.selectedIndex = index;   
}

function selectListItemByValue(control, value)
{
    if (null == value) return;
    
    for (var i = 0; i < control.options.length; ++i)
        control.options[i].selected = (value == control.options[i].value);            
}

function getSelectedListItemArray(listBox, allowMultiples)
{
    var items = [];
    for (var i = 0; i < listBox.length; ++i)
        if (listBox.options[i].selected && ('' != listBox.options[i].value.trim()))
        {
            items.push(escape(listBox.options[i].value));
            if (!allowMultiples) break;                
        }         
    return items;   
}


function getListBasedQuery(paramName, listBox, allowMultiples)
{
    var query = [];
    for (var i = 0; i < listBox.length; ++i)
        if (listBox.options[i].selected && ('' != listBox.options[i].value.trim()))
        {
            query.push(escape(paramName) + '=' + escape(listBox.options[i].value));
            if (!allowMultiples) break;                
        }         
    return query;   
}

function getTextBasedQuery(paramName, textBox)
{
    var textValue = (null != textBox.value ? textBox.value.trim() : '');
    if ('' != textValue)
        return escape(paramName) + '=' + escape(textValue);
    
    return '';
}


//japan function
function smoothScroll() 
{
    var xScrollSpeed = 1;
    var yScrollSpeed = 1.1;

    var x1 = document.documentElement ? document.documentElement.scrollLeft || 0 : 0;
    var y1 = document.documentElement ? document.documentElement.scrollTop || 0 : 0;
    var x2 = document.body ? document.body.scrollLeft || 0 : 0;
    var y2 = document.body ? document.body.scrollTop || 0 : 0;
    
    var x = Math.max(x1, x2);
    var y = Math.max(y1, y2);
    window.scrollTo(Math.floor(x/xScrollSpeed), Math.floor(y/yScrollSpeed));
    if (x > 0 || y > 0)
        window.setTimeout("smoothScroll()", 25);    
}

