function $(value){ return document.getElementById(value);}
var xmlhttp;

function sendWelcomeTweet(twitterID)
{
        //alert('sending tweet');
        //alert('http://www.cbjobtweets.php/ajax/sendWelcomeMessage.php?toAccount='+twitterID);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/sendWelcomeTweet.php?toAccount='+twitterID, handleResponse_global);
}

function sendTestTweet(twitterID, clientID)
{
	if(clientID == null){clientID = -1;}
        //alert('sending tweet');
        //alert('http://www.cbjobtweets.com/ajax/sendWelcomeMessage.php?toAccount='+twitterID);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/sendTestTweet.php?toAccount='+twitterID+'&ClientID='+clientID, handleResponse_global);
        toggleAlert("a test tweet was sent to your account");
}

function registerUser(twitterID, searchTerms, searchLocation, searchFreq, clientID)
{

	if(clientID == null){clientID = -1;}
       //  alert('http://www.cbjobtweets.com/ajax/registerUser.php?TwitterName='+twitterID+'&SearchTerms='+escape(searchTerms)+'&SearchLocation='+escape(searchLocation)+'&SearchFreq='+searchFreq+'&ClientID='+clientID);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/registerUser.php?TwitterName='+twitterID+'&SearchTerms='+escape(searchTerms)+'&SearchLocation='+escape(searchLocation)+'&SearchFreq='+searchFreq+'&ClientID='+clientID, handleResponse_global);
	toggleAlert('Your search has been saved.  You will receive a notification message shortly.');
}

/*function checkUsersFollowingStatus(twitterID)
{
	//alert('http://www.cbjobtweets.com/ajax/checkUsersFollowingStatus.php?toAccount='+twitterID);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/checkUsersFollowingStatus.php?toAccount='+twitterID, handleResponse_IsFollower);
}*/

function checkUsersFollowingStatus(twitterID, clientID)
{
	if(clientID == null){clientID = -1;}
//	alert('http://www.cbjobtweets.com/ajax/checkUsersFollowingStatus.php?toAccount='+twitterID+'&ClientID='+clientID);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/checkUsersFollowingStatus.php?toAccount='+twitterID+'&ClientID='+clientID, handleResponse_IsFollower);
}

function getJobResults(searchKeywords, searchLocation, clientID)
{
	if(clientID == null){clientID = -1;}
	var cleanKeywords = escape(searchKeywords);
	if(searchKeywords == 'Enter search keywords'){cleanKeywords = ''}
	var cleanLocation = escape(searchLocation);
	if(searchLocation == 'Enter job location'){cleanLocation = ''}
	//alert('http://www.cbjobtweets.com/ajax/getJobResults.php?KeyWords=' + cleanKeywords + '&Loction=' + cleanLocation);
        makeAjaxCall_global('http://www.cbjobtweets.com/ajax/getJobResults.php?SearchKeywords=' + cleanKeywords + '&SearchLocation=' + cleanLocation+'&ClientID='+clientID, handleResponse_GetJobResults);
}


function clearDefaultText(field, defaultText)
{
	if(field.value == defaultText)
	{
		field.value = '';
	}
}

function toggleAlert(newValue, newWidth)
{
	var myDiv = document.getElementById('alertDiv');
	var formDiv = document.getElementById('alertContent');
	var InnerDiv = document.getElementById('alertDivInner');
	if(newWidth == null || newWidth == '')
	{
		newWidth = 550;
	}

	if(myDiv.style.visibility == 'hidden')
	{
		myDiv.style.visibility = 'visible';
		myDiv.style.top = document.body.scrollTop; 
		myDiv.style.height = document.body.scrollHeight;
		InnerDiv.style.top = document.body.scrollTop + (document.body.clientHeight / 15);
		formDiv.style.width = newWidth;
		formDiv.innerHTML = newValue;
	}else{
		myDiv.style.visibility = 'hidden';
	}
}

function resetDefaultText(field, defaultText)
{
	if(field.value == '')
	{
		field.value = defaultText;
		return false;
	}else{
		return true;
	}
}

function showSection4(value)
{
	if(value != '')
	{
		$('section4').className = 'active';
	}
}

/*
*  AJAX SUPPORT FUNCTION
*/
function makeAjaxCall_global(url, callback) {
        if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

   	if (arguments.length==3) async=true;

        xmlhttp.onreadystatechange = callback;
        xmlhttp.open("GET", url, true);

        xmlhttp.send(null);
}

/*
*  AJAX SUPPORT FUNCTION
*/
function handleResponse_global()
{
                // only if req shows "complete"
                if (xmlhttp.readyState == 4) {
                           // only if "OK"
                                if (xmlhttp.status == 200) {
                                         // ...processing statements go here...
                                  //response = xmlhttp.responseXML.documentElement;
                                        //document.getElementById('storyline').innerHTML = xmlhttp.responseText;
                                } else {
                                        //document.getElementById('result').innerHTML = theUrl + "<br><br>" + theXml + "<br><br>" + xmlhttp.responseText;
                                        //alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
                                }
                }
}

/*
 * AJAX handler that takes success/fail and determines next course of action
 */

function handleResponse_IsFollower()
{
                // only if req shows "complete"
                if (xmlhttp.readyState == 4) {
                           // only if "OK"
                                if (xmlhttp.status == 200) {
                                         // ...processing statements go here...
                                  	//response = xmlhttp.responseXML.documentElement;
                                        //document.getElementById('storyline').innerHTML = xmlhttp.responseText;
                                	if(xmlhttp.responseText == true)
					{
						$('section2_actionnotrequired').style.visibility = 'visible';
						$('section2_actionnotrequired').style.display = '';
						$('section2').className = 'active';
						$('section3').className = 'active';
						$('saveButton').className = 'active';
						$('section2_actionrequired').style.visibility = 'hidden';
						$('section2_actionrequired').style.display = 'none';
						//alert('This account is a follower');
					}else{
						$('section2').className = 'active';
						$('section3').className = 'inactive';
						$('saveButton').className = 'inactive';
						//$('jobResults').innerHTML = 'job results go here';
						$('section2_actionrequired').style.visibility = 'visible';
						$('section2_actionrequired').style.display = '';
						$('section2_actionnotrequired').style.visibility = 'hidden';
						$('section2_actionnotrequired').style.display = 'none';
						//alert('This account is NOT a follower');
					}
				} else {
                                        //document.getElementById('result').innerHTML = theUrl + "<br><br>" + theXml + "<br><br>" + xmlhttp.responseText;
                                        //alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
                                }
                }
}



function handleResponse_GetJobResults()
{
        // only if req shows "complete"
	if (xmlhttp.readyState == 4) {
		// only if "OK"
		if (xmlhttp.status == 200) {
			//alert('got something back');
			tempTable = document.createElement('div');
			tempTable.innerHTML = '<table style="font-size:12px;" width="500">' + xmlhttp.responseText + '</table>';
			var node = $('jobResults');
			   var len = node.childNodes.length;

   			for(var i = 0; i < len; i++)
   			{
   			   node.removeChild(node.childNodes[i]);
   			}
			
			node.appendChild(tempTable);
		} else {
		}
	}
}
