﻿// JScript File

function OnLoad()
{
    var agt = navigator.userAgent.toLowerCase();    
    var is_iphone = (agt.indexOf('iphone') != -1);
    var is_android = (agt.indexOf('android') != -1);
    var is_winmobile = (agt.indexOf('windows phone') != -1);
    var is_winphone7 = (agt.indexOf('windows phone os 7') != -1);    
    // look for 'mobile safari' AND 'blackberry' 
    var is_blackberry = ((agt.indexOf('mobile safari') != -1) && (agt.indexOf('blackberry') != -1));


//alert(document.referrer);
var docURL = document.URL;
var mobileCAP = (docURL.toLowerCase().indexOf('mobilecap') != -1);

if (mobileCAP)
{
    var mobileXXX = "mobileCAP ";
}
else
{
    var mobileXXX = "mobileRCC ";
}
//alert(mobileXXX);

var referrer = QueryString("referrer");
//alert(referrer);

    if (is_iphone)
    {    
        if (document.referrer.indexOf('captionedtext') == -1)            
        {
            // PROD
            window.location.href = "http://www.captionedtext.com/client/mobileProfile.aspx?mobileRef=" + mobileXXX;
            // QA
            //window.location.href = "http://captionedtext.qa.captcolo.com/client/mobileProfile.aspx?mobileRef=" + mobileXXX;
        }
    }

    if (is_android)
    {
	if (referrer != 'captionedtext')
	{
            // PROD
            window.location.href = "http://www.captionedtext.com/client/androidProfile.aspx?mobileRef=" + mobileXXX;
            // QA
            //window.location.href = "http://captionedtext.qa.captcolo.com/client/androidProfile.aspx?mobileRef=" + mobileXXX;
	}
    }

    if (is_winmobile)
    {
	if (referrer != 'captionedtext')
	{
            // check for windows phone 7
            if (is_winphone7)
            {
                // PROD
                window.location.href = "http://www.captionedtext.com/client/winphone7Profile.aspx?mobileRef=" + mobileXXX;
                // QA
                //window.location.href = "http://captionedtext.qa.captcolo.com/client/winphone7Profile.aspx?mobileRef=" + mobileXXX;
            }
            else
            {
                // PROD
                window.location.href = "http://www.captionedtext.com/client/winmobileProfile.aspx?mobileRef=" + mobileXXX;
                // QA
                //window.location.href = "http://captionedtext.qa.captcolo.com/client/winmobileProfile.aspx";
            }
	}
    }
    
    if (is_blackberry)
    {
        if (document.referrer.indexOf('captionedtext') == -1)            
        {
            // PROD
            window.location.href = "http://www.captionedtext.com/client/blackberryProfile.aspx?mobileRef=" + mobileXXX;
            // QA
            //window.location.href = "http://captionedtext.qa.captcolo.com/client/blackberryProfile.aspx?mobileRef=" + mobileXXX;
        }
    }
}


function QueryString(key)
{
	var url = window.location.href;
	var index = url.indexOf("?");
	var queryString = url.substr(index + 1);
	var url = url.substr(0, index + 1);
	var pairs = queryString.split("&");
	for(index = 0; index < pairs.length; index++)
	{
		var term = pairs[index];
		var pair = term.split("=");
		if(pair[0].toLowerCase() == key.toLowerCase())
		{
			return pair[1];
		}
	}
}



