﻿function load_file(filename, filetype){
	 if (filetype=="js"){ //if filename is a external JavaScript file
	  var fileref=document.createElement('script')
	  fileref.setAttribute("type","text/javascript")
	  fileref.setAttribute("src", filename)
	 }
	 else if (filetype=="css"){ //if filename is an external CSS file
	  var fileref=document.createElement("link")
	  fileref.setAttribute("rel", "stylesheet")
	  fileref.setAttribute("type", "text/css")
	  fileref.setAttribute("href", filename)
	 }
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
}

function add_init(name) {
	if(is_defined(name)) {
		eval(name+"()");
	}
}

function run_function(name) {
	if(is_defined(name)) {
		return eval(name+"()");
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
// WINDOW
////////////////////////////////////////////////////////////////////////////////////////////////
function get_inner_window_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var sizes = {height:myHeight, width:myWidth};
  return sizes;
}


function get_window_scroll_xy() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
}

function open_win(address) {
	window.open(address,"_blank","toolbar=yes, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=900, height=400, top=50, left=50");
}



function is_IE6() {
      return ( document.all && (/msie 6./i).test(navigator.appVersion) && window.ActiveXObject ) ? true : false;
}

function reload_site() {
	window.location = window.location.href;
}

function is_defined(variable) {
	return eval('(typeof('+variable+') != "undefined");');
}

function show_file() {
	var pathname = location.pathname;
	var filename =
	pathname.substr(pathname.lastIndexOf("\\")+1,pathname.length);
	alert(filename);
}

function e(sth) {
	if(is_object(sth)) {
		_errp(sth);
	} else {
		_err(sth);	
	}
}

function _errp(obj) {
	for(var prop in obj) {
		$('test').innerHTML += prop + " = " +obj[prop] + "<br>";	
	}
	$('test').innerHTML += "<br>";
}

function _err(str) {
	$('test').innerHTML += str +"<br><br>";
}


function check_for_even_number(number) {
   var reminder=1;
   if ( number >= 0 ){
        reminder = number % 2;
        if ( reminder == 0){
            return true;
        }else{
            return false;
        }
   }  
}


function generate_id(){
	var newDate = new Date;
	return newDate.getTime();
}

function is_object (mixed_var){
    if (mixed_var instanceof Array) {        
		return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}


function findLinkByHref(href) {
  for (var i=0; i<document.links.length; i++) {
    if (document.links[i].href == href) return i;
  }
  return -1;
}

function changeLinkHref(id,newHref,oldHref) {
  if (document.links.length > 0) {
    if (document.getElementById) {
      document.getElementById(id).href = newHref;
    }
    else if (document.all) {
      document.all[id].href = newHref;
    }
    else {
      var index = findLinkByHref(oldHref);
      if (index > -1)
        document.links[index].href = newHref;
    }
  }
}


function limit_text(limit_field_id, limit_num, limit_count_id) {
	if($(limit_field_id)!=null && $(limit_field_id).value.length>limit_num) {
		$(limit_field_id).value = $(limit_field_id).value.substring(0, limit_num);
		alert("Maksymalna ilosc znaków to "+limit_num);
	} 
	if(limit_count_id!=null && $(limit_count_id)!=null) {
		$(limit_count_id).innerHTML = 	$(limit_field_id).value.length;
	}
}

function getHeighest(ids) {
	if(!Arrayer.is_array(ids)) {
		return 1;	
	} else {
		var nums = new Array();
		for(var  i=0; i < ids.length; i++) {
			nums.push($(ids[i]).offsetHeight);	
		}
		nums = nums.sort(sortNumber);
		var heighest = nums[nums.length-1];
		return heighest;
	}
}

function sortNumber(a,b) {
	return a - b;
}

function setEqualHeights(hei, ids) {
	if(!Arrayer.is_array(ids)) {
		return false;	
	} else {
		for(var  i=0; i < ids.length; i++) {
			$(ids[i]).style.height = hei + 'px';
		}
		return true;
	}
}
//zwraca jeden element wg nazwy klasy
function getElementByClass(the_class, tag_name) {
	   var elements = new Array();
		var elements = document.getElementsByTagName(tag_name);
		for (i=0; i<elements.length; i++) {
			if ( $(elements[i]).hasClassName(the_class) ) {
				return elements[i];
			}
	   }
	   return {};
}


// SUBMITTER
var Submitter = {};

Submitter.Submit = function (form_id, notice) {
	if(notice==null) {
		$(form_id).submit();
	} else {
		if(!confirm(notice)) {
			return false;
		} else {
			$(form_id).submit();	
		}
	}
}

Submitter.GoTo = function (url) {
	window.location.replace(url)
}

Submitter.AskAndGo = function (url, notice) {
	if(notice==null) {
		window.location.replace(url)
	} else {
		if(!confirm(notice)) {
			return false;
		} else {
			window.location.replace(url)	
		}
	}
}





// NOTICER
var Noticer = {};

Noticer.display_errors = function (array, errors_name) {
	if(array != null && array != undefined && array.length>0) {
		var str = errors_name+":\n";
		for(var i=0; i<array.length; i++) {
			str += array[i];
			str += "\n";
		}
		alert(str)
	}
}




// TEXTER
var Texter = {};
Texter.has_text = function (str) {
	if(str=="" || str== "undefined" || str == null) {
		return false;
	} else {
		var reWhiteSpace = new RegExp(/^\s+$/);
		 if (reWhiteSpace.test(str)) {
			  return false;
		 } else {
			 return true;
		}
	}
}

Texter.is_numeric =  function(str) {
   
   if(str!=null && str!=undefined) {
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	   for (i = 0; i < str.length && IsNumber == true; i++) { 
		  Char = str.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
   } else {
		return false;   
	}
}

Texter.is_mail = function (str) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(str)) {
		return false;
	} else {
		return true;	
	}
}


