﻿function logOut(lang, currentWindow) {
    if (ajax.ajaxFunction('../../_ajax.php?logout')) {
        if (currentWindow) {
            currentWindow.location = '/index.php?lang=' + lang + '';
        } else {
            window.location = '/index.php?lang=' + lang + '';
        }
	}
}

function hideElement(elementID) {
	var element			=	document.getElementById(elementID);
	element.className	=	"invisible";
}

function urlencode(s){
    s = encodeURIComponent(s);
    return s.replace(/~/g, '%7E').replace(/%20/g, '+');
}

function getMouseXY_Realative(e) {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		// posx and posy contain the mouse position relative to the document
		// Do something with this information
		
		var mouseCords = Array();
		mouseCords[0]	=	posx;
		mouseCords[1]	=	posy;
		
		return mouseCords;
}

function getMouseXY(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX - window.pageXOffset;
		posy = e.pageY - window.pageYOffset;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX; 
		posy = e.clientY;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	
	var mouseCords = Array();
	mouseCords[0]	=	posx;
	mouseCords[1]	=	posy;
	
	return mouseCords;
}


function deleteInputBox(value, validation_string) {
	if (value == "") {
		return false;
	} else if(value == validation_string) {
		return false;
	} else {
		return true;
	}
}

function getLangFromUrl(url) {
	if (url.search(/lang/) != -1) {
		if (url.search(/lang=cz/) != -1)
			return "cz";
		else if (url.search(/lang=sk/) != -1)
			return "sk";
		else if (url.search(/lang=eng/) != -1)
			return "eng";
		else if (url.search(/lang=de/) != -1)
			return "de";
	}
	return false;
}

function getLangStringFromNumb(numb) {
	if (numb == "0")
		return "cz";
	else if (numb == "1")
		return "sk";
	else if (numb == "2")
		return "eng";
	else if (numb == "3")
		return "de";
}

function clearInnerHtml(id) {
	document.getElementById(id).innerHTML = "";
}

function isInteger (s) {
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true;
	} else{
		return false;
	}
}

function removeSpaces(string) {
    return string.split(' ').join('');
}


function sendReportMsg() {
    var msg = document.getElementById('report_msg_id');
    var form = document.getElementById('formular_report_bug');
    var email = document.getElementById('rEmailID');
    var recCode = document.getElementById('recCode');

    if (removeSpaces(msg.value) != "") {
        if (email.value.indexOf(".") > 0 && email.value.indexOf("@") > 0 && email.value != "") {
            if (ajax.ajaxFunction('../recaptcha/verifier.php?code=' + recCode.value + '')) {
                form.submit();
            } else {
                //alert("Zly opsany text!");
            }
        } else {
            //alert("Zly email!");
        }
    } else {
        //alert("Nezadan vzkaz!");
    }
}

function goDefaultColor(itemID) {
	var item = document.getElementById(itemID);
	var classValue = item.className;
	
	if (isRedColor(itemID)) {
		var classArray = classValue.split(' ');
		classValue = "";
		for(var index=0; index<classArray.length; index++) {
			if (classArray[index] == "color_red") ;
			else
				classValue += " "+classArray[index];
		}
		item.className = classValue;
	}
	
	if (classValue.search(/color_gray/) != -1) {
		var classArray = classValue.split(' ');
		classValue = "";
		for(var index=0; index<classArray.length; index++) {
			if (classArray[index] == "color_gray") ;
			else
				classValue += " "+classArray[index];
		}
		item.className = classValue;
	}
}

function getValueFromOneIDtoOther(fromtID, toID) {
	document.getElementById(toID).value = document.getElementById(fromtID).value;
}

function unCheckedRadio(itemsIdArray) {
	for(var index=0; index<itemsIdArray.length; index++) {
		document.getElementById(itemsIdArray[index]).checked = false;
	}
}

function goRedColor(itemID) {
	document.getElementById(itemID).className += " color_red";
}

function isRedColor(itemID) {
	var item = document.getElementById(itemID);
	var classValue = item.className;
	if (classValue.search(/color_red/) != -1)
		return true;
	else
		return false;
}

