/*
 * 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 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;

//    var s = parseInt(now, 10);

function sb_link(Link, Action, Form, disableErrors, hasWin) {
 if (typeof(sb_lock_check) == 'function' && sb_lock_check()) {
  if (confirm("Warning, page is locked. No changes will be saved.\n\nContinue?")) {
   window.location.href = Link;
  }
  return;
 }

 if(Form && document[Form]) {
  var pass = true;

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

  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) {
    var modified_fields = false;

    var elements = document[Form].elements;
    for (var i = 0; i < elements.length; i++) {
     if (elements[i].value != elements[i].defaultValue) {  
      modified_fields = true;
      break;
     }
    }

    /** RTE does not submit its value at this time so check it manually **/
    if (typeof(CKEDITOR) != "undefined") {
     for (var i in CKEDITOR.instances) {
      if (CKEDITOR.instances[i].checkDirty()) {
       modified_fields = true;
       break;
      }
     }
    }
    document[Form].sb_action.value = Action + (modified_fields ? ':yes' : '');
   }
   if (hasWin) {
    sb_window(null, hasWin, 'center');
   }

   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_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' || p.className.match(/^noErrors$|^noErrors | noErrors$| noErrors /)) {
   return false;
  }
 }
 return true;
}


function number_format (number, decimals, dec_point, thousands_sep) {
 var n = number, prec = decimals;
 
 var toFixedFix = function (n,prec) {
  var k = Math.pow(10,prec);
  return (Math.round(n*k)/k).toString();
 };
 
 n = !isFinite(+n) ? 0 : +n;
 prec = !isFinite(+prec) ? 0 : Math.abs(prec);
 var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
 var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
 var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
 var abs = toFixedFix(Math.abs(n), prec);
 var _, i;
 
 if (abs >= 1000) {
  _ = abs.split(/\D/);
  i = _[0].length % 3 || 3;
 
  _[0] = s.slice(0,i + (n < 0)) + _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
  s = _.join(dec);
 } 
 else {
  s = s.replace('.', dec);
 }
 
 var decPos = s.indexOf(dec);
 if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
  s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
 }
 else if (prec >= 1 && decPos === -1) {
  s += dec+new Array(prec).join(0)+'0';
 }
 return s;
}
/*
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, local, level) {
 var ref = function(req) {
  if (req.responseXML) {
   sb_help (node, req.responseXML, level)
  }
 }
 sb_req((local == true ? '/includes/help/' : "/smallbox4/help/")+file+".xml", ref);
}

function sb_help(thisId, doc, level) {
 escapeClear(level);
 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;
  }
 }

/*
 var kit = new sb_window_kit(title, level);
 sb_import_html(kit.body, Message); 
 kit.relativeNode = thisId;
 kit.cont.style.width = '350px';
 kit.append();
 return;
*/
 pos = getPos(thisId); 
 cont = sb_node(document, "div");
 cont.style.top  = pos.top+'px';
 cont.className = "help";

 if(pos.left + cont.offsetWidth > document.body.offsetWidth)
  cont.style.left = (pos.left  - cont.offsetWidth - 5)+'px';
 else
  cont.style.left = (pos.left + thisId.offsetWidth + 5)+'px';
 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/images5/icons/close_small.png";
 img["onclick"] = escapeClear;
 img.style.cursor = 'pointer';
 sb_rollover(img);


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

 sb_import_html(div, Message); 


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

 escapeNode(cont, true, level);
 cont.style.visibility = 'visible';
 document.body.appendChild(cont);
}


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 setCookie (name, value,  path, expiration_date) {
 var cookie_string;
 if (!expiration_date) {
  var expiration_date = new Date ();
  expiration_date.setYear (expiration_date.getFullYear() + 1);
  expiration_date = expiration_date.toGMTString();
 }

 cookie_string = escape(name) + "=" + escape (value); 
 cookie_string    += ";expires=" + expiration_date;
 cookie_string += ";path=" + (path ? path : '/');

//alert(cookie_string);
 document.cookie = cookie_string;
}


function deleteCookie (name, path) {
 var expiration_date = new Date ();
 expiration_date.setYear (expiration_date.getFullYear () - 1);
 expiration_date = expiration_date.toGMTString ();

 var cookie_string = escape (name) + "=;expires=" + expiration_date;
 cookie_string += ";path=" + (path ? path : '/');


 document . cookie = cookie_string;
}



function sb_icon(node, src, alt, ref) {
 if (src[0] == '/') { 
  var path = src.replace(/\/[^/]+$/, '');
  src = src.replace(/^.+\//, '');
 }
 else if(src.match("\.png$"))
  var path = "/smallbox4/images5/icons";
 else
  var path = "/smallbox4/images";
 var over = src.replace(".", "_over.");

 var img = sb_node(node, "img");
 img.src = path+"/"+src;
 img.alt = alt;
 img.style.cursor = "pointer";
 img["onclick"] = function(e) {
  if (ref) {
   ref(e);
  }
  sb_tooltip_clear();
 }

 img["onmouseover"] = function() {
  sb_tooltip(this, alt);
  this.src = path+"/"+over;
 }
 img["onmouseout"] = function() {
  sb_tooltip_clear();
  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.write("<a href='mailto:"+email+"'>"+text+"</a>");
}

