//var uri = "/Default.aspx";
var uri = "/DefaultProxy3.aspx";
var cX = 0; var cY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d) 
{
d.style.left = (cX-125) + "px";
d.style.top = (cY+775) + "px";
}

function AssignPositionFF(d) 
{
d.style.left = (cX-40) + "px";
d.style.top = (cY-390) + "px";
}


function HideContent(d, posContent) {
if(d.length < 1) { return; }
//document.getElementById(d).style.display = "none";
document.getElementById(d).style.visibility = "hidden";
}
function ShowContent(d, posContent) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
if(posContent)
{
    AssignPosition(dd);
}
//dd.style.display = "inline-block";
dd.style.visibility = "visible";
}



function HideContent2FF(d, posContent) 
{
    if(d.length < 1) { return; }
    document.getElementById(d).style.visibility = "hidden";
}

function HideContent2(d, posContent) 
{
    if(d.length < 1) { return; }
    document.getElementById(d).style.display = "none";
}
function ShowContent2(d, posContent) 
{
    if(d.length < 1) 
    { 
        return; 
    }
    var dd = document.getElementById(d);
    if(posContent)
    {
        AssignPosition(dd);
    }
    dd.style.display = "block";
}

function ShowContent2FF(d, posContent) 
{
    if(d.length < 1) 
    { 
        return; 
    }
    var dd = document.getElementById(d);
    if(posContent)
    {
        AssignPositionFF(dd);
    }
    dd.style.visibility = "visible";
}

function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
//AssignPosition(dd);
if(dd.style.display == "none") { dd.style.display = "block"; }
else { dd.style.display = "none"; }
}


function RegisterValidate()
{
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    var re = /^\w+$/;
    
    if (d('txtUsername').value == "")
	{
		alert('Username cannot be empty');
		return;
	}
	else if (!re.test(d('txtUsername').value))
	{
	    alert("Username can contain only letters, numbers and underscores.");
	    return;
	}
	else if (!filter.test(d('txtEmail').value))
	{
		alert('Please enter a valid email address.');
		return;
	}
    else if (d('txtPassword').value == "")
	{
		alert('Password cannot be empty');
		return;
	}	
	else if (d('txtPassword').value != d('txtConfirmPassword').value)
	{
		alert('Passwords do not match.');
		return;
	}	
		
	d('lblError').innerHTML = 'Registering...';
    s('lblError').display = '';
	var data = "a=register&tc=1&u=" + d('txtUsername').value + "&p=" + d('txtPassword').value + "&e=" + d('txtEmail').value;	
	SendData(data);
}




var httpreq = null;
var httpreqGet = null;

function start()
{
	httpreq = new getHTTPObject();
	if (httpreq == null)
	{
		alert("Sorry your browser does not support XMLHTTPObject");
		document.location = "about:blank";
	}
	else
	{
	}
}

function getHTTPObject()
{
	var xmlhttp = null;
	var ie5 = document.all && document.getElementById;
	var ns6 = document.getElementById && !document.all;
	if(ns6)
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp = null;
		}
	}
	else
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			xmlhttp = null;
		}
	}
	return xmlhttp;
}

function Login()
{	
    d('ml_error').innerHTML = 'Logging in';
    s('ml_error').display = '';
	var data = "a=login&tc=1&u=" + d('ml_username').value + "&p=" + d('ml_password').value;	
	SendData(data);
}



function RequestPassword()
{
    d('fp_msg').innerHTML = 'Validating username...';
    s('fp_msg').display = '';
	var data = "a=RequestPwd&tc=1&u=" + d('fp_username').value;
	SendData(data);
}

var sendDataWatchTimer = null;
var sendDataWatchDelay = 5000;
var lastDataStr = null;
var lastUriParam = null;

function RetrySendData()
{
    //alert('RetrySendData : ' + lastDataStr);
    SendData(lastDataStr, lastUriParam);
}

function watchSendData(d)
{
    lastDataStr = d;
    if(sendDataWatchTimer != null)
    {
        clearTimeout(sendDataWatchTimer);
    }
	sendDataWatchTimer = setTimeout("RetrySendData()", sendDataWatchDelay);
}

