var clearMePreviousKeyword = '';
var clearMePreviousEmail = '';
jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

$(document).ready(function () {
					
	$('.jqurey-qty-input').keydown(function(e) {
	   switch( e.keyCode ) {
			case 9:case 97:case 98:case 96:case 48:case 99:case 100:case 101:case 102:case 103:case 104:case 105:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:case 37:case 39:case 8:case 13:case 46:return;}
		e.preventDefault();
    });
	
	$('#NewsLetterSignUpEmail').focus(function()	{
		clearMePreviousEmail = $(this).val();
		$(this).val('') ;
	});

	$('#NewsLetterSignUpEmail').blur(function()	{
		if($(this).val()=='')
		{
			$(this).val(clearMePreviousEmail);
		}
	});

	$("#NewsLetterSignUp").click(function () {
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php' ,
		  dataType: "html",
		  data: 'actType=addNewsLetter&email='+$("#NewsLetterSignUpEmail").val(),
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
			 $("#NewsLetterSignUpEmail").val('');
			alert(data);	 
		  })
		});
		return false;
	});
	
	$("#SearchKeyword").keypress(function(event) {
		var a = $(this).val();
		a = $.trim(a);
	  	if ( event.which == 13 && a != '' && a != 'Keyword or item#' ) {
			$('#search').trigger("click");
		}
		return true;
	});

	$("#search").click(function () {
		var s = $("#SearchKeyword").val();
		s = $.trim(s);
		if(s == '' || s == 'Keyword or item#' ) {
			alert("Please enter search keywords.");
			return false;
		}
		window.location = SITE_URL + 'search/'+ s;
		return false;
	});
	
	$("#country").change(function () {
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php',
		  dataType: "html",
		  data: 'actType=getStates&cId='+$("#country").val(),
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
			 $("#stateAjax").html(data);
		  })
		});
		return false;
	});

	$("#AddToCart").click(function () {
		var isRedirectToCart;
		var tmpStr = '&pgroup='+$('#pGroup').val() + '&prodqty='+ $('#ProdQty').val();
		if($('#Metal').exists())	{
			tmpStr = tmpStr + '&metal='+$('#Metal').val();
		}
		if($('#RingSize').exists())	{
			tmpStr = tmpStr + '&ringsize='+$('#RingSize').val();
			var a = $('#RingSize').attr('selectedIndex');
			var p = $("#SizePrice"+a).attr('ishash');
			tmpStr = tmpStr + '&ishash='+p;
		}
		if($('#ChainLength').exists())	{
			tmpStr = tmpStr + '&chainlength='+$('#ChainLength').val();
			var a = $('#ChainLength').attr('selectedIndex');
			var p = $("#ChainLengthPrice"+a).attr('ishashCL');
			tmpStr = tmpStr + '&ishashCL='+p;
		}
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'addtocart',
		  dataType: "html",
		  data: 'pid='+$(this).attr('pid')+'&cid='+$(this).attr('cid')+tmpStr,
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
		     $('#OverLayer').css({'width':$(document).width(),'height':$(document).height()}); 
			 $('#OverLayer').show();
 			 $("#CartProductDetail").html(data);
			 $('#CartProductDetail').center();
			 $('#CartProductDetail').show();
			 
			 $("#AddToCartClose").click(function () {
				$('#OverLayer').hide();
				$('#CartProductDetail').hide();
				$("#CartProductDetail").html('');
				return false;
			 });

			$("#AddToCartContinue").click(function () {
				isRedirectToCart = false;
				AddToCartAction();
				return false;
			 });
		 	
			 $("#AddToCartCheckout").click(function () {
				isRedirectToCart = true;
				AddToCartAction();
				return false;
			 });

			function AddToCartAction () {
				$.ajax({
				  type: 'POST',
				  url: SITE_URL + 'ajax_front.php',
				  dataType: "html",
				  data: 'actType=AddToCart&pid='+$("#pid").val()+'&cid='+$("#cid").val()+'&prodqty='+$("#ProdAddQty").val()+"&hashRingSize="+$("#hashRingSize").val()+"&hashChainLength="+$("#hashChainLength").val(),
				  beforeSend: function() {
					 $('#AjaxWaiting').show();
				  },
				  success: (function(data, status) {
					 $('#AjaxWaiting').hide();
					 $('#OverLayer').hide();
					 $('#CartProductDetail').hide();
					 $("#CartProductDetail").html('');
					 if(isRedirectToCart == true) { 
					 	window.location = SITE_URL + 'shoppingcart';
					 }
					 setJSONdata(data);
				 })
				});
				return false;
			 }
		  })
		});
		return false;
	});

	/* Gemes Add to cart start */
	$("#AddToGems").click(function () {
		var isRedirectToCart;
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'addtogems',
		  dataType: "html",
		  data: 'gid='+$(this).attr('gid'),
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
		     $('#OverLayer').css({'width':$(document).width(),'height':$(document).height()}); 
			 $('#OverLayer').show();
 			 $("#CartProductDetail").html(data);
			 $('#CartProductDetail').center();
			 $('#CartProductDetail').show();
			 
 			 $("#AddToCartClose").click(function () {
				$('#OverLayer').hide();
				$('#CartProductDetail').hide();
				$("#CartProductDetail").html('');
				return false;
			 });
			$("#AddToCartContinue").click(function () {
				isRedirectToCart = false;
				AddToCartAction();
				return false;
			 });
			 $("#AddToCartCheckout").click(function () {
				isRedirectToCart = true;
				AddToCartAction();
				return false;
			 });
			function AddToCartAction () {
				$.ajax({
				  type: 'POST',
				  url: SITE_URL + 'ajax_front.php',
				  dataType: "html",
				  data: 'actType=AddToGems&pid='+$("#pid").val(),
				  beforeSend: function() {
					 $('#AjaxWaiting').show();
				  },
				  success: (function(data, status) {
					 $('#AjaxWaiting').hide();
					 $('#OverLayer').hide();
					 $('#CartProductDetail').hide();
					 $("#CartProductDetail").html('');
					 if(isRedirectToCart == true) { 
					 	window.location = SITE_URL + 'shoppingcart';
					 }
				 })
				});
				return false;
			 }
		  })
		});
		return false;
	});
	/* Gemes Add to cart end */

	$(".MultiAddToCartBtn").click(function () {
		var chkCnt = 0;
		var allProdId = [];
		var allProdQty = [];
		var allProdCat = [];
		var q = '';
		var c = '';
		$("INPUT[@class='prodCheckBox'][type=checkbox]:checked").each(function(){
			chkCnt += 1;
			q = $('#itemqty'+$(this).val()).val();
			c = $('#itemqty'+$(this).val()).attr('cid');
			if(q > 0)	{
				allProdId.push($(this).val());
				allProdQty.push(q);
				allProdCat.push(c);
			}
		});
		if(chkCnt <= 0 ) {
			alert("Please select product(s) to add into cart.");
			return false;
		}
		if(allProdQty.length <= 0 ) {
			alert("Please enter product(s) quantity to add into cart.");
			return false;
		}
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php',
		  dataType: "html",
		  data: 'actType=MultiAddToCart&ProdId='+allProdId+'&ProdQty='+allProdQty+'&ProdCat='+allProdCat,
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
		 	 window.location = SITE_URL + 'shoppingcart';
		  })
		});
		return false;
	});

	function setJSONdata(data) {	
		if(data == '') return;
		 var objJSON = jQuery.parseJSON(data);
		 $('#SubTotal').html(objJSON.SubTotal);
		 $('#TotalQty').html(objJSON.TotalQty);
	}
	
	$(".RemoveItem").click(function () {
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php',
		  dataType: "html",
		  data: 'actType=DeleteToCart&idx='+$(this).attr('idx'),
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
			 location.reload();
		  })
		});
		return false;
	});

	$(".IncDecItemQty").click(function () {
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php',
		  dataType: "html",
		  data: 'actType=IncDecItemQty&idx='+$(this).attr('idx')+'&by='+$(this).attr('by'),
		  beforeSend: function() {
			 $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
			 location.reload();
		  })
		});
		return false;
	});

	$("#OrderConfirm").click(function () {
	 	$('#AjaxWaiting').show();
		if($("#OrderConfirm").attr("isClicked") == 1 )
			return false;
		$("#OrderConfirm").attr("isClicked", "1");
 		window.location = SITE_URL + 'orderprocess';
	});
	$("#PrintinPopup").click(function() {
       //printElem({ printMode: 'popup' });
	   var header = $("#OrdReceiptHeader").html();
	   printElem({ leaveOpen: true, printMode: 'popup', pageTitle: 'Print Order Receipt', pageHeader: header });
    });

	$("#PrintinProdDetail").click(function() {
	   printElem({ leaveOpen: true, printMode: 'popup', pageTitle: 'Print Product Detail', pageHeader: '' });
    });

	$("a.detail_tab").click(function() {
		var t = $("a.detail_taba");
		t.removeClass("detail_taba");
		t.addClass("detail_tab");
		$(this).removeClass("detail_tab");	
		$(this).addClass("detail_taba");
		var showid = $(this).attr('showID');
		$("div.detail_tab").hide();
		$("#"+showid).show();
	});

	$(".OpenTab").click(function() {
		var t = $(this).attr('showID');
		$('#'+t).trigger("click");
	});
	

	$("#Metal").change(function() {
		var a = $(this).attr('selectedIndex');
		var u = $("#metalUrl"+a).val();
 		window.location = u;
	});

	$("#RingSize").change(function() {
		var a = $(this).attr('selectedIndex');
		var u = $("#SizePrice"+a).val();
		var p = $("#SizePrice"+a).attr('ishash');
		$("#FinalPrice").html('$'+u);
	});
	$('#RingSize').trigger("change");

	$("#ChainLength").change(function() {
		var a = $(this).attr('selectedIndex');
		var u = $("#ChainLengthPrice"+a).val();
		$("#FinalPrice").html('$'+u);
	});
	$('#ChainLength').trigger("change");

	$("#PayPalButton").click(function() {
	     $('#AjaxWaiting').show();
	});
	$("#GoogleButton").click(function() {
	     $('#AjaxWaiting').show();
	});
	
	
	$("#DefaultActiveTab").click(function() {
		$(this).removeClass("detail_tab");	
		$(this).addClass("detail_taba");
	});
	$('#DefaultActiveTab').trigger("click");

