var SSOhandlerPath = "http://ssl.dstv.com/connect/SSO/SSOHandler.ashx"; 
var glob_SSOreturnUrl = "";
var glob_SSOTimeOut = 12000;


function SSOResolve(_SSOLocalHandlerPath, _returnUrl, _beginCallback, _timeoutCallback) {
    $(function () {

	if (_returnUrl != null && _returnUrl != "")
	    glob_SSOreturnUrl = _returnUrl;

        if (typeof (_beginCallback) !== 'undefined' && _beginCallback != null)
            _beginCallback();
        $.ajax({
            url: SSOhandlerPath,
            data: { method: "SSOResolve", SSOLocalHandlerPath: _SSOLocalHandlerPath },
            timeout: glob_SSOTimeOut,
            cache: false,
            dataType: 'jsonp',
            success: function (data) {
            },
			timeout: function(){
			 if (typeof (_timeoutCallback) !== 'undefined' && _timeoutCallback != null)
            _timeoutCallback();
			}
        });
    });
}


function SSOLogin(_un, _pw, _sessionExpireInMinutes, _returnUrl, _SSOLocalHandlerPath, _beginCallback, _errorCallback, _timeoutCallback) {
    $(function () {
	   if (typeof (_beginCallback) !== 'undefined' && _beginCallback != null)
            _beginCallback();
        $.ajax({
            url: SSOhandlerPath,
            data: { method: "SSOLogin", un: _un, pw: _pw, sessionExpireInMinutes: _sessionExpireInMinutes, SSOLocalHandlerPath: _SSOLocalHandlerPath, site: _returnUrl },
            timeout: glob_SSOTimeOut,
            cache: false,
            dataType: 'jsonp',
            success: function (data) {
                if (data.SSOLogin == 'true') {
				   ResolveToken(data.tokenID, _SSOLocalHandlerPath, _returnUrl);
                } else {
                    if (typeof (_errorCallback) !== 'undefined' && _errorCallback != null)
                        _errorCallback(data);
                }
            },
			timeout: function(){
			 if (typeof (_timeoutCallback) !== 'undefined' && _timeoutCallback != null)
            _timeoutCallback();
			}
        });
    });
}


function SSOLogout(logoutPath, _beginCallback, _timeoutCallback) {
    $(function () {
	 if (typeof (_beginCallback) !== 'undefined' && _beginCallback != null)
            _beginCallback();
        $.ajax({
            url: SSOhandlerPath,
            data: { method: "SSOLogout" },
            timeout: glob_SSOTimeOut,
            cache: false,
            dataType: 'jsonp',
            success: function (data) {
                window.location = logoutPath;
            },
			timeout: function(){
			 if (typeof (_timeoutCallback) !== 'undefined' && _timeoutCallback != null)
            _timeoutCallback();
			}
        });
    });
}


function ResolveToken(Token, _SSOLocalHandlerPath, _returnUrl, _errorCallback, _timeoutCallback) {

	if (glob_SSOreturnUrl != null && glob_SSOreturnUrl != "")
	    _returnUrl = glob_SSOreturnUrl;

    $(function () {
	   if (typeof _resolveTokenCallback == 'function')
                    _resolveTokenCallback();
        $.ajax({
            url: _SSOLocalHandlerPath,
            data: { method: "ResolveToken", authToken: Token },
            timeout: glob_SSOTimeOut,
            cache: false,
            success: function (data) {
                if (data == "true") {
				if(_returnUrl != null)
				    //window.location = _returnUrl;
					window.open(_returnUrl,'_parent');
					//post(_returnUrl);
				else
                    //window.location = window.location;
					  window.parent.location = window.location;
					//window.open(window.location,'_parent'); 
					//post(window.location);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
			       if (typeof (_errorCallback) !== 'undefined' && _errorCallback != null)
                        _errorCallback(errorThrown);
            },
			timeout: function(){
			 if (typeof (_timeoutCallback) !== 'undefined' && _timeoutCallback != null)
            _timeoutCallback();
			}
        });
    });
}

