// 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';

function getSubAttributeValues(val,mavid,symbol,xrate){
	if(mavid >0){

		currency_symbol = symbol;
		exchange_rate = xrate;
		
		prod_id = val;
		main_id = mavid;
		httpRequest = createXMLHttpRequest();
		sendRequest("POST",root+"ajax/getAttributeValues.php",displaySubAttributeValues,"cmd=subav&product_id="+val+"&mav_id="+mavid); 
	}
}
/***********************************************************/
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,main_id,0)
		}
		//document.write(httpRequest.responseText);
   }
}
/************************************************************************************/
function getAttributeDetails(pid,maid,said){
	if(maid > 0){
		preloadImages();
		httpRequest = createXMLHttpRequest();
		sendRequest("POST",root+"ajax/getAttributeValues.php",displayAttributeDetails,"cmd=attdetails&product_id="+pid+"&mav_id="+maid+"&sid="+said+"&symb="+currency_symbol+"&xrate="+exchange_rate); 
	}
}
/***********************************************************************************/
var image1, image2, image3, image4;
var mimage1, mimage2, mimage3, mimage4;
var himage1, himage2, himage3, himage4;
function displayAttributeDetails(){
	 if(httpRequest.readyState == 4){
	//	alert(httpRequest.responseText);
	var img1 = new Image;
	var img2 = new Image;
	var img3 = new Image;
	var img4 = new Image;
	
	var mimg1 = new Image;
	var mimg2 = new Image;
	var mimg3 = new Image;
	var mimg4 = new Image;
	
	var himg1 = new Image;
	var himg2 = new Image;
	var himg3 = new Image;
	var himg4 = new Image;
	
		var resp = httpRequest.responseText;
		respon = resp.split(",");
		price = respon[0];
		weight = respon[1];
		if(respon[2]){
			image1 = root+"product_images/___"+respon[2];
			mimage1 = root+"product_images/_"+respon[2];
			himage1 = root+"product_images/"+respon[2];
			img1.src = image1;
			mimg1.src = mimage1;
			himg1.src = himage1;
		}else{
			image1 = "";
		}
		
		if(respon[3]){
			image2 = root+"product_images/___"+respon[3];
			mimage2 = root+"product_images/_"+respon[3];
			himage2 = root+"product_images/"+respon[3];
			img2.src = image2;
			mimg2.src = mimage2;
			himg2.src = himage2;
		}else{
			image2 = "";
		}
		
		if(respon[4]){
			image3 = root+"product_images/___"+respon[4];
			mimage3 = root+"product_images/_"+respon[4];
			himage3 = root+"product_images/"+respon[4];
			img3.src = image3;
			mimg3.src = mimage3;
			himg3.src = himage3;
		}else{
			image3 = "";
		}
		
		if(respon[5]){
			image4 = root+"product_images/___"+respon[5];
			mimage4 = root+"product_images/_"+respon[5];
			himage4 = root+"product_images/"+respon[5];
			img4.src = image4;
			mimg4.src = mimage4;
			himg4.src = himage4;
		}else{
			image4 = "";
		}
		
//		alert(image1+" >> "+image2+" >> "+image3+" >> "+image4);

		document.getElementById("exc_vat").innerHTML = respon[0];
		if(respon[6]){
			document.getElementById("inc_vat").innerHTML = respon[6];
		}else{
			document.getElementById("inc_vat").innerHTML = respon[0];
			document.getElementById("exc_vat").innerHTML = "";
		}
		if(document.getElementById("inc_vat2") && (document.getElementById("inc_vat2")!="undefined"))
		document.getElementById("inc_vat2").innerHTML = (respon[6]?respon[6]:respon[0]);
		document.getElementById("product_price").value = (respon[6]?respon[6]:respon[0]);
		document.getElementById("product_weight").value = weight;
		document.getElementById("product_attribute_value_id").value = respon[7];
		
		if(respon[8]){
			document.getElementById("mail_stock").style.display = "block";
					
		}else{
			document.getElementById("mail_stock").style.display = "none";
		}
		
		if(respon[9]){
			
			document.getElementById("av_data").innerHTML = "This Product will be available on "+respon[9];
		}
		else{
			document.getElementById("av_data").innerHTML = "";
		}
		
		setTimeout('displayImages()', 1500);
   }
}
/***********************************************************************************/
function removeOption(optId){
	var elm = document.getElementById(optId);
	var prnt = elm.parentNode;
	prnt.removeChild(elm);
}
///////////////////////////////////////////////////////////////////////////////////
function displayImages(){
	
	var sub_hrefs = new Array();
	var main_a = document.getElementById("main_a");
	sub_image1 = document.getElementById("thumb1");
	sub_image2 = document.getElementById("thumb2");
	sub_image3 = document.getElementById("thumb3");
	sub_image4 = document.getElementById("thumb4");
	if(mimage1)
	document.getElementById("main_image").src = mimage1;
	else
	document.getElementById("main_image").src = orignal_main_image1;
	//document.getElementById("attrib_images").innerHTML =str;
	//document.getElementById("firstImage").src = himage1;
	if(image1){
		sub_image1.src = image1;
		sub_image1.onmouseover = function(){maximizeThis(mimage1);main_a.href = himage1;}
		main_a.href = himage1;
	}else{
		sub_image1.src = orignal_thum1;
		
	}
	if(image2){
		sub_image2.src = image2;
		sub_image2.onmouseover = function(){maximizeThis(mimage2);main_a.href = himage2;}
		
	}else{
		sub_image2.src = orignal_thum2;
		
	}
	
	if(image3){
		sub_image3.src = image3;
		sub_image3.onmouseover = function(){maximizeThis(mimage3);main_a.href = himage3;}
		
	}else{
		sub_image3.src = orignal_thum3;
		
	}
	
	if(image4){
		sub_image4.src = image4;
		sub_image4.onmouseover = function(){maximizeThis(mimage4);main_a.href = himage4;}
		
	}else{
		sub_image4.src = orignal_thum4;
		
	}
	
	var elms = document.getElementsByTagName("a");
	var j = 0;
	for(i=0; i<elms.length;i++){
		
		if(elms[i].rel){
			if(elms[i].className == 'myclass'){
				sub_hrefs[j++] = elms[i];
			}
			
		}
	}
	if(himage1){
		sub_hrefs[0].href = himage1;
	}
	if(himage2){
		sub_hrefs[1].href = himage2;
	}
	if(himage3){
		sub_hrefs[2].href = himage3;
	}
	if(himage4){
		sub_hrefs[3].href = himage4;
	}
	
	/*<!--document.getElementById("thumb1").src = image1;
	document.getElementById("thumb2").src = image2;
	document.getElementById("thumb3").src = image3;
	document.getElementById("thumb4").src = image4;-->*/
}

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



