/* Global js-variables */
var IE = document.all ? true : false;
var timeoutId = null;
var lastTooltipId = null;
var searchedViaKeyPress = false ;

var aryClassElements = new Array();

function leftMenuSubs(category_id) {
    elementId = 'leftmenu_category_' + category_id ;
    aryClassElements.length = 0;
    getElementsByClassName( 'leftmenu_category', document.body );
    for ( var i = 0; i < aryClassElements.length; i++ ) {
        if (aryClassElements[i].id != elementId) {
		//aryClassElements[i].style.display='none' ;
	}
    }
	if (objElementHL = document.getElementById(elementId)) {
		objElementHL.style.display='block';
	}
}

function getElementsByClassName( strClassName, obj ) {
    if ( obj.className == strClassName ) {
        aryClassElements[aryClassElements.length] = obj;
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i] );
}


function signupSearchSocietyOnChange() {
    if (!searchedViaKeyPress) { // Check if search has already been made when enter was hit
    	signupSearchSociety() ;
    }
}

function signupSearchSociety() {
    var societyName = $F('SocietyName');

    $('signupSearchbox').innerHTML = 'Söker...';

    if (societyName.length > 1) {
        $('signupSearchbox').style.display = 'block';
        new Ajax.Updater('signupSearchbox', '/societies/search', 
        {
            parameters: { '_method': 'POST', 'data[Society][name]': $F('SocietyName'), 'data[User][society_id]': $F('UserSocietyId')
        }});
    }
	searchedViaKeyPress = false ;
}

function signupSearchSocietyOnKeyPress(e) {

    var key;
    if(window.event) {
        key = window.event.keyCode; //IE
    } else {
        key = e.which; //firefox
    }

    if (key == 13) {
        signupSearchSociety();
        searchedViaKeyPress = true ;
        return false;
    } else {
        return true;
    }

}

function selectSociety(societyId, title) {
    if (societyId != $('UserSocietyId').value) {
        $('choose-' + societyId).innerHTML = 'vald';
        $('choose-' + societyId).style.color = 'green';
        
        if ($('UserSocietyId').value != '') {
            oldId = $('UserSocietyId').value;
            if ($('choose-' + oldId) != null) {
                $('choose-' + oldId).innerHTML = 'välj';
                $('choose-' + oldId).style.color = '#000';
            }
        }
        
        $('UserSocietyId').value = societyId;
        $('SocietyName').value = title;
        $('signupSearchbox').innerHTML = $('searchResult-' + societyId).innerHTML ;

    }
    
    return false;
}

function clearField(element, defaultText, clear) {
    
    var name = element.name.toLowerCase();
    
    if (clear && element.value == defaultText) {
        element.value = '';
    } else if (!clear && element.value == '') {
        element.value = defaultText;
    }
}

function clearFieldById(id,  defaultText, clear) {
   var element = document.getElementById(id) ;
   clearField(element, defaultText, clear) ;
}

function clearUsername(element, clear) {
    if (element != null) {
        if (clear && element.value == '') {
            element.style.backgroundImage = "url('/img/input_login_unfilled.png')";
        } else if (element.value == '') {
            element.style.backgroundImage = "url('/img/input_username_filled.png')";
        } else {
            element.style.backgroundImage = "url('/img/input_login_unfilled.png')";
        }
    }
}

function clearPassword(element, clear) {
    if (element != null) {
        if (clear && element.value == '') {
            element.style.backgroundImage = "url('/img/input_login_unfilled.png')";
        } else if (element.value == '') {
            element.style.backgroundImage = "url('/img/input_password_filled.png')";
        } else {
            element.style.backgroundImage = "url('/img/input_login_unfilled.png')";
        }
    }
}

