/**
 * @package DeityFramework
 * @version 1.5.0 2007-09-17
 * @copyright George Popov
 * @author George Popov <nodude at gmail dot com>
 */
var form = {
	highlights:function(form_id){
		form_obj = document.getElementById(form_id);
	
		for(var p=0; p<form_obj.length; p++){
			if(
			form_obj[p].type=="submit" ||
			form_obj[p].type=="reset" ||
			form_obj[p].type=="text" || 
			form_obj[p].type=="password" || 
			form_obj[p].type=="file" || 
			form_obj[p].type=="textarea" || 
			form_obj[p].type=="select-one" || 
			form_obj[p].type=="select-multiple" ||
			form_obj[p].type=="radio" || 
			form_obj[p].type=="checkbox"
			){/*check for valid form element*/
				util.add_event(form_obj[p], 'focus', this.field_highlight);
				util.add_event(form_obj[p], 'blur', this.field_unhighlight);
			}
		}
	},
	
	field_highlight:function(){
		util.add_class(this, 'focused');
	},
	
	field_unhighlight:function(){
		util.remove_class(this, 'focused');
	},

	"validationJson":{},//temporary placeholder, while validation or binding is taking place

	//Binding methods
	bind:function(form_id, validationJson){
		this.validationJson[form_id] = validationJson;
		validationJson = null;
	
		for(var field_name in this.validationJson[form_id].fields){
			var field_object = document.getElementById(field_name);
									
			if(field_object && typeof(field_object)=='object'){
				field_object.hint_message = '';//the message that appears on focus
				util.add_event(field_object, 'focus', this.show_hint);
				util.add_event(field_object, 'blur', this.hide_tooltip);
			
				for(validations in this.validationJson[form_id].fields[field_name]){
					var curr_child = this.validationJson[form_id].fields[field_name][validations];
					
					if(curr_child.message){
						field_object.hint_message += this.message_substitutes(curr_child.message, curr_child.name, field_object.value, curr_child.requirement)+'<br>';
					}
				}
			}			
		}
	},
	
	//Validation methods
	validate:function(form_obj){
		var form_id = form_obj.id;
		var errors_found = false;
	
		for(var field_name in this.validationJson[form_id].fields){
			var field_object = document.getElementById(field_name);
									
			if(field_object && typeof(field_object)=='object'){
				if(!this.validate_single_field(form_id, field_object))
					errors_found = true;
			}
		}
		
		errors_found ? this.validation_error() : this.validation_success(form_obj);
	},
	
	validate_single_field:function(form_id, field_object){
		this.output_message("");
	
		var fval = field_object.value;
		var fid = field_object.id;
		var parent_child = this.validationJson[form_id].fields[fid];
		
		//field is empty, but is optional - clear it
		if(!this.validate_required(form_id, fval) && parent_child.optional){
			this.clear_error(field_object);
			return true;
		}
		
		//lets give the field a clean start
		var error_stack = '';
	
		//time to go through the required validations
		for(validations in this.validationJson[form_id].fields[fid]){
			var curr_child = parent_child[validations];
			
			//there be validations in them thar hills
			if(curr_child){
				switch(validations){
					case 'required':
					case 'email':
					case 'integer':
					case 'float':
						if(!this['validate_'+validations](form_id, fval))
							error_stack += this.message_substitutes(curr_child.error, curr_child.name, field_object.value, curr_child.requirement)+'<br>';
					break;
												
					case 'custom':
					case 'minLength':
					case 'maxLength':
					case 'minValue':
					case 'maxValue':
						if(!this['validate_'+validations](fval, curr_child.requirement))
							error_stack += this.message_substitutes(curr_child.error, curr_child.name, field_object.value, curr_child.requirement)+'<br>';
					break;
				}
			}
		}
		
		//we scritinized thorugh that field. let's see what we've got here
		if(error_stack){
			this.show_error(field_object, error_stack);
			return false;
		}else{
			this.clear_error(field_object);
			return true;
		}
	},
	
	//blur methods
	hide_tooltip:function(){
		form.hide_message(this.id);
	},	
	
	hide_message:function(oid){
		var tooltip = document.getElementById(oid+'_msg');
		if(tooltip)	tooltip.style.display = 'none';
	},
	
	//display methods
	show_hint:function(){
		form.show_message(this.id, this.hint_message, 'succ');
	},
	
	validate_field:function(){
		var form_id = this.form.id;
		form.validate_single_field(form_id, this);
	},
	
	show_error:function(obj, msg){
		form.show_message(obj.id, msg, 'err');
		util.add_class(obj, 'warn');
		util.add_event(obj, 'blur', this.validate_field);
	},
	
	clear_error:function(obj){
		util.remove_class(obj, 'warn');
		util.add_event(obj, 'blur', this.hide_tooltip);
		this.hide_message(obj.id)
	},
	
	//validation helpers
	validation_success:function(form_obj){
		this.output_message("<b><img src='http://www.bulgariapropertiesmanagement.com/images/admin/waiting_small.gif' width='12' /> form is being sent</b>");
		
		document.getElementsByTagName('body')[0].style.cursor = 'wait';
		
		//we disable the form fields below, so it would be nice to sumit now
		form_obj.submit();
		
		for(var p=0; p<form_obj.length; p++){
			if(
			form_obj[p].type=="submit" ||
			form_obj[p].type=="reset" ||
			form_obj[p].type=="text" || 
			form_obj[p].type=="password" || 
			form_obj[p].type=="file" || 
			form_obj[p].type=="textarea" || 
			form_obj[p].type=="select-one" || 
			form_obj[p].type=="select-multiple" ||
			form_obj[p].type=="radio" || 
			form_obj[p].type=="checkbox"
			){/*check for valid form element*/
				form_obj[p].disabled = true;
				form_obj[p].style.cursor = 'not-allowed';
				util.add_class(form_obj[p], 'disabled');
			}
		}	
	},
	
	validation_error:function(){
		this.output_message("<span class='err'><b>form is not complete...</b></span>");
	},
	
	validate_required:function(form_id, val){
		var reg = this.validationJson[form_id].regex.empty;
		reg = new RegExp(reg);
		return !reg.test(val);
	},
	
	validate_email:function(form_id, val){
		var reg = this.validationJson[form_id].regex.email;
		reg = new RegExp(reg);
		return reg.test(val);
	},
	
	validate_integer:function(form_id, val){
		var reg = this.validationJson[form_id].regex.integer;
		reg = new RegExp(reg);
		return reg.test(val);
	},
	
	validate_float:function(form_id, val){
		var reg = this.validationJson[form_id].regex.float;
		reg = new RegExp(reg);
		return reg.test(val);
	},
	
	validate_custom:function(val, req){
		reg = new RegExp(req);
		return reg.test(val);
	},
	
	validate_minLength:function(val, req){
		return val.length>=req;
	},
	
	validate_maxLength:function(val, req){
		return val.length<=req;
	},
	
	validate_minValue:function(val, req){
		return val>=req;
	},
	
	validate_maxValue:function(val, req){
		return val<=req;
	},
	
	//general helpers
	output_message:function(msg){
		var output_container = document.getElementById("form_output");
		
		if(output_container){
			output_container.innerHTML = msg;
		}else{
			alert(msg);
		}
	},
	
	show_message:function(oid, msg, msg_class){
		var field_object = document.getElementById(oid);
		var msgTooltip = document.getElementById(oid+'_msg');
		var msgHtml = "<div><b class='rnd'></b><b></b><b></b></div><div class='msgBoxText'><span class='"+msg_class+"'>"+msg+"</span></div><div><b></b><b></b><b class='rnd'></b></div>";
		
		if(msgTooltip){//we have a tooltip, make it visible and fill in the text
			msgTooltip.innerHTML = msgHtml;
			msgTooltip.style.display = 'inline';
			
		}else{//we haven't built a message tooltip yet, make one
			var msgNode = document.createElement('div');
			msgNode.id = oid+'_msg';
			msgNode.innerHTML = msgHtml;
			msgNode.className = 'msgBox';
			field_object.offsetParent.appendChild(msgNode);
			msgTooltip = msgNode;
		}
	},
	
	message_substitutes:function(message, field_name, field_value, field_requirement){
		field_name = util.ucwords(field_name.replace('_', ' '));
		var field_length = field_value ? field_value.length : 0;

		message = message.replace('%field%', field_name);
		message = message.replace('%value%', field_value);
		message = message.replace('%length%', field_length);
		message = message.replace('%requirement%', field_requirement);
		
		return message;
	}
}
/**
 * @version 1.5.0 2007-10-03
 * @copyright George Popov
 * @author George Popov <nodude at gmail dot com>
 */