function preloadImages(){
	
	document.getElementById("main_image").src = root+'admin/images/wait.gif';
	document.getElementById("thumb1").src  =root+'admin/images/wait2.gif';
	document.getElementById("thumb2").src  =root+'admin/images/wait2.gif';
	document.getElementById("thumb3").src  =root+'admin/images/wait2.gif';
	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;
	 var ind = strpos(img_name,"/");
	 var himg = img_name.substr((ind+2),img_name.length);
	 document.getElementById("main_a").href = 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("<?//=$attribute_exist?>");
//"<?=$attribute_exist?>"	
	if(isexist){

		if(document.getElementById("main_attribute_id") && (document.getElementById("main_attribute_id") != "undefined")){
			if(document.getElementById("main_attribute_id").selectedIndex == 0)
			{
				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").selectedIndex == 0)
					{
						alert("Please select a sub attribute first");
						return false;
					}
				}
			}
		}
	}
	if(document.getElementById("product_attribute_value_id").value.length < 1){
		alert("Please wait a moment while product details fully loaded");
		return false;
	}
	return true;
}
/*************************************************/
function appendMainAttribValue(val){
/*	document.getElementById("tellafriendhref").href = "tellafriend.php?product_id=<?//=$_GET['product_id']?>&m_a_id="+val;
	append = document.getElementById("tellafriendhref").href ;
	//alert(document.getElementById("tellafriendhref").href);
	
*/
val = "";
}
function appendsubattributevalue(val){
	val = "";
	/*if(append){
		document.getElementById("tellafriendhref").href = append+"&s_a_id="+val;
	}*/
	//alert(document.getElementById("tellafriendhref").href);
}
function saveOrignalImages(){
	orignal_main_image1 = document.getElementById("main_image").src;
	orignal_thum1 = document.getElementById("thumb1").src;
	orignal_thum2 = document.getElementById("thumb2").src;
	orignal_thum3 = document.getElementById("thumb3").src;
	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;
}	