function lead0(val) {
        return (val < 10) ? "0" + val.toString() : val;
}

function random_imglink(){
        var y = Math.round(Math.random()*14)
        if (y == 0)
                y=1
        document.write('<img src="../images/dosug/photo'+y+'.jpg" border="0">')
}

function show_hide(id) {
        var show_hide_text = document.getElementById('k' + id);
        var show_hide_pic = document.getElementById('pic' + id);

        if (show_hide_text.style.display == 'none') {
                show_hide_text.style.display = 'block';
                show_hide_pic.src = 'pic/minus.gif';
        }
        else {
                show_hide_text.style.display = 'none';
                show_hide_pic.src = 'pic/plus.gif';
        }
}

function check(field) {
        if (checkflag == "false") {
                for (i = 0; i < field.length; i++) {
                        field[i].checked = true;
				}
                        checkflag = "true";
        }
        else {
                for (i = 0; i < field.length; i++) {
                        field[i].checked = false;
				}
                        checkflag = "false";
        }
}

function imgFit (img, maxImgWidth)
{
   if (typeof img.naturalWidth == 'undefined') {
      img.naturalHeight = img.height;
      img.naturalWidth = img.width;
   }
   if (img.width > maxImgWidth) {
      img.height = Math.round(((maxImgWidth)/img.width)*img.height);
      img.width = maxImgWidth;
      img.title = 'Click image to view full size';
      img.style.cursor = 'move';
   } else if (img.width == maxImgWidth && img.width < img.naturalWidth) {
      img.height = img.naturalHeight;
      img.width = img.naturalWidth;
      img.title = 'Click to fit in the browser window';
   }
}

function printf () {
    var num = arguments.length;
    var oStr = arguments[0];

    for (var i = 1; i < num; i++) {
        var pattern = "\\{" + (i-1) + "\\}";
        var re = new RegExp(pattern, "g");
        oStr = oStr.replace(re, arguments[i]);
    }
    return oStr;
}

function return_field_val ( field_val ) {
	return field_val.replace(/(^\s+)|(\s+$)/g, "");
}

var clock = {
	clocks: new Array(),
	re: new RegExp("(\\d{4})-(\\d{2})-(\\d{2})\\s(\\d{2}):(\\d{2}):(\\d{2})"),
	format: "H:i:s",

	add_clock: function(date, target) {
		/*
		str date - in format "YYYY-MM-DD hh:mm:ss";
		str target - some html el. with innetHTML property!!!
		*/
		var da = date.match(this.re);
		var t = document.getElementById(target);
		var d = new Date(
		        parseInt(da[1]),
                        parseInt(da[2]),
                        parseInt(da[3]),
                        parseInt(da[4]),
                        parseInt(da[5]),
                        parseInt(da[6]));

                        var nd = new Array(d,t);
                        this.clocks.push(nd);
                },

                tik_tak: function() {
                	var i = 0;
                	while ( i < this.clocks.length ) {
                		this.clocks[i][1].innerHTML=this.clocks[i][0].getHours() + ':'+ ( this.clocks[i][0].getMinutes()<10?'0'+this.clocks[i][0].getMinutes():this.clocks[i][0].getMinutes()) + ':'+ ( this.clocks[i][0].getSeconds()<10?'0'+this.clocks[i][0].getSeconds():this.clocks[i][0].getSeconds());
                		this.clocks[i][0].setSeconds(this.clocks[i][0].getSeconds()+1);
                		i++;
                }
                setTimeout('clock.tik_tak()',1000);
        }

}