$(function() {
    //ajax setup
    $.ajaxSetup({ cache: false, dataType: "html" });

    //when ESC key is pressed close popup
    $(document).keypress(function(e) { if (e.keyCode == 27 && popupStatus == 1) { closePopup(); } });

    //close event on popup background
    $('#modalwindowbackground').click(function() { closePopup(); });

    //teaser carousel
    $('#teaser').jcarousel({
        auto: 4, //seconds before scroll
        vertical: false,
        scroll: 3, //number of items to scroll
        initCallback: carousel_initCallback,
        wrap: 'both'
    });

    //lastviewed carousel
    $('#lastviewed').jcarousel({
        auto: 4, //seconds before scroll
        vertical: true,
        scroll: 2, //number of items to scroll
        initCallback: carousel_initCallback,
        wrap: 'both'
    });
});

//carousel init callback
function carousel_initCallback(carousel) {
    //disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    //pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

//ads.aspx last selected
var ajaxLastSelectedPropertyTypeId = "#";
var startpos = 0;


/////////////////  LOGIN - default.aspx

//if user press enter in loginform call ajax
function onLogin(e)  {
    if (navigator.appName == "Netscape") 
        var key_index = e.which;
    else 
        var key_index = window.event.keyCode;

    if (key_index == 13) {
        loginUser();
    }
}

//login
function loginUser() {
    $.ajax({
        url: 'http://www.simply.si/ajaxlogin.aspx',
        data: ({ email: $('#email').val(), password: $('#password').val() }),
        success: function(data) {
            //error / submit
            if (data != "") {
                showCenteredError(data, "errorLogin");
            }
            else {
                document.loginform.submit();
            }
        }
    });
}

//show info div - centered
function showCenteredInfo(info, id) {
    //lastInfoDiv = document.getElementById(id);

    clearTimeout($('#time' + id).val());
    $('#time' + id).val(setTimeout("closePopup()", 7000));

    //load error div
    $('#modalwindow').html($('#' + id).html());

    //load error message
    $("#text" + id).html(info);

    //open popup
    openPopup();
}


//show error div - centered
function showCenteredError(error, id) {
    //lastInfoDiv = document.getElementById(id);
    clearTimeout($('#time' + id).val());
    $('#time' + id).val(setTimeout("closePopup()", 7000));
    
    //load error div
    $('#modalwindow').html($('#' + id).html());

    //load error message
    $("#text" + id).html('<span class="arialwhite12">' + error + '</span>');

    //open popup
    openPopup();
}

//show info popup
function showInfoPopup(contentid) {
    //send ajax
    $.ajax({
        url: 'http://www.simply.si/ajaxinfopopup.aspx',
        data: ({ contentid: contentid, validate: 0 }),
        success: function(data) {
            //if info popup is ok to show
            if (data == 'show') {
                //load content for info popup
                $('#infopopup').html($('#infopopupcontainer').html());
                $('#modalwindow').html($('#infopopupcontent').html());

                //open popup
                setTimeout(function() {
                    openPopup();
                }, 250);
            }
        }
    });
}

//keypress: submit info popup
function onInfoPopup(e, mailingid) {
    if (navigator.appName == "Netscape")
        var key_index = e.which;
    else
        var key_index = window.event.keyCode;

    if (key_index == 13) {
        submitInfoPopup(mailingid);
    }
}

//submit info popup
function submitInfoPopup(mailingid) {
    //get email
    var email = $('#infopoupmailinginput').val();
    
    //send ajax
    $.ajax({
        url: 'http://www.simply.si/ajaxinfopopup.aspx',
        data: ({ email: email, mailingid: mailingid, validate: 1 }),
        success: function(data) {
            //if submit was ok
            if (data == '') {
                $('#infopopuperrortext').slideUp(200);
                $('#infopopupformholder').slideUp(200);
                $('#infopopupinfotext').slideDown(200);
            }
            else {
                $('#infopopuperrortext').html(data);
            }
        }
    });
}

//open send contact
function openSendContact(articleid) {
    //load send to friend 
    $('#modalwindow').html($('#sendadcontact').html());
    //load loader
    $('#sendadcontactdiv').html($("#processdiv").html());
    //open popup
    openPopup();
    //set content by ajax request
    $.ajax({
        url: 'http://www.simply.si/ajaxsendadcontact.aspx',
        data: ({ articleid: articleid }),
        success: function(data) {
            $('#sendadcontactdiv').html(data);
        }
    });
}

//open content image modal dialog
function openContentImageModalDialog(docid, documentimagepos) {
    //load article image
    $('#modalwindow').html($('#ajaxlargeimage').html());
    //load loader
    $('#ajaxlargeimagediv').html($("#processdiv").html());
    //open popup
    openPopup();
    //set content by ajax request
    $.ajax({
        url: 'http://www.simply.si/ajaxlargeimage.aspx',
        data: ({ docid: docid, documentimagepos: documentimagepos }),
        success: function(data) {
            $('#ajaxlargeimagediv').html(data);
        }
    });
}

//open article images modal dialog
function openArticleImageModalDialog(articleid, articleimagepos) {
    //load article image
    $('#modalwindow').html($('#ajaxlargeimage').html());
    //load loader
    $('#ajaxlargeimagediv').html($("#processdiv").html());
    //open popup
    openPopup();
    //set content by ajax request
    $.ajax({
        url: 'http://www.simply.si/ajaxlargeimage.aspx',
        data: ({ articleid: articleid, articleimagepos: articleimagepos }),
        success: function(data) {
            $('#ajaxlargeimagediv').html(data);
        }
    });
}

//open send to friend modal dialog
function openSendToFriendModalDialog(articleid) {
    //load send to friend 
    $('#modalwindow').html($('#sendadtofriend').html());
    //load loader
    $("#sendadtofrienddiv").html($("#processdiv").html());
    //open popup
    openPopup();
    //send ajax request
    $.ajax({
        url: 'http://www.simply.si/ajaxsendadtofriend.aspx',
        data: ({ articleid: articleid }),
        success: function(data) {
            //ajax response
            $('#sendadtofrienddiv').html(data);
        }
    });
}

//open najdi.si map
function openNajdiMap() {
    //load send to friend
    $('#modalwindow').css({ width: 800, height: 700 }).html($('#mapbiginfo').html());
    //open popup
    openPopup();
}

//open forgot password modal dialog
function openForgotPasswordModalDialog() {

    //load send to friend
    $('#modalwindow').html($('#sendpassword').html());
    
    //open popup
    openPopup();
}

/////////////////  MENU - default.aspx

//show menu dropdown
function showMenu(position, value) {
    if(position==1)
        document.getElementById("level1" + value).style.backgroundImage = "url('http://www.simply.si/images/header_menu_over1.gif')";
    else
        document.getElementById("level1" + value).style.backgroundImage = "url('http://www.simply.si/images/header_menu_over.gif')";

    document.getElementById("level1" + value).style.cursor = 'pointer';
    document.getElementById("font" + value).className = "trebuchetorange16";

    if (document.getElementById("level2" + value))
        document.getElementById("level2" + value).style.display = "block";
}

//hide menu dropdown
function hideMenu(value) {
    document.getElementById("level1" + value).style.backgroundImage = "";
    document.getElementById("font" + value).className = "trebuchetwhite16";

    if (document.getElementById("level2" + value))
        document.getElementById("level2" + value).style.display = "none";
}


/////////////////  SENDPASSWORD - default.aspx

//if user press enter in sendform call ajax
function onSendPassword(e)
{
    if (navigator.appName == "Netscape")
        var key_index = e.which;
    else
        var key_index = window.event.keyCode;

    if (key_index == 13) sendPassword();

}

//login
function sendPassword() {
    $.ajax({
        url: 'http://www.simply.si/ajaxsendpassword.aspx',
        data: ({ email: $('#emailsendpassword').val() }),
        success: function(data) {
            //info / error
            if (data.indexOf("#") == -1) {
                $('#sendPasswordInfo').hide();
                showCenteredError(data, "errorSendPassword");
            }
            else {
                $('#sendPasswordInfo').show();
                $('#sendpasswordbutton').hide();
                setTimeout("closePopup();", 5000); 
            }
        }
    })
}

///////////////// - ads.aspx leftbar

//currency format 
function formatCurrency(name, dec, showComma) {
    //init variables
    inp = name.value;
    comma = 0;
    sign = ''

    //get sign
    if (inp.substring(0, 1) == '-') {
        sign = '-';
        inp = inp.substring(1, inp.length);
    };

    //validate input value
    if (inp == '')
        return;
    outt = '';

    //loop characters
    for (var i = 0; i < inp.length; i++) {
        //get character
        sChar = inp.charAt(i);
        if (sChar == '.')
            continue;
        else if (sChar == ',') {
            if (dec == 0)
                break;
            if (comma > 0)
                break;
            comma = 1;
            outt = outt + sChar;
        }
        else if (sChar < '0' || sChar > '9')
            break;
        else
            outt = outt + sChar;
    }

    //get value with zeros
    out = '';
    if (dec) {
        zeros = '000000000';
        out = ',';
        pos1 = outt.indexOf(',');
        if (pos1 != -1) {
            pos2 = outt.length;
            if (pos2 - pos1 - 1 > dec)
                pos2 = pos1 + dec + 1;
            out = out + outt.substring(pos1 + 1, pos2);
            outt = outt.substring(0, pos1);
        }
        out = out + zeros.substring(0, 3 - out.length);
    }
    //format value with separators
    if (showComma == 'true') 
    {
        for (i = outt.length; i > 3; i -= 3)
            out = "." + outt.substring(i - 3, i) + out;
    }

    //return formated value
    out = outt.substring(0, i) + out;
    name.value = sign + out;

}


/////////////////  TREE selection - myaccountad.aspx
function ajaxMyAccountAd(nodeid, articleid) {
    document.getElementById('myaccountaddiv').innerHTML = document.getElementById('processdiv').innerHTML;
    sendAjaxRequest("http://www.simply.si/ajaxmyaccountad.aspx?nodeid=" + nodeid + "&articleid=" + articleid, "myaccountaddiv", "handleMyAccountAjax");
}


/////////////////  select radio input
function setRadioValue(rooturl, name, value) {
    if (document.getElementById(name + document.getElementById(name).value + "div"))
        document.getElementById(name + document.getElementById(name).value + "div").style.backgroundImage = "url('http://www.simply.si/images/input_radio.png')";

    if (document.getElementById(name + value + "div")) {
        document.getElementById(name + value + "div").style.backgroundImage = "url('http://www.simply.si/images/input_radio_selected.png')";
        document.getElementById(name).value = value;
    }
}

function keyOnlyNumeric(e) {
    if (navigator.appName == 'Netscape')
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 13)
        return true;
    else
        return false;
}