// ARRAYER
var Arrayer = {};

Arrayer.is_array = function (input) {
	return typeof(input)=='object'&&(input instanceof Array);
}

Arrayer.in_array = function (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;	
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}

Arrayer.is_once_in_array = function (needle, haystack) {
    var check_arr = [];
	
	for (key in haystack) {
       if (haystack[key] == needle) {
             check_arr.push("1");
           }
    }

	if(check_arr.length==1) {
    	return true;
	} else {
		return false;	
	}
}

Arrayer.is_max_once_in_array = function (needle, haystack) {
    var check_arr = [];
	
	for (key in haystack) {
       if (haystack[key] == needle) {
             check_arr.push("1");
           }
    }

	if(check_arr.length<=1) {
    	return true;
	} else {
		return false;	
	}
}

Arrayer.remove_value = function(value, arr) {
	if(value!=undefined && this.is_array(arr)) {
		var position_arr = [];
		for (var i=0; i < arr.length; i++) {
		   if (arr[i] == value) {
				 position_arr.push(i);
		   }
		}	
		for (var i=0; i<position_arr.length; i++) {
			arr.splice(position_arr[i], 1);	
		}
	}
}


Arrayer.get_array_by_first_element = function (value, arr) {
	if(value!=undefined && value!="" && this.is_array(arr)) {
		for (var i=0; i < arr.length; i++) {
		   if (arr[i][0] == value) {
				return arr[i];
		   }
		}	
	}
	
	return null;
}

//////////////////////////////////////////////////////////////////////////////////////////////
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
 	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

var flashResizer = {
    fo:null, //flash object
    ZOOM_FACTOR:0.1, // how much resizing will happen. used in zoom

    ZOOM_IN:1,
    ZOOM_OUT:-1,

    /**
     * find flash object - cross browser
     * look at http://blog.codefidelity.com/?p=14
     * @param _foid  DOM id of flash object. if no _foid is given,
     *               uses previously found flash object
     * @return       flash object. null if no object is found
     */
    findObj:function(_foid){
        if(_foid==undefined || _foid==null){
           return this.fo;
        }
        this.fo=null;
        if (window.document[_foid]) this.fo=window.document[_foid];
        else if (navigator.appName.indexOf("Microsoft Internet")==-1){
            if (document.embeds && document.embeds[_foid]) this.fo = document.embeds[_foid];
        }
        else this.fo = document.getElementById(_foid);

        return this.fo;
    },

    /**
     * resize flash to specified size
     * @param _w: new width
     * @param _h: new height
     * @param _foid: flash object id. if no _foid is given,
     *               uses previously found flash object.
     * @return this object
     */
    resize:function(_w, _h, _foid){
        if(this.findObj(_foid)){
            this.fo.width = _w;
            this.fo.height = _h;
        }
        return this;
    },

    /**
    * @param _d: zoom direction. flashResize.ZOOM_IN or 1 to zoom in,
    *            flashResize.ZOOM_OUT or-1 to zoom out.
    *            factor size also changes the zoom factor
    * @param _foid: flash object id. if no _foid is given,
    *               uses previously found flash object.
    * @return  this object
    */
    zoom:function(_d, _foid){
        if(this.findObj(_foid)){
            var zf = 1 + _d * this.ZOOM_FACTOR;
            this.resize(this.fo.width*zf, this.fo.height*zf);
        }
        return this;
    },

    /**
     * @param _foid: flash id. if no _foid is given, uses previously found flash object.
     */
    zoomin:function(_foid){
        return this.zoom(this.ZOOM_IN, _foid);
    },

    /**
     * @param _foid: flash id. if no _foid is given, uses previously found flash object.
     */
    zoomout:function(_foid){
        return this.zoom(this.ZOOM_OUT, _foid);
    }
}
