//kreiranje postavki za dialogbox kod greški
// global variables //
var TIMER = 10;
var SPEED = 100;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(message)
{	var dialog;
	var dialogOK;
	var dialogcontent;
	var dialogmask;
	if(!document.getElementById('dialog')) 
	{dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogOK = document.createElement('div');
    dialogOK.id = 'dialog-OK'
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
	document.body.appendChild(dialog);
    dialog.appendChild(dialogcontent);
	dialog.appendChild(dialogOK);
    dialogOK.setAttribute('onclick','hideDialog()');
    dialogOK.onclick = hideDialog;
	} else {
    dialog = document.getElementById('dialog');
    dialogcontent = document.getElementById('dialog-content');
    dialogOK = document.getElementById('dialog-OK');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
	dialog.style.visibility = "visible";
	}
	var width = pageWidth();
	var height = pageHeight();
	var left = leftPosition();
	var top = topPosition();
	var dialogwidth = dialog.offsetWidth;
	var dialogheight = dialog.offsetHeight;
	var topposition = top + (height / 2) - (dialogheight / 2);
	var leftposition = left + (width / 2) - (dialogwidth / 2);
	dialog.style.top = topposition + "px";
	dialog.style.left = leftposition + "px";
	dialogcontent.innerHTML = message;
	dialogOK.innerHTML = 'OK';
}

// hide the dialog box //
function hideDialog() 
{
  var dialog = document.getElementById('dialog');
  dialog.style.visibility = "hidden";
  var dialog = document.getElementById('dialog-mask');
  dialog.style.visibility = "hidden";
	if (setfocus==1){document.getElementById('poruka').focus()}
	if (setfocus==2){document.getElementById('name').focus()}
	if (setfocus==3){document.getElementById('email').focus()}
}


// provjera da li su ispunjeni podaci za Ime i e-mail i ako nisu prikazivanje dialoga i postavljanje fokusa na polje koje je potrebno ispuniti//
function validate_poruka(porukafield)
    {
    with (porukafield)
      {
      if (value==null||value=="")
        {setfocus=1;showDialog('Molimo upi&scaron;ite tekst poruke.');return false;}
      else
        {return true;}
      }
    }

function validate_name(namefield)
    {
    with (namefield)
      {
      if (value==null||value=="")
        {setfocus=2;showDialog('Molimo unesite Va&scaron;e ime.');return false;}
      else
        {return true;}
      }
    }
    
function validate_email(emailfield)
    {
    with (emailfield)
      {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
      if (apos<1||dotpos-apos<2)
        {setfocus=3;showDialog('Niste unijeli e-mail adresu ili je neispravna. Molimo unesite ispravnu e-mail adresu.');return false;}
      else {return true;}
      }
    }
    
    
function validate_form(thisform)
    {
	var setfocus;
	setfocus = 0;
    with (thisform)
      {
      if (validate_poruka(poruka)==false || validate_name(Name)==false || validate_email(Email)==false)
      {return false;}
      }
	}

// izrada layera za direktni kontakt preko gumba koji se uvijek vidi desno u sredini//
function kontaktlayer()
    {
		pic1= new Image(40,100); // preload slike za kontakt over
		pic1.src="../kontakt_over.png"; 
		var kontaktdiv = document.createElement("div");
		kontaktdiv.id = "kontakt";
		var k_height = 92; //postaviti isto kao #kontakt height u contact.css
		var p_height = pageHeight();
		var p_top = (p_height / 2) - (k_height / 2);
		kontaktdiv.style.top = ((p_top / p_height) * 100) + "%";
		
		document.body.appendChild(kontaktdiv);
    	kontaktdiv.onclick = sendMail;
    }

function sendMail()
	{
		location.href = 'mailto:' + 'info' + '@' + '2d3dstudio.hr';
	}
