function showElement(id, sender) {
    if(document.getElementById) {
        var element = document.getElementById(id);
		
        if(element.tagName == 'A') {
            element.style.display = 'inline';
        } else {
            element.style.display = 'block';
        }
		
        sender.style.display = 'none';
    }
}

function hideElement(id) {
    if(document.getElementById) {
        var element = document.getElementById(id);
        element.style.display = 'none';
    }
}

function openExport(type) {
    if(document.getElementById) {
        var element = null;
		
        switch(type) {
            case "source":
                element = document.getElementById('Template_Content_ExportFrom');
                break;
            case "type":
                element = document.getElementById('Template_Content_ExportType');
                break;
        }
		
        window.open('/admin/contacts/excel.aspx?' + type + '=' + element.value,'export');
    }
}

function dispFileAttach()
{
    document.getElementById('file_attach').style.display="block";
    document.getElementById('tagline').style.display="none";
}

function hideFileAttach()
{
    document.getElementById('file_attach').style.display="none";
    document.getElementById('tagline').style.display="block";
}


function initializeMenu(){
    initializeSubMenus();
    $('body').click(function(){
        $('.hide').trigger('click');
    })

    $('#menu ul li').each(function(){
        if($(this).parent().parent().attr('id') == 'menu'){
            $(this).addClass('base');
        }
    })
    hideMenus();
    $('#menu p').css('display', 'block');
    var menuWidth = 400;
    $('#menu ul li').mouseover(function(){
        if($(this).hasClass('base')){
            $('#menu p').css('display', 'none');
            $('#menu p').text($('a', this).attr('title'));
            if($(this).attr("id") == "aeroprep"){
                setCookie("cat","aeroprep", false, "/", false, false);
            }
            else if($(this).attr("id") == "aerolube"){
                setCookie("cat","aerolube", false, "/", false, false);
            }

            else if($(this).attr("id") == "corban"){
                setCookie("cat","corban", false, "/", false, false);
            }

            else if($(this).attr("id") == "calla"){
                setCookie("cat","calla", false, "/", false, false);
            }

            $('.hide').trigger('click');
            $(this).removeClass('hov');
            $(this).addClass('hov');
            var width = $('body').width();
            var left = $(this).position().left;
            var offset = $('#wrapper').offset().left;
            if(left + offset + menuWidth > width){
                $('ul:eq(0)', this).css('left', 'auto');
                $('ul:eq(0)', this).css('right', 0);
            }
            else{
                $('ul:eq(0)', this).css('left', 0 + 'px');
            }
        }
    });
}

function hideMenus(){
    $('.hide').click(function(){
        $('#menu p').css('display', 'block');
        $('#menu ul li.base').removeClass('hov');   
        $('ul:eq(0)', '#menu ul li.base').css('left', '-9999em');
        $('ul:eq(0)', '#menu ul li.base').css('right', 'auto');
        return false;
    });
}

function initializeSubMenus(){
    $('#menu ul li ul li').mouseover(function(){
        $('ul:eq(0)', this).css('left', '-180px');
        $('ul:eq(0)', this).css('top', '20px');
        $('ul:eq(0)', this).css('z-index', '100');
    });

    $('#menu ul li ul li').mouseout(function(){
        $(this).removeClass('hov');
        $('ul:eq(0)', this).css('right', 'inherit');
        $('ul:eq(0)', this).css('left', '-9999em');
    })
}



function getMenu(){
    var id = window.location.search;
    if ('?id=MTAwMA==' == id ){
        setCookie("cat","aeroprep", false,"/", false, false);
        activateMenu("aeroprep");
        changeStylesheet("aeroprep");
			
    }
    else if('?id=MTAwMw==' == id){
        setCookie("cat","aerolube", false, "/", false, false);
        activateMenu("aerolube");
        changeStylesheet("aerolube");
    }
    else if('?id=MTAwMQ==' == id){
        setCookie("cat","corban", false, "/", false, false);
        activateMenu("corban");
        changeStylesheet("corban");
    }
		
    else if('?id=MTAwMg==' == id){
        setCookie("cat","calla", false, "/", false, false);
        activateMenu("calla");
        changeStylesheet("calla");
    }
		
    else if((getCookie("cat")) == "aeroprep"){
        activateMenu("aeroprep");
        changeStylesheet("aeroprep");
    }
		
    else if((getCookie("cat")) == "aerolube"){
        activateMenu("aerolube");
        changeStylesheet("aerolube");
    }
		
    else if((getCookie("cat"))== "corban"){
        activateMenu("corban");
        changeStylesheet("corban");
    }
		
    else if((getCookie("cat")) == "calla"){
        activateMenu("calla");
        changeStylesheet("calla");
    }
}

