//************************ HFC FUNKTIONER  *********************
function toggleLines()
	{
		var _divLines = document.getElementById('EcomCartLines');
		if (showAllLines == false)
		{
			showAllLines = true;
			_divLines.style.overflow = 'visible';
			_divLines.style.maxHeight = '1000px';
			_divLines.style.height = '';
			document.getElementById('imgShowAllLines').src = '/files/system/hide_lines.png';
		}
		
		else
		{
			showAllLines = false;
			_divLines.style.overflow = 'hidden';
			_divLines.style.height = '171px';
			_divLines.style.maxHeight = '171px';
			
			document.getElementById('imgShowAllLines').src = '/files/system/show_lines.png';
		}
	}

function multiOrderGroupClick()
	{
		
		var includeElements = getElementsByIdPart('qty_',null,'input')
		var len = includeElements.length;
		//var firstProduct = true;
		
		var productAdded = false
		for (i = 0; i < len; i++) 
		{
			productid = includeElements[i].id.replace('qty_','');
			qoQuantity = includeElements[i];
			
			if ((qoQuantity.value != '') && (productid != ''))
			{
				quantity = new Number(qoQuantity.value);
				
				if (quantity > 0)
				{
					if (!productAdded)
					{	
						//Show AJAX loader
						showBox(document.getElementById('UpdateProgressSmall_EcomCart'));
						document.getElementById('UpdateProgress_EcomCart').style.display = 'block';
					
						productAdded = true;
					}
					ajaxOrder(productid, quantity, false)
					
				}
			//Clear alle fields;
			qoQuantity.value = '0';
			}
			
		}
		if (productAdded)
		{
			//Update cart
			//requestPageContent('/Default.aspx?id=' + AjaxCart_PageID,'EcomCart');
			requestAjaxCartPage('/Default.aspx?id=' + AjaxCart_PageID,'EcomCart');
			//isCartEmpty('1',1);
		}
	}
	
function OnBodyLoad()
{
    //InitializeMenu();
    
    if (document.getElementById('quickorderProductId') != null)
    {
        document.getElementById('quickorderProductId').focus();
    }
}


//------------------------------------------------------------------------------------
function showDiscount(tmpevent, sender)
{
    divContainer = sender.parentNode;
    
    for (var index=0; index < divContainer.childNodes.length; index++) { 
        if (divContainer.childNodes[index].id == 'discount')
        {
            tmpDiscountBox = divContainer.childNodes[index];
            break;
        }
    } 
    
    var ypos 
    var maxypos 
    var bodyHight = getBodyHeight();

    maxypos = tmpevent.clientY + tmpDiscountBox.height + 10;
    ypos = tmpevent.clientY + getScrollY();
    if (maxypos > bodyHight) 
    {
        ypos = ypos - (tmpDiscountBox.height + 10);
    }
    
    tmpDiscountBox.style.top = ypos;
    tmpDiscountBox.style.left = tmpevent.clientX + 10;
    tmpDiscountBox.style.display = 'block';
}
//------------------------------------------------------------------------------------
function hideDiscount(sender)
{
    divContainer = sender.parentNode;
    
    for (var index=0; index < divContainer.childNodes.length; index++) { 
        if (divContainer.childNodes[index].id == 'discount')
        {
            tmpDiscountBox = divContainer.childNodes[index];
            break;
        }
    } 
    tmpDiscountBox.style.display = 'none';
}

//------------------------------------------------------------------------------------
// Anvendes kun på produkt liste siden
function previewpicture(tmpevent, sti, formId, checkPicture)
{
    var tmpForm = document.getElementById(formId);
    var tmpPicture = document.getElementById('picture_' + formId);
    var tmpPicbox = document.getElementById('picbox_' + formId);
    
    var tmpImg = new Image()
    
    if ((checkPicture) && (doPictureExists(sti) == false))
    {
        tmpImg.src = imgNoPicture;
        tmpPicture.src = imgNoPicture;
    }
    else
    {
        tmpImg.src = sti;
        tmpPicture.src = sti;
    }
    
    tmpPicture.style.display = 'block';
   	
    var ypos 
    var maxypos 
    var bodyHight = getBodyHeight();
/*
    if (navigator.appName.indexOf('Microsoft')!=-1) {
    bodyHight = document.body.offsetHeight;
    }
    else {
        bodyHight = window.innerHeight;
    }
*/
    maxypos = tmpevent.clientY + tmpImg.height + 10;
    ypos = tmpevent.clientY + getScrollY(); //document.documentElement.scrollTop;
    if (maxypos > bodyHight) {
    ypos = ypos - (tmpImg.height + 10);
    }
    
    tmpPicbox.style.top = ypos + 'px';
    tmpPicbox.style.left = tmpevent.clientX + 10 + 'px';
    tmpPicbox.style.display = 'block';
}

function submitSearch(form, save)
{
    form.submit();
}

function gotoBasket()
{
    location.href = linkBasket;
}

