
if(!window.__rageLib){
  window.__rageLib = {};
  window.__rageLib.instances = {};
}

__rageLib.helper = function(){}

__rageLib.helper.prototype = {
  actCorrectness: [],
  orygStyles: [],
  trim: function(str){
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  },
  getOnlyNumbersString: function(str, maxLength){
    var v2 = '';
    for(var i=0; i<str.length; i++){
      if(str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57){
  	    v2 += ''+str.charAt(i);
        if(maxLength && v2.length == maxLength) 
          break;
  	  }
    }
    return v2;
  },
  isOnlyNumbersString: function(str){
    for(var i=0; i<str.length; i++){
      if(str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57){}
      else return false;
    }
    return true;
  },
  updateFieldCorrectnessDisplay: function(elem, correct, marker){
    if(marker == null || !marker) marker = 'invalid';
    //alert(this.actCorrectness[elem.id] != correct);
    if(!correct || (this.actCorrectness[elem.id] != correct)){
      if(correct){ //go to oryg
        this.removeClass(elem,marker);
      }else{
        this.addClass(elem,marker);
      }
      this.actCorrectness[elem.id] = correct;
    }
    if(this.actCorrectness[elem.id] == null){
      //this.orygStyles[elem.id] = new Array();
      //this.orygStyles[elem.id]['border'] = elem.style.border;
      this.actCorrectness[elem.id] = correct;
    }
  },
  addClass: function(elem, nc){
    if(!elem.className){ 
      elem.className = nc;
      return true;
    }
    var t = elem.className;
    t = t.split(' ');
    for(c in t){
      if(t[c] == nc) return false; 
    }
    elem.className += ' '+nc; 
    return true;
  },
  removeClass: function(elem, nc){
    if(!elem.className){
      return false;
    }
    var res = false,
        t = elem.className,
        newT = new Array();
    t = t.split(' ');
    var i = 0;
    while(c = t[i++]){
      if(c == nc){ 
        res = true;
      }else{
        newT[newT.length] = c; 
      }
    }
    if(res){
      elem.className = newT.join(' ');
    }
    return res;
  },
  isClass: function(elem, nc){
    if(!elem.className)
      return false;
    var t = elem.className;
    t = t.split(' ');
    var i = 0;
    while(c = t[i++])
      if(c == nc) 
        return true;
    return false;
  },
  switchDisplay: function(elem){
    var t= null;
    if(t = document.getElementById(elem)) elem = t;
    if(elem.style.display == 'block' || !elem.style.display){ 
      elem.style.display = 'none';
    }else{                    
      elem.style.display = 'block';
    }
  },
  calculateGeoDistance: function(lat1, lon1, lat2, lon2, radius){
    if(!radius)
      radius = 6371; //in km
      
    var dLat = (lat2-lat1)*Math.PI/180,
        dLon = (lon2-lon1)*Math.PI/180,
        a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *   
            Math.sin(dLon/2) * Math.sin(dLon/2),
        c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    return radius * c;
  }
};

var __rageHelper = new __rageLib.helper();

/*alert(__rageHelper.calculateGeoDistance(
  52.23071582077548,21.016388113711837,
  51.76058759858995,19.456329519961837));*/