function keyOnlyNumericWDotWComma(e) {
    if (navigator.appName == 'Netscape')
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 44 || keyIndex == 46 || keyIndex == 13)
        return true;
    else
        return false;
}

function keyOnlyNumericWDot(e) {
    if (navigator.appName == 'Netscape')
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 46 || keyIndex == 13)
        return true;
    else
        return false;
}


//send ajax reminder
function sendAjaxReminder(articleid) {
    $.ajax({
        url: 'http://www.simply.si/ajaxadreminder.aspx',
        data: ({ articleid : articleid }),
        success: function(data) {
            $('#ajaxadreminderdiv' + articleid).html(data);
        }
    });
}


//set checkbox value
function setCheckboxValue(rooturl, name, value) {
    var array = document.getElementById(name).value.split(',');
    //deselect / select
    if (document.getElementById(name + value + "div").style.backgroundImage.indexOf("selected") > 0) {
        document.getElementById(name + value + "div").style.backgroundImage = "url('http://www.simply.si/images/include_checkbox.gif')";
        document.getElementById(name).value = document.getElementById(name).value.replace("," + value, "").replace(value + ",", "").replace(value, "");
        //document.getElementById(name).value = value;
    }
    else {
        if (array[0] == '')
            document.getElementById(name).value = value;
        else {
            array[array.length] = value;
            document.getElementById(name).value = array;
        }
        document.getElementById(name + value + "div").style.backgroundImage = "url('http://www.simply.si/images/include_checkbox_selected.png')";
    }
}

