﻿/*
    common functions 
    Richard Appelgren, Zooma By Semcon 2008
*/

var common = new Object();
// addded, returns a url to the MiscService
common.miscServiceUrl = '/Templates/KappAhlWeb/WebServices/MiscServices.asmx';
common.fileDownloaderUrl = '/Templates/KappAhlWeb/Utils/FileDownload.aspx';


// addded, returns a ref to the MiscService
common.getMiscServiceRef = function() {
    return EPiServer.Templates.KappAhlWeb.WebServices.MiscServices;
}
// added, returs ref to tempaltes;
common.jqueryTemplatesBaseUrl = '/Templates/KappAhlWeb/_includes/jquery/Templates/';
common.ajaxTimout = 40000;

// create a cookie, only needs to supply name and value
common.setCookie = function(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
					((expires) ? "; expires=" + expires.toGMTString() : "") +
					((path) ? "; path=" + path : "; path=/") +
					((domain) ? "; domain=" + domain : "") +
					((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

// get a cookie-value by its name, returns null if the name isnt found
common.getCookieValue = function(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;

    return unescape(dc.substring(begin + prefix.length, end));
}
common.removeCookie = function(name) {
    common.setCookie(name, '', -1);
}

common.isValue = function(v) {
    var bIsNotValue = (v == null || v == 'null' || (v == "") || (v == undefined || v == 'undefined'));
    return !bIsNotValue;
}

common.getQueryStringValue = function(skey) {
    var val = "", qs, arr = [], nr, key = skey + "=";
    if (window.location.href.indexOf("?") > -1) {
        qs = window.location.href.substring(window.location.href.indexOf("?") + 1);
        arr = qs.split("&");
        for (var i = 0; i < arr.length; i++) {
            nr = arr[i].lastIndexOf(key);
            if (nr > -1) {
                val = arr[i].substring(nr + key.length);
                break;
            }
        }
    }
    return val;
}
common.downloadFile = function(strUrl) {
    window.open(common.fileDownloaderUrl + '?url=' + strUrl, 'DownloadFile', 'width=250,height=200');
    return false;
}

common.utils = new Object();
common.utils.regExp_dblSlash = /(http(s){0,1}:)?\/\//g;
common.utils.reqExp_absUrl = /http(s)?:\/\//g;

// this is bec javascript impl of ReqExp does not have negative lookbehind
common.utils.replaceNegLookBehind = function(strValue, regExp, strReplace) {
    var output = strValue.replace(regExp, function($0, $1) {
        return $1 ? $0 : strReplace;
    });
    return output;
}


$(document).ready(function() {
    
});

