var mainUrl;
var httpReq;
var idArr = new Array();
var txtArr = new Array();

function loadPage ()
{
	var divs = document.getElementsByTagName ("DIV");
	var spans = document.getElementsByTagName ("SPAN");
	var hrefs = document.getElementsByTagName ("A");
	var refCter = 0;
	var sBody = "";

	fixAjax ();
	//alert (mainUrl);
	for (var i = 0;i < divs.length;i++)
	{
		if (divs[i].id.substr (0, 3) == "lbl")
		{
			idArr[refCter] = divs[i].id;
			sBody += idArr[refCter] + ",";
			refCter++;
		}
	}

	for (var i = 0;i < spans.length;i++)
	{
		if (spans[i].id.substr (0, 3) == "lbl")
		{
			idArr[refCter] = spans[i].id;
			sBody += idArr[refCter] + ",";
			refCter++;
		}
	}

	for (var i = 0;i < hrefs.length;i++)
	{
		if (hrefs[i].id.substr (0, 3) == "lbl")
		{
			idArr[refCter] = hrefs[i].id;
			sBody += idArr[refCter] + ",";
			refCter++;
		}
	}

	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=page&qry=" + sBody, true);
	httpReq.onreadystatechange = loadTexts;
	httpReq.send(null);
	hoverImg ();
}


function loadTexts ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			//alert (httpReq.responseText);
			txtArr = httpReq.responseText.split (",");
			for (var i = 0;i < txtArr.length;i++)
			{
				try
				{
					tmpElm = document.getElementById (idArr[i]);
					tmpElm.style.opacity = 0;
					tmpElm.innerHTML = unescape (txtArr[i]);
				}
				catch (e)
				{
					//alert (idArr[i]);
				}
			}
			flashIn (idArr);
		}
	}
}

function loadHome (refSect)
{
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=page&qry=" + sBody, true);
	httpReq.onreadystatechange = loadHomeTexts;
	httpReq.send(null);
}

function loadSubcat (catRef, limit, offset)
{
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (mainUrl);
	httpReq.open("GET", mainUrl + "receiver.php?req=subcat&ref=" + catRef + "&limit=" + limit + "&offset=" + offset, true);
	httpReq.onreadystatechange = loadRightPane;
	httpReq.send(null);
}

function loadImages (refItem)
{
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (mainUrl);
	httpReq.open("GET", mainUrl + "receiver.php?req=images&ref=" + refItem, true);
	httpReq.onreadystatechange = loadRightPane;
	httpReq.send(null);
}

function loadImage (imgSrc)
{
	//alert (imgSrc);
	var imgMain = document.getElementById ("imgMain");
	imgMain.src = "img/pics/" + imgSrc;
}

function loadRightPane ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			document.getElementById ("divHomeRight").innerHTML = unescape (httpReq.responseText);
		}
	}
}

function userLogin ()
{
	var txtUser = document.getElementById ("txtUser").value;
	var txtPass = document.getElementById ("txtPass").value;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=login&user=" + txtUser + "&pass=" + txtPass, true);
	httpReq.onreadystatechange = basketRemSuccess;
	httpReq.send(null);
}

function addToBasket (refItem)
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=addtobasket&qry=" + refItem, true);
	httpReq.onreadystatechange = basketSuccess;
	httpReq.send(null);
}

function basketSuccess ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			alert (httpReq.responseText);
		}
	}
}

function removeFromBasket (refItem)
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=remfrombasket&qry=" + refItem, true);
	httpReq.onreadystatechange = basketRemSuccess;
	httpReq.send(null);
}

function basketRemSuccess ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			alert (httpReq.responseText);
			document.location = "myacc.php";
		}
	}
}

function sendBasket ()
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=sendbasket", true);
	httpReq.onreadystatechange = basketRemSuccess;
	httpReq.send(null);
}