//show myaccountsettingsbusiness div
function showMyaccountDiv(id) {
    var objStyle = document.getElementById(id).style.display;

    if (objStyle == 'block') {
        document.getElementById(id).style.display = 'none';
    }
    else {
        document.getElementById(id).style.display = 'block';
    }
}

//bookmark link function
function bookmark_click(url) {
    u = location.href; //get current browser url
    t = document.title; //get current browser title
    window.open(url + encodeURIComponent(u) + '&amp;t=' + encodeURIComponent(t), 'sharer');
    return false; //halts href link
}

//show/hide advance search
function toggleAdvanceSearch() {
    //get object
    var obj = document.getElementById('advancesearch');
    
    //show else hide
    if (obj.style.display == '' || obj.style.display == 'none' || obj.style.display == null) {
        obj.style.display = 'block';
       // document.getElementById('blankbox').style.display = 'block';
        //set to accept advance search filter
        document.getElementById('advancesearchtoggle').value = 1;
        
        //prepare price inputs
        document.getElementById('pricewtaxfrom1').style.color = '#9a999a';
        document.getElementById('pricewtaxfrom1').style.fontStyle = 'italic';
        document.getElementById('pricewtaxto1').style.color = '#9a999a';
        document.getElementById('pricewtaxto1').style.fontStyle = 'italic';
    }
    else {
        obj.style.display = 'none';
       // document.getElementById('blankbox').style.display = 'none';
       
        //reset checkbox and radio
        if (document.getElementById('valuenewads1') && document.getElementById('valuenewads1').value > 0) { setCheckboxValue(rooturl, 'valuenewads1', 1); }
        if (document.getElementById('valueadditionalimage1') && document.getElementById('valueadditionalimage1').value > 0) { setCheckboxValue(rooturl, 'valueadditionalimage1', 1); }
        if (document.getElementById('valueadditionalpartnertype1') && document.getElementById('valueadditionalpartnertype1').value > 0) { setRadioValue(rooturl, 'valueadditionalpartnertype1', 0); }

        //reset price range
        document.getElementById('pricewtaxfrom1').value = '';
        document.getElementById('pricewtaxto1').value = '';

        //clear default text from price inputs
        resetPricewtaxFrom1();
        resetPricewtaxTo1();

        //set to ignore advance search
        document.getElementById('advancesearchtoggle').value = 0;
    }
}

