
function addWebURL (cid) {
	var url = encodeURIComponent(document.getElementById('webURL').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=web&item=URL&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addWebQuery (cid) {
	var url = encodeURIComponent(document.getElementById('webQuery').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=web&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addYoutubeQuery (cid) {
	var url = encodeURIComponent(document.getElementById('youtubeQuery').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=youtube&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addYoutubeChannel (cid) {
	var url = 'CHANNEL:'+encodeURIComponent(document.getElementById('youtubeChannel').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=youtube&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addBlogsQuery (cid) {
	var url = encodeURIComponent(document.getElementById('blogsQuery').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=blogs&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addTwitterQuery (cid) {
	var query = (document.getElementById('twitterQuery')) ? document.getElementById('twitterQuery').value : '';
	var user = (document.getElementById('twitterUser')) ? document.getElementById('twitterUser').value : '';
	var hash = (document.getElementById('twitterHash')) ? document.getElementById('twitterHash').value : '';
	var reply = (document.getElementById('twitterReply')) ? document.getElementById('twitterReply').value : '';
	var mention = (document.getElementById('twitterMention')) ? document.getElementById('twitterMention').value : '';
	var source = (document.getElementById('twitterSource')) ? document.getElementById('twitterSource').value : '';

	if (query == '') {
		document.getElementById('message').innerHTML =
			'Your query cannot be blank. All the other fields are optional.<br />';
	} else {
		var bigQuery = query;
		if (user != '') {
			bigQuery += ' from:' + user;
		}
		if (hash != '') {
			bigQuery += ' #' + hash.replace('#', '');
		}
		if (reply != '') {
			bigQuery += ' to:' + reply.replace('@', '');
		}
		if (mention != '') {
			bigQuery += ' @' + mention.replace('@', '');
		}
		if (source != '') {
			bigQuery += ' source:' + source;
		}
		bigQuery = encodeURIComponent(bigQuery);
		var serverScript = 'addoption.php?cid='+cid+'&type=twitter&item=query&url='+bigQuery;
		req = new phpRequest(serverScript);
		var response = req.execute();
	}
}

function addFlickrQuery (cid) {
	var url = encodeURIComponent(document.getElementById('flickrQuery').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=flickr&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function addCommonQuery (cid) {
	var url = encodeURIComponent(document.getElementById('commonQuery').value);
	var serverScript = 'addoption.php?cid='+cid+'&type=common&item=query&url='+url;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function updateOption (type, cid, ID) {
	switch (type) {
		case 'webURL':
			var elementID = 'webURLValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=web&webID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'webQuery':
			var elementID = 'webQueryValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=web&webID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'youtubeQuery':
			var elementID = 'youtubeQueryValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=youtube&youtubeID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'blogsQuery':
			var elementID = 'blogQueryValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=blog&blogID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'twitterQuery':
			var elementID = 'twitterQueryValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=twitter&twitterID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'flickrQuery':
			var elementID = 'flickrQueryValue_'+ID;
			var value = document.getElementById(elementID).value;
			var serverScript = 'updateoption.php?cid='+cid+'&type=flickr&flickrID='+ID+'&value='+value;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
	}
}

function itemAction (type, cid, ID, action) {
	switch (type) {
		case 'webURL':
			var serverScript = 'itemaction.php?cid='+cid+'&type=web&item=URL&webID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'webQuery':
			var serverScript = 'itemaction.php?cid='+cid+'&type=web&item=query&webID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'youtubeQuery':
			var serverScript = 'itemaction.php?cid='+cid+'&type=youtube&item=query&youtubeID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'blogsQuery':
			var serverScript = 'itemaction.php?cid='+cid+'&type=blog&item=query&blogID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'twitterQuery':
			var serverScript = 'itemaction.php?cid='+cid+'&type=twitter&item=query&twitterID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
		case 'flickrQuery':
			var serverScript = 'itemaction.php?cid='+cid+'&type=flickr&item=query&flickrID='+ID+'&action='+action;	
			req = new phpRequest(serverScript);
			var response = req.execute();
			break;
	}
}

function itemFrequency (type, item, cid, ID, freq) {
	var serverScript = 'itemfreq.php?cid='+cid+'&type='+type+'&item='+item+'&ID='+ID+'&freq='+freq;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function inlinksUpdate (type, ID) {
	var serverScript = 'inlinksupdate.php?type='+type+'&ID='+ID;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function criteriaUpdate (criteria, ID) {
	var serverScript = 'criteriaupdate.php?criteria='+criteria+'&ID='+ID;
	req = new phpRequest(serverScript);
	var response = req.execute();
}

function everytimeUpdate (type, ID, value) {
	var serverScript = 'everytimeupdate.php?type='+type+'&ID='+ID+'&value='+value;	
	req = new phpRequest(serverScript);
	var response = req.execute();
}

//Start phpRequest Object
function phpRequest(serverScript) {
	//Set some default variables
	this.parms = new Array();
	this.parmsIndex = 0;

	//Set the server url
	this.server = serverScript;

	//Add two methods
	this.execute = phpRequestExecute;
	this.add = phpRequestAdd;
}

function phpRequestAdd(name,value) {
    //Add a new pair object to the params
    this.parms[this.parmsIndex] = new pair(name,value);
    this.parmsIndex++;
}

function phpRequestExecute() {
    //Set the server to a local variable
    var targetURL = this.server;

    //Try to create our XMLHttpRequest Object
    try {
        var httpRequest = new XMLHttpRequest();
    }
    catch (e) {
//        alert('Error creating the connection!');
        return;
    }

    //Make the connection and send our data
    try {
        var txt = "?1";
        for(var i in this.parms) {
            txt = txt+'&'+this.parms[i].name+'='+this.parms[i].value;
        }
        //Two options here, only uncomment one of these
        //GET REQUEST
		var currentURL = targetURL+txt;
//		if (currentURL != lastURL) {
//			lastURL = currentURL;
	        httpRequest.open("GET", currentURL, false, null, null);  
	        httpRequest.send('');			
//		}		
    }
    catch (e) {
//        alert('An error has occured calling the external site: '+e);
        return false;
    }

    //Make sure we received a valid response
    switch(httpRequest.readyState) {
        case 1,2,3:
 //           alert('Bad Ready State: '+httpRequest.status);
            return false;
            break;
        case 4:
            if(httpRequest.status !=200) {
 //               alert('The server respond with a bad status code: '+httpRequest.status);
                return false;
            }
            else {
                var response = httpRequest.responseText;
            }
            break;
    }
    return response;
}
