﻿// JScript File

//ÅÄÖ problem fix with modalpopup for save search
var CS_encodeURIComponent = encodeURIComponent;
var CS_decodeURIComponent = decodeURIComponent;
encodeURIComponent = function(s){
    s = escape(s);
    while (s.indexOf('/') >= 0) {
        s = s.replace('/', '%2F');
    }
    while (s.indexOf('+') >= 0) {
        s = s.replace('+', '%2B');
    }
    return s;
}
decodeURIComponent = function(s){
    while (s.indexOf('%2B') >= 0) {
        s = s.replace('%2B', '+');
    }
    while (s.indexOf('%2F') >= 0) {
        s = s.replace('%2F', '/');
    }
   
    return unescape(s);
}    


function SaveSearchMenu(strElement, strImageElement)
{

    var objExpand = document.getElementById(strElement);
    if (objExpand != null)
    {
        if (objExpand.style.display == "block") 
        {
            objExpand.style.visibility = 'hidden';
            objExpand.style.display = 'none';
            
            if (strImageElement != null)
                document.images[strImageElement].src = 'http://www.bygglo.se/Images/Layout/menuTreePlusPay.gif';
            
        }
        else
        {
            objExpand.style.visibility = 'visible';
            objExpand.style.display = 'block';

            if (strImageElement != null)
                document.images[strImageElement].src = 'http://www.bygglo.se/Images/Layout/menuTreeMinusPay.gif';

        }
    }

}


function CloseBanners() 
{
    
    //Left banner
    var objLeftBanner = document.getElementById("ctl00_ctrBannerLeft_panBannerLeft");
    if (objLeftBanner != null)
        objLeftBanner.style.visibility = 'hidden';
        
 
}


function OpenBanners() 
{

    
    //Left banner
    var objLeftBanner = document.getElementById("ctl00_ctrBannerLeft_panBannerLeft");
    if (objLeftBanner != null)
        objLeftBanner.style.visibility = 'visible';
        

}


function LayerVisible(divID) 
{
    //alert(divID);
   
    var objDiv = document.getElementById(divID);
    if (objDiv != null)
    {
        if (objDiv.style.visibility == 'visible')
        {
            //alert('1');
            objDiv.style.display = 'none';
            objDiv.style.visibility = 'hidden';    
        }
        else
        {
            //alert('2');
            objDiv.style.visibility = 'visible';    
            objDiv.style.display = 'inline';
        }
    }
}


function CheckBoxString(strValue, txtId, chkId) 
{
    
    //alert(strValue + ' - ' + txtId + ' - ' + chkId);


    var objTxt = document.getElementById(txtId);
    var objChk = document.getElementById(chkId);

    if (objTxt != null && objChk != null)
    {
        var stringNew = objTxt.value;
        var catArray = stringNew.split(",");

        //alert(catArray[44] + ' ' + catArray.length);
        //alert(stringNew + ' - ' + strValue + ' - ' + catArray[0]);
        
        
        if (stringNew.match(',' + strValue + ','))
        {
            //delete
            stringNew = stringNew.replace(strValue + ',', "");   
        }
        else if (catArray[0] == strValue)
        {
            //Delete first category
            stringNew = stringNew.replace(strValue + ',', "");  
        }
        else
        {
            //Insert
            stringNew = objTxt.value + strValue + ',';

            if (catArray.length > 11)
                alert("Du kan max välja 10 kategorier");
 
         }   
       
        

        objTxt.value = stringNew;
    
    }
        

}


