function runQuery(a)
{
    var search = document.getElementById('ctl00_mainBody_txtSearch').value;
    a.setAttribute('href','http://search.twitter.com/search?q=' + escape(search))
}

function searchChange(search)
{
    var search = document.getElementById('ctl00_mainBody_txtSearch').value;
    search = search.replace(/and/i, " ");
    search = search.replace(" or ", " OR ");
    document.getElementById('ctl00_mainBody_txtSearch').value = search;
}

function domainCheck(domain)
{
    try
    {
        var topLocation = top.location.href;
        var thisLocation = location.href;
        var temp = topLocation.split('/');
        if(temp.length >= 3)
        {
            var topDomain = temp[2];
        }
        temp = thisLocation.split('/');
        if(temp.length >= 3)
        {
            var thisDomain = temp[2];
        }
        
        topDomain = topDomain.toLowerCase();
        topDomain = topDomain.replace("www.","");
        thisDomain = thisDomain.toLowerCase();
        thisDomain = thisDomain.replace("www.","");
        
        if(thisDomain != topDomain)
        {
            if(topDomain != domain)
            {
                location.href = "nodomain.htm";
            }
        }
    }
    catch(e)
    {
    }
}

function reload(code, drpSelect)
{
    if(drpSelect.selectedIndex >= 0)
    {
        var id = drpSelect.options[drpSelect.selectedIndex].value;
        top.location.href = "settings.aspx?id=" + id + "&code=" + escape(code);
    }
    else
    {
        document.getElementById("msg").innerHTML = "Nothing to select.";
    }
}

function checkNew()
{
   var id = document.getElementById("txtID").value;
   var num = document.getElementById("txtNum").value;
   var reqtoken = document.getElementById("reqtoken").value;
   var since_id = document.getElementById("since_id").value;
   
   $.ajax({
       type: "POST",
       url: "check.aspx",
       data: "id=" + id + "&reqtoken=" + reqtoken + "&since_id=" + since_id,
       success: function(msg){
         if(msg.length < 4 && msg != "0")
         {
            var msgShow = msg + " more result";
            var themit;
            if(msg == "1")
            {
                themit = "it";
            }
            else
            {
                msgShow += "s";
                themit = "them";
            }
            msgShow += ".  <a href='chatter.aspx?id=" + id + "&num=" + num + "'>Refresh</a> to see " + themit + ".";
            document.getElementById("msg").innerHTML = msgShow; 
         }
       },
       error: function(msg){
             //alert("The new tweet check failed.");
         }
     });
 }

function loginPage(login)
{
   if(!login)
   {
       document.getElementById("loginMsg").innerHTML = "";
       document.getElementById("login").style.display = "none";
       document.getElementById("create").style.display = "block";
       document.getElementById("spnEmailAgain").style.display = "block";
       document.getElementById("spnEmailAgainInput").style.display = "block";
       document.getElementById("spnPasswordAgain").style.display = "block";
       document.getElementById("spnPasswordAgainInput").style.display = "block";
   }
   else
   {
       document.getElementById("loginMsg").innerHTML = "";
       document.getElementById("login").style.display = "block";
       document.getElementById("create").style.display = "none";
       document.getElementById("spnEmailAgain").style.display = "none";
       document.getElementById("spnEmailAgainInput").style.display = "none";
       document.getElementById("spnPasswordAgain").style.display = "none";
       document.getElementById("spnPasswordAgainInput").style.display = "none";
   }
   
}

function createLogin(username, emailAgain, password, passwordAgain, objRememberme)
{
    document.getElementById("loginMsg").innerHTML = "";
    
    if(username != emailAgain)
    {
        document.getElementById("loginMsg").innerHTML = "Email and Email Again must match.";
        return;
    }
    if(password != passwordAgain)
    {
        document.getElementById("loginMsg").innerHTML = "Password and Password Again must match.";
        return;
    }
    
   
   $.ajax({
       type: "POST",
       url: "login.aspx",
       data: "username=" + username + "&password=" + password + "&create=true",
       success: function(msg){
         if(msg != "error")
         {
            login(username,password,objRememberme);
         }
         else
         {
            document.getElementById("loginMsg").innerHTML = "Account creation failed, please try again.";
         }
         
       },
       error: function(msg){
             alert("An error occurred during the account creation process.  Please try again.");
         }
     });
 }



function login(username, password, objRememberme)
{
    document.getElementById("loginMsg").innerHTML = "";
    var rememberme = "N";
    if(objRememberme.checked)
    {
        rememberme = "Y";
    }
   
   $.ajax({
       type: "POST",
       url: "login.aspx",
       data: "username=" + username + "&password=" + password,
       success: function(msg){
         if(msg != "error")
         {
            if(rememberme == "Y")
            {
                $.cookie("code",msg);
            }
            top.location.href = "settings.aspx?code=" + msg;
         }
         else
         {
            document.getElementById("loginMsg").innerHTML = "Login failed, please try again.";
         }
         
       },
       error: function(msg){
             alert("An error occurred during the login.  Please try again.");
         }
     });
 }
 
 function cookieCheck()
 {
    if($.cookie("code"))
    {
    if($.cookie("code").length > 0)
    {
        top.location.href = "settings.aspx?code=" + $.cookie("code");
    }
    }
 }

function logout()
{
    $.cookie("code",null);
    location.href="default.aspx";
}

/* GetTimeZone returns the client's UTC time zone offset. */
function GetTimeZone(){
	var tzostring;
	// tzo will be the return value.
	var tzo = "";
	var tzoarr
	//create a new date object, and use it to get the UTC Timezone Offset.
	var d = new Date();

	/* getTimezoneOffset returns the number of minutes
	   difference between Greenwich Mean Time (UTC Time) and the client's time.
		 Divide the result by 60 to get the hours offset. */
	tzostring = (d.getTimezoneOffset()/60).toString();
	
	/* For some reason, the getTimeZoneOffset gives a negative number where
	   there should be a positive, and vice-versa. So, if the result contains a negative(-) sign, 
		 prefix the return value with a plus(+) and vice versa. */
	if (tzostring.indexOf("-") > -1){
		tzostring = tzostring.replace("-", "");
		tzo += "+";
	}
	else {
		tzo += "-";
	}

/* Next, search for a decimal point. If one exists, use split() 
	   to create an array of values on each side of the decimal point. */
	if (tzostring.indexOf(".") > 0){
		// split tzostring into an array.
		tzoarr = tzostring.split(".");
		
		/* append tzo[0] (everything to the left of the decimal point)
		   to tzo. PadWithZero will pad if neccesary. */
		tzo += PadWithZero(tzoarr[0]);
		
		/* Convert tzoarr[1] (everything to the right of the decimal point)
		   from base 10 to base 60 (e.g., from 5.75, the 75 would become 45). 
			 This is necessary because the timezoneOffset value for Kathmandu would be -5.75,
			 which has to be converted to +0545 to be valid as an RFC-822 timezone offset. */
		if (parseInt(tzoarr[1]) == 5){
			tzo += "30";
		}
		else if(parseInt(tzoarr[1]) == 75){
			tzo += "45";
		}
		else if(parseInt(tzoarr[1]) == 25){
			tzo += "15";
		}
	}
	else {
		// If no decimal points, just append tzostring to tzo, and tack on 2 zeros at the end
		tzo += PadWithZero(tzostring) + "00";
	}
	return tzo;
}

/* PadWithZero returns "0" + val if the number 
   is less than 10 and greater than -1.
	 Parameter: any integer. */
function PadWithZero(val){
	val = parseInt(val)
	if (val < 10 && val > -1){
		return "0" + val;
	}
	return val;
}