////// FOR LEFT FRAME//////////////////
	$(".leftalphabet").click(function() {
      	var alpha = $(this).html();
		LeftAlphabet(alpha);
		$(".leftalphabet").removeClass("leftalphabetAct");
		$(this).addClass("leftalphabetAct");	
		//return false;
	});
	$("#leftalphabetActive").click(function() {
		var alpha = $(this).html();
		LeftAlphabet(alpha);
		$("#leftalphabetActive").addClass("leftalphabetAct");
		return false;
	});
	$('#leftalphabetActive').trigger("click");
	function LeftAlphabet(alpha)	{
		var alpha = alpha;
		var alphacatid = $('#alphacatid').val();
		$.ajax({
		  type: 'POST',
		  url: SITE_URL + 'ajax_front.php',
		  dataType: "html",
		  data: 'actType=GetLeftFrame&alpha='+alpha+'&category_id='+alphacatid,
		  beforeSend: function() {
		     $('#AjaxWaiting').show();
		  },
		  success: (function(data, status) {
			 $('#AjaxWaiting').hide();
			 $('#wholeframe').html(data);
			 scrolldiv(alpha);
		  })
		});
		return false;
	}

////// FOR LEFT FRAME//////////////////

	$(".popupWindow").click(function() {
		var defaultSetting =  {"toolbar" : "no", "location" : "no", "directories":"no", "status" : "no",   "menubar" : "no", "scrollbars":"no", "resizable":"no", "copyhistory":"no", "height":"300" , "width":"300" };
		var popupSetting = '';	
		var pageURL = '';
		var title = '';
		pageURL = $(this).attr("href");
		title = 'View_Image';
		mysettings = ($(this).attr("rel") || "").split(",");
		if(mysettings.length > 0)	{
			for(var i=0; i < mysettings.length; i++)
			{
				b = mysettings[i].split(":");
				if(b.length > 0)	{
					defaultSetting[ b[0] ] = b[1];
				}
			}
		}
		var left = (screen.width/2)-( defaultSetting['width'] /2);
		var top = (screen.height/2)-(defaultSetting['height']/2);
		popupSetting = 'toolbar='+defaultSetting['toolbar']+', location='+defaultSetting['location']+', directories='+defaultSetting['directories']+', status='+defaultSetting['status']+', menubar='+defaultSetting['menubar']+', scrollbars='+defaultSetting['scrollbars']+', resizable='+defaultSetting['resizable']+', copyhistory='+defaultSetting['copyhistory']+', width='+defaultSetting['width']+', height='+defaultSetting['height']+', top='+top+', left='+left;
		var targetWin = window.open (pageURL, title, popupSetting);
		return false;
	});
	
});

