/* apsolva.js - Helper JavaScript functions for various things
                Gleaned over the years
*/

/*  trim
    Remove characters on the left, right or both sides of the string.
    String.prototype.trim([chars: String = " "], [type: Integer = 0]): String
      chars - set of characters that will be removed
      type - specifies where the trim will occur, possible values are:
        * 0 = remove on both sides
        * 1 = remove characters on the left
        *	2 = remove characters on the right
*/
String.prototype.trim = function(c, t)
{
    return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+",
    this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
}

/*  pad
   	Returns the string with a substring padded on the left, right or both sides.
    String.pad(length: Integer, [substring: String = " "], [type: Integer = 0]): String
      length - amount of characters that the string must have
      substring - string that will be concatenated
      type - specifies the side where the concatenation will happen, where:
        * 0 = left
        * 1 = right
        * 2 = both sides (centres text!)
*/
String.prototype.pad = function(l, s, t)
{
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
        + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
}

/*  isPositiveInt
    Returns true if the number is an integer and positive, false if not.
*/
function isPositiveInt(s)
{
  if (s == "") return true;
  for (i = 0 ; i < s.length ; i++){
    if ((s.charAt(i) < '0') || (s.charAt(i) > '9')) return false
  }
  return true;
}

// Cookie functions
function createCookie(name, value, days)
{
	if (days){
  	var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else{
    var expires = "";
  }

	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name, "", -1);
}

// Don't display an object
function apsolvaHide(anObject)
{
  $(anObject).setStyle({display:"none"});
}
// Display an object
function apsolvaShow(anObject, blockInline)
{
  $(anObject).setStyle({display:blockInline});
}

// Validate an email address
function validEmail(address)
{
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

  return(reg.test(address));
}

/* Searchn an array and returns and array of found items
var test=[1,58,'blue','baby','boy','cat',35,'35',18,18,104]
result1=test.find(35);    //returns 6
result2=test.find(/^b/i); //returns 2,3,4
result3=test.find('35');  //returns 7
result4=test.find(18);    // returns 8,9
result5=test.find('zebra'); //returns false
*/
/*
Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}
*/