function starsHover(star) {
    vote = star.className.charAt(4);
    starContainer = star.parentNode;
    stars = starContainer.getElementsByTagName('img');
    for (i = 0; i < stars.length; i++) {
        currentStar = stars[i];
        if (vote > i) {
            currentStar.src = '/img/star_on.png';
        } else {
            currentStar.src = '/img/star_off.png';
        }
    }
    
    divs = starContainer.parentNode.getElementsByTagName('div');
    for (i = 0; i < divs.length; i++) {
        if (divs[i].className == 'starsCount') {
            divs[i].innerHTML = vote + ',0';
        }
    }
}
function starsOut(starContainer) {
    stars = starContainer.getElementsByTagName('img');
    inputs = starContainer.getElementsByTagName('input');
    vote = inputs[0].value;
    for (i = 0; i < stars.length; i++) {
        currentStar = stars[i];
        if (vote > i) {
            currentStar.src = '/img/star_on.png';
        } else {
            currentStar.src = '/img/star_off.png';
        }
    }
    
    divs = starContainer.parentNode.getElementsByTagName('div');
    for (i = 0; i < divs.length; i++) {
        if (divs[i].className == 'starsCount') {
            divs[i].innerHTML = vote.replace('.', ',');
        }
    }
}
function starsVote(star, type, formVote) {
    vote = star.className.charAt(4);
    starContainer = star.parentNode;
    inputs = starContainer.getElementsByTagName('input');
    
    if (formVote) {
        inputs[0].value = vote;
    } else {
        id = starContainer.id.substr(6);
        new Ajax.Request('/votes/vote/' + type + '/' + id + '/' + vote + '.json', {
            method:'get',
            requestHeaders: {Accept: 'application/json'},
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON(true);
                if (json.status == 0) {
                    parentNode = starContainer.parentNode;
                    parentNode.innerHTML = json.template;
                    
                    $('lightbox').style.position = 'fixed';
                    lightbox.prototype.content = '/votes/voteDone';
                    lightbox.prototype.displayLightbox("block");
                    
                } else if (json.status == 1) {
                }
            }
        });
    }
    
}

function inviteBoardMember() {
    
    var name = $('UserName').value.toLowerCase();
    var titleId = $('UserBoardTitleId_hidden').value;
    var email = $('UserEmail').value.toLowerCase();
    
    if (name == '' || name == 'namn' || titleId == '' || email == '' || email == 'email') {
        return false;
    } else {
        
        lightbox.prototype.content = '/users/inviteBoardMember';
        lightbox.prototype.displayLightbox("block");
        
        return false;
    }
}

function inviteBoardMemberSave() {
    new Ajax.Updater('lbMiddleContent', '/users/inviteBoardMember/true', {});
}

function editNoticeText(saving) {
    if (saving) {
        $('SocietyNoticeTextForm').style.display = 'none'; 
        $('noticeText').style.display = 'block'; 
        $('noticeText').innerHTML =  $('SocietyNoticeText').value; 
        $('buttonEditNotice').style.display = 'block';
    } else {
        $('SocietyNoticeTextForm').style.display = 'block'; 
        $('noticeText').style.display = 'none'; 
        $('buttonEditNotice').style.display = 'none';
    }
    return false;
}

function editContactInfo(field, save) {
    
    if (save) {
		var value_to_save ;
		if ($('input-' + field).type == 'checkbox') {
			value_to_save = $('input-' + field).checked ? 1 : 0 ;
		} else {
			value_to_save = $('input-' + field).value ;
		}
		
        new Ajax.Request('/societies/saveContactField', {
            asynchronous:true, 
            evalScripts:true,
            parameters: {
                'data[field]': field,
                'data[value]': value_to_save
            },
            onSuccess: function(transport) {
                if (field == 'notice_text') {
                    $('text-' + field).innerHTML = $('input-' + field).value.replace(/\n/g, '<br />');
                } else if (field == 'active') {
                    if (value_to_save == 1) {
                        $('text-' + field).innerHTML = 'Ja';
                    } else {
                        $('text-' + field).innerHTML = 'Nej';
                    }
	                
                } else if (field == 'member_count') {
                    if ($('input-' + field).value == '') {
                        $('text-' + field).innerHTML = 'Antal medlemmar';
                    } else {
                        $('text-' + field).innerHTML = $('input-' + field).value + ' medlemmar';
                    }
                } else {
                    $('text-' + field).innerHTML = $('input-' + field).value;
                }
                
                switchContact(field, true);
            }
        });

        
    } else {
        switchContact(field, false);
    }
    
    return false;
}

