function jQueryGetElem(elm) {
	return $("*").get($("*").index($("#" + elm)[0]));
}

function openPopup(url, width, height) {
	var mywindow = window.open(url, "mywindow","toolbar=no,location=0,status=0,scrollbars=1,width=" + width + ",height=" + height + "");
	mywindow.moveTo(screen.availWidth / 2 - (width / 2), screen.availHeight / 2 - (height / 2));
	return false;
}

function goLocation(url) {
	window.location = url;
}

function getCallbackYes() {
	var callback = function() {
		$('#dialog').dialog('close');
	};
	return callback;
}

function getCallbackNo() {
	var callback = function() {
		$('#dialog').dialog('close');
	};
	return callback;
}

function openDialogConfirm(yes, no) {
	$('#dialog').dialog({
		autoOpen : false, 
		buttons : 	{ "ลบรายการ" : yes, 
						   "ยกเลิก" : no
						}, 
		draggable : false, 
		modal : true
	});
	$('#dialog').dialog('open');
}

function getCallbackDeleteItem(id, url) {
	var callback = function() {
		goLocation(url);
		jQuery('#dialog').dialog('close');
	};
	return callback;
}

function openPopup(url, width, height) {
	var mywindow = window.open(url, "mywindow","toolbar=no,location=0,status=0,scrollbars=1,width=" + width + ",height=" + height + "");
	mywindow.moveTo(screen.availWidth / 2 - (width / 2), screen.availHeight / 2 - (height / 2));
	return false;
}
 
function removeFormField(id) {
	$(id).remove();
}

/*
* AJAX by classic javascript
*/

function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} 
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
};

/*
* AJAX by JQuery
*/

var ajaxSendElementItems;

function ajaxSend() {
	$("#" + ajaxSendElementItems).html('');
	var loading = '<img src="images/animation/ajaxLoading.gif" align="absmiddle">';
	$("#" + ajaxSendElementItems).append(loading);
}

function getStockRemain(src) {
	var product_id = jQueryGetElem('product_id');
	var color_id = jQueryGetElem('color_id');
	var size_id = jQueryGetElem('size_id');
	$.ajax({
	  	type: "POST",
	  	url: "ajaxGetStockRemain.php",
	  	data: "product_id=" + product_id.value + "&color_id=" + color_id.value + "&size_id=" + size_id.value,
		success: function(msg) {
			var oSrc = $("*").get($("*").index($("#" + src)[0]));
			if (!oSrc)
			{
				return false;
			}
			oSrc.innerHTML = msg;
		}
	});
}

function getStockRemainMulti(src, index) {
	var product_id = jQueryGetElem('product_id_' + index);
	var color_id = jQueryGetElem('color_id_' + index);
	var size_id = jQueryGetElem('size_id_' + index);
	$.ajax({
	  	type: "POST",
	  	url: "ajaxGetStockRemain.php",
	  	data: "product_id=" + product_id.value + "&color_id=" + color_id.value + "&size_id=" + size_id.value,
		success: function(msg) {
			var oSrc = $("*").get($("*").index($("#" + src)[0]));
			if (!oSrc)
			{
				return false;
			}
			oSrc.innerHTML = msg;
		}
	});
}


function listSize(src, val) {
	ajaxSendElementItems = src;
	var product_id = jQueryGetElem('product_id');
	$("#" + src).bind("ajaxSend", ajaxSend);
	$.ajax({
	  	type: "POST",
	  	url: "ajaxGetSize.php",
	  	data: "product_id=" + product_id.value + "&color_id=" +  val,
		success: function(msg) {
			var oSrc = $("*").get($("*").index($("#" + src)[0]));
			if (!oSrc)
			{
				return false;
			}
			oSrc.innerHTML = msg;
		}
	});
	$("#" + src).unbind("ajaxSend");
}

var iPicIndex = new Array;
var func = new Array();
var intervalIndex = new Array();
function rotateImage(index, img1, img2) {
	func[index] = function() {
		var img = jQueryGetElem(index);
		if (iPicIndex[index] == img1) 
			iPicIndex[index] = img2; 
		else
			iPicIndex[index] = img1;
		img.src = iPicIndex[index];
	}
	clearInterval(intervalIndex[index]);
	intervalIndex[index] = setInterval(func[index], 2000);
}
