﻿/****************************************************************************
	Signup helpers
/****************************************************************************/

function saiSetReadonly(element,valueElement) {
    element.style.color = "#808080";            
    element.style.backgroundColor = "#F0F0F0";
    element.readOnly = true;
    element.value = valueElement.value;                
}

function saiReleaseReadonly(element) {
    element.style.color = "#000000";            
    element.style.backgroundColor = "#FFFFFF";
    if(!saiInitialUpdate)
        element.value = "";
    element.readOnly = false;                
}

/****************************************************************************
	Fact boxes
/****************************************************************************/

var lastFactBoxId = null;
var FactBoxDuration = 0.2;
function ShowFactBox(e, imgId, id) {
    Event.stop(e);

    var hideElements = false;

    if(lastFactBoxId!=null) {
        var lastElement = $(lastFactBoxId);
        if(lastElement.style.display!="none") {
                
            var selObjs = document.getElementsByTagName("SELECT");
            showTopElements();
            
            Effect.toggle(lastElement,'appear', { duration: FactBoxDuration });
        }
        else
            hideElements = true;
    }
    else
        hideElements = true;

    var offset = Position.cumulativeOffset($(imgId));
    var scrollOffset = Position.realOffset($(imgId));
    var boxElement = $(id);
    if(boxElement==null) {
        window.status = "Factbox not found: " + id;
        return;
    }
    
    // fix over right-side overflowing
    var windowWidth = document.body.offsetWidth;
    var offsetLeft = offset[0] - scrollOffset[0];
    var boxWidth = 200;
    if(offsetLeft+boxWidth>windowWidth) {
        var adjustment = windowWidth - (offsetLeft+boxWidth);
        offsetLeft += adjustment;        
    }
    
    // set position 
    boxElement.style.top = offset[1] + "px";
    boxElement.style.left = offsetLeft + "px";
    
    if (hideElements || (lastFactBoxId != null && lastFactBoxId != id))
        hideTopElements($(id));

    Effect.toggle(boxElement,'appear', { duration: FactBoxDuration });
    lastFactBoxId = id;
}

function HideFactBox(id) {
    showTopElements();
    Effect.toggle(id,'appear', { duration: FactBoxDuration });
}

// Added by Mikkel, for hiding dropdownlists
var TopElementsHidden = new Array();
function hideTopElements(element)
{
    var topElements = document.getElementsByTagName('Select');
    var elementCount = 0;
       
    for (var i = 0; i < topElements.length; i++)
    {
        if (doElementsCollide(element, topElements[i]))
        {
            TopElementsHidden[elementCount] = topElements[i];
            elementCount++;
            topElements[i].style.visibility = 'hidden';
        }
    }
}

function showTopElements()
{
    for (var i = 0; i < TopElementsHidden.length; i++)
        TopElementsHidden[i].style.visibility = 'visible';
        
    TopElementsHidden = new Array();
}

function doElementsCollide(obj1, obj2)
{
    var offset = Position.cumulativeOffset(obj2);
    var dimensions = Element.getDimensions(obj2);
    
    if (isPointInObject(offset[0], offset[1], obj1))
        return true;
        
    if (isPointInObject(offset[0]+dimensions.width, offset[1], obj1))
        return true;
        
    if (isPointInObject(offset[0]+dimensions.width, offset[1]+dimensions.height, obj1))
        return true;
        
    if (isPointInObject(offset[0], offset[1]+dimensions.height, obj1))
        return true;
        
    return false;
}

function isPointInObject(x, y, obj)
{
    var offset = Position.cumulativeOffset(obj);
    var dimensions = Element.getDimensions(obj);
    var posX = offset[0];
    var posY = offset[1];
    var height = dimensions.height;
    var width = dimensions.width;
        
    if (x < posX || x > (posX+width) )
        return false;
        
    if (y < posY || y > (posY+height) )
        return false;
        
    return true;
}

/****************************************************************************
	Grid hover effects etc.
/****************************************************************************/

// used by various grids to indicate row-hover
var prevCss = "";
var otherPrevCss = "";
function changeBackColor(row, highlight, twinGridRowId)
{
    var twinGridRow = null;
    if(twinGridRowId!=null)
        twinGridRow = $(twinGridRowId);

	if (highlight)
	{
		prevCss = row.className;
		if(twinGridRow!=null)
    		otherPrevCss = twinGridRow.className;
		
		row.className += " hover";

		if(twinGridRow!=null)
	        twinGridRow.className = row.className;
    } else {
		row.className = prevCss;

    	if(twinGridRow!=null)
	        twinGridRow.className = otherPrevCss;
    }

	/*if(twinGridRowId!=null)
	    $(twinGridRowId).className = row.className;*/
}

cgb = changeBackColor; // alias for less html-output on grids :)

// grid click
function gc(row,twinMasterGridRowId) {
    if(twinMasterGridRowId!=null)
        row = $(twinMasterGridRowId);

    location.href = "/fakta/" + row.cells[0].firstChild.id.substring(4) + "/generelt.aspx";
}

// grid click
function gc2(row,vareNummer) {
    location.href = "/fakta/" + vareNummer + "/generelt.aspx";
}

function GridLayoutAsssist(masterId, slaveId) {
    $(masterId).parentNode.style.height = ($(slaveId).offsetHeight)+"px";
}

/* 
todo: Consider using optimization from WEE:

	if (highlight)
	{
		prevBackgroundColor = row.style.backgroundColor;
		row.style.backgroundColor = hoverColor;
	}
	else
		row.style.backgroundColor = prevBackgroundColor;

	if(leftGrid!=null) {
	    var srcDiv = $(row).up('div', 1);//row.parentNode.parentNode.parentNode.parentNode;
	    var trgDiv = (leftGrid?srcDiv.next('div'):srcDiv.previous('div'));	    
	    
	    //trgDiv.firstChild.firstChild.rows[rowIndex+1].style.backgroundColor = row.style.backgroundColor;
	    var table = trgDiv.getElementsByTagName('table')[0];
	    //trgDiv.down('table').rows[rowIndex+1].style.backgroundColor = row.style.backgroundColor;
	    table.rows[rowIndex+1].style.backgroundColor = row.style.backgroundColor;
	}
 */

/****************************************************************************
	ImportantBox updater
/****************************************************************************/

var importantBoxContent = null, importantBox = null;
function ImportantBoxUpdate() {
    importantBoxContent = $("importantBoxContent");
    importantBox = $("importantBox");

    new Ajax.PeriodicalUpdater(importantBoxContent, 
        "/Handlers/ImportantInfoHandler.ashx", 
        { frequency : 10, insertion : ImportantBoxUpdateComplete });
}

function ImportantBoxUpdateComplete(sender, value) {
    var header = value.split("|")[0];
    var body = value.split("|")[1];

    var content = body.unescapeHTML().stripTags();
    
    if(content.length<7) {
        importantBox.style.display = "none";
        importantBoxContent.innerHTML = "";
    } else {
        importantBox.style.display = "";
        importantBoxContent.innerHTML = "<h2 style='color:#000000;font-size:13px'>"+header+"</h2>"+body;
    }
}

/****************************************************************************
	Character limitation fields
/****************************************************************************/

function removeCharacters(e)
{
    var target = Event.element(e);
    if(e.altKey) {
        target.focus();
        target.select();
    } else {
        var val = target.value.replace(/[^\,\.\d]/g, '');
        if(target.value!=val)
            target.value = val;
        // target.value = 
    }
}

