// JavaScript Document
Event.observe(window, 'load', set_image_rollover, false);
Event.observe(window, 'load', set_input_rollover, false);
Event.observe(window, 'load', set_editable_fields, false);
Event.observe(window, 'load', set_input_accept, false);
Event.observe(window, 'load', set_price_fields, false);

function set_image_rollover(){ images = document.getElementsByTagName("img");for(i=0;i<images.length;i++){ if(images[i].getAttribute("rollover")!=null){ pre_img = new Image(); pre_img.src = images[i].getAttribute("rollover"); pre_img = null; images[i].setAttribute('origional', images[i].src); images[i].onmouseover = function(){ this.src=this.getAttribute('rollover');}; images[i].onmouseout = function(){ this.src=this.getAttribute('origional');};}}}
function set_input_rollover(){ images = document.getElementsByTagName("input");for(i=0;i<images.length;i++){ if(images[i].getAttribute("rollover")!=null){ pre_img = new Image(); pre_img.src = images[i].getAttribute("rollover"); pre_img = null; images[i].setAttribute('origional', images[i].src); images[i].onmouseover = function(){ this.src=this.getAttribute('rollover');}; images[i].onmouseout = function(){ this.src=this.getAttribute('origional');};}}}
function set_input_accept(){ images = document.getElementsByTagName("input");for(i=0;i<images.length;i++){ if(images[i].getAttribute("type")=="text"&&images[i].getAttribute("accept")!=null){ images[i].onkeypress = function(e){ key = get_key(e); if(this.getAttribute('accept').indexOf(key)>=0) return true; return false;};}}}
function set_editable_fields(){ 	
	elements = document.getElementsByTagName("*");
	for(i=0;i<elements.length;i++){
		if(isset(elements[i].getAttribute("editable"))){
			elements[i].setAttribute("editable-origional-style-background", elements[i].style.background);
			elements[i].setAttribute("editable-origional-style-border", elements[i].style.border);
			elements[i].style.cursor="pointer";
			elements[i].onmouseover=function(){this.style.background="#CCCCCC";/*this.style.border="1px solid black";*/}
			elements[i].onmouseout=function(){this.style.background=this.getAttribute("editable-origional-style-background");/*this.style.border=this.getAttribute("editable-origional-style-border");*/}
			elements[i].onmousedown=function(){ 
				extra=""; 
				page=window.location.href.split("\?").shift();
				if(isset(this.getAttribute('editable_extra'))) extra="&"+this.getAttribute('editable_extra'); 
				if(isset(this.getAttribute('editable_page'))) page=this.getAttribute('editable_page'); 
				redirect(page+"?edit-field="+this.getAttribute("editable")+extra);
			}
			//window.alert(elements[i].getAttribute("editable"));
		}
	}
}
function set_price_fields(){ 	
	elements = document.getElementsByTagName("input");
	for(i=0;i<elements.length;i++){
		if(isset(elements[i].getAttribute("price"))){
			elements[i].onkeypress=function(oEvent){
				var code;
				if (!oEvent) var oEvent = window.event;
				if (oEvent.keyCode) code = oEvent.keyCode;
				else if (oEvent.which) code = oEvent.which;
				if(code=="9"||code=="8"||code=="116") return true;
				var character = String.fromCharCode(code);
				
				if("0123456789".indexOf(character)>=0) return true;
				else if(character=="."&&this.value.indexOf(".")==-1) return true;
				return false;
			}
		}
	}
}

function isset(VALUE){
	return !(VALUE==null);	
}
function redirect(URL){
	window.location.href=URL;
}
function get_key(e){
	if(window.event){
		keynum = e.keyCode;
	}else if(e.which){
		keynum = e.which;
	}
	return(String.fromCharCode(keynum));
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}