function setWAPCookie(wapVar){
    document.cookie='wappref='+wapVar+';path=/'; 
}
var wapUserAgent = navigator.userAgent;
var wapDestURL = window.location.href;
if(wapDestURL.match(/wappref=wap/)){
        setWAPCookie('wap');
}
else if(wapDestURL.match(/wappref=std/)){
        setWAPCookie('std');
}
var wapAllcookies = document.cookie;

function testForWap(wapURL){
    document.write('<!-- ran testForWAP -->');
    if(wapDestURL.match(/[http:\/\/]*[^\/]+\/[^\/]+/)){
            // noop - just serve the page quietly
        document.write('<!-- deeplink -->');
        
    }
    else if(wapAllcookies.match(/wappref=std/)){
            // noop - just serve the page quietly
        document.write('<!-- std WAP pref detected -->');
    }
    else if(wapAllcookies.match(/wappref=wap/)){
        window.location=wapURL;
    } 
    else if( // no cookie
        (wapUserAgent.match(/iPhone/i)) ||
        (wapUserAgent.match(/Android/i)) ||
        (wapUserAgent.match(/Windows Phone OS 7/i))
         ) { 
                window.location="/choosewap.html?thisurl="+wapDestURL;
    }
}

