﻿// Genio.NET
// Written By Peter Širka
// JavaScript File

var times = 0;

function getID(id){
    return document.getElementById(id);
}

function check_integer(n){return RegExp ( "^[-+]?[0-9]+$" ).test(n)}
function check_string(s){return RegExp ( "^[a-zA-Z]+$" ).test(s)}
function check_alfanum_string(s){return RegExp ( "^[a-zA-Z0-9]+$" ).test(s)}
function check_date(s){return RegExp ( "[0-9]{2,2}.[0-9]{2,2}.[0-9]{4,4}$" ).test(s)}
function check_time(s){return RegExp ( "^[012][0-9]:[0-5][0-9]$" ).test(s)}
function check_email(s){return RegExp ( "^[a-zA-Z0-9-_.]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" ).test(s)}
function check_url(s){var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; return regexp.test(s)}
function check_sqldate(s){reg_date = /^\d{4}-\d{2}-\d{2}$/; return reg_date.test(s)}
function check_hex(n) {return (n<16 ? '0' : '' ) + n.toString(16)}

function hi(f){
 f = f.elements;
 f.result.value = highlight( f.query.value ) + 'x ' + f.query.value;
 return false}

function highlight(s,o){
  if(!s){return 0}
  var d = window.document;
  s = s.replace( /([\\|^$()[\]{}.*+?])/g, '\\$1' );
  o = [ o || d.documentElement || d.body ];
  var r = new RegExp( s, 'gi'),
    h = d.createElement( 'span' ), i = 0, j, k, l, m, n=0, t;
  h.style.color = 'yellow';
  h.style.backgroundColor = 'red'
  times++;
  do {
    m = o[i];
    if( m.nodeType===3 ) {
      r.lastIndex = 0;
      l = r.exec(m.nodeValue);
      if( l !== null ) {
        k = l[0].length;
        if( r.lastIndex > k ) {
          m.splitText( r.lastIndex - k );
          m = m.nextSibling}
        if( m.nodeValue.length > k ) {
          m.splitText(k);
          o[i++] = m.nextSibling}
        t = h.cloneNode( true );
        t.appendChild( d.createTextNode( l[0] ) );n++;
        m.parentNode.replaceChild( t, m )}} else {
      j = m.childNodes.length;
      while ( j ) { o[i++] = m.childNodes.item( --j )}}
  } while( i-- );
  return n}

function unhighlight(s,o){
 var d = window.document;
 s = s.replace(/([\\|^$()[\]{}.*+?])/g, '\\$1').split( /\s+/ ).join( '|' );
 o = o || d.documentElement || d.body;
 var a = o.getElementsByTagName( 'span' ), i = a.length, j,
  re = new RegExp( '^' + s + '$', 'i' );
 while( i-- ) {
  j = a[i].firstChild;
  if( j ) {
   if( j.nodeType===3 && re.test( j.nodeValue ) ) {
    a[i].parentNode.replaceChild( d.createTextNode( j.nodeValue ), a[i] )}}}
 return false}

var FIND_TEXT1 = "";
var FIND_TEXT2 = "";
var FIND_ID;

function FINDTEXT(){
  clearTimeout(FIND_ID);
  highlight(FIND_TEXT1);
  highlight(FIND_TEXT2)}

function HIGHLIGHT(FIND1, FIND2){
  FIND_TEXT1 = FIND1;
  FIND_TEXT2 = FIND2;
  if ((FIND1 == null) || (FIND1 == "")) {return}
  if ((FIND2 == null) || (FIND2 == "")) {return}
  FIND_ID = setTimeout("FINDTEXT()", 2000)}


function png(b){
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
if ((version >= 5.5) && (document.body.filters) && (version < 7)) 
{
   for(var i=0; i<document.images.length; i++)
   {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (img.className) 
      {        
        if (img.className == "HomePageLogo")
            continue;
      }
      
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
      {
         if ((b) && (img.id == "logo")) continue;
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML
         i = i-1
      }
   }
}
}

function page_submenu(id)
{
    if(!getID(id))
        return;
                
   getID(id).className = getID(id).className + "Down";
}

function send(url,target,posttrue,params,values){
      var forma = document.createElement("FORM");
      forma.method = posttrue ? "POST" : "GET";
      forma.action = url;
      //forma.target = "_self";
      document.body.appendChild(forma);

      for(var i = 0; i < params.length; i++)
      {
            if(params[i] == "")
                continue;
            if(values[i] == "")
                continue;
            var input = document.createElement("INPUT");
            input.type = "hidden";
            input.name = params[i];
            input.value = values[i];
            forma.appendChild(input);      
      }

      forma.submit();
      document.body.removeChild(forma);
      forma = null;
      input = null;
}

function ToBasket(zIDPRODUKT, zPOCET)
{
    var params = new Array(3);
    var values = new Array(3);
    
    params[0] = "TYPE";
    values[0] = "add";
    
    params[1] = "ID";
    values[1] = zIDPRODUKT.toString();
    
    params[2] = "COUNT";
    values[2] = zPOCET.toString();
    
    params[3] = "PAGE";
    values[3] = window.location.pathname;
    send("/SiteTools/iobchod-data.ashx", "_self", true, params, values);
}

