function UsiCommon()
{
    UsiCommon.input_list = null;
};

UsiCommon.js_element_node = 1;
UsiCommon.js_text_node = 3;


function showHand(object){
    object.style.cursor = "pointer";
}

function showNormal(object){
    object.style.cursor = "normal";
}

function highlightLink(anchor){
    anchor.style.color = "#333333";
}

function unHighlightLink(anchor){
    anchor.style.color = "#666666";
}

function setImageOff(image, dir) {

    var prefix = "images/buttons/";
    if(typeof(dir) != 'undefined' && dir != null && dir != 'null'){
        prefix = "images/"+ dir +"/";
    }

    var image_source = image.src;
    var image_index = image_source.indexOf(prefix);
    if(image_index != -1){
        image_source = image_source.substring(image_index + prefix.length);
    }


    var start_index = image_source.indexOf('over');

    if(start_index != -1)
    {
        var new_source = image_source.substring(0, start_index);

        new_source = prefix + new_source +"off"+ image_source.substring(image_source.indexOf("."));

        image.src = new_source;

        showNormal(image);
    }
}

function setImageActive(image, dir) {

    var prefix = "images/buttons/";
    if(typeof(dir) != 'undefined' && dir != null && dir != 'null'){
        prefix = "images/"+ dir +"/";
    }

    var image_source = image.src;
    var image_index = image_source.indexOf(prefix);
    if(image_index != -1){
        image_source = image_source.substring(image_index + prefix.length);
    }

    var start_index = image_source.indexOf('off');

    if(start_index != -1)
    {
        var new_source = image_source.substring(0, start_index);
        new_source = prefix + new_source +"over"+ image_source.substring(image_source.indexOf("."));
        image.src = new_source;

        showHand(image);
    }
}

function subNavSelected(ul, li) {
                       
    var child_nodes = ul.childNodes;
    for(var i = 0; i < child_nodes.length; i++)
    {
        if(UsiCommon.isElementNode(child_nodes[i]))
        {
            child_nodes[i].style.color = "#ffffff";
            child_nodes[i].style.paddingBottom = "8px";
            child_nodes[i].style.backgroundColor = "";
        }
    }


    li.style.color = "#000000";
    li.style.backgroundColor = "#ffffff";
    li.style.paddingBottom = "9px";
}

function hideChildElements(div) {

    var child_nodes = div.childNodes;
    for(var i = 0; i < child_nodes.length; i++)
    {
        if(UsiCommon.isElementNode(child_nodes[i])){
            child_nodes[i].style.display = "none";
        }
    }  
}

UsiCommon.isElementNode = function(object){
    return (!UsiCommon.isNull(object) && object.nodeType == UsiCommon.js_element_node)
}

UsiCommon.isNull = function(obj){
    return (typeof(obj) == 'undefined' || obj == null);
}


UsiCommon.inputFocus = function(input, custom_color, custom_style)
{
    var color = "#333333";
    var style = "normal";
    if(typeof(custom_color) != 'undefined' && custom_color != null && custom_color != 'null'){
        color = custom_color;
    }

    if(typeof(custom_style) != 'undefined' && custom_style != null && custom_style != 'null'){
        style = custom_style;
    }

    var value = input.value;
    if(value == input.title)
    {
        input.value = '';
        input.style.color = color;
        input.style.fontStyle = style;
    }
}

UsiCommon.inputBlur = function(input, custom_color, custom_style)
{
    var color = "#aaaaaa";
    var style = "normal";
    if(typeof(custom_color) != 'undefined' && custom_color != null && custom_color != 'null'){
        color = custom_color;
    }

    if(typeof(custom_style) != 'undefined' && custom_style != null && custom_style != 'null'){
        style = custom_style;
    }

    var value = input.value;
    if(value == null || value == '')
    {
        input.value = input.title;
        input.style.color = color;
        input.style.fontStyle = style;
    }
}


UsiCommon.validateForm = function(form, email_input1, email_input2)
{
    var error = false;
    var error_message = "Please correct the following before continuing:\n\n";
    var temp_value;

    UsiCommon.input_list = new Array();

    var children = form.childNodes;
    if(!UsiCommon.isNull(children) && children.length > 0)
    {
        UsiCommon.recursiveSearchElement(children, 'text');
        if(!UsiCommon.isNull(UsiCommon.input_list) && UsiCommon.input_list.length > 0)
        {
            for(var i = 0; i < UsiCommon.input_list.length; i++)
            {
                if(!UsiCommon.isNull(UsiCommon.input_list[i]) && UsiCommon.input_list[i].nodeType == UsiCommon.js_element_node && UsiCommon.input_list[i].type == 'text')
                {
                    temp_value = UsiCommon.input_list[i].value;
                    if(temp_value == null || temp_value == '' || temp_value == UsiCommon.input_list[i].title)
                    {
                        error = true;
                        error_message = error_message + "- enter a value for : "+ UsiCommon.input_list[i].title +"\n";
                    }
                }
            }
        }
    }


    if(!error && !UsiCommon.isNull(email_input1))
    {
        var email_value = document.getElementById(email_input1).value;

        var email_regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if(!email_regexp.test(email_value))
        {
            error = true;
            error_message = error_message + '- make sure email address is valid\n';
        }
    }

    if(!error && !UsiCommon.isNull(email_input1) && !UsiCommon.isNull(email_input2))
    {
        var email_value1 = document.getElementById(email_input1).value;
        var email_value2 = document.getElementById(email_input2).value;

        if(email_value1 != email_value2)
        {
            error = true;
            error_message = error_message + '- make sure email addresses match\n';
        }
    }


    if(error)
    {
        alert(error_message);
        return false;
    }
    else{
        return true;
    }
}

/* dynamically search down every node of a tree */
UsiCommon.recursiveSearchElement = function(children, element_type)
{
    if(!UsiCommon.isNull(children))
    {
        for(var i = 0; i < children.length; i++)
        {
            if(!UsiCommon.isNull(children[i]) && children[i].nodeType == UsiCommon.js_element_node)
            {
                if(children[i].type == element_type && !UsiCommon.containsID(UsiCommon.input_list, children[i].id))
                {
                    UsiCommon.input_list.push(children[i]);
                    UsiCommon.recursiveSearchElement(children[i].childNodes, element_type);
                }
                else{
                    UsiCommon.recursiveSearchElement(children[i].childNodes, element_type);
                }
            }
        }
    }
}

UsiCommon.containsID = function(list, id)
{
    if(!UsiCommon.isNull(list) && list.length > 0)
    {
        for(var i = 0; i < list.length; i++)
        {
            if(!UsiCommon.isNull(list[i]) && list[i].id == id){
                return true;
            }
        }
    }

    return false;
}

UsiCommon.click2chat = function(question)
{
    var url = '/SSA/upsellitChat/upsellitChat.jsp';
    if(typeof(question) != 'undefined' && question != null && question != 'null'){
        url = url +"?question="+ question;
    }

    window.resizeTo(screen.width-310,900);
    window.moveTo(0,0);
    window.open(url,'SMARTagent','scrollbars=no,scrolling=no,resizable=yes,status=no,toolbar=no,location=no,dialog,height=702px,width=375px,left='+(screen.width-300)+',top=0');
}

UsiCommon.openNewWindow = function(url_to_open){
    window.open(url_to_open,'UpSellit','scrollbars=yes,scrolling=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,dialog,height=768px,width=1024px,');
}