function switchContact(field, hide) {
    if (hide) {
        $('input-' + field).style.display = 'none';
        $('save-' + field).style.display = 'none';
        $('cancel-' + field).style.display = 'none';
        $('text-' + field).style.display = 'inline';
        $('edit-' + field).style.display = 'block';
    } else {
        $('input-' + field).style.display = 'inline';
        $('save-' + field).style.display = 'block';
        $('cancel-' + field).style.display = 'block';
        $('text-' + field).style.display = 'none';
        $('edit-' + field).style.display = 'none';
    }
    
    return false;
}

function createEventForm(show) {
    if (show) {
        $('CalenderEventAddForm').style.display = 'block';
    } else {
        $('CalenderEventAddForm').style.display = 'none';
    }
    
    return false;
}

function editRow(type, id, status) {
    if (status) {
        
        if (type == 'document') {
            postParameters = { 
                'data[id]': id,
                'data[type]': type,
                'data[title]': $('inputDocTitle-' + id).value,
                'data[description]': $('inputDocDescr-' + id).value,
                'data[filename]': $('inputDocFilename-' + id).value
            };
        } else if (type == 'member') {
            postParameters = { 
                'data[id]': id,
                'data[type]': type,
                'data[name]': $('inputMemberName-' + id).value,
                'data[board_title_id]': $('inputMemberTitle-' + id + '_hidden').value,
                'data[email]': $('inputMemberEmail-' + id).value
            };
        } else if (type == 'event') {
            postParameters = $('CalenderEventAddForm-' + id).serialize(true);
        } else if (type == 'notice') {
            postParameters = { 
                'data[id]': id,
                'data[type]': type,
                'data[title]': $('inputNoticeTitle-' + id).value,
                'data[content]': $('inputNoticeContent-' + id).value
            };
        } else if (type == 'supplier') {
            
            var holder = $$('#supplierItem' + id + ' .supplierChange')[0];
            var phoneNumber = holder.select('.inputPhoneNumber')[0].value;
            var info = holder.select('.inputInfo')[0].value ;
            var comment = holder.select('.inputComment')[0].value;
            postParameters = {
                'data[id]': id,
                'data[type]' : type,
                'data[phone_number]': phoneNumber,
                'data[info]': info,
                'data[comment]': comment
            };
        }
        
        if (postParameters) {
            
            new Ajax.Request('/societies/saveRow', {
                asynchronous:true, 
                evalScripts:true,
                parameters: postParameters,
                onSuccess: function(transport) {
                    if (type == 'document') {
                        var title = $('inputDocTitle-' + id).value;
                        var description = $('inputDocDescr-' + id).value;
                        $('contentDocTitle-' + id).innerHTML = '<strong>' + title + '</strong> ' + description;
                    } else if (type == 'member') {
                        // Change html
                        var name = $('inputMemberName-' + id).value;
                        var title = $('inputMemberTitle-' + id).options[$('inputMemberTitle-' + id).selectedIndex].innerHTML;
                        var phone = $('inputMemberPhone-' + id).value;
                        var email = $('inputMemberEmail-' + id).value;
                        
                        var output = name + ', ' + title;
                        if (phone != '') {
                            output = output + ', ' + phone;
                        }
                        if (email != '') {
                            output = output + ', <a href="mailto:' + email + '">' + email + '</a>';
                        }
                        
                        $('memberInfo-' + id).innerHTML = output;
                    } else if (type == 'event') {
                        var title = $('calenderEventTitle-' + id).value;
                        var description = $('calenderEventDescr-' + id).value;
                        $('contentEventTitle-' + id).innerHTML = transport.responseText;
                    } else if (type == 'notice') {
                        $('noticeTitle-' + id).innerHTML = $('inputNoticeTitle-' + id).value;
                        $('noticeContent-' + id).innerHTML = $('inputNoticeContent-' + id).value.replace(/\n/g, '<br />');
                        $('noticeSmallDescr-' + id).innerHTML = transport.responseText;
                    } else if (type == 'supplier') {
                        var commentsHolder = $$('#supplierItem' + id + ' .supplierMainInfo .commentsCount')[0];
                        if (commentsHolder) { commentsHolder.innerHTML = transport.responseText; }
                        var commentInput = $$('#supplierItem' + id + ' .supplierChange .inputComment')[0];
                        commentInput.value = 'Lägg till ny kommentar om leverantören för besökare på Styrelseguiden.se';
                        
                    }
                    
                    switchAll(type, id, true);
                }
            });
        }
    } else {
        switchAll(type, id, false);
    }
    
    return false;
}

