/* System IX-CMS javascript's
 (c) 2006 by ixRock (roman@ixrock.net) */

var isFF = navigator.userAgent.indexOf('Gecko')!=-1 || navigator.userAgent.indexOf('Firefox')!=-1;
var upload_counter = 2;
var counter = 0;

function create_request(method, request, datasend, handler) {
      /**
       * Функция создает запрос XMLHttpRequest, получает или отсылает данные
       * (string) method = метод отправки данных (обычно GET или POST);
       * (string) request = абсолютный или относительный путь до файла (этот файл и запрашиваемый должны быть с одного домена);
       * (string) datasend = данные отсылаемые при запросе (например: nick=ixRock&msg=JS/PHP 4EVER!);
       * (function) handler = функция обработчик для события onreadystatechange, by default в виде первого агрумента будет принимать xml.responseText;
       */
       handler = handler || false;
       if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
       else {
              try { xhr = new ActiveXObject("Msxml2.XMLHTTP") }
              catch(e) {
                     try { xhr = new ActiveXObject("Microsoft.XMLHTTP") }
                     catch(e) {
                            try { xhr = new ActiveXObject("Msxml2.XMLHTTP.4.0") }
                            catch(e) { xhr = null }
                            }
              }
       }

       if(xhr != null) {
              rand = method.toUpperCase() == 'GET' ? '?' + Math.random() * 10E15 : '';
              datasend = method.toUpperCase() == 'POST' ? encodeURI(datasend) : datasend;
              xhr.open(method, request + rand, true);
              xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
              xhr.send(datasend);
       xhr.onreadystatechange = function() {
              if(xhr.readyState == 4) {
                     if(xhr.status == 404) alert('Запрашиваемый файл не существует! \n\t(404 - Not Found)');
                     else if(xhr.status == 403) alert('Этот файл запрещен для просмотра! \n\t(403 - Forbidden)');
                     else if(xhr.status == 401) alert('Пользователь не авторизирован! \n\t(401 - Unauthorized)');
                     else if(handler != false) {
                            handler(xhr.responseText);
                            xhr.onreadystatechange = function() {}
                            delete xhr;
                            }
                     }
              }
       }
       else alert('Ваш браузер не поддерживает AJAX-технологию!'); 
}

function xhr_replace(pattern, str) { return eval("'" + str.replace(pattern, "' + encodeURIComponent('$1') + '") + "'") }
function xhr_replace_simple(str) { return str.replace(/\?/g, "%3F").replace(/\+/g, "%2B").replace(/&/g, "%26") }

function $(obj_id) {
       if(document.getElementById && (o = document.getElementById(obj_id)) != null) {}
       else if(document.all && (o = document.all[obj_id]) != null) {}
       else o = eval(obj_id);
       return o;
       }

function confirm_delete_item(filename) {
       filename = filename || '';
       filename = filename != ''?' ' + filename:'';
       if(confirm('Удаляем'+filename+'?','')) return true;
       else return false;
       }

function check_form(objs,values) {
var errors, i, obj;
var areas=objs.split(',');
var areas_= (values || "именем,адресом электронной почты,темой сообщения,сообщением").split(',');
for(i=0;i<areas.length;i++) {
       obj=eval('document.forms[0].'+areas[i]);
       if(/^\s*$/.test(obj.value)) {
              alert('Незаполнено поле с '+areas_[i]+'!');
              obj.focus();
              errors=true;
              break;
              }
       }
return !errors;
}

function go_back() {
       history.back();
       return false;
       }

/* 4upload files on server */
function del_upload_file(obj,locked_button) {
       if(upload_counter <= 3) locked_button.disabled = true;
       upload_counter = (upload_counter == 2)?++upload_counter:upload_counter;
       
       id_name='name'+(--upload_counter);
       if(!isFF) obj.removeChild(obj.children[id_name])
       else {
              for(i=0;i<obj.childNodes.length;i++) {
                     if(obj.childNodes[i].id==id_name) obj.removeChild(obj.childNodes[i])
                     }
              }
}
function add_upload_file(obj,del_button) {
       if(del_button.disabled) del_button.disabled = false;
       obj.innerHTML += '<div id=name'+upload_counter+' style="margin-top:5px;">'
+'<font id=file_number style="margin-left:10px;">Файл №'+upload_counter+'</font>'
+' <input type=file name="uploaded_files[]" class=uploaded_more></div>';

       upload_counter++;
}
/*                   */

function check_upload_form(obj) {
       if(obj.value.search(/^\s*$/) != -1) { 
              alert('Невыбран файл загрузки!');
              return false;
              }
       }

function del_templ(obj,prefix) {
       if(confirm('Удаляем шаблон?','')) {
       prefix = prefix || '';
              if(obj != '') location.href += prefix + obj;
       }
       else return false;
}


function win_open(url,w,h) {
       counter++;
       w = w || 920;
       h = h || 675;
       pos_x = (screen.width - w)/2;
       pos_y = (screen.height - h)/2;
       open(url,'ix_window_'+counter,"width="+w+",height="+h+",left="+pos_x+",top="+pos_y+",toolbar=no,menubar=no,status=no");
       }


function admin_hint(obj_img, obj_hint) {
       obj_hint.innerHTML = obj_img.title;
       obj_img.onmouseout = function() {
              obj_hint.innerHTML = "";
              }
       }


function set_header(obj_link) {
       obj = $('header_title');
       old_header = obj.innerHTML;
       obj.innerHTML = obj_link.title;
       obj.style.color = '#666';
       obj_link.onmouseout = function out() {
              obj.innerHTML = old_header;
              obj.style.color = 'black';
              }
       if(window.opera != null) obj_link.firstChild.onmouseout = out;
       }


