var ua			= navigator.userAgent;
var IsOpera		= /opera [56789]|opera\/[56789]/i.test(ua);
var IsIE		= !IsOpera && /msie [56789]/i.test(ua);		// preventing opera to be identified as ie
var IsMozilla	= !IsOpera && /mozilla\/[56789]/i.test(ua);	// preventing opera to be identified as mz

var defaultFontSize = 76;
var currentFontSize = defaultFontSize;

//******************************************************
function getEvent() {
	var e = false;
	if (IsMozilla) {
		if (window.event) {
			//inspect(window.event.srcElement);
			e = window.event.srcElement;
		} else {
			//inspect(window.event);
		}
	} else {
		e = event;
	}
	return e;
}

function getPE(id) {return window.opener.document.getElementById(id);}
function getE(id) { return document.getElementById(id); }

//******************************************************
var pageLoaded  = '';
var windowOnloadFunctions = new Array();
var windowOnunloadFunctions = new Array();

function add_to_window_onload(function_) {
    ind = windowOnloadFunctions.length;
	windowOnloadFunctions[ind]=function_;
}

function add_to_window_onunload(function_) {
    ind = windowOnunloadFunctions.length;
	windowOnunloadFunctions[ind]=function_;
}

function window_onload_script() {
	set_page_loaded();
	ar = windowOnloadFunctions;
	for (i = 0; i < ar.length ; i++) {
		ar[i].call();
	}
}

function window_onunload_script() {
	ar = windowOnunloadFunctions;
	for (i = 0; i < ar.length ; i++) {
		ar[i].call();
	}
}

function set_page_loaded() {
	pageLoaded  = '1';
}



//******************************************************
function show_in_popup(text, lines) {
	var w = window.open('', 'inspect', 'width=400, height=400, scrollbars=yes, resizeable=yes');
	var rows = (lines < 20) ? lines : 20;
	var t = w.document.write('<textarea rows="' + rows + '" cols="40">' + text + '</textarea>');
}

function inspect(obj) {
	var str = "";
	var lines = 0;
	for (var prop in obj) {
		str += "obj." + prop + " = " + obj[prop] + "\n";
		lines++;
	}
	show_in_popup(str, lines);
}

function inspect1(obj) {
	var str = "";
	var lines = 0;
	for (var prop in obj) {
		var value = '' + obj[prop];
		if (value.indexOf('function') == -1) {
			str += "obj." + prop + " = " + value + "\n";
			lines++;
		}
	}
	show_in_popup(str, lines);
}

function inspect2(obj, filter) {
	var str = "";
	var lines = 0;
	for (var prop in obj) {
		var value = '' + obj[prop];
		var prop1 = prop.toLowerCase();
		if ((value.indexOf('function') == -1) && (prop1.indexOf(filter) != -1)) {
			str += "obj." + prop + " = " + value + "\n";
			lines++;
		}
	}
	show_in_popup(str, lines);
}

//******************************************************
//TODO - make this to work for more than one form
// on a document
var form_changed = false;
var is_pre_submit_form = 0;
function window_unload_handler() {
	if (form_changed) {
	    return 'This form has unsaved data.';
	}
}
function form_submit_handler() {
	form_changed = 0;
	//alert('changed flag resetted');
}
function form_change_handler() {
	form_changed = 1;
	//alert('changed flag set');
}
function attach_event_handler(obj, event, handler) {
	if (IsIE) {
		obj.attachEvent('on' + event, handler);
	} else {
		obj.addEventListener(event, handler, false);
	}
}

function set_form_change_notifiers(frm) {
	for (var i=0; i<frm.elements.length; i++) {
		var o=frm.elements[i];
		//alert(o.id + ':' + o.type);
		if (o.type == 'radio' || o.type == 'checkbox') {
			attach_event_handler(o, 'click', form_change_handler);
		}
		if (o.type == 'password' || o.type == 'text' || o.type == 'select-one' || o.type == 'textarea') {
			attach_event_handler(o, 'change', form_change_handler);
		}
	}
}

function set_form_change_tracking(formId) {
	var frm = getE(formId);
	set_form_change_notifiers(frm);
	attach_event_handler(frm, 'submit', form_submit_handler);
	//attach_event_handler(window, 'unload', window_unload_handler);
	//attach_event_handler(window, 'beforeunload', window_unload_handler);
	window.onbeforeunload = window_unload_handler;
}

function add_form_field (formId, fieldType, fieldName, fieldValue) {
  	var form = getE(formId);
	if (form != null && document.getElementById) {
   		var input = document.createElement('INPUT');
		if (document.all) { // what follows should work with NN6 but doesn't in M14
	        input.type = fieldType;
   		    input.name = fieldName;
       		input.value = fieldValue;
		} else if (document.getElementById) { // NN6 workaround
			input.setAttribute('type', fieldType);
			input.setAttribute('name', fieldName);
			input.setAttribute('value', fieldValue);
		}
		form.appendChild(input);
		return true;
	}
	return false;
}

function submit_form(formId) {
  	var form = getE(formId);
	if (form != null) {
		form_changed = 0;
		form.submit();
		return true;
	}	
	return false;
}

//******************************************************
function submitOnEnter() {
    //used for submit on ENTER pressed when 'drop-down' box being focused
    e = getEvent();
	if (e.keyCode == 13) {
		getE('main_form').submit();
	}
	return true;
}

function openpopupSmall(popurl)
{
    openpopup(popurl, 220, 170);
}

function openpopupBasic(popurl)
{
    openpopup(popurl, 300, 400);
}

function openpopupMedium(popurl)
{
    openpopup(popurl, 600, 500);
}

function openpopupMediumHalf(popurl)
{
    openpopup(popurl, 600, 200);
}

function openpopup(popurl, scrWidth, scrHeight) {
	leftVal = (screen.width - scrWidth) / 2;
	topVal = ((screen.height - scrHeight) / 2) - 150;
	var winpops=window.open(popurl,"","width="+scrWidth+",height="+scrHeight+",top="+topVal+",left="+leftVal+",scrollbars=yes,resizable=yes")
}

function openpopupPrint(popurl)
{
	var scrWidth=screen.width;
	var scrHeight=screen.height;
	leftVal = 0;
	topVal = 0;
	var winpops=window.open(popurl,"","width="+scrWidth+",height="+scrHeight+",top="+topVal+",left="+leftVal+",scrollbars=yes,resizable=yes,menubar=yes")
}

function relocate(url) {
	window.location.replace(url);
}

//******************************************************
function highlightE(id, checked)
{
	var elem = getE(id);
	if (elem != null) {
		if(checked)
			elem.style.backgroundColor = '#CCCCCC';
		else
			elem.style.backgroundColor = '';//'#FFFFFF';
	}
};

function showE(id) {
	var elem = getE(id);
	if (elem != null) {
		elem.style.display = 'block';
	}
}

function hideE(id) {
	var elem = getE(id);
	if (elem != null) {
		elem.style.display = 'none';
	}
}

function rollMainMenuitemOver(itemName) {
    var elem = getE("tmiul" + itemName);
    elem.className = "menuunderlineover";
}

function rollMainMenuitemOut(itemName) {
    var elem = getE("tmiul" + itemName);
    elem.className = "menuunderline";
}

function rollMenuitemOver(itemName) {
    var elem = getE(itemName);
    elem.className = "menuitemroll";
}

function rollMenuitemOut(itemName) {
    var elem = getE(itemName);
    elem.className = "menuitem";
}

function selectMainMenuitem(itemName) {
    var elem = getE("tmiul" + itemName);
    elem.className = "menuunderlineover";
}

function createDayList(sel_day,day,month,year) {
 	var day = getE(day);
 	last_day = new Date(year,month,0,0,0,0).getDate();
 	if (last_day < sel_day) {
        sel_day = last_day;
    }
 	day.options.length = last_day;
 	for( i=0,val=1; i < day.options.length; i++,val++) {
		day.options[i] = new Option(val,val);
 		if(day.options[i].value == sel_day)day.options[i].selected = true;
 	}
}

function switchPicture(switching, switched) {
    var switchingElem = getE(switching);
    switchingElem.style.display = "block";
    var switchedElem = getE(switched);
    switchedElem.style.display = "none";
}

function pressButton(elemTitle) {
    var leftBut = getE(elemTitle + "left");
    if (leftBut!= undefined) {
        leftBut.src="images/button3left.gif";
        var centerBut = getE(elemTitle + "center");
        centerBut.className = "button3";
        var rightBut = getE(elemTitle + "right");
        rightBut.src="images/button3right.gif";
    }
}

function releaseButton(elemTitle) {
    var leftBut = getE(elemTitle + "left");
    if (leftBut!= undefined) {
        leftBut.src="images/buttonleft.gif";
        var centerBut = getE(elemTitle + "center");
        centerBut.className = "button";
        var rightBut = getE(elemTitle + "right");
        rightBut.src="images/buttonright.gif";
    }
}

function changeFontSize(sizeDifference){
	currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 5);

	if(currentFontSize > 100){
		currentFontSize = 100;
	}else if(currentFontSize < 60){
		currentFontSize = 60;
	}

	setFontSize(currentFontSize);
}

function setFontSize(fontSize){
	var stObj = (document.getElementById) ? document.getElementById('content') : document.all('content');
	document.body.style.fontSize = fontSize + '%';
}

function toggleFullContent(id) {
    var plusElem = getE("plus" + id);
    var minusElem = getE("minus" + id);
    if (plusElem.style.display == "none") {
        plusElem.style.display = "inline";
        minusElem.style.display = "none";
    } else {
        plusElem.style.display = "none";
        minusElem.style.display = "inline";
    }

    var fullElem = getE("fullcontent" + id);
    var shortElem = getE("shortcontent" + id);
    if (fullElem.style.display == "none") {
        fullElem.style.display = "block";
        shortElem.style.display = "none";
    } else {
        fullElem.style.display = "none";
        shortElem.style.display = "block";
    }

    var toggleInput = getE("check" + id);
    if (fullElem.style.display == "none") {
	    toggleInput.value = 0;
    } else {
	    toggleInput.value = 1;
    }
}

function assembleDate() {
    var DateInElem = getE('DateIn');
    var DayInElem = getE('checkInDay');
    var MonthInElem = getE('checkInMonth');
    var YearInElem = getE('checkInYear');
    DateInElem.value = MonthInElem.value + "/" + DayInElem.value + "/" + YearInElem.value;
}

function openCalendar(FormElement) {
    var calendarwindow;

    url = "calendar.html?formname=form1&formelement=" + FormElement;
    calendarwindow = window.open(url,"thewindow","toolbar=no,LEFT=300,TOP=250, WIDTH=170, HEIGHT=140,status=no,scrollbars=no,resize=no,menubar=no");
    calendarwindow.focus();
}

function show(id,display) {
    if (!display) display='inherit';
	var obj = getE(id);
	if (!obj) return;
	obj.style.display = display;
	obj.style.visibility = '';
}


function hide(id) {
	var obj = getE(id);
	if (!obj) return;
	obj.style.display = 'none';
	obj.style.visibility = 'hidden';
}

function toggleBookNow() {
    toggleElement('booknowsect');
}

function toggleLeftMenu() {
    toggleElement('leftMenu');
    elemLeftMenu = getE('leftMenu');
    elemLeftMenuSwitcher = getE('leftMenuSwitcher');
    if (elemLeftMenu.style.display=="none") {
        elemLeftMenuSwitcher.innerHTML = "Show list of pages";
    } else {
        elemLeftMenuSwitcher.innerHTML = "Hide list of pages";
    }
}

function toggleElement(id) {
    elem = getE(id);
    if (elem.style.display=="none") {
        elem.style.display="block";
    } else {
        elem.style.display="none";
    }
}

function open_popup_for_content(str) {
	var w = window.open('','new',"width=300,height=100");
	w.document.open();
	w.document.write("<html>"+str+"</html>");
	w.document.close();
}	

function open_popup_for_link(str) {
    var str2="<head><title>Link</title></head>"
    	+"<body><b>"+str+"</b></body>";
	open_popup_for_content(str2);
}

function getWCX() {
	return document.body.scrollLeft + document.body.clientWidth/2;
}

function getWCY() {
	return document.body.scrollTop + document.body.clientHeight/2;
}

