//	shop.js  

var	cartMessage='',
	now=new Date(),
	expires,
	cookieEnabled=(navigator.cookieEnabled)?1:0,
	AuId = '83153861',
	CaId = 'C7154192',
	NZId = 'C3151673',
	ToolsId = 'C5184078',
	USId = '84206801',
	AuServer = 'http://www6.aitsafe.com',
	CaServer = 'http://ww10.aitsafe.com',
	NZServer = 'http://ww10.aitsafe.com',
	ToolsServer = '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
} 

expires=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,'',(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][0])
		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,expires,'/')

	// 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,expires,'/'):cookieDelete('P'+p,'/')
}

function idSet(pId){
	var s=window.location.href,
	    u=s.substr(s.indexOf('?')+1, (s.indexOf('*')) - (s.indexOf('?')) - 1),	 	   	// user
	    n=s.substr(s.indexOf('*')+1, (s.indexOf('=')) - (s.indexOf('*')) - 1),	 	   	// name
    	    p=s.substr(s.indexOf('=')+1, (s.indexOf('+')) - (s.indexOf('=')) - 1),		    	// price
	    t=s.substr(s.indexOf('+')+1, (s.lastIndexOf('?')) - (s.indexOf('+')) - 1),    		// tax
	    r=s.substr(s.lastIndexOf('?')+1, s.length), 							// return path
	    yIdText="Youngevity+Membership+Id+++"+pId;

	// replace all spaces in the name with '+'
	u=u.replace(/ /g,'+');
	n=n.replace(/ /g,'+');
	p=p.replace(/ /g,'+');
	t=t.replace(/ /g,'+');
	r=r.replace(/ /g,'+');

	if (pId > 0) {
	  // set the memberId cookie	
	  cookieSet('memberId',pId,expires,'/');

	  // set the vServer and joining fee according to the current user
	  if (u == AuId){
	    vServer = AuServer;
	    joinFee = 12.00;	   
 	  }
	  else if (u == CaId){
	    vServer = CaServer;   
	    joinFee = 10.00;	   
	  }
	  else if (u == NZId){
	    vServer = NZServer;   
	    joinFee = 13.00;	   
	  }
	  else if (u == ToolsId){
	    vServer = ToolsServer;   
	    joinFee = 0.00;	   
	  }

	  // go to shopping cart
	  window.open(vServer+"/cf/addmulti.cfm?userid="+u+"&qty1=1&product1="+yIdText+"&price1=0.00&tax1=0.0&qty2=1&product2="+n+"&price2="+p+"&tax2="+t+"&return="+r, "_self");
	}
	else 
	  alert("Please enter your Youngevity id or register as a Youngevity member!");
}

function setNewMember(){
	var s=window.location.href,
	    u=s.substr(s.indexOf('?')+1, (s.indexOf('*')) - (s.indexOf('?')) - 1),	 	   	// user
	    n=s.substr(s.indexOf('*')+1, (s.indexOf('=')) - (s.indexOf('*')) - 1),	 	   	// name
    	    p=s.substr(s.indexOf('=')+1, (s.indexOf('+')) - (s.indexOf('=')) - 1),		    	// price
	    t=s.substr(s.indexOf('+')+1, (s.lastIndexOf('?')) - (s.indexOf('+')) - 1),    		// tax
	    r=s.substr(s.lastIndexOf('?')+1, s.length);								// return path

	// replace all spaces in the name with '+'
	u=u.replace(/ /g,'+');
	n=n.replace(/ /g,'+');
	p=p.replace(/ /g,'+');
	t=t.replace(/ /g,'+');
	r=r.replace(/ /g,'+');

	// set the new member cookie
	cookieSet('newMember','1',expires,'/');

	// set the vServer and joining fee according to the current user
	if (u == AuId){
	  vServer = AuServer;
	  joinFee = 12.00;	   
	}
	else if (u == CaId){
	  vServer = CaServer;   
	  joinFee = 10.00;	   
	}
	else if (u == NZId){
	  vServer = NZServer;   
	  joinFee = 13.00;	   
	}
	else if (u == ToolsId){
	  vServer = ToolsServer;   
	  joinFee = 0.00;	   
	}

	// go to shopping cart
	window.open(vServer+"/cf/addmulti.cfm?userid="+u+"&qty1=1&product1=Youngevity+Membership+Pack&price1="+joinFee+"&tax1=0.0&qty2=1&product2="+n+"&price2="+p+"&tax2="+t+"&return="+r, "_self");
}

function AddItem(pUser,pName,pPrice,pTax,pReturn){

	  var vmemberId=cookieGet('memberId'),
	      vnewMember=cookieGet('newMember');

	  // check the user (which country) and set the correct server name
	  if (pUser == AuId){
	    vServer = AuServer;
	    joinFee = 12.00;	   
	  }
	  else if (pUser == CaId){
	    vServer = CaServer;   
	    joinFee = 10.00;	   
	  }
	  else if (pUser == NZId){
	    vServer = NZServer;   
	    joinFee = 13.00;	   
	  }
	  else if (pUser == ToolsId){
	    vServer = ToolsServer;   
	    joinFee = 0.00;	   
	    vmemberId = 1;
	  }

	  // check if the cookies memberId (Youngevity membership id) OR newMember (set when a customer purchases a Youngevity membership) has been set
	  if (vmemberId || vnewMember ){

	    // customer has ordered previously in this session
	    if (vmemberId) 
	      // set vMsg to memberId
	      vMsg = vmemberId;	
	    else
	      vMsg = "Your new Youngevity membership id will be in your Membership pack."

	    // pass vMsg to the shopping cart to display     
	    // window.open("http://www6.aitsafe.com/cf/add.cfm?userid="+pUser+"&product="+pName+"&price="+pPrice+"&tax=6.25&return="+pReturn, "_self");
	    window.open(vServer+"/cf/add.cfm?userid="+pUser+"&product="+pName+"&price="+pPrice+"&tax=6.25&return="+pReturn, "_self");
	  }	
	  else{
	    // open the new member page - user will either purchase a new membership pack or input their Youngevity id
	    // window.open("/shop/member.php?"+pName+'='+pPrice+'+'+pTax+'?'+pReturn, "_self");
	    window.open("/shop/member.php?"+pUser+'*'+pName+'='+pPrice+'+'+pTax+'?'+pReturn, "_self");
	  }
}