
/*

ALL RIGHTS RESERVED

www.rolts.eu

*/

function loadFunction(){
	loadImage();
	fixHeight();
	showBottomPageNav();
//	ifIE6();
}
function loadAdminFunction(){
	loadImage();
	showBottomPageNav();
//	ifIE6();
}

function OnChange(link,vars,name){
	var url = link;
	var selIndex = vars.value;
	
	var back_link = url+'&'+name+'='+selIndex;
	
	window.location.href = back_link;
}

function setLoginLink(e){
	var login_link = document.getElementById("_LOGIN_URL").value;
	var oEvent = window.event ? window.event : e;
	if (oEvent.keyCode == 36){
		window.location.href = login_link;
	}
}


function submitenter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13){
		myfield.form.submit();
		return false;
   }else
   return true;
}

function clicked(txt){
	if(txt == null){
		txt='Mina siin ....';
	}
	return alert(txt);
}

// hides elements :: puts 1. element visible
function hideElements(elems,showDef,style_deact,style_act){
	element = elems.split('|');

	for(var i=0;i<element.length;i++){
		cur = element[i];
		if(cur != null && cur.length > 1){
			cur_elem = document.getElementById(cur);
			head_elem = document.getElementById("header_"+cur);
			if(cur_elem != null){
				// hide all element
				cur_elem.style.display = "none";
				/*	-> show all element
				if(cur_elem.style.display == "show") {
					cur_elem.style.display = "none";
				}else{
					cur_elem.style.display = "block";
				}
				*/

			}
			if(head_elem != null){
				head_elem.className = style_deact;
			}
		}
	} 
	// by default show 1 element content
	if(showDef){
		def_elem = document.getElementById(element[0]);
		head_elem = document.getElementById("header_"+element[0]);
		if(def_elem != null){
			def_elem.style.display = "block";
		}
		if(head_elem != null){
			head_elem.className = style_act;
		}
	}
}
function showElementContent(elem,me,style_deact,style_act){
	hideElements(elem,0,style_deact,style_act);
	me_elem = document.getElementById(me);
	head_elem = document.getElementById("header_"+me);
	if(me_elem != null && head_elem != null){
		me_elem.style.display = "block";
		head_elem.className = style_act;
	}
}

function loadImage() {
  if(document.images){ 
	if(!document.picture){
		document.picture=new Array();
	}
	var i,j=document.picture.length;

	for(i=0; i<loadImage.arguments.length; i++)
		if (loadImage.arguments[i].indexOf("#")!=0){ 
			document.picture[j]=new Image; 
			document.picture[j++].src=loadImage.arguments[i];
		}
	}
}

function redirect(url){
	window.location=url;
}

function fixHeight(){
	var size = false
	var div1 = document.getElementById("body");
	var div1a = document.getElementById("main_left");
	var div2 = document.getElementById("body_left");
	var div3 = document.getElementById("body_right");
	var div_admin = document.getElementById("body_admin");

	if(div1 > div1a){
		size = div1;
	}else{
		size = div1a;
	}

	if(div2 != null){
		div2.style.height = size.offsetHeight+"px";
	}
	if(div3 != null){
		div3.style.height = size.offsetHeight+"px";
	}
	if(div_admin != null){
		div_admin.style.height = size.offsetHeight+"px";
	}
	//div1.style.height = size.offsetHeight+"px";
}

function ifIE6(){
	bName = navigator.appName; //1
	bVer = parseInt(navigator.appVersion); //2
	aVer= navigator.appVersion.substring(1,0);

	//if (bName == "Microsoft Internet Explorer" && aVer >= "4"){ 
	if (bName == "Microsoft Internet Explorer" && aVer < "4"){
		document.getElementById("sisu_layout").innerHTML = 'Please upgrade your <A HREF="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx">Internet Explorer version</A> to view webpage correctly!';
	} 
}

function showBottomPageNav(){
	var i;
	var nav_nr_2 = document.getElementById('bottom_navigation_layout');
	var content = document.getElementById("body");
	// nav_nr_2.length
	if(content != null){
		if(content.offsetHeight>'450'){	   // in css #body min-height
			nav_nr_2.style.display = "";
		}
	}
}

function clicker(pic_file,w,h,close){
	var thediv=document.getElementById('displaybox');
	if(thediv.style.display == "none"){
		thediv.style.display = "";
		/* width="+w+" height="+h+" */
		thediv.innerHTML = "<div id='displaybox_box'><IMG SRC="+pic_file+" > <br><br><a href='#' onclick='return clicker();'>"+close+"</a></div>";
	}else{
		thediv.style.display = "none";
		thediv.innerHTML = '';
	}
	return;
}