function saveSetts ()
{
	var txtPassword = document.getElementById ("txtPassword").value;
	var txtName = document.getElementById ("txtName").value;
	var txtAddress = document.getElementById ("txtAddress").value;
	var txtTel = document.getElementById ("txtTel").value;
	var txtEmail = document.getElementById ("txtEmail").value;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=savesetts&pass=" + txtPassword + "&name=" + txtName + "&address=" + txtAddress + "&tel=" + txtTel + "&email=" + txtEmail, true);
	httpReq.onreadystatechange = saveSettsResult;
	httpReq.send(null);
}

function saveSettsResult ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			if (httpReq.responseText == 1)
			{
				document.location = "myacc.php";
			}
		}
	}
}

function selectSect (sectRef)
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=section&sect=" + sectRef, true);
	httpReq.onreadystatechange = loadSectCats;
	httpReq.send(null);
}

function loadSectCats ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			//alert (httpReq.responseText);
			var cats = httpReq.responseText.split ("||");
			var slctCategory = document.getElementById ("slctCategory");
			//alert (httpReq.responseText + " " + cats.length);
			//alert (slctCategory.options.length)
			while (slctCategory.length > 0)
			{
				slctCategory.options[0] = null;
			}
			var slctSubCategory = document.getElementById ("slctSubCategory");
			//alert (httpReq.responseText + " " + cats.length);
			//alert (slctCategory.options.length)
			while (slctSubCategory.length > 0)
			{
				slctSubCategory.options[0] = null;
			}
			for (var j = 0;j < cats.length - 1;j++)
			{
				slctCategory.options[j] = new Option (cats[j], cats[j]);
			}
			var spanSearch = document.getElementById ("spanSearch");
			spanSearch.innerHTML = cats[j];
			document.getElementById ("txtSearch").value = "";
		}
	}
}

function selectCat ()
{
	var slctCategory = document.getElementById ("slctCategory");
	var slctCategoryVal = slctCategory.options[slctCategory.selectedIndex].innerHTML;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=category&cat=" + escape (slctCategoryVal), true);
	httpReq.onreadystatechange = loadSubcats;
	httpReq.send(null);
}

function loadSubcats ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			//alert (httpReq.responseText);
			var subcats = httpReq.responseText.split ("||");
			var slctSubCategory = document.getElementById ("slctSubCategory");
			//alert (httpReq.responseText + " " + cats.length);
			//alert (slctCategory.options.length)
			while (slctSubCategory.length > 0)
			{
				slctSubCategory.options[0] = null;
			}
			for (var j = 0;j < subcats.length - 1;j++)
			{
				slctSubCategory.options[j] = new Option (subcats[j], subcats[j]);
			}
			var spanSearch = document.getElementById ("spanSearch");
			spanSearch.innerHTML = subcats[j];
			document.getElementById ("txtSearch").value = "";
		}
	}
}

function selectSubCat ()
{
	var slctSubCategory = document.getElementById ("slctSubCategory");
	var slctSubCategoryVal = slctSubCategory.options[slctSubCategory.selectedIndex].innerHTML;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=subcategory&subcat=" + escape (slctSubCategoryVal), true);
	httpReq.onreadystatechange = loadItemsCnt;
	httpReq.send(null);
}

function typeSearchText ()
{
	var txtSearch = document.getElementById ("txtSearch").value;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	httpReq.open("GET", mainUrl + "receiver.php?req=searchtext&text=" + escape (txtSearch), true);
	httpReq.onreadystatechange = loadItemsCnt;
	httpReq.send(null);
}

function loadItemsCnt ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			var spanSearch = document.getElementById ("spanSearch");
			spanSearch.innerHTML = httpReq.responseText;
		}
	}
}

function loadSearch ()
{
	document.location = "search.php";
}

function browseResults (limit, offset)
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=result&limit=" + limit + "&offset=" + offset, true);
	httpReq.onreadystatechange = loadResults;
	httpReq.send(null);
}

