var alertDialog, confirmDialog;

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);
}

function __hideLoader(name) {
	var d = document.getElementById(name);
	if (d) {
		d.innerHTML = '&nbsp;';
	}
}

function intval( mixed_var, base ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: stensi
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// *	 example 1: intval('Kevin van Zonneveld');
	// *	 returns 1: 0
	// *	 example 2: intval(4.2);
	// *	 returns 2: 4
	// *	 example 3: intval(42, 8);
	// *	 returns 3: 42
	// *	 example 4: intval('09');
	// *	 returns 4: 9

	var tmp;

	var type = typeof( mixed_var );

	if(type == 'boolean'){
		if (mixed_var == true) {
			return 1;
		} else {
			return 0;
		}
	} else if(type == 'string'){
		tmp = parseInt(mixed_var * 1);
		if(isNaN(tmp) || !isFinite(tmp)){
			return 0;
		} else{
			return tmp.toString(base || 10);
		}
	} else if(type == 'number' && isFinite(mixed_var) ){
		return Math.floor(mixed_var);
	} else{
		return 0;
	}
}

function showAlertDialog(t) {
	if (alertDialog) {
		return;
	}

	var handleYes = function() {
		alertDialog.hide();
		alertDialog = null;
	};

	var alertPanel = document.createElement('div');
	alertPanel.setAttribute("id", "alertDialog");
	alertPanel.setAttribute("class", "yui-picker-panel");
	alertPanel.innerHTML = "<div class=\"hd\">Ostrzeżenie</div><div class=\"bd\" style=\"padding:5px 4px;\"><table height=50 width=100%><tr><td align='center' valign='middle'>" + t + "</td></tr></table></div></div><div class=\"ft\"></div></div>";
	document.body.appendChild(alertPanel);

	alertDialog = new YAHOO.widget.SimpleDialog("alertDialog", {
		width: "350px",
		fixedcenter: true,
		visible: true,
		draggable: true,
		modal: true,
		close: true,
		constraintoviewport: true,
		buttons: [ { text:"Zamknij", handler:handleYes, isDefault:true } ]
	});
	alertDialog.setBody("<table height=50 width=100%><tr><td align='center' valign='middle'>" + t + "</td></tr></table>");
	alertDialog.render();
	alertDialog.show;
}

function showConfirmDialog(t, no_action) {
	if (confirmDialog) {
		return;
	}

	var handleYes = function() {
		confirmDialog.hide();
		confirmDialog = null;
		window.location = no_action;
	};

	var handleNo = function() {
		confirmDialog.hide();
		confirmDialog = null;
	};

	var confirmPanel = document.createElement('div');
	confirmPanel.setAttribute("id", "confirmDialog");
	confirmPanel.setAttribute("class", "yui-picker-panel");
	confirmPanel.innerHTML = "<div class=\"hd\">Czy jestes pewien?</div><div class=\"bd\" style=\"padding:5px 4px;\"><table height=50 width=100%><tr><td align='center' valign='middle'>" + t + "</td></tr></table></div></div><div class=\"ft\"></div></div>";
	document.body.appendChild(confirmPanel);

	confirmDialog = new YAHOO.widget.SimpleDialog("confirmDialog", {
		width: "350px",
		fixedcenter: true,
		visible: true,
		draggable: true,
		modal: true,
		close: true,
		constraintoviewport: true,
		buttons: [ { text:"Tak", handler:handleYes, isDefault:true }, { text:"Nie", handler:handleNo } ]
	});
	confirmDialog.setBody("<table height=50 width=100%><tr><td align='center' valign='middle'>" + t + "</td></tr></table>");
	confirmDialog.render();
	confirmDialog.show;
}

function showSystemMsg(team, field, btnField) {
	var reqMsg = '/datafeed.php?ctx=message&action=load&team=' + team;

	YAHOO.util.Connect.asyncRequest(
		'GET', reqMsg, {
			success: function (oResponse) {
				// Process the JSON data returned from the server
				var res;
				active = null;
				try {
					res = YAHOO.lang.JSON.parse(oResponse.responseText);
				}
				catch (e) {
					return;
				}
				if (res.Database['totalRecords'] == 1) {
					if (res.Database['items']['code'] == 1) {
						// show button;
						document.getElementById(btnField).style.display = 'none';
						document.getElementById(field + 'ok').style.display = 'block';
						document.getElementById(field + 'ok').innerHTML = res.Database['items']['msg'];
						document.getElementById(field + 'warn').style.display = 'none';
					} else {
						// hide button
						if (res.Database['items']['code'] == 2) {
							document.getElementById(btnField).style.display = 'block';
						} else {
							document.getElementById(btnField).style.display = 'none';
						}
						document.getElementById(field + 'ok').style.display = 'none';
						document.getElementById(field + 'warn').style.display = 'block';
						document.getElementById(field + 'warn').innerHTML = res.Database['items']['msg'];
					}
				}
			},
			failure: function (oResponse) {
			},
			scope:this,
			timeout: 10000
		}
	);
}

function wordwrap( str, int_width, str_break, cut ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Nick Callen
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // *     example 1: wordwrap('Kevin van Zonneveld', 6, '|', true);
    // *     returns 1: 'Kevin |van |Zonnev|eld'
    // *     example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
    // *     returns 2: 'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
    // *     example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
    // *     returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'
 
    // PHP Defaults
    var m = ((arguments.length >= 2) ? arguments[1] : 75   );
    var b = ((arguments.length >= 3) ? arguments[2] : "\n" );
    var c = ((arguments.length >= 4) ? arguments[3] : false);
 
    var i, j, l, s, r;
 
    str += '';
 
    if (m < 1) {
        return str;
    }

    for (i = -1, l = (r = str.split("\n")).length; ++i < l; r[i] += s) {
        for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
            j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
        }
    }
 
    return r.join("\n");
}

function floatval(mixed_var) {
	return (parseFloat(mixed_var) || 0);
}