function changeStylesheet(sheet){
    var stylesheet = document.getElementById("alt");
    var link = "/webresources/stylesheets/" + sheet + ".css";
    stylesheet.href = '/webresources/stylesheets/' + sheet + '.css';
}


function activateMenu(id){
    var menu = document.getElementById("menu");
    var tagline = menu.getElementsByTagName("p");
    var currentCat = document.getElementById(id);
    var currMenu = currentCat.getElementsByTagName("a");
    currMenu[0].className = "on";
    var attr = currMenu[0].getAttribute("title");
    if(tagline[0].firstChild.nodeType == 3){
        tagline[0].firstChild.nodeValue = attr;

    }
}

function printDoc(){
    var links = document.getElementsByTagName("a");
    for(var i=0; i<links.length; i++){
        if(links[i].className == "print"){
            links[i].onclick = function() {
                window.print();
                return false;
            }
        }
    }
}


function requestMSDS(){
    var links = document.getElementsByTagName("a");
    for(var i=0; i<links.length; i++){
        if(links[i].className == "add"){
            links[i].onclick = function() {
                var msds = document.getElementById('Template_Content_MSDSList');
                var interested = document.getElementById('Template_Content_InterestedList');
                var hiddeninterested = document.getElementById('Template_Content_RequestedProducts');
                var hidden = new Array();
                var foundFlag = 0;
			
                if(msds.selectedIndex > -1) {
                    var newItem = new Option();
			
                    newItem.value = msds.options[msds.selectedIndex].value;
                    newItem.text = msds.options[msds.selectedIndex].text;
                    interested.options[interested.options.length] = newItem;
					
                    for(var j=0; j< hidden.length; j++) {
                        if (hidden[j] == newItem.value) {
                            foundFlag = 1;
                        }
                    }

                    if (foundFlag != 1) {
                        hidden.push(newItem.value);
                        hiddeninterested.value += hidden[j];
                        hiddeninterested.value += ",";
                    }
						
                    return false;
                }//end of if
                return false;
            }
        }
		
        if(links[i].className == "remove"){
            links[i].onclick = function() {
                var interested = document.getElementById('Template_Content_InterestedList');
                var hiddeninterested = document.getElementById('Template_Content_RequestedProducts');
                var hidden = new Array();
                var foundFlag = 0;
				
                hiddeninterested.value = "";
				
                if(interested.selectedIndex > -1) {
                    interested.remove(interested.selectedIndex);
                    ///	alert(interested.options.length);
						
                    for(var j=0; j< interested.options.length; j++) {
						
                        if (interested.options[j] != null) {
                            hiddeninterested.value += interested.options[j].value;
                            hiddeninterested.value += ",";
                        }
                    }
				
                    ///alert(hiddeninterested.value);
                    return false;
                }
                return false;
            }
        }
    }
}

function highlight(){
    if(!document.getElementsByTagName) return false;
    tables = document.getElementsByTagName("table");
    for(var j=0; j<tables.length; j++){
        var rows = tables[j].getElementsByTagName("tr");
        for(var i=0; i<rows.length; i++){
            var currColor;
            var textColor;
            rows[i].onmouseover = function(){
                currColor = this.style.backgroundColor;
                textColor = this.style.color;
				
                this.style.backgroundColor = "#FFF2CF";
                this.style.color = "#000000";
            }//end of mouseover
            rows[i].onmouseout = function(){
                this.style.backgroundColor = currColor;
                this.style.color = textColor;
            }//end of mouseout
        }//end of i loop
    }
}


function popImage(){
    var links = document.getElementsByTagName("a")
    for(i=0; i<links.length; i++){
        if(links[i].className == "card"){
            links[i].onclick = function(){
                var imgPath = this.href;
                window.open(imgPath,'pop','width='+ 360 +',' + 'height='+ 320)
                return false;
            }
        }
    }
}	
	

function getCategory(){
    var cookie = getCookie("cat");
    var stylesheet = document.getElementById("alt");
    if(cookie == "aeroprep"){
        stylesheet.href = '/webresources/stylesheets/' + cookie + '.css';
    }
	
    else if(cookie == "aerolube"){
        stylesheet.href = '/webresources/stylesheets/' + cookie + '.css';
    }
	
    else if(cookie == "corban"){
        stylesheet.href = '/webresources/stylesheets/' + cookie + '.css';
    }
	
    else if (cookie == "calla"){
        stylesheet.href = '/webresources/stylesheets/' + cookie + '.css';
    }
}



