// JavaScript Document
prod_id = 0;
main_id = 0;
var currency_symbol;
var exchange_rate
var orignal_main_image1 = "";
var orignal_thum1 = "";
var orignal_thum2 = "";
var orignal_thum3 = "";
var orignal_thum4 = "";
var orignal_main_image2="";
var orignal_main_image3="";
var orignal_main_image4="";
//var root = "http://70.84.114.162/~abida/";
var img_wait = new Image;
img_wait.src = root+'admin/images/wait2.gif';
var IE = document.all?true:false;
function getSubAttributeValues(mavid,pid,symbol,xrate){
//alert("Product id: "+pid+" >   main attribute value id: "+mavid);
	if(mavid >0){
		prod_id = pid;
		currency_symbol = symbol;
		exchange_rate = xrate;
		main_id = mavid;
		httpRequest = createXMLHttpRequest();
		sendRequest("POST",root+"ajax/getAttributeValues.php",displaySubAttributeValues,"cmd=subav&mav_id="+mavid+"&product_id="+pid); 
	}
}
/***********************************************************/
function displaySubAttributeValues(){
	if(httpRequest.readyState == 4){
		var resp = httpRequest.responseText.split("^");	
		if(httpRequest.responseText){
			document.getElementById("span_sav").innerHTML = resp[0];
			document.getElementById("span_sav").style.visibility="visible";
			document.getElementById("subname").innerHTML = "Select "+resp[1]+":";
			
		}else{
			document.getElementById("subname").innerHTML = "";
			document.getElementById("span_sav").innerHTML = "";
			document.getElementById("span_sav").style.visibility="hidden";	
	
			getAttributeDetails(prod_id,0,main_id)
		}
		//document.write(httpRequest.responseText);
   }
}
/************************************************************************************/
function getAttributeDetails(product_id,parnet_id,attibute_value_id){
	//alert(attibute_value_id);
	if(attibute_value_id > 0){
		preloadImages();
		httpRequest = createXMLHttpRequest();
		var url = "cmd=attdetails&product_id="+product_id+"&parent_id="+parnet_id+"&attibute_value_id="+attibute_value_id+"&symb="+currency_symbol+"&xrate="+exchange_rate;
		//alert(url);
		
		sendRequest("POST",root+"ajax/getAttributeValues.php",displayAttributeDetails,url); 
	}
}
/***********************************************************************************/
function displayAttributeDetails(){

if(httpRequest.readyState == 4){
	//alert(httpRequest.responseText);
	var	xml = httpRequest.responseXML;

	var resp = xml.getElementsByTagName("response").item(0);
	var exc_vat = resp.getElementsByTagName("excluding_price")[0].firstChild.nodeValue;
	var inc_vat = resp.getElementsByTagName("including_price")[0].firstChild.nodeValue;
	var weight = resp.getElementsByTagName("weight")[0].firstChild.nodeValue;
	var product_attribute_id = resp.getElementsByTagName("product_attribute_id")[0].firstChild.nodeValue;
	var availability = resp.getElementsByTagName("availabiltiy_date")[0].firstChild.nodeValue;
	var out_of_stock = resp.getElementsByTagName("out_of_stock")[0].firstChild.nodeValue;
	var stock = resp.getElementsByTagName("attribute_stock")[0].firstChild.nodeValue;
	
	//document.getElementById("exc_vat").innerHTML = Base64.decode(exc_vat);
	document.getElementById("inc_vat").innerHTML = Base64.decode(inc_vat);
	document.getElementById("product_price").value = Base64.decode(inc_vat);
	document.getElementById("product_weight").value = weight;
	document.getElementById("product_attribute_id").value = product_attribute_id;
		
	/*if(document.getElementById("mail_stock") && (document.getElementById("stock") != "undefined")){	//alert('m here');
		if(out_of_stock > 0){
			document.getElementById("mail_stock").style.display = "block";
		}else{
			document.getElementById("mail_stock").style.display = "none";
		}
	}
	if(document.getElementById("av_data") && (document.getElementById("stock") != "undefined")){	
		if(availability){
			document.getElementById("av_data").innerHTML = "This Product will be available on "+availability;
		}
		else{
			document.getElementById("av_data").innerHTML = "";
		}
	}*/
	
	if(document.getElementById("stock") && (document.getElementById("stock") != "undefined")){	//alert(stock);
		document.getElementById("stock").innerHTML = stock;
		if(stock > 1){
			document.getElementById("instock").style.display = "block";
			document.getElementById("outofstock").style.display = "none";
		}else{
			document.getElementById("outofstock").style.display = "block";
			document.getElementById("instock").style.display = "none";
		}
	}
	
 /*#############################		For Displaying Images   	################################*/
	var main_a = document.getElementById("main_a");
		if(main_a){
			//main_a.innerHTML = '<img src="'+img_wait.src+'"  name="main_image"  border="0" id="main_image" />';
		}
		var images = resp.getElementsByTagName("images").item(0);	
		
		images = images.getElementsByTagName("image");
	
		
		var mtbl = document.getElementById('thumbs_tbl');
		
		var mtbody = mtbl.childNodes[0];
		//alert(mtbody);
		
		var mtrs = document.getElementsByTagName("tr");
	
		if(mtrs){
			for(i=0; i<mtrs.length; i++){
				while(mtrs[i].id.match("thumb_tbl_tr_")){
					var prnt = mtrs[i].parentNode;
					prnt.removeChild(mtrs[i]);
					var mtrs = document.getElementsByTagName("tr");
				}
			}
		}
		
		if(images){
			main_a.innerHTML =  '<img src="'+root+"product_images/_"+images[0].firstChild.nodeValue+'"  name="main_image"  border="0" id="main_image" />';
			main_a.href = root+"product_images/"+images[0].firstChild.nodeValue;
		}
		//alert(images.length);
		for (var i = 0 ; i < images.length ; i++) {
			// a_value = item.getElementsByTagName("attribute_value")[0].firstChild.nodeValue;
			//alert(images[i].firstChild.nodeValue);
			
			var mtr = document.createElement("tr");
			mtr.setAttribute("id","thumb_tbl_tr_"+i);
			
			var mtd = document.createElement("td");
			mtd.setAttribute("valign","top");
			mtd.setAttribute("width","100%");
			mtd.setAttribute("align","center");
			mtd.className = "browse_table2";

			var tbl = document.createElement("table");
			tbl.setAttribute("width","100%");
			tbl.setAttribute("height","90");
			tbl.setAttribute("border","0");
			tbl.setAttribute("cellpadding","0");
			tbl.setAttribute("cellspacing","0");
			
			var tbody = document.createElement("TBODY");
			tbl.appendChild(tbody);
			
			var tr = document.createElement("tr");

			var td = document.createElement("td");
			td.setAttribute("align","center");
			td.setAttribute("valign","middle");
			td.setAttribute("width","100%");
			td.setAttribute("height","90");
			//alert(IE);
		
			var hrf = document.createElement("a");
			hrf.href = root+'product_images/'+images[i].firstChild.nodeValue;
			hrf.setAttribute("rel","lightbox[tap]");
			hrf.innerHTML = '<img src="'+root+'product_images/___'+images[i].firstChild.nodeValue+'"  name=""  border="0" id="" onmouseover="javascript:maximizeThis(\''+root+'product_images/_'+images[i].firstChild.nodeValue+'\');" />';

			td.appendChild(hrf);
			tr.appendChild(td);
			tbody.appendChild(tr);
			mtd.appendChild(tbl);
			mtr.appendChild(mtd);
			//mtbl
			if(IE){
				mtbody.appendChild(mtr);
				/*var r  = document.createElement('tr');
				var ca = document.createElement('td');
				ca.setAttribute("height","15");
				//var ta = document.createElement('<Br>');
				//var ta = document.createTextNode('');
				//ca.appendChild(ta);
				r.appendChild(ca);
				mtbody.appendChild(r);*/
			}else{
				mtbl.appendChild(mtr);
				/*var r  = document.createElement('tr');
				var ca = document.createElement('td');
				ca.setAttribute("height","15");
				r.appendChild(ca);
				mtbl.appendChild(r);*/
			}
		}
		myLightbox.updateImageList();
   }
	
}
function displayAttributeDetailsForHorizontalThumbnail(){

if(httpRequest.readyState == 4){
	//alert(httpRequest.responseText);
	var	xml = httpRequest.responseXML;

	var resp = xml.getElementsByTagName("response").item(0);
	var exc_vat = resp.getElementsByTagName("excluding_price")[0].firstChild.nodeValue;
	var inc_vat = resp.getElementsByTagName("including_price")[0].firstChild.nodeValue;
	var weight = resp.getElementsByTagName("weight")[0].firstChild.nodeValue;
	var product_attribute_id = resp.getElementsByTagName("product_attribute_id")[0].firstChild.nodeValue;
	var availability = resp.getElementsByTagName("availabiltiy_date")[0].firstChild.nodeValue;
	var out_of_stock = resp.getElementsByTagName("out_of_stock")[0].firstChild.nodeValue;
	var stock = resp.getElementsByTagName("attribute_stock")[0].firstChild.nodeValue;
	
	//document.getElementById("exc_vat").innerHTML = Base64.decode(exc_vat);
	document.getElementById("inc_vat").innerHTML = Base64.decode(inc_vat);
	document.getElementById("product_price").value = Base64.decode(inc_vat);
	document.getElementById("product_weight").value = weight;
	document.getElementById("product_attribute_id").value = product_attribute_id;
		
	if(document.getElementById("mail_stock") && (document.getElementById("stock") != "undefined")){
		if(out_of_stock > 0){
			document.getElementById("mail_stock").style.display = "block";
		}else{
			document.getElementById("mail_stock").style.display = "none";
		}
	}
	if(document.getElementById("av_data") && (document.getElementById("stock") != "undefined")){		
		if(availability){
			document.getElementById("av_data").innerHTML = "This Product will be available on "+availability;
		}
		else{
			document.getElementById("av_data").innerHTML = "";
		}
	}
	
	if(document.getElementById("stock") && (document.getElementById("stock") != "undefined")){
		document.getElementById("stock").innerHTML = stock;
	}
	
 /*#############################		For Displaying Images   	################################*/
	var main_a = document.getElementById("main_a");
		if(main_a){
			//main_a.innerHTML = '<img src="'+img_wait.src+'"  name="main_image"  border="0" id="main_image" />';
		}
		var images = resp.getElementsByTagName("images").item(0);	
		
		images = images.getElementsByTagName("image");
	
		
		var mtbl = document.getElementById('thumbs_tbl');
		
		var mtbody = mtbl.childNodes[0];
		//alert(mtbody);
	
		var mtrs = document.getElementsByTagName("td");
	
		if(mtrs){
			for(i=0; i<mtrs.length; i++){
				while(mtrs[i].id.match("thumb_tbl_tr_")){
					var prnt = mtrs[i].parentNode;
					prnt.removeChild(mtrs[i]);
					var mtrs = document.getElementsByTagName("td");
				}
			}
		}
		
		if(images){
			
			main_a.innerHTML =  '<img src="'+root+"product_images/_"+images[0].firstChild.nodeValue+'"  name="main_image"  border="0" id="main_image" />';
			
			main_a.href = root+"product_images/"+images[0].firstChild.nodeValue;
		}
		//alert(images.length);
		
		for (var i = 0 ; i < images.length ; i++) {
			// a_value = item.getElementsByTagName("attribute_value")[0].firstChild.nodeValue;
			//alert(images[i].firstChild.nodeValue);
			
			//var mtr = document.createElement("tr");
			//mtr.setAttribute("id","thumb_tbl_tr_"+i);
			
			var mtd = document.createElement("td");
			mtd.setAttribute("id","thumb_tbl_tr_"+i);
			mtd.setAttribute("valign","top");
			mtd.setAttribute("width","100%");
			mtd.setAttribute("align","center");
			mtd.className = "tbl_whiteBrdr";

			var tbl = document.createElement("table");
			tbl.setAttribute("width","100%");
			tbl.setAttribute("height","60");
			tbl.setAttribute("border","0");
			tbl.setAttribute("cellpadding","0");
			tbl.setAttribute("cellspacing","0");
			
			var tbody = document.createElement("TBODY");
			tbl.appendChild(tbody);
			
			var tr = document.createElement("tr");

			var td = document.createElement("td");
			td.setAttribute("align","center");
			td.setAttribute("valign","middle");
			td.setAttribute("width","100%");
			td.setAttribute("height","60");
			//alert(IE);
		
			var hrf = document.createElement("a");
			hrf.href = root+'product_images/'+images[i].firstChild.nodeValue;
			hrf.setAttribute("rel","lightbox[tap]");
			hrf.innerHTML = '<img src="'+root+'product_images/___'+images[i].firstChild.nodeValue+'"  name=""  border="0" id="" onmouseover="javascript:maximizeThis(\''+root+'product_images/_'+images[i].firstChild.nodeValue+'\');" />';

			td.appendChild(hrf);
			tr.appendChild(td);
			tbody.appendChild(tr);
			mtd.appendChild(tbl);
			//mtr.appendChild(mtd);
			//mtbl
			if(IE)
			mtbody.appendChild(mtd);
			else
			mtbl.appendChild(mtd);
		}
		myLightbox.updateImageList();
   }
	
}
/***********************************************************************************/
function matchdDiv(ctr_id){
	var mtbl = document.getElementById('thumbs_tbl');	
	var	divs = mtbl.getElementsByTagName("tr");
	var found = false;
	for(var k=0; k<divs.length; k++){
		if(divs[k].id.match(ctr_id)){
			found = k;
			break;
		}
	}
	if(found){  return k;}
	return false;
}

