﻿function highlightElement(bool, elem) {
    var highlightedClass = "input2";
    var normalClass = "input1";

    var oldclass = elem.className;
    if (bool) {
        var newclass = oldclass.replace(normalClass, highlightedClass);
    } else {
        var newclass = oldclass.replace(highlightedClass, normalClass);
    }
    elem.className = newclass;
}

function colorFormValues() {
    $('input').each(function(i) {
        var relValue = trim(this.getAttribute('rel'));
        if (relValue != null && (trim(this.value) != relValue)) {
            highlightElement(true, this);
        }
    });

    $('select').each(function(i) {
        var relValue = trim(this.getAttribute('rel'));
        selIndex = this.selectedIndex;
        if (relValue != null && (this.selectedIndex > 0 && trim(document.getElementById(this.id)[selIndex].value) != relValue)) {
            highlightElement(true, this);
        }
    });

    $('textarea').each(function(i) {
        var relValue = trim(this.getAttribute('rel'));
        if (relValue != null && (trim(this.value) != relValue)) {
            highlightElement(true, this);
        }
    });
}

function clearElement(elem) {
    elem.value = '';
    highlightElement(true, elem);
}

function resetElement(elem) {
    elem.value = elem.defaultValue;
    highlightElement(false, elem);
}

function formFunctions() {
    $('input.clearMe').focus(function() {
        if (trim(this.value) == trim(this.defaultValue)) {
            clearElement(this);
        }
    });

    $('input.clearMe').blur(function() {
        if (this.value == '') {
            resetElement(this);
        }
    });

    $('input.password').focus(function() {
        if (this.type != 'password') {
            changeInputType(this, 'password');
            clearElement(this);
        }
    });

    $('select').change(function() {
        if (this.value != trim(document.getElementById(this.id).options[0].value)) {
            highlightElement(true, this);
        }
        else {
            highlightElement(false, this);
        }
    });

    $('textarea.clearMe').focus(function() {
        if (trim(this.value) == trim(this.defaultValue)) {
            clearElement(this);
        }
    });

    $('textarea.clearMe').blur(function() {
        if (trim(this.value) == '') {
            resetElement(this);
        }
    });
}

function changeInputType(oldObject, oType) {
    var newObject = document.createElement('input');
    newObject.type = oType;
    if (oldObject.size) newObject.size = oldObject.size;
    if (oldObject.value) newObject.value = oldObject.value;
    if (oldObject.name) newObject.name = oldObject.name;
    if (oldObject.id) newObject.id = oldObject.id;
    if (oldObject.className) newObject.className = oldObject.className;
    oldObject.parentNode.replaceChild(newObject, oldObject);
    setTimeout(function() { newObject.focus(); }, 10);
    return newObject;
}

function submitForm(FormName) {
    clearDefaults(FormName);
    document.getElementById(FormName).submit();
}

function clearDefaults(FormName) {
    $('input.clearMe').each(function(i) {
        if (trim(this.value) == trim(this.defaultValue)) this.value = '';
    });
    $('textarea.clearMe').each(function(i) {
        if (trim(this.value) == trim(this.defaultValue)) this.value = '';
    });
}

function checkCR(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);

    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if ((evt.keyCode == 13) && (node.type == "text" || node.type == "password")) {
        submitForm(node.form.id);
    } else if ((evt.keyCode == 13) && (node.type == "file")) {
        submitForm(node.form.id);
    }
}

function simple_tooltip(target_items, name) {
    $(target_items).each(function(i) {

        var element = "<div class='" + name + "' id='" + name + i + "'>";
        if ($(this).attr("alt") != "" && $(this).attr('alt') != "undefined") {
            element = element + "<p class='tooltipTitle'>" + $(this).attr('alt') + "</p><p>" + $(this).attr('title') + "</p></div>";
            $(this).removeAttr("alt");
        }
        else {
            element = element + "<p>" + $(this).attr('title') + "</p></div>";
        }
        $("body").append(element);
        var my_tooltip = $("#" + name + i);

        $(this).removeAttr("title").mouseover(function() {
            my_tooltip.css({ display: "none" }).fadeIn(400);
        }).mousemove(function(kmouse) {
            my_tooltip.css({ left: kmouse.pageX + 15, top: kmouse.pageY + 15 });
        }).mouseout(function() {
            my_tooltip.fadeOut(400);
        });
    });
}

function shopHighlights() {
    /* Simultaneous highlighting of links on shop bulk page */
    $('.bulkProductDescription dt').hover(
		function() {
		    $(this).next().find('a:link').addClass('hover');
		},
		function() {
		    $(this).next().find('a:link').removeClass('hover');
		});

    /* Simultaneous highlighting of links on shop bulk page, in reverse */
		$('.bulkProductDescription dd a').hover(
		function() {
		    $(this).parent().prev().find('a').addClass('hover');
		},
		function() {
		    $(this).parent().prev().find('a').removeClass('hover');
		});
}

function trim(stringToTrim) {
    if (stringToTrim != null) {
        return stringToTrim.replace(/^\s+|\s+$/g, '');
    }
}

function faqHover() {
    /* Applies mouseover on FAQ titles */
    $('dl#faq dt').hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		});
}

$(document).ready(function() {
    colorFormValues();

    formFunctions();

    document.onkeypress = checkCR;

    simple_tooltip('img.showToolTip', 'tooltip');

    shopHighlights();

    faqHover();

    $('.profile_orderhistory_table').parent().addClass('orderHistoryBox');
});