/*
 * Smallbox CMS http://www.smallboxsoftware.com
 *
 * Copyright (C) 2000-2008 Smallbox Software Inc.
 * Author: Kenneth Spencer
 *
 * This file is intended only for use withing Smallbox CMS.
 * Unauthorized use is strictly prohibited.
 *
 */

var sbpc;
var debug = false;
var modified_fields = new Array();
var errorArray = new Array();
var firstError = 0;
var errorCheckingActivated = 0;
var errorAlert = false;
var errorMode = new Array();
var errorEdit = new Array();
var enableErrors = true;
var currentError;

function sb_link(Link, Action, Form, disableErrors) {
 if(Form && document[Form]) {
   var pass = true;

   if(disableErrors == 1) {
    disable_errors();
   }


   if(typeof(rteList) != "undefined") {
    for(i = 0; i < rteList.length; i++) {
     setTextarea(rteList[i]);
    }
   }

  if(enableErrors == true && (errorEdit[Form] || errorMode[Form] == 'hard' || (window.sb_blank_form && sb_blank_form(Form)))) {
   errorCheckingActivated = 1;
   pass = sb_check_errors(Form, true);
  }
  if(pass == true) {
   document[Form].action =  Link;
   if(document[Form].sb_action) {
    if(modified_fields[Form]) {
     var Val =  Action+":"+modified_fields[Form].join(',');
     document[Form].sb_action.value = Val;
    }
    else {
     document[Form].sb_action.value = Action;
    }
   }
   document[Form].submit();
  }
 }
 else {
/*
  if(Action)
   window.location.href = Link+"&sb_action="+Action;
  else
*/
   window.location.href = Link;
 }
}

function sb_save_search() {
 var str;
 if(str = window.prompt("Enter name of saved search")) {
  document.search_form.search_title.value = str;
  return true;
 }
}

function sb_button_on(thisId) {
 if(disable_events == 1) return;
 thisId.className='adminButton adminButtonOn'; 
}

function sb_button_off(thisId) {
 if(disable_events == 1) return;
 thisId.className='adminButton'; 
}

function sb_sub_button_on(thisId) {
 if(disable_events == 1) return;
 thisId.className='adminSubButtonOn'; 
}

function sb_sub_button_off(thisId) {
 if(disable_events == 1) return;
 thisId.className='adminSubButton'; 
}

function sb_delete(Page) {
 if(confirm("Are you sure you wish to delete this item?")) {
  window.location.href=Page;
 }
}




function sb_is_display(thisId) {
 for(var p = thisId; p.nodeType == 1 ; p = p.parentNode) {
  if(p.style.display == 'none') {
   return false;
  }
 }
 return true;
}


function number_format(num, decimal) {
 var str = "" + Math.round(num * Math.pow(10, decimal));
 while(str.length <= decimal) {
  str = "0" + str;
 }
 var position = str.length - decimal;
 return str.substr(0, position)+"."+str.substr(position, str.length);
}

function number_pad(Num, Len) {
 str = ""+Num;
 while(str.length < Len) {
  str = '0' + str;
 }
 return str;
}





function sb_help_request(node, file) {
 sb_load_xml(node, "/smallbox4/help/"+file+".xml", sb_help);
}

function sb_help (thisId, doc) {
 escapeClear();
 var Message = "error loading help";
 var title   = "";
 tags = doc.getElementsByTagName("help");
 for(i = 0; i < tags.length; i++) {
  tag = tags.item(i);
  if(tag.getAttribute("language") == "EN") {
 //  Message = tag.firstChild.nodeValue;
   Message = tag;
   title = tag.getAttribute("title");
   break;
  }
 }


 pos = getPos(thisId); 
 cont = sb_node(document, "div");
 cont.style.visibility = 'hidden';
 cont.style.position = 'absolute';
 cont.style.top  = pos.top+'px';
 if(pos.left + 300 > document.body.offsetWidth)
  cont.style.left = (pos.left  - 305)+'px';
 else
  cont.style.left = (pos.left + thisId.offsetWidth + 5)+'px';
 cont.className = "help";
 div = sb_node(cont, "div", title);
 div.className = "title";

 div = sb_node(div, "div");
 div.className = "minus";
 img = sb_node(div, "img");
 img.src = "/smallbox4/images/minus.gif";
 img["onclick"] = escapeClear;
 img.style.cursor = 'pointer';

 div = sb_node(cont, "div");
 div.className = "text";

 sb_import_html(div, Message); 

/*
 var text = document.createTextNode(Message);
 div.appendChild(text);
*/

 escapeNode(cont, true);
 cont.style.visibility = 'visible';
}


function sb_id_go(link) {
 var num = parseInt(document.id_form.id.value);

 if(!isNaN(num)) {
  window.location.href = link.replace("sbREP", num);
 }
}


function getCookie(Variable) {
 temp = document.cookie.split(';');
 for(i=0; i< temp.length; i++) {
  temp2=temp[i].split('=');
  name = temp2[0].replace(/^\s*/, "");
  if(name ==Variable) {
   return temp2[1];
  }
 }
 return false;
}

function sb_icon(node, src, alt, ref) {
 var over = src.replace(".", "_over.");
 var path = "/smallbox4/images";
 var img = sb_node(node, "img");
 img.src = path+"/"+src;
 img.alt = alt;
 img.title = alt;
 img.style.cursor = "pointer";
 img["onclick"] = ref;
 img["onmouseover"] = function() {this.src = path+"/"+over;}
 img["onmouseout"] = function() {this.src = path+"/"+src;}
 return img;
}

function sb_in_array(array, value) {
 for(var i = 0; i < array.length; i++) {
  if(array[i] == value) {
   return true;
  }
 }
}

function sb_email(mailbox, host, text) {
 var email = mailbox+'@'+host;
 if(!text) {
  text = email;
 }
 document.writeln("<a href='mailto:"+email+"'>"+text+"</a>");
}
