/*
-- Program: CakeMail Interface
-- Author: The Code Kitchen Inc.
-- Email: info@cakemail.com
-- Date: 10/31/2007
@ [2007] - The Code Kitchen.

This software is subjected to the terms and conditions of the CakeMail Interface
License Agreement, version 1.0. You cannot use this software, except by 
complying to the terms of this license.  In short, the CakeMail interface is 
available for you to download, use, copy, modify, integrate in your own software
and propagate, as long as you agree to use it exclusively with the CakeMail 
hosted application service and application programming interface (API). You can
download a copy of the license at the following URL:
http://www.cakemail.com/license_1.0

This software is propagated "as is", without any warranty of any kind, express
or tacit.  Read the license to learn the exact conditions pertaining to your 
rights and obligations in relation with this work.
*/


function check_session(root_path) {
    return true;

    if (null == initXmlHttpRequest()) {
        return false;
    }

    var xmlHttp = initXmlHttpRequest();

    if (xmlHttp == null) {
        alert(browser_error);
        return false;
    }

    var url = root_path + "check_session.php";

    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var response = xmlHttp.responseText;
            if ("true" == response) {
                setTimeout("check_session('" + root_path + "')", 10000);
            } else {
                var params = new Array(); 
                params['height'] = 444; 
                params['width'] = 800; 
                iBox.setPath(root_path + images_path);
                iBox.showURL(root_path + 'signin.php?content=raw', popup_title,  params);
                // wait for the pop-up
                while (document.getElementById("ibox_overlay").style.display != "block") {
                    setTimeout("", 500);
                }
                check_logged_in(root_path);
            }
        }
    }
    xmlHttp.send("");

    return false;
}

// function to restart to session checker
function check_logged_in(root_path) {
    if (document.getElementById("ibox_overlay").style.display == "none") {
        if (null == initXmlHttpRequest()) {
            return false;
        }

        var xmlHttp = initXmlHttpRequest();

        if (xmlHttp == null) {
            alert(browser_error);
            return false;
        }

        var url = root_path + "check_session.php";

        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                var response = xmlHttp.responseText;
                if ("true" == response) {
                    check_session(root_path);
                } else {
                    window.location = root_path + "signin.php";
                }
            }
        }
        xmlHttp.send("");
    } else {
        setTimeout("check_logged_in('" + root_path + "')", 500);
    }
}


// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function
(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
//~

function addslashes_all(str) {
    str = str.replace(/\'/g,'\\\'');
    str = str.replace(/\"/g,'\\"');
    str = str.replace(/\\/g,'\\\\');
    return str;
}


function addslashes(str) {
    str = str.replace(/\"/g,'\\"');
}


function autocorrect_url(element_id) {
	var value = document.getElementById(element_id).value;
	if ("" == value) {
		return true;
	}
	if (-1 == value.search(/http/) && -1 == value.search(/:\/\//)) {
		value = "http://" + value;
		document.getElementById(element_id).value = value;
	}
	return true;
}


// get the coordinates to position an element on the left-top of the display
function on_top() {
	if (window.innerHeight) return [window.innerWidth-10,window.innerHeight-10,window.pageXOffset,window.pageYOffset + 10];
	else if (document.documentElement.clientHeight) return [document.documentElement.clientWidth-10,document.documentElement.clientHeight-10,document.documentElement.scrollLeft,document.documentElement.scrollTop + 10];
	return [document.body.clientWidth,document.body.clientHeight,document.body.scrollLeft,document.body.scrollTop + 10];
}


// toggle the personalization cheat
function localization_cheat_toggle(root) {
    var xmlHttp = initXmlHttpRequest();

    if (xmlHttp == null) {
        alert(browser_error);
        return;
    }

    xmlHttp.open("POST", root + "localization_cheat_toggle.php", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var response = xmlHttp.responseText;
            window.location.reload();
            return false;
        }
    }
    xmlHttp.send("");

    return false;
}


function stripslashes_all(str) {
    str = str.replace(/\\'/g,'\'');
    str = str.replace(/\\"/g,'"');
    str = str.replace(/\\\\/g,'\\');
    return str;
}


function strip_tags(element) {
    //element.value = element.value.stripTags();
}


function strip_tags_form(form) {
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "text" || form.elements[i].type == "password") {
            //form.elements[i].value = form.elements[i].value.stripTags();
        }
    }
}

function change_div_display(divObj, isVisible, visibleStyle) {
	try {
		divObj.style.setAttribute("display", isVisible ? visibleStyle : "none");
	} catch(e) {
		divObj.style.display = isVisible ? visibleStyle : "none";
	}
}

function show_client_dd() {
	change_div_display(document.getElementById("header_top_menu"), false);
	change_div_display(document.getElementById("switch_client"), true, "inline");
	return false;
}

function hide_client_dd() {
	change_div_display(document.getElementById("switch_client"), false);
	change_div_display(document.getElementById("header_top_menu"), true, "inline");
	return false;
}

function switch_to_client() {
	var client_id = document.getElementById("client_id_to_switch").value;
	if (client_id) {
		window.location.href = "admin/clients.php?action=login&client_id=" + client_id;
	}
	return false;
}

/**
 * Copies all the properties of config to obj.
 * @param {Object} obj The receiver of the properties
 * @param {Object} config The source of the properties
 * @param {Object} defaults A different object that will also be applied for default values
 * @return {Object} returns obj
 */
function apply(o, c, defaults) {
	if (defaults){
		apply(o, defaults);
	}
	if (o && c && typeof c == 'object') {
		for(var p in c){
			o[p] = c[p];
		}
	}
	return o;
}

/**
 * Copies all the properties of config to obj if they don't already exist.
 * @param {Object} obj The receiver of the properties
 * @param {Object} config The source of the properties
 * @return {Object} returns obj
 */
function applyIf(o, c) {
	if (o) {
		for (var p in c){
			if (isEmpty(o[p])) {
				o[p] = c[p];
			}
		}
	}
	return o;
}

/**
 * <p>Returns true if the passed value is empty.</p>
 * @param {Mixed} value The value to test
 * @param {Boolean} allowBlank (optional) true to allow empty strings (defaults to false)
 * @return {Boolean}
 */
function isEmpty(v, allowBlank) {
	return v === null || v === undefined || ((isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
}

/**
 * Returns true if the passed object is a JavaScript array, otherwise false.
 * @param {Object} object The object to test
 * @return {Boolean}
 */
function isArray(v) {
	return toString.apply(v) === '[object Array]';
}

/**
 * Returns true if the passed object is a JavaScript Object, otherwise false.
 * @param {Object} object The object to test
 * @return {Boolean}
 */
function isObject(v) {
	return v && typeof v == "object";
}

/**
 * Returns true if the passed object is a JavaScript 'primitive', a string, number or boolean.
 * @param {Mixed} value The value to test
 * @return {Boolean}
 */
function isPrimitive(v){
	return isString(v) || isNumber(v) || isBoolean(v);
}

/**
 * Returns true if the passed object is a JavaScript Function, otherwise false.
 * @param {Object} object The object to test
 * @return {Boolean}
 */
function isFunction(v) {
	return toString.apply(v) === '[object Function]';
}

/**
 * Returns true if the passed object is a number. Returns false for non-finite numbers.
 * @param {Object} v The object to test
 * @return {Boolean}
 */
function isNumber(v) {
	return typeof v === 'number' && isFinite(v);
}

/**
 * Returns true if the passed object is a string.
 * @param {Object} v The object to test
 * @return {Boolean}
 */
function isString(v) {
	return typeof v === 'string';
}

/**
 * Returns true if the passed object is a boolean.
 * @param {Object} v The object to test
 * @return {Boolean}
 */
function isBoolean(v) {
	return typeof v === 'boolean';
}

/**
 * Returns true if the passed object is not undefined.
 * @param {Object} v The object to test
 * @return {Boolean}
 */
function isDefined(v) {
	return typeof v !== 'undefined';
}
