/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		string = string.replace(/\//g,"%2F");
		string = string.replace(/\\/g,"%5C");
		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;
	}
}

function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}


//
// getStyle()
// Gets current style of element el and style styleProp
//
function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}


//
// showHide()
// Toggles element 'elemId'
//
function showHide(elemId, typ){

	var state;
	var obj = document.getElementById(elemId);

	if (obj) {
		if (getStyle(elemId, 'display') == 'none') {
			obj.style.display = typ
		} else {
			obj.style.display = 'none'
		}
	}
}



//
// showHideAll()
// Toggles all elements of declared class name
//
function showHideAll(className, attrValue){

	var objs = getElementsByClass(className);

	for (var S = 0; S < objs.length; S++) {

		if (objs[S]) {
			objs[S].style.display = attrValue
		}
	}
}



//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
function changecss(theClass,element,value) {

	 var cssRules;
	 var added = false;

	 for (var S = 0; S < document.styleSheets.length; S++) {

       if (document.styleSheets[S]['rules']) {

 	     cssRules = 'rules';

	   } else if (document.styleSheets[S]['cssRules']) {

	     cssRules = 'cssRules';

	   } else {

	     //no rules found... browser unknown
	   }

	   for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {

	     if (document.styleSheets[S][cssRules][R].selectorText == theClass) {

	       if(document.styleSheets[S][cssRules][R].style[element]) {

	         document.styleSheets[S][cssRules][R].style[element] = value;
	         added = true;
		     break;
	       }
	     }
	   }

	   if(!added) {

	     if(document.styleSheets[S].insertRule) {

			document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);

		 } else if (document.styleSheets[S].addRule) {

			document.styleSheets[S].addRule(theClass,element+': '+value+';');
	     }
	   }
	 }
}


function listaorgSubmit(recno,avfg) {

	var dmaila       = Url.encode(document.getElementById('dmaila'+recno).value);
	var dwplaty      = Url.encode(document.getElementById('dwplaty'+recno).value);
	var kwota        = Url.encode(document.getElementById('kwota'+recno).value);
	var typ_wplaty   = Url.encode(document.getElementById('typ_wplaty'+recno).value);
	var roomtype_id  = Url.encode(document.getElementById('roomtype_id'+recno).value);
	var niedziela_ma = Url.encode(document.getElementById('niedziela_ma'+recno).checked ? 't' : 'f');
	var organizator  = Url.encode(document.getElementById('organizator'+recno).checked ? 't' : 'f');
	var vip          = Url.encode(document.getElementById('vip'+recno).checked ? 't' : 'f');
	var potwierdzony = Url.encode(document.getElementById('potwierdzony'+recno).checked ? 't' : 'f');
	var person_id    = Url.encode(document.getElementById('person_id'+recno).value);

	sourceURL = 'dmaila='+dmaila+'&dwplaty='+dwplaty+'&kwota='+kwota+'&typ_wplaty='+typ_wplaty+'&roomtype_id='+roomtype_id+'&niedziela_ma='+niedziela_ma+'&organizator='+organizator+'&vip='+vip+'&potwierdzony='+potwierdzony+'&person_id='+person_id+'&avfg='+avfg;
	location.href = 'listaorg,save,' + sourceURL;
}


function fakturySubmit(recno,av) {

	var typdok  = Url.encode(document.getElementById('typdok'+recno).value);
	var nrdok   = Url.encode(document.getElementById('nrdok'+recno).value);
	var wysylka = Url.encode(document.getElementById('wysylka'+recno).value);
	var id      = document.getElementById('id'+recno).value;

	sourceURL = 'typdok='+typdok+'&nrdok='+nrdok+'&wysylka='+wysylka+'&id='+id+'&av='+av;
	location.href = 'faktury,save,' + sourceURL;
}


function welcomeSubmit(recno,avf) {

	var dowod       = Url.encode(document.getElementById('dowod'+recno).value);
	var numerdow    = Url.encode(document.getElementById('numerdow'+recno).value);
	var meldunek    = Url.encode(document.getElementById('meldunek'+recno).value);
	var obecny      = Url.encode(document.getElementById('obecny'+recno).checked ? 't' : 'f');
	var akredytacja = Url.encode(document.getElementById('akredytacja'+recno).value);
	var person_id   = Url.encode(document.getElementById('person_id'+recno).value);

	sourceURL = 'dowod='+dowod+'&numerdow='+numerdow+'&meldunek='+meldunek+'&obecny='+obecny+'&akredytacja='+akredytacja+'&person_id='+person_id+'&avf='+avf;
	location.href = 'welcome,save,' + sourceURL;
}


function proformaIlosc() {

	var ilosc = document.getElementById('ilosc');
	var email = document.getElementById('email');

	ilosc.disabled = (email.value == '')
}

function newsCzasNow() {

	var data = document.getElementById('data');
	var hdata = document.getElementById('hdata');
	var now = document.getElementById('now');

	if (now.checked) {
		hdata.value = data.value;
		data.value = '';
		data.disabled = true;
	} else {
		data.value = hdata.value;
		data.disabled = false;
	}
}