/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function scroll(content,subintro,scrollBox,scroller){
	
    var contentHeight = document.getElementById(content).offsetHeight;
    var subintroHeight = document.getElementById(subintro).offsetHeight;
    var scrollBoxHeight = document.getElementById(scrollBox).offsetHeight;
    var scrollerHeight = document.getElementById(scroller).offsetHeight;
	
    var subintro = document.getElementById(subintro);
    var scroller = document.getElementById(scroller);
    var scrollBox = document.getElementById(scrollBox);
	
	
    if(contentHeight < subintroHeight){
        var scrollH = 30;
        scroller.style.height = Math.round(scrollH) + "px";
        scrollDist = Math.round(scrollBoxHeight - scrollH);
		
        Drag.init(scroller,null,0,0,0,scrollDist);
		
        scroller.onDrag = function (x,y) {
            var scrollY = parseInt(scroller.style.top);
            var contentY = 0 + (scrollY * (contentHeight - subintroHeight) / scrollDist);
            subintro.style.top = contentY + "px";
        }
    }
    else{
        scrollH = 0;
        scroller.style.display = "none"
        scrollBox.style.display = "none"
    }
}

function highlightForm(){
    var forms = document.getElementsByTagName("dl");
    var dlForms = new Array;
    var currStyle;
    for(i=0; i<forms.length; i++){
        if((forms[i].className == "form") || (forms[i].className == "form50") || (forms[i].className == "search")){
		
            dlForms.push(forms[i]);
            for(j=0; j<dlForms.length; j++){
                var formInp = dlForms[j].getElementsByTagName("input");
				
                var selectInp = dlForms[j].getElementsByTagName("select");
                var textInp = dlForms[j].getElementsByTagName("textarea");
				
				
                for(z=0; z<formInp.length; z++){
					
                    formInp[z].onfocus = function(){
                        currStyle = this.className;
                        this.className = currStyle + " focus";
                    }
                    formInp[z].onblur = function(){
                        this.className = currStyle;
                    }
                }
                for(x=0; x<selectInp.length; x++){
					
                    selectInp[x].onfocus = function(){
                        currStyle = this.className
                        this.className = currStyle + " focus";
                    }
                    selectInp[x].onblur = function(){
                        this.className = currStyle;
                    }
                }
				
                for(y=0; y<textInp.length; y++){
					
                    textInp[y].onfocus = function(){
                        currStyle = this.className
                        this.className = currStyle + " focus";
                    }
                    textInp[y].onblur = function(){
                        this.className = currStyle;
                    }
                }
			
            }
			
        }//end of if for dlform
			
    }
}
 
 
function quote(){
    $('#requestQuote').click(function(){
        $('#dialog').show();
        $('html,body').animate({
            scrollTop: 0
        }, 1000);

        /*$('#dialog').dialog({
            width: 450,
            bgiframe: true,
            autoOpen: false,
            modal: true,
            title: 'Request a Quote'
        })
        $('#dialog').dialog('open');
        */
        return false;
    })

    $('#cancelDialog').click(function(){
        $('#dialog').hide();
        $('#dialog ul.error').remove();
        return false;
    })

    $('.submitDialog').click(function(){
        var error = '';
        var name = $('#dialog .name').attr('value');
        var phone = $('#dialog .phone').attr('value');
        var email = $('#dialog .email').attr('value');
        var question = $('#dialog .question').val();
        var quantity = $('#dialog .quantity').val();


        var emailRegex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;

        if(name === ''){
            error = error + '<li>You need to enter a name.</li>';
        }

        if(phone === ''){
            error = error + '<li>You need to enter a phone number.</li>';
        }

        if(email !== ''){
            var pass = emailRegex.test(email);
            if(!pass){
                error = error + '<li>You need to enter a valid email address.</li>';
            }
        }
        
        if($.trim(question) === ''){
            error = error + '<li>You need to enter a question.</li>';
        }

        if(isNaN(quantity)){
            error = error + '<li>You need to enter a number for quantity.</li>';
        }

        
        if(error.length == 0){
            return true;
        }
        else{
            $('#dialog ul.error').remove();
            $('#dialog dl').before('<ul class="error">' + error + '</ul>');
            $('html,body').animate({
                scrollTop: 0
            }, 1000);

            return false;
        }
    })
}


function showError(error){
    
}
