 /*            Mobile detection for iPhone, Android and Blackberry        */       

 
//mobile redirect function.
function mobile_redirect()
{
	//check the url to see a redirect is forced
	force_pc = get_url_vars()["force_pc"];
	if (force_pc == undefined)
	{	
		//check if the cookie is set to force the redirect
		if (get_cookie('force_pc') == undefined)
		{
			var device = navigator.userAgent.toLowerCase();         
			var iphone = device.indexOf('iphone') > -1;        
			var android = device.indexOf('android') > -1;       
			var blackberry = device.indexOf('blackberry') > -1;   
				 
			if (iphone && !getQueryString('mobile') || android && !getQueryString('mobile'))
			{            
				window.location = "http://m.tapenawines.com";       
			}
			if (blackberry && !getQueryString('mobile'))
			{            
				window.location = "http://m.tapenawines.com/index.php";        
			}
		}
	}
	else
	{
		// if a force_pc url check is used, set a cookie so that the rest of the user experience takes place on the pc site
		set_cookie('force_pc','true');
	}
}

/*            Get Query String        */        
function getQueryString(obj){           
	var string = window.location.search.substring(1);           
	var strings = string.split('&');          
	for (var i=0; i < strings.length; i++) 
	{
		var set = strings[i].split('=');  
		if (set[0] == obj) 
		{ 
			return set[1];
		}
	}
} 

// set the cookie
function set_cookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

// get the cookie value
function get_cookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
		{
		return unescape(y);
		}
	  }
}

// delete the cookie
function delete_cookie(name) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() - 1);
	document.cookie = name += "=; expires=" + expdate.toGMTString();
}

// get the $_GET variables from the url 
function get_url_vars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

