/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */
function GetXmlHttpObjectNew()
{
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 sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;


						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}
// for DIV DIsplay
var xmlHttp;
function showUser(str,pname,attribute)
{ 
xmlHttp=GetXmlHttpObjectNew();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="getproduct.php";
url=url+"?attrvalue="+str;
url=url+"&attr="+attribute;
url=url+"&pid="+pname;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 } 
}

function updateWishList(productid,userid) {
	xmlHttp=GetXmlHttpObjectNew();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="addToWishList.php";
	url=url+"?prId="+productid;
	url=url+"&uId="+userid;
	xmlHttp.onreadystatechange=isUpdatedWishList;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function isUpdatedWishList() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 alert("You have successfully added the product to wish list");
 } 
}


var xmlHttp;

function showProducts(str)
{
xmlhttp=GetXmlHttpObjectNew();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../getproduct.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged1;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged1()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtShowproducts").innerHTML=xmlhttp.responseText;
}
}
var xmlhttpFabric;

function loadFabric(productId,type) {
	//function written by Santhosh
	product_color_id = document.getElementById("product_color").value;
	xmlhttpFabric=GetXmlHttpObjectNew();
	if (xmlhttpFabric==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getfabric.php";
	url=url+"?type="+type;
	url=url+"&cid="+product_color_id;
	url=url+"&pid="+productId;
	url=url+"&sid="+Math.random();
	xmlhttpFabric.onreadystatechange=function() {
		if (xmlhttpFabric.readyState==4)
		{
		document.getElementById("product_fabricDiv").innerHTML=xmlhttpFabric.responseText;
			if(xmlhttpFabric.responseText.search(/<input/i)) {
				switch(type) {
						case 'CM':
						checkStock(productId,type);
						case 'MS':
						loadSize(productId,type);
						break;
						case 'CMS':
						loadSize(productId,type);
						break;
					}					
			}		
		}
	};
	xmlhttpFabric.open("GET",url,true);
	xmlhttpFabric.send(null);
}
var xmlHttpOptionPic;

function loadOptionPic(productId) {
	//function written by Santhosh
	product_color_id = document.getElementById("product_color").value;
	
	if(product_color_id) {
	xmlHttpOptionPic=GetXmlHttpObjectNew();
	if (xmlHttpOptionPic==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getOptionPic.php";
	url=url+"?cid="+product_color_id;
	url=url+"&pid="+productId;
	url=url+"&sid="+Math.random();
	xmlHttpOptionPic.onreadystatechange=stateOptionPicChanged1;
	xmlHttpOptionPic.open("GET",url,true);
	xmlHttpOptionPic.send(null);
	}
}
function stateOptionPicChanged1() {
	if (xmlHttpOptionPic.readyState==4)
	{
	document.getElementById("optionPic").innerHTML=xmlHttpOptionPic.responseText;
	}
}

var xmlHttpSize;
function loadSize(productId,type) {
	//function written by Santhosh
	switch(type) {
		case 'CS':
		product_color_id = document.getElementById("product_color").value;
		product_fabric_id ="";
		break;
		case 'MS':
		product_color_id = "";
		product_fabric_id = document.getElementById("product_fabric").value;
		break;
		case 'CMS':
		product_color_id = document.getElementById("product_color").value;
		product_fabric_id = document.getElementById("product_fabric").value;
		break;
	}
	
	xmlHttpSize=GetXmlHttpObjectNew();
	if (xmlHttpSize==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getSize.php";
	url=url+"?type="+type;
	url=url+"&cid="+product_color_id;
	url=url+"&fid="+product_fabric_id;
	url=url+"&pid="+productId;
	url=url+"&sid="+Math.random();
	xmlHttpSize.onreadystatechange=function() {
		if (xmlHttpSize.readyState==4)
		{
		document.getElementById("product_sizeDiv").innerHTML=xmlHttpSize.responseText;
		if(xmlHttpSize.responseText.search(/<input/i)) {
		checkStock(productId,type);
		}
		}
	};
	xmlHttpSize.open("GET",url,true);
	xmlHttpSize.send(null);
}

var xmlHttpStock;
function checkStock(productId,type) {
/*
Santhosh done the change here, Please consult before doing any change
1)Color Only                 	-C
2)Size Only		     		 	-S
3)Material Only		     	 	-M
4)Color and Size only	      	-CS
5)Color and Material Only     	-CM
6)Material and Size Only      	-MS
7)color size and Material     	-CMS
*/
	switch(type) {
		case 'C':
		
		product_color_id = document.getElementById("product_color").value;
		if(product_color_id == "") {
			return false;
		}
		product_size_id = "";
		product_fabric_id = "";
		break;
		case 'S':
		product_color_id = "";
		product_size_id = document.getElementById("product_size").value;
		product_fabric_id = "";
		if(product_size_id == "") {
			return false;
		}
		break;
		case 'M':
		product_color_id = "";
		product_size_id = "";
		product_fabric_id = document.getElementById("product_fabric").value;
		if(product_fabric_id == "") {
			return false;
		}
		break;
		case 'CS':
		product_color_id = document.getElementById("product_color").value;
		product_size_id = document.getElementById("product_size").value;
		if(product_color_id == "" || product_size_id == "") {
			return false;
		}
		product_fabric_id = "";
		break;
		case 'CM':
		product_color_id = document.getElementById("product_color").value;
		product_size_id = "";
		product_fabric_id = document.getElementById("product_fabric").value;
		if(product_color_id == "" || product_fabric_id == "") {
			return false;
		}
		break;
		case 'MS':
		product_color_id = "";
		product_fabric_id = document.getElementById("product_fabric").value;
		product_size_id = document.getElementById("product_size").value;
		if(product_size_id == "" || product_fabric_id == "") {
			return false;
		}
		break;
		case 'CMS':
		product_color_id = document.getElementById("product_color").value;
		product_fabric_id = document.getElementById("product_fabric").value;
		product_size_id = document.getElementById("product_size").value;
		if(product_color_id == "" || product_size_id == "" || product_fabric_id == "") {
			return false;
		}
		break;
	}
	xmlHttpStock=GetXmlHttpObjectNew();
	if (xmlHttpStock==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getStock.php";
	url=url+"?type="+type;
	url=url+"&cid="+product_color_id;
	url=url+"&fid="+product_fabric_id;
	url=url+"&sizeid="+product_size_id;
	url=url+"&pid="+productId;
	url=url+"&sid="+Math.random();
	xmlHttpStock.onreadystatechange=stateStockChanged1;
	xmlHttpStock.open("GET",url,true);
	xmlHttpStock.send(null);
}
function stateStockChanged1() {
	
	if (xmlHttpStock.readyState==4)
	{
		document.getElementById("product_option_id").value=xmlHttpStock.responseText;
		if(document.getElementById("product_option_id").value=="") {
			document.getElementById("addToCartButton").style.display = "none";
			document.getElementById("notInStock").style.display = "";
		}else {
			document.getElementById("addToCartButton").style.display = "";
			document.getElementById("notInStock").style.display = "none";
		}
	}
}


