//Common
var http = false;
var http1 = false;
var http2 = false;
var ajaxurl = '/code/ajax.php?';

function createRequestObject()
{
  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;
}

function cursorOver() {
    document.body.style.cursor = 'pointer';
}

function cursorClear() {
    document.body.style.cursor = 'default';
}

//Deletions
function deleteArticle(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{			
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteArticle&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteSystemCode(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteSystemCode&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteUser(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteUser&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteProduct(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteProduct&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteVariation(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteVariation&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteVariation1(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteVariation1&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteVariation2(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteVariation2&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteContact(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteContact&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function deleteCategory(recordID)
{
	if(confirm("Are you sure you want to delete record " + recordID + "?"))
	{
		http = createRequestObject(); 
		if(http)
		{
			document.body.style.cursor = 'wait';
			http.open("GET", ajaxurl + "ajaxMethod=deleteCategory&recordID=" + recordID, true);
			http.onreadystatechange = handleDelete;
			http.send(null);
		}
	}
}

function shopAdd(recordID,actionType,elemID)
{
	http = createRequestObject(); 
	if(http)
	{
		document.body.style.cursor = 'wait';
		var quantity = "";
		var variation = "";
		if(elemID != "")
		{
			quantity = document.getElementById('txtQTY-'+recordID).value;
		}
		if(elemID != "")
		{
			variationElem = document.getElementById('ddnVariation-'+recordID);
			if(variationElem != null) variation = variationElem.options[variationElem.selectedIndex].value;
		}
		if(quantity == "") quantity = 1;
		if(variation == "") variation = "-";
		http.open("GET", ajaxurl+"ajaxMethod="+actionType+"&recordID="+recordID+"&quantity="+quantity+"&variation="+variation+"&dummy="+new Date().getTime(), true);
		http.onreadystatechange = handleShop;
		if(elemID != "")
		{
			document.getElementById('txtQTY-'+recordID).value = "";
			var obj = document.getElementById(elemID);
			obj.style.backgroundColor = "#0C3";
		}
		http.send(null);
	}
}

function favRemove(recordID)
{
	http = createRequestObject(); 
	if(http)
	{
		document.body.style.cursor = 'wait';
		http.open("GET", ajaxurl+"ajaxMethod=removeFromFavourites&recordID="+recordID+"&dummy="+new Date().getTime(), true);
		http.onreadystatechange = handleDelete;
		http.send(null);
	}
}


function updateTotals()
{
	http2 = createRequestObject(); 
	if(http2)
	{
		document.body.style.cursor = 'wait';
		http2.open("GET", ajaxurl+"ajaxMethod=basketTotals"+"&dummy="+new Date().getTime(), true);
		http2.onreadystatechange = handleBasket;
		http2.send(null);
	}
	http1 = createRequestObject(); 
	if(http1)
	{
		document.body.style.cursor = 'wait';
		http1.open("GET", ajaxurl+"ajaxMethod=favTotals"+"&dummy="+new Date().getTime(), true);
		http1.onreadystatechange = handleFavourites;
		http1.send(null);
	}
	document.body.style.cursor = 'default';
}

function handleDelete()
{
	if(http.readyState == 4)
	{
	    document.body.style.cursor = 'default';
		window.location.href = window.location.href;
	}
}

function handleShop()
{
	if(http.readyState == 4)
	{
		updateTotals();
	}
}

function handleBasket()
{
	if(http2.readyState == 4)
	{
		var basketTotals = document.getElementById('basketTotals');
		basketTotals.innerHTML = http2.responseText;	
	}
}

function handleFavourites()
{
	if(http1.readyState == 4)
	{
		var favTotals = document.getElementById('favTotals');
		favTotals.innerHTML = http1.responseText;	
	}
}

function swapImage(imageName)
{	
    document.body.style.cursor = 'pointer';
	//var miniImage = document.getElementById(imageName);
	var mainImage = document.getElementById('productMainImage');	
	var src = imageName.src
	mainImage.src = src.replace("/small/","/large/");
	
	document.body.style.cursor = 'default';
}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function wholeNumberOnly(varElemID)
{
	var isWhole_re = /^\s*\d+\s*$/; 
	var elemValue = document.getElementById(varElemID).value;
	if(String(elemValue).search(isWhole_re) == -1)
	{
		document.getElementById(varElemID).value = '';
		alert("Sorry, you can only order this item in whole units.\n\ni.e. not 1.5");
	}
}