//key: only numeric with enter with dot and form submit
function keyAdvancedSearchOnlyNumericWEnterWDotWFormSubmit(e) {
    if (navigator.appName == "Netscape")
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 44 || keyIndex == 46 || keyIndex == 13) {
        if (keyIndex == 13) {
            resetPricewtaxFrom1();
            resetPricewtaxTo1();
            document.getElementById('searchform1').submit();
        }
    }
    else
        return false;
}

//-- scroll --
//variables
var bannerscrolltime = 10;
var bannerspeedupstart = 600;
var bannerspeedupstep = 250;

//onload scroll right
function onloadBannerScroll(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    if (document.getElementById(id + 'scrollstep').value > 0) {
        document.getElementById(id).scrollLeft -= document.getElementById(id + 'scrollstep').value;
        document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) - 1;
        document.getElementById(id + 'bannertimer').value = setTimeout("onloadBannerScroll('" + id + "')", bannerscrolltime);
    }
    else
        document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

//scroll banner left
function scrollBannerLeft(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id).scrollLeft -= parseInt(document.getElementById(id + 'scrollstep').value);
    document.getElementById(id + 'bannertimer').value = setTimeout("scrollBannerLeft('" + id + "')", bannerscrolltime);

    //speed up
    if (document.getElementById(id + 'speeduptimer').value == 0)
        document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerLeft('" + id + "')", bannerspeedupstart);
}

//speed up banner left
function speedUpBannerLeft(id) {
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) + 1;
    document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerLeft('" + id + "')", bannerspeedupstep);
}

//stop left scrolling
function stopBannerLeft(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id + 'bannertimer').value = 0;
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'speeduptimer').value = 0;
    document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

//scroll banner right
function scrollBannerRight(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id).scrollLeft += parseInt(document.getElementById(id + 'scrollstep').value);
    document.getElementById(id + 'bannertimer').value = setTimeout("scrollBannerRight('" + id + "')", bannerscrolltime);

    //speed up
    if (document.getElementById(id + 'speeduptimer').value == 0)
        document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerRight('" + id + "')", bannerspeedupstart);
}

