/////////////////////////////////////////////////////////////////////////////////
//
// Mari.hr Javascript
//
//  (c) 2009 by Abacus, info ath abacus doth hr
//  
//  v1.01 22.07.2009. DSalopek
//
/////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------
// globalne varijable
//-------------------------------------------------------------------------------

var xMENU='NAS';
var xWSPCID=1631;
var xNS = (navigator.appName.indexOf("Netscape") != -1);
var xDOC = document;
var xAJAXURI;

//-------------------------------------------------------------------------------
// status
//-------------------------------------------------------------------------------

window.defaultStatus='MARI.hr';
window.name='MR';

function S(pMSG) {
  if (pMSG!='') { pMSG='MARI.hr: '+pMSG }
  window.status=pMSG;
  return(true);
}


//-------------------------------------------------------------------------------
// AJAX
//-------------------------------------------------------------------------------

function ajaxEvaluate (pTXT) {
  var rSCRIPT = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img');
  var tSCRIPT = '';
  while (tSCRIPT = rSCRIPT.exec(pTXT)) { eval(tSCRIPT[1]) }
}

function getDataReturnText (pURL, pTAG) { 
  var XMLHttpRequestObject = false; 
  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open('GET', pURL+'&DUMMY='+new Date().getTime()); 

    XMLHttpRequestObject.onreadystatechange = function() 
    { 
      if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) { 
          ajaxCallback(XMLHttpRequestObject.responseText, pTAG);
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null); 
  }
}


function ajaxCallback (pTXT,pTAG) {
  ajaxEvaluate(pTXT);
  if (pTAG==undefined) { 
    pTAG='BODY';
    if (xAJAXURI) { getDataReturnText('?JSRS=MENU-MAIN&'+xAJAXURI,'MENU-MAIN'); getDataReturnText('?JSRS=STRANA-RIGHT&'+xAJAXURI,'STRANA-RIGHT'); }
  }
  rNAVIG=/^<NAVIGATOR>(.*?)<\/NAVIGATOR>/m;
  var NAVIG=rNAVIG.exec(pTXT);
  if (NAVIG) {
    pTXT=pTXT.replace(rNAVIG,'');
  }
  if (xDOC.getElementById(pTAG)) { xDOC.getElementById(pTAG).innerHTML=pTXT; DekodirajURI(); }
  if (xDOC.getElementById('NAVIGATOR')) { xDOC.getElementById('NAVIGATOR').innerHTML=NAVIG[1] }
}


function Update(pTAG,pURI) {
  //if( hs ) hs.close();
  xURI=pURI;
  if (pTAG=='BODY') { 
    getDataReturnText('?JSRS=MENU-MAIN&'+pURI,'MENU-MAIN');
    getDataReturnText('?JSRS=STRANA-RIGHT&'+pURI,'STRANA-RIGHT');
    xSAL.PassAjaxResponseToFunction('?JSRS='+pTAG+'&'+pURI+'&DUMMY='+new Date().getTime(),'ajaxCallback');
  }
  else { getDataReturnText('?JSRS='+pTAG+'&'+pURI,pTAG) }
} 


