﻿var Mian = {}
Mian.Lib = {}
//Dictionary Class
//=========================================================================
Mian.Lib.Dictionary = function(){ this.InnerArray = new Array();}
Mian.Lib.Dictionary.COLUMN_KEY = 0;
Mian.Lib.Dictionary.COLUMN_VALUE = 1;
Mian.Lib.Dictionary.prototype.Length = function(){ return this.InnerArray.length; }
Mian.Lib.Dictionary.prototype.Add = function(key, value){ this.InnerArray.push([key, value]); }
Mian.Lib.Dictionary.prototype.Clear = function(){ this.InnerArray.splice(0, this.InnerArray.length); }
Mian.Lib.Dictionary.prototype.GetKeyByIndex = function(index){ return this.InnerArray[index][Mian.Lib.Dictionary.COLUMN_KEY]; }
Mian.Lib.Dictionary.prototype.GetValueByIndex = function(index){ return this.InnerArray[index][Mian.Lib.Dictionary.COLUMN_VALUE]; }
Mian.Lib.Dictionary.prototype.GetValueByKey = function(key){
    var index = this.GetIndexByKey(key);
    if(index != -1) return this.GetValueByIndex(index);
    return null;
}
Mian.Lib.Dictionary.prototype.GetIndexByKey = function(key){ return this.FindRowIndex(Mian.Lib.Dictionary.COLUMN_KEY, key); }
Mian.Lib.Dictionary.prototype.GetIndexByValue = function(value){ return this.FindRowIndex(Mian.Lib.Dictionary.COLUMN_VALUE, value); }
Mian.Lib.Dictionary.prototype.ContainsKey = function(key){ return this.GetIndexByKey(key) != -1; }
Mian.Lib.Dictionary.prototype.ContainsValue = function(value){return this.GetIndexByValue(value) != -1;}
Mian.Lib.Dictionary.prototype.FindRowIndex = function(columnNo, value) {
    for(var index in this.InnerArray) 
    if(this.InnerArray[index][columnNo] == value) return index;
    return -1;
}
Mian.Lib.Dictionary.prototype.GetValues = function(){
    var values = new Array();
    for(var index in this.InnerArray) values.push(this.GetValueByIndex(index));
    return values;
}
//String Functions
//=========================================================================
Mian.Lib.DeserializeFromJSON = function(jsonString){
    if(jsonString ==null || jsonString == '') return null;
    eval("var object = " + jsonString);
    return object;
}
Mian.Lib.GetLocalNumberString = function(number){
    var numberString = new Number(number).toLocaleString();
    return numberString.substr(0, numberString.length - 3);
}
Mian.Lib.ParseToInt = function(str){
    if(str==null || str =='') return '';
    var onlyDigits = Mian.Lib.String.Replace(str,'[^+-0123456789]','')
    return parseInt(onlyDigits);
}
Mian.Lib.String = {}
Mian.Lib.String.Replace = function(text, findString, replaceString){
    var regEx = new RegExp(findString, "g");
    return text.replace(regEx, replaceString);
}
//not tested
//function replace_string(text, findString, replaceString){
//    var findStringLength =  findString.length;
//    for (var replaceIndex = text.indexOf(findString); replaceIndex != -1; replaceIndex = text.indexOf(findString)){ 
//            text = text.substr(0, replaceIndex) + replaceString + text.substr(replaceIndex + findStringLength);
//    }
//    return text;
//}
//RequestXMLHttp Class
//=========================================================================
Mian.Lib.HttpRequest = function(){}
Mian.Lib.HttpRequest.prototype.CreateRequest = function(){
    if (window.XMLHttpRequest) {
        this.Request = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) {
        this.Request = new ActiveXObject('Msxml2.XMLHTTP');
        if(!this.Request) this.Request = new ActiveXObject("Microsoft.XMLHTTP");
    }    
}
Mian.Lib.HttpRequest.prototype.IsLoading = function(){
    return this.Request != null;
}
Mian.Lib.HttpRequest.prototype.Send = function(method, url, callback, parameters){
    if(this.Request) this.Request.abort();
    this.CreateRequest();
    this.Request.onreadystatechange = callback;
    this.Request.open(method, url, true);
    this.Request.send(null);
}
Mian.Lib.HttpRequest.prototype.Receive = function(callbacks, options){
    if(this.Request){
        if (this.Request.readyState == 4){
            if (this.Request.status == 200){
                if(callbacks && callbacks.success!=null){ 
                    callbacks.success(options && options.isJSON ? Mian.Lib.DeserializeFromJSON(this.Request.responseText) : this.Request.responseText);
                }
            } else {
                if(callbacks && callbacks.error != null) callbacks.error();
            }
            this.Request = null;
        }
    }
}
Mian.Lib.HttpRequest.prototype.Get = function(url, callbacks) {
    var thisObject = this;
    this.Send("GET", url, function(){ thisObject.Receive(callbacks, {isJSON : false}); });
}
Mian.Lib.HttpRequest.prototype.GetJSON = function(url, callbacks) {
    var thisObject = this;
    this.Send("GET", url, function(){ thisObject.Receive(callbacks, {isJSON : true}); });
}
//Event Manager Class
//=========================================================================
Mian.Lib.EventManager = function(){
    this.Objects = new Mian.Lib.Dictionary();
}
Mian.Lib.EventManager.prototype.GetEventHandlers = function(eventName, targetObject){
    var objectEventHandlers = this.Objects.GetValueByKey(targetObject);
    if(objectEventHandlers == null) return null;
    return objectEventHandlers.GetValueByKey(eventName);
}
Mian.Lib.EventManager.prototype.GetOrCreateEventHandlers = function(eventName, targetObject){
    if(!this.Objects.ContainsKey(targetObject)) this.Objects.Add(targetObject, new Mian.Lib.Dictionary());
    var objectEventHandlers = this.Objects.GetValueByKey(targetObject);
    if(!objectEventHandlers.ContainsKey(eventName)) objectEventHandlers.Add(eventName, new Array());
    return objectEventHandlers.GetValueByKey(eventName);
}
Mian.Lib.EventManager.prototype.AddEventHandler = function(eventName, targetObject, handler){
    var eventHandlers = this.GetOrCreateEventHandlers(eventName, targetObject);
    eventHandlers.push(handler);
}
Mian.Lib.EventManager.prototype.RemoveEventHandler = function(eventName, targetObject, handler, isRemoveAll){
    var eventHandlers = this.GetEventHandlers(eventName, targetObject);
    for(var index in eventHandlers){ 
        if(handler == eventHandlers[index]){
            eventHandlers.slice(index, index);
            if(isRemoveAll != true) return;
        }
    }
}
Mian.Lib.EventManager.prototype.RaiseEvent = function(eventName, targetObject){
    var eventHandlers = this.GetEventHandlers(eventName, targetObject);
    for(var index in eventHandlers) eventHandlers[index]();
}
//static
Mian.Lib.EventManager.Instance = new Mian.Lib.EventManager();

function OnClickPrice(rur,usd,meas,spID,link)
{
        var cur = '';
        sp = document.getElementById(spID);    
        if(sp.getAttribute('cur') == usd){
            sp.setAttribute('cur',rur);
            link.innerHTML = '&rarr;дол';   
        } else {
            sp.setAttribute('cur',usd);
            link.innerHTML = '&rarr;руб';
        }
        sp.innerHTML = sp.getAttribute('cur') + meas;
} 
