var newWindow = null;

function openWindow (Url, Title, Width, Height) {
	var win_opt = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0";
	win_opt += ",width=" + Width + ",height=" + Height;

	if (newWindow && !newWindow.closed) {
		newWindow.close();
	}

	newWindow = window.open(Url, Title, win_opt);

	if (newWindow != null) {
		if (newWindow.opener == null) {
			newWindow.opener = self;
		}
		if (newWindow.focus) newWindow.focus();
	}
}

function toggleCheckbox(id) {
	elt = document.getElementById(id);
	elt.checked = ! elt.checked;
}


function setCheckboxes(name, val) {

	var elts = document.getElementsByName(name);
	var elts_cnt = (typeof(elts.length) != 'undefined') ? elts.length : 0;

	if (elts_cnt) {
		for (var i = 0; i < elts_cnt; i++) {
			elts[i].checked = val;
		} // end for
	} else {
		elts.checked = val;
	} // end if... else

} // end of the 'setCheckboxes()' function

function getElementsByClassName(tagname, classname) {

	var all_tags = document.getElementsByTagName(tagname);
    var retvalue = new Array();
    var i, j;
 
    for (i = 0, j = 0; i < all_tags.length; i++) {
        var c = " " + all_tags[i].className + " ";
        if (c.indexOf(" " + classname + " ") != -1)
            retvalue[j++] = all_tags[i];
    }

	return retvalue;

}


// Hide and show div blocks
// -------------------------

function showAllBlocks(img_dir) {

	var blocks = getElementsByClassName('div', 'block');
    for (i = 0; i<blocks.length; i++) {
		showBlock (blocks[i].id, img_dir);
	}
}


function hideAllBlocks(img_dir) {

	var blocks = getElementsByClassName('div', 'block');
    for (i = 0; i<blocks.length; i++) {
		hideBlock (blocks[i].id, img_dir);
	}
}

function showBlock (id, img_dir){
	if (document.layers) {
		document.layers[id].visibility="show";
	} else {
		s = document.getElementById(id).style;
		s.visibility="visible";
		s.display="block";
	}
	if (document.images['img_' + id]) {
		document.images['img_' + id].src = img_dir+"/hide_block.gif";
	}
} 


function hideBlock (id, img_dir){
	if (document.layers) {
		document.layers[id].visibility="hide";
	} else {
		s = document.getElementById(id).style;
		s.visibility="hidden";
		s.display="none";
	}
	if (document.images['img_' + id]) {
		document.images['img_' + id].src = img_dir+"/show_block.gif";
	}
} 


function toggleVisibility(id, img_dir){
	if (document.layers) {
		v = document.layers[id].visibility;
		if (v=="hide") {
			showBlock(id, img_dir);
		} else {
			hideBlock(id, img_dir);
		}
	} else {
		v = document.getElementById(id).style.visibility;
		if (v=="hidden") {
			showBlock(id, img_dir);
		} else {
			hideBlock(id, img_dir);
		}
	}
} 


// Some PHP equivalent functions
// -----------------------------

function print_r(theObj){
	if(theObj.constructor == Array || theObj.constructor == Object) {
		document.write("<ul>")
		for(var p in theObj){
			if(theObj[p].constructor == Array || theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>")
	}
}

function ucFirst(str) {
   return str.substr(0,1).toUpperCase()+str.substr(1)
}

function is_integer (str){
	var i = parseInt (str);
	if (isNaN (i)) {
		return false;
	}

	i = i.toString ();
	if (i != str) {
		return false;
	}
	return true;
}