function encodeHtml(pTXT) {
  var tRET=escape(pTXT);
  tRET=tRET.replace(/&/g, '%26');
  tRET=tRET.replace(/\//g,'%2F');
  tRET=tRET.replace(/=/g, '%3D');
  tRET=tRET.replace(/\?/g,'%3F');
  tRET=tRET.replace(/@/g, '%40');
  return(tRET);
} 


function getElementFormValues(pE) {
  var tURI='';
  var rTAG=/^(TABLE|TBODY|TR|TD|NOBR|CENTER|DIV|BR|A|B|SPAN|FIELDSET|LABEL|H1|H2|FONT|IMG)$/;
  for (var i=0; i<pE.childNodes.length; i++) {
    var tNODE=pE.childNodes[i];
    if (rTAG.test(tNODE.tagName))  { tURI+=getElementFormValues(tNODE) }

    if (tNODE.id) {
      if (tNODE.tagName=='INPUT') {
        if (tNODE.type=='password')      { tURI+=tNODE.id+'='+encodeHtml(tNODE.value)+'&' }
        else if (tNODE.type=='text')     { tURI+=tNODE.id+'='+encodeHtml(tNODE.value)+'&' }
        else if (tNODE.type=='hidden')   { tURI+=tNODE.id+'='+encodeHtml(tNODE.value)+'&' }
        else if (tNODE.type=='checkbox') { if (tNODE.checked) { tURI+=tNODE.id+'='+encodeHtml(tNODE.value)+'&' } }
      }
      else if (tNODE.tagName=='TEXTAREA') { tURI+=tNODE.id+'='+encodeHtml(tNODE.value)+'&' }
      else if (tNODE.tagName=='SELECT')   { tURI+=tNODE.id+'='+encodeHtml(tNODE.options[tNODE.selectedIndex].value)+'&' }
    }
    else if (tNODE.name && tNODE.tagName=='INPUT' && (tNODE.type=='radio') && tNODE.checked) {
      tURI+=tNODE.name+'='+encodeHtml(tNODE.value)+'&'
    }
  }
  return(tURI);
}


function UpdateForm(pTAG,pFORMID) {
  var tURI=getElementFormValues(getO(pFORMID));
  Update(pTAG,tURI);
}


//-------------------------------------------------------------------------------
// dekodiranja E-mailova
//-------------------------------------------------------------------------------

function DekodirajURI() {
  if (!xDOC.getElementsByTagName && !xDOC.createElement && !xDOC.createTextNode) return;
  var cvorovi=xDOC.getElementsByTagName('span');
  for(var i=cvorovi.length-1;i>=0;i--) {
    if (cvorovi[i].className=='DECODE') {
      var na=/ \(na\) /;
      var tocka=/ \(tocka\) /g;
      var cvor=xDOC.createElement('a');
      var URI=cvorovi[i].firstChild.nodeValue;

      URI = URI.replace(na,'@');
      URI = URI.replace(tocka,'.');
      cvor.setAttribute('href','mailto:'+URI);
      cvor.setAttribute('className','LINK');
      cvor.setAttribute('class','LINK');
      cvor.appendChild(xDOC.createTextNode(URI));
      
      var ispis = cvorovi[i].parentNode;
      for(var j=0;j<ispis.childNodes.length;j++)
        if (ispis.childNodes[j] == cvorovi[i]) {
          if (!ispis.replaceChild) return;
          ispis.replaceChild(cvor,ispis.childNodes[j]);
          break;
        }
    }
  }
}

//-------------------------------------------------------------------------------
// provjera formi
//-------------------------------------------------------------------------------

function CheckForm(pFORM) {
    
  var rEMAIL=/[\w\-\.]+\@[\-\w\.]+\.[\w\-\.]{2,4}/;
  var rIME=/[\w\.]+.*\s+.*[\w\.]+/;
  var rPRAZNO=/[\d\w]+/;
  var rCAPTCHA=/^[A-Z]{5}$/;

  // kontakt
  if (pFORM=='fKON') {
    if (!rCAPTCHA.test(getT('KON_CODE'))) {
      alert(xJ.NisUpiCC+'!');
      getO('KON_CODE').focus();
      return(false);
    }
    else if (!rIME.test(getT('NAM'))) {
      alert(xJ.NisUpiIme+'!');
      getO('NAM').focus();
      return(false);
    }
    else if (!rEMAIL.test(getT('EMA'))) {
      alert(xJ.NeiEmaAdr+'!');
      getO('EMA').focus();
      return(false);
    }
    else if (!rPRAZNO.test(getT('TEM'))) {
      alert(xJ.NisUpiTem+'!');
      getO('TEM').focus();
      return(false);
    }
    else if (!rPRAZNO.test(getT('POR'))) {
      alert(xJ.NisUpiPor+'!');
      getO('POR').focus();
      return(false);
    }
  }
  // slanje linka
  else if (pFORM=='LIN') {
    if (!rCAPTCHA.test(getT('LIN_CODE'))) {
      alert(xJ.NisUpiCC+'!');
      getO('LIN_CODE').focus();
      return(false);
    }
    else if (!rIME.test(getT('IME'))) {
      alert(xJ.NisUpiIme+'!');
      getO('IME').focus();
      return(false);
    }
    else if (!rEMAIL.test(getT('EPO'))) {
      alert(xJ.NeiEmaAdr+'!');
      getO('EPO').focus();
      return(false);
    }
    else if (!rEMAIL.test(getT('EPR'))) {
      alert(xJ.NeiEmaAdr+'!');
      getO('EPR').focus();
      return(false);
    }
  }
      
  return(true);
}

//-------------------------------------------------------------------------------
// print
//-------------------------------------------------------------------------------

function PrintBody () {
  window.open('index.cgi?&PRINT=Y&'+xURI,'PRINT','width=980,height=600,screenX=20,screenY=20,left=20,top=20')
}


//-------------------------------------------------------------------------------
// style/button switch
//-------------------------------------------------------------------------------

function C(pO,pID) {
   pO.className=pO.className.substr(0,pO.className.length-1)+pID;
}

function B(pO,pID) {
  pO.src=pO.src.replace(/[0-2]./,pID+'.');
}

//-------------------------------------------------------------------------------
// vracanje vrijednosti polja
//-------------------------------------------------------------------------------

function getS(pID) {
  var tSEL=xDOC.getElementById(pID);
  if (tSEL && tSEL.selectedIndex>=0) { return(tSEL.options[tSEL.selectedIndex].value) }
  return('')
}

function getT(pID) {
  var tTXT=xDOC.getElementById(pID);
  if (tTXT) { return(tTXT.value) }
  return('')
}

function getO(pID) {
  return(xDOC.getElementById(pID))
}

//-------------------------------------------------------------------------------
// slanje linka
//-------------------------------------------------------------------------------

function SendLink (form,xLAN) {
  var tURI=xURI.replace(/=/g,'%3D');
  Update('SENDLINK','A=LIN&SEND=LIN&L='+xLAN+'&URI='+tURI+'&IME='+getT('IME')+'&EPR='+getT('EPR')+'&EPO='+getT('EPO')+
		'&LIN_KEY='+getT('LIN_KEY')+'&LIN_CODE='+getT('LIN_CODE'));
}

//-------------------------------------------------------------------------------
//  WSP Capcha zastita formulara
//-------------------------------------------------------------------------------

function WSPCaptcha(ID) {
  if (!ID) { ID='WSP' }
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  var wsp_key_length = 32;
  var wsp_key = '';
  for (var i=0; i<wsp_key_length; i++) {
    var rnum = Math.floor(Math.random() * (chars.length-1));
    wsp_key += chars.substring(rnum, rnum+1);
  }
  xDOC.getElementById(ID+'_CAPTCHA').src = 'http://webspamprotect.com/captcha.php?id=' + xWSPCID + '&key=' + wsp_key;
  xDOC.getElementById(ID+'_KEY').value   = wsp_key;
}

//-------------------------------------------------------------------------------
// Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
// This notice MUST stay intact for legal use
// Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
//-------------------------------------------------------------------------------

var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=xDOC.all
var ns6=xDOC.getElementById && !xDOC.all
var enabletip=false

function ietruebody(){
  return (xDOC.compatMode && xDOC.compatMode!="BackCompat")? xDOC.documentElement : xDOC.body
}

function ddrivetip(thetext, thecolor, thewidth){
  if (ns6||ie){
    var tipobj=xDOC.all? xDOC.all["dhtmltooltip"] : xDOC.getElementById? xDOC.getElementById("dhtmltooltip") : ""
    if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
    if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
    tipobj.innerHTML=thetext
    enabletip=true
    return false
  }
}

function positiontip(e){
  if (enabletip){
    var tipobj=xDOC.all? xDOC.all["dhtmltooltip"] : xDOC.getElementById? xDOC.getElementById("dhtmltooltip") : ""
    var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
    //Find out how close the mouse is to the corner of the window
    var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
    var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

    var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge<tipobj.offsetWidth)
      //move the horizontal position of the menu to the left by it's width
      tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
    else if (curX<leftedge)
      tipobj.style.left="5px"
    else
      //position the horizontal position of the menu where the mouse is positioned
      tipobj.style.left=curX+offsetxpoint+"px"

    //same concept with the vertical position
    if (bottomedge<tipobj.offsetHeight)
      tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
    else
      tipobj.style.top=curY+offsetypoint+"px"

    tipobj.style.visibility="visible"
  }
}

function hideddrivetip(){
  var tipobj=xDOC.all? xDOC.all["dhtmltooltip"] : xDOC.getElementById? xDOC.getElementById("dhtmltooltip") : ""
  if (ns6||ie){
    enabletip=false
    tipobj.style.visibility="hidden"
    tipobj.style.left="-1000px"
    tipobj.style.backgroundColor=''
    tipobj.style.width=''
  }
}

xDOC.onmousemove=positiontip 