//speed up banner right
function speedUpBannerRight(id) {
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) + 1;
    document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerRight('" + id + "')", bannerspeedupstep);
}

//stop right scrolling
function stopBannerRight(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id + 'bannertimer').value = 0;
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'speeduptimer').value = 0;
    document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

//variables
var bannerscrolltime1 = 10;
var bannerspeedupstart1 = 600;
var bannerspeedupstep1 = 250;

//onload scroll right
function onloadBannerScroll1(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    if (document.getElementById(id + 'scrollstep').value > 0) {
        document.getElementById(id).scrollLeft -= document.getElementById(id + 'scrollstep').value;
        document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) - 1;
        document.getElementById(id + 'bannertimer').value = setTimeout("onloadBannerScroll('" + id + "')", bannerscrolltime1);
    }
    else
        document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

//scroll banner left
function scrollBannerLeft1(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id).scrollLeft -= parseInt(document.getElementById(id + 'scrollstep').value);
    document.getElementById(id + 'bannertimer').value = setTimeout("scrollBannerLeft('" + id + "')", bannerscrolltime1);

    //speed up
    if (document.getElementById(id + 'speeduptimer').value == 0)
        document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerLeft('" + id + "')", bannerspeedupstart1);
}

//speed up banner left
function speedUpBannerLeft1(id) {
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) + 1;
    document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerLeft('" + id + "')", bannerspeedupstep1);
}

//stop left scrolling
function stopBannerLeft1(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id + 'bannertimer').value = 0;
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'speeduptimer').value = 0;
    document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

//scroll banner right
function scrollBannerRight1(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id).scrollLeft += parseInt(document.getElementById(id + 'scrollstep').value);
    document.getElementById(id + 'bannertimer').value = setTimeout("scrollBannerRight('" + id + "')", bannerscrolltime1);

    //speed up
    if (document.getElementById(id + 'speeduptimer').value == 0)
        document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerRight('" + id + "')", bannerspeedupstart1);
}

//speed up banner right
function speedUpBannerRight1(id) {
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'scrollstep').value = parseInt(document.getElementById(id + 'scrollstep').value) + 1;
    document.getElementById(id + 'speeduptimer').value = setTimeout("speedUpBannerRight('" + id + "')", bannerspeedupstep1);
}

//stop right scrolling
function stopBannerRight1(id) {
    clearTimeout(document.getElementById(id + 'bannertimer').value);
    document.getElementById(id + 'bannertimer').value = 0;
    clearTimeout(document.getElementById(id + 'speeduptimer').value);
    document.getElementById(id + 'speeduptimer').value = 0;
    document.getElementById(id + 'scrollstep').value = document.getElementById(id + 'scrollstepdefault').value;
}

function redirectSearchsid(searchsid) {
    var loc = window.location.href.toLowerCase();
    if (loc.indexOf('searchsid=') != -1) {
        var hash = location.hash;
        if (hash) {
            alert('test'); 
            hash = hash.replace('#', '');
            hash = hash + '?searchsid=' + searchsid;
            location.href = hash;
        } 
    }
}

function loadAllImage(pos,maxpos) {
    document.getElementById('allimage' + startpos).style.display = 'none';
    document.getElementById('alltitle' + startpos).style.display = 'none';
    startpos = startpos + pos;
    document.getElementById('allimage' + startpos).style.display = 'block';
    document.getElementById('alltitle' + startpos).style.display = 'block';
    
    if (startpos == 1) {
        document.getElementById('allarticleleft').style.display = 'none';
    } else {
    document.getElementById('allarticleleft').style.display = 'block';
    }
    if (startpos == maxpos) {
        document.getElementById('allarticleright').style.display = 'none';
    } else {
    document.getElementById('allarticleright').style.display = 'block';
    }

}

//silverlight error handler
function onSilverlightError(sender, args) {

    //application source
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    //error message
    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    //error details
    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    //if error type is parser error
    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") { //runtime error
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}

//twit set page
function setPageTwitListPage(url, page,sorting) {
    window.location = url + "&page=" + page + "&sorting=" + sorting;
}

