﻿/*
    Name:       Default.js
    Purpose:    Default javascripts
    Created:    2008-02-05; Internetfabriken
    Modified:   2008-12-03; Internetfabriken
    Comment:     
*/

// The mighty dollar function
// Renamed $$ by frax
function $$() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

// Get a cookie by name
function GetCookie(name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) 
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return GetCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

// Get Cookie value
function GetCookieVal(offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

// Set cookie by name
function SetCookie (name, value) 
{
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}

// Delete cookie by name
function DeleteCookie(name) 
{    
    var exp = new Date();
    exp.setTime (exp.getTime() - 1000000000);
    SetCookie(name,'', exp.toGMTString());
}

// Show a layer
function ShowLayer(targetId) {
    try {
        var target = $$(targetId);
        target.style.display = 'block';
    }
    catch (err) { }
}

// Hide a layer
function HideLayer(targetId) {
    try {
        var target = $$(targetId);
        target.style.display = 'none';
    }
    catch (err) { }
}

// Show/Hide a layer
function ToggleLayer(targetId) {
    try {
        var target = $$(targetId);
        if (target.style.display == 'block') target.style.display = 'none';
        else target.style.display = 'block';
    }
    catch (err) { }
}

// Clear textbox if it's contents is the default value
function ClearTextBox(tb) {
    if (tb.defaultValue == tb.value) {
        tb.value = "";
    }
}

// Reset textbox value to default value if it's contents is empty
function ResetTextBox(tb) {
    if (tb.value == "") {
        tb.value = tb.defaultValue;
    }
}

// Open a popup window
function OpenPopUp(url) {
    window.open(url, "WindowPopUp", "menubar=no,width=460,height=600,toolbar=no");
}

// Submit a button on enter keys
function XForm_SubmitOnEnter(buttonName, event) {
    var key = 0;
    if (window.event) key = window.event.keyCode;
    else if (event) key = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);

    if (key == 13) {
        
        // Find submit button and trigger it
        var all = document.getElementsByTagName("input");
        for (var i = 0; i < all.length; i++) {
            if (all[i].type == "submit" && all[i].name == buttonName)
            {
                var submitButton = all[i];
                //submitButton.form.submit();
                submitButton.click();
                return false;
            }
        }
    }

    return true;
}

function initFileUploads() {
    var W3CDOM = (document.createElement && document.getElementsByTagName);
    if (!W3CDOM) return;
    var fakeFileUpload = document.createElement('div');
    fakeFileUpload.className = 'fakeinputs';
    var textBox = document.createElement('input');
    textBox.type = 'text';
    textBox.className = 'fakebox';
    fakeFileUpload.appendChild(textBox);
    var image = document.createElement('span');
    image.innerHTML = '&nbsp;';
    image.className = 'browseimage';
    fakeFileUpload.appendChild(image);
    var x = document.getElementsByTagName('input');
    for (var i = 0; i < x.length; i++) {
        if (x[i].type != 'file') continue;
        if (x[i].parentNode.className != 'fileinputs') continue;
        x[i].className = 'file hidden';
        var clone = fakeFileUpload.cloneNode(true);
        x[i].parentNode.appendChild(clone);
        x[i].relatedElement = clone.getElementsByTagName('input')[0];
        x[i].onchange = x[i].onmouseout = function() {
            this.relatedElement.value = this.value;
        }
    }
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function PopupWindow() {
    var marketPopup = getCookie('MarketPopup');
    if (marketPopup == null || marketPopup == "") {
        var leftMargin = (screen.width - 600) / 2;
        setCookie('MarketPopup', 'MarketPopup', 8);
        window.open('http://www.bwz.se/forma/b.aspx?subscribeto=20&ucrc=E2E51191', null,
            'height=550,width=600,left=' + leftMargin + ',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
    }
}