function getFormDateString(FormID) {
	if(FormID == '') return null;
	
	var dataString = '';
	var $inputs = $('#'+FormID+' :input');
	var values = {};
	$inputs.each(function() {
		if( $(this).attr('type') != 'radio')	{
			values[this.name] = $(this).val();
		} else {
			if( $(this).is(':checked') && $(this).attr('type') == 'radio')	{
				values[this.name] = $(this).val();
			}
		}
	});
	for (i in values)
	{
		dataString = dataString + i + '=' + values[i] + '&';
	}
	dataString = dataString.substring(0, dataString.length - 1);
	return dataString;
}

function printElem(options){
     $('#toPrint').printElement(options);
}
function bookmarksite()
{

	var bookmarkurl=window.location;
	var bookmarktitle=window.location;
	if (document.all)
	{
	window.external.AddFavorite(bookmarkurl,bookmarktitle);
	}
	else if (window.sidebar)
	{
	window.sidebar.addPanel(bookmarkurl,bookmarktitle,"");
	}
	else if(window.opera && window.print)
	{ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',bookmarkurl);
	elem.setAttribute('title',bookmarktitle);
	elem.setAttribute('rel','sidebar');
	elem.click();
	}
	else
	{
	alert('Please press ctrl+D');
	}

}
/* Function use for Search Diamonds start */
function funFrmHPDSearch()
{
	var frm = document.frmDiamondSearch;
	var tot_ds_shape = frm.sel_shape_id.length;
	var ds_flag = false;
	if ( tot_ds_shape > 0 )
	{
		for ( var k=0; k < tot_ds_shape; k++ )
		{
			if(frm.sel_shape_id[k].checked)
			{
				ds_flag = true;
			}	
				
		}
	}
	// delete the cookies for DS
	deleteDiamondSearchCookies();
	
	if ( ds_flag == false )
	{
		alert("Please select diamond shape for search.");
		return false;
	}	
	
	return true;
}
function clickHPDSShapes(id)
{		
	var frm = document.frmDiamondSearch;
	
	var tot_ds_shape = frm.sel_shape_id.length;
	
	frm.sel_shape_id[id].checked = true;
	
	var sImgId = '';
	
	if ( tot_ds_shape > 0 )
	{
		for ( var k=0; k < tot_ds_shape; k++ )
		{
			sImgId = "img_ds_shape_id_"+k;
			
			if(frm.sel_shape_id[k].checked)
			{							
				sImgURL = document.getElementById(sImgId).getAttribute('src');
								
				sImgURL = sImgURL.replace(/_in.png/i, "_a.png");
				
				document.getElementById(sImgId).setAttribute('src',sImgURL);
			}
			else
			{
				sImgURL = document.getElementById(sImgId).getAttribute('src');
								
				sImgURL = sImgURL.replace(/_a.png/i, "_in.png");
				
				document.getElementById(sImgId).setAttribute('src',sImgURL);
			}	
		}
	}
}
/* Function use for Search Diamonds End */

