String.prototype.trim = function(char) {
	if (typeof(char) == 'undefined') {
		return this.replace(/^\s+|\s+$/g, "");
	} else {
		var re = new RegExp('^['+char+']+|['+char+']+$', 'g');
		return this.replace(re, "");
	}
}


function install_form(form) {
	//	To exploit javascript field validation, fields have to have defined classes, which 
	//	define what kind of information they expect.
	//	Examples:
	//		- required: (eg. <input type="text" class="required" ...) basic class which 
	//		  which make this field required (user has to type some text in it)
	//		- required_type: this is custom reg. exp validator. There should be one global 
	//		  variable (object) REG_EXP_CHECKS defined which holds information about validation
	//		  types, their reg. exp. checks and warning text if check faild
	//			var REG_EXP_CHECKS = {
	//				email:{
	//					regExp:/^[0-9a-z_]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$/i,
	//					regExpWrn:'Please enter valid e-mail address!'
	//				},
	//				time:{
	//					regExp:/^[0-9]{1,2}[\s]*[:][\s]*[0-9]{1,2}$/,
	//					regExpWrn:'Please enter time in format HH:MM!'	
	//				}
	//			};
	//			With above array present form would automaticly validate fields w/ class 'required_email' 
	//			and 'required_time' as emails and time  	 
	//	How it works: each field w/ class that begins 'required_' gets validate according to variable REG_EXP_CHECKS,
	//	if check faild, input parrent div gets appended new division w/ class 'form_error' and title according to the
	//	type of check (defined by REG_EXP_CHECKS). 
	//		

	if (typeof(form)=='string') {
		form = $('#'+form);
	}
	
	//
	//	uncomment bottom line to validate field on blur
	//
	$('input', form).focus(function() {
		$($(this).parents('div').get(0)).addClass('selected');
	});
	$('input', form).blur(function() {
		$($(this).parents('div').get(0)).removeClass('selected');
	});
	
	// installing input focus handlers
	$('input[class^="required"]', form).focus(function() {
		add_field_error(this);
	});

	$('select.required', form).focus(function() {
		add_field_error(this);
	});

	$('textarea[class^="required"]', form).focus(function() {
		add_field_error(this);
	});
	
	// install tooltips
	$('.form_error', form).Tooltip();
	$('.form_help', form).Tooltip({
		delay: 0,
  		track: false,
		event: 'mouseover',
		extraClass: 'help_screen'
	});
	
	//
	//	form submit action triggers validation, if any field should fail form will not be submitted.
	//
	
	$(form).submit(function () {
		$('div.form_error').remove();
		
		var form_ok = true;
		$('*[class^="required"]', this).each(function() {
			if (!validate_field.call(this)) {
				form_ok = false;
			}
		
		});

/*
		$('select.required', this).each(function() {
			if (!validate_field.call(this)) {
				form_ok = false;
			}
		});
*/
		$('.form_error', form).Tooltip();
        //var position = p.position();
		return form_ok;
	});
	
	// installing calendar widget
	/*$('input.required_date, input.date', form).calendar({
		dateFormat:DEFAULT_DATE_FORMAT,
		autoPopUp: 'button', 
		buttonImageOnly: true,
		buttonImage: 'images/icons/calendar.gif', buttonText: 'calendar',
		speed:''	
	});*/
	
	// validating time key inputs 
	$('input.required_time', form).keydown(function(e) {
		if ( (e.keyCode>=48 && e.keyCode<=49) ||
			 (e.keyCode>=96 && e.keyCode<=105) ||
			  e.keyCode==190 ||
			  e.keyCode==32  ||
			  e.keyCode<65) {
			return true;		
		} else {
			return false;
		}
		
	});
}

//
//	adds error message to the field
//
function add_field_error(field, msg, title) {
	if (typeof msg != 'undefined' && msg!='') {
		var form = $($(field).parents('form')[0]);
		if ($('div.form_error').length==0) {
			$(form).before('<div class="form_error"><ul></ul></div>');
		}
		if (typeof(title)=='undefined') {
			title = $('label', $(field).parent()).html();
			title = title.trim('* '); 
		}
		
		msg = title+': '+msg;
		$('div.form_error ul').append('<li>'+msg+'</li>');
        var position = $(".form_error").offset();
        $("html").scrollTop(position.top);
        
		$(field).addClass('error');
	} else { 
		$(field).removeClass('error');
	}
}

//
//	validate field according to REG_EXP_CHECKS
//
function validate_field(full) {
	var visible = $(this).css('display')!='none';
	if (visible) {
		$(this).parents().each(function() {
			if ($(this).css('display')=='none') {
				visible = false;
				return false;
			}
		});
	}
	
	if (!visible || this.disabled) {
		return true		
	}
	
	var field_ok = true;
	var	type_ar = $(this).attr('class').match(/required_([a-zA-Z0-9_]+)/);
	if (this.type=='text') {	
		$(this).val($(this).val().trim());
	} else if (this.type=='file') {
		return true;		
	}
	
	if (typeof type_ar != 'undefined' && type_ar!=null && type_ar.length>1) {
		type=type_ar[1];
		if (typeof REG_EXP_CHECKS[type]=='undefined') {
			//alert('Reg. exp. type not defined');
		} else {
			var ret;

			if ((ret = $(this).val().match(REG_EXP_CHECKS[type].regExp))) {
				add_field_error(this);
			} else {
				add_field_error(this, REG_EXP_CHECKS[type].regExpWrn);
				field_ok = false;
			}	
		}
	} else {
		if (this.tagName=='select' || this.tagName=='SELECT') {
			if (this.selectedIndex==-1 || this.value=='' || this.value=='0' || this.value==0) {
				add_field_error(this, STRING_CONST.form_req_field);
				field_ok = false;
			} else {
				add_field_error(this);
			}
		} else if (this.type=='checkbox' || this.type=='CHECKBOX') {
			var wrapper = $(this).parents('div')[1];
			if ($('input', wrapper)[0]==this) {

                if ($('input[type=checkbox][checked]', wrapper).length==0) {
				//if ($('input[@checked]', wrapper).length==0) {
                    var label_text = $('label', wrapper).html();
					add_field_error(this, STRING_CONST.form_req_field, label_text);
					field_ok = false;
				} else {
					add_field_error(this);
					field_ok = true;
				}

			}
		} else if (this.type=='radio' || this.type=='RADIO') {
			var wrapper = $(this).parents('div')[1];
			if ($('input', wrapper)[0]==this) {

                if ($('input[type=radio][checked]', wrapper).length==0) {
				//if ($('input[@checked]', wrapper).length==0) {
                    var label_text = $('label', wrapper).html();
					add_field_error(this, STRING_CONST.form_req_field, label_text);
					field_ok = false;
				} else {
					add_field_error(this);
					field_ok = true;
				}

			}

		} else {
			if ($(this).val()=='') {
				add_field_error(this, STRING_CONST.form_req_field);
				field_ok = false;
			} else {
				add_field_error(this);
			}
		}
	}
	return field_ok;
}
