function enter_pressed(e) {
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else
		return true;

	if (keycode == 13) {
		return true;
	} else
		return false;
}

var open_help_div;

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
} 


function show_hints (form) {
	Form.getElements(form).collect(function(elm) {
	    if (elm.title) {
	    	insert_advice(elm);
	    }
	}).all();
};


function help_box(name, title) {

	if (open_help_div != null && document.getElementById("help_div_" + open_help_div).className != "help_div_hidden") {
		help_box_close(open_help_div);
		if (open_help_div == name) {
			open_help_div = null;
			return null;
		}
	}
	
	help_div = document.getElementById("help_div_" + name);
	
	if (!help_div) {
		var help_div = document.createElement("span");
		help_div.setAttribute("id", "help_div_" + name);
	}
	
	var img_elm = document.getElementById("advice-" + name);
	var div_elm = document.getElementById("help_box_" + name);
	
	help_div.innerHTML = "<div style='float: left; width: 133px;' id='div_message_" + name + "'>" + title + "</div><div id='close_x_" + name + "' onClick='help_box(\"" + name + "\", \"\")'>x</div>";
	
	div_elm.appendChild(help_div);
	
	var close_x_elm = document.getElementById("close_x_" + name);
	
	if (title && img_elm.className == "help_icon") {
		help_div.className = "help_div_visible_yellow";
		close_x_elm.className = "help_div_x_yellow";
		img_elm.className = "help_icon_yellow";
	}
	else if (title && img_elm.className == "help_icon_error") {
		help_div.className = "help_div_visible_error";
		close_x_elm.className = "help_div_x_error";
		img_elm.className = "help_icon_error";
	}

	open_help_div = name;
	
}

function help_box_close(name){

	var help_div = document.getElementById("help_div_" + name);
	var img_elm = document.getElementById("advice-" + name);
	if (help_div && img_elm) {
		if (help_div.className == "help_div_visible_yellow") {
			help_div.className = "help_div_hidden";
			img_elm.className = "help_icon";
		}
		else if (help_div.className == "help_div_visible_error") {
			help_div.className = "help_div_hidden";
			img_elm.className = "help_icon_error";
		}
	}
}

function insert_advice(elm) {

	var advice = '<span id="help_box_' + elm.name + '"><span onClick="help_box(\'' + elm.name + '\', document.getElementById(\'advice-' + elm.name + '\').title)" class="help_icon" id="advice-' + elm.name + '" title="' + elm.title + '"></span></span>';
    switch (elm.type.toLowerCase()) {
		case 'checkbox':
		case 'radio':
			var p = elm.parentNode;
			if(p) {
				new Insertion.Bottom(p, advice);
			} else {
				new Insertion.After(elm, advice);
			}
			break;
		default:
			new Insertion.After(elm, advice);
    }
}

Object.extend(Validation, {
	test : function(name, elm, useTitle) {
		var v = Validation.get(name);
		try {
			help_box_close(open_help_div);
			open_help_div = null;
			
			if(!v.test($F(elm), elm)) {
				if (!$('advice-' + elm.name)) {
					insert_advice(elm);
				}
			    var errorMsg = useTitle ? ((elm && elm.title) ? elm.title : v.error) : v.error;
			    var errorMsgString = error_head + " " + errorMsg + " " + (elm.title ? elm.title : "");

			    $('advice-' + elm.name).removeClassName('help_icon');
			    $('advice-' + elm.name).addClassName('help_icon_error');
			    $('advice-' + elm.name).title = errorMsgString;
			    
				elm.removeClassName('validation-passed');
				elm.addClassName('validation-failed');

				return false;
			} else if ($('advice-' + elm.name)){
				if (!$('advice-' + elm.name).title) {
					$('advice-' + elm.name).remove();
				} else {
					$('advice-' + elm.name).removeClassName('help_icon_error');
					$('advice-' + elm.name).addClassName('help_icon');
					$('advice-' + elm.name).title = elm.title;
				}
				elm.removeClassName('validation-failed');
				elm.addClassName('validation-passed');
				return true;
			} else {
				elm.removeClassName('validation-failed');
				elm.addClassName('validation-passed');
				return true;
			}
		} catch(e) {
			throw(e)
		}
	}
});

