window.onload=function() {
  gMessageElement = document.getElementById("poruka");
  new Accordian('basic-accordian',5,'header_highlight');
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

var gReq = null;
var gMessageElement = null;
var gLang = null;

function createAndSendRequest() {

  var lHttpMethod = "GET";
  var lUrl="../php/do_newsletter.php?";
  var nameValue = document.getElementById("name").value;
  var surnameValue = document.getElementById("surname").value;
  var companyValue = document.getElementById("company").value;
  var emailValue = document.getElementById("email").value;
  gLang = document.getElementById("lang").value;

  lUrl += "name=";
  lUrl += escape(nameValue);
  lUrl += "&";
  lUrl += "surname=";
  lUrl += escape(surnameValue);
  lUrl += "&";
  lUrl += "company=";
  lUrl += escape(companyValue);
  lUrl += "&";
  lUrl += "email=";
  lUrl += escape(emailValue);
  lUrl += "&";
  lUrl += "lang=";
  lUrl += escape(gLang);
  lUrl += "&subject=subscribe";
  lUrl += "&require=name,surname,email";
  lUrl += "&sort=order:name,surname,email";
  lUrl += "&env_report=REMOTE_HOST,HTTP_USER_AGENT";

  gReq = getHTTPObject();
  if (gReq) {
    gReq.onreadystatechange=onReadyStateChange;
    gReq.open(lHttpMethod, lUrl, true);
    gReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    gReq.send(null);    
  }
}

function onReadyStateChange() {  
  var lReady = gReq.readyState;
  if (lReady = READY_STATE_COMPLETE) {
    var lData = gReq.responseText.split(",");
    toMessageText(lData);    
  }  
}

function toMessageText(pData) {
  if (gMessageElement != null) {
    var lText = new Array();
    var lStyle = null;
    
    if (pData[0] == "ok") {
      if (gLang == "rs") {
        lText[0] = "Hvala. Upisani ste u listu za naš newsletter.";
      } else if (gLang == "de") {
        lText[0] = "Danke. Anmeldung erfolgreich.";
      } else {
        lText[0] = "Subscribed.";
      }
      lStyle = "ispravno";
    }
    if (pData[0] == "error") {
      for (var lIdx = 1; lIdx<pData.length; lIdx++) {
      	lText[lIdx-1] = pData[lIdx];      	
      }
      lStyle = "greska";
    }
    if (lStyle != null) {
      var lTextPoruke = document.getElementById("tekstPoruke1");
      if (lTextPoruke != null) {
        removeFromParent(gMessageElement, lTextPoruke);
      } 
      addDivNode(gMessageElement, lText, "tekstPoruke1");
      gMessageElement.className = lStyle;
    } 
  }
}

function addDivNode(pElement, pText, pTextId) {
  var lChild = document.createElement("div");
  pElement.appendChild(lChild);
  
  for (var lIdx = 0; lIdx<pText.length; lIdx++) {
    var lTextNode = document.createTextNode(pText[lIdx]  + "\n" );
    lChild.appendChild(lTextNode);

    var lBr=document.createElement("BR");
    lChild.appendChild(lBr);      	
  }
  
  lChild.setAttribute("id", pTextId);
}

function removeFromParent(pParentNode, pChildNode) {
  pParentNode.removeChild(pChildNode);
}

function openWindow (pAddress, pTitle) {
  // myWindow = window.open(pAddress, pTitle, "width=800,height=564,left=100,top=200,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
// Zbog problema sa IE6 i IE7, parametar pTitle se ne koristi !!!! ???? !!!	
	myWindow = window.open(pAddress,"", "width=800, height=564, left=100,top=200,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");  
	myWindow.focus();
}

/*
DezinerFolio.com Simple Accordians.

Author  : G.S.Navin Raj Kumar
Website : http://dezinerfolio.com

*/

/*
* The Variable names have been compressed to achive a higher level of compression.
*/

// Prototype Method to get the element based on ID
function $(d){
	return document.getElementById(d);
}

// set or get the current display style of the div
function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.height=v;
	}
}
/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=7;
t=10;

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}

//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
	}
}

// Collapse Initializer
function cl(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}

//Expand Initializer
function ex(d){
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
	}
}

// Removes Classname from the given div.
function cc(n,v){
	s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
}
//Accordian Initializer
function Accordian(d,s,tc){
	// get all the elements that have id as content
	l=$(d).getElementsByTagName('div');
	c=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){c.push(h);}
	}
	sel=null;
	//then search through headers
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			d=$(h.substr(0,h.indexOf('-'))+'-content');
			d.style.display='none';
			d.style.overflow='hidden';
			d.maxh =sh(d);
			d.s=(s==undefined)? 7 : s;
			h=$(h);
			h.tc=tc;
			h.c=c;
			// set the onclick function for each header.
			h.onclick = function(){
				for(i=0;i<this.c.length;i++){
					cn=this.c[i];
					n=cn.substr(0,cn.indexOf('-'));
					if((n+'-header')==this.id){
						ex($(n+'-content'));
						n=$(n+'-header');
						cc(n,'__');
						n.className=n.className+' '+n.tc;
					}else{
						cl($(n+'-content'));
						cc($(n+'-header'),'');
					}
				}
			}
			if(h.className.match(/selected+/)!=undefined){ sel=h;}
		}
	}
	if(sel!=undefined){sel.onclick();}
}