/* These functions are used for the Product Detail Page More image view start*/
var timerLeft="";
var timerRight="";
var scrollStep=1;	

function funScrollDivLeft(id)
{
  clearTimeout(timerRight); 
  $("#"+id).scrollLeft($("#"+id).scrollLeft()+scrollStep);
  timerRight=setTimeout("funScrollDivLeft('"+id+"')",10);
}
function funScrollDivRight(id)
{
  clearTimeout(timerLeft)
  $("#"+id).scrollLeft($("#"+id).scrollLeft()-scrollStep);
  timerLeft=setTimeout("funScrollDivRight('"+id+"')",10);
}
		
function funStopScrollDiv()
{
  clearTimeout(timerRight); 
  clearTimeout(timerLeft);
}
	
function funShowHideScrollBtn()
{
	if(document.getElementById('exPrdImgThumbTrId'))
	{
		var count = 0, child = document.getElementById('exPrdImgThumbTrId').firstChild;
		
		while (child) 
		{
		  if (child.nodeType == 1)
		  	++count;
		  child = child.nextSibling;
		}
		
		if(count>maxExImgFirstTime)
		{
			if(document.getElementById('scrollLeftArrowId'))
				document.getElementById('scrollLeftArrowId').style.display='block';
			if(document.getElementById('scrollRightArrowId'))
				document.getElementById('scrollRightArrowId').style.display='block';
		}else
		{
			if(document.getElementById('scrollLeftArrowId'))
				document.getElementById('scrollLeftArrowId').style.display='none';
			if(document.getElementById('scrollRightArrowId'))
				document.getElementById('scrollRightArrowId').style.display='none';
		}
	}
}
/* This functions are used for the Product Detail Page More image view end*/


function Print_Page(product_id,cid)
{
	var disp_setting="toolbar=0,location=0,directories=1,menubar=1,scrollbars=1,resizable=1,width=800, height=600, left=100, top=0"; 
	
	var filename = SITE_URL+'index.php?file=product&pid='+product_id+'&cid='+cid+'&isPrintOpt=1';
	window.open (filename,'pop',disp_setting);
	return false;
}

