//	shop.js  

var	cartMessage='',
	now=new Date(),
	expires,
	cookieEnabled=(navigator.cookieEnabled)?1:0,
	AuId = '83153861',
	CaId = 'C7154192',
	NZId = 'C3151673',
	USId = '84206801',
	ToolsId = 'C5184078',
	AuServer = 'http://www6.aitsafe.com',
	CaServer = 'http://ww10.aitsafe.com',
	NZServer = 'http://ww10.aitsafe.com',
	USServer = 'http://www6.aitsafe.com',
	vServer = USServer, 				// default the shopping cart server to the U.S.
	joinFee = 10.00;	

function addDays(x,n){
	z=new Date();
	z.setDate(x.getDate()+n);
	return z
} 

tomorrow=addDays(now,1); 

function cookieWarn(){
 	if(!cookieEnabled)
	 alert('Cookies must be enabled to use this function')
} 
function cookieSet(name,value,expires,path,domain,secure){
 	cookieWarn()
	if(name){ 
		var cx=name+"="+escape(value)+
			((expires)?"; expires="+expires.toGMTString():"")+
			((path)?"; path="+path:"")+
			((domain)?"; domain="+domain:"")+
			((secure)?"; secure":"") 		
		if((escape(value)).length>3995) alert("Cookie exceeds 4KB and will be cut!")
 		document.cookie=cx 	
	}
} 
function cookieGet(name){
 	cookieWarn();
 	thisCookie=document.cookie.split("; ");
	for(var i=0;i<thisCookie.length;i++)
	  if(name==thisCookie[i].split("=")[0])
		return unescape(thisCookie[i].split("=")[1]);
 	return null
} 
function cookieDelete(name,path,domain){
 	cookieWarn();
 	if(cookieGet(name))
	 cookieSet(name,'/',addDays(now,-1),path,domain)
//	 cookieSet(name,'',(new Date(1)),path,domain)
} 

// special function to clear all cookie products
function cookieClearP(){
 	cookieWarn();

 	thisCookie=document.cookie.split("; ");
	for(var i=0;i<thisCookie.length;i++)
	  if("P"==thisCookie[i].split("=")[0].substring(0,1))
		cookieDelete(thisCookie[i].split("=")[0]);		
}

// find value x in array arr at element elm return array index
function findXinArray(x,arr,elm){
 	for(var i=0;i<arr.length;i++)
	 if(arr[i][elm]==x)
	   return i
} 

function isInt(x){
	var y=parseInt(x); 
	if(isNaN(y)) return false; 
	return x==y&&x.toString()==y.toString(); 
} 

function trim(s){
	while(s.substring(0,1)==' ') 
	    s=s.substring(1,s.length)   
	while(s.substring(s.length-1,s.length)==' ') 
	    s=s.substring(0,s.length-1)   
	return s 
} 

function cartInc(p,i,s){
	var obj=document.getElementById('cart'+p)
	var x=parseInt(obj.value)
	var oldCo = cookieGet('Co');

	// set the cookie for shopping cart id
	cookieSet('Co',s,tomorrow,'/')

	// check if the country changed
	if (oldCo != s)
		// clean up the products
		cookieClearP();

	if(!x||x==' ') x=0
	if(!isInt(x)){alert('Entry requires an integer 0,1,2...');x=0}
	x=Math.max(0,x+i)
	x=x>0?x:''
	obj.value=x
	x>0?cookieSet('P'+p,x,tomorrow,'/'):cookieDelete('P'+p,'/')
}

function gotoCart(pCo){
	var Co = cookieGet('Co');
	var Ca = CaId.replace(/C/, "0");
	var Nz = NZId.replace(/C/, "0");

	if (!Co) Co=pCo;
	Co=Co.replace(/C/, "0");	
	
	switch (parseInt(Co))
	{
	case parseInt(AuId): 
		document.location.href = "http://www.myyoungevity.com/shop_new/au/cart.php"; 	
		break;
	case parseInt(Ca): 	
		document.location.href = "http://www.myyoungevity.com/shop_new/ca/cart.php"; 	
		break;
	case parseInt(Nz): 	
		document.location.href = "http://www.myyoungevity.com/shop_new/nz/cart.php"; 	
		break;
	case parseInt(USId): 	
		document.location.href = "http://www.myyoungevity.com/shop_new/us/cart.php"; 	
		break;
	default: 	
		document.location.href = "http://www.myyoungevity.com/shop_new/tt/cart.php"; 	
		break;
	}
}