//Scrips 2.0 with ajax check of the static value


Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

function file_basename(filename)
 {
  base='';
  i=filename.length;
  while((ch=filename.charAt(i))!='/' && ch!='\\' && i>0)
   {
    base=ch+base;
    i--;
   }
  return base.toLowerCase();
 }
  

function file_extension(filename)
 {
  ext='';
  i=filename.length;
  while((ch=filename.charAt(i))!='.' && i>0)
   {
    ext=ch+ext;
    i--;
   }
  return ext.toLowerCase();
 }
 
 
function test_input()
 {
  ok=true;
  i=1;
  while (f=document.getElementById('r'+i))
   {
    if (typeof window['tinyMCE']=='object' && tinyMCE.get('r'+i)) val=tinyMCE.get('r'+i).getContent(); else val=document.getElementById('r'+i).value;
    if (val=='') ok=false;
    i++;
   }
  if (ok==false) alert("Вам нужно заполнить все обязательные поля!");
  return ok;
 }
 
 
function test_filename(filename)
 {
  if ((file_extension(filename))!='xls')
   {
    alert('Вы можете импортировать файлы только .xls типа ');
    return false;
   }
 }
 
function insert_email(id)
 {
  obj=document.getElementById('i_'+id);
  if (obj.value=='0')
   {
    obj.value='1';
    document.getElementById('emails').value+=document.getElementById('email_'+id).title+'; ';
   }
  else
   {
    alert('Ви вже додали у список даного користувача');
   }
 }
 
function tinymceonchange()
 {
  if (document.delivery) document.delivery.text.value=tinyMCE.get('r2').getContent();
 }
 
function change_banner_type(type)
 {
  if (type=='c') hide='p'; else hide='c';
  i=1;
  while (show_el=document.getElementById(type+'_'+i++)) show_el.style.display='';
  i=1;
  while (hide_el=document.getElementById(hide+'_'+i++)) hide_el.style.display='none';
 }
 
function put_shop_coords()
 {
  //alert(window.event.clientX);
  //alert(this.)
 }
 
function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}

function put_marker(obj,event)
 {
  mouse_x=mousePageXY(event).x;
  mouse_y=mousePageXY(event).y;

  document.getElementById('marker').style.display='';
  
  /*
  document.getElementById('marker').style.top=(mouse_y-document.getElementById('div_map').offsetTop-23)+'px';
  document.getElementById('marker').style.left=(mouse_x-document.getElementById('div_map').offsetLeft)+'px';
  
  v_proc=(mouse_y-document.getElementById("div_map").offsetTop-obj.offsetTop)/obj.offsetHeight*100; 
  h_proc=(mouse_x-document.getElementById("div_map").offsetLeft-obj.offsetLeft)/obj.offsetWidth*100;*/
  
  document.getElementById('marker').style.top=mouse_y-18+'px';
  document.getElementById('marker').style.left=mouse_x-9+'px';
  
  //alert(obj.offsetTop);
  //alert(mouse_y);
  
  v_proc=(mouse_y-obj.offsetTop)/obj.offsetHeight*100; 
  h_proc=(mouse_x-obj.offsetLeft)/obj.offsetWidth*100;
  
  document.main.x.value=h_proc;
  document.main.y.value=v_proc;
 }

function insert_text(element, text)
{
 element.focus();
 if (document.selection)
 {
  var s = document.selection.createRange();
  s.text = text;
  s.select();
 }
 else if (typeof element.selectionStart == "number" && typeof element.selectionEnd == "number")
 {
  var start = element.selectionStart;
  var end = element.selectionEnd;
  element.value = element.value.substr(0, start) + text + element.value.substr(end);
  element.setSelectionRange(start += text.length, start);
 }
 else element.value += text;
}

function insert_html(html)
{
 tinyMCE.execCommand('mceInsertRawHTML',false, html);
}

function image_template(path, title)
{
 var href = prompt("Ссылка:");
 return (href ? "<a href=\"" + href + "\" target=\"_blank\">" : "") + "<img src=\"" + path + "\" border=\"0\" title=\"" + title + "\" />" + (href ? "</a>" : "");
}

function flash_template(path)
{
 var width = prompt("Ширина flash:");
 var height = prompt("Высота flash:");
 width = width && !isNaN(width = parseInt(width)) ? " width = \"" + width + "\"" : '';
 height = height && !isNaN(height = parseInt(height)) ? " height = \"" + height + "\"" : '';
 return "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\"" + width + height + ">\n" +
        " <param name=\"movie\" value=\"" + path + "\" />\n" +
        " <param name=\"quality\" value=\"high\" />\n" +
        " <embed src=\"" + path + "\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\"" + width + height + "></embed>\n" +
        "</object>";
}

function file_template(path, title)
{
 return "<a href=\"" + path + "\" target=\"_blank\">" + title + "</a>";
}

function insert_file(path, title)
{
 var file = path.split("/").pop();
 var file_name = file.split(".");
 var file_ext = (file_name.length > 1) ? file_name.pop() : "";
 file_name = file_name.join(".");
 if (!title) title = file;
 var template;
 switch(file_ext)
 {
  case "gif":
  case "jpe":
  case "jpeg":
  case "jpg":
  case "png": template = image_template(path, title); break;
  case "swf": template = flash_template(path); break;
  default: template = file_template(path, title);
 }
 insert_html(template);
}

function insert_file_ex(path, title, element)
{
 var file = path.split("/").pop();
 var file_name = file.split(".");
 var file_ext = (file_name.length > 1) ? file_name.pop() : "";
 file_name = file_name.join(".");
 if (!title) title = file;
 var template;
 switch(file_ext)
 {
  case "gif":
  case "jpe":
  case "jpeg":
  case "jpg":
  case "png": template = image_template(path, title); break;
  case "swf": template = flash_template(path); break;
  default: template = file_template(path, title);
 }
 insert_text(element, template);
}
/*
function showline(row)
 {
  if (row<=10) document.getElementById('row'+row).style.display='';
 }*/
 

function make_static(mode,id,normal_field_value,father,staticobject)
 {
  normal_words = Array('А','а','Б','б','В','в','Г','г','Ґ','ґ','Д','д','Е','е','Є','є','Ж','ж','З','з','И','и','І','і','Ї','ї','Й','й','К','к','Л','л','М','м','Н','н','О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ч','ч','Ш','ш','Щ','щ','Ю','ю','Я','я','Ь','ь','Ы','ы',' ','Ъъ','-','Э','э');
  translate_words = Array('A','a','B','b','V','v','Gh','gh','G','g','D','d','E','e','Je','je','Zh','zh','Z','z','I','i','I','i','Ji','ji','J','j','K','k','L','l','M','m','N','n','O','o','P','p','R','r','S','s','T','t','U','u','F','f','Kh','kh','C','c','Ch','ch','Sh','sh','Shh','shh','Ju','ju','Ja','ja','J','j','Y','y','-','','-','E','e');
  result='';
  
  for(i=0;i<normal_field_value.length;i++)
   {
    if ((word_index=(normal_words.indexOf(normal_field_value[i])))!=-1)
     result+=translate_words[word_index];
    else
     if ((code=normal_field_value.charCodeAt(i)) && ((code>=48 && code<=57) || (code>=65 && code <=90) || (code>=97 && code<=122))) result+=normal_field_value.charAt(i);
   }
  if (staticobject==undefined) staticobject=document.main.static;
  staticobject.value=result;
 
  if (result && mode) check_static(staticobject.value,mode,id,father);
 }
