Checking to see if vistor is on a mobile device

Checking to see if vistor is on a mobile device

I am trying to read all mobile devices so that I can redirect them to a mobile site. Once they go to that site they will then have a choice to go to the full site. This isn’t working on iphone devices as they won’t redirect to the full site when it’s clicked.

// Checks to see if the visitor is on a mobile device, and if so, redirects to mobile site.
if (wireless()) {
    window.location="example.com";
}

// Checks incoming url is from the "Full Site" link on mobile home page and creates a cookie to remember choice.
if (window.location.href.indexOf("index=1") > -1) {
 	createCookie('mobileSite', 'true', 7);
}

function wireless () {

    try {
        if (window.location.href.indexOf("index=1") > -1) return false;

		if (readCookie('mobileSite') == "true" ) return false;

        userAgent = navigator.userAgent;

        if (userAgent == null) return false;

        if(userAgent.indexOf("Novarra-Vision") > -1) return true;

        if(userAgent.indexOf("iPad") > -1) return false;

        var names= ["320x", "160x", "Blazer", "Danger", "UP.Browser", "NetFront", "blackberry", "UP.Link", "CLDC", "J2ME", "AU-MIC", "PalmSource", "PalmOS", "Xiino" , "Windows CE", "Ericsson", "Avantgo", "Nokia", "Samsung", "Symbian", "SEC-SGH", "ATTWS", "Mobile", "Elaine", "OpenWeb", "LGE VX", "LGE-VX", "LGE-LG", "MOTO", "MOT-", "DoCo Mo", "HTC", "SKPD0", "SKPB0", "webOS", ];
        for (var x in names ) {
            if(userAgent.indexOf(names[x]) > -1) return true;

            if (names[x] == "blackberry")
                if (userAgent.toLowerCase().indexOf(names[x]) > -1) return true;

        } //* for

    } catch (ex) { }

    return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie('mobileSite', '', -1);
}