var menu_links_current = '';
var menu_links = new Object();
var menu_links_classes = new Object();
var menu_pos_top = 0;
var menu_pos_left = 0;
addMenuLink = function(type_name, parent_name, child_name, title,id) {
	if (typeof menu_links[id] == "undefined") {
		menu_links[id] = new Object();
	}
  if (typeof menu_links[id][type_name] == "undefined") {
		menu_links[id][type_name] = new Object();
	}
	if (typeof menu_links[id][type_name][parent_name] == "undefined") {
		menu_links[id][type_name][parent_name] = new Object();
	}
	if (typeof menu_links[id][type_name][parent_name][child_name] == "undefined") {
		menu_links[id][type_name][parent_name][child_name] = title;
	}
}
hideMenuLink = function() {
	if (document.getElementById('topmenu_' + menu_links_current)) {
		document.getElementById('topmenu_' + menu_links_current).className = menu_links_classes['menu_link_' + menu_links_current];
		var menu_links_div = document.getElementById('menu_link_' + menu_links_current);
        if (menu_links_div == null) { return }
		doHeightChange(menu_links_current,menu_links_div.offsetHeight,0);
	}
}
viewMenuLink = function(type_name, parent_name, menu_obj_id) {
	var menu_obj = document.getElementById('topmenu_' + menu_obj_id);
	var menu_links_div = document.getElementById('menu_link_' + menu_obj_id);
	if (menu_links_current != menu_obj_id) {
		hideMenuLink();	
	} else {
		menu_obj.className = menu_links_classes['menu_link_' + menu_obj_id];
	}
	if (menu_links_div == null) {
		var menu_links_div = document.getElementById('menu_link_' + menu_links_current);
        menu_links_current = menu_obj_id;
    	menu_links_classes['menu_link_' + menu_obj_id] = menu_obj.className;
    	menu_obj.className = 'selected';
		return;
	}
	var menu_links_count = 0;
	var box = '<div class="content">';
	for (i in menu_links[menu_obj_id]) {
		for (j in menu_links[menu_obj_id][i]) {
			for (k in menu_links[menu_obj_id][i][j]) {
				//Modified 04.06.2007
				//menu_links_count++;
				box = box + '<a href="'+ menu_links_href +'/' + i + '/' + j + '/' + k + '"><div class="child" id="menu_link_' + i +'" onmouseover="showMenuHover(this,\'childSel\');" onmouseout="hideMenuHover(this);">' + menu_links[menu_obj_id][i][j][k] + '</div></a>';
			}
		}
	}
	box = box + '</div>';
	menu_links_div.innerHTML = box;
	menu_pos_top = Position.cumulativeOffset(menu_obj)[1];
	menu_pos_left = Position.cumulativeOffset(menu_obj)[0];
	if (menu_links_div.offsetHeight > 0) {
		if (menu_links_div.heightChangeMemInt) {
		window.clearInterval(menu_links_div.heightChangeMemInt);
		} else {
			menu_links_div.style.top = (menu_pos_top - 5) + 'px';
		}
	} else {
		menu_links_div.style.top = (menu_pos_top - 5) + 'px';
	}
	menu_links_div.style.left = menu_pos_left + 'px';
	menu_links_current = menu_obj_id;
	menu_links_classes['menu_link_' + menu_obj_id] = menu_obj.className;
	menu_obj.className = 'selected';
	var menu_links_height = 0;
	for(var i = 0; i < menu_links_div.childNodes.length; i++) {
		if (menu_links_div.childNodes[i].nodeName == 'DIV') {
			menu_links_height = Element.getDimensions(menu_links_div.childNodes[i]).height;
		}
	}
	//doHeightChange(menu_obj_id,menu_links_div.offsetHeight,(menu_links_count * 15));
	doHeightChange(menu_obj_id,menu_links_div.offsetHeight,menu_links_height);
}
doHeightChange = function(menu_obj_id,start_height,end_height) {
	var menu_links_div = document.getElementById('menu_link_' + menu_obj_id);
	if (menu_links_div.heightChangeMemInt) window.clearInterval(menu_links_div.heightChangeMemInt);
	var actStep = 0;
	if (end_height > start_height) {
		var stepcount = end_height - start_height;
	} else {
		var stepcount = start_height - end_height;
	}
	if (stepcount == 0) {
		stepcount = 30;
	}
	menu_links_div.heightChangeMemInt = window.setInterval(
		function() {
			menu_links_div.currentHeight = easeInOut(start_height,end_height,actStep,stepcount);
			menu_links_div.style.top = (100 - menu_links_div.currentHeight) + 'px';
			menu_links_div.style.height = menu_links_div.currentHeight + 'px';
			actStep++;
			if (actStep > stepcount) {
				window.clearInterval(menu_links_div.heightChangeMemInt);
			}
		}
	,10)
}
easeInOut = function(minValue,maxValue,actualStep,stepcount) {
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / stepcount)*actualStep),0.5)*delta);
	return Math.ceil(stepp);
}
setHeights = function() {
	if (document.getElementById('obj7').offsetHeight > document.getElementById('obj8').offsetHeight) {
		document.getElementById('obj8').style.height = document.getElementById('obj7').offsetHeight + 'px';
	} else {
		document.getElementById('obj7').style.height = document.getElementById('obj8').offsetHeight + 'px';
	}
}
showMenuHover = function(menu,menuclass) {
	menu_links_classes[menu.id] = menu.className;
	menu.className = menuclass;
}
hideMenuHover = function(menu) {
	menu.className = menu_links_classes[menu.id];
}
var d=document;
function createCustomBoxes() {
	// bail out is this is an older browser
	if(!d.getElementById)return;
	// remove our test image from the DOM
	//d.body.removeChild(d.getElementById("so_testImage"));
	// an array of applicable events that we'll need to carry over to our custom checkbox
	events = new Array("onfocus", "onblur", "onselect", "onchange", "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmousemove", "onmouseout", "onkeypress", "onkeydown", "onkeyup");
	// a reference var to all the forms in the document

	frm = d.getElementsByTagName("form");
	// loop over the length of the forms in the document
	for(i=0;i<frm.length;i++) {
		// reference to the elements of the form
		c = frm[i].elements;
		// loop over the length of those elements
		for(j=0;j<c.length;j++) {
			// if this element is a checkbox, do our thing
			if(c[j].getAttribute("type") == "checkbox") {
				// hide the original checkbox
				//c[j].style.position = "absolute";
				//c[j].style.left = "-9000px";
				c[j].style.display = 'none';
				// create the replacement image
				n = d.createElement("img");
				n.setAttribute("class","chk");
				// check if the corresponding checkbox is checked or not. set the
				// status of the image accordingly
				if(c[j].checked == false) {
					n.setAttribute("src","img/unchecked.png");
					n.setAttribute("title","click here to select this option.");
					n.setAttribute("alt","click here to select this option.");
				} else {
					n.setAttribute("src","img/checked.png");
					n.setAttribute("title","click here to deselect this option.");
					n.setAttribute("alt","click here to deselect this option.");
				}
				// there are several pieces of data we'll need to know later.
				// assign them as attributes of the image we've created
				// first - the name of the corresponding checkbox
				n.xid = c[j].getAttribute("name");
				// next, the index of the FORM element so we'll know which form object to access later

				n.frmIndex = i;
				// assign the onclick event to the image
				n.onclick = function() { so_toggleCheckBox(this,0);return false; }
				// insert the image into the DOM
				c[j].parentNode.insertBefore(n,c[j].nextSibling)
				// this attribute is a bit of a hack - we need to know in the event of a label click (for browsers that support it)
				// which image we need turn on or off. So, we set the image as an attribute!
				c[j].objRef = n;
				// assign the checkbox objects event handlers to its replacement image
				for(e=0;e<events.length;e++) if(eval('c[j].' +events[e])) eval('n.' + events[e] + '= c[j].' + events[e]);
				// append our onchange event handler to any existing ones.
				fn = c[j].onchange;
				if(typeof(fn) == "function") {
					c[j].onchange = function() { fn(); so_toggleCheckBox(this.objRef,1); return false; }
				} else {
					c[j].onchange = function () { so_toggleCheckBox(this.objRef,1); return false; }
				}
			}
			if (c[j].type == "radio") {
						// hide the original checkbox
						//c[j].style.position = "absolute";
						//c[j].style.left = "-9000px";
						c[j].style.display = 'none';
						// create the replacement image
						n = d.createElement("img");
						n.setAttribute("class","chk");
					// check if the corresponding checkbox is checked or not. set the
						// status of the image accordingly
						if(c[j].checked == false) {
							n.setAttribute("src","img/unchecked.png");
							n.setAttribute("title","click here to select this option.");
							n.setAttribute("alt","click here to select this option.");
						} else {
							n.setAttribute("src","img/checked.png");
							n.setAttribute("title","click here to deselect this option.");
							n.setAttribute("alt","click here to deselect this option.");
						}
						// there are several pieces of data we'll need to know later.
						// assign them as attributes of the image we've created
						// first - the name of the corresponding checkbox
						n.xid = c[j];
						// next, the index of the FORM element so we'll know which form object to access later
						n.frmIndex = i;
						// assign the onclick event to the image
						n.onclick = function() { so_toggleRadioBox(this,0);return false; }
						// insert the image into the DOM
						c[j].parentNode.insertBefore(n,c[j].nextSibling)
						// this attribute is a bit of a hack - we need to know in the event of a label click (for browsers that support it)
						// which image we need turn on or off. So, we set the image as an attribute!
						c[j].objRef = n;
						// assign the checkbox objects event handlers to its replacement image
						for(e=0;e<events.length;e++) if(eval('c[j].' +events[e])) eval('n.' + events[e] + '= c[j].' + events[e]);
						// append our onchange event handler to any existing ones.
						fn = c[j].onchange;
						if(typeof(fn) == "function") {
							c[j].onchange = function() { fn(); so_toggleRadioBox(this.objRef,1); return false; }
						} else {
							c[j].onchange = function () { so_toggleRadioBox(this.objRef,1); return false; }
						}
			}
		}
	}
}
function so_toggleRadioBox(imgObj,caller) {
	// if caller is 1, this method has been called from the onchange event of the checkbox, which means
	// the user has clicked the label element. Dont change the checked status of the checkbox in this instance
	// or we'll set it to the opposite of what the user wants. caller is 0 if coming from the onclick event of the image
	
	// reference to the form object
	// the name of the checkbox we're changing
	objName = imgObj.xid;
	// change the checked status of the checkbox if coming from the onclick of the image
	if(!caller)objName.checked = !objName.checked?true:false;
	elm = document.getElementsByName(objName.name);
	for (i=0;i<elm.length;i++) {
		// finally, update the image to reflect the current state of the checkbox.
		if(elm[i].checked == false) {
			elm[i].objRef.setAttribute("src","img/unchecked.png");
			elm[i].objRef.setAttribute("title","click here to select this option.");
			elm[i].objRef.setAttribute("alt","click here to select this option.");
		} else {
			elm[i].objRef.setAttribute("src","img/checked.png");
			elm[i].objRef.setAttribute("title","click here to deselect this option.");
			elm[i].objRef.setAttribute("alt","click here to deselect this option.");
		}
	}
}
function so_toggleCheckBox(imgObj,caller) {
	// if caller is 1, this method has been called from the onchange event of the checkbox, which means
	// the user has clicked the label element. Dont change the checked status of the checkbox in this instance
	// or we'll set it to the opposite of what the user wants. caller is 0 if coming from the onclick event of the image
	
	// reference to the form object
	formObj = d.forms[imgObj.frmIndex];
	// the name of the checkbox we're changing
	objName = imgObj.xid;
	// change the checked status of the checkbox if coming from the onclick of the image
	if(!caller)formObj.elements[objName].checked = !formObj.elements[objName].checked?true:false;
	// finally, update the image to reflect the current state of the checkbox.
	if(formObj.elements[objName].checked == true) {
		imgObj.setAttribute("src","img/checked.png");
		imgObj.setAttribute("title","click here to select this option.");
		imgObj.setAttribute("alt","click here to select this option.");
	} else {
		imgObj.setAttribute("src","img/unchecked.png");
		imgObj.setAttribute("title","click here to deselect this option.");
		imgObj.setAttribute("alt","click here to deselect this option.");
	}
}
  /*
    Function calculates days between two dates
  */
	daysBetween = function (DateFormat_get) {     
		//gets start and end date
		var day1 = $('IERASANAS_DIENA').value;
		var month1 = $('IERASANAS_MENESIS').value;
		var year1 = $('IERASANAS_GADS').value;
	
		var day2 = $('AIZBRAUKSANAS_DIENA').value;
		var month2 = $('AIZBRAUKSANAS_MENESIS').value;
		var year2 = $('AIZBRAUKSANAS_GADS').value;
		if (day1 == '0' || month1 == '0' || year1 == '0'||day2 == '0' || month2 == '0' || year2 == '0') {
			return;
		}
		var start_date=new Date(year1,month1, day1);
	//alert(start_date)
		var end_date=new Date(year2,month2, day2);
		var one_day=1000*60*60*24;
		var days_between = (Math.ceil((end_date.getTime()-start_date.getTime())/(one_day))); //calculates days between
		
		if (days_between<0) {
			days_between = '';
		}
		$('NAKSU_SKAITS').value = days_between;
	}

  
  /*
    Function calculates end date from start date and days between
  */
	daysBetween2 = function(DateFormat) {
		var days_between = parseInt($('NAKSU_SKAITS').value);
		if (days_between == 'NaN' || days_between < 0) {
			return;
		}
		var day1 = $('IERASANAS_DIENA').value;
		var month1 = $('IERASANAS_MENESIS').value;
		var year1 = $('IERASANAS_GADS').value;
		if (day1 == '0' || month1 == '0' || year1 == '0') {
			return;
		}
      		var end_date = new Date(year1,month1, parseInt(day1) + parseInt(days_between));
//alert(end_date.getDate()+'.'+end_date.getMonth()+'.'+end_date.getFullYear());
		$('AIZBRAUKSANAS_DIENA').value = end_date.getDate();
		$('AIZBRAUKSANAS_MENESIS').value = end_date.getMonth();
		$('AIZBRAUKSANAS_GADS').value = end_date.getFullYear();
  	}