function isDefaultColor(itemID) {
}

function disableItem(itemID, item_label) {
	var item = document.getElementById(itemID);
	var label = document.getElementById(item_label);

	item.disabled = true;
	item.className += " color_gray";
	label.className += " color_gray";
}

function enableItem(itemID, item_label) {
	var item = document.getElementById(itemID);
	item.disabled = false;
	goDefaultColor(itemID);
	goDefaultColor(item_label);
}

function isEmpty(itemID) {
	var item = document.getElementById(itemID);
	
	if (item.value == "")
		return true;
	else
		return false;
}

function isValidEmail(str, itemID) {
	if ((str.indexOf(".") > 0) && (str.indexOf("@") > 0) || str == "") {
		goDefaultColor(itemID+'-label');
		return true;
	}
	else {
		//alert(ajax.ajaxFunction('', getLangFromUrl(window.location.href)));
		goRedColor(itemID+'-label');
		return false;
	}
}

function check_exe_id_format (id) {
	var item = document.getElementById(id);
	var value = item.value;
	if (value == "")
		;
	else {
		if (isInteger(value)) {
			goDefaultColor(id);
		} else {	
			goRedColor(id);
		}
	}
}

function hasRightFormat(type, itemID) {
	var item = document.getElementById(itemID);
	var value = item.value;
	if (value == "")
		goDefaultColor(itemID+'-label');
	else {
		// PHONE, MOBILE, FAX
		if (type == "contact_number") {
			var string = value.replace(/\s+/g,'');
			for (i = 0; i < string.length; i++) {
				// OK
				if ((string.charAt(i) >= "0") && (string.charAt(i) <= "9") || (string.charAt(i) == "/") || (string.charAt(i) == "\\") || (string.charAt(i) == "+")) {
					goDefaultColor(itemID+'-label');
				// ERROR
				} else {
					goRedColor(itemID+'-label');
					//alert(ajax.ajaxFunction('', getLangFromUrl(window.location.href)));
				}
			}
		} else if (type == "ico_dic") {
			var value = document.getElementById(itemID).value;
			string = value.replace(/\s+/g,'');
			for (i = 0; i < string.length; i++) {
				if (isDigit(string.charAt(i))) {
					goDefaultColor(itemID+"-label");
				}
				else {
					goRedColor(itemID+"-label");
				}
			}
		} else if (type == "pc") {
		} else if (type == "birth_number") {
			for (i = 0; i < value.length; i++) {
				if ((value.charAt(i) >= "0") && (value.charAt(i) <= "9") || (value.charAt(i) == "/") || (value.charAt(i) == "\\"))
					goDefaultColor(itemID+"-label");
				else{
					goRedColor(itemID+"-label");
				} 
			}		
		} else {}
	}
}

/**
 * @param array data itemsID
 * 
 */
function hasCorrectData(data) {
	var correct = true;
	for(var index=0; index<data.length; index++) {
		if (isRedColor(data[index]+'-label')) {
			correct = false;
			break;
		} else {}
	}
	if (correct)
		return true;
	else
		return false;
}

function addValueToURL(name) {
  var uri = parseUri(window.location.href);
  if (uri['anchor'] != "")
    return uri['directory']+uri['file']+"?"+uri['query']+"&"+name+"#"+uri['anchor'];
  else 
    return uri['directory']+uri['file']+"?"+uri['query']+"&"+name;
}

function removeValueFromURL(url, name) {
 	var value 				= grapValueFromURL(name);
	var string_length		= name.length +  value.length + 1;	// +1 koli =
	var startIndex			= url.indexOf(name)-1;	// -1 koli & or ?
	var newUrl 				= url.substring(0, startIndex) +  url.substring(startIndex+string_length, url.length);
	
	return newUrl;
}

/**
 * 
 * @param name
 * @return
 */
function grapValueFromURL(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


function checkActiveSession() {
    if (ajax.ajaxFunction('../_ajax.php?is_session_active')) {
        window.close();
    } else { 
    }
}