//upload profile image
function imageupload(id, divid, inputid, validate) {
    sendAjaxRequest("ajaxmyaccountpersonaladimage.aspx?fileid=" + document.getElementById(inputid).value + "&typeid=" + id + "&validate=" + validate, divid, "");
}

//remove upload profile image
function removeimageupload(fileid, id, divid) {
    sendAjaxRequest("ajaxmyaccountpersonaladimage.aspx?removefileid=" + fileid + "&typeid=" + id + "&validate=1", divid, "");
}

//upload selected image
function uploadselectedimage(fileid, typeid) {
//    if(document.changeavatar.profileimage.value!="" || document.changeavatar.personalimage.value!="")
    //    {
    if (typeid == 0) {
        document.changeavatarform.action = "ajaxmyaccountpersonaladimage.aspx?typeid=0&validate=1";
        document.changeavatarform.submit();
    }
    else {
        document.changelogoform.action = "ajaxmyaccountpersonaladimage.aspx?typeid=1&validate=1";
        document.changelogoform.submit();
    }
//    }
}

//upload register profile image
function ajaxmyaccountimageuploadform(validate, fileid) {
    sendAjaxRequest("ajaxmyaccaountregisterimageupload.aspx?fileid=" + fileid + "&validate=" + validate, "personalimagediv", "");
}

function stopAjaxMyaccountImageUpload(fileid, wrongfileformat) {
    if (wrongfileformat == 1)
        document.getElementById("personaluploaddocumenterror").style.display = "block";
    else {
        sendAjaxRequest("ajaxmyaccaountregisterimageupload.aspx?fileid=" + fileid, "personalimagediv", "");
        document.getElementById('personalimagefileid').value = fileid;
    }
}


function removeAjaxMyaccountImageUpload(fileid) {
    sendAjaxRequest("ajaxmyaccaountregisterimageupload.aspx?removefileid=" + fileid, "personalimagediv", "handleRemoveAjaxMyaccountImageUpload");
}

function handleRemoveAjaxMyaccountImageUpload(responseText) {
    document.getElementById('personalimagefileid').value = 0;
}


function stopPersonalUpload(wrongfileformat, personalfileid, type) {
    //error personal image
    if (wrongfileformat == 1)
        document.getElementById("personaluploaddocumenterror0").style.display = "block";
        
    //upload personal image
    if (wrongfileformat == 0 && type == 0)
    {
        document.getElementById("personalimagefileid").value = personalfileid;
        sendAjaxRequest("ajaxmyaccountpersonaladimage.aspx?fileid=" + document.getElementById('personalimagefileid').value + "&typeid=0", "personalimagediv", "");
    }

    //error company banner image
    if (wrongfileformat == 2)
        document.getElementById("personaluploaddocumenterror1").style.display = "block";

    //upload company banner image
    if (wrongfileformat == 0 && type == 1) 
    {
        document.getElementById("companybannerimage").value = personalfileid;
        sendAjaxRequest("ajaxmyaccountpersonaladimage.aspx?fileid=" + document.getElementById('companybannerimage').value + "&typeid=1", "companybannerimagediv", "");
    }
}

function ajaxalluserads(articleid, businesspartnerid, searchsid, page) {
    document.getElementById('alluserloader').style.display = 'block';
    document.getElementById('alluserads').style.display = 'none';
    if (page == 0) {
        sendAjaxRequest("http://www.simply.si/ajaxbusinesspartnerads.aspx?articleid=" + articleid + "&businesspartnerid=" + businesspartnerid + "&searchsid=" + searchsid, "alluserads", "alluseradshandler")
    } else {
        sendAjaxRequest("http://www.simply.si/ajaxbusinesspartnerads.aspx?articleid=" + articleid + "&businesspartnerid=" + businesspartnerid + "&searchsid=" + searchsid + "&page=" + page, "alluserads", "alluseradshandler")
    }
}

function alluseradshandler(responseText) {
    if (responseText != "") {
        document.getElementById('alluserloader').style.display = 'none';
        document.getElementById('alluserads').style.display = 'block';
    }
}

function onAjaxSendAdToFriend(e) {
    var keyIndex = 0;
    if (navigator.appName == "Netscape")
        keyIndex = e.which;
    else
        keyIndex = window.event.keyCode;

    if (keyIndex == 13)
        sendAjaxSendAdToFriend();
}

