﻿var arrInputs;
var sErrMsg = '';
var nErrIndex = null;
var fTwited = readCookie('twited');

if (fTwited == '1') document.getElementById("tweetcontainer").style.display = 'none';

function tweetform_button_click(btn) {
    if (arrInputs == null) {
        arrInputs = new Array();
        arrInputs[0] = document.forms[0]['twitterform_follow'];
        arrInputs[1] = document.forms[0]['twitterform_username'];
        arrInputs[2] = document.forms[0]['twitterform_password'];
        arrInputs[3] = document.forms[0]['tweetform_button'];

    }
    createXMLHttpRequest();
    if (xmlHTTP) {
        if (processTweet()) btn.disabled = true;
    }
} //end tweetform_button_click

function processTweet() {
    nErrIndex = null;
    sErrMsg = '';
    if (arrInputs[1].value == null || arrInputs[1].value == '') {
        sErrMsg += "Please enter a valid Userame.\n";
        if (nErrIndex == null) nErrIndex = 1;
    }

    if (arrInputs[2].value.length < 6) {
        sErrMsg += "Please enter a valid Password.";
        if (nErrIndex == null) nErrIndex = 2;
    }

    if (sErrMsg.length > 0) {
        alert(sErrMsg);
        arrInputs[nErrIndex].focus();
        arrInputs[nErrIndex].select();
        return false;
    }

    var url;
    url = 'TweetHandler.ashx?follow='
    url += escape(arrInputs[0].checked);
    url += '&name='
    url += escape(arrInputs[1].value);
    url += '&pass='
    url += escape(arrInputs[2].value);

    xmlHTTP.open('GET', url, true)
    xmlHTTP.onreadystatechange = doUpdate;
    xmlHTTP.send(null);

    return true;
} //end processTweet

function doUpdate() {
    if (xmlHTTP.readyState != 4) return;
    nErrIndex = null;
    sErrMsg = '';
    var sResponse = xmlHTTP.responseText;
    var mySplitResult = sResponse.split('|');
    if (mySplitResult.length != 3) {
        alert('We\'re sorry.\nThere was an error processing your information.\n');
        return;
    }

    if (mySplitResult[0] != 'brokenrules') {
        alert(mySplitResult[1]);
        if (mySplitResult[0] == 'ok') {
            document.getElementById("tweetcontainer").style.display = 'none';
            createCookie('twited', '1', 1);
            window.location = mySplitResult[2]
        }
        return;
    }
    else {
        if (mySplitResult[1] == 'name') {
            sErrMsg += "Please enter a valid Username.\n";
            if (nErrIndex == null) nErrIndex = 1;
        }
        if (mySplitResult[1] == 'passwd') {
            sErrMsg += "Please enter a valid Password.";
            if (nErrIndex == null) nErrIndex = 2;
        }
        if (sErrMsg.length > 0) {
            alert(sErrMsg);
            arrInputs[nErrIndex].focus();
            arrInputs[nErrIndex].select();
            arrInputs[3].disabled = false;
            return false;
        }
    }

} //end doUpdate

