// IMPORT js files
try{
	var jsRoot = root_url + '_includes/js/';
	if (typeof(km_scripts) == 'undefined') var km_scripts = new Object();
}catch(ex){}
function importJS(jsFile, charset) {
	if (km_scripts[jsFile] != null) return;
	var scriptElt = document.createElement('script');
	scriptElt.type = 'text/javascript';
	scriptElt.src = jsRoot+jsFile;
	if(charset == undefined) charset = "utf-8";
	scriptElt.charset = charset;
	document.getElementsByTagName('head')[0].appendChild(scriptElt);
	km_scripts[jsFile] = jsFile; 
}

var utilities = new Utilities();
function Utilities(){
	var __this = this;
	this.tips = new Array();
	this.debug = true;
	
	this.pregMatch = function (regexp, value) {
		var re = new RegExp(regexp);	
		if (value.match(re) != null) return true;	
		return false;	
	};
	
	this.goTo = function(url){
		window.location = url;	
	};
	
	this.toggle = function(target_caller, parent_selector, toggle_target_selector){
		try{
			var parent = this.findParentBySelector(parent_selector, target_caller);
			if (toggle_target_selector instanceof Array) {
				for (var i = 0; i < toggle_target_selector.length; i++) {
					jQuery(toggle_target_selector[i], parent).toggle("fast");
				}
			} else {
				jQuery(toggle_target_selector, parent).toggle("fast");
			}
		}catch(ex){
			if(this.debug) alert(ex.message);	
		}
	};
	
	this.standardizeEvent = function(event) {
		if (!event.target) {
		 event.target = event.srcElement;
		 event.stopPropagation = function() { 
			 this.cancelBubble = true; // note, this-scope is the event
		 };
		 event.preventDefault = function() {};
		}
		return event;
	};
	
	this.findParentBySelector = function(parent_selector, child_element) {
		var parents = jQuery(parent_selector);
		for(var i=0; i<parents.length; i++){
			var elements = jQuery('*', parents[i]);
			for(var a=0; a<elements.length; a++){
				var child = elements[a];
				if(child == child_element) return parents[i];
			}
		}
		return false;
	};
		
	this.hideTip = function(tt, change_type){	
		try{
			var scope = this;
			if((tt.nodeName.toLowerCase() == "textarea" || tt.nodeName.toLowerCase() == "input") && tt.name){;
				if(!this.tips[tt.name]){
					this.tips[tt.name] = new Array();
					this.tips[tt.name]["value"] = tt.value;
				}
				if(tt.value == this.tips[tt.name]["value"]){
					if(!tt.orginal_type) tt.orginal_type = tt.type;
					if(change_type){						
						/**
						  * IE issue
						*/
						var input = document.createElement('input');
						input.onfocus = tt.onfocus;
						input.name = tt.name;
						input.className = tt.className;
						input.id = tt.id;
						input.type = change_type;
						input.orginal_type = tt.orginal_type;
						
						jQuery(tt).replaceWith(input);	
						tt = input;
					}
					tt.value = "";
					tt.select();
				}
				tt.onblur = function(){
					scope.showTip(tt);
				}
			}
		}catch(ex){
			if(this.debug) alert(ex.message);	
		}
	};
	
	this.showTip = function(tt){
		var scope = this;
		try{
			if((tt.nodeName.toLowerCase() == "textarea" || tt.nodeName.toLowerCase() == "input") && tt.name){;
				if(this.tips[tt.name] && tt.value == ""){
					tt.empty = true;
					if(this.tips[tt.name]["value"]){
						if(tt.orginal_type != tt.type){
							/**
							  * IE issue
							*/
							var type = tt.type;
							var input = document.createElement('input');
							input.onfocus = function(){
								scope.hideTip(this, type);
							};
							input.name = tt.name;
							input.className = tt.className;
							input.id = tt.id;
							input.type = tt.orginal_type;
							
							jQuery(tt).replaceWith(input);	
							tt = input;
						}
						tt.value = this.tips[tt.name]["value"];
					}
				}else 
					tt.empty = false;
			}
		}catch(ex){
			if(this.debug) alert(ex);	
		}
	}	
	this.isDefaultValue = function(tt){
		try{
			this.hideTip(tt);
			this.showTip(tt);
			if(tt.empty) return true;
			else 
				return false;
		}catch(ex){
			if(this.debug) alert(ex);				
		}
		return true;
	}
	this.hasDefaultValue = function(tt){
		try{
			if(tt.default_value == tt.value || jQuery.trim(tt.value) == '') return true;
			else 
				return false;
		}catch(ex){
			if(this.debug) alert(ex);				
		}
		return true;
	}
	
	setTimeout(function(){
		__this.setDefaultParams();
	}, 300);
	
	this.setDefaultParams = function(){
		var elements = jQuery('[type=text]');
		for(var i=0; i < elements.length; i++){
			try{
				if(elements[i].default_value == undefined) elements[i].default_value = elements[i].value;	
			}catch(ex){ }
		}
	}
}