function onAjaxSendContact(e) {
    var keyIndex = 0;
    if (navigator.appName == "Netscape")
        keyIndex = e.which;
    else
        keyIndex = window.event.keyCode;

    if (keyIndex == 13)
        sendAjaxSendContact();
}

//opens send ad contact modal window
function sendAdContactModal(articleid) {
	//load send to friend
	$('#modalwindow').html($('#sendadcontact').html());
	//load loader
	$("#sendadcontactdiv").html($("#processdiv").html());
	//open popup
	openPopup();
	//send ajax request
	$.ajax({
	    url: 'http://www.simply.si/ajaxsendadcontact.aspx',
	    data: ({ articleid: articleid }),
	    success: function(data) {
	        //ajax response
	        $('#sendadcontactdiv').html(data);
	    }
	});
}

//POPUP ###############
//0 = disabled; 1 = enabled;  
var popupStatus = 0;

//show popup
function openPopup() {
    if (popupStatus == 0) {

        //set that popup is open
        popupStatus = 1;

        //check for IE - show modal window
        if ($.browser.msie) {
            $('#modalwindowbackground').css({ 'opacity': '0.0' });
            $('#modalwindowbackground').css('display', 'block');
            $('#modalwindow').css('display', 'block');
        }
        else {
            $('#modalwindowbackground').css({ 'opacity': '0.0' });
            $('#modalwindowbackground').fadeIn('fast');
            $('#modalwindow').fadeIn('fast');
        }

        //popup - center
        centerPopup();
    }
}

//position popup in center of window
function centerPopup() {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();

    var popupWidth = $("#modalwindow").width();
    var popupHeight = $("#modalwindow").height();

    var newWidth = windowHeight / 2 - popupHeight / 2;
    var newHeight = windowWidth / 2 - popupWidth / 2;

    //centering
    $('#modalwindow').css({ top: newWidth + 100, left: newHeight });
}

//position popup in center of window
function centerAdSelectionPopup() {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();

    var popupWidth = $("#adseldiv").width();
    var popupHeight = $("#adseldiv").height();

    var newWidth = windowHeight / 2 - popupHeight / 2;
    var newHeight = windowWidth / 2 - popupWidth / 2;
    //centering
    $('#adseldiv').css({ top: newWidth + 100, left: newHeight });
}


//dispose popup
function disposePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        //mozilla
        if ($.browser == "mozilla" && $.browser.version.substr(0, 3) == "1.9") {
            $('#modalwindowbackground').fadeOut('fast');
            $('#modalwindow').fadeOut('fast');
            $('#adseldiv').fadeOut('fast');
        } else {
            $('#modalwindowbackground').css('display', 'none');
            $('#modalwindow').css('display', 'none');
            $('#adseldiv').css('display', 'none');
        }

        //set that popup is closed
        popupStatus = 0;
    }
}

//close popup window
function closePopup() {
    //popup - dispose
    disposePopup();
}

//ajax survey
function ajaxSurvey(docid, vote) {
    //show process img
    $('#ajaxsurvey').html($("#processdiv").html());
    //show survey
    $.ajax({
        url: 'http://www.simply.si/ajaxsurvey.aspx',
        data: ({ docid: docid, vote: vote }),
        success: function(data) {
                $('#ajaxsurvey').html(data); 
            }
    });
}

//show info div - centered
function showRegSuccessPopup(info, id) {
    //lastInfoDiv = document.getElementById(id);
    clearTimeout($('#time' + id).val());
    $('#time' + id).val(setTimeout("closePopup()", 12000));

    //load error div
    $('#modalwindow').html($('#' + id).html());

    //load error message
    $("#text" + id).html(info);

    //open popup
    openPopup();
    //setTimeout("location.href = 'http://www.simply.si/';", 10000);
}

//key: only numeric with + and space (for phone number)
function keyOnlyNumericWEnter(e) {

    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    //only allow numbers (48-57), tab (0), backspace (8), enter (13), space (32), + (43), - (45) //(.(46),(47))
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 13 || keyIndex == 32 || keyIndex == 45)
        return true;
    else
        return false;
}