function initPage() {
 	var map = $('map');
	if(map) {
		var diamond_list = map.getElementsByTagName('AREA');
		for(var i=0; i<diamond_list.length; i++){
		    el = diamond_list[i];
			el.style.display = 'none';
			Event.observe(
              el.id,
              'mouseover',
              function() { 
				var diamond_id = this.id;
				diamond_id = diamond_id.substring(diamond_id.indexOf('_')+1, diamond_id.length);
				$(diamond_id).style.display = 'inline';
              }
            );
            
			Event.observe(
              el.id,
              'mouseout',
              function() { 
				var diamond_id = this.id;
				diamond_id = diamond_id.substring(diamond_id.indexOf('_')+1, diamond_id.length);
				$(diamond_id).style.display = 'none';
              }
            );
            
			Event.observe(
              el.id,
              'click',
              function() { 
				var diamond_id = this.id;
				diamond_id = diamond_id.substring(diamond_id.indexOf('_')+1, diamond_id.length);
				window.location = $(diamond_id + '_a').href;
				return false;
              }
            );
		}
	}
}

/**
 * IE7 Fix where BODY element was skipped which produced an error
 * @author Marko.Kruustuk
 * @date 22.04.2009
 */
//-----------------------------------------------------------------------------
Object.extend(Epoch.prototype, {
	getTop : function (element) //PRIVATE: returns the absolute Top value of element, in pixels
	{
	    var oNode = element;
	    var iTop = 0;
	    
	    while(oNode.tagName != 'BODY' && oNode.tagName != 'HTML') {
	        iTop += oNode.offsetTop;
	        oNode = oNode.offsetParent;
	    }
	    
	    return iTop;
	}
});
//-----------------------------------------------------------------------------
Object.extend(Epoch.prototype, {
	getLeft : function (element) //PRIVATE: returns the absolute Left value of element, in pixels
	{
	    var oNode = element;
	    var iLeft = 0;
	    
	    while(oNode.tagName != 'BODY' && oNode.tagName != 'HTML') {
	        iLeft += oNode.offsetLeft;
	        oNode = oNode.offsetParent;        
	    }
	    
	    return iLeft;
	}
});

function show_notice() {
    if ($('hide_notice').style.display == 'none') {
        $('hide_notice').style.display = 'block'; 
        setTimeout('document.forms["name"].submit();', 1000);
    }
}

function eurCalculatoret() {
    window.open( "http://www.mkindlustus.ee/wp-content/themes/insurance/calculator/et/index.html", "myWindow", "status = 0, height = 220, scrollbars=0, width = 230, resizable = 0" );
}

function eurCalculatorru() {
    window.open( "http://www.mkindlustus.ee/wp-content/themes/insurance/calculator/ru/index.html", "myWindow", "status = 0, height = 220, scrollbars=0, width = 230, resizable = 0" );
}

Object.extend(Epoch.prototype, {
	setLang : function ()
	{
		this.daylist = new Array(ep_Su,ep_Mo,ep_Tu,ep_We,ep_Th,ep_Fr,ep_Sa,ep_Su,ep_Mo,ep_Tu,ep_We,ep_Th,ep_Fr,ep_Sa); /*<lang:en>*/
		this.months_sh = new Array(ep_Jan,ep_Feb,ep_Mar,ep_Apr,ep_May,ep_Jun,ep_Jul,ep_Aug,ep_Sep,ep_Oct,ep_Nov,ep_Dec);
		this.monthup_title = ep_next_month;
		this.monthdn_title = ep_previous_month;
		this.clearbtn_caption = ep_clear;
		this.clearbtn_title = ep_clears;
		this.maxrange_caption = ep_max;
	}
});

Object.extend(Epoch.prototype, {
	calConfig : function ()
	{
		//this.mode = 'flat'; //can be 'flat' or 'popup'
		this.displayYearInitial = this.curDate.getFullYear(); //the initial year to display on load
		this.displayMonthInitial = this.curDate.getMonth(); //the initial month to display on load (0-11)
		this.rangeYearLower = 2005;
		this.rangeYearUpper = 2037;
		this.minDate = new Date(2005,0,1);
		this.maxDate = new Date(2037,0,1);
		this.startDay = 1; // the day the week will 'start' on: 0(Sun) to 6(Sat)
		this.showWeeks = false; //whether the week numbers will be shown
		this.selCurMonthOnly = false; //allow user to only select dates in the currently displayed month
		this.clearSelectedOnChange = true; //whether to clear all selected dates when changing months
		
		//flat mode-only settings:
		//this.selectMultiple = true; //whether the user can select multiple dates (flat mode only)
	
		switch(this.mode) //set the variables based on the calendar mode
		{
			case 'popup': //popup options
				this.visible = false;
				break;
			case 'flat':
				this.visible = true;
				
				break;
		}
		this.setLang();
	}
});