function isCartEmty(cartTotalQuantity, mode)
{
    if((cartTotalQuantity.length > 20) ||(cartTotalQuantity == "0"))
    {
        if (mode == 2)
        {
            if (document.getElementById('emtyBasket1') != null)
            {
                document.getElementById('emtyBasket1').style.display = 'block';
            }
        }
        else
        {
	        var eComContent = "";
	        eComContent += "<table cellpadding=0 cellspacing=0 border=0 style='width: 100%; height: 195px;background-image:url(/files/system/basket_emty.gif);background-repeat:no-repeat;background-position:center center;'>";
	        eComContent += "<tr>";
	        eComContent += "    <td valign='top' style='padding-top:10px;padding-left:5px;'>" + textEmptyBasket + "</td>";
	        eComContent += "</tr>";
	        eComContent += "</table>";
	        if (document.getElementById('btnEmtyBasket') != null)
	        {
	            document.getElementById('btnEmtyBasket').disabled = true;
	        }
	        if (document.getElementById('Content_EcomCart') != null)
            {
                document.getElementById('Content_EcomCart').innerHTML = eComContent;
            }
	     }
    }
}

function doUserNameExists(sender, myaccessuserid)
{
                        	    
    var result = false;
    if (sender.value != '')
    {
        var http_request = initXMLHttpRequestObject();
        if (http_request != null)
        {
            http_request.open('GET', 'Default.aspx?ID=' + JSON_doUsernameExists_PageId + '&accessuserusername=' + sender.value, true);
            http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //Don't cache

            http_request.onreadystatechange = function () 
            {
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
                        the_object = eval('(' + http_request.responseText + ')');
                        if (the_object.result[0].accessuserid != '') 
                        {
                            if (the_object.result[0].accessuserid == myaccessuserid) 
                            {
                                checkUserNameComplete(false, sender);
                            }
                            else
                            {
                                checkUserNameComplete(true, sender);
                            }
                        }
                        else
                        {
                            checkUserNameComplete(false, sender);
                        }
                    } 
                    http_request = null;
                }
            }
            http_request.send(null);
        }
    }
    return result;
}

function doEmailExists(sender, myaccessuserid)
{
                        	    
    var result = false;
    if (sender.value != '')
    {
        var http_request = initXMLHttpRequestObject();
        if (http_request != null)
        {
            http_request.open('GET', 'Default.aspx?ID=' + JSON_doEmailExists_PageId + '&accessuseruseremail=' + sender.value, true);
            http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //Don't cache

            http_request.onreadystatechange = function () 
            {
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
                        the_object = eval('(' + http_request.responseText + ')');
                        if (the_object.result[0].accessuserid != '') 
                        {
                            if (the_object.result[0].accessuserid == myaccessuserid) 
                            {
                                checkEmailComplete(false, sender);
                            }
                            else
                            {
                                checkEmailComplete(true, sender);
                            }
                        }
                        else
                        {
                            checkEmailComplete(false, sender);
                        }
                    } 
                    http_request = null;
                }
            }
            http_request.send(null);
        }
    }
    return result;
}

function FG(groupid, pageLinkId, level)
{
	location.href = 'Default.aspx?ID=' + pageLinkId + '&GroupID=' + groupid;
}

function FP(groupid, pageLinkId, level)
{
	location.href = 'Default.aspx?ID=' + pageLinkId + '&GroupID=' + groupid;
}

function SG(groupid, level)
{
    var divVisible = true;
    if (document.getElementById(level + '_G_' + groupid).style.display != 'block')
	{
		divVisible = false;
	}
    
    hideAllElements('div', level + '_G_');
    var tmpElements = getElementsByIdPart(level + '_I_',null,'img')
    var len = tmpElements.length;
    //var RegEx = /n.gif/g;
    //tmpId = tmpId.replace(RegEx,'_I_');
	for (i = 0; i < len; i++) {
	    //alert(tmpElements[i].src);
	    if (tmpElements[i].src.indexOf("n.gif") < 0)
	        {
			tmpElements[i].src = '/files/system/d.gif';
			}
	}
    
    if (divVisible)
    {
    	document.getElementById(level + '_G_' + groupid).style.display = 'none';
    	document.getElementById(level + '_I_' + groupid).src = '/files/system/d.gif';
	}
	else
	{
		document.getElementById(level + '_G_' + groupid).style.display = 'block';
		document.getElementById(level + '_I_' + groupid).src = '/files/system/u.gif';
	}
}

function ShowGroup() 
	{
	    thisGroupId = javaqueryString('GroupID');
	    if (thisGroupId != 'false')
	    {
	        tmpTableElement = document.getElementById('T_' + thisGroupId + '_T');
	        if (tmpTableElement != null)
	        {
	            tmpTableElement.className = 't4';
	            //tmpParentElement = tmpTableElement.parentElement;
		    tmpParentElement = tmpTableElement.parentNode
	            do
	            {
	                tmpParentElement.style.display = 'block';
	                tmpId = tmpParentElement.id;
	                RegEx = /_G_/g;
	                tmpId = tmpId.replace(RegEx,'_I_');
	                document.getElementById(tmpId).src = '/files/system/u.gif';
        	        
	                tmpParentElement = tmpParentElement.parentNode; //parentElement;
	            }
	            while (tmpParentElement.id != '')
	        }
	    }
	}


