/**
 * @author chernov
 */
String.prototype.toCharCode = function(){
    var r = '', string = this.split('');
    for (var i in string){
        r += String.charCodeAt(string[i]) + ',';
    }
    return r.substr(0,r.length - 1);
}
function scrollUp() {
	window.scrollTo(0, 0);
}
function isString(mixed) {
	return (typeof(mixed) == 'string');
}	
function isArray(mixed) {
	return (typeof(mixed) == 'array');
}	
function isObject(mixed) {
	return (typeof(mixed) == 'object');
}	
/**
 * @author maksim.chernov
 */
function alertMessage(message, options)
{
	options = $.extend({type:"notice", addIns:""}, options);
	var type = 'notice';
	var addIns = '';
	if (options.type != 'undefined') {
		type = options.type;
	}
	if (options.addIns != 'undefined') {
		addIns = options.addIns;
	}
		
	speed = parseInt(options.speed);
	if (speed == 'NaN' || !speed) {
		speed = 0;
	}
	if (!$('#alertMessage')[0]) {
		alert(message);
	}
	else
	{
		$('#alertMessage').attr("class", type+"Message");
		$('#alertMessage').html(message);
		if (addIns != '') {
			$('#alertMessage').append("&nbsp;" + addIns);
		}
		var alertMessage;
		$('#alertMessage').fadeIn("fast", function(){
			if (speed > 0)
			{
				alertMessage = setInterval(function(){
					clearInterval(alertMessage);
					$('#alertMessage').fadeOut("slow");
				}, speed);
			}
		});
	}
	scrollUp();
}
function hideAlertMessage() {
	$('#alertMessage').hide();
}

/**
 * @author maksim.chernov
 */
var numb = '0123456789.,/';
var IE='\v'=='v';
/**
 * @author maksim.chernov
 */
function numbersOnly(myfield, e)
{
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	(key==9) || (key==13) || (key==27) )
		return true;
	else if (((numb).indexOf(keychar) > -1))
		return true;
	else
		return false;
}
/**
 * @author maksim.chernov
 */
function disallowChars(myfield, e, chars)
{
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	(key==9) || (key==13) || (key==27) )
		return true;
	else if (((chars).indexOf(keychar) > -1))
		return true;
	else
		return false;
}
/**
 * @author maksim.chernov
 */
function allowedNameRegex(myfield, e)
{
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	if ((key==92)||(key==93)||(key==32))
		return false;
	if (((key==39)||(key==45))&&myfield.value == '')
		return false;
	return allowedRegex(key, '(^[^0-9,/"/</>/._///(/)/{/}/[\|/*/&/^/%/$/#@!/?:;/+/=]+$)');	
}
/**
 * @author maksim.chernov
 */
function allowedRegex(key, pattern)
{
	keychar = String.fromCharCode(key);
	var re = new RegExp(pattern);
	if ((key == null) || (key == 0) || (key == 8) ||
	(key == 9) ||
	(key == 13) ||
	(key == 27)) 
		return true;
	else 
		if (keychar && keychar.match(re)) 
			return true;
		else 
			return false;
}
/**
 * @author maksim.chernov
 */
function allowedNameUp(myfield, e)
{
	if (myfield.value[0])
	{
		key = myfield.value[0].toCharCode();
		if ((key==39)||(key==45))
			myfield.value = '';
	}
}
/**
 * @author maksim.chernov
 */
function allowedCityRegex(myfield, e)
{
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	if ((key==92)||(key==93))
		return false;
		
	if (((key==39)||(key==45))&&myfield.value == '')
		return false;
	if (myfield.value == '')
		return allowedNameRegex(myfield, e);
	// control keys
	return allowedRegex(key, '(^[^,/"/</>/._///(/)/{/}/[\|/*/&/^/%/$/#@!/?:;/+/=]+$)');	
}
/**
 * @author maksim.chernov
 */
function allowedCityUp(myfield, e)
{
	var key;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	if (myfield.value[0])
	{
		key = myfield.value[0].toCharCode();
		if ((key==39)||(key==32)||(key==45)||(!allowedRegex(key, '(^[^0-9,/"/</>/._///(/)/{/}/[\|/*/&/^/%/$/#@!/?:;/+/=]+$)')))
			myfield.value = '';
	}
}
