function ajaxStart(){
	this.httpXML;
	this.workid;
	this.url;
	this.parameters;	
	this.nametag;
	this.activetag;
	this.XmlHttp = function() {
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 httpRequest = new XMLHttpRequest();
			 if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  }
		  if (!httpRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
		  return httpRequest;
	}

	this.sendPost = function() {
		var base = this;
		var loading = "Loading...";
		this.httpXML = this.XmlHttp();
		this.httpXML.open('POST', this.url, true);
		this.httpXML.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.httpXML.onreadystatechange = function() {
			 if (base.httpXML.readyState == 4) {
					if (base.httpXML.status == 200) {
						result = base.httpXML.responseText;
						document.getElementById(base.nametag).innerHTML=result;
					} else {
						PostData(base.workid, base.url, base.parameters, base.nametag);
					}
			 }else{
					if(document.getElementById(base.activetag))
					{
						document.getElementById(base.activetag).innerHTML= "&nbsp;"+loading;
					}
			 }
		}
		this.httpXML.send(this.parameters);
	}
}

function PostData(workid, url, parameters, nametag, activetag)
{
	var divObjects = new Array();
	var workid;
	var url;
	var parameters;
	var nametag;
	divObjects[workid] = new ajaxStart();
	divObjects[workid].workid=workid;
	divObjects[workid].url=url;
	divObjects[workid].parameters = parameters;
	divObjects[workid].nametag = nametag;
	divObjects[workid].activetag = activetag;
	divObjects[workid].sendPost();
}

function addToCart(pweb, pid, name)
{
	if(pweb!='')
	{
		PostData(1, '../../modules/product/addtocart_ajax.php', 'pweb='+pweb+'&id='+pid, 'cart-info', '');
		if(pid!='')
		{
			//document.getElementById("statusAreaProductName").innerHTML = name;
			$("#statusArea").slideDown("slow");
			setTimeout('$("#statusArea").slideUp("slow")', 4000);
		}
	}
}

function compareProduct(lang, wid, data)
{
	if(wid!='' && data!='')
	{
		$.gritter.removeAll();
		if(document.getElementById("compare"+data).checked)
		{
			PostData(1, "../../modules/product/compareproduct_query.php", "wid="+wid+"&compareID="+data, '', '');
			if(lang=="e")
			{
				var text_selected = 'You selected products';
				var text_compare = 'click this link to compare';
			}
			else
			{
				var text_selected = 'คุณได้คลิ๊กเลือกสินค้า';
				var text_compare = 'คลิ๊กที่นี่เพื่อเปรียบเทียบสินค้า';
			}

			var noticetext = text_selected+' <a href="../../modules/product/compare.php?wid='+wid+'" target="_blank" style="color:#ccc; text-decoration: underline;">'+text_compare+'</a>';

			var unique_id = $.gritter.add({
				// (string | mandatory) the heading of the notification
				title: 'Notification',
				// (string | mandatory) the text inside the notification
				text: noticetext,
				// (string | optional) the image to display on the left
				// image: 'http://s3.amazonaws.com/twitter_production/profile_images/132499022/myface_bigger.jpg',
				// (bool | optional) if you want it to fade out on its own or just sit there
				sticky: true, 
				// (int | optional) the time you want it to be alive for before fading out
				time: '',
				// (string | optional) the class name you want to apply to that specific message
				class_name: 'my-sticky-class' 
			});
			return false;
		}
		else
		{
			PostData(1, "../../modules/product/compareproduct_query.php", "wid="+wid+"&compareID="+data, '', '');
		}
	}
}

function slideUp(obj)
{
	$("#"+obj).slideUp("fast");
}

function slideDown(obj)
{
	$("#"+obj).slideDown("fast");
}

function removeCompareProduct(id, wid, data)
{
	if(wid!='' && data!='')
	{
		var columnArray = new Array('productimage', 'productid', 'productname', 'productprice', 'productbrand', 'productmodel', 'productdetail', 'productaddtocart', 'producttellafriend', 'productremove');
		for(i=0;i<5;i++)
		{
			for(a=0;a<columnArray.length;a++)
			{
				if(document.getElementById(columnArray[a]+'-'+i))
				{
					document.getElementById(columnArray[a]+'-'+id).style.display = 'none';
				}
			}
		}
		PostData(1, "../../modules/product/compareproduct_query.php", "wid="+wid+"&compareID="+data, '', '');
	}
}
