//  ------------------------------------------------------------------------ //
//              Brick CMS - PHP Content Management System                    //
//                    Copyright (c) 2007 Brick-CMS.org                       //
//                    <http://www.brick-cms.org/>                            //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

/** 
 * Brick CMS javascripts
 * 
 * @name javascripts 
 * @author Brick CMS Team 
 * @link http://www.brick-cms.org
 * @copyright Brick CMS 
 * @package manager/scripts.js 
 */

function getElement(id) { return $('#'+id).get(0); }
function show(id, type) { $(type+'#'+id).slideDown('normal'); }
function hide(id, type) { $(type+'#'+id).slideUp('normal'); }
function show_hide(id, img) {
	var style = $("#"+id).css("display");
	if(style == 'block' ) {
		$('#'+id).slideUp('normal');
		if(img != '') {
			$('#'+img).attr('src', getElement('image_url').innerHTML + 'open.png');
		}
	} else {
		$('#'+id).slideDown('normal');
		if(img != '') {
			$('#'+img).attr('src', getElement('image_url').innerHTML + 'close.png');
		}
	}
}
function httpRequest(url, target_result, param) {
	if(getElement(target_result).innerHTML) {
		getElement(target_result).innerHTML = '<img src="' + getElement('image_url').innerHTML + 'waiting.gif" alt="O" />';
	}
	var xmlhttp = false;
	if(window.XMLHttpRequest) // Firefox
		xmlhttp = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else { // not supported
		getElement(target_result).innerHTML = 'Browser not compatible...';
		return;
	}
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			getElement(target_result).innerHTML = xmlhttp.responseText;
			getElement(target_result).value = xmlhttp.responseText; // For insert in input or textarea items
		}
	}
	xmlhttp.send(param);
}

function askConfirmation(message, url, true_button, false_button) { if(confirm(message)) { window.location.href = url; } }
function checkRequiredFields(form_id, message) {
	var form = getElement(form_id);
	var required_fields = $('form#'+form_id+' .required');
	var fields_list = '';
	$.each(required_fields, function(i, n){
		if(n.value == '') {
			var field = getElement(n.id)
			var label = getElement(n.id + '_label');
			fields_list += (fields_list != '') ? ', ' + label.innerHTML : label.innerHTML;
			label.style.color = 'red';
			field.focus();
		}
	});
	if(fields_list != '') {
		alert(message + fields_list);
		return false;
	} else {
		return true;
	}
}
function textareaMaxLength(textarea_id, maxlength, message) {
	var textarea = getElement(textarea_id);
	if(textarea.value.length > maxlength) {
		alert(message);
		if (document.selection) { //Internet Explorer
			sel.text = textarea.value.substring(0, maxlength);
		} else if (textarea.selectionStart >= 0) { //Gecko
			textarea.selectionStart = 0;
			textarea.selectionEnd = maxlength;
		} else { //Others
			textarea.value = textarea.value.substring(0, maxlength);
		}
		textarea.focus();
		return false;
	} else {
		return true;
	}
}
function checkAll(checkboxes, checker) {
	var checked_status = checker.checked;
	return $('.'+checkboxes).each(function() {
    	this.checked = checked_status;
    });
}
function preview_image(selector, directory) {
	var image_select = getElement(selector);
	var image_input = getElement(selector + '_preview');
	if(image_select.value != '') {
		image_input.src = directory + '/' + image_select.value;
		image_input.style.display = 'inline';
	} else {
		image_input.style.display = 'none';
	}
}

var form_datas = 0;
window.onbeforeunload = function() { if(form_datas > 0) { return ""; } }