function loadResults ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			var divResults = document.getElementById ("divResults");
			divResults.innerHTML = httpReq.responseText;
		}
	}
}

function logOut ()
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=logout", true);
	httpReq.onreadystatechange = logOutSuccess;
	httpReq.send(null);
}

function logOutSuccess ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			alert (httpReq.responseText);
			document.location = "myacc.php";
		}
	}
}

function register ()
{
	var txtUName = document.getElementById ("txtUName").value;
	var txtPassword = document.getElementById ("txtPassword").value;
	var txtPassword2 = document.getElementById ("txtPassword2").value;
	var txtName = document.getElementById ("txtName").value;
	var txtAddress = document.getElementById ("txtAddress").value;
	var txtTel = document.getElementById ("txtTel").value;
	var txtEmail = document.getElementById ("txtEmail").value;
	var txtCaptcha = document.getElementById ("txtCaptcha").value;

	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	var cChar;
	var validUser = true;
	var validPass = true;
	var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789";

	if (txtUName.length < 3)
	{
		validUser = false;
		alert (document.getElementById ("txtShortUname").value);
		return;
	}
	for (var i = 0;i < txtUName.length && validUser == true;i++)
	{
		cChar = txtUName.charAt (i);
		if (validChars.indexOf (cChar) == -1)
		{
			validUser = false;
			alert (document.getElementById ("txtInvalidUname").value);
			return;
		}
	}
	if (txtPassword != txtPassword2)
	{
		validPass = false;
		alert (document.getElementById ("txtPassMismatch").value);
		return;
	}
	if (txtPassword.length < 3)
	{
		validPass = false;
		alert (document.getElementById ("txtShortPass").value);
		return;
	}
	for (var i = 0;i < txtPassword.length && validPass == true;i++)
	{
		cChar = txtPassword.charAt (i);
		if (validChars.indexOf (cChar) == -1)
		{
			validPass = false;
			alert (document.getElementById ("txtInvalidPass").value);
			return;
		}
	}
	if (validUser == true && validPass == true)
	{
		//alert (validUser);
		httpReq.open("GET", mainUrl + "receiver.php?req=register&uname=" + escape (txtUName) + "&pass=" + escape (txtPassword) + "&name=" + escape (txtName) + "&address=" + escape (txtAddress) + "&tel=" + escape (txtTel) + "&email=" + escape (txtEmail) + "&captcha=" + escape (txtCaptcha), true);
		httpReq.onreadystatechange = registerSuccess;
		httpReq.send(null);
	}
}

function registerSuccess ()
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			var regResult = httpReq.responseText.split ("||");
			if (regResult[0] == 0)
			{
				alert (regResult[1]);
			}
			else
			{
				alert (regResult[1]);
				document.location = "myacc.php";
			}
		}
	}
}

function changeLang (sectRef, langRef)
{
	fixAjax ();
	if (!httpReq)
	{
		httpReq = createRequestObject ();
	}

	//alert (refItem);
	httpReq.open("GET", mainUrl + "receiver.php?req=lang&ref=" + langRef, true);
	httpReq.onreadystatechange = function () {langRedirect (sectRef);};
	httpReq.send(null);
}

function langRedirect (sectRef)
{
	if (checkReadyState (httpReq))
	{
		if (httpReq.responseText)
		{
			document.location = sectRef + ".php";
		}
	}
}

function fixAjax ()
{
	var tmpUrl = location.toString ();
	var tmpUrlParts = tmpUrl.split ("/");
	mainUrl = "http://";
	for (var i = 2;i < tmpUrlParts.length - 1;i++)
	{
		mainUrl += tmpUrlParts[i] + "/";
	}
}

function createRequestObject ()
{
	var refHttp;

	if (window.XMLHttpRequest)
	{
		refHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
  {
		refHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
	return refHttp;
}

function checkReadyState(refReq)
{
	if (refReq.readyState == 4)
  {
	  if (refReq.status == 200)
    {
    	return true;
    }
  }
}
