function choose_window_open(link) {
	return window.open(link, "", "toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1,width=340,height=300,left=300,top=200");
}

function openw(link, width, height) {
	if (width == null) {
		width = 640;
	}
	
	if (height == null) {
		height = 480;
	}
	
	return window.open(link, "", "toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=1,width=" + width + ",height=" + height);
}

function focus_select(obj) {
	if (obj != null) {
		
		if (obj.disabled) {
			obj.disabled = false;
		}
		
		obj.focus();
		obj.select();
	}
}

function strim(string) {
	var result = "";
	var i = 0;
	var ch;
	
	if(string.length == 0 ) {
		return result;
	}
	
	while(string.charAt(i) == ' ') {
		i++;
	}
	
	while(i < string.length && (ch = string.charAt(i)) != ' ' ) {
		result = result + ch; 
		i++;
	}
	
	return result;
}

var isNn, isIe;

if (navigator.appName == "Microsoft Internet Explorer") {
	isIe = true;
}

if (navigator.appName == "Netscape") {
	isNn = true;
}

function right(e) {
	if (isNn && (e.which == 3 || e.which == 2 )) {
		return false;
	} else if (isIe && (event.button == 2 || event.button == 3)) {
		// alert("Sorry, you do not have permission to right click on this page.");
		return false;
	}

	return true;
}

document.onmousedown = right;
document.onmouseup = right;

/**
 * Work functions
 */
 
/**
 * Browser specific variables
 */
var agt = navigator.userAgent.toLowerCase();
var is_nav = ((agt.indexOf("mozilla") != -1) && (agt.indexOf("spoofer") == -1)
	&& (agt.indexOf("opera") == -1) //&& (agt.indexOf("compatible") == -1)
	&& (agt.indexOf("webtv") == -1) && (agt.indexOf("hotjava")==-1));
var is_ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

/**
 * Cross-browser function for handling pressed key
 */
function getKey(event) {
	var key;
	if (is_ie) {
		key = window.event.keyCode;
	} else if (is_nav) {
		key =  event.which;
	} else {
		key = window.event.keyCode;
	}
	
	return key;
}

/**
 * Standart left and right trimming function (only spaces)
 */
function trim(string) {
	re = /\s*(.*)\s*/;
	re.exec(string);
	
	return RegExp.$1;
}

/**
 * Mail validation
 * returns true if e-mail valid
 */
function isValidEmail(e) {
	e = trim(e);
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	
	for(i = 0; i < e.length; i++) {
		if (ok.indexOf(e.charAt(i)) < 0){
			return false;
		}
	}

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return true;
		} else {
			return false;
		}
	}
}

/**
 * Named mail validation
 * returns true if named e-mail valid
 */
function isValidNamedEmail(e) {
	e = trim(e);
	
	lessCharcterIndex = e.indexOf("<");
	greaterCharcterIndex = e.indexOf(">");
	if (lessCharcterIndex < 0 && greaterCharcterIndex < 0) {
		return isValidEmail(e);
	} else if ( (lessCharcterIndex > 0 && e.indexOf("<", lessCharcterIndex + 1) > 0) || (greaterCharcterIndex > 0 && e.indexOf("<", greaterCharcterIndex + 1) > 0) ) {
		return false;
	} 
	
	badNames = "\";,:";
	
	for(i = 0; i < badNames.length; i++) {
		if (e.indexOf(badNames.charAt(i)) > 0) {
			return false;
		}
	}
	
	re = /<(.*)>/;
	re.exec(e);
	emailAddress = RegExp.$1;
	
	return isValidEmail(emailAddress);
}

/**
 * Checking maximum length of the field
 * Default is 15 characters
 */
function checkMaxLength(field) {
	var len = 15;
	
	if (field.value.length <= len) {
		return true;
	} else {
		alert("Field's length must be < " + len);
		field.focus();
		field.select();
		
		return false;
	}
}

/**
 * Break out of frames
 * used at onload event hander
 */
function break_frames() {
	page = self.location.href;
	if (page != top.location.href) {
		top.location.href = page;
		return true;
	}
}

/**
 * Opens centered window
 * Like showModalDialog method
 */
function openCenteredWindow(url, width, height, overlappingParameters) {
	if (url.indexOf('%') < 0) {
		url = escape(url);
	}
	
	if (/(\.(jpg|gif))/.test(url) && !/(\.php)/.test(url)) {
		url = "image.php?i=" + url;
	}

	if (width == null) {
		width = 200;
	}
	
	if (height == null) {
		height = 200;
	}
	
	var left = Math.floor((Screen.width - width) / 2);
	var top = Math.floor((Screen.height - height) / 2);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;

	overlappingParameters = (overlappingParameters != null) ? "," + overlappingParameters : "";
	winParms += "," + "scrollbars=yes,status=no,menubar=no,resizable=yes" + overlappingParameters;
	win = window.open(url, null, winParms);
	
	return win;
}

/**
 * Opens centered image
 */
function openCenteredImage(url, width, height) {
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
	
	winParms += "," + "scrollbars=no,status=no,menubar=no,resizable=no";
	win = window.open(url, null, winParms);
	
	return win;
}

/**
 * Unified error messaging function
 * @param errorMessage The message to show
 */
function showError(errorMessage) {
	errorMessage = errorMessage.replace(/<br>/gi, '\n');
	
	if (trim(errorMessage) != "") {
		alert(errorMessage);
	}
}

/**
 * Some useful value checking functions
 */
function isInteger(value) {
	return /^\d+$/.test(trim(value));
}

function isNumeric(value) {
	return /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/.test(trim(value));
}

function isDate(date) {
	re = /^([01]?\d\/)([0123]?\d)\/[12][09][09]\d$/;

	return re.test(trim(date));
}