function popUp(Link, Name, Width, Height) {
	newwindow = window.open(Link,Name,"toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + Width + ",height=" + Height);

	if (window.focus) {newwindow.focus()}

}



function popUpTarget(theURL, Name, popW, popH, scroll) 
{ // V 1.0
    var winleft = (screen.width - popW) / 2;
    var winUp = (screen.height - popH) / 2;
    winProp =
    'width='+popW+',height='+popH+',left='+winleft+',t op='+winUp+',toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,'
    Win = window.open(theURL, Name, winProp)
    if (parseInt(navigator.appVersion) >= 4){
        Win.window.focus();
    }
}


















 // Get an Array.
 function GetFastAdsFunc(Search) 
 {	  
    AfterPageLoad.WebService.GetFastAds(Search, SucceededCallbackArray, FailedCallback);
 }


// Callback function invoked when the call to 
// the Web service methods succeeds. 
function SucceededCallbackArray(result, eventArgs)
{ 

    var RsltElemPrem = document.getElementById("FastAdsPremiumResult");
    var RsltElemStand = document.getElementById("FastAdsStandardResult");

    
    var i = 0;
    for(var item in result)
    {    
        if (result[item] != '' && i == 0 && RsltElemPrem != null)
            RsltElemPrem.innerHTML = result[item];
        
        else if (result[item] != '' && i == 1 && RsltElemStand != null)
            RsltElemStand.innerHTML = result[item];
            
       i++;
    }

    
    //break;
    
}



function GetSearchCloud(Rows, Search, TypeId)
{
    AfterPageLoad.WebService.SearchCloud(Rows, Search, TypeId, SucceededCallbackCloud, FailedCallback);
}

function SucceededCallbackCloud(result, eventArgs)
{
    // Page element to display feedback.
    var RsltElem = document.getElementById("CloudResult");
    
    if (RsltElem != null)
        RsltElem.innerHTML = result;
}





//Get result map
function GetResultMap(strIds)
{
    AfterPageLoad.WebService.ResultMap(strIds, SucceededCallbackResultMap, FailedCallback);
}

function SucceededCallbackResultMap(result, eventArgs)
{
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultMap");
    
    if (RsltElem != null)
        RsltElem.innerHTML = result;
}







// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(errors, userContext, methodName) 
{
    //alert(errors.get_Message());
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();



function addEngine() {

    //<a href="#" onclick="addEngine()">Lägg till Bygglo i din webbläsare</a>

    if (window.external && ("AddSearchProvider" in window.external)) {
        // Firefox 2 and IE 7, OpenSearch
        window.external.AddSearchProvider("http://www.bygglo.se/SearchPluginWeb.xml");
    } else if (window.sidebar && ("addSearchEngine" in window.sidebar)) {
        // Firefox <= 1.5    
        window.sidebar.addSearchEngine(
            "http://www.bygglo.se/SearchPluginWeb.src",  /* engine URL */
            "http://www.bygglo.se/SearchPluginWeb.png",  /* icon URL */
            "Bygglo",                                             /* engine name */
            "Web"); 
    } else {
        // No search engine support (IE 6, Opera, etc).
        alert("Din läsare stödjer inte denna installations funktion!");
    }
}




function openModalDialogueSave(id) {
    openModalDialogue(id, 400, 400);
}

function openModalDialogueContact(id) {
    openModalDialogue(id, 440, 380);
}

function openModalDialogueResMenu(id) {
    openModalDialogue(id, 400, 140);
}

function openModalDialogue(id, width, height) {

    $(id).dialog("destroy");

    $(id).dialog({
        minHeight: height,
        minWidth: width,
        show: "fade",
        hide: "drop",
        modal: true

    });
}



$(document).ready(function () {

    //attach autocomplete
    $("#ctl00_ContentPlaceHolder_ctrSearchBox_txtSearch, #ctl00_ctrTopMenu_txtSearch, #ctl00_ContentPlaceHolder_txtSearch").autocomplete({

        //define callback to format results
        source: function (req, add) {

            //pass request to server
            //$.getJSON("http://localhost:51494/SB2Web/WebService/Autocomplete.ashx?q=", req, function (data) {
            $.getJSON("http://www.bygglo.se/WebService/Autocomplete.ashx?q=", req, function (data) {
                


                //create array for response objects
                var suggestions = [];

                //process response
                $.each(data, function (i, val) {
                    if (val != '' && val != null)
                        suggestions.push(val);
                });

                //pass array to callback
                add(suggestions);

            });
        },
        open: function () {
            $("#ctl00_ContentPlaceHolder_ctrPlusTicker_panTicker").hide();
        },
        close: function () {
            $("#ctl00_ContentPlaceHolder_ctrPlusTicker_panTicker").show();
        }


    });
});



