// global variables //
var TIMER = 5;
var SPEED = 10;
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(title,message,type,autohide) 
{
  if(!type) 
  {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) 
  {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } 
  else 
  {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) 
  {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } 
  else 
  {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() 
{
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) 
{
  if(flag == null) 
  {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) 
  {
    value = dialog.alpha + SPEED;
  } 
  else 
  {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) 
  {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } 
  else if(value <= 1) 
  {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

/*function categoryClick(cat_id)
{	
	displaySubcategories(cat_id);
	displayCategoryProducts("?cat_id=" + cat_id);
}*/

//************************************************************
function displaySubcategories(cat_id)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
				var el = document.getElementById('cat_id_' + cat_id);
				//
				if (document.getElementById('expanded_' + cat_id).value == 0)
					{
					//dolzinata na cist html(prazna tabela-bez podkategorii) e 78. 
					//Vo uslovot stavame 80, za kodot da raboti i koga kategorijaID ke bide petocifren broj
					if (ajaxRequest.responseText.length > 80)  
						{
						el.innerHTML = el.innerHTML + ajaxRequest.responseText;
						document.getElementById('expanded_' + cat_id).value = 1;
						}
					}
				else
					{
					display = el.innerHTML;
					position = display.indexOf("<table");
					el.innerHTML = display.slice(0, position);
					document.getElementById('expanded_' + cat_id).value = 0;
					}
			}
		
		};
	ajaxRequest.open("GET","subcategories.php?cat_id=" + cat_id, true);
	ajaxRequest.send(null); 
}

//********************************************************************************************
function displayCategoryProducts(cat_id)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
					var el = document.getElementById('main_frm');
					el.innerHTML = ajaxRequest.responseText;
			}
			else
			{
				var el = document.getElementById('main_frm');
				el.innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='images/preload.gif' /></p>"}
		};
		//ajaxRequest.open("GET","displayCategoryProducts.php?cat_id=" + cat_id, true);
		ajaxRequest.open("GET","displayCategoryProducts.php" + cat_id, true);
		ajaxRequest.send(null); 
}

function displayProduct(p_id)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			var el = document.getElementById('main_frm');
			if(ajaxRequest.readyState == 4)					
				el.innerHTML = ajaxRequest.responseText;
			else
				el.innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='images/preload.gif' /></p>";

		}
		//ajaxRequest.open("GET","displayCategoryProducts.php?cat_id=" + cat_id, true);
		//window.alert(p_id);
		ajaxRequest.open("GET","displayProduct.php?pid=" + p_id, true);
		ajaxRequest.send(null); 			
}


//********************************************************************************************
/*function displayFoundProducts(search_str)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
					el = document.getElementById('main_frm');
					el.innerHTML = ajaxRequest.responseText;
			}
			else
			{el = document.getElementById('main_frm');
				el.innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='images/preload.gif' /></p>"}
		};
		ajaxRequest.open("GET","displayCategoryProducts.php?search_str=" + search_str, true);
		ajaxRequest.send(null); 
}*/

/*function prikazi_proizvodi(cat_id)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
					el = document.getElementById('main_frm');
					el.innerHTML = ajaxRequest.responseText;
			}
			else
			{el = document.getElementById('main_frm');
				el.innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='images/preload.gif' /></p>"}
		};
		window.alert("OPAAAAAAAA!!!");
		ajaxRequest.open("GET","displayCategoryProducts.php?cat_id=" + cat_id, true);
		//ajaxRequest.open("GET","displayCategoryProducts.php" + cat_id, true);
		ajaxRequest.send(null); 
}*/

//********************************************************************************************
function displayCart()
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if((ajaxRequest.readyState == 4) &&  (ajaxRequest.responseText != null))
			{
				var el = document.getElementById('cartDisplay');
				el.innerHTML = ajaxRequest.responseText;
			}
		};
		document.getElementById("CartRow").style.display="block";
		ajaxRequest.open("GET","cart_preview.php", true);
		ajaxRequest.send(null); 
}		

//*******************************************************************
function addToCart(product_id)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
				//popupFadeAway(ajaxRequest.responseText)	
				//window.location.reload();
				window.alert(ajaxRequest.responseText)
				//showDialog('Error',ajaxRequest.responseText,'success');

			}
		};
		ajaxRequest.open("GET","addToCart.php?product_ID=" + product_id, true);
		ajaxRequest.send(null);
		displayCart();
}; 
		
//*******************************************************************
function updateQuantity(product_id, qnt)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
				if (ajaxRequest.responseText.length > 0) 
				{
					alert(ajaxRequest.responseText);
					//popupFadeAway(ajaxRequest.responseText);
					window.location.reload();
				}
				else {window.location.reload();}	
			}
			else
			{}
		};
		ajaxRequest.open("GET","update_qnt.php?product_id=" + product_id + "&qnt=" + qnt, true);
		ajaxRequest.send(null);
		displayCart();
		}; 

//*******************************************************************
function displayContentInMainWin(page_name)
{
	var ajaxRequest;
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			var main_el = document.getElementById('main_frm');
			if(ajaxRequest.readyState == 4)	
				main_el.innerHTML = ajaxRequest.responseText;
			else
				main_el.innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='images/preload.gif' /></p>";
		}
		ajaxRequest.open("GET",page_name, true);
		ajaxRequest.send(); 
}
