/* Ajax Function declaration Start */
function GetXmlHttpObject() 
{
	var xmlHttpGnl=null;
	try 
	{
		//Firefox, Opera 8.0+, Safari
		xmlHttpGnl=new XMLHttpRequest();
	} 
	catch (e) 
	{
		try 
		{
			//Internet Explorer
			xmlHttpGnl=new ActiveXObject("Msxm12.XMLHTTP");
		} 
		catch(e) 
		{
			xmlHttpGnl=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttpGnl;
}
/* Ajax Function declaration end */

var GoToShoppingCartPage = false;
function DisplayLightBoxAddToCart (pid , pqty)	
{
	GetProductDetail (pid) ;
	document.getElementById('fadeCart').style.display='block';
	document.getElementById('lightCart').style.display='block';

	arrayPageSizeWithScroll = getPageSizeWithScroll();
	document.getElementById('fadeCart').style.height = arrayPageSizeWithScroll[1] + "px";

	showdeadcenterdiv(700,400,'lightCart');
	document.getElementById("AddToCartContinue").onclick = function()
	{
			AjaxAddToCart(pid , pqty);
	} 
	document.getElementById("AddToCartCheckout").onclick = function()
	{
			AjaxAddToCart(pid , pqty);
			GoToShoppingCartPage = true;
	} 	
	return false;
}

function getCheckedValue(radioObj) 
{
	if(!radioObj) 	return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}


function GetShippingCharge ()	
{
		xmlHttpAddtoCart=GetXmlHttpObject();			
		if(xmlHttpAddtoCart == null) 
		{
			alert("Browser doesnt support ajax");
			return;
		}
		
		var m 	 = getCheckedValue(document.forms['billing'].elements['shippingId']);
		if(m <= 0 ) 
		{
			alert('Please select shipping method.');
			return false;
		}
		
		if(document.getElementById('asbill').checked == true ) 
		{
			var z 	 = document.getElementById('bl_zip').value;
			var c 	 = document.getElementById('bl_country').value;;
			if(c == 'US')
				var s 	 = document.getElementById('bl_state').value;
			else 
				var s 	 = document.getElementById('votherstate').value;
			
		}	
		else 
		{
			var z 	 = document.getElementById('sh_zip').value;
			var c 	 = document.getElementById('sh_country').value;
			if(c == 'US')
				var s 	 = document.getElementById('sh_state').value;
			else 
				var s 	 = document.getElementById('sh_otherstate').value;

		}
		
		var url="GetShippingCharge.php?m="+m+"&z="+z+"&s="+s+"&c="+c;
		
		xmlHttpAddtoCart.onreadystatechange=GetShippingCharge_response;
		xmlHttpAddtoCart.open("GET",url,true);
		xmlHttpAddtoCart.send(null);
		return false;
		
		function GetShippingCharge_response () 
		{
			if(xmlHttpAddtoCart.readyState==4 || xmlHttpAddtoCart.readyState=="complete") 	
			{
				response=xmlHttpAddtoCart.responseText;
				//alert(response);
				var response_ary = response.split('##'); 
				document.getElementById('ship').innerHTML  = response_ary[0];
				document.getElementById('tax').innerHTML   = response_ary[1];
				document.getElementById('GrandTotal').innerHTML   = response_ary[2];
				return false;
			}		
		}
}

function LTrim(str) 
{
   var whitespace = new String(" tnr");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {

      var j=0, i = s.length;

      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)	
{
   var whitespace = new String(" tnr");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

      var i = s.length - 1;       

      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

/*=============Product Compare JS Start======================*/
function Ajax_Product_Compare_Link(products_id,flg_chk)
{
	//var flg_chk = obj.checked;
	//var products_id = obj.value;
	
	/*alert('ckh'+flg_chk);
	alert('val'+products_id);*/
	if(products_id =="") 
		return false;	
	
	var action = "add";
	if(flg_chk == false)
		action = "remove";
		
	//alert(action);
	
	xmlHttp=GetXmlHttpObject();			
	if(xmlHttp == null) 
	{
		alert("Browser doesnt support ajax");
		return;
	}
	
		
	var url=Site_URL+"script/product_compare_ajax.php?action="+action+"&products_id="+products_id;
	
	xmlHttp.onreadystatechange=Ajax_Product_Compare_Response;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function Ajax_Product_Compare(obj)
{
	var flg_chk = obj.checked;
	var products_id = obj.value;
	
	if(products_id =="") 
		return false;	
	
	var action = "add";
	if(flg_chk == false)
		action = "remove";
		
	//alert(action);
	
	xmlHttp=GetXmlHttpObject();			
	if(xmlHttp == null) 
	{
		alert("Browser doesnt support ajax");
		return;
	}
	
		
	var url=Site_URL+"script/product_compare_ajax.php?action="+action+"&products_id="+products_id;
	
	xmlHttp.onreadystatechange=Ajax_Product_Compare_Response;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function Ajax_Product_Compare_Response()
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		response=xmlHttp.responseText;
		if(response!="")
		{
			//alert(response);
			popup_show('compare_msg_popup', 'compare_msg_drag', 'compare_msg_exit', 'screen-center', 50, -200);
		}
		return false;
	}
}

/*=============Product Compare JS End======================*/

/* News Letter sign up java script declaration Start */
function Ajax_News_Letter_SignUp()	
{
	var flg_news = Validate_News_Letter_Email() ;
	if(flg_news == false) return false;		
	xmlHttp=GetXmlHttpObject();			
	if(xmlHttp == null) 
	{
		alert("Browser doesnt support ajax");
		return;
	}
	
	var url="Ajax_News_Letter_SignUp.php?eid=" + document.getElementById('news_email').value;
	xmlHttp.onreadystatechange=Ajax_News_Letter_SignUp_response;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function Ajax_News_Letter_SignUp_response() 
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		response=xmlHttp.responseText;
		alert(response);
		document.getElementById('news_email').value = '';
		document.getElementById('news_email').focus();
		return false;
	}
}

function Validate_News_Letter_Email()
{
	var emailID=document.getElementById('news_email');

	if ((emailID.value==null)||(emailID.value==""))
	{
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	
	if (echeck(emailID.value)==false)
	{
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
}

function echeck(str) 
{
	if (str=="") 
	{
		alert("Please Enter your Email ID");
		return false;
	}
	else
	{
		var str=str;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		{
				sliptarr = str.split("@");
				var emailRegEx1 = /[a-zA-Z]+/;
				if(sliptarr[0].match(emailRegEx1))
				{
				}
				else
				{
					alert("Please input a valid email address.")
					return false;
				}
			
		}
		else
		{
			alert("Please input a valid email address.")
			return false;
		}
	}
 return true					
}
/* News Letter sign up java script declaration end */


function showdeadcenterdiv(Xwidth,Yheight,divid) 
{
	var scrolledX, scrolledY;
	if( self.pageYOffset ) 
	{
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} 
	else if( document.documentElement && document.documentElement.scrollTop ) 
	{
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} 
	else if( document.body ) 
	{
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}
	
	// Next, determine the coordinates of the center of browser's window
	
	var centerX, centerY;
	if( self.innerHeight ) {
	centerX = self.innerWidth;
	centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	centerX = document.documentElement.clientWidth;
	centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
	centerX = document.body.clientWidth;
	centerY = document.body.clientHeight;
	}
	
	// Xwidth is the width of the div, Yheight is the height of the
	// div passed as arguments to the function:
	var leftOffset = scrolledX + (centerX - Xwidth) / 2;
	var topOffset = scrolledY + (centerY - Yheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	var o=document.getElementById(divid);
	var r=o.style;
	r.position='absolute';
	r.top = topOffset + 'px';
	r.left = leftOffset + 'px';
	r.display = "block";
} 
	


function getPageSizeWithScroll()	
{
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function RemoveLightBoxAddToCart ()	
{
	document.getElementById('fadeCart').style.display='none';
	document.getElementById('lightCart').style.display='none';
	return false;
}

// Convert the no to float with 2 decimal places
function funConvertToFloat(str, points) 
{
	str = new String(str).replace(/,/g, '');
	var num = parseFloat(str).toFixed(points || 2);
	return num;
}
// format the number 
function funNumberFormat(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