function quickOrderSearch(qos)
{
    var result = false;
    if (qos != '')
    {
        var http_request = initXMLHttpRequestObject();
        if (http_request != null)
        {
            http_request.open('GET', 'Default.aspx?ID=' + JSON_ProductIdLookup_PageId + '&qos=' + qos, true);
            http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //Don't cache

            http_request.onreadystatechange = function () 
            {
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
                        var the_object = eval('(' + http_request.responseText + ')');
                        var StockGroupID = "";
                        var StockGroupText = "";
                        if (the_object.result[0].productid != '') 
                        {
			    
                            var Stock = Number(the_object.result[0].productstock);
                            var minStockRate = 99999;
                            
                            //Find stock group
                            var StockStatusList = the_object.result[0].stockstatuslist.split("|");
                            var len = StockStatusList.length - 1;
	                        for (i = 0; i < len; i++) 
	                        {
	                            var StockGroup = StockStatusList[i].split(",");
				    var StockGroupRate = Number(StockGroup[3]);
	                            if (StockGroup[2] == "<=")
	                            {
	                                if ((Stock <= StockGroupRate) && (StockGroupRate < minStockRate))
	                                {
	                                    minStockRate = StockGroupRate;
	                                    StockGroupID = StockGroup[0];        
	                                    StockGroupText = StockGroup[1];
	                                }
	                            }
	                            else if (StockGroup[2] == ">=")
	                            {
	                                if ((Stock >= StockGroupRate) && (StockGroupRate < minStockRate))
	                                {
	                                    minStockRate = StockGroupRate;
	                                    StockGroupID = StockGroup[0];        
	                                    StockGroupText = StockGroup[1];
	                                }
	                            }
	                        }
                            if (StockGroupID != "GREEN")
                            {
                                if (confirm('Lager status for denne vare er: ' + StockGroupText + '\n\nVil du afgive bestilling alligevel?' ))
                                {
                                    quickOrderSubmit(the_object.result[0].productid, the_object.result[0].productname);
                                }
                                else
                                {
					objProductId.title = '';
                                    	objProductId.className = 'input_normal';
					objProductId.value = '';
                                   	objQuantity.title = '';
                                    	objQuantity.className = 'input_normal';
                                    	objQuantity.value = '0';
                                   	objProductId.focus();					
                                    
                                }
                                
                            }
                            else
                            {
                                quickOrderSubmit(the_object.result[0].productid, the_object.result[0].productname);
                            }
                        }
                        else
                        {
                            quickOrderSubmit(null,null);
                        }
                    } 
                    http_request = null;
                }
            }
            http_request.send(null);
        }
    }
    return result;
}

function checkQuantity(formId, quantity, mode, kolli)
{
    tmpForm = document.getElementById(formId);
    obj = tmpForm.Quantity;
    if (mode == 1)
    {
        if (obj.value > quantity)
        {
            tmpForm.submit(); 
        }
        else
        {
            obj.className = 'input_error';
            obj.title = validationQuantityError +  ' ' + quantity;
        }
    }

}

function lineAddSearch(qos)
{
    var result = false;
    if (qos != '')
    {
        var http_request = initXMLHttpRequestObject();
        if (http_request != null)
        {
            http_request.open('GET', 'Default.aspx?ID=' + JSON_ProductIdLookup_PageId + '&qos=' + qos, true);
            http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //Don't cache

            http_request.onreadystatechange = function () 
            {
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
                        
                        the_object = eval('(' + http_request.responseText + ')');
                        if (the_object.result[0].productid != '') 
                        {
                            lineAddSubmit(the_object.result[0].productid);
                        }
                        else
                        {
                            lineAddSubmit(null);
                        }
                    } 
                    http_request = null;
                }
            }
            http_request.send(null);
        }
    }
    return result;
}


function getDeliveryAddresses(targetId, selectValue) 
{
    var http_request = initXMLHttpRequestObject();
    if (http_request != null)
    {
	//alert(JSON_DeliveryLookup_PageId);
        http_request.open('GET', 'Default.aspx?ID=' + JSON_DeliveryLookup_PageId, true);
        http_request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //Don't cache
        http_request.onreadystatechange = function () 
        {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    the_object = eval('(' + http_request.responseText + ')');
                    objDropDown = document.getElementById(targetId);
                    for (var t in the_object.result)
                    {
                        objDropDown.options[t] = new Option(the_object.result[t].text,the_object.result[t].value);
                    }
                } 
                else 
                {
                    return null;
                }
                http_request = null;
            }
        }
        http_request.send(null);
    }
}
	