/**********************************************************************************/
function removeOption(optId){
	var elm = document.getElementById(optId);
	var prnt = elm.parentNode;
	prnt.removeChild(elm);
}
///////////////////////////////////////////////////////////////////////////////////

/************************************************************************************************************/



function preloadImages(){
	
	//if(document.getElementById("main_image") && (document.getElementById("main_image") != "undefined"))
	//document.getElementById("main_image").src = root+'admin/images/wait.gif';
	if(document.getElementById("thumb1") && (document.getElementById("thumb1") != "undefined"))
	document.getElementById("thumb1").src  =root+'admin/images/wait2.gif';
	if(document.getElementById("thumb2") && (document.getElementById("thumb2") != "undefined"))
	document.getElementById("thumb2").src  =root+'admin/images/wait2.gif';
	if(document.getElementById("thumb3") && (document.getElementById("thumb3") != "undefined"))
	document.getElementById("thumb3").src  =root+'admin/images/wait2.gif';
	if(document.getElementById("thumb4") && (document.getElementById("thumb4") != "undefined"))
	document.getElementById("thumb4").src  =root+'admin/images/wait2.gif';
}
/******************************************/
function changeWishRef(val,opt){
  elm = document.getElementById("wish_ref");	
	if(val > 0){
	  if(opt == 0){
		if(!main_ref){
			main_ref = elm.href;
			main_atrib = val;
		}
		elm.href = main_ref +"&m_av_id="+ val;
	  }else{
		 elm.href = main_ref +"&m_av_id="+ main_atrib+"&s_av_id="+val;
	  }	
	}
}
/**********************************************************/
function maximizeThis(img_name){
	 document.images["main_image"].src = img_name;
	 img_name = img_name.replace(root,"");
	 var ind = strpos(img_name,"/");
	 var himg = img_name.substr((ind+2),img_name.length);
	 document.getElementById("main_a").href = root+img_name.substr(0,(ind+1))+himg;
}
var isrc,img;
function enlarge(imgsrc){
//alert(imgsrc);
	isrc = imgsrc.replace(root+"product_images/_","");
	isrc = root+"product_images/"+isrc;
	img = new Image;
	img.src = isrc;
	//setTimeout('zoom()',2000);
	zoom();
}
function zoom(){
	if((img.height ==0) || (img.height == "undefined") || (img.height < 80)){
		setTimeout('zoom()',500);
	}else{
	//alert(img.height);
		mywin = window.open(isrc,'productImage',"height="+(img.height+20)+",width="+(img.width+20));
		mywin.moveTo((screen.width - img.width)/2, (screen.height - img.height)/2);
		mywin.focus();
	}
}
/*****************************************************************/
function assignSubAttValue(val){
	//document.getElementById("sub_attribute_value_idt").value = val;
}
/*****************************************************************/
function postToWishList(act,attrib){
	if(validatethis(document.productForm,attrib)){
		document.productForm.action = act;
		document.productForm.submit();
	}
}
/*****************************************************************/
var main_ref;
var main_atrib;
function validatethis(frm,isexist){		//alert(document.getElementById("stock").innerHTML);
	if(document.getElementById("stock").innerHTML<1){
		alert("Sorry currently out of stock");
		return false;
	}
	if(isexist){
		if(document.getElementById("main_attribute_id") && (document.getElementById("main_attribute_id") != "undefined")){
			if(document.getElementById("main_attribute_id").value == ""){
				alert("Please select an attribute first");
				return false;
			}else{
				if(document.getElementById("sub_attribute_value_id") && (document.getElementById("sub_attribute_value_id") != "undefined")){
					if(document.getElementById("sub_attribute_value_id").value == ""){
						alert("Please select a sub attribute first");
						return false;
					}
				}
			}
		}
	}
	return true;
}
/*************************************************/
var append = "";
var ap_url;
function appendMainAttribValue(val){
    if(!ap_url) ap_url = document.getElementById("tellafriendhref").href; 
	document.getElementById("tellafriendhref").href = ap_url+(val+"/").replace(" ","-");
	append = document.getElementById("tellafriendhref").href ;
	//alert(document.getElementById("tellafriendhref").href);
}
function appendsubattributevalue(val){
	if(append){
		document.getElementById("tellafriendhref").href = (append+val+"/").replace(" ","-");
	}
	//alert(document.getElementById("tellafriendhref").href);
}
function saveOrignalImages(){
	if(document.getElementById("main_image") && (document.getElementById("main_image") != "undefined"))
	orignal_main_image1 = document.getElementById("main_image").src;
	if(document.getElementById("thumb1") && (document.getElementById("thumb1") != "undefined"))
	orignal_thum1 = document.getElementById("thumb1").src;
	if(document.getElementById("thumb2") && (document.getElementById("thumb2") != "undefined"))
	orignal_thum2 = document.getElementById("thumb2").src;
	if(document.getElementById("thumb3") && (document.getElementById("thumb3") != "undefined"))
	orignal_thum3 = document.getElementById("thumb3").src;
	if(document.getElementById("thumb4") && (document.getElementById("thumb4") != "undefined"))
	orignal_thum4 = document.getElementById("thumb4").src;
}
function strpos(str, ch) {
	for (var i = 0; i < str.length; i++)
	if (str.substring(i, i+1) == ch) return i;
	return -1;
}