﻿/*
 * Beanstr Common Functions
 *
 * Copyright (c) 2009 Mehrbod Sharifi
 *
 */
 
function z(txt) {
    var d = $("#debug");
    if (d.length == 0) {
        $("body").append("<div id='debug' style='background-color: White;'><b>Debug Panel:</b><br /></div>");
        d = $("#debug");
    }
    d.append((new Date()) + ": " + txt + "<br />");
}

jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

function centerTop(p) {
    return ($(window).height() - p) / 2 + $(window).scrollTop() + "px";
}

function centerLeft(p) {
    return ($(window).width() - p) / 2 + $(window).scrollLeft() + "px";
}


function createCustomDropDown(arr, cols) {
    if (!cols) cols = 1;
    $.each(arr, function(i, item) { optionDiv.append("<a href='#' class='choicelist'>" + item + "</a>&nbsp;" + (((i + 1) % cols == 0) ? "<br />" : "")); });
    optionDiv.find("a").each(function(i) {
        $(this).click(function(event) {
            activeTxt.attr("value", $(this).text());
            optionDiv.hide();
            event.preventDefault();
        });
    });
}

//$(function() { z("loaded") });

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 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 promptCookies() {
    alert(document.cookie);
    //for (var i = 0; i < document.cookie.length; ++i)
        //alert(document.cookie[i]);
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

if (!Number.toFixed) {
    Number.prototype.toFixed = function(x) {
        var temp = this;
        temp = Math.round(temp * Math.pow(10, x)) / Math.pow(10, x);
        return temp;
    }
}