function WindowOpen(URL, dialogWidth, dialogHeight, CommandPlus){
    if (CommandPlus == '')
        CommandPlus='scrollbars=no,toolbar=no,resizable=no,status=no,location=no';
        
    var iTop  = (screen.height - dialogHeight) / 2 ;
	var iLeft = (screen.width  - dialogWidth)  / 2 ;
    return window.open(URL, '', 'width='+dialogWidth+',height='+dialogHeight+',left='+iLeft+',top='+iTop+','+CommandPlus);
}   
   

function Printer()
{
    var OpenWindow = WindowOpen("/printer.html", 700, 450, "");
    if (OpenWindow.focus())
        OpenWindow.focus();
}

var old_subponuka;

function subponuka(id, show)
{
    if (getID(old_subponuka))
    {
        getID(old_subponuka).style.display = "none";
        if (getID(old_subponuka.replace("sub", "ctl00_menu")).className == "menudown")
            getID(old_subponuka.replace("sub", "ctl00_menu")).style.color = "white";
        else
        {
            getID(old_subponuka.replace("sub", "ctl00_menu")).style.fontWeight = "normal";
            getID(old_subponuka.replace("sub", "ctl00_menu")).style.color = "#FF6666";
        }
    }

    if (!id) return;    
    if (!getID(id)) return;

    LogoClose();
            
    if (show)
    {
        getID(id).style.display = "block";
        getID(id.replace("sub", "ctl00_menu")).style.fontWeight = "bold";
        getID(id.replace("sub", "ctl00_menu")).style.color = "white";
        old_subponuka = id;
    }
    else
    {
        old_subponuka = "";
        getID(id).style.display = "none";
        if (getID(old_subponuka.replace("sub", "ctl00_menu")).className == "menudown")
            getID(old_subponuka.replace("sub", "ctl00_menu")).style.color = "white";
        else
        {
            getID(old_subponuka.replace("sub", "ctl00_menu")).style.color = "#FF6666";            
            getID(id.replace("sub", "ctl00_menu")).style.fontWeight = "normal";
        }
    }
}

function searchSend()
{
    var txt = getID("searchInput");
    
    if (!txt)
        return;
    
    if ((txt.value == "") || (txt.value.length < 5))
    {
        txt.style.backgroundColor = "#FFCCFF";
        alert("Vyhľadávač potrebuje k vyhľadávaniu minimálne 5 znakov.");
        return;
    }
    
      var forma = document.createElement("FORM");
      forma.method = "GET";
      forma.action = "/site/search/";
      document.body.appendChild(forma);

      var input = document.createElement("INPUT");
      input.type = "hidden";
      input.name = "search";
      input.value = txt.value;
      forma.appendChild(input);      

      forma.submit();
      document.body.removeChild(forma);
      forma = null;
      input = null;
}

function myKeyPres(e){
    if(e.keyCode==13){return searchSend()}
}

function DecodeAdress()
{        
    for (var i = 0; i < 20; i++)
        if (getID("adress" + i.toString()))
        {
            var Adress = getID("adress" + i.toString()).innerHTML.replace(/\(at\)/ig, "@").replace(/\(dot\)/, ".");
            Adress = '<a href="mailto:' + Adress + '" style="color:White">' + Adress + "</a>";
            getID("adress" + i.toString()).innerHTML = Adress;
        }
}

function ShowBasket()
{
}

HttpRequest.prototype.MS_PROGIDS = new Array(
	"Msxml2.XMLHTTP.7.0",
	"Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0",
	"Msxml2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP");
HttpRequest.prototype.READY_STATE_UNINITIALIZED = 0;
HttpRequest.prototype.READY_STATE_LOADING       = 1;
HttpRequest.prototype.READY_STATE_LOADED        = 2;
HttpRequest.prototype.READY_STATE_INTERACTIVE   = 3;
HttpRequest.prototype.READY_STATE_COMPLETED     = 4;
HttpRequest.prototype.successCallback = null;
HttpRequest.prototype.failureCallback = null;
HttpRequest.prototype.url             = null;
HttpRequest.prototype.username        = null;
HttpRequest.prototype.password        = null;
HttpRequest.prototype.requestHeaders  = new Array();
HttpRequest.prototype.status          = null;
HttpRequest.prototype.statusText      = null;
HttpRequest.prototype.responseXML     = null;
HttpRequest.prototype.responseText    = null;
HttpRequest.prototype.abort                 = HttpRequestAbort;
HttpRequest.prototype.setRequestHeader      = HttpRequestSetRequestHeader;
HttpRequest.prototype.clearRequestHeaders   = HttpRequestClearRequestHeaders;
HttpRequest.prototype.get                   = HttpRequestGet;
HttpRequest.prototype.post                  = HttpRequestPost;
HttpRequest.prototype.initiateRequest       = HttpRequestInitiateRequest;
HttpRequest.prototype.getResponseHeader     = HttpRequestGetResponseHeader;
HttpRequest.prototype.getAllResponseHeaders = HttpRequestGetAllResponseHeaders;

