/*
Trims whitespace from strings
 */
function trim(s)
{
    var l=0; var r=s.length -1;
    while(l < s.length && s[l] == ' ')
    {	l++; }
    while(r > l && s[r] == ' ')
    {	r-=1;	}
    return s.substring(l, r+1);
}


function changeJQueryThickboxTitle( sTitle ) {
//    document.domain="aftenposten.no";
    var oTitleDiv = document.getElementById("TB_ajaxWindowTitle");
    if( oTitleDiv )
        oTitleDiv.innerHTML = sTitle;            
}


function changeJQueryThickboxBottom( sTitle ) {
//    document.domain="aftenposten.no";
    var oBottomDiv = document.getElementById("TB_ajaxWindowBottom");
    if( oBottomDiv )
        oBottomDiv.innerHTML = sTitle;
}



function redirectTo( sUrl ) {
    window.open(sUrl);
}



function imageExists( sImagePath ) {
    var oRequest = getRequest();
    if(!oRequest)
        return false;

    try {
        oRequest.open("HEAD", sImagePath, false);
       oRequest.send(null);
        return (oRequest.status==200) ? true : false;
    }
    catch(er){
        return false;
    }
}


function getRequest() {
    if(window.XMLHttpRequest)
        return new XMLHttpRequest();
    else if(window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
}


function get_object(o)
{
    if (document.all)
    {
        return document.all[o];
    }
    else if (document.getElementById)
    {
        return document.getElementById(o);
    }
}

function toggle_display(o1, o2)
{
    hideall();
    show(o1);
    return true;
}

function hide(o1)
{
    o2 = get_object("link-" + o1);
    o1 = get_object(o1);
    o2.style.background = "url(images/vmenu_arrow.gif) no-repeat left";
    o2.style.color = "#000000";
    o1.style.display = "none";
}
function show(o1)
{
    o2 = get_object("link-" + o1);
    o1 = get_object(o1);
    o2.style.background = "url(images/vmenu_arrow_on.gif) no-repeat left";
    o2.style.color = "#FF6100";
    o1.style.display = "block";
    o2.blur();
}

/**
 *
 * Changes the html(DOM) elements opacity. Cross browser support.
 * Needed is the html element as an Javascript object and the
 * amount (given between 0 and 100)
 *
 *
 * @param oElement of type Object
 * @param iAmount of type integer
 */
function changeElementOpacity(oElement, iAmount) {

    sAmountCal = (iAmount / 100).toString();

    oElement.style.opacity = sAmountCal;
    oElement.style.MozOpacity = sAmountCal;
    oElement.style.KhtmlOpacity = sAmountCal;
    oElement.style.filter = "alpha(opacity=" + iAmount + ")";
    oElement.style.height = '1%';

//    oElement.style.visibility='hidden';
}

function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }

}
function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function drawRating(data) {
    var avgRating = parseFloat(data.replace(",", "."));
    var tempRating = data.split(",");
    var wholeNum = parseInt(tempRating[0]);
    var decimals = avgRating - wholeNum;

    var sHtml = "leserne mener ";
    for (var i = 1; i <= wholeNum; i++) {
        sHtml += "<img src='../img/oslopuls/userstar_small_on.gif'>";
    }
    if ((0.25 < decimals) && (decimals < 0.75)) {
        sHtml += "<img src='../img/oslopuls/userstar_small_half.gif'>";
        wholeNum = wholeNum + 1;

    }
    for (var y = 1; y <= (6 - wholeNum); y++) {
        sHtml += "<img src='../img/oslopuls/userstar_small_off.gif'>";
    }

    return sHtml;
}

function drawLargeOPRating(data) {
    var wholeNum = parseInt(data);

    var sHtml = "oslopuls mener<br/>";
    for (var i = 1; i <= wholeNum; i++) {
        sHtml += "<img src='../img/oslopuls/opstar_on.gif'>";
    }
    for (var y = 1; y <= (6 - wholeNum); y++) {
        sHtml += "<img src='../img/oslopuls/opstar_off.gif'>";
    }

    var o = document.getElementById("ratingOP");
    if( o )
        o.innerHTML = sHtml;
}



function drawOPRatingForSingleObject(iObjectId, data, sSize ) {

    var thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_medium_on.gif\" alt=\"\">";
    var thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_medium_off.gif\" alt=\"\">"
    if( sSize ) {
        if( sSize === 'SMALL' ) {
            thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_small_on.gif\" alt=\"\">";
            thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_small_off.gif\" alt=\"\">"
        }
        else if( sSize === 'LARGE' ) {
            thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_on.gif\" alt=\"\">";
            thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/opstar_off.gif\" alt=\"\">"
        }
    }

    var wholeNum = parseInt(data);

    var sHtml = "";
    for (var i = 1; i <= wholeNum; i++) {
        sHtml += thisSStarOn;
    }
    for (var y = 1; y <= (6 - wholeNum); y++) {
        sHtml += thisSStarOff;
    }

    var o = document.getElementById(iObjectId + "_ratingOP");
    if( o )
        o.innerHTML = sHtml;
}



function drawLargeOPRatingForSingleObject(iObjectId, data ) {
    drawOPRatingForSingleObject( iObjectId, data, 'LARGE' );
}


function drawMediumOPRatingForSingleObject(iObjectId, data ) {
    drawOPRatingForSingleObject( iObjectId, data, 'MEDIUM' );
}
function replaceCarriageReturn(text,replaceWith){
    text = escape(text);
    //encode all characters in text area to find carriage return character

    for (var i = 0; i < text.length; i++) {
        //loop through string, replacing carriage return encoding with HTML break tag
        if (text.indexOf("%0D%0A") > -1) {
            //Windows encodes returns as \r\n hex
            text = text.replace("%0D%0A", replaceWith)
        }
        else if (text.indexOf("%0A") > -1) {
            //Unix encodes returns as \n hex
            text = text.replace("%0A", replaceWith)
        }
        else if (text.indexOf("%0D") > -1) {
            //Macintosh encodes returns as \r hex
            text = text.replace("%0D", replaceWith)
        }
    }
    text = unescape(text);
    //decode all characters in text area back
    return text;
}