/* items roller function */
 function toggleDivItemContent(divID,divDir,divStep){

	var div = document.getElementById(divID);
	var maxDivWidth = div.offsetHeight;
	var curPos = div.offsetTop;
	var newPos;
	
	if(divDir == '+'){
		newPos = curPos+divStep;
		if(newPos <= 0){
			div.style.top = newPos+'px';
			/*document.getElementById("items_toolbar_nav_bottom").style.display = "";*/
		}else{
			/*document.getElementById("items_toolbar_nav_top").style.display = "none";*/
		}
	}else{
		newPos = curPos-divStep;
		if(newPos >= -(maxDivWidth)+(300)){
			div.style.top = newPos+'px';
			/*document.getElementById("items_toolbar_nav_top").style.display = "";*/
		}else{
			/*document.getElementById("items_toolbar_nav_bottom").style.display = "none";*/
		}
	}
	/*alert(maxDivWidth+'::'+curPos+'::'+newPos); */
}


function set_AJAX(target,county_id,city_id,array) {
	var make_query = false;
	var not_empty_1 = array[0];
	var not_empty_2 = array[1];

	if(county_id == false && not_empty_1 == false && city_id == false && not_empty_2 == false){
		make_query = true;
	}else if(county_id != false && not_empty_1 != false && city_id == false && not_empty_2 == false){
		make_query = true;
	}else if(county_id != false && not_empty_1 != false && city_id != false && not_empty_2 != false){
		make_query = true;
	}

	if(make_query == true){
		get_county(target,county_id,city_id);
	}
}


