function addCookie(name, value, time)
{
	var date = new Date(); 
	date.setTime(date.getTime()+time*24*3600*1000);
	document.cookie = name + '=' + encodeURI(value) + '; expire=' + date.toGMTString;
}
function getCookie(name)
{
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
  	if(arr != null) return encodeURI(arr[2]); return null;
}
function deleteCookie(name)
{ 
	var date = new Date(); 
	date.setTime(date.getTime()-10000); 
	document.cookie = name + "=n; expire=" + date.toGMTString(); 
}
function getOs()   
{   
   var OsObject = "";   
   if(navigator.userAgent.indexOf("MSIE")>0) {   
        return "MSIE";   
   }
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){   
        return "Firefox";   
   }
   if(isSafari=navigator.userAgent.indexOf("Safari")>0) {   
        return "Safari";   
   }
   if(isCamino=navigator.userAgent.indexOf("Camino")>0){   
        return "Camino";   
   }
   if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){   
        return "Gecko";   
   }
} 
var cart_offset;
var btn_add_to_cart;
var script_name = '';
function btn_in_cart_offset(obj)
{
	btn_add_to_cart = obj;
	cart_offset     = obj.offset();
}
function add_to_cart(pid)
{	
	var form = $('input:checked');
	var qty  = $('#qty').val();
	var TransWay  = $('#TransWay').val();
	var zip_code  = $('#zip_code').val();
	
	if (zip_code == 'Zip Code') zip_code = '';
	
	var datas = '&pid=' + pid + '&qty=' + qty + '&act=add_to_cart' + '&TransWay=' + TransWay + '&zip_code=' + zip_code;
	for (i = 0; i < form.length; i++)
	{
		datas += '&' + form[i].name + '=' + form[i].value;
	}
//	window.location = "cart.php?" + datas;
	$.ajax({type: "POST",
			url: "cart.php",
			data:datas,
			dataType: 'json',
			success: function(response)
			{
				if (response.quick_cart != undefined)
				{
					$('#quick_cart').html(response.quick_cart);
					
					$('#in_cart_message').remove();
					
					btn_add_to_cart.before(
									'<div id="in_cart_message" class="t_l" style="display:block;width:190px;border:1px solid #b85243;height:50px;background:#fff url(/images/in_cart_box.png) left bottom;padding:5px;">'+
									'<div style="height:25px;" class="b ">My cart <span class="price">'+response.quick_cart+'</span> Items</div>'+
									'<div><input type="button" value="keep Shopping" onclick="$(\'#in_cart_message\').remove()" />'+
									'<input type="button" value="View Cart" onclick="window.location=\'/cart.php\'" style="margin-left:10px" /></div>'+
									'</div>'
									);
					if (script_name == '')
					{
						$('#in_cart_message').css({position:'absolute'});
					}
					else
					{
						$('#in_cart_message').css({position:'absolute', marginLeft: ''});
					}
					
				}
			}
		 });
}
function add_to_mywishlist(pid, wish_id, qty)
{
	var url = '/wish_list.php?act=append';
	url += '&p_id='+pid;
	url += '&wish_id='+wish_id;
	url += '&qty='+qty;
	
	$.ajax({type: "GET",
			url: url,
			dataType: 'text',
			success: function(response)
			{
				$('#in_cart_message').remove();
				btn_add_to_cart.before(
								'<div id=in_cart_message class="t_c" style="z-index:99;display:block;width:190px;border:1px solid #b85243;height:60px;background:#fff url(/images/in_cart_box.png) left bottom;padding:5px;">'+
								'<div style="height:40px;" class="b ">'+response+'</div>'+
								'<div><input type="button" value="keep Shopping" onclick="$(\'#in_cart_message\').remove()" />'+
								'<input type="button" value="View Wish" onclick="window.location=\'/wish_list.php\'" style="margin-left:10px" /></div>'+
								'</div>'
								);
				if (script_name == '')
				{
					$('#in_cart_message').css({position:'absolute'});
				}
				else
				{
					$('#in_cart_message').css({position:'absolute', marginLeft: ''});
				}
				
			}
		 });
}
function reset_imgsize(img_src, width, height, id)
{
	var ImgObj   = new Image();
	ImgObj.src   = img_src;
	var property = CheckProperty(ImgObj);
	
	if (property == false)
	{
		setTimeout("reset_imgsize('"+img_src+"', '"+width+"', '"+height+"', '"+id+"')", 500);
	}
	if (property.height < property.width)
	{
		if (width != 0)
		{
			var rate = property.width / width;
			rate_height = parseInt(property.height * rate);
			if (width < rate_height)
			{
				rate_height = width;
			}
			height = rate_height;
		}
		else if (height != 0 && property.width > height)
		{
			var rate = property.height / height;
			
			rate > 1 ? rate = 1 : '';
			rate_width = parseInt(property.width * rate);
			
			if (height < rate_width)
			{
				rate_width = height;
			}
			width = rate_width;
		}
	}
	else
	{
		width == 0 ? width = 'auto' : '';
		height == 0 ? height = 'auto' : '';		
	}
	width == 0 ? width = 'auto' : '';
	height == 0 ? height = 'auto' : '';
	if (id != '')
	{
		$('#'+id).css('width', width);
		$('#'+id).css('height', height);
	}
	return {"width":width ,"height":height}
}
function reset_imgsize_for_category(img_src, width, height, id)
{
	var ImgObj   = new Image();
	ImgObj.src   = img_src;
	var property = CheckProperty(ImgObj);
	
	if (property == false)
	{
		setTimeout("reset_imgsize_for_category('"+img_src+"', '"+width+"', '"+height+"', '"+id+"')", 500);
	}
	if (property.height < property.width)
	{
		if (width != 0)
		{
			var rate = property.width / width;
			rate_height = parseInt(property.height * rate);
			if (width < rate_height)
			{
				rate_height = width;
			}
			height = rate_height;
		}
		else if (height != 0 && property.width > height)
		{
			var rate = property.height / height;
			
			rate > 1 ? rate = 1 : '';
			rate_width = parseInt(property.width * rate);
			
			if (height < rate_width)
			{
				rate_width = height;
			}
			width = rate_width;
		}
	}
	else
	{
		width == 0 ? width = 'auto' : '';
		height == 0 ? height = 'auto' : '';		
	}
	width == 0 ? width = 'auto' : '';
	height == 0 ? height = 'auto' : '';
	if (id != '')
	{
		$('#'+id).css('width', width);
		$('#'+id).css('height', height);
	}
	return {"width":width ,"height":height}
}
function CheckProperty(obj)
{
	FileObj = obj;
	
	if(obj.complete == false)
	{
		setTimeout("CheckProperty(FileObj)", 500);
		return false; 
	}
	
	ImgFileSize=Math.round(obj.fileSize/1024*100)/100;
	
	ImgWidth=obj.width;
	
	ImgHeight=obj.height;
		
	return {"width":ImgWidth ,"height":ImgHeight}

}