function switchAll(type, id, hide) {
    if (type == 'document') {
        if (hide) {
            $('contentDocTitle-' + id).style.display = 'block';
            $('inputDocEdit-' + id).style.display = 'block';
            $('inputDocSave-' + id).style.display = 'block';
            $('inputDocTitle-' + id).style.display = 'none';
            $('inputDocDescr-' + id).style.display = 'none';
            $('inputDocFilename-' + id).style.display = 'none';
            $('inputChangeFilename-' + id).style.display = 'none';
            $('docDelete-' + id).style.display = 'none';
            $('docCancel-' + id).style.display = 'none';
            $('inputDocSave-' + id).style.display = 'none';
        } else {
            $('inputDocTitle-' + id).style.display = 'block';
            $('inputDocDescr-' + id).style.display = 'block';
            $('inputDocFilename-' + id).style.display = 'block';
            $('inputChangeFilename-' + id).style.display = 'block';
            $('inputDocSave-' + id).style.display = 'block';
            $('docDelete-' + id).style.display = 'block';
            $('docCancel-' + id).style.display = 'block';
            $('contentDocTitle-' + id).style.display = 'none';
            $('inputDocEdit-' + id).style.display = 'none';
        }
    } else if (type == 'member') {
        if (hide) {
            $('inputMemberEdit-' + id).style.display = 'block';
            $('memberInfo-' + id).style.display = 'block';
            $('inputMemberTitle-' + id).style.display = 'none';
            $('inputMemberName-' + id).style.display = 'none';
            $('inputMemberEmail-' + id).style.display = 'none';
            $('inputMemberSave-' + id).style.display = 'none';
            $('memberDelete-' + id).style.display = 'none';
            $('memberCancel-' + id).style.display = 'none';
        } else {
            $('inputMemberTitle-' + id).style.display = 'block';
            $('inputMemberName-' + id).style.display = 'block';
            $('inputMemberEmail-' + id).style.display = 'block';
            $('inputMemberSave-' + id).style.display = 'block';
            $('memberCancel-' + id).style.display = 'block';
            $('memberInfo-' + id).style.display = 'none';
            $('memberDelete-' + id).style.display = 'block';
            $('inputMemberEdit-' + id).style.display = 'none';
        }
    } else if (type == 'event') {
        if (hide) {
            $('inputEventEdit-' + id).style.display = 'block';
            $('contentEventTitle-' + id).style.display = 'block';
            $('CalenderEventAddForm-' + id).style.display = 'none';
            $('inputEventSave-' + id).style.display = 'none';
            $('eventDelete-' + id).style.display = 'none';
            $('eventCancel-' + id).style.display = 'none';
        } else {
            $('inputEventEdit-' + id).style.display = 'none';
            $('contentEventTitle-' + id).style.display = 'none';
            $('CalenderEventAddForm-' + id).style.display = 'block';
            $('inputEventSave-' + id).style.display = 'block';
            $('eventDelete-' + id).style.display = 'block';
            $('eventCancel-' + id).style.display = 'block';
        }
    } else if (type == 'notice') {
        if (hide) {
            $('inputNoticeEdit-' + id).style.display = 'block';
            $('textNotice-' + id).style.display = 'block';
            $('inputNoticeSave-' + id).style.display = 'none';
            $('inputNoticeCancel-' + id).style.display = 'none';
            $('inputNoticeDelete-' + id).style.display = 'none';
            $('inputsNotice-' + id).style.display = 'none';
        } else {
            $('inputNoticeEdit-' + id).style.display = 'none';
            $('textNotice-' + id).style.display = 'none';
            $('inputNoticeSave-' + id).style.display = 'block';
            $('inputNoticeCancel-' + id).style.display = 'block';
            $('inputNoticeDelete-' + id).style.display = 'block';
            $('inputsNotice-' + id).style.display = 'block';
        }
    } else if (type == 'supplier') {
        
        var holder = $('supplierItem' + id);
        
        if (hide) {
            holder.select('.supplierChange')[0].hide();
            holder.select('.supplierMainInfo')[0].show();
        } else {
            holder.select('.supplierChange')[0].show();
            holder.select('.supplierMainInfo')[0].hide();
        }
    }
    
    return false;
}

function societySearchSupplierOnChange() {
    if (!searchedViaKeyPress) { // Check if search has already been made when enter was hit
    	societySearchSupplier() ;
    }
}

function societySearchSupplier() {
    
    var supplierName = $F('AddSupplierAddName');
    $('AddSupplierId').value = '0';
    $('supplierSearchResult').show();
    $('supplierSearchResultText').show();
    
    if (supplierName.length > 1) {
        $('supplierSearchResult').innerHTML = 'Söker...';
        new Ajax.Updater('supplierSearchResult', '/suppliers/ajaxSearchByName', 
        {
            parameters: {
                '_method': 'POST', 
                'data[Supplier][name]': supplierName
        }});
    } else {
        $('supplierSearchResult').innerHTML = 'Ange minst 2 bokstäver.';
    }
    
	return false;
}

function societySearchSupplierOnKeyPress(e) {

    var key;
    if(window.event) {
        key = window.event.keyCode; //IE
    } else {
        key = e.which; //firefox
    }

    if (key == 13) {
        societySearchSupplier() ;
        searchedViaKeyPress = true ;
        return false;
    } else {
	return true ;
    }
}


function societySearchSupplierChoose(supplierId) {
    
    if ($F('AddSupplierId') != '' && $F('AddSupplierId') != '0') {
        listItem = $('supplierSearchItem' + $F('AddSupplierId'));
        link = listItem.select('.chooseLink')[0];
        link.removeClassName('greenLink');
        link.innerHTML = 'välj';
    }
    
    listItem = $('supplierSearchItem' + supplierId);
    link = listItem.select('.chooseLink')[0];
    supplierName = listItem.select('.supplierName')[0];
    
    link.addClassName('greenLink');
    link.innerHTML = 'vald';
    
    $('AddSupplierId').value = supplierId;
    $('AddSupplierAddName').value = supplierName.innerHTML.replace(/&amp;/g,"&");
    
    return false;
}

/* Function to open lightbox with special size */
function societyLbAddSupplier() {
    
    $('lightbox').style.width = '676px';
    lightbox.prototype.content = '/societies/lbAddSupplier';
    lightbox.prototype.displayLightbox("block");
    return false;
}

/* Submit for society add supplier lightbox */
function societyLbAddSupplierSubmit() {
    
    alert('test');
    return false;
    
    
    
}

function showHideElement(id) {
    if ($(id).style.display == 'none') {
        $(id).style.display = 'block'; 
    } else {
        $(id).style.display = 'none'; 
    }
    
    return false;
}

function expandItem(prefix) {
    var listItem = $(prefix + '_listItem');
    var expItem = $(prefix + '_expItem');
    
    if (listItem.style.display != 'none') {
        listItem.style.display = 'none';
        expItem.style.display = 'block';
    } else {
        listItem.style.display = 'block';
        expItem.style.display = 'none';
    }
    
    return false;
}

// Variables for addSupplierSubmit()
var addSupplierSubmitAction = '';

function addSupplierSubmit() {
    
    if ($('SupplierCurrentSupplier').checked == false) {
        return true;
    }
    
    var supplierCategoryId = $('SupplierSupplierCategoryId').options[$('SupplierSupplierCategoryId').selectedIndex].value;
    var supplierId = $('SupplierId').value;
    
    if (supplierId == 0) {
        return true;
    }
    
    new Ajax.Request('/suppliers/existsSameType/' + supplierId, {
        onSuccess: function(transport) {
            if (transport.responseText == '1') {
                $('lightbox').style.height = '190px';
                lightbox.prototype.content = '/suppliers/addSameType/' + supplierCategoryId;
                lightbox.prototype.displayLightbox("block");
            } else if(transport.responseText == '2') {
                $('lightbox').style.height = '190px';
                lightbox.prototype.content = '/suppliers/displayExistsSameType/' + supplierId;
                lightbox.prototype.displayLightbox("block");
            } else if (transport.responseText == 'err') {
                return false;
            } else {
                return true;
            }
        }
    });

    return false;
}

function setAddSupplierSubmitAction(action) {
    $('SupplierAction').value = action;
    $('SupplierAddForm').submit(); 
    return false;
}

function changeDocumentFilename(id) {
    $('inputDocFilename-' + id).disabled = false;
    $('inputDocFilename-' + id).className = 'textInput textInput7-active';
    
    return false;
}

function register() {
    $('lightbox').style.top = '250px';
    $('lightbox').style.position = 'absolute';
    lightbox.prototype.content = '/users/create';
    lightbox.prototype.displayLightbox("block");
}

function forgotPassword() {
    lightbox.prototype.content = '/users/forgotPassword';
    lightbox.prototype.displayLightbox("block");
    return false;
}

function tooltip(id) {
    clearTimeout(timeoutId);
    var element = $('ttTrigger-' + id);

    if (id != lastTooltipId) {
        
        $('tooltip').style.display = 'none';
        var positions = element.cumulativeOffset();
        var width = element.getWidth();
        
        var top = positions[1] - 100;
        var left = positions[0] - 102 + width/2;
        
        $('tooltip').innerHTML = $('tooltip-content-' + id).innerHTML;
        $('tooltip').style.top = top + 'px';
        $('tooltip').style.left = left + 'px';
        Effect.Appear('tooltip', { duration: 0.3 });
        lastTooltipId = id;
    }
}

function hideTooltip() {
    $('tooltip').innerHTML = '';
    $('tooltip').hide();
    lastTooltipId = null;
}

function tooltipMouseOut() {
    timeoutId = setTimeout('hideTooltip()', 300);
}

function tooltipMouseOver() {
    clearTimeout(timeoutId);
}