var util = {
	//misc funcs
	popup:function(url, w, h, params){
		var id = 'popupWin';
		var w = w || 900;
		var h = h || 750;
		var params = params || 'toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resizable=1';
				
		var win = window.open(url, id, 
		params+', width='+w+', height='+h+', left='+(screen.availWidth/2-w/2)+', top='+(screen.availHeight/2-h/2));
	},
	
	//css manipulation
	has_class:function(ele, cls) {
		return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	},
	
	add_class:function(ele, cls) {
		if(!this.has_class(ele,cls)) ele.className += " "+cls;
	},
	
	remove_class:function(ele, cls){
		if(this.has_class(ele, cls)){
			var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
			ele.className = ele.className.replace(reg, ' ');
		}
	},
	
	add_event:function(obj, type, fn){
		if (obj.attachEvent){
			obj['e'+type+fn] = fn;
			obj[type+fn] = function(){obj['e'+type+fn](window.event);}
			obj.attachEvent('on'+type, obj[type+fn]);
		}else{
			obj.addEventListener(type, fn, false);
		}
	},
	
	remove_event:function(obj, type, fn) {
		if (obj.detachEvent){
			obj.detachEvent('on'+type, obj[type+fn]);
			obj[type+fn] = null;
		}else{
			obj.removeEventListener(type, fn, false);
		}
	},
	
	//string manipulation
	ucwords:function(val){
		val = val.split(' ');
		newVal = '';
		for(var i = 0; i<val.length; i++) {
			newVal += val[i].substring(0, 1).toUpperCase() + val[i].substring(1, val[i].length) + ' ';
		}
		return newVal;
	}
}