function HttpRequest(id){
    this.id = id;
	this.xmlHttpRequest = null;
	if (window.XMLHttpRequest != null)
		this.xmlHttpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null){
		var success = false;
		for (var i = 0; i < HttpRequest.prototype.MS_PROGIDS.length && !success; i++){
			try{
				this.xmlHttpRequest = new ActiveXObject(HttpRequest.prototype.MS_PROGIDS[i]);
				success = true}
			catch (ex){}}}

	if (this.xmlHttpRequest == null){
		alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");
		return}}

function HttpRequestAbort(){this.xmlHttpRequest.abort()}

function HttpRequestSetRequestHeader(name, value){
	for (var i = 0; i < this.requestHeaders.length; i++){
		var pair = this.requestHeaders[i].split("\n");
		if (pair[0].toLowerCase() == name.toLowerCase()){
			this.requestHeaders[i] = name + "\n" + value;
			return}}
	var n = this.requestHeaders.length;
	this.requestHeaders.push(name + "\n" + value)}

function HttpRequestClearRequestHeaders(){this.requestHeaders = new Array()}

function HttpRequestGet(){this.initiateRequest("GET", null)}
function HttpRequestPost(data){this.initiateRequest("POST", data)}
function HttpRequestGetResponseHeader(name){return this.xmlHttpRequest.getResponseHeader(name)}
function HttpRequestGetAllResponseHeaders(){return this.xmlHttpRequest.getAllResponseHeaders()}

function HttpRequestInitiateRequest(method, data){
	this.status       = null;
	this.statusText   = null;
	this.responseText = null;
	this.responseXML  = null;
	var refObj = this;
	this.xmlHttpRequest.onreadystatechange =
		function(){
			refObj.readyState = refObj.xmlHttpRequest.readyState
			if (refObj.readyState == HttpRequest.prototype.READY_STATE_COMPLETED){
				refObj.status       = refObj.xmlHttpRequest.status;
				refObj.statusText   = refObj.xmlHttpRequest.statusText;
				refObj.responseText = refObj.xmlHttpRequest.responseText;
				refObj.responseXML  = refObj.xmlHttpRequest.responseXML;
				if (refObj.status == 200){if (refObj.successCallback != null)refObj.successCallback(refObj)}
				else{
					if (refObj.failureCallback != null)
						refObj.failureCallback(refObj)}}
		}
	var url = this.url;
	if (this.queryString != null)
		url = url + "?" + this.queryString;
	this.xmlHttpRequest.open(method, url, true, this.username, this.password);
	for (var i = 0; i < this.requestHeaders.length; i++){
		var pair = this.requestHeaders[i].split("\n");
		this.xmlHttpRequest.setRequestHeader(pair[0], pair[1])}
	this.xmlHttpRequest.send(data)}

function ajax_error(httpRequest)
{
   alert("Chyba, HTTP: " + httpRequest.status + " " + httpRequest.statusText + ".");
}

function ajax_call(url)
{
    var ajax_id = new HttpRequest();
    ajax_id.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax_id.failureCallback = ajax_error;
    ajax_id.url = url;
    return ajax_id;
}

var AjaxId;
var AjaxTimer;
var AjaxTop;

function GetDescription_Result(http)
{
    var obj = getID("AjaxDescription");
    obj.innerHTML = http.responseText;
    obj.style.display = "block";
    obj.style.marginTop = AjaxTop.toString() + "px";          
}

function SetDescriptionHide()
{
    if (!AjaxId)
        return;
    var obj = getID("Description" + AjaxId.toString());
    if (!obj)
        return;
    obj.style.display = "none";
}

function GetDescriptionTimer()
{
    clearTimeout(AjaxTimer);
    var ajax_id = ajax_call("/SiteTools/ajax.ashx?zIDPRODUKT=" + AjaxId.toString());
    ajax_id.successCallback = GetDescription_Result;
    ajax_id.get();
}

function GetDescription(id,top)
{
    SetDescriptionHide();
    AjaxId = id;
    
    if (getID("Description" + id.toString()).innerHTML == "")
        return;
    getID("Description" + id.toString()).style.display = "block";

//    AjaxId = id;
//    AjaxTimer = setTimeout("GetDescriptionTimer()", 1000);
}

var LogoId;

function LogoOnHover(id)
{
    for (var i = 0; i < 10; i++)
    {
        var o = getID("znacka" + i.toString());
        if (!o)
            continue;
        o.className = "HomePageLogo";
    }
    LogoId = setTimeout("LogoClose()", 4000);
    var IMAGE = getID("znacka" + id.toString());
    IMAGE.className = "HomePageLogoSel";
    subponuka(null, false);
}

function LogoClose()
{
    clearTimeout(LogoId);
    for (var i = 0; i < 10; i++)
    {
        var o = getID("znacka" + i.toString());
        if (!o)
            continue;
        o.className = "HomePageLogo";
    }    
}
