

//-----------------------------------------------------
// Form Check functions

// Forms fields config
var submit_form_fields = new Array();

function is_empty(s) 
{
  var r = /^\s*$/;
  return r.test(s);
}

function get_form_message(msg, title) 
{
  if(!msg) 
  {
    msg = "Please enter valid '"+title+"' value\n";
  }
  return msg+"\n";
}

function submit_form(form, todo, page, target) 
{
  if((!page) || (page == ""))
    if(action_page)
      page = action_page;
    else
      page = action_page;

  if((!target) || (target == "")) 
    target = "_top";

  var name, control, rexp, msg, title, s;

  var flds = submit_form_fields[form];
  var f    = document.forms[form];

  var error_messages = "";
  
  if(typeof(flds) == 'object')
  {
    for(var i = 0; i < flds.length - 1; i++) 
    {
      name    = flds[i][0];
      control = flds[i][1];
      rexp    = flds[i][2];
      msg     = flds[i][3];
      title   = flds[i][4];

      switch(control)
      {
        case "text":
        case "textarea":
          s = f[name].value;
  
          if(rexp != "") 
          {
            var r = new RegExp(rexp);
            if(!r.test(s)) 
              error_messages += get_form_message(msg, title);
          } 
          else 
            if(is_empty(s)) 
              error_messages += get_form_message(msg, title);

          break;
  
        case "select":
          s = f[name].selectedIndex;
          if(s == 0) 
            error_messages += get_form_message(msg, title);

          break;
      }
    }
  }

  if(error_messages == "") 
  {
    f.elements['actioncontext[page]'].value = page;
    f.elements['actioncontext[todo]'].value = todo;
    f.target = target;
    f.submit();
    return true;
  }
  else
  {
    alert(error_messages);
    return false;
  }
}
//-----------------------------------------------------


function openPopup(name, todo, page) 
{
  if(!page) 
  {
    page = action_page;
  }
  if(!todo) 
  {
    todo = name;
  }

  popup = window.open('', name, 'width=620,height=420,left=0,top=0,resizable=yes,scrollbars=yes');

  popup.document.open();
  popup.document.write('<form name="refresh_popup_form" action="' + script_url + '" method="post">');
  popup.document.write('<input type="hidden" name="actioncontext[page]" value="'+page+'">');
  popup.document.write('<input type="hidden" name="actioncontext[todo]" value="'+todo+'">');
  popup.document.write('</form>');
  popup.document.close();
  popup.document.refresh_popup_form.submit();
}


//-----------------------------------------------------
// Service Function
//-----------------------------------------------------


//-----------------------------------------------------
// System functions
//-----------------------------------------------------

//---------------------------------------------
// Math functions

Math._round = Math_round;
function Math_round(val, n)
{
  n = this.pow(10,n);
  return this.round(val * n) / n;
}


//-----------------------------------------------------
// String functions

String.prototype.repeat = String_repeat;
function String_repeat(num)
{
  var res = "";
  for(var i = 1; i <= num; i++) res += this;
  return res;
}


//-----------------------------------------------------
// Array functions

function array_length(obj)
{
  var i = 0;
  for(var element in obj) i++;
  
  return i;
}


//-----------------------------------------------------
// Data format functions
//-----------------------------------------------------

function sprintf()
{
  if (!arguments || arguments.length < 1 || !RegExp)
  {
    return;
  }
  var str = arguments[0];
  var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
  var a = b = [], numSubstitutions = 0, numMatches = 0;
  while (a = re.exec(str))
  {
    var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
    var pPrecision = a[5], pType = a[6], rightPart = a[7];
    
    //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

    numMatches++;
    if (pType == '%')
    {
      subst = '%';
    }
    else
    {
      numSubstitutions++;
      if (numSubstitutions >= arguments.length)
      {
        alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
      }
      var param = arguments[numSubstitutions];
      var pad = '';
             if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
        else if (pPad) pad = pPad;
      var justifyRight = true;
             if (pJustify && pJustify === "-") justifyRight = false;
      var minLength = -1;
             if (pMinLength) minLength = parseInt(pMinLength);
      var precision = -1;
             if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
      var subst = param;
             if (pType == 'b') subst = parseInt(param).toString(2);
        else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
        else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
        else if (pType == 'u') subst = Math.abs(param);
        else if (pType == 'f') 
        {
          subst = parseFloat(param);
          if(!isNaN(subst))
          {
            subst += "";
            if(subst.indexOf(".") != -1)
            {
              var fract_no = subst.length - subst.indexOf(".") - 1;
              if(precision >= fract_no)
                subst += "0".repeat(precision - fract_no);
              else
                subst = subst.substr(0, subst.indexOf(".") + precision + 1);
            }
            else
              subst += "." + "0".repeat(precision);
          }
        }
        else if (pType == 'o') subst = parseInt(param).toString(8);
        else if (pType == 's') subst = param;
        else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
        else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
    }
    str = leftpart + subst + rightPart;
  }
  return str;
}

function parse1(t, p)
{
  var rxp;
  for(var i in p) 
  {
    rxp = new RegExp("\\\$\\\$"+i+"\\\$\\\$", "g");
    t = t.replace(rxp, p[i]);
  }
  return t;
}

function formatPrice($val)
{
  $val = parseFloat($val);
  $val = Math._round($val, 2);
  $val = sprintf("%.2f", $val);
  return $val;
}

function wpopup(url, name, rwidth, rheight) {
	swidth=screen.width;
	sheight=screen.height;
	vleft=Math.round((swidth - rwidth) / 2);
	vtop=Math.round((sheight - rheight) / 2);
	window.open(url, name, 'width='+rwidth+',height='+rheight+',left='+vleft+',top='+vtop);
	return false;
}

function wscrollpopup(url, name, rwidth, rheight) {
	swidth=screen.width;
	sheight=screen.height;
	vleft=Math.round((swidth - rwidth) / 2);
	vtop=Math.round((sheight - rheight) / 2);
	window.open(url, name, 'width='+rwidth+',height='+rheight+',left='+vleft+',top='+vtop+',scrollbars=yes');
	return false;
}