function showHideItem(id) {
    if ($('short-' + id).style.display == 'none') {
        $('short-' + id).style.display = 'block'; 
        if ($('expanded-' + id) != null) {
            $('expanded-' + id).style.display = 'none'; 
        }
        if ($('expanded-' + id + '-1') != null) {
            $('expanded-' + id + '-1').style.display = 'none'; 
        }
        if ($('expanded-' + id + '-2') != null) {
            $('expanded-' + id + '-2').style.display = 'none'; 
        }
    } else {
        $('short-' + id).style.display = 'none'; 
        if ($('expanded-' + id) != null) {
            $('expanded-' + id).style.display = 'block'; 
        }
        if ($('expanded-' + id + '-1') != null) {
            $('expanded-' + id + '-1').style.display = 'block'; 
        }
        if ($('expanded-' + id + '-2') != null) {
            $('expanded-' + id + '-2').style.display = 'block'; 
        }
    }
    
    return false;
}

function hideItem(id) {
    $('short-' + id).style.display = 'block'; 
    if ($('expanded-' + id) != null) {
        $('expanded-' + id).style.display = 'none'; 
    }
    if ($('expanded-' + id + '-1') != null) {
        $('expanded-' + id + '-1').style.display = 'none'; 
    }
    if ($('expanded-' + id + '-2') != null) {
        $('expanded-' + id + '-2').style.display = 'none'; 
    }

    
    return false;
}

function showItem(id) {

    $('short-' + id).style.display = 'none'; 
    if ($('expanded-' + id) != null) {
        $('expanded-' + id).style.display = 'block'; 
    }
    if ($('expanded-' + id + '-1') != null) {
        $('expanded-' + id + '-1').style.display = 'block'; 
    }
    if ($('expanded-' + id + '-2') != null) {
        $('expanded-' + id + '-2').style.display = 'block'; 
    }

    
    return false;
}


function ajaxFetchSupplierInfo(supplierId) {
    
    new Ajax.Request('/suppliers/populateAddSupplierForm/' + supplierId + '.json', {
        method:'get',
        requestHeaders: {Accept: 'application/json'},
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON(true);
            $('SupplierId').value = json.Supplier.id;
            
            if (json.Supplier.description != '') {
                $('SupplierDescription').value = json.Supplier.description;
            } else {
                $('SupplierDescription').value = 'Beskrivning';
            }
            $('SupplierDescription').disabled = true;
            
            $('SupplierSupplierCategoryId').selectedIndex = json.Supplier.supplier_category_id;
            $('SupplierSupplierCategoryId').disabled = true;
            
            if (json.Supplier.webpage != '') {
                $('SupplierWebpage').value = json.Supplier.webpage;
            } else {
                $('SupplierWebpage').value = 'Webbsida';
            }
            $('SupplierWebpage').disabled = true;
            
            if (json.Supplier.city != '') {
                $('SupplierCity').value = json.Supplier.city;
            } else {
                $('SupplierCity').value = 'Stad';
            }
            $('SupplierCity').disabled = true;
            
            dropdown = $('SupplierCountyId');
            for (i = 0; i < dropdown.options.length; i++) {
                if (dropdown.options[i].value == json.Supplier.county_id) {
                    break;
                }
            }
            dropdown.selectedIndex = i;
            dropdown.disabled = true;
            
            if (json.Supplier.address != '') {
                $('SupplierAddress').value = json.Supplier.address;
            } else {
                $('SupplierAddress').value = 'Adress';
            }
            $('SupplierAddress').disabled = true;
            
            if (json.Supplier.phoneNumber != '') {
                $('SupplierPhoneNumber').value = json.Supplier.phoneNumber;
            } else {
                $('SupplierPhoneNumber').value = 'Telefonnummer';
            }
            $('SupplierPhoneNumber').disabled = true;
            
        }
    });

}

function supplierSelectCounty() {
    var href = window.location.href;
    id = dropdownGetValue('selectCountyId');
    
    $('formChooseCounty').submit();
}

function toggleSearch(adv) {
    if (adv) {
        Effect.SlideUp('searchBasic', { duration: 0.5 })
        setTimeout("Effect.SlideDown('searchAdvanced', { duration: 1.0 })", 600);
    } else {
        
        Effect.SlideUp('searchAdvanced', { duration: 1.0 })
        setTimeout("Effect.SlideDown('searchBasic', { duration: 0.5 })", 1100);
    }
    
    return false;
}