function SendData(params, uriParam)
{ 
    try
    {
        if (httpreq.readyState === 4 || httpreq.readyState === 0) 
        {               
            var lRand = Math.random();
            if(uriParam != null) // for CRM Demo only
            {
                httpreq.open("POST", uriParam + "?r=" + lRand, true);
                lastUriParam = uriParam;
            }
            else
            {
                httpreq.open("POST", uri + "?r=" + lRand, true);
            }
            httpreq.onreadystatechange = ReadyHandler;
            httpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	            
            httpreq.send(params);
            lastDataStr = params;
        }
    }
    catch(e)
    {
        //alert(httpreq.readyState);
        //throw e;
    }
}

function ReadyHandler()
{
	if (httpreq.readyState == 4)
	{
		if (httpreq.status == 200) 
		{
			str = httpreq.responseText;
			str = str.replace(/\</g, "&lt;");
			str = str.replace(/\>/g, "&gt;");
			try
			{	
			    if(httpreq.responseText != "")
			    {
			        clearTimeout(sendDataWatchTimer);		
			        eval(httpreq.responseText);		    
			    }	
			    else
			    {
			        RetrySendData();
			    }		    
			}
			catch (e)
			{
				//throw e;
			}
		}
		else
		{
		}		
	}
	return;
}



function showloginarea()
		{
			var login = document.getElementById('login')
			var forgotpwd = document.getElementById('forgotpwd')
			var signup = document.getElementById('signup')
			login.style.display="block";
			forgotpwd.style.display="none";
			signup.style.display="none";
		}


	function showsignuparea()
		{
			var login = document.getElementById('login')
			var forgotpwd = document.getElementById('forgotpwd')
			var signup = document.getElementById('signup')
			login.style.display="none";
			forgotpwd.style.display="none";
			signup.style.display="block";
		}


	function showforgotpwdarea()
		{
			var login = document.getElementById('login')
			var forgotpwd = document.getElementById('forgotpwd')
			var signup = document.getElementById('signup')
			login.style.display="none";
			forgotpwd.style.display="block";
			signup.style.display="none";
		}

	function gotoskrbl()
		{
			//alert("start skrbl")
			document.location.href = '/start?';
		}


	function gotofeedback()
			{
				alert("go to feedback ")
		}

	function gotoblog()
				{
					alert("go to blog ")
			}
	function d(tag_id)
    {
	    return document.getElementById(tag_id);
    }		
    
    function s(tag_id)
    {
	    return document.getElementById(tag_id).style;
    }

    function submit_ml()
    {        
        d('actionField').value='login';
        d('homepage').submit();
    }
    
    function OpenAltSessionID()
    {
        if(d('altSessionID').value == "")
        {
            alert("Please enter a session ID");
        }
        else
        {
            document.location.href = '/'+ d('altSessionID').value;
        }
    }
    
    function OpenAltSessionID2()
    {
        if(d('altSessionID2').value == "")
        {
            alert("Please enter a session ID");
        }
        else
        {
            document.location.href = '/'+ d('altSessionID2').value;
        }
    }
    
    function SubmitSessionPwd()
    {
        document.cookie='wb_pwd_'+ d('currentSessionId').value + '=' + d('ppSkrblPwdText').value + ';path=/';
        document.location.href = document.location.href;
    }
    
    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 startNewSkrbl()
    {
        var userid = "";
        if(getCookieValue('AppUsername'))
        {
            userid = getCookieValue('AppUsername');
        }
        
        var expireStr = 'expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
        try{document.cookie = 'tokenHostGuid=;'+ expireStr;}catch(e){}
        
        document.location.href = '/start?appid=skrbl&appuserid=' + userid;
    }
    
    function userNotHost()
    {
    }
    
    /*==============================================================================
    http://www.braemoor.co.uk/software/cookies.shtml
    Routine to get the current value of a cookie
        Parameters:
            cookieName        Cookie name    
        Return value:
            false             Failed - no such cookie
            value             Value of the retrieved cookie

       e.g. if (!getCookieValue("pans") then  {
               cookieValue = getCoookieValue ("pans2);
            }
    */

    function getCookieValue (cookieName) 
    {
      var exp = new RegExp (escape(cookieName) + "=([^;]+)");
      if (exp.test (document.cookie + ";")) {
        exp.exec (document.cookie + ";");
        return unescape(RegExp.$1);
      }
      else return false;
    }
    
    
    function GoToSkrblConference()
    {
        var strUserName = getCookieValue('AppUsername');
        
        if(strUserName != false)
        {
            document.getElementById('hidUserName').value = strUserName;        
        }
        else
        {
            document.getElementById('hidUserName').value = "";
        }
        
        document.getElementById('TweeboForm').action = "http://www.tweebo.com/ScheduleConference.aspx";
        document.getElementById('TweeboForm').submit();
    }
    
    
    /////////////////////////////////////////////skrbl for groups//////////////////////////////////
    function GroupLogin()
    {
        if (d('txtUsername').value == "")
	    {
		    alert('Username cannot be empty');
		    return;
	    }
	    else if (d('txtGroupname').value == "")
	    {
		    alert('Groupname cannot be empty');
		    return;
	    }
	    
        //d('ml_error').innerHTML = 'Logging in';
        //s('ml_error').display = '';
        var data = "a=grouplogin&tc=1&u=" + d('txtUsername').value 
        + "&p=" + d('txtPassword').value
        + "&gpn=" + d('txtGroupname').value;        
        SendData(data);   
    }
        
    /*
    function CheckGrpName()
    {
        //come back and tell the user if the group is already taken
        if(d('ml_group').value != "")
        {
            var data = "a=checkgpname&tc=1&gp=" + d('ml_group').value;              
            SendData(data);   
        }
    }
    */
    
    var re_2 = /^\w+$/;
    function RegisterGroupValidate()
    {
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;        
        var re = /^\w+$/; //alphanum and underscores
        var re2 =/[0-9A-Za-z]*/; //alphanum
        if (d('txtUsername').value == "")
	    {
		    alert('Username cannot be empty');
		    return;
	    }
	    else if (!re.test(d('txtUsername').value))
	    {
	        alert("Username can contain only letters, numbers and underscores.");
	        return;
	    }
	    else if (!filter.test(d('txtEmail').value))
	    {
		    alert('Please enter a valid email address.');
		    return;
	    }	    
	    else if (d('txtGroupname').value == "")
	    {
		    alert('Group name cannot be empty');
		    return;
	    }
	    else if (!re.test(d('txtGroupname').value))
	    {
	        alert("Group name can contain only letters and numbers.");
	        return;
	    }
        else if (d('txtPassword').value == "")
	    {
		    alert('Password cannot be empty');
		    return;
	    }	
	    else if (d('txtPassword').value != d('txtConfirmPassword').value)
	    {
		    alert('Passwords do not match.');
		    return;
	    }	
    		
	    //d('lblError').innerHTML = 'Registering...';
        //s('lblError').display = '';
	    var data = "a=groupregister&tc=1&u=" + d('txtUsername').value 
	    + "&p=" + d('txtPassword').value 
	    + "&e=" + d('txtEmail').value	
	    + "&gpn=" + d('txtGroupname').value
	    + "&cpn=" + d('txtOrgname').value
	    + "&actype=" + "T0" + "&skypeID=" + d('txtSkypeID').value;
	    SendData(data);
    }
    
    function ProcessGpUserLogin(uId, gpId, loginGuid, sessionId, userType)
    {
        //s('ml_error').display = 'none';
        document.cookie='AppUsername=' + uId + ';path=/';
        document.cookie='AppID=' + "skrblGroups" + ';path=/';
        document.cookie='Groupname=' + gpId + ';path=/';
        document.cookie='token=' + loginGuid + ';path=/';
        document.cookie='UserType=' + userType + ';path=/';
        //document.cookie=sessionId+'=1;path=/';    
        
        //document.location.href = '/start?appid=skrblgroups&appuserid='+uId + '&groupid=' + gpId + '&userType=' + userType;
        //document.location.href = '/' + sessionId;
        
    }
    
    function CatchLoginEnter(e) 
    {
	    if (!e) 
	        e = window.event;
    	    
	    if (e.keyCode) 
		    code = e.keyCode;		
	    else if (e.which) 
	        code = e.which;
    	    
	    if(code == 13) 
        {            
            GroupLogin();
        }
    }
    
    function focusinput()
    {
        try
        {
            if(d('txtGroupname').value == "")
            {
                d('txtGroupname').focus();
            }
            else
            {
                d('txtUsername').focus();
            }
        }catch(e) {}
    }