var showLoading = false, tmrLoading;
var xmlHttp

var showLoading = false, tmrLoading;
	var xmlHttp
	function AjaxSomething_20100804(What, Where)
		{
// alert(What);
// alert(Where);
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
				{
					alert ("Your browser does not support AJAX!");
					return;
				}
				var ctime = new Date();
				var url=Where+"&amp;Time="+ctime.getTime();
				xmlHttp.onreadystatechange=function()
			    {
					if (xmlHttp.readyState==4)
						{
							document.getElementById(What).innerHTML=xmlHttp.responseText;
						}
					if (xmlHttp.readyState==1)
	 					{
							// document.getElementById(What).innerHTML="<hr>Refreshing list<hr>";
						}
				}
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		}
function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
  	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
	catch (e)
		{
		// Internet Explorer
		try
			{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e)
			{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}

function ajaxThis(divId) {
	var thisURL;
	thisURL = '&Name='+document.contact_form.Name.value;
	thisURL = thisURL+'&Email='+document.contact_form.Email.value;
	thisURL = thisURL+'&Telephone='+document.contact_form.Telephone.value;
	thisURL = thisURL+'&Message='+document.contact_form.Message.value;
	thisURL = thisURL+'&ContactPosted='+document.contact_form.ContactPosted.value;
	thisURL = thisURL+'&PageName='+document.contact_form.PageName.value;
	thisURL = SiteURL+'ajax.asp?Do=contactform'+thisURL;
	// document.getElementById('tester').innerHTML=thisURL;
	AjaxSomething(divId, thisURL);
}



/*
	Ajax jQuery functions
*/
$(function() {
	// jQuery DOM loaded function
	$(".postcode").live('mouseover', function() {
		$(this).css('cursor', 'move');
		$(this).draggable();
		makeDivsDroppable();
	});
	
	$(".area_postcode").live('mouseover', function() {
		$(this).css('cursor', 'move');
		$(this).draggable({
			start: function() {
				$(this).parent(".delivery_areas").css('overflow', 'visible');
			},
			stop: function() {
				$(this).parent(".delivery_areas").css('overflow', 'auto');
			},
			distance: 20,
			scroll: false
		});
		makeDivsDroppable();
	});
	
	$(".tab_off").live('click', function() {
		changeTab($(this));
	});
});

function changeTab(obj) {
	var c;
	c = obj.attr('id').replace(/tab_/, 'content_');
	c = $("#"+c); // Find the content object
	$(".tab_on").removeClass().addClass('tab_off');
	obj.removeClass().addClass('tab_on');
	$(".tab_target").html(c.html());
}

function showLargeImage(ImageURL) {
	/*winImage = window.open( SiteURL+"ajax.asp?Do=ViewLargeImage&ImageID="+ImageID, "winImage", 
	"status = 1, width = 800, height = 600, resizable = 1, dependent = 1, top = 0, left = 0" );*/
    winImage = window.open( ImageURL, "winImage", 
	"status = 1, width = 800, height = 600, resizable = 1, dependent = 1, top = 0, left = 0" );
}

function loadProductImage(ProductID, ImageID) {
	AjaxSomething('product_image_selection', SiteURL+'ajax.asp?Do=ProductImagesDiv&ProductID='+ProductID+"&ImageID="+ImageID);
	AjaxSomething('product_main_image', SiteURL+'ajax.asp?Do=ProductMainImage&ProductID='+ProductID+"&ImageID="+ImageID);
}

function makeDivsDroppable() {
	$("#delivery_groups").droppable({
		drop: function(event, ui) {
			// alert(ui.draggable.children().attr('value'));
			url = SiteURL+'ajax.asp?Do=DeliveryPostcodes&T=Split&FirstPostcodeID='+ui.draggable.children().attr('value');
			url += '&FirstPostcode='+ui.draggable.text();
			AjaxSomething('content_div', url);
		}
	});
	
	$(".delivery_areas").droppable({
		drop: function(event, ui) {
			// alert(ui.draggable.children().attr('value'));
			url = SiteURL+'ajax.asp?Do=DeliveryPostcodes&T=AddArea&FirstPostcodeID='+ui.draggable.children().attr('value');
			url += '&FirstPostcode='+ui.draggable.text();
			url += '&AreaGroupID='+$(this).children().attr('value');
			AjaxSomething('content_div', url);
		},
		out: function(event, ui) {
			url = SiteURL+'ajax.asp?Do=DeliveryPostcodes&T=DeleteArea&ThisPostcodeID='+ui.draggable.children().attr('value');
			AjaxSomething('content_div', url);
		}
	});
}

function addToCart(productID, quantity) {
	AjaxSomething('cart_content', SiteURL+'ajax.asp?Do=MyCart&ProductID='+productID+'&Quantity='+quantity);
	$("#pop_over").html('<p style="position: relative; top: 25%;">The item has been added to your cart.<br /><br /><img src="'+SiteURL+'images/ajax-loader.gif"></p>');
	$("#pop_over").fadeIn();
	window.setTimeout(function() {
		$("#pop_over").fadeOut();
	}, 5000);
}

function toggleLoader() {
	if (InAdmin) {
		showLoading = (showLoading) ? false : true;
		if (showLoading) {
			tmrLoading = window.setTimeout(function() {
				$("#loading").show();
			}, 300);
		} else {
			$("#loading").hide();
			clearTimeout(tmrLoading);
			tmrLoading = undefined;
		}
	}
}

function AjaxSomething(divId, url) {
	var ctime = new Date();
	if (tmrLoading == undefined) { // Make sure we're not already trying to load something
		toggleLoader();
		$.get(url+"&amp;Time="+ctime.getTime(), function(d) {
			$("#"+divId).html(d);
			toggleLoader();
			tinymce_setup();
		});
	}
}


function formSubmission(f, divId) {
	var ctime = new Date();
	var showTime = ($(f).attr('action').indexOf('?')>-1) ? '&Time='+ctime.getTime() : '?Time='+ctime.getTime();
	window.setTimeout(function() { // Wait 100ms for tinyMCE to update our textareas
		if (tmrLoading == undefined) { // Make sure we're not already trying to load something
			toggleLoader();
			if ($(f).attr('method') == 'post') {
				$.post($(f).attr('action')+showTime, $(f).serialize(), function(d) {
					$("#"+divId).html(d);
					if (divId == 'content_div') { window.location = "#admin_top"; }
					tinymce_setup();
					toggleLoader();
				});
			} else {
				$.get($(f).attr('action'), $(f).serialize(), function(d) {
					$("#"+divId).html(d);
					tinymce_setup();
					toggleLoader();
				});
			}
		}
	}, 100);
	return false;
}

/*
	General JavaScript
*/
function openFileUpload(QS) {
	winUpload = window.open( SiteURL+"admin/upload.asp?"+QS, "winUpload", 
	"status = 1, height = 215, width = 320, resizable = 0, dependent = 1, top = 100, left = 100" );
}

function BlankStuff(id) {
	document.getElementById('content_div').innerHTML="";
	document.getElementById('menu_2').innerHTML="";
	document.getElementById('menu_3').innerHTML="";
	document.getElementById('help_div').style.display="none";
	highlightMenuItem(id);
}

function BlankStuff2(id) {
	document.getElementById('content_div').innerHTML="";
	document.getElementById('menu_3').innerHTML="";
	document.getElementById('help_div').style.display="none";
	highlightMenuItem(id);
}

function BlankStuff3(id) {
	document.getElementById('content_div').innerHTML="";
	document.getElementById('help_div').style.display="none";
	highlightMenuItem(id);
}

function highlightMenuItem(id) {
	$(".admin_menu_button_on").removeClass('admin_menu_button_on').addClass('admin_menu_button');
	$("#"+id).removeClass('admin_menu_button').addClass('admin_menu_button_on');
}

function confirmNavigate(divId, url) {
	if (confirm('Are you sure?')) {
		AjaxSomething(divId, url);
	}
}

function confirmNavigateTo(url) {
	if (confirm('Are you sure?')) {
		window.location.href = url;
	}
}

function clearSearchBox() {
	if (document.top_search.query.value=='Search entire store here...') {
		document.top_search.query.value='';
	}
}

/*function toggleAvailable(id, idColumn, tableName) {
	to = ($("#available_star_"+id).attr('src').indexOf('_off')>-1) ? 1 : 0;
	$.get('../ajax.asp?'+idColumn+'='+id, { Do: 'AvailableSwitch', TableName: tableName, IDColumn: idColumn, ToggleTo: to }, function(d) { 
		$("#available_star_"+id).attr('src', d);
		$("#lilink"+id).removeClass().addClass('lilink'+to);
	});
}*/