function societySupplierSave(supplierId) {
    
    console.log('test');
    return false;
    
    
}

function supplierCommentDone(id) {
    var textareas = $('makeComment-' + id).select('.textarea3');
    textareas[0].value = '';
    $('makeComment-' + id).hide();
}

function dropdownDisplayList(element) {
    element = $(element);
    var container = element.up();
    container.select('.dropdownList').each(function(el) {
        el.toggleClassName('hidden');
    });
}

function dropdownSelect(element, onchange) {
    element = $(element);
    
    var container = element.up().up().up();
    container.select('.dropdownSelect').each(function(el) {
        el.innerHTML = element.innerHTML;
    });
    
    element.up().select('span').each(function(el) {
        value = el.innerHTML;
    });
    
    container.select('input[type="hidden"]').each(function(el) {
        el.value = value;
    });
    container.select('.dropdownList').each(function(el) {
        el.toggleClassName('hidden');
    });
    
    if (onchange != '' && onchange != null) {
        eval(onchange);
    }
    
}

function dropdownSetToValue(id, value) {
    $(id).select('input[type="hidden"]').each(function(el) {
        el.value = value;
    });

    if (value == '' || value == 0) {
        var select = $(id).select('.dropdownSelect');
        var title = $(id).select('.dropdownTitle');
        select[0].innerHTML = title[0].innerHTML;
    } else {
        $(id).select('span').each(function(span) {
            if (span.innerHTML == value) {
                var select = $(id).select('.dropdownSelect');
                var link = span.up().select('a');
                select[0].innerHTML = link[0].innerHTML;
            }
        });
    }
}

function dropdownGetValue(id) {
    
    var value = null;
    $(id).select('input[type="hidden"]').each(function(el) {
        value = el.value;
    });
    
    return value;
}

function lightboxDisplayFixed(url) {
    lightbox.prototype.content = url;
    lightbox.prototype.displayLightbox("block");
    return true;
}

function checkboxToggle(id) {
    var hidden = $(id);
    var img = $(id + '_checkbox');
    
    if (hidden.value == '1') {
        img.src = '/img/checkbox_off_ffffff.png';
        hidden.value = '0';
    } else {
        img.src = '/img/checkbox_on_ffffff.png';   
        hidden.value = '1';
    }
}

function checkboxInit(id) {
    
    var hidden = $(id);
    var img = $(id + '_checkbox');
    var label = $(id + '_label');
    
    if (hidden.value == '1') {
        img.src = '/img/checkbox_on_ffffff.png';
        hidden.value = '1';
    } else {
        img.src = '/img/checkbox_off_ffffff.png';   
        hidden.value = '0';
    }
    
    img.onclick = function() { 
        checkboxToggle(id);
    }
    
    label.onclick = function() { 
        checkboxToggle(id);
    }
    
}

var oldMenuImage = null;

function documentLoad() {
    $$('div.left-menu ul li a').each(function(element) {
        
        element = element.up();
        
        element.onmouseover = function() {
            element.addClassName('hover');
            element.select('img').each(function(img) {
                oldMenuImage = img.src;
		oldFileName = oldMenuImage.substring(oldMenuImage.lastIndexOf('/')+1);
		switch(oldFileName) {
			case "leftmenu_minus_black.png" : case "leftmenu_minus_blue.png" : img.src = "/img/leftmenu_minus_blue.png" ; break ;
			case "leftmenu_plus_black.png" : img.src = "/img/leftmenu_plus_blue.png" ; break ;
			default : img.src = '/img/leftmenu_arrow_blue_dddddd.png';
		}
            });
        }
        element.onmouseout = function() {
            element.removeClassName('hover');
            element.select('img').each(function(img) {
                img.src = oldMenuImage;
            });
        }
    });
}