/*ADD TOOLTIPS TO a ELEMENTS WITH title*/
/* create a dynamic layer and attach it to the HTML document. */
function init_tooltips(){
	var objBody = document.getElementsByTagName("body")[0];
	var objContainer = document.createElement('div');
	objContainer.setAttribute('id', 'tooltip');
	objContainer.innerHTML = "&nbsp;";
	
	/*document.body.appentChild(objContainer); */
	objBody.appendChild(objContainer);
		  
	var objTag = document.getElementsByTagName('a');/*these are the a elements*/
	var objTagLength = objTag.length;
	for (i=0; i<objTagLength; i++) {
		if(objTag[i].title){
			objTag[i].onmouseover = showNote;
			objTag[i].onmousemove = moveNote;
			objTag[i].onmouseout = hideNote;
		}
	}
}

/*TOOLTIP FUNCTIONS*/
function getScroll() {
	if(document.body.scrollTop != undefined){  /* IE model*/
		var ieBox = document.compatMode != "CSS1Compat";
		var cont = ieBox ? document.body : document.documentElement;
		return {x : cont.scrollLeft, y : cont.scrollTop};
	}else{
		return {x : window.pageXOffset, y : window.pageYOffset};
	}
}

function showNote(e) {
	var objEvent = e ? e : event;
	var objTip = document.getElementById('tooltip');
	var scroll = getScroll();
	objTip.innerHTML = this.title;
	this.title = '';
	moveNote;
	objTip.style.visibility = (objTip.innerHTML == '') ? 'hidden' : 'visible';
}

function hideNote(e) {
	this.title = document.getElementById('tooltip').innerHTML;
	document.getElementById('tooltip').style.visibility = 'hidden';
}

function moveNote(e) {
	var objEvent = e ? e : event;
	var objTip = document.getElementById('tooltip');
	var scroll = getScroll();
	
	/* position tooltip*/
	var dw = document.width ? document.width : document.documentElement.offsetWidth - 25;
	if (objEvent.clientX > dw - objTip.offsetWidth)
		objTip.style.left = dw - objTip.offsetWidth + scroll.x + "px";
	else {
		objTip.style.left = objEvent.clientX - 2 + scroll.x + "px";
		objTip.style.top = objEvent.clientY + 15 + scroll.y + "px";
	}
}

function init(){
	init_tooltips();
}