function get_county(target,county_id,city_id) {
    if (window.XMLHttpRequest) { 
        http = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
        http = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    handle = document.getElementById(target); 
    var url = "get_county.php"; 
	var alphaNRExp = /^[0-9]+$/;
	var alphaExp = /^[0-9a-zA-Z]+$/;

	if(handle.value.match(alphaExp)){
		var fullurl = url + '?searchID=get_county_data&county_id=' + encodeURIComponent(county_id) + '&city_id=' + encodeURIComponent(city_id); 
		http.open("GET", fullurl, true); 
		http.send(null);
		// http.onreadystatechange = statechange_county; 
		// OR
		http.onreadystatechange = function () {
			statechange_county(target);
		};
	}else{
		return;
	}
}
function statechange_county(target) {
    if (http.readyState == 4) { 
        var xmlObj = http.responseXML; 
        var cource_contact = xmlObj.getElementsByTagName('result').item(0).firstChild.data; 
		if(cource_contact != 'error'){
			array_contact = cource_contact.split(';');
			for(i=0;i<array_contact.length;i++){
				contact_name = array_contact[i];				
				if(contact_name.length>0){
					var optn = document.createElement("OPTION");
					contact = contact_name.split('|');
					optn.text = contact[0];
					optn.value = contact[1];
					document.getElementById(target).options.add(optn);
				}
			}
			// enable drop down
			document.getElementById(target).disabled = false;
		}
    } 
}
function empty_county(target){
/*
document.getElementById(\"county\").value
OR
this.options[this.selectedIndex].value
*/
	var old = document.getElementById(target).options[0].text;
	// remove old one
	var old_contact = document.getElementById(target);
	while(old_contact.hasChildNodes()){
		old_contact.removeChild(old_contact.childNodes[0])
	}

	var optn = document.createElement("OPTION");
	contact = contact_name.split('|');
	optn.text = old;
	optn.value = 0;
	document.getElementById(target).options.add(optn);	
	document.getElementById(target).selectedIndex=0;

	document.getElementById(target).disabled = true;
}

function setCalendarDatum(target, datum){
	window.opener.document.getElementById(target).value = datum;
	window.close();
}
function getCalendarDatum(target,paste,link){
	// inserted date
	var old_datum = window.opener.document.getElementById(target).value;

	var current_date = new Date();
	var current_month = current_date.getMonth() + 1;
	var current_day = current_date.getDate();
	var current_year = current_date.getFullYear();

	var datum_element = old_datum.split('-');

	var datum_day = datum_element[0];
	var datum_month = datum_element[1];
	var datum_year = datum_element[2];

	if(datum_month == null){
		datum_month	= current_month;
	}
	if(datum_year == null){
		datum_year	= current_year;
	}
	if(datum_day == null){
		datum_day = current_day;
	}

	if((datum_month != current_month || datum_year != current_year) || datum_day != current_day){
		//alert(link+'&month='+datum_month+'&year='+datum_year+'&day='+datum_day);
		window.location.replace(link+'&month='+datum_month+'&year='+datum_year+'&day='+datum_day+'&redir=1');
	}	
}















/* javascript shortcuts */

/*
var shortcutListener = {

    listen: true,

    shortcut: null,
    combination: '',
    lastKeypress: 0,
    clearTimeout: 2000,

    // Keys we don't listen 
    keys: {
        KEY_BACKSPACE: 8,
        KEY_TAB:       9,
        KEY_ENTER:    13,
        KEY_SHIFT:    16,
        KEY_CTRL:     17,
        KEY_ALT:      18,
        KEY_ESC:      27,
        KEY_SPACE:    32,
        KEY_LEFT:     37,
        KEY_UP:       38,
        KEY_RIGHT:    39,
        KEY_DOWN:     40,
        KEY_DELETE:   46,
        KEY_HOME:     36,
        KEY_END:      35,
        KEY_PAGEUP:   33,
        KEY_PAGEDOWN: 34
    },

    init: function() {
        if (!window.SHORTCUTS) return false;
        this.createStatusArea();
        this.setObserver();
    },

    isInputTarget: function(e) {
        var target = e.target || e.srcElement;
        if (target && target.nodeName) {
            var targetNodeName = target.nodeName.toLowerCase();
            if (targetNodeName == "textarea" || targetNodeName == "select" ||
                (targetNodeName == "input" && target.type &&
                    (target.type.toLowerCase() == "text" ||
                         target.type.toLowerCase() == "password"))
                             )  {
                return true;
            }
        }
        return false;
    },

    stopEvent: function(event) {
        if (event.preventDefault) {
            event.preventDefault();
            event.stopPropagation();
        } else {
            event.returnValue = false;
            event.cancelBubble = true;
        }
    },


    // shortcut notification/status area
    createStatusArea: function() {
        var area = document.createElement('div');
        area.setAttribute('id', 'shortcut_status');
        area.style.display = 'none';
        document.body.appendChild(area);
    },

    showStatus: function() {
        document.getElementById('shortcut_status').style.display = '';
    },

    hideStatus: function() {
        document.getElementById('shortcut_status').style.display = 'none';
    },

    showCombination: function() {
        var bar = document.getElementById('shortcut_status');
        bar.innerHTML = this.combination;
        this.showStatus();
    },

    // This method creates event observer for the whole document
    // This is the common way of setting event observer that works 
    // in all modern brwosers with "keypress" fix for
    // Konqueror/Safari/KHTML borrowed from Prototype.js
    setObserver: function() {
        var name = 'keypress';
        if (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || document.detachEvent) {
          name = 'keydown';
        }
        if (document.addEventListener) {
            document.addEventListener(name, function(e) {shortcutListener.keyCollector(e)}, false);
        } else if (document.attachEvent) {
            document.attachEvent('on'+name, function(e) {shortcutListener.keyCollector(e)});
        }
    },

    // Key press collector. Collects all keypresses into combination 
    // and checks it we have action for it
    keyCollector: function(e) {
        // do not listen if no shortcuts defined
        if (!window.SHORTCUTS) return false;
        // do not listen if listener was explicitly turned off
        if (!shortcutListener.listen) return false;
        // leave modifiers for browser
        if (e.altKey || e.ctrlKey || e.metaKey) return false;
        var keyCode = e.keyCode;
        // do not listen for Ctrl, Alt, Tab, Space, Esc and others
        for (var key in this.keys) {
            if (e.keyCode == this.keys[key]) return false;
        }
        // do not listen for functional keys
        if (navigator.userAgent.match(/Gecko/)) {
            if (e.keyCode >= 112 && e.keyCode <=123) return false;
        }
        // do not listen in input/select/textarea fields
        if (this.isInputTarget(e)) return false;
        // get letter pressed for different browsers
        var code = e.which ? e.which : e.keyCode
        var letter = String.fromCharCode(code).toLowerCase();
        if (e.shiftKey) letter = letter.toUpperCase();
        if (shortcutListener.process(letter)) shortcutListener.stopEvent(e);
    },

    // process keys
    process: function(letter) {
        if (!window.SHORTCUTS) return false;
        if (!shortcutListener.listen) return false;
        // if no combination then start from the begining
        if (!shortcutListener.shortcut) { shortcutListener.shortcut = SHORTCUTS; }
        // if unknown letter then say goodbye
        if (!shortcutListener.shortcut[letter]) return false
        if (typeof(shortcutListener.shortcut[letter]) == "function") {
            shortcutListener.shortcut[letter]();
            shortcutListener.clearCombination();
        } else {
            shortcutListener.shortcut = shortcutListener.shortcut[letter];
            // append combination
            shortcutListener.combination = shortcutListener.combination + letter;
            if (shortcutListener.combination.length > 0) {
                shortcutListener.showCombination();
                // save last keypress timestamp (for autoclear)
                var d = new Date;
                shortcutListener.lastKeypress = d.getTime();
                // autoclear combination in 2 seconds
                setTimeout("shortcutListener.clearCombinationOnTimeout()", shortcutListener.clearTimeout);
            };
        }
        return true;
    },

    // clear combination
    clearCombination: function() {
        shortcutListener.shortcut = null;
        shortcutListener.combination = '';
        this.hideStatus();
    },

    clearCombinationOnTimeout: function() {
        var d = new Date;
        // check if last keypress was earlier than (now - clearTimeout)
        // 100ms here is used just to be sure that this will work in superfast browsers :)
        if ((d.getTime() - shortcutListener.lastKeypress) >= (shortcutListener.clearTimeout - 100)) {
            shortcutListener.clearCombination();
        }
    }
}
*/
/*

<script type="text/javascript">
    var SHORTCUTS = {
        'h': function() { alert('Help!'); },
        'f': {
            'o': {
                'o': function() { alert('"foo" has been typed!'); }
            }
        },
        'b': {
            'a': {
                'R': function() { alert('"baR" has been typed!'); },
                'z': function() { alert('"baz" has been typed!'); }
            }
        }
    }
</script>
<body onload="shortcutListener.init();">
*/

/* end of javascript